]> mj.ucw.cz Git - libucw.git/blobdiff - lib/ipaccess.c
Fixed const qualifiers in regex library.
[libucw.git] / lib / ipaccess.c
index 79508ab0eb113e4d72b32562fe4e9725118de058..5dd388ccd36ee0a5a68c71c47656650458629d13 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- IP address access lists
  *
 /*
  *     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.
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
 
 #include <string.h>
 
 
 #include <string.h>
 
-struct addrmask {
-  u32 addr;
-  u32 mask;
-};
-
 struct ipaccess_entry {
   cnode n;
   int allow;
 struct ipaccess_entry {
   cnode n;
   int allow;
-  struct addrmask addr;
+  struct ip_addrmask addr;
 };
 
 };
 
-static byte *
-addrmask_parser(byte *c, void *ptr)
+static char *
+addrmask_parser(char *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.
    */
 {
   /*
    * 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));
 
   bzero(am, sizeof(*am));
 
-  byte *p = strchr(c, '/');
+  char *p = strchr(c, '/');
   if (p)
     *p++ = 0;
   if (p)
     *p++ = 0;
-  byte *err = cf_parse_ip(c, &am->addr);
+  char *err = cf_parse_ip(c, &am->addr);
   if (err)
     return err;
   if (p)
   if (err)
     return err;
   if (p)
@@ -59,13 +54,13 @@ addrmask_parser(byte *c, void *ptr)
 static void
 addrmask_dumper(struct fastbuf *fb, 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);
 }
 
   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
 };
   .parser = addrmask_parser,
   .dumper = addrmask_dumper
 };
@@ -73,17 +68,22 @@ static struct cf_user_type addrmask_type = {
 struct cf_section ipaccess_cf = {
   CF_TYPE(struct ipaccess_entry),
   CF_ITEMS {
 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_LOOKUP("Mode", PTR_TO(struct ipaccess_entry, allow), ((char*[]) { "deny", "allow", NULL })),
+    CF_USER("IP", PTR_TO(struct ipaccess_entry, addr), &ip_addrmask_type),
     CF_END
   }
 };
 
     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)
 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;
 }
       return a->allow;
   return 0;
 }
@@ -110,7 +110,7 @@ int main(int argc, char **argv)
   byte buf[256];
   while (fgets(buf, sizeof(buf), stdin))
     {
   byte buf[256];
   while (fgets(buf, sizeof(buf), stdin))
     {
-      byte *c = strchr(buf, '\n');
+      char *c = strchr(buf, '\n');
       if (c)
        *c = 0;
       u32 ip;
       if (c)
        *c = 0;
       u32 ip;