diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2008-08-20 13:19:15 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2008-08-20 17:46:47 +0200 |
commit | c0d02ffc3d11e96977b549563f679c5bfe30f359 (patch) | |
tree | 3470589077bff99339d192a095949617ce992f53 /net | |
parent | 33c488f8c13bb606a6b60df04461d56fc782574a (diff) | |
download | barebox-c0d02ffc3d11e96977b549563f679c5bfe30f359.tar.gz barebox-c0d02ffc3d11e96977b549563f679c5bfe30f359.tar.xz |
Fix string_to_ip
Use a pointer to an ip address instead of the return value in string_to_ip
and use the return value for error indication only. 0.0.0.0 can be a valid
ip address
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'net')
-rw-r--r-- | net/net.c | 32 | ||||
-rw-r--r-- | net/ping.c | 3 |
2 files changed, 20 insertions, 15 deletions
@@ -82,6 +82,8 @@ #include <param.h> #include <net.h> #include <driver.h> +#include <errno.h> +#include <linux/ctype.h> #include "bootp.h" #include "tftp.h" #include "rarp.h" @@ -1095,24 +1097,33 @@ char *ip_to_string (IPaddr_t x, char *s) return s; } -IPaddr_t string_to_ip(const char *s) +int string_to_ip(const char *s, IPaddr_t *ip) { - IPaddr_t addr; + IPaddr_t addr = 0; char *e; int i; if (!s) - return 0; + return -EINVAL; - for (addr = 0, i = 0; i < 4; ++i) { - ulong val = s ? simple_strtoul(s, &e, 10) : 0; + for (i = 0; i < 4; i++) { + ulong val; + + if (!isdigit(*s)) + return -EINVAL; + + val = simple_strtoul(s, &e, 10); addr <<= 8; addr |= (val & 0xFF); - if (s) - s = *e ? e + 1 : e; + + if (*e != '.' && i != 3) + return -EINVAL; + + s = e + 1; } - return htonl(addr); + *ip = htonl(addr); + return 0; } void VLAN_to_string(ushort x, char *s) @@ -1152,11 +1163,6 @@ void print_IPaddr (IPaddr_t x) puts (tmp); } -IPaddr_t getenv_IPaddr (char *var) -{ - return (string_to_ip(getenv(var))); -} - ushort getenv_VLAN(char *var) { return (string_to_VLAN(getenv(var))); diff --git a/net/ping.c b/net/ping.c index 4d61532b1e..14224c6b91 100644 --- a/net/ping.c +++ b/net/ping.c @@ -93,8 +93,7 @@ int do_ping (cmd_tbl_t *cmdtp, int argc, char *argv[]) if (argc < 2) return -1; - NetPingIP = string_to_ip(argv[1]); - if (NetPingIP == 0) { + if (string_to_ip(argv[1], &NetPingIP)) { printf ("Usage:\n%s\n", cmdtp->usage); return -1; } |