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/fastbuf.h"
14 #include "lib/ipaccess.h"
23 struct ipaccess_entry {
30 addrmask_parser(byte *c, void *ptr)
33 * This is tricky: addrmasks will be compared by memcmp(), so we must ensure
34 * that even the padding between structure members is zeroed out.
36 struct addrmask *am = ptr;
37 bzero(am, sizeof(*am));
39 byte *p = strchr(c, '/');
42 byte *err = cf_parse_ip(c, &am->addr);
48 if (!cf_parse_int(p, &len) && len <= 32)
49 am->mask = ~(len == 32 ? 0 : ~0U >> len);
50 else if (cf_parse_ip(p, &am->mask))
51 return "Invalid prefix length or netmask";
59 addrmask_dumper(struct fastbuf *fb, void *ptr)
61 struct addrmask *am = ptr;
62 bprintf(fb, "%08x/%08x ", am->addr, am->mask);
65 static struct cf_user_type addrmask_type = {
66 .size = sizeof(struct addrmask),
67 .parser = addrmask_parser,
68 .dumper = addrmask_dumper
71 struct cf_section ipaccess_cf = {
72 CF_TYPE(struct ipaccess_entry),
74 CF_LOOKUP("Mode", PTR_TO(struct ipaccess_entry, allow), ((byte*[]) { "deny", "allow", NULL })),
75 CF_USER("IP", PTR_TO(struct ipaccess_entry, addr), &addrmask_type),
81 ipaccess_check(clist *l, u32 ip)
83 CLIST_FOR_EACH(struct ipaccess_entry *, a, *l)
84 if (! ((ip ^ a->addr.addr) & a->addr.mask))
95 static struct cf_section test_cf = {
97 CF_LIST("A", &t, &ipaccess_cf),
102 int main(int argc, char **argv)
104 cf_declare_section("T", &test_cf, 0);
105 if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
106 die("Invalid arguments");
109 while (fgets(buf, sizeof(buf), stdin))
111 byte *c = strchr(buf, '\n');
115 if (cf_parse_ip(buf, &ip))
116 puts("Invalid IP address");
117 else if (ipaccess_check(&t, ip))