diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2018-11-29 11:17:08 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2018-11-29 15:04:31 +0100 |
commit | 4b48ae4bcdbc137c80b77adc5aa614e5f4cb9f51 (patch) | |
tree | ec502443c6dd7e665a8b420d49eb5ae811de7775 /net | |
parent | cd9a7567b0d5afd874dcd31fd22918868e9eab9e (diff) | |
download | barebox-4b48ae4bcdbc137c80b77adc5aa614e5f4cb9f51.tar.gz barebox-4b48ae4bcdbc137c80b77adc5aa614e5f4cb9f51.tar.xz |
net: dns: return error codes
The resolv() function used to return the IP address. When net_udp_new()
fails we return an error code though which the callers of resolv() take
as an IP address. This is wrong of course and we could return 0 in this
case. Instead we return an error code and pass the resolved IP as a
pointer which allows us to return proper error codes.
This patch also adds error messages and error returns to the various
callers of resolv() which used to just continue with a zero IP and
let the user figure out what went wrong.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'net')
-rw-r--r-- | net/dhcp.c | 6 | ||||
-rw-r--r-- | net/dns.c | 36 | ||||
-rw-r--r-- | net/net.c | 4 | ||||
-rw-r--r-- | net/ping.c | 6 | ||||
-rw-r--r-- | net/sntp.c | 6 |
5 files changed, 35 insertions, 23 deletions
diff --git a/net/dhcp.c b/net/dhcp.c index 79aa75d878..670a6a6422 100644 --- a/net/dhcp.c +++ b/net/dhcp.c @@ -562,6 +562,8 @@ out: int dhcp_set_result(struct eth_device *edev, struct dhcp_result *res) { + int ret; + net_set_ip(edev, res->ip); net_set_netmask(edev, res->netmask); net_set_gateway(res->gateway); @@ -580,8 +582,8 @@ int dhcp_set_result(struct eth_device *edev, struct dhcp_result *res) if (res->tftp_server_name) { IPaddr_t ip; - ip = resolv(res->tftp_server_name); - if (ip) + ret = resolv(res->tftp_server_name, &ip); + if (!ret) net_set_serverip_empty(ip); } else if (res->serverip) { net_set_serverip_empty(res->serverip); @@ -201,26 +201,27 @@ static void dns_handler(void *ctx, char *packet, unsigned len) net_eth_to_udplen(packet)); } -IPaddr_t resolv(const char *host) +int resolv(const char *host, IPaddr_t *ip) { - IPaddr_t ip; + IPaddr_t nameserver; - if (!string_to_ip(host, &ip)) - return ip; + if (!string_to_ip(host, ip)) + return 0; dns_ip = 0; + *ip = 0; dns_state = STATE_INIT; - ip = net_get_nameserver(); - if (!ip) { + nameserver = net_get_nameserver(); + if (!nameserver) { pr_err("no nameserver specified in $net.nameserver\n"); return 0; } - pr_debug("resolving host %s via nameserver %pI4\n", host, &ip); + pr_debug("resolving host %s via nameserver %pI4\n", host, &nameserver); - dns_con = net_udp_new(ip, DNS_PORT, dns_handler, NULL); + dns_con = net_udp_new(nameserver, DNS_PORT, dns_handler, NULL); if (IS_ERR(dns_con)) return PTR_ERR(dns_con); dns_timer_start = get_time_ns(); @@ -240,25 +241,32 @@ IPaddr_t resolv(const char *host) net_unregister(dns_con); - pr_debug("host %s is at %pI4\n", host, &dns_ip); + if (dns_ip) { + pr_debug("host %s is at %pI4\n", host, &dns_ip); + } else { + pr_debug("host %s not found\n", host); + return -ENOENT; + } + + *ip = dns_ip; - return dns_ip; + return 0; } #ifdef CONFIG_CMD_HOST static int do_host(int argc, char *argv[]) { IPaddr_t ip; + int ret; if (argc != 2) return COMMAND_ERROR_USAGE; - ip = resolv(argv[1]); - if (!ip) + ret = resolv(argv[1], &ip); + if (ret) printf("unknown host %s\n", argv[1]); - else { + else printf("%s is at %pI4\n", argv[1], &ip); - } return 0; } @@ -107,7 +107,9 @@ IPaddr_t getenv_ip(const char *name) if (!string_to_ip(var, &ip)) return ip; - return resolv((char*)var); + resolv(var, &ip); + + return ip; } int setenv_ip(const char *name, IPaddr_t ip) diff --git a/net/ping.c b/net/ping.c index b5926280e0..a71f59a805 100644 --- a/net/ping.c +++ b/net/ping.c @@ -61,9 +61,9 @@ static int do_ping(int argc, char *argv[]) if (argc < 2) return COMMAND_ERROR_USAGE; - net_ping_ip = resolv(argv[1]); - if (!net_ping_ip) { - printf("unknown host %s\n", argv[1]); + ret = resolv(argv[1], &net_ping_ip); + if (ret) { + printf("Cannot resolve \"%s\": %s\n", argv[1], strerror(-ret)); return 1; } diff --git a/net/sntp.c b/net/sntp.c index 577c859616..b4e6d6439c 100644 --- a/net/sntp.c +++ b/net/sntp.c @@ -120,9 +120,9 @@ s64 sntp(const char *server) if (!server) return -EINVAL; - net_sntp_ip = resolv(server); - if (!net_sntp_ip) { - printf("unknown host %s\n", server); + ret = resolv(server, &net_sntp_ip); + if (ret) { + printf("Cannot resolve \"%s\": %s\n", server, strerror(-ret));; return 1; } |