9
0
Fork 0

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>
This commit is contained in:
Sascha Hauer 2008-08-20 13:19:15 +02:00
parent 33c488f8c1
commit c0d02ffc3d
4 changed files with 27 additions and 17 deletions

View File

@ -398,7 +398,7 @@ static inline void NetCopyLong(ulong *to, ulong *from)
char * ip_to_string (IPaddr_t x, char *s);
/* Convert a string to ip address */
IPaddr_t string_to_ip(const char *s);
int string_to_ip(const char *s, IPaddr_t *ip);
/* Convert a VLAN id to a string */
void VLAN_to_string (ushort x, char *s);

View File

@ -58,7 +58,12 @@ const char *dev_get_param(struct device_d *dev, const char *name)
#ifdef CONFIG_NET
IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
{
return string_to_ip(dev_get_param(dev, name));
IPaddr_t ip;
if (string_to_ip(dev_get_param(dev, name), &ip))
return 0;
return ip;
}
int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)

View File

@ -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)));

View File

@ -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;
}