From 1781e9e758b6fbeb8a91b60d566aef4ac4b79f3a Mon Sep 17 00:00:00 2001 From: Damien Stuart Date: Tue, 11 Aug 2009 02:04:51 +0000 Subject: [PATCH] Updated ip,port format and value check. git-svn-id: file:///home/mbr/svn/fwknop/trunk@133 510a4753-2344-4c79-9c09-4d669213fbeb --- client/fwknop.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/client/fwknop.c b/client/fwknop.c index 4bec0888..7fc1d798 100644 --- a/client/fwknop.c +++ b/client/fwknop.c @@ -432,27 +432,31 @@ dump_transmit_options(fko_cli_options_t *options) } /* See if the string is of the format ":", - * e.g. "123.1.2.3,12345" - this needs work. -*/ + */ static int ipv4_str_has_port(char *str) { - int rv = 0, i; - int st_len = strlen(str); + int o1, o2, o3, o4, p; - for (i=0; i < st_len; i++) { - if (str[i] == ',' || str[i] == ':') { - str[i] = ','; /* force "," format */ - rv = 1; - continue; - } - if (rv && ! isdigit(str[i])) { - rv = 0; - break; - } + /* Force the ':' (if any) to a ',' + */ + char *ndx = strchr(str, ':'); + if(ndx != NULL) + *ndx = ','; + + /* Check format and values. + */ + if((sscanf(str, "%u.%u.%u.%u,%u", &o1, &o2, &o3, &o4, &p)) == 5 + && o1 >= 0 && o1 <= 255 + && o2 >= 0 && o2 <= 255 + && o3 >= 0 && o3 <= 255 + && o4 >= 0 && o4 <= 255 + && p > 0 && p < 65536) + { + return 1; } - return rv; + return 0; } /* Set NAT access string