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"
12 #include "lib/conf2.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));
40 // FIXME: replace by cf_parse_ip()
42 parse_ip(byte **p, u32 *varp)
47 return "Missing IP address";
49 if (**p == '0' && *(*p + 1) | 32 == 'X')
52 x = strtoul(*p + 2, (char **)p, 16);
53 if (errno == ERANGE || x > 0xffffffff)
57 for (uns i = 0; i < 4; i++)
69 uns y = strtoul(*p, (char **)p, 10);
70 if (errno == ERANGE || y > 255)
77 return "Invalid IP address";
81 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
83 byte *p = strchr(c, '/');
85 struct ipaccess_entry *a = cf_malloc(sizeof(struct ipaccess_entry));
93 pxlen = strtoul(p, &q, 10);
94 if ((!q || !*q) && pxlen <= 32)
97 a->mask = ~(~0U >> (uns) pxlen);
99 else if (q = parse_ip(&p, &a->mask))
102 add_tail(&l->l, &a->n);
103 return parse_ip(&c, &a->addr);
107 ipaccess_check(struct ipaccess_list *l, u32 ip)
109 struct ipaccess_entry *a;
112 if (! ((ip ^ a->addr) & a->mask))