1 /* The 'ipset' data structure and related algorithms in this file were
2 inspired by the 'ipaddr.py' library from Cendio Systems AB. */
9 #define DEFAULT_ALLOC 2
10 #define EXTEND_ALLOC_BY 4
12 struct subnet_list *subnet_list_new(void)
14 struct subnet_list *r;
15 r=safe_malloc(sizeof(*r),"subnet_list_new:list");
17 r->alloc=DEFAULT_ALLOC;
18 r->list=safe_malloc(sizeof(*r->list)*r->alloc,"subnet_list_new:data");
22 void subnet_list_free(struct subnet_list *a)
24 if (a->list) free(a->list);
28 static void subnet_list_set_len(struct subnet_list *a, uint32_t l)
34 na=a->alloc+EXTEND_ALLOC_BY;
35 nd=realloc(a->list,sizeof(*nd)*na);
37 fatal_perror("subnet_list_set_len: realloc");
45 void subnet_list_append(struct subnet_list *a, uint32_t prefix, uint32_t len)
48 subnet_list_set_len(a,a->entries+1);
49 sn=&a->list[a->entries-1];
52 sn->mask=len?(0xffffffff << (32-len)):0;
55 struct ipset *ipset_new(void)
58 r=safe_malloc(sizeof(*r),"ipset_new:set");
61 r->d=safe_malloc(sizeof(*r->d)*r->a,"ipset_new:data");
65 void ipset_free(struct ipset *a)
72 static void ipset_dump(struct ipset *a, string_t name)
77 for (i=0; i<a->l; i++) {
78 printf("[%08x-%08x] ",a->d[i].a,a->d[i].b);
84 struct ipset *ipset_from_subnet(struct subnet s)
91 r->d[0].b=s.prefix | (~s.mask);
95 struct ipset *ipset_from_subnet_list(struct subnet_list *l)
97 struct ipset *r, *a, *b;
101 for (i=0; i<l->entries; i++) {
102 a=ipset_from_subnet(l->list[i]);
111 static void ipset_set_len(struct ipset *a, uint32_t l)
117 na=a->a+EXTEND_ALLOC_BY;
118 nd=realloc(a->d,sizeof(*nd)*na);
120 fatal_perror("ipset_set_len: realloc");
128 static void ipset_append_range(struct ipset *a, struct iprange r)
130 ipset_set_len(a,a->l+1);
134 #define max(a,b) (a>b?a:b)
135 struct ipset *ipset_union(struct ipset *a, struct ipset *b)
143 while (ia<a->l || ib<b->l) {
146 if (a->d[ia].a < b->d[ib].a)
156 ipset_append_range(c,r);
157 else if (r.a <= c->d[c->l-1].b+1)
158 /* Extends (or is consumed by) the last range */
159 c->d[c->l-1].b=max(c->d[c->l-1].b, r.b);
161 ipset_append_range(c,r);
166 struct ipset *ipset_intersection(struct ipset *a, struct ipset *b)
169 struct iprange ra, rb;
175 while (ia<a->l && ib<b->l) {
179 /* The first entry of a doesn't overlap with any part of b */
181 else if (ra.a > rb.b)
182 /* The first entry of b doesn't overlap with any part of a */
185 /* Trim away any leading edges */
187 /* a starts before b */
189 else if (ra.a > rb.a)
190 /* b starts before a */
193 /* Now the ranges start at the same point */
195 /* The ranges are equal */
196 ipset_append_range(r,ra);
199 } else if (ra.b < rb.b) {
200 /* a is the smaller range */
201 ipset_append_range(r,ra);
204 /* y is the smaller range */
205 ipset_append_range(r,rb);
213 struct ipset *ipset_complement(struct ipset *a)
222 for (i=0; i<a->l; i++) {
228 ipset_append_range(r,n);
232 if (pre!=0xffffffff) {
235 ipset_append_range(r,n);
241 struct ipset *ipset_subtract(struct ipset *a, struct ipset *b)
244 c=ipset_complement(b);
245 r=ipset_intersection(a,c);
250 bool_t ipset_is_empty(struct ipset *a)
255 bool_t ipset_contains_addr(struct ipset *a, uint32_t addr)
260 for (i=0; i<a->l; i++) {
262 if (addr>=r.a && addr<=r.b) return True;
263 if (addr<r.a) return False;
268 /* sub is a subset of super if it does not intersect with the complement
270 bool_t ipset_is_subset(struct ipset *super, struct ipset *sub)
272 struct ipset *superc;
276 superc=ipset_complement(super);
277 inter=ipset_intersection(superc,sub);
278 empty=ipset_is_empty(inter);
284 struct subnet_list *ipset_to_subnet_list(struct ipset *is)
286 struct subnet_list *r;
287 int64_t a,b,lobit,himask,lomask;
292 for (i=0; i<is->l; i++) {
301 if ((a & lomask) != 0) {
302 subnet_list_append(r,a,bits);
304 } else if ((b & lomask) != lomask) {
305 subnet_list_append(r,b&himask,bits);
308 lomask = (lomask << 1) | 1;
309 lobit = (lobit << 1);
310 himask = himask ^ lobit;
320 /* The string buffer must be at least 16 bytes long */
321 string_t ipaddr_to_string(uint32_t addr)
326 s=safe_malloc(16,"ipaddr_to_string");
331 snprintf(s, 16, "%d.%d.%d.%d", a, b, c, d);
335 string_t subnet_to_string(struct subnet sn)
337 uint32_t addr=sn.prefix;
341 s=safe_malloc(19,"subnet_to_string");
346 snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, sn.len);
350 static struct subnet string_item_to_subnet(item_t *i, cstring_t desc,
354 uint32_t a, b, c, d, n;
360 /* i is not guaranteed to be a string */
361 if (i->type!=t_string) {
362 cfgfatal(i->loc,desc,"expecting a string (subnet specification)\n");
366 if (strcmp(in,"default")==0) {
377 /* We expect strings of the form "a.b.c.d[/n]", i.e. the dots are
378 NOT optional. The subnet mask is optional; if missing it is assumed
380 match=sscanf(in,"%u.%u.%u.%u/%u", &a, &b, &c, &d, &n);
382 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
383 "subnet specification\n",in);
388 if (a>255 || b>255 || c>255 || d>255 || n>32) {
389 cfgfatal(i->loc,desc,"\"%s\": range error\n",in);
391 s.prefix=(a<<24)|(b<<16)|(c<<8)|(d);
392 s.mask=n?(~0UL << (32-n)):0;
394 if (s.prefix & ~s.mask) {
395 cfgfatal(i->loc,desc,"\"%s\": prefix not fully contained "
401 uint32_t string_item_to_ipaddr(item_t *i, cstring_t desc)
406 /* i is not guaranteed to be a string */
407 if (i->type!=t_string) {
408 cfgfatal(i->loc,desc,"expecting a string (IP address)\n");
411 match=sscanf(i->data.string,"%u.%u.%u.%u", &a, &b, &c, &d);
413 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
414 "IP address\n",i->data.string);
416 if (a>255 || b>255 || c>255 || d>255) {
417 cfgfatal(i->loc,desc,"\"%s\": range error\n",i->data.string);
419 return (a<<24)|(b<<16)|(c<<8)|(d);
422 struct ipset *string_list_to_ipset(list_t *l, struct cloc loc,
423 cstring_t module, cstring_t param)
425 struct ipset *r, *n, *isn;
432 for (i=0; i<e; i++) {
434 isn=ipset_from_subnet(string_item_to_subnet(item,param,&inv));
436 n=ipset_subtract(r,isn);
438 n=ipset_union(r,isn);