summaryrefslogtreecommitdiff
path: root/qa/src/chkacc2.c
blob: 597367c20483aad76bece230a75b3d73fd1031e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (c) 2012-2014 Red Hat.
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 */

/* Check all ops except one turned off for all combinations.
 * Check connection limits
 */

#include <pcp/pmapi.h>
#include <pcp/impl.h>
#include "localconfig.h"

int
main(int argc, char **argv)
{
    int			s, sts, op, host;
    unsigned int	i;
    char		name[4*8 + 7 + 1]; /* handles full IPv6 address, if supported */
    int			ipv4 = -1;
    int			ipv6 = -1;
    int			errflag = 0;
    int			c;
    __pmSockAddr	*inaddr;

    /* trim cmd name of leading directory components */
    __pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "46D:?")) != EOF) {
	switch (c) {

	case '4':	/* ipv4 (default) */
	    ipv4 = 1;
	    break;

	case '6':	/* ipv6 */
	    ipv6 = 1;
	    break;

	case 'D':	/* debug flag */
	    sts = __pmParseDebug(optarg);
	    if (sts < 0) {
		fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n",
		    pmProgname, optarg);
		errflag++;
	    }
	    else
		pmDebug |= sts;
	    break;

	case '?':
	default:
	    errflag++;
	    break;
	}
    }

    if (errflag) {
	fprintf(stderr,
"Usage: %s [options]\n\
\n\
Options:\n\
  -4             do IPv4 (default)\n\
  -6		 do IPv6\n",
                pmProgname);
        return 1;
    }

    /* defaults */
    if (ipv4 == -1) ipv4 = 1;
    if (ipv6 == -1) ipv6 = 0;

    sts = 0;
    for (op = 0; op < WORD_BIT; op++)
	if ((s = __pmAccAddOp(1 << op)) < 0) {
	    printf("Bad op %d: %s\n", op, strerror(errno));
	    sts = s;
	}

    if (sts < 0)
	return 1;


    for (host = 0; host < WORD_BIT; host++) {
	if (ipv4) {
	    sprintf(name, "155.%d.%d.%d", host * 3, 17+host, host);
	    if ((s = __pmAccAddHost(name, ~(1 << host), ~(1 << host), host)) < 0) {
		printf("cannot add inet host for op%d: %s\n", host, strerror(s));
		sts = s;
	    }
	}
	if (ipv6) {
	    sprintf(name, "fec0::%x:%x:%x:%x:%x:%x",
		    host * 3, 17+host, host,
		    host * 3, 17+host, host);
	    if ((s = __pmAccAddHost(name, ~(1 << host), ~(1 << host), host)) < 0) {
		printf("cannot add IPv6 host for op%d: %s\n", host, strerror(s));
		sts = s;
	    }
	}
    }
    if (sts < 0)
	return 1;

    putc('\n', stderr);

    putc('\n', stderr);
    __pmAccDumpHosts(stderr);

    putc('\n', stderr);

    if (ipv4) {
	for (host = 0; host < WORD_BIT; host++) {
	    int	j;

	    for (j = 0; j <= host; j++) {
		char	buf[20];
		sprintf(buf, "%d.%d.%d.%d", 155, host * 3, 17+host, host);
		if ((inaddr =__pmStringToSockAddr(buf)) == NULL) {
		  printf("insufficient memory\n");
		  continue;
		}
		sts = __pmAccAddClient(inaddr, &i);
		__pmSockAddrFree(inaddr);
		if (sts < 0) {
		    if (j == host && sts == PM_ERR_CONNLIMIT)
			continue;
		    printf("add inet client from host %d (j=%d): %s\n",
			   j, host, pmErrStr(sts));
		    continue;
		}
		else if (i != (~(1 << host)))
		    printf("inet host %d: __pmAccAddClient returns denyOpsResult 0x%x (expected 0x%x)\n",
			   host, i, ~(1 << host));
	    }
	}
    }
    if (ipv6) {
	for (host = 0; host < WORD_BIT; host++) {
	    int	j;

	    for (j = 0; j <= host; j++) {
		char	buf[4*8 + 7 + 1]; /* handles full IPv6 address */
		sprintf(buf, "fec0::%x:%x:%x:%x:%x:%x",
			host * 3, 17+host, host,
			host * 3, 17+host, host);
		if ((inaddr =__pmStringToSockAddr(buf)) == NULL) {
		  printf("insufficient memory\n");
		  continue;
		}
		sts = __pmAccAddClient(inaddr, &i);
		__pmSockAddrFree(inaddr);
		if (sts < 0) {
		    if (j == host && sts == PM_ERR_CONNLIMIT)
			continue;
		    printf("add IPv6 client from host %d (j=%d): %s\n",
			   j, host, pmErrStr(sts));
		    continue;
		}
		else if (i != (~(1 << host)))
		    printf("IPv6 host %d: __pmAccAddClient returns denyOpsResult 0x%x (expected 0x%x)\n",
			   host, i, ~(1 << host));
	    }
	}
    }

    putc('\n', stderr);

    putc('\n', stderr);
    __pmAccDumpHosts(stderr);

    putc('\n', stderr);

    return 0;
}