1 /* The 'ipset' data structure and related algorithms in this file were
2 inspired by the 'ipaddr.py' library from Cendio Systems AB. */
11 #define DEFAULT_ALLOC 2
12 #define EXTEND_ALLOC_BY 4
14 struct subnet_list *subnet_list_new(void)
16 struct subnet_list *r;
17 r=safe_malloc(sizeof(*r),"subnet_list_new:list");
19 r->alloc=DEFAULT_ALLOC;
20 r->list=safe_malloc_ary(sizeof(*r->list),r->alloc,"subnet_list_new:data");
24 void subnet_list_free(struct subnet_list *a)
26 if (a->list) free(a->list);
30 static void subnet_list_set_len(struct subnet_list *a, int32_t l)
36 assert(a->alloc < (int)(INT_MAX/sizeof(*nd))-EXTEND_ALLOC_BY);
37 na=a->alloc+EXTEND_ALLOC_BY;
38 nd=realloc(a->list,sizeof(*nd)*na);
40 fatal_perror("subnet_list_set_len: realloc");
48 void subnet_list_append(struct subnet_list *a, uint32_t prefix, int len)
51 assert(a->entries < INT_MAX);
52 subnet_list_set_len(a,a->entries+1);
53 sn=&a->list[a->entries-1];
56 sn->mask=len?(0xffffffff << (32-len)):0;
59 struct ipset *ipset_new(void)
62 r=safe_malloc(sizeof(*r),"ipset_new:set");
65 r->d=safe_malloc(sizeof(*r->d)*r->a,"ipset_new:data");
69 void ipset_free(struct ipset *a)
76 static void ipset_dump(struct ipset *a, string_t name)
81 for (i=0; i<a->l; i++) {
82 printf("[%08x-%08x] ",a->d[i].a,a->d[i].b);
88 struct ipset *ipset_from_subnet(struct subnet s)
95 r->d[0].b=s.prefix | (~s.mask);
99 struct ipset *ipset_from_subnet_list(struct subnet_list *l)
101 struct ipset *r, *a, *b;
105 for (i=0; i<l->entries; i++) {
106 a=ipset_from_subnet(l->list[i]);
115 static void ipset_set_len(struct ipset *a, int32_t l)
121 assert(a->a < INT_MAX-EXTEND_ALLOC_BY);
122 na=a->a+EXTEND_ALLOC_BY;
123 nd=realloc(a->d,sizeof(*nd)*na);
125 fatal_perror("ipset_set_len: realloc");
133 static void ipset_append_range(struct ipset *a, struct iprange r)
135 ipset_set_len(a,a->l+1);
139 #define max(a,b) (a>b?a:b)
140 struct ipset *ipset_union(struct ipset *a, struct ipset *b)
148 while (ia<a->l || ib<b->l) {
151 if (a->d[ia].a < b->d[ib].a)
161 ipset_append_range(c,r);
162 else if (r.a <= c->d[c->l-1].b+1)
163 /* Extends (or is consumed by) the last range */
164 c->d[c->l-1].b=max(c->d[c->l-1].b, r.b);
166 ipset_append_range(c,r);
171 struct ipset *ipset_intersection(struct ipset *a, struct ipset *b)
174 struct iprange ra, rb;
180 while (ia<a->l && ib<b->l) {
184 /* The first entry of a doesn't overlap with any part of b */
186 else if (ra.a > rb.b)
187 /* The first entry of b doesn't overlap with any part of a */
190 /* Trim away any leading edges */
192 /* a starts before b */
194 else if (ra.a > rb.a)
195 /* b starts before a */
198 /* Now the ranges start at the same point */
200 /* The ranges are equal */
201 ipset_append_range(r,ra);
204 } else if (ra.b < rb.b) {
205 /* a is the smaller range */
206 ipset_append_range(r,ra);
209 /* y is the smaller range */
210 ipset_append_range(r,rb);
218 struct ipset *ipset_complement(struct ipset *a)
228 for (i=0; i<a->l; i++) {
234 ipset_append_range(r,n);
238 if (pre!=0xffffffff) {
241 ipset_append_range(r,n);
247 struct ipset *ipset_subtract(struct ipset *a, struct ipset *b)
250 c=ipset_complement(b);
251 r=ipset_intersection(a,c);
256 bool_t ipset_is_empty(struct ipset *a)
261 bool_t ipset_contains_addr(struct ipset *a, uint32_t addr)
266 for (i=0; i<a->l; i++) {
268 if (addr>=r.a && addr<=r.b) return True;
269 if (addr<r.a) return False;
274 /* sub is a subset of super if it does not intersect with the complement
276 bool_t ipset_is_subset(struct ipset *super, struct ipset *sub)
278 struct ipset *superc;
282 superc=ipset_complement(super);
283 inter=ipset_intersection(superc,sub);
284 empty=ipset_is_empty(inter);
290 struct subnet_list *ipset_to_subnet_list(struct ipset *is)
292 struct subnet_list *r;
293 int64_t a,b,lobit,himask,lomask;
298 for (i=0; i<is->l; i++) {
307 if ((a & lomask) != 0) {
308 subnet_list_append(r,a,bits);
310 } else if ((b & lomask) != lomask) {
311 subnet_list_append(r,b&himask,bits);
314 lomask = (lomask << 1) | 1;
315 lobit = (lobit << 1);
316 himask = himask ^ lobit;
326 /* The string buffer must be at least 16 bytes long */
327 string_t ipaddr_to_string(uint32_t addr)
332 s=safe_malloc(16,"ipaddr_to_string");
337 snprintf(s, 16, "%d.%d.%d.%d", a, b, c, d);
341 string_t subnet_to_string(struct subnet sn)
343 uint32_t addr=sn.prefix;
347 s=safe_malloc(19,"subnet_to_string");
352 snprintf(s, 19, "%d.%d.%d.%d/%d", a, b, c, d, sn.len);
356 static struct subnet string_item_to_subnet(item_t *i, cstring_t desc,
360 uint32_t a, b, c, d, n;
366 /* i is not guaranteed to be a string */
367 if (i->type!=t_string) {
368 cfgfatal(i->loc,desc,"expecting a string (subnet specification)\n");
372 if (strcmp(in,"default")==0) {
383 /* We expect strings of the form "a.b.c.d[/n]", i.e. the dots are
384 NOT optional. The subnet mask is optional; if missing it is assumed
386 match=sscanf(in,"%u.%u.%u.%u/%u", &a, &b, &c, &d, &n);
388 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
389 "subnet specification\n",in);
394 if (a>255 || b>255 || c>255 || d>255 || n>32) {
395 cfgfatal(i->loc,desc,"\"%s\": range error\n",in);
397 s.prefix=(a<<24)|(b<<16)|(c<<8)|(d);
398 s.mask=n?(~0UL << (32-n)):0;
400 if (s.prefix & ~s.mask) {
401 cfgfatal(i->loc,desc,"\"%s\": prefix not fully contained "
407 uint32_t string_item_to_ipaddr(item_t *i, cstring_t desc)
412 /* i is not guaranteed to be a string */
413 if (i->type!=t_string) {
414 cfgfatal(i->loc,desc,"expecting a string (IP address)\n");
417 match=sscanf(i->data.string,"%u.%u.%u.%u", &a, &b, &c, &d);
419 cfgfatal(i->loc,desc,"\"%s\" is not a valid "
420 "IP address\n",i->data.string);
422 if (a>255 || b>255 || c>255 || d>255) {
423 cfgfatal(i->loc,desc,"\"%s\": range error\n",i->data.string);
425 return (a<<24)|(b<<16)|(c<<8)|(d);
428 struct ipset *string_list_to_ipset(list_t *l, struct cloc loc,
429 cstring_t module, cstring_t param)
431 struct ipset *r, *n, *isn;
438 for (i=0; i<e; i++) {
440 isn=ipset_from_subnet(string_item_to_subnet(item,param,&inv));
442 n=ipset_subtract(r,isn);
444 n=ipset_union(r,isn);