From 8da81578b0e5822719ca764416ebb6a74e568fa4 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 30 Mar 2020 18:06:47 +0800 Subject: [PATCH] nsh: Call getaddrinfo in cmd_nslookup to support the dual stack host Signed-off-by: Xiang Xiao Change-Id: Iac7a9a6b871d473e4720db1d6cbfeea3dc9796a0 --- nshlib/nsh.h | 15 ++++---- nshlib/nsh_command.c | 3 +- nshlib/nsh_netcmds.c | 82 ++++++++++++++------------------------------ 3 files changed, 35 insertions(+), 65 deletions(-) diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 16c2387fa..feed7bd7d 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -440,11 +440,15 @@ #endif #ifdef CONFIG_NSH_STRERROR -# define NSH_ERRNO strerror(errno) -# define NSH_ERRNO_OF(err) strerror(err) +# define NSH_ERRNO strerror(errno) +# define NSH_ERRNO_OF(err) strerror(err) +# define NSH_HERRNO gai_strerror(h_errno) +# define NSH_HERRNO_OF(err) gai_strerror(err) #else -# define NSH_ERRNO (errno) -# define NSH_ERRNO_OF(err) (err) +# define NSH_ERRNO (errno) +# define NSH_ERRNO_OF(err) (err) +# define NSH_HERRNO (h_errno) +# define NSH_HERRNO_OF(err) (err) #endif /* 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 /* CONFIG_NET */ -#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ - !defined(CONFIG_NSH_DISABLE_NSLOOKUP) +#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP) int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index cb97dfda2..819bfae54 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -372,8 +372,7 @@ static const struct cmdmap_s g_cmdmap[] = # endif #endif -#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ - !defined(CONFIG_NSH_DISABLE_NSLOOKUP) +#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP) { "nslookup", cmd_nslookup, 2, 2, "" }, #endif diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index a85ac2937..d103d5c33 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -58,10 +58,8 @@ #include #include -#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) -# ifndef CONFIG_NSH_DISABLE_NSLOOKUP -# include -# endif +#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP) +# include #endif #include @@ -961,82 +959,52 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) } #endif - /**************************************************************************** * Name: cmd_nslookup ****************************************************************************/ -#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ - !defined(CONFIG_NSH_DISABLE_NSLOOKUP) +#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP) int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { - FAR struct hostent *host; - FAR const char *addrtype; + FAR struct addrinfo *info; + FAR struct addrinfo *next; char buffer[48]; + int ret; /* We should be guaranteed this by the command line parser */ DEBUGASSERT(argc == 2); - /* Get the matching address + any aliases */ + /* Get the matching address + canonical name */ - host = gethostbyname(argv[1]); - if (!host) + ret = getaddrinfo(argv[1], NULL, NULL, &info); + if (ret != OK) { - /* REVISIT: gethostbyname() does not set errno, but h_errno */ - - nsh_error(vtbl, g_fmtcmdfailed, argv[0], "gethostbyname", NSH_ERRNO); - return ERROR; + nsh_error(vtbl, g_fmtcmdfailed, + argv[0], "getaddrinfo", NSH_HERRNO_OF(ret)); + return ERROR; } - /* Convert the address to a string */ - /* Handle IPv4 addresses */ - - if (host->h_addrtype == AF_INET) + for (next = info; next != NULL; next = next->ai_next) { - if (inet_ntop(AF_INET, host->h_addr, buffer, 48) == NULL) + /* Convert the address to a string */ + + ret = getnameinfo(next->ai_addr, next->ai_addrlen, + buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST); + if (ret != OK) { - nsh_error(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO); + freeaddrinfo(info); + nsh_error(vtbl, g_fmtcmdfailed, + argv[0], "getnameinfo", NSH_HERRNO_OF(ret)); 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 */ - - nsh_output(vtbl, "Host: %s %s Addr: %s\n", host->h_name, addrtype, 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"); + /* Print the host name / address mapping */ + + nsh_output(vtbl, "Host: %s Addr: %s\n", next->ai_canonname, buffer); } + freeaddrinfo(info); return OK; } #endif