update otp_hotp() to support 6,7,8,9 digit otp's
[freeradius.git] / src / modules / rlm_sqlippool / ip_set.c
1 /*
2  * ip_set.c
3  *
4  * Version:  $Id$
5  *
6  * Copyright 2002  Globe.Net Communications Limited
7  */
8
9 #if 0
10 #include "config.h"
11 #include <freeradius-devel/autoconf.h>
12 #include <freeradius-devel/libradius.h>
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18
19 #include <freeradius-devel/radiusd.h>
20 #include <freeradius-devel/modules.h>
21 #include <freeradius-devel/conffile.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <netinet/in.h>
28
29 #include "ip_set.h"
30
31 void ip_set_initialize(ip_set * ips)
32 {
33         ips->length = 0;
34         ips->allocated = 0;
35         ips->ranges = NULL;
36 }
37
38 void ip_set_free(ip_set * ips)
39 {
40         if (ips->ranges)
41                 free(ips->ranges);
42         ip_set_initialize(ips);
43 }
44
45 int ip_set_add(ip_set * ips, uint32_t h_ip)
46 {
47         int i;
48         int new_i;
49         ip_range * ipr;
50
51         for (i = 0; i < ips->length; i++) {
52                 ipr = &ips->ranges[i];
53
54                 if (h_ip == (ipr->h_start - 1))
55                 {
56                         ipr->h_start = h_ip;
57                         return 1;
58                 }
59                 else if (h_ip < ipr->h_start)
60                         break;
61                 else if (h_ip <= ipr->h_finish)
62                         return 0;
63                 else if (h_ip == (ipr->h_finish + 1))
64                 {
65                         ipr->h_finish = h_ip;
66
67                         if (i+1 < ips->length && h_ip == ((ipr+1)->h_start - 1)) {
68                                 /*
69                                  * Join two ranges
70                                  */
71                                 ipr->h_finish = (ipr+1)->h_finish;
72
73                                 for (i = i+1; i < ips->length; i++) {
74                                         ipr = &ips->ranges[i];
75
76                                         ipr->h_start = (ipr+1)->h_start;
77                                         ipr->h_finish = (ipr+1)->h_finish;
78                                 }
79                                 ips->length--;
80                         }
81
82                         return 1;
83                 }
84         }
85         new_i = i;
86
87         /*
88          * Ok, add another range
89          */
90         ips->length++;
91
92         if (ips->ranges == NULL) {
93 #ifdef TEST_IP_SET
94                 ips->allocated = 4;
95 #else /* !TEST_IP_SET */
96                 ips->allocated = 64;
97 #endif /* !TEST_IP_SET */
98                 ips->ranges = malloc(ips->allocated * sizeof(ip_range));
99                 if (ips->ranges == NULL)
100                         return -1;
101         }
102         else if (ips->length > ips->allocated) {
103                 ip_range * ranges;
104
105                 ips->allocated *= 2;
106                 ranges = realloc(ips->ranges, ips->allocated * sizeof(ip_range));
107                 if (ranges == NULL)
108                         return -1;
109                 ips->ranges = ranges;
110         }
111
112         for (i = ips->length-2; i >= new_i; i--) {
113                 ipr = &ips->ranges[i];
114
115                 (ipr+1)->h_start = ipr->h_start;
116                 (ipr+1)->h_finish = ipr->h_finish;
117         }
118
119         ipr = &ips->ranges[new_i];
120         ipr->h_start = h_ip;
121         ipr->h_finish = h_ip;
122
123         return 1;
124 }
125
126 int ip_set_test(ip_set * ips, uint32_t h_ip)
127 {
128         int i;
129         ip_range * ipr;
130
131         for (i = 0; i < ips->length; i++) {
132                 ipr = &ips->ranges[i];
133
134                 if (h_ip < ipr->h_start)
135                         break;
136
137                 else if (ipr->h_start <= h_ip && h_ip <= ipr->h_finish)
138                         return 1;
139         }
140
141         return 0;
142 }
143
144 #ifdef TEST_IP_SET
145 void ip_set_dump(ip_set * ips, FILE * f)
146 {
147         int i;
148         ip_range * ipr;
149         uint32_t h_ip;
150
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",
154                         i,
155                         ips->ranges[i].h_start,
156                         ips->ranges[i].h_finish);
157         }
158
159         h_ip = 0;
160         for (i = 0; i < ips->length; i++) {
161                 ipr = &ips->ranges[i];
162
163                 if (h_ip+1 == ipr->h_start) {
164                         fprintf(f, "\tinvalid gap at %d\n", i);
165                 }
166                 if (h_ip >= ipr->h_start) {
167                         fprintf(f, "\tinvalid start at %d\n", i);
168                 }
169                 if (ipr->h_start > ipr->h_finish) {
170                         fprintf(f, "\tinvalid range at %d\n", i);
171                 }
172
173                 h_ip = ipr->h_finish;
174         }
175         fprintf(f, "\n");
176
177         for (h_ip = 0x0a000030; h_ip < 0x0a000090; h_ip++)
178         {
179                 if (h_ip % 16 == 0)
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" : " ");
183         }
184 }
185
186 int main(void)
187 {
188         ip_set ips;
189         uint32_t h_ip;
190
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");
197
198         ip_set_add(&ips, 0x0a000048);
199         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
200
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");
205
206         ip_set_add(&ips, 0x0a000038);
207         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
208
209         ip_set_add(&ips, 0x0a000039);
210         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
211
212         ip_set_add(&ips, 0x0a000077);
213         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
214
215         ip_set_add(&ips, 0x0a000037);
216         ip_set_add(&ips, 0x0a000079);
217         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
218
219         ip_set_add(&ips, 0x0a000080);
220         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
221
222         ip_set_add(&ips, 0x0a000080);
223         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
224
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");
228
229         ip_set_add(&ips, 0x0a000076);
230         ip_set_dump(&ips, stdout); fprintf(stdout, "\n");
231
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");
235
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");
239
240         ip_set_free(&ips);
241
242         return 0;
243 }
244 #endif /* TEST_IP_SET */