2 * Sherlock Library -- IP address access lists
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
10 #include "lib/chartype.h"
11 #include "lib/ipaccess.h"
15 struct ipaccess_list {
19 struct ipaccess_entry {
25 struct ipaccess_list *
28 /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
29 struct ipaccess_list *l = malloc(sizeof(*l));
36 parse_ip(byte *x, u32 *a)
46 q = q*10 + *x++ - '0';
48 return "Invalid IP address";
50 if (*x++ != ((i == 3) ? 0 : '.'))
51 return "Invalid IP address";
59 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
61 byte *p = strchr(c, '/');
63 struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
69 if (q = parse_ip(p, &a->mask))
74 add_tail(&l->l, &a->n);
75 return parse_ip(c, &a->addr);
79 ipaccess_check(struct ipaccess_list *l, u32 ip)
81 struct ipaccess_entry *a;
84 if (! ((ip ^ a->addr) & a->mask))