From 05f608322ed8fbfb76b80be663050e54bc64d6e6 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 8 Jul 2015 16:02:18 -0600 Subject: [PATCH] Add a command to access the network database --- ChangeLog.txt | 3 + system/Kconfig | 1 + system/Make.defs | 4 + system/Makefile | 2 +- system/netdb/.gitignore | 11 ++ system/netdb/Kconfig | 21 ++++ system/netdb/Makefile | 116 +++++++++++++++++++++ system/netdb/netdb_main.c | 205 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 362 insertions(+), 1 deletion(-) create mode 100644 system/netdb/.gitignore create mode 100644 system/netdb/Kconfig create mode 100644 system/netdb/Makefile create mode 100644 system/netdb/netdb_main.c diff --git a/ChangeLog.txt b/ChangeLog.txt index 202a6300f..848ce589f 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1336,3 +1336,6 @@ * apps/nshlib: Add support for a uname command (2015-07-04). * apps/system/sysinfo: Remove the system sysinfo command. This is replaced with the NSH uname command (2015-07-04). + * apps/syste/netdb: Add a command to access the network database + (2015-07-08). + diff --git a/system/Kconfig b/system/Kconfig index adb8dc927..7e4a61fe8 100644 --- a/system/Kconfig +++ b/system/Kconfig @@ -11,6 +11,7 @@ source "$APPSDIR/system/flash_eraseall/Kconfig" source "$APPSDIR/system/hex2bin/Kconfig" source "$APPSDIR/system/i2c/Kconfig" source "$APPSDIR/system/inifile/Kconfig" +source "$APPSDIR/system/netdb/Kconfig" source "$APPSDIR/system/nxplayer/Kconfig" source "$APPSDIR/system/ramtest/Kconfig" source "$APPSDIR/system/readline/Kconfig" diff --git a/system/Make.defs b/system/Make.defs index 07d9bb0bc..c7acecd61 100644 --- a/system/Make.defs +++ b/system/Make.defs @@ -70,6 +70,10 @@ ifeq ($(CONFIG_SYSTEM_FLASH_ERASEALL),y) CONFIGURED_APPS += system/flash_eraseall endif +ifeq ($(CONFIG_SYSTEM_NETDB),y) +CONFIGURED_APPS += system/netdb +endif + ifeq ($(CONFIG_SYSTEM_NXPLAYER),y) CONFIGURED_APPS += system/nxplayer endif diff --git a/system/Makefile b/system/Makefile index 4f8aef185..f23bbfdcb 100644 --- a/system/Makefile +++ b/system/Makefile @@ -38,7 +38,7 @@ # Sub-directories containing system tasks/libraries SUBDIRS = cdcacm cle composite cu flash_eraseall free i2c hex2bin inifile -SUBDIRS += install lm75 mdio nxplayer ramtest ramtron readline sdcard +SUBDIRS += install lm75 mdio netdb nxplayer ramtest ramtron readline sdcard SUBDIRS += stackmonitor sudoku usbmonitor usbmsc vi zmodem zoneinfo # Create the list of installed runtime modules (INSTALLED_DIRS) diff --git a/system/netdb/.gitignore b/system/netdb/.gitignore new file mode 100644 index 000000000..83bd7b811 --- /dev/null +++ b/system/netdb/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src +/*.obj diff --git a/system/netdb/Kconfig b/system/netdb/Kconfig new file mode 100644 index 000000000..302f17af2 --- /dev/null +++ b/system/netdb/Kconfig @@ -0,0 +1,21 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +menuconfig SYSTEM_NETDB + bool "netdb interface" + default n + depends on LIB_NETDB + +if SYSTEM_NETDB + +config SYSTEM_NETDB_STACKSIZE + int "netdb task stack size" + default 2048 + +config SYSTEM_NETDB_PRIORITY + int "netdb task priority" + default 100 + +endif diff --git a/system/netdb/Makefile b/system/netdb/Makefile new file mode 100644 index 000000000..99431f096 --- /dev/null +++ b/system/netdb/Makefile @@ -0,0 +1,116 @@ +############################################################################ +# apps/system/netdb/Makefile +# +# Copyright (C) 2015 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +ifeq ($(WINTOOL),y) +INCDIROPT = -w +endif + +# netdb Application + +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 768 + +ASRCS = +CSRCS = netdb_main.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + $(Q) touch .built + +install: + +# Register application + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +$(BUILTIN_REGISTRY)$(DELIM)netdb.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,"netdb",$(PRIORITY),$(STACKSIZE),netdb_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)netdb.bdat +else +context: +endif + +# Create dependencies + +.depend: Makefile $(SRCS) + $(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + $(Q) touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/system/netdb/netdb_main.c b/system/netdb/netdb_main.c new file mode 100644 index 000000000..a094ca38e --- /dev/null +++ b/system/netdb/netdb_main.c @@ -0,0 +1,205 @@ +/**************************************************************************** + * apps/system/netdb/netdb.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#ifdef CONFIG_SYSTEM_NETDB + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Configuration ************************************************************/ + +#ifndef CONFIG_SYSTEM_NETDB_STACKSIZE +# define CONFIG_SYSTEM_NETDB_STACKSIZE 2048 +#endif + +#ifndef CONFIG_SYSTEM_NETDB_PRIORITY +# define CONFIG_SYSTEM_NETDB_PRIORITY 50 +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void show_usage(FAR const char *progname, int exitcode) noreturn_function; +static void show_usage(FAR const char *progname, int exitcode) +{ + fprintf(stderr, "USAGE: %s --ipv4 \n", progname); + fprintf(stderr, " %s --ipv6 \n", progname); + fprintf(stderr, " %s --host \n", progname); + fprintf(stderr, " %s --help\n", progname); + exit(exitcode); +} + + /**************************************************************************** + * Public Functions + ****************************************************************************/ + +int netdb_main(int argc, char **argv) +{ + FAR struct hostent *host; + char buffer[48]; + int ret; + + if (argc == 2 && strcmp(argv[1], "--help") == 0) + { + show_usage(argv[0], EXIT_SUCCESS); + } + else if (argc < 3) + { + fprintf(stderr, "ERROR -- Missing arguments\n\n"); + show_usage(argv[0], EXIT_FAILURE); + } + else if (argc > 3) + { + fprintf(stderr, "ERROR -- Too many arguments\n\n"); + show_usage(argv[0], EXIT_FAILURE); + } + else if (strcmp(argv[1], "--ipv4") == 0) + { + struct in_addr addr; + + ret = inet_pton(AF_INET, argv[2], &addr); + if (ret < 0) + { + fprintf(stderr, "ERROR -- Bad IPv4 address\n\n"); + show_usage(argv[0], EXIT_FAILURE); + } + + host = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET); + if (!host) + { + fprintf(stderr, "ERROR -- gethostbyaddr failed. h_errno=%d\n\n", + h_errno); + show_usage(argv[0], EXIT_FAILURE); + } + + printf("Addr: %s Host: %s\n", argv[2], host->h_name); + return EXIT_SUCCESS; + } + else if (strcmp(argv[1], "--ipv6") == 0) + { + struct in_addr addr; + + ret = inet_pton(AF_INET6, argv[2], &addr); + if (ret < 0) + { + fprintf(stderr, "ERROR -- Bad IPv6 address\n\n"); + show_usage(argv[0], EXIT_FAILURE); + } + + host = gethostbyaddr(&addr, sizeof(struct in6_addr), AF_INET6); + if (!host) + { + fprintf(stderr, "ERROR -- gethostbyaddr failed. h_errno=%d\n\n", + h_errno); + show_usage(argv[0], EXIT_FAILURE); + } + + printf("Addr: %s Host: %s\n", argv[2], host->h_name); + return EXIT_SUCCESS; + } + else if (strcmp(argv[1], "--host") == 0) + { + FAR const char *addrtype; + + host = gethostbyname(argv[2]); + if (!host) + { + fprintf(stderr, "ERROR -- gethostbyname failed. h_errno=%d\n\n", + h_errno); + show_usage(argv[0], EXIT_FAILURE); + } + + if (host->h_addrtype == AF_INET) + { + if (inet_ntop(AF_INET, host->h_addr, buffer, + sizeof(struct in_addr)) == NULL) + { + fprintf(stderr, + "ERROR -- gethostbyname failed. h_errno=%d\n\n", + h_errno); + show_usage(argv[0], EXIT_FAILURE); + } + + addrtype = "IPv4"; + } + else if (host->h_addrtype == AF_INET6) + { + if (inet_ntop(AF_INET6, host->h_addr, buffer, + sizeof(struct in6_addr)) == NULL) + { + fprintf(stderr, + "ERROR -- gethostbyname failed. h_errno=%d\n\n", + h_errno); + show_usage(argv[0], EXIT_FAILURE); + } + + addrtype = "IPv6"; + } + else + { + fprintf(stderr, "ERROR -- gethostbyname address type&d not recognized.\n\n", + host->h_addrtype); + show_usage(argv[0], EXIT_FAILURE); + } + + printf("Host: %s %s Addr: %s\n", argv[2], addrtype, buffer); + return EXIT_SUCCESS; + } + else + { + fprintf(stderr, "ERROR -- Unrecognized option: %s\n\n", argv[1]); + show_usage(argv[0], EXIT_FAILURE); + } +} + +#endif /* CONFIG_SYSTEM_NETDB */