From 7ebd094ecfb663684e92d98666e52b83764e34cf Mon Sep 17 00:00:00 2001 From: George Bosilca Date: Mon, 6 Jun 2011 03:08:02 +0000 Subject: [PATCH] Cleanup the IPv4 address parsing, and correct the error message. This commit was SVN r24750. --- opal/util/help-opal-util.txt | 19 +++++------- opal/util/if.c | 60 +++++++++++++++++------------------- opal/util/net.c | 3 +- 3 files changed, 37 insertions(+), 45 deletions(-) diff --git a/opal/util/help-opal-util.txt b/opal/util/help-opal-util.txt index 1af117c7b0..234acdd0a8 100644 --- a/opal/util/help-opal-util.txt +++ b/opal/util/help-opal-util.txt @@ -24,7 +24,7 @@ install its own signal handler for this signal by setting the Signal: %d Current opal_signal value: %s * -[malformed IP address or netmask] +[malformed net_private_ipv4] Open MPI has detected at least one malformed IP address or netmask in the value of the opal_net_private_ipv4 MCA parameter. The opal_net_private_ipv4 MCA parameter accepts a semicolon-delimited list @@ -37,16 +37,11 @@ All malformed entries will be ignored; Open MPI will attempt to continue your job. The first detected malformed entry was %s. # [invalid-net-mask] -We were unable to parse the provided network interface: +Open MPI has detected an malformed IPv4 address or netmask +in %s. Accepted values follows the Classless Inter-Domain +Routing (CIDR) notation specifications. For example: - Interface: %s + 10.0.0.0/8;172.16/12;192.168;169.254.0.0/16 -The interface must be one of the following forms: - - 123.456.789.123 - 123.456/16 - 123.456.789 - -The system can parse any one of these, and will find an interface -that matches within the provided scope. Please revise your input -and try again. +All malformed entries will be ignored; Open MPI will attempt to continue +your job. diff --git a/opal/util/if.c b/opal/util/if.c index 5a5b6d3cf0..7c45ac20a5 100644 --- a/opal/util/if.c +++ b/opal/util/if.c @@ -490,29 +490,37 @@ opal_ifislocal(const char *hostname) return false; } -static uint32_t parse_dots(const char *addr) +static uint32_t parse_ipv4_dots(const char *addr, uint32_t* net, int* dots) { - char **tuple; + const char *start = addr, *end; uint32_t n[]={0,0,0,0}; - uint32_t net; - int i; + int i, rc = OPAL_SUCCESS; - tuple = opal_argv_split(addr, '.'); /* now assemble the address */ - for (i=0; NULL != tuple[i]; i++) { - n[i] = strtoul(tuple[i], NULL, 10); + for( i = 0; i < 4; i++ ) { + n[i] = strtoul(start, (char**)&end, 10); + if( end == start ) { /* error case: use what we have so far */ + rc = OPAL_ERROR; + break; + } + /* skip all the . */ + for( start = end; '\0' != *start; start++ ) + if( '.' != *start ) break; + /* did we read something sensible? */ + if( n[i] > 255 ) { + return OPAL_ERROR; + } } - net = OPAL_IF_ASSEMBLE_NETWORK(n[0], n[1], n[2], n[3]); - opal_argv_free(tuple); - return net; + *dots = i; + *net = OPAL_IF_ASSEMBLE_NETWORK(n[0], n[1], n[2], n[3]); + return OPAL_SUCCESS; } int opal_iftupletoaddr(const char *inaddr, uint32_t *net, uint32_t *mask) { - char **tuple; - int pval; - char *msk, *ptr; + int pval, dots, rc = OPAL_SUCCESS; + const char *ptr; /* if a mask was desired... */ if (NULL != mask) { @@ -520,19 +528,17 @@ opal_iftupletoaddr(const char *inaddr, uint32_t *net, uint32_t *mask) *mask = 0xFFFFFFFF; /* if entry includes mask, split that off */ - msk = NULL; if (NULL != (ptr = strchr(inaddr, '/'))) { - *ptr = '\0'; - msk = ptr + 1; + ptr = ptr + 1; /* skip the / */ /* is the mask a tuple? */ - if (NULL != strchr(msk, '.')) { + if (NULL != strchr(ptr, '.')) { /* yes - extract mask from it */ - *mask = parse_dots(msk); + rc = parse_ipv4_dots(ptr, mask, &dots); } else { /* no - must be an int telling us how much of the addr to use: e.g., /16 * For more information please read http://en.wikipedia.org/wiki/Subnetwork. */ - pval = strtol(msk, NULL, 10); + pval = strtol(ptr, NULL, 10); if ((pval > 31) || (pval < 1)) { opal_output(0, "opal_iftupletoaddr: unknown mask"); return OPAL_ERROR; @@ -541,8 +547,8 @@ opal_iftupletoaddr(const char *inaddr, uint32_t *net, uint32_t *mask) } } else { /* use the number of dots to determine it */ - tuple = opal_argv_split(inaddr, '.'); - pval = opal_argv_count(tuple); + for( ptr = inaddr, pval = 0; '\0'!= *ptr; ptr++ ) + if( '.' == *ptr ) pval++; /* if we have three dots, then we have four * fields since it is a full address, so the * default netmask is fine @@ -559,24 +565,16 @@ opal_iftupletoaddr(const char *inaddr, uint32_t *net, uint32_t *mask) return OPAL_ERROR; } } - opal_argv_free(tuple); } } /* if network addr is desired... */ if (NULL != net) { - /* set default */ - *net = 0; - - /* if entry includes mask, split that off */ - if (NULL != (ptr = strchr(inaddr, '/'))) { - *ptr = '\0'; - } /* now assemble the address */ - *net = parse_dots(inaddr); + rc = parse_ipv4_dots(inaddr, net, &dots); } - return OPAL_SUCCESS; + return rc; } /* diff --git a/opal/util/net.c b/opal/util/net.c index 62c36b3aaf..6b9ccf4504 100644 --- a/opal/util/net.c +++ b/opal/util/net.c @@ -155,9 +155,8 @@ opal_net_init() * get added to the trunk. */ if (0 == found_bad) { - opal_output(0, "FOUND BAD!\n"); opal_show_help("help-opal-util.txt", - "malformed IP address or netmask", + "malformed net_private_ipv4", true, args[i]); found_bad = 1; }