]> mj.ucw.cz Git - libucw.git/commitdiff
IP parsing code moved to lib/conf.c, closes Bug #2375.
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 10 Apr 2006 10:53:31 +0000 (12:53 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 10 Apr 2006 10:53:31 +0000 (12:53 +0200)
lib/conf.c
lib/conf.h
lib/ipaccess.c

index 7f593d8e419d68f6c46288f439a083db7f73d216..30608790b1ddb2eb158a3b91371b48f5d5df6750 100644 (file)
@@ -218,6 +218,45 @@ byte *cf_parse_double(byte *value, double *varp)
        return msg;
 }
 
+byte *
+cf_parse_ip(byte **p, u32 *varp)
+{
+  while (Cspace(**p))
+    (*p)++;
+  if (!**p)
+    return "Missing IP address";
+  uns x = 0;
+  if (**p == '0' && *(*p + 1) | 32 == 'X')
+    {
+      errno = 0;
+      x = strtoul(*p + 2, (char **)p, 16);
+      if (errno == ERANGE || x > 0xffffffff)
+        goto error;
+    }
+  else
+    for (uns i = 0; i < 4; i++)
+      {
+        if (i)
+          {
+            while (Cspace(**p))
+              (*p)++;
+            if (*(*p)++ != '.')
+              goto error;
+          }
+        while (Cspace(**p))
+          (*p)++;
+        errno = 0;
+        uns y = strtoul(*p, (char **)p, 10);
+        if (errno == ERANGE || y > 255)
+          goto error;
+        x = (x << 8) + y;
+      }
+  *varp = x;
+  return NULL;
+error:
+  return "Invalid IP address";
+}
+
 byte *cf_set_item(byte *sect, byte *name, byte *value)
 {
        struct cfitem *item;
index ef727512b2b08f1d97706dd3ad9614f9434d2843..7b86dc24f4c7a93e4a0e775de68ce4a0a2047828 100644 (file)
@@ -65,6 +65,12 @@ byte *cf_parse_int(byte *value, uns *varp);
 byte *cf_parse_u64(byte *value, u64 *varp);
 byte *cf_parse_double(byte *value, double *varp);
 
+/* 
+ * Some useful parsing functions.
+ */
+
+byte *cf_parse_ip(byte **value, u32 *varp);
+
 /*
  * When using cf_getopt, you must prefix your own short/long options by the
  * CF_(SHORT|LONG)_OPTS.
index 51d9eb586fe8e1859394b19ea02f78ec2d4b7193..1bbd085fc2ee69b16384cd4f69d5515f13c629c9 100644 (file)
@@ -36,33 +36,10 @@ ipaccess_init(void)
   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)
 {
-  char *p = strchr(c, '/');
+  byte *p = strchr(c, '/');
   char *q;
   struct ipaccess_entry *a = cfg_malloc(sizeof(struct ipaccess_entry));
   unsigned long pxlen;
@@ -78,11 +55,11 @@ ipaccess_parse(struct ipaccess_list *l, byte *c, int is_allow)
          if (pxlen != 32)
            a->mask = ~(~0U >> (uns) pxlen);
        }
-      else if (q = parse_ip(p, &a->mask))
+      else if (q = cf_parse_ip(&p, &a->mask))
        return q;
     }
   add_tail(&l->l, &a->n);
-  return parse_ip(c, &a->addr);
+  return cf_parse_ip(&c, &a->addr);
 }
 
 int