2 * UCW Library -- IP address access lists
4 * (c) 1997--2006 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/clists.h"
12 #include "lib/conf2.h"
13 #include "lib/ipaccess.h"
22 struct ipaccess_entry {
28 static byte *addrmask_parser(byte *c, void *ptr)
31 * This is tricky: addrmasks will be compared by memcmp(), so we must ensure
32 * that even the padding between structure members is zeroed out.
34 struct addrmask *am = ptr;
35 bzero(am, sizeof(*am));
37 byte *p = strchr(c, '/');
40 byte *err = cf_parse_ip(c, &am->addr);
46 if (!cf_parse_int(p, &len) && len <= 32)
47 am->mask = ~(len == 32 ? 0 : ~0U >> len);
48 else if (cf_parse_ip(p, &am->mask))
49 return "Invalid prefix length or netmask";
56 static struct cf_user_type addrmask_type = {
57 .size = sizeof(struct addrmask),
58 .parser = addrmask_parser
61 struct cf_section ipaccess_cf = {
62 CF_TYPE(struct ipaccess_entry),
64 CF_LOOKUP("Mode", PTR_TO(struct ipaccess_entry, allow), ((char*[]) { "deny", "allow" })),
65 CF_USER("IP", PTR_TO(struct ipaccess_entry, addr), &addrmask_type),
71 ipaccess_check(clist *l, u32 ip)
73 CLIST_FOR_EACH(struct ipaccess_entry *, a, *l)
74 if (! ((ip ^ a->addr.addr) & a->addr.mask))
85 static struct cf_section test_cf = {
87 CF_LIST("A", &t, &ipaccess_cf),
92 int main(int argc, char **argv)
94 cf_declare_section("T", &test_cf, 0);
95 if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
96 die("Invalid arguments");
99 while (fgets(buf, sizeof(buf), stdin))
101 byte *c = strchr(buf, '\n');
105 if (cf_parse_ip(buf, &ip))
106 puts("Invalid IP address");
107 else if (ipaccess_check(&t, ip))