]> mj.ucw.cz Git - libucw.git/blobdiff - lib/ipaccess.c
Always define the memory allocation primitives with the `sh_' prefix,
[libucw.git] / lib / ipaccess.c
index 8a4841695fd8cce56187748fbd6120ea6aae4826..d00ac54ab4501e0079f03defc3288a2f536f877c 100644 (file)
@@ -2,14 +2,23 @@
  *     Sherlock Library -- IP address access lists
  *
  *     (c) 1997--2001 Martin Mares <mj@ucw.cz>
  *     Sherlock Library -- IP address access lists
  *
  *     (c) 1997--2001 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 "lib/lib.h"
  */
 
 #include "lib/lib.h"
+#include "lib/lists.h"
 #include "lib/conf.h"
 #include "lib/chartype.h"
 #include "lib/ipaccess.h"
 
 #include <string.h>
 #include "lib/conf.h"
 #include "lib/chartype.h"
 #include "lib/ipaccess.h"
 
 #include <string.h>
+#include <stdlib.h>
+
+struct ipaccess_list {
+  list l;
+};
 
 struct ipaccess_entry {
   node n;
 
 struct ipaccess_entry {
   node n;
@@ -17,10 +26,14 @@ struct ipaccess_entry {
   u32 addr, mask;
 };
 
   u32 addr, mask;
 };
 
-void
-ipaccess_init(ipaccess_list *l)
+struct ipaccess_list *
+ipaccess_init(void)
 {
 {
-  init_list(l);
+  /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
+  struct ipaccess_list *l = xmalloc(sizeof(*l));
+
+  init_list(&l->l);
+  return l;
 }
 
 static byte *
 }
 
 static byte *
@@ -47,31 +60,37 @@ parse_ip(byte *x, u32 *a)
 }
 
 byte *
 }
 
 byte *
-ipaccess_parse(ipaccess_list *l, byte *c, int is_allow)
+ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
 {
 {
-  byte *p = strchr(c, '/');
-  byte *q;
+  char *p = strchr(c, '/');
+  char *q;
   struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
   struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
+  unsigned long pxlen;
 
   a->allow = is_allow;
 
   a->allow = is_allow;
+  a->mask = ~0U;
   if (p)
     {
       *p++ = 0;
   if (p)
     {
       *p++ = 0;
-      if (q = parse_ip(p, &a->mask))
+      pxlen = strtoul(p, &q, 10);
+      if ((!q || !*q) && pxlen <= 32)
+       {
+         if (pxlen != 32)
+           a->mask = ~(~0U >> (uns) pxlen);
+       }
+      else if (q = parse_ip(p, &a->mask))
        return q;
     }
        return q;
     }
-  else
-    a->mask = ~0;
-  add_tail(l, &a->n);
+  add_tail(&l->l, &a->n);
   return parse_ip(c, &a->addr);
 }
 
 int
   return parse_ip(c, &a->addr);
 }
 
 int
-ipaccess_check(ipaccess_list *l, u32 ip)
+ipaccess_check(struct ipaccess_list *l, u32 ip)
 {
   struct ipaccess_entry *a;
 
 {
   struct ipaccess_entry *a;
 
-  DO_FOR_ALL(a, *l)
+  DO_FOR_ALL(a, l->l)
     if (! ((ip ^ a->addr) & a->mask))
       return a->allow;
   return 0;
     if (! ((ip ^ a->addr) & a->mask))
       return a->allow;
   return 0;