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"
18 struct ipaccess_list {
22 struct ipaccess_entry {
28 struct ipaccess_list *
31 /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
32 struct ipaccess_list *l = xmalloc(sizeof(*l));
39 parse_ip(byte *x, u32 *a)
49 q = q*10 + *x++ - '0';
51 return "Invalid IP address";
53 if (*x++ != ((i == 3) ? 0 : '.'))
54 return "Invalid IP address";
62 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
64 byte *p = strchr(c, '/');
66 struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
72 if (q = parse_ip(p, &a->mask))
77 add_tail(&l->l, &a->n);
78 return parse_ip(c, &a->addr);
82 ipaccess_check(struct ipaccess_list *l, u32 ip)
84 struct ipaccess_entry *a;
87 if (! ((ip ^ a->addr) & a->mask))