From: Robert Spalek Date: Fri, 21 Apr 2006 18:06:41 +0000 (+0200) Subject: conf2: added a parser for IP addresses X-Git-Tag: holmes-import~645^2~11^2~87 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=2685be345e1fd22cf49a6a97db98dc1495066558;p=libucw.git conf2: added a parser for IP addresses plus added two error messages when trying to set an attribute without giving any value --- diff --git a/lib/conf2.c b/lib/conf2.c index 25808b8a..dd3fda03 100644 --- a/lib/conf2.c +++ b/lib/conf2.c @@ -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); diff --git a/lib/conf2.h b/lib/conf2.h index 6920be75..7244af10 100644 --- a/lib/conf2.h +++ b/lib/conf2.h @@ -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