X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fipaccess.c;h=79508ab0eb113e4d72b32562fe4e9725118de058;hb=8c48090e240d68564c79eb29ac9004cc911bb5d0;hp=2116f71d9b6193815a947c6653b1a8440601e437;hpb=b1fbf9e3292eab73fa448dd6c937e70eb66dc4a7;p=libucw.git diff --git a/lib/ipaccess.c b/lib/ipaccess.c index 2116f71d..79508ab0 100644 --- a/lib/ipaccess.c +++ b/lib/ipaccess.c @@ -9,63 +9,72 @@ #include "lib/lib.h" #include "lib/clists.h" -#include "lib/conf2.h" +#include "lib/conf.h" +#include "lib/getopt.h" +#include "lib/fastbuf.h" #include "lib/ipaccess.h" -#undef ipaccess_check /* FIXME */ - #include +struct addrmask { + u32 addr; + u32 mask; +}; + struct ipaccess_entry { cnode n; - uns allow; - u32 addr, mask; + int allow; + struct addrmask addr; }; static byte * -ipaccess_cf_ip(uns n UNUSED, byte **pars, struct ipaccess_entry *a) +addrmask_parser(byte *c, void *ptr) { - byte *c = pars[0]; - CF_JOURNAL_VAR(a->addr); - CF_JOURNAL_VAR(a->mask); + /* + * This is tricky: addrmasks will be compared by memcmp(), so we must ensure + * that even the padding between structure members is zeroed out. + */ + struct addrmask *am = ptr; + bzero(am, sizeof(*am)); byte *p = strchr(c, '/'); if (p) *p++ = 0; - byte *err = cf_parse_ip(c, &a->addr); + byte *err = cf_parse_ip(c, &am->addr); if (err) return err; if (p) { uns len; if (!cf_parse_int(p, &len) && len <= 32) - a->mask = ~(len == 32 ? 0 : ~0U >> len); - else if (cf_parse_ip(p, &a->mask)) + am->mask = ~(len == 32 ? 0 : ~0U >> len); + else if (cf_parse_ip(p, &am->mask)) return "Invalid prefix length or netmask"; } else - a->mask = ~0U; + am->mask = ~0U; return NULL; } -static byte * -ipaccess_cf_mode(uns n UNUSED, byte **pars, struct ipaccess_entry *a) +static void +addrmask_dumper(struct fastbuf *fb, void *ptr) { - CF_JOURNAL_VAR(a->allow); - if (!strcasecmp(pars[0], "allow")) - a->allow = 1; - else if (!strcasecmp(pars[0], "deny")) - a->allow = 0; - else - return "Either `allow' or `deny' expected"; - return NULL; + struct addrmask *am = ptr; + bprintf(fb, "%08x/%08x ", am->addr, am->mask); } +static struct cf_user_type addrmask_type = { + .size = sizeof(struct addrmask), + .name = "addrmask", + .parser = addrmask_parser, + .dumper = addrmask_dumper +}; + struct cf_section ipaccess_cf = { CF_TYPE(struct ipaccess_entry), CF_ITEMS { - CF_PARSER("Mode", NULL, ipaccess_cf_mode, 1), - CF_PARSER("IP", NULL, ipaccess_cf_ip, 1), + CF_LOOKUP("Mode", PTR_TO(struct ipaccess_entry, allow), ((byte*[]) { "deny", "allow", NULL })), + CF_USER("IP", PTR_TO(struct ipaccess_entry, addr), &addrmask_type), CF_END } }; @@ -74,7 +83,7 @@ int ipaccess_check(clist *l, u32 ip) { CLIST_FOR_EACH(struct ipaccess_entry *, a, *l) - if (! ((ip ^ a->addr) & a->mask)) + if (! ((ip ^ a->addr.addr) & a->addr.mask)) return a->allow; return 0; } @@ -95,7 +104,7 @@ static struct cf_section test_cf = { int main(int argc, char **argv) { cf_declare_section("T", &test_cf, 0); - if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) + if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1) die("Invalid arguments"); byte buf[256];