]> 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 18b1383ae6e25b5024a7b4a651a927eb9d0fa6a7..d00ac54ab4501e0079f03defc3288a2f536f877c 100644 (file)
@@ -2,6 +2,9 @@
  *     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"
@@ -11,6 +14,7 @@
 #include "lib/ipaccess.h"
 
 #include <string.h>
+#include <stdlib.h>
 
 struct ipaccess_list {
   list l;
@@ -26,7 +30,7 @@ struct ipaccess_list *
 ipaccess_init(void)
 {
   /* Cannot use cfg_malloc() here as the pool can be uninitialized now */
-  struct ipaccess_list *l = malloc(sizeof(*l));
+  struct ipaccess_list *l = xmalloc(sizeof(*l));
 
   init_list(&l->l);
   return l;
@@ -58,19 +62,25 @@ parse_ip(byte *x, u32 *a)
 byte *
 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));
+  unsigned long pxlen;
 
   a->allow = is_allow;
+  a->mask = ~0U;
   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;
     }
-  else
-    a->mask = ~0;
   add_tail(&l->l, &a->n);
   return parse_ip(c, &a->addr);
 }