2 * Sherlock 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 parse_ip(byte *x, u32 *a)
50 q = q*10 + *x++ - '0';
52 return "Invalid IP address";
54 if (*x++ != ((i == 3) ? 0 : '.'))
55 return "Invalid IP address";
63 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
65 char *p = strchr(c, '/');
67 struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
75 pxlen = strtoul(p, &q, 10);
76 if ((!q || !*q) && pxlen <= 32)
79 a->mask = ~(~0U >> (uns) pxlen);
81 else if (q = parse_ip(p, &a->mask))
84 add_tail(&l->l, &a->n);
85 return parse_ip(c, &a->addr);
89 ipaccess_check(struct ipaccess_list *l, u32 ip)
91 struct ipaccess_entry *a;
94 if (! ((ip ^ a->addr) & a->mask))