nsh: Call getaddrinfo in cmd_nslookup to support the dual stack host

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Iac7a9a6b871d473e4720db1d6cbfeea3dc9796a0
This commit is contained in:
Xiang Xiao 2020-03-30 18:06:47 +08:00 committed by patacongo
parent 326c80db72
commit 8da81578b0
3 changed files with 35 additions and 65 deletions

View File

@ -442,9 +442,13 @@
#ifdef CONFIG_NSH_STRERROR #ifdef CONFIG_NSH_STRERROR
# define NSH_ERRNO strerror(errno) # define NSH_ERRNO strerror(errno)
# define NSH_ERRNO_OF(err) strerror(err) # define NSH_ERRNO_OF(err) strerror(err)
# define NSH_HERRNO gai_strerror(h_errno)
# define NSH_HERRNO_OF(err) gai_strerror(err)
#else #else
# define NSH_ERRNO (errno) # define NSH_ERRNO (errno)
# define NSH_ERRNO_OF(err) (err) # define NSH_ERRNO_OF(err) (err)
# define NSH_HERRNO (h_errno)
# define NSH_HERRNO_OF(err) (err)
#endif #endif
/* Maximum size of one command line (telnet or serial) */ /* Maximum size of one command line (telnet or serial) */
@ -1136,8 +1140,7 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif # endif
#endif /* CONFIG_NET */ #endif /* CONFIG_NET */
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ #if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
!defined(CONFIG_NSH_DISABLE_NSLOOKUP)
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
#endif #endif

View File

@ -372,8 +372,7 @@ static const struct cmdmap_s g_cmdmap[] =
# endif # endif
#endif #endif
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ #if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
!defined(CONFIG_NSH_DISABLE_NSLOOKUP)
{ "nslookup", cmd_nslookup, 2, 2, "<host-name>" }, { "nslookup", cmd_nslookup, 2, 2, "<host-name>" },
#endif #endif

View File

@ -58,11 +58,9 @@
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) #if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
# ifndef CONFIG_NSH_DISABLE_NSLOOKUP
# include <netdb.h> # include <netdb.h>
#endif #endif
#endif
#include <net/ethernet.h> #include <net/ethernet.h>
#include <net/if.h> #include <net/if.h>
@ -961,82 +959,52 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
} }
#endif #endif
/**************************************************************************** /****************************************************************************
* Name: cmd_nslookup * Name: cmd_nslookup
****************************************************************************/ ****************************************************************************/
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ #if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
!defined(CONFIG_NSH_DISABLE_NSLOOKUP)
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{ {
FAR struct hostent *host; FAR struct addrinfo *info;
FAR const char *addrtype; FAR struct addrinfo *next;
char buffer[48]; char buffer[48];
int ret;
/* We should be guaranteed this by the command line parser */ /* We should be guaranteed this by the command line parser */
DEBUGASSERT(argc == 2); DEBUGASSERT(argc == 2);
/* Get the matching address + any aliases */ /* Get the matching address + canonical name */
host = gethostbyname(argv[1]); ret = getaddrinfo(argv[1], NULL, NULL, &info);
if (!host) if (ret != OK)
{ {
/* REVISIT: gethostbyname() does not set errno, but h_errno */ nsh_error(vtbl, g_fmtcmdfailed,
argv[0], "getaddrinfo", NSH_HERRNO_OF(ret));
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "gethostbyname", NSH_ERRNO);
return ERROR; return ERROR;
} }
for (next = info; next != NULL; next = next->ai_next)
{
/* Convert the address to a string */ /* Convert the address to a string */
/* Handle IPv4 addresses */
if (host->h_addrtype == AF_INET) ret = getnameinfo(next->ai_addr, next->ai_addrlen,
buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST);
if (ret != OK)
{ {
if (inet_ntop(AF_INET, host->h_addr, buffer, 48) == NULL) freeaddrinfo(info);
{ nsh_error(vtbl, g_fmtcmdfailed,
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO); argv[0], "getnameinfo", NSH_HERRNO_OF(ret));
return ERROR; return ERROR;
} }
addrtype = "IPv4";
}
/* Handle IPv6 addresses */
else /* if (host->h_addrtype == AF_INET6) */
{
DEBUGASSERT(host->h_addrtype == AF_INET6);
if (inet_ntop(AF_INET6, host->h_addr, buffer, 48) == NULL)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO);
return ERROR;
}
addrtype = "IPv6";
}
/* Print the host name / address mapping */ /* Print the host name / address mapping */
nsh_output(vtbl, "Host: %s %s Addr: %s\n", host->h_name, addrtype, buffer); nsh_output(vtbl, "Host: %s Addr: %s\n", next->ai_canonname, buffer);
/* Print any host name aliases */
if (host->h_aliases != NULL && *host->h_aliases != NULL)
{
FAR char **alias;
nsh_output(vtbl, "Aliases:");
for (alias = host->h_aliases; *alias != NULL; alias++)
{
nsh_output(vtbl, " %s", *alias);
}
nsh_output(vtbl, "\n");
} }
freeaddrinfo(info);
return OK; return OK;
} }
#endif #endif