diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index 3fdde4976..61628108c 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -487,8 +487,7 @@ errout: ****************************************************************************/ #ifdef HAVE_PING6 -static int nsh_gethostip(FAR char *hostname, FAR union ip_addr_u *ipaddr, - int addrtype) +static int nsh_gethostip(FAR char *hostname, FAR union ip_addr_u *ipaddr) { #ifdef CONFIG_LIBC_NETDB @@ -502,30 +501,15 @@ static int nsh_gethostip(FAR char *hostname, FAR union ip_addr_u *ipaddr, nerr("ERROR: gethostbyname failed: %d\n", h_errno); return -ENOENT; } - else if (he->h_addrtype != addrtype) - { - nerr("ERROR: gethostbyname returned an address of type: %d\n", - he->h_addrtype); - return -ENOEXEC; - } - else if (addrtype == AF_INET) - { - memcpy(&ipaddr->ipv4, he->h_addr, sizeof(in_addr_t)); - } - else /* if (addrtype == AF_INET6) */ + else if (he->h_addrtype == AF_INET6) { memcpy(ipaddr->ipv6, he->h_addr, sizeof(net_ipv6addr_t)); } - - if (he->h_addrtype != AF_INET6) - { - nerr("ERROR: gethostbyname returned an address of type: %d\n", - he->h_addrtype); - return -ENOEXEC; - } else { - memcpy(ipaddr->ipv6, he->h_addr, sizeof(net_ipv6addr_t)); + nerr("ERROR: gethostbyname returned an address of type: %d\n", + he->h_addrtype); + return -ENOEXEC; } return OK; @@ -1364,7 +1348,7 @@ int cmd_ping6(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) /* Get the IP address in binary form */ - ret = nsh_gethostip(staddr, (FAR union ip_addr_u *)&ipaddr, AF_INET6); + ret = nsh_gethostip(staddr, (FAR union ip_addr_u *)&ipaddr); if (ret < 0) { nsh_output(vtbl, g_fmtarginvalid, argv[0]); diff --git a/system/ping/ping.c b/system/ping/ping.c index d010cc45d..1ed84d781 100644 --- a/system/ping/ping.c +++ b/system/ping/ping.c @@ -49,6 +49,10 @@ #include #include +#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) +# include +#endif + #include #include @@ -107,6 +111,65 @@ static inline uint16_t ping_newid(void) return ++g_pingid; } +/**************************************************************************** + * Name: ping_gethostip + * + * Description: + * Call gethostbyname() to get the IP address associated with a hostname. + * + * Input Parameters + * hostname - The host name to use in the nslookup. + * ipv4addr - The location to return the IPv4 address. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int ping_gethostip(FAR char *hostname, FAR struct ping_info_s *info) +{ +#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) + /* Netdb DNS client support is enabled */ + + FAR struct hostent *he; + + he = gethostbyname(hostname); + if (he == NULL) + { + nerr("ERROR: gethostbyname failed: %d\n", h_errno); + return -ENOENT; + } + else if (he->h_addrtype = AF_INET) + { + memcpy(&info->dest, he->h_addr, sizeof(in_addr_t)); + } + else + { + nerr("ERROR: gethostbyname returned an address of type: %d\n", + he->h_addrtype); + return -ENOEXEC; + } + + return OK; + +#else /* CONFIG_LIBC_NETDB */ + + /* No host name support */ + /* Convert strings to numeric IPv6 address */ + + int ret = inet_pton(AF_INET, hostname, &info->dest); + + /* The inet_pton() function returns 1 if the conversion succeeds. It will + * return 0 if the input is not a valid IPv4 dotted-decimal string or a + * valid IPv6 address string, or -1 with errno set to EAFNOSUPPORT if + * the address family argument is unsupported. + */ + + return (ret > 0) ? OK : ERROR; + +#endif /* CONFIG_LIBC_NETDB */ +} + /**************************************************************************** * Name: do_ping ****************************************************************************/ @@ -452,9 +515,9 @@ int ping_main(int argc, char **argv) show_usage(argv[0], EXIT_FAILURE); } - if (inet_pton(AF_INET, argv[optind], &info->dest) == 0) + if (ping_gethostip(argv[optind], info) == 0) { - fprintf(stderr, "ERROR: inet_aton(%s) failed\n", argv[optind]); + fprintf(stderr, "ERROR: ping_gethostip(%s) failed\n", argv[optind]); goto errout_with_info; }