1 /* The 'ipset' data structure and related algorithms in this file were
2 inspired by the 'ipaddr.py' library from Cendio Systems AB. */
8 #define DEFAULT_ALLOC 2
9 #define EXTEND_ALLOC_BY 4
11 struct subnet_list *subnet_list_new(void)
13 struct subnet_list *r;
14 r=safe_malloc(sizeof(*r),"subnet_list_new:list");
16 r->alloc=DEFAULT_ALLOC;
17 r->list=safe_malloc(sizeof(*r->list)*r->alloc,"subnet_list_new:data");
21 void subnet_list_free(struct subnet_list *a)
23 if (a->list) free(a->list);
27 static void subnet_list_set_len(struct subnet_list *a, uint32_t l)
33 na=a->alloc+EXTEND_ALLOC_BY;
34 nd=realloc(a->list,sizeof(*nd)*na);
36 fatal_perror("subnet_list_set_len: realloc");
44 void subnet_list_append(struct subnet_list *a, uint32_t prefix, uint32_t len)
47 subnet_list_set_len(a,a->entries+1);
48 sn=&a->list[a->entries-1];
51 sn->mask=len?(0xffffffff << (32-len)):0;
54 struct ipset *ipset_new(void)
57 r=safe_malloc(sizeof(*r),"ipset_new:set");
60 r->d=safe_malloc(sizeof(*r->d)*r->a,"ipset_new:data");
64 void ipset_free(struct ipset *a)
71 static void ipset_dump(struct ipset *a, string_t name)
76 for (i=0; i<a->l; i++) {
77 printf("[%08x-%08x] ",a->d[i].a,a->d[i].b);
83 struct ipset *ipset_from_subnet(struct subnet s)
90 r->d[0].b=s.prefix | (~s.mask);
94 struct ipset *ipset_from_subnet_list(struct subnet_list *l)
96 struct ipset *r, *a, *b;
100 for (i=0; i<l->entries; i++) {
101 a=ipset_from_subnet(l->list[i]);
110 static void ipset_set_len(struct ipset *a, uint32_t l)
116 na=a->a+EXTEND_ALLOC_BY;
117 nd=realloc(a->d,sizeof(*nd)*na);
119 fatal_perror("ipset_set_len: realloc");
127 static void ipset_append_range(struct ipset *a, struct iprange r)
129 ipset_set_len(a,a->l+1);
133 #define max(a,b) (a>b?a:b)
134 struct ipset *ipset_union(struct ipset *a, struct ipset *b)
142 while (ia<a->l || ib<b->l) {
145 if (a->d[ia].a < b->d[ib].a)
155 ipset_append_range(c,r);
156 else if (r.a <= c->d[c->l-1].b+1)
157 /* Extends (or is consumed by) the last range */
158 c->d[c->l-1].b=max(c->d[c->l-1].b, r.b);
160 ipset_append_range(c,r);
165 struct ipset *ipset_intersection(struct ipset *a, struct ipset *b)
168 struct iprange ra, rb;
174 while (ia<a->l && ib<b->l) {
178 /* The first entry of a doesn't overlap with any part of b */
180 else if (ra.a > rb.b)
181 /* The first entry of b doesn't overlap with any part of a */
184 /* Trim away any leading edges */
186 /* a starts before b */
188 else if (ra.a > rb.a)
189 /* b starts before a */
192 /* Now the ranges start at the same point */
194 /* The ranges are equal */
195 ipset_append_range(r,ra);
198 } else if (ra.b < rb.b) {
199 /* a is the smaller range */
200 ipset_append_range(r,ra);
203 /* y is the smaller range */
204 ipset_append_range(r,rb);
212 struct ipset *ipset_complement(struct ipset *a)
221 for (i=0; i<a->l; i++) {
227 ipset_append_range(r,n);
231 if (pre!=0xffffffff) {
234 ipset_append_range(r,n);
240 struct ipset *ipset_subtract(struct ipset *a, struct ipset *b)
243 c=ipset_complement(b);
244 r=ipset_intersection(a,c);
249 bool_t ipset_is_empty(struct ipset *a)
254 bool_t ipset_contains_addr(struct ipset *a, uint32_t addr)
259 for (i=0; i<a->l; i++) {
261 if (addr>=r.a && addr<=r.b) return True;
262 if (addr<r.a) return False;
267 /* sub is a subset of super if it does not intersect with the complement
269 bool_t ipset_is_subset(struct ipset *super, struct ipset *sub)
271 struct ipset *superc;
275 superc=ipset_complement(super);
276 inter=ipset_intersection(superc,sub);
277 empty=ipset_is_empty(inter);
283 struct subnet_list *ipset_to_subnet_list(struct ipset *is)
285 struct subnet_list *r;
288 uint32_t lomask,lobit,himask,bits;
291 for (i=0; i<is->l; i++) {
300 if ((a & lomask) != 0) {
301 subnet_list_append(r,a,bits);
303 } else if ((b & lomask) != lomask) {
304 subnet_list_append(r,b&himask,bits);
307 lomask = (lomask << 1) | 1;
308 lobit = (lobit << 1);
309 himask = himask ^ lobit;
319 /* The string buffer must be at least 16 bytes long */
320 string_t ipaddr_to_string(uint32_t addr)
325 s=safe_malloc(16,"ipaddr_to_string");
330 snprintf(s, 16, "%d.%d.%d.%d", a, b, c, d);
334 string_t subnet_to_string(struct subnet sn)
336 uint32_t addr=sn.prefix;
340 s=safe_malloc(19,"subnet_to_string");
345 snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, sn.len);
349 static struct subnet string_item_to_subnet(item_t *i, string_t desc,
353 uint32_t a, b, c, d, n;
359 /* i is not guaranteed to be a string */
360 if (i->type!=t_string) {
361 cfgfatal(i->loc,desc,"expecting a string (subnet specification)\n");
365 if (strcmp(in,"default")==0) {
376 /* We expect strings of the form "a.b.c.d[/n]", i.e. the dots are
377 NOT optional. The subnet mask is optional; if missing it is assumed
379 match=sscanf(in,"%u.%u.%u.%u/%u", &a, &b, &c, &d, &n);
381 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
382 "subnet specification\n",in);
387 if (a>255 || b>255 || c>255 || d>255 || n>32) {
388 cfgfatal(i->loc,desc,"\"%s\": range error\n",in);
390 s.prefix=(a<<24)|(b<<16)|(c<<8)|(d);
391 s.mask=n?(~0UL << (32-n)):0;
393 if (s.prefix & ~s.mask) {
394 cfgfatal(i->loc,desc,"\"%s\": prefix not fully contained "
400 uint32_t string_item_to_ipaddr(item_t *i, string_t desc)
405 /* i is not guaranteed to be a string */
406 if (i->type!=t_string) {
407 cfgfatal(i->loc,desc,"expecting a string (IP address)\n");
410 match=sscanf(i->data.string,"%u.%u.%u.%u", &a, &b, &c, &d);
412 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
413 "IP address\n",i->data.string);
415 if (a>255 || b>255 || c>255 || d>255) {
416 cfgfatal(i->loc,desc,"\"%s\": range error\n",i->data.string);
418 return (a<<24)|(b<<16)|(c<<8)|(d);
421 struct ipset *string_list_to_ipset(list_t *l, struct cloc loc,
422 string_t module, string_t param)
424 struct ipset *r, *n, *isn;
431 for (i=0; i<e; i++) {
433 isn=ipset_from_subnet(string_item_to_subnet(item,param,&inv));
435 n=ipset_subtract(r,isn);
437 n=ipset_union(r,isn);