2 * UCW Library -- IP address access lists
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/lists.h"
13 #include "lib/chartype.h"
14 #include "lib/ipaccess.h"
19 struct ipaccess_list {
23 struct ipaccess_entry {
29 struct ipaccess_list *
32 /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
33 struct ipaccess_list *l = xmalloc(sizeof(*l));
40 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
42 byte *p = strchr(c, '/');
44 struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
52 pxlen = strtoul(p, &q, 10);
53 if ((!q || !*q) && pxlen <= 32)
56 a->mask = ~(~0U >> (uns) pxlen);
58 else if (q = cf_parse_ip(&p, &a->mask))
61 add_tail(&l->l, &a->n);
62 return cf_parse_ip(&c, &a->addr);
66 ipaccess_check(struct ipaccess_list *l, u32 ip)
68 struct ipaccess_entry *a;
71 if (! ((ip ^ a->addr) & a->mask))