]> mj.ucw.cz Git - libucw.git/blobdiff - lib/ipaccess.c
IP parsing code moved to lib/conf.c, closes Bug #2375.
[libucw.git] / lib / ipaccess.c
index 18b1383ae6e25b5024a7b4a651a927eb9d0fa6a7..1bbd085fc2ee69b16384cd4f69d5515f13c629c9 100644 (file)
@@ -1,7 +1,10 @@
 /*
- *     Sherlock Library -- IP address access lists
+ *     UCW 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,53 +30,36 @@ 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;
 }
 
-static byte *
-parse_ip(byte *x, u32 *a)
-{
-  uns i, q;
-  u32 z = 0;
-
-  for(i=0; i<4; i++)
-    {
-      q = 0;
-      while (Cdigit(*x))
-       {
-         q = q*10 + *x++ - '0';
-         if (q > 255)
-           return "Invalid IP address";
-       }
-      if (*x++ != ((i == 3) ? 0 : '.'))
-       return "Invalid IP address";
-      z = (z << 8) | q;
-    }
-  *a = z;
-  return NULL;
-}
-
 byte *
 ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
 {
   byte *p = strchr(c, '/');
-  byte *q;
+  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 = cf_parse_ip(&p, &a->mask))
        return q;
     }
-  else
-    a->mask = ~0;
   add_tail(&l->l, &a->n);
-  return parse_ip(c, &a->addr);
+  return cf_parse_ip(&c, &a->addr);
 }
 
 int