Add an NSH nslookup command

This commit is contained in:
Gregory Nutt
2015-07-13 11:19:02 -06:00
parent 7e43ffa602
commit 25d45d642f
6 changed files with 120 additions and 9 deletions

View File

@@ -38,8 +38,11 @@
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#include <nuttx/compiler.h>
#include <sys/stat.h> /* Needed for open */
#include <stdint.h>
#include <stdbool.h>
@@ -54,6 +57,12 @@
#include <errno.h>
#include <debug.h>
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
# ifndef CONFIG_NSH_DISABLE_NSLOOKUP
# include <netdb.h>
# endif
#endif
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
@@ -1268,6 +1277,86 @@ 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)
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR struct hostent *host;
FAR const char *addrtype;
char buffer[48];
/* We should be guaranteed this by the command line parser */
DEBUGASSERT(argc == 2);
/* Get the matching address + any aliases */
host = gethostbyname(argv[1]);
if (!host)
{
/* REVISIT: gethostbyname() does not set errno, but h_errno */
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "gethostbyname", NSH_ERRNO);
return ERROR;
}
/* Convert the address to a string */
/* Handle IPv4 addresses */
if (host->h_addrtype == AF_INET)
{
if (inet_ntop(AF_INET, host->h_addr, buffer, 48) == NULL)
{
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO);
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_output(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");
}
return OK;
}
#endif
/****************************************************************************
* Name: cmd_ping
****************************************************************************/