diff --git a/ChangeLog.txt b/ChangeLog.txt index 896408f12..8960de630 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1349,3 +1349,5 @@ * apps/netutils/netlib and other files: Create netlib wrapper functions around dns_getserver() and dns_setserver() to isolate application code from changes to those interfaces (2015-07-12). + * apps/nshlib: Add an nslookup command (2015-07-13). + diff --git a/nshlib/Kconfig b/nshlib/Kconfig index 41ddc479f..348d55d64 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -298,6 +298,11 @@ config NSH_DISABLE_NSFMOUNT default n depends on NFS +config NSH_DISABLE_NSLOOKUP + bool "Disable nslookup" + default n + depends on LIBC_NETDB && NETDB_DNSCLIENT + config NSH_DISABLE_POWEROFF bool "Disable poweroff" default n if !DEFAULT_SMALL && !BOARDCTL_RESET diff --git a/nshlib/README.txt b/nshlib/README.txt index 0d63b9f3a..16bf825ff 100644 --- a/nshlib/README.txt +++ b/nshlib/README.txt @@ -762,6 +762,10 @@ o nfsmount Mount the remote NFS server directory at on the target machine. is the IP address of the remote server. +o nslookup + + Lookup and print the IP address associated with + o ping [-c ] [-i ] ping6 [-c ] [-i ] @@ -1025,9 +1029,10 @@ Command Dependencies on Configuration Settings mount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE (see note 3) mv (((!CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_WRITABLE) || !CONFIG_DISABLE_PSEUDOFS_OPERATIONS) && CONFIG_NFILE_DESCRIPTORS > 0) (see note 4) nfsmount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NET && CONFIG_NFS - poweroff CONFIG_BOARDCTL_POWEROFF + nslookup CONFIG_LIBC_NETDB && CONFIG_NETDB_DNSCLIENT ping CONFIG_NET && CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING && !CONFIG_DISABLE_SIGNALS ping6 CONFIG_NET && CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING && !CONFIG_DISABLE_SIGNALS + poweroff CONFIG_BOARDCTL_POWEROFF ps -- put CONFIG_NET && CONFIG_NET_UDP && CONFIG_NFILE_DESCRIPTORS > 0 && MTU >= 558 (see note 1,2) pwd !CONFIG_DISABLE_ENVIRON && CONFIG_NFILE_DESCRIPTORS > 0 @@ -1073,14 +1078,14 @@ also allow it to squeeze into very small memory footprints. CONFIG_NSH_DISABLE_MD5 CONFIG_NSH_DISABLE_MB, CONFIG_NSH_DISABLE_MKDIR, CONFIG_NSH_DISABLE_MKFATFS, CONFIG_NSH_DISABLE_MKFIFO, CONFIG_NSH_DISABLE_MKRD, CONFIG_NSH_DISABLE_MH, CONFIG_NSH_DISABLE_MOUNT, CONFIG_NSH_DISABLE_MW, - CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_POWEROFF, - CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING, CONFIG_NSH_DISABLE_PING6, - CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, CONFIG_NSH_DISABLE_REBOOT, - CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, CONFIG_NSH_DISABLE_SET, - CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN, CONFIG_NSH_DISABLE_SLEEP, - CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT, CONFIG_NSH_DISABLE_UNSET, - CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, CONFIG_NSH_DISABLE_USLEEP, - CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD + CONFIG_NSH_DISABLE_MV, CONFIG_NSH_DISABLE_NFSMOUNT, CONFIG_NSH_DISABLE_NSLOOKUP, + CONFIG_NSH_DISABLE_POWEROFF, CONFIG_NSH_DISABLE_PS, CONFIG_NSH_DISABLE_PING, + CONFIG_NSH_DISABLE_PING6, CONFIG_NSH_DISABLE_PUT, CONFIG_NSH_DISABLE_PWD, + CONFIG_NSH_DISABLE_REBOOT, CONFIG_NSH_DISABLE_RM, CONFIG_NSH_DISABLE_RMDIR, + CONFIG_NSH_DISABLE_SET, CONFIG_NSH_DISABLE_SH, CONFIG_NSH_DISABLE_SHUTDOWN, + CONFIG_NSH_DISABLE_SLEEP, CONFIG_NSH_DISABLE_TEST, CONFIG_NSH_DISABLE_UMOUNT, + CONFIG_NSH_DISABLE_UNSET, CONFIG_NSH_DISABLE_URLDECODE, CONFIG_NSH_DISABLE_URLENCODE, + CONFIG_NSH_DISABLE_USLEEP, CONFIG_NSH_DISABLE_WGET, CONFIG_NSH_DISABLE_XD Verbose help output can be suppressed by defining CONFIG_NSH_HELP_TERSE. In that case, the help command is still available but will be slightly smaller. diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 503fe2275..c8a087a8b 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -988,6 +988,11 @@ void nsh_usbtrace(void); # endif #endif /* CONFIG_NET */ +#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); +#endif + #if defined(CONFIG_BOARDCTL_POWEROFF) && !defined(CONFIG_NSH_DISABLE_POWEROFF) int cmd_poweroff(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 294c8d3f4..0d18905d1 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -317,6 +317,11 @@ static const struct cmdmap_s g_cmdmap[] = # endif #endif +#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ + !defined(CONFIG_NSH_DISABLE_NSLOOKUP) + { "nslookup", cmd_nslookup, 2, 2, "" }, +#endif + #if defined(CONFIG_NET) && defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && !defined(CONFIG_DISABLE_SIGNALS) # ifndef CONFIG_NSH_DISABLE_PING { "ping", cmd_ping, 2, 6, "[-c ] [-i ] " }, diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index c241e0b57..aa6ab0058 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -38,8 +38,11 @@ ****************************************************************************/ #include + #ifdef CONFIG_NET +#include + #include /* Needed for open */ #include #include @@ -54,6 +57,12 @@ #include #include +#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) +# ifndef CONFIG_NSH_DISABLE_NSLOOKUP +# include +# endif +#endif + #include #include #include @@ -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 ****************************************************************************/