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"
15 #undef ipaccess_check /* FIXME */
19 struct ipaccess_entry {
26 ipaccess_cf_ip(uns n UNUSED, byte **pars, struct ipaccess_entry *a)
29 CF_JOURNAL_VAR(a->addr);
30 CF_JOURNAL_VAR(a->mask);
32 byte *p = strchr(c, '/');
35 byte *err = cf_parse_ip(c, &a->addr);
41 if (!cf_parse_int(p, &len) && len <= 32)
42 a->mask = ~(len == 32 ? 0 : ~0U >> len);
43 else if (cf_parse_ip(p, &a->mask))
44 return "Invalid prefix length or netmask";
52 ipaccess_cf_mode(uns n UNUSED, byte **pars, struct ipaccess_entry *a)
54 CF_JOURNAL_VAR(a->allow);
55 if (!strcasecmp(pars[0], "allow"))
57 else if (!strcasecmp(pars[0], "deny"))
60 return "Either `allow' or `deny' expected";
64 struct cf_section ipaccess_cf = {
65 CF_TYPE(struct ipaccess_entry),
67 CF_PARSER("Mode", NULL, ipaccess_cf_mode, 1),
68 CF_PARSER("IP", NULL, ipaccess_cf_ip, 1),
74 ipaccess_check(clist *l, u32 ip)
76 CLIST_FOR_EACH(struct ipaccess_entry *, a, *l)
77 if (! ((ip ^ a->addr) & a->mask))
88 static struct cf_section test_cf = {
90 CF_LIST("A", &t, &ipaccess_cf),
95 int main(int argc, char **argv)
97 cf_declare_section("T", &test_cf, 0);
98 if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
99 die("Invalid arguments");
102 while (fgets(buf, sizeof(buf), stdin))
104 byte *c = strchr(buf, '\n');
108 if (cf_parse_ip(buf, &ip))
109 puts("Invalid IP address");
110 else if (ipaccess_check(&t, ip))