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"
20 struct ipaccess_list {
24 struct ipaccess_entry {
30 struct ipaccess_list *
33 /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
34 struct ipaccess_list *l = xmalloc(sizeof(*l));
41 parse_ip(byte **p, u32 *varp)
46 return "Missing IP address";
48 if (**p == '0' && *(*p + 1) | 32 == 'X')
51 x = strtoul(*p + 2, (char **)p, 16);
52 if (errno == ERANGE || x > 0xffffffff)
56 for (uns i = 0; i < 4; i++)
68 uns y = strtoul(*p, (char **)p, 10);
69 if (errno == ERANGE || y > 255)
76 return "Invalid IP address";
80 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
82 byte *p = strchr(c, '/');
84 struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
92 pxlen = strtoul(p, &q, 10);
93 if ((!q || !*q) && pxlen <= 32)
96 a->mask = ~(~0U >> (uns) pxlen);
98 else if (q = parse_ip(&p, &a->mask))
101 add_tail(&l->l, &a->n);
102 return parse_ip(&c, &a->addr);
106 ipaccess_check(struct ipaccess_list *l, u32 ip)
108 struct ipaccess_entry *a;
111 if (! ((ip ^ a->addr) & a->mask))