]> mj.ucw.cz Git - libucw.git/commitdiff
Exported the addrmasks and their matching.
authorMartin Mares <mj@ucw.cz>
Sun, 3 Jun 2007 19:38:13 +0000 (21:38 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 3 Jun 2007 19:38:13 +0000 (21:38 +0200)
lib/ipaccess.c
lib/ipaccess.h

index 79508ab0eb113e4d72b32562fe4e9725118de058..215fb5345f4e8b9b08abab86f566b512daaa5fdb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- IP address access lists
  *
- *     (c) 1997--2006 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2007 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 
 #include <string.h>
 
-struct addrmask {
-  u32 addr;
-  u32 mask;
-};
-
 struct ipaccess_entry {
   cnode n;
   int allow;
-  struct addrmask addr;
+  struct ip_addrmask addr;
 };
 
 static byte *
@@ -34,7 +29,7 @@ addrmask_parser(byte *c, void *ptr)
    * 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;
+  struct ip_addrmask *am = ptr;
   bzero(am, sizeof(*am));
 
   byte *p = strchr(c, '/');
@@ -59,13 +54,13 @@ addrmask_parser(byte *c, void *ptr)
 static void
 addrmask_dumper(struct fastbuf *fb, void *ptr)
 {
-  struct addrmask *am = ptr;
+  struct ip_addrmask *am = ptr;
   bprintf(fb, "%08x/%08x ", am->addr, am->mask);
 }
 
-static struct cf_user_type addrmask_type = {
-  .size = sizeof(struct addrmask),
-  .name = "addrmask",
+struct cf_user_type ip_addrmask_type = {
+  .size = sizeof(struct ip_addrmask),
+  .name = "ip_addrmask",
   .parser = addrmask_parser,
   .dumper = addrmask_dumper
 };
@@ -74,16 +69,21 @@ struct cf_section ipaccess_cf = {
   CF_TYPE(struct ipaccess_entry),
   CF_ITEMS {
     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_USER("IP", PTR_TO(struct ipaccess_entry, addr), &ip_addrmask_type),
     CF_END
   }
 };
 
+int ip_addrmask_match(struct ip_addrmask *am, u32 ip)
+{
+  return !((ip ^ am->addr) & am->mask);
+}
+
 int
 ipaccess_check(clist *l, u32 ip)
 {
   CLIST_FOR_EACH(struct ipaccess_entry *, a, *l)
-    if (! ((ip ^ a->addr.addr) & a->addr.mask))
+    if (ip_addrmask_match(&a->addr, ip))
       return a->allow;
   return 0;
 }
index a38c5fe3cd9a6e457b1c3bae007f862cd6767a8b..b407783885eef4d93a36413000b3492c87eca036 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- IP address access lists
  *
- *     (c) 1997--2006 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2007 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 extern struct cf_section ipaccess_cf;
 int ipaccess_check(clist *l, u32 ip);
 
+/* Low-level handling of addresses and masks */
+
+struct ip_addrmask {
+  u32 addr;
+  u32 mask;
+};
+
+extern struct cf_user_type ip_addrmask_type;
+int ip_addrmask_match(struct ip_addrmask *am, u32 ip);
+
 #endif