]> mj.ucw.cz Git - libucw.git/commitdiff
conf2: added a parser for IP addresses
authorRobert Spalek <robert@ucw.cz>
Fri, 21 Apr 2006 18:06:41 +0000 (20:06 +0200)
committerRobert Spalek <robert@ucw.cz>
Fri, 21 Apr 2006 18:06:41 +0000 (20:06 +0200)
plus added two error messages when trying to set an attribute without
giving any value

lib/conf2.c
lib/conf2.h

index 25808b8a70567789e8694478d9748d5d19c89732..dd3fda030f41c8d60022c5cad53a36a763e9f2de 100644 (file)
@@ -348,6 +348,7 @@ lookup_unit(byte *value, byte *end, byte **msg)
 
 static char cf_rngerr[] = "Number out of range";
 
+//FIXME: parsers should handle well empty strings, unwanted suffixes etc.
 byte *
 cf_parse_int(byte *str, int *ptr)
 {
@@ -427,6 +428,38 @@ cf_parse_double(byte *str, double *ptr)
   return msg;
 }
 
+byte *
+cf_parse_ip(byte *p, u32 *varp)
+{
+  if (!*p)
+    return "Missing IP address";
+  uns x = 0;
+  if (*p == '0' && p[1] | 32 == 'X') {
+    errno = 0;
+    x = strtoul(p + 2, NULL, 16);
+    if (errno == ERANGE || x > 0xffffffff)
+      goto error;
+  }
+  else
+    for (uns i = 0; i < 4; i++) {
+      if (i) {
+       if (*p++ != '.')
+         goto error;
+      }
+      errno = 0;
+      char *p2;
+      uns y = strtoul(p, &p2, 10);
+      p = p2;
+      if (errno == ERANGE || y > 255)
+       goto error;
+      x = (x << 8) + y;
+    }
+  *varp = x;
+  return NULL;
+error:
+  return "Invalid IP address";
+}
+
 static byte *
 cf_parse_string(byte *str, byte **ptr)
 {
@@ -444,6 +477,7 @@ static struct {
   { sizeof(int), cf_parse_int },
   { sizeof(u64), cf_parse_u64 },
   { sizeof(double), cf_parse_double },
+  { sizeof(u32), cf_parse_ip },
   { sizeof(byte*), cf_parse_string }
 };
 
@@ -564,6 +598,8 @@ add_to_list(void *list, struct cnode *node, enum operation op)
 static byte *
 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum operation op)
 {
+  if (!number)
+    return "Missing value";
   ASSERT(op < OP_REMOVE);
   struct cf_section *sec = item->u.sec;
   *processed = 0;
@@ -594,6 +630,8 @@ interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed
   switch (item->cls)
   {
     case CC_STATIC:
+      if (!number)
+       return "Missing value";
       taken = MIN(number, item->number);
       *processed = taken;
       cf_journal_block(ptr, taken * parsers[item->u.type].size);
index 6920be751b1c2d80b6a3ba3e141d4148dc7ce866..7244af10bebe1ca409f3a2543ff84527ea65bdc4 100644 (file)
@@ -22,6 +22,7 @@ enum cf_class {
 
 enum cf_type {
   CT_INT, CT_U64, CT_DOUBLE,           // number types
+  CT_IP,                               // IP address
   CT_STRING                            // string type
 };
 
@@ -87,6 +88,9 @@ struct clist;
 #define CF_DOUBLE(n,p)         CF_STATIC(n,p,DOUBLE,double,1)
 #define CF_DOUBLE_ARY(n,p,c)   CF_STATIC(n,p,DOUBLE,double,c)
 #define CF_DOUBLE_DYN(n,p,c)   CF_DYNAMIC(n,p,DOUBLE,double,c)
+#define CF_IP(n,p)             CF_STATIC(n,p,IP,u32,1)
+#define CF_IP_ARY(n,p,c)       CF_STATIC(n,p,IP,u32,c)
+#define CF_IP_DYN(n,p,c)       CF_DYNAMIC(n,p,IP,u32,c)
 #define CF_STRING(n,p)         CF_STATIC(n,p,STRING,byte*,1)
 #define CF_STRING_ARY(n,p,c)   CF_STATIC(n,p,STRING,byte*,c)
 #define CF_STRING_DYN(n,p,c)   CF_DYNAMIC(n,p,STRING,byte*,c)
@@ -122,5 +126,6 @@ byte *cf_set(byte *string);
 byte *cf_parse_int(byte *str, int *ptr);
 byte *cf_parse_u64(byte *str, u64 *ptr);
 byte *cf_parse_double(byte *str, double *ptr);
+byte *cf_parse_ip(byte *p, u32 *varp);
 
 #endif