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"
17 struct ipaccess_entry {
24 ipaccess_cf_ip(uns n UNUSED, byte **pars, struct ipaccess_entry *a)
27 CF_JOURNAL_VAR(a->addr);
28 CF_JOURNAL_VAR(a->mask);
30 byte *p = strchr(c, '/');
33 byte *err = cf_parse_ip(c, &a->addr);
39 if (!cf_parse_int(p, &len) && len <= 32)
40 a->mask = ~(len == 32 ? 0 : ~0U >> len);
41 else if (cf_parse_ip(p, &a->mask))
42 return "Invalid prefix length or netmask";
49 struct cf_section ipaccess_cf = {
50 CF_TYPE(struct ipaccess_entry),
52 CF_LOOKUP("Mode", PTR_TO(struct ipaccess_entry, allow), ((char*[]) { "deny", "allow" })),
53 CF_PARSER("IP", NULL, ipaccess_cf_ip, 1),
59 ipaccess_check(clist *l, u32 ip)
61 CLIST_FOR_EACH(struct ipaccess_entry *, a, *l)
62 if (! ((ip ^ a->addr) & a->mask))
73 static struct cf_section test_cf = {
75 CF_LIST("A", &t, &ipaccess_cf),
80 int main(int argc, char **argv)
82 cf_declare_section("T", &test_cf, 0);
83 if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
84 die("Invalid arguments");
87 while (fgets(buf, sizeof(buf), stdin))
89 byte *c = strchr(buf, '\n');
93 if (cf_parse_ip(buf, &ip))
94 puts("Invalid IP address");
95 else if (ipaccess_check(&t, ip))