From d775f3ca9c8619bc1f0d2f98a9518fed60fe6658 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Mon, 10 Apr 2006 12:53:31 +0200 Subject: [PATCH] IP parsing code moved to lib/conf.c, closes Bug #2375. --- lib/conf.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/conf.h | 6 ++++++ lib/ipaccess.c | 29 +++-------------------------- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/lib/conf.c b/lib/conf.c index 7f593d8e..30608790 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -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; diff --git a/lib/conf.h b/lib/conf.h index ef727512..7b86dc24 100644 --- a/lib/conf.h +++ b/lib/conf.h @@ -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. diff --git a/lib/ipaccess.c b/lib/ipaccess.c index 51d9eb58..1bbd085f 100644 --- a/lib/ipaccess.c +++ b/lib/ipaccess.c @@ -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 -- 2.39.2