6 * Copyright 2002 Globe.Net Communications Limited
11 #include <freeradius-devel/autoconf.h>
12 #include <freeradius-devel/libradius.h>
19 #include <freeradius-devel/radiusd.h>
20 #include <freeradius-devel/modules.h>
21 #include <freeradius-devel/conffile.h>
24 #include <sys/types.h>
27 #include <netinet/in.h>
31 void ip_set_initialize(ip_set * ips)
38 void ip_set_free(ip_set * ips)
42 ip_set_initialize(ips);
45 int ip_set_add(ip_set * ips, uint32_t h_ip)
51 for (i = 0; i < ips->length; i++) {
52 ipr = &ips->ranges[i];
54 if (h_ip == (ipr->h_start - 1))
59 else if (h_ip < ipr->h_start)
61 else if (h_ip <= ipr->h_finish)
63 else if (h_ip == (ipr->h_finish + 1))
67 if (i+1 < ips->length && h_ip == ((ipr+1)->h_start - 1)) {
71 ipr->h_finish = (ipr+1)->h_finish;
73 for (i = i+1; i < ips->length; i++) {
74 ipr = &ips->ranges[i];
76 ipr->h_start = (ipr+1)->h_start;
77 ipr->h_finish = (ipr+1)->h_finish;
88 * Ok, add another range
92 if (ips->ranges == NULL) {
95 #else /* !TEST_IP_SET */
97 #endif /* !TEST_IP_SET */
98 ips->ranges = malloc(ips->allocated * sizeof(ip_range));
99 if (ips->ranges == NULL)
102 else if (ips->length > ips->allocated) {
106 ranges = realloc(ips->ranges, ips->allocated * sizeof(ip_range));
109 ips->ranges = ranges;
112 for (i = ips->length-2; i >= new_i; i--) {
113 ipr = &ips->ranges[i];
115 (ipr+1)->h_start = ipr->h_start;
116 (ipr+1)->h_finish = ipr->h_finish;
119 ipr = &ips->ranges[new_i];
121 ipr->h_finish = h_ip;
126 int ip_set_test(ip_set * ips, uint32_t h_ip)
131 for (i = 0; i < ips->length; i++) {
132 ipr = &ips->ranges[i];
134 if (h_ip < ipr->h_start)
137 else if (ipr->h_start <= h_ip && h_ip <= ipr->h_finish)
145 void ip_set_dump(ip_set * ips, FILE * f)
151 fprintf(f, "ip_set: length=%d, allocated=%d\n", ips->length, ips->allocated);
152 for (i = 0; i < ips->length; i++) {
153 fprintf(f, "\t%d: %08x-%08x\n",
155 ips->ranges[i].h_start,
156 ips->ranges[i].h_finish);
160 for (i = 0; i < ips->length; i++) {
161 ipr = &ips->ranges[i];
163 if (h_ip+1 == ipr->h_start) {
164 fprintf(f, "\tinvalid gap at %d\n", i);
166 if (h_ip >= ipr->h_start) {
167 fprintf(f, "\tinvalid start at %d\n", i);
169 if (ipr->h_start > ipr->h_finish) {
170 fprintf(f, "\tinvalid range at %d\n", i);
173 h_ip = ipr->h_finish;
177 for (h_ip = 0x0a000030; h_ip < 0x0a000090; h_ip++)
180 fprintf(f, "\t%08x: ", h_ip);
181 fprintf(f, "%d", ip_set_test(ips, h_ip));
182 fprintf(f, (h_ip % 16 == 15) ? "\n" : " ");
191 ip_set_initialize(&ips);
192 ip_set_add(&ips, 0x0a000040);
193 ip_set_add(&ips, 0x0a000050);
194 ip_set_add(&ips, 0x0a000060);
195 ip_set_add(&ips, 0x0a000070);
196 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
198 ip_set_add(&ips, 0x0a000048);
199 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
201 ip_set_add(&ips, 0x0a000058);
202 ip_set_add(&ips, 0x0a000068);
203 ip_set_add(&ips, 0x0a000078);
204 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
206 ip_set_add(&ips, 0x0a000038);
207 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
209 ip_set_add(&ips, 0x0a000039);
210 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
212 ip_set_add(&ips, 0x0a000077);
213 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
215 ip_set_add(&ips, 0x0a000037);
216 ip_set_add(&ips, 0x0a000079);
217 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
219 ip_set_add(&ips, 0x0a000080);
220 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
222 ip_set_add(&ips, 0x0a000080);
223 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
225 for (h_ip = 0x0a000070; h_ip <= 0x0a000075; h_ip++)
226 ip_set_add(&ips, h_ip);
227 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
229 ip_set_add(&ips, 0x0a000076);
230 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
232 for (h_ip = 0x0a000070; h_ip <= 0x0a00007f; h_ip++)
233 ip_set_add(&ips, h_ip);
234 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
236 for (h_ip = 0x0a000062; h_ip >= 0x0a000041; h_ip--)
237 ip_set_add(&ips, h_ip);
238 ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
244 #endif /* TEST_IP_SET */