From 4f64213758235cc6a664b0d0675406a401392f82 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 10:58:41 -0600 Subject: [PATCH 01/23] examples/udp: Renaming some files to prevent name collision in libapps.a --- examples/udp/Makefile | 15 ++++++++------- examples/udp/{host.c => udp_host.c} | 2 +- examples/udp/{target1.c => udp_target1.c} | 2 +- examples/udp/{target2.c => udp_target2.c} | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) rename examples/udp/{host.c => udp_host.c} (99%) rename examples/udp/{target1.c => udp_target1.c} (98%) rename examples/udp/{target2.c => udp_target2.c} (98%) diff --git a/examples/udp/Makefile b/examples/udp/Makefile index 0627426e6..1e80bba84 100644 --- a/examples/udp/Makefile +++ b/examples/udp/Makefile @@ -1,7 +1,7 @@ ############################################################################ # apps/examples/udp/Makefile # -# Copyright (C) 2007-2008, 2011-2012, 2014 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2008, 2011-2012, 2014, 2017 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -53,7 +53,7 @@ TARG1_CRCS += udp_server.c else TARG1_CRCS += udp_client.c endif -TARG1_MAINSRC = target1.c +TARG1_MAINSRC = udp_target1.c TARG1_COBJS = $(TARG1_CRCS:.c=$(OBJEXT)) TARG1_MAINOBJ = $(TARG1_MAINSRC:.c=$(OBJEXT)) @@ -82,7 +82,7 @@ TARG2_CRCS += udp_client.c else TARG2_CRCS += udp_server.c endif -TARG2_MAINSRC = target2.c +TARG2_MAINSRC = udp_target2.c TARG2_COBJS = $(TARG2_CRCS:.c=$(OBJEXT)) TARG2_MAINOBJ = $(TARG2_MAINSRC:.c=$(OBJEXT)) @@ -97,6 +97,7 @@ endif CONFIG_EXAMPLES_UDP_PRIORITY2 ?= 100 CONFIG_EXAMPLES_UDP_STACKSIZE2 ?= 2048 + PROGNAME2 = $(CONFIG_EXAMPLES_UDP_PROGNAME2) PRIORITY2 = $(CONFIG_EXAMPLES_UDP_PRIORITY2) STACKSIZE2 = $(CONFIG_EXAMPLES_UDP_STACKSIZE2) @@ -117,16 +118,16 @@ ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) HOSTCFLAGS += -DEXAMPLES_UDP_HOST=1 HOSTOBJSEXT ?= o1 -HOST_SRCS = host.c udp_cmdline.c +HOST_SRCS = udp_host.c udp_cmdline.c ifeq ($(CONFIG_EXAMPLES_UDP_SERVER1),y) HOST_SRCS += udp_client.c +HOST_BIN = udpclient$(EXEEXT) else HOST_SRCS += udp_server.c +HOST_BIN = udpserver$(EXEEXT) endif HOST_OBJS = $(HOST_SRCS:.c=.$(HOSTOBJSEXT)) -HOST_BIN = host$(EXEEXT) - endif ifeq ($(CONFIG_WINDOWS_NATIVE),y) @@ -161,7 +162,7 @@ VPATH = all: .built .PHONY: clean depend distclean preconfig -$(TARG1_COBJS) $(TARG1_MAINOBJ)$(TARG2_COBJS) $(TARG2_MAINOBJ) $(TARGCMN_COBJS): %$(OBJEXT): %.c +$(TARG1_COBJS) $(TARG1_MAINOBJ) $(TARG2_COBJS) $(TARG2_MAINOBJ) $(TARGCMN_COBJS): %$(OBJEXT): %.c $(call COMPILE, $<, $@) $(TARG_BIN): $(TARG_OBJS) $(HOST_BIN) diff --git a/examples/udp/host.c b/examples/udp/udp_host.c similarity index 99% rename from examples/udp/host.c rename to examples/udp/udp_host.c index f51410913..7e1aaacc5 100644 --- a/examples/udp/host.c +++ b/examples/udp/udp_host.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/udp/host.c + * examples/udp/udp_host.c * * Copyright (C) 2007 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/udp/target1.c b/examples/udp/udp_target1.c similarity index 98% rename from examples/udp/target1.c rename to examples/udp/udp_target1.c index f64c7e098..ce655aec5 100644 --- a/examples/udp/target1.c +++ b/examples/udp/udp_target1.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/udp/target1.c + * examples/udp/udp_target1.c * * Copyright (C) 2007, 2011, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/udp/target2.c b/examples/udp/udp_target2.c similarity index 98% rename from examples/udp/target2.c rename to examples/udp/udp_target2.c index 84332504b..0fcb6e37c 100644 --- a/examples/udp/target2.c +++ b/examples/udp/udp_target2.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/udp/target2.c + * examples/udp/udp_target2.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt From 41457d6fe8cd3d6cf5db4737967f665bd8b76159 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 11:23:43 -0600 Subject: [PATCH 02/23] examples/nettest: Renaming some files to prevent name collision in libapps.a and to prepare to support target boards on both ends of the test --- examples/nettest/Makefile | 62 +++++++++---------- examples/nettest/{host.c => nettest_host.c} | 2 +- .../nettest/{nettest.c => nettest_target1.c} | 2 +- examples/udp/Makefile | 2 +- 4 files changed, 31 insertions(+), 37 deletions(-) rename examples/nettest/{host.c => nettest_host.c} (98%) rename examples/nettest/{nettest.c => nettest_target1.c} (99%) diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index a76a15d46..e51da8fdd 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -1,7 +1,7 @@ ############################################################################ # examples/nettest/Makefile # -# Copyright (C) 2007-2008, 2010-2012 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2008, 2010-2012, 2017 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -39,27 +39,24 @@ include $(APPDIR)/Make.defs # Basic TCP networking test -TARG_ASRCS = -TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT)) - -TARG_CSRCS = +TARG1_CSRCS = ifeq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) -TARG_CSRCS += nettest_server.c nettest_client.c +TARG1_CSRCS += nettest_server.c nettest_client.c else ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER),y) -TARG_CSRCS += nettest_server.c +TARG1_CSRCS += nettest_server.c else -TARG_CSRCS += nettest_client.c +TARG1_CSRCS += nettest_client.c endif -TARG_MAINSRC = nettest.c +TARG1_MAINSRC = nettest_target1.c -TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT)) -TARG_MAINOBJ = $(TARG_MAINSRC:.c=$(OBJEXT)) +TARG1_COBJS = $(TARG1_CSRCS:.c=$(OBJEXT)) +TARG1_MAINOBJ = $(TARG1_MAINSRC:.c=$(OBJEXT)) -TARG_SRCS = $(TARG_ASRCS) $(TARG_CSRCS) $(TARG_CSRCS) -TARG_OBJS = $(TARG_AOBJS) $(TARG_COBJS) +TARG_CSRCS = $(TARG1_CSRCS) $(TARG1_CSRCS) +TARG_OBJS = $(TARG1_COBJS) ifneq ($(CONFIG_BUILD_KERNEL),y) - TARG_OBJS += $(TARG_MAINOBJ) + TARG_OBJS += $(TARG1_MAINOBJ) endif ifeq ($(CONFIG_WINDOWS_NATIVE),y) @@ -81,16 +78,17 @@ ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) HOSTCFLAGS += -DCONFIG_EXAMPLES_NETTEST_PERFORMANCE=1 endif - HOST_SRCS = host.c + HOST_SRCS = nettest_host.c ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER),y) - HOST_SRCS += nettest_client.c + HOST_SRCS += nettest_client.c + HOST_BIN = tcpclient$(EXEEXT) else - HOST_SRCS += nettest_server.c + HOST_SRCS += nettest_server.c + HOST_BIN = tcpserver$(EXEEXT) endif - HOSTOBJEXT ?= .hobj - HOST_OBJS = $(HOST_SRCS:.c=$(HOSTOBJEXT)) - HOST_BIN = host + HOSTOBJEXT ?= hobj + HOST_OBJS = $(HOST_SRCS:.c=.$(HOSTOBJEXT)) endif ifeq ($(WINTOOL),y) @@ -118,16 +116,13 @@ PROGNAME = $(CONFIG_EXAMPLES_HELLO_PROGNAME) VPATH = all: .built $(HOST_BIN) -.PHONY: clean depend distclean +.PHONY: clean depend distclean preconfig -$(TARG_AOBJS): %$(OBJEXT): %.S - $(call ASSEMBLE, $<, $@) - -$(TARG_COBJS) $(TARG_MAINOBJ): %$(OBJEXT): %.c +$(TARG1_COBJS) $(TARG1_MAINOBJ): %$(OBJEXT): %.c $(call COMPILE, $<, $@) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) -$(HOST_OBJS): %$(HOSTOBJEXT): %.c +$(HOST_OBJS): %.$(HOSTOBJEXT): %.c @echo "CC: $<" $(Q) $(HOSTCC) -c $(HOSTCFLAGS) $< -o $@ endif @@ -147,9 +142,9 @@ endif $(Q) touch .built ifeq ($(CONFIG_BUILD_KERNEL),y) -$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(TARG_MAINOBJ) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(TARG1_MAINOBJ) @echo "LD: $(PROGNAME)" - $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(TARG_MAINOBJ) $(LDLIBS) + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(TARG1_MAINOBJ) $(LDLIBS) $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) install: $(BIN_DIR)$(DELIM)$(PROGNAME) @@ -168,15 +163,15 @@ else context: endif -.depend: Makefile config.h $(TARG_SRCS) - @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep +.depend: Makefile config.h $(TARG_CSRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(TARG_CSRCS) >Make.dep @touch $@ depend: .depend clean: ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) - $(call DELFILE, *$(HOSTOBJEXT)) + $(call DELFILE, *.$(HOSTOBJEXT)) $(call DELFILE, $(HOST_BIN)) endif $(call DELFILE, .built) @@ -188,7 +183,6 @@ distclean: clean $(call DELFILE, Make.dep) $(call DELFILE, .depend) --include Make.dep - -.PHONY: preconfig preconfig: + +-include Make.dep diff --git a/examples/nettest/host.c b/examples/nettest/nettest_host.c similarity index 98% rename from examples/nettest/host.c rename to examples/nettest/nettest_host.c index c376dac8e..f89477655 100644 --- a/examples/nettest/host.c +++ b/examples/nettest/nettest_host.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/nettest/host.c + * examples/nettest/nettest_host.c * * Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/nettest/nettest.c b/examples/nettest/nettest_target1.c similarity index 99% rename from examples/nettest/nettest.c rename to examples/nettest/nettest_target1.c index ca5437b80..52aed8a13 100644 --- a/examples/nettest/nettest.c +++ b/examples/nettest/nettest_target1.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/nettest/nettest.c + * examples/nettest/nettest_target1.c * * Copyright (C) 2007, 2009-2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/udp/Makefile b/examples/udp/Makefile index 1e80bba84..b53a053c6 100644 --- a/examples/udp/Makefile +++ b/examples/udp/Makefile @@ -116,7 +116,7 @@ endif ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) HOSTCFLAGS += -DEXAMPLES_UDP_HOST=1 -HOSTOBJSEXT ?= o1 +HOSTOBJSEXT ?= hobj HOST_SRCS = udp_host.c udp_cmdline.c ifeq ($(CONFIG_EXAMPLES_UDP_SERVER1),y) From c88cf75064f3fd7a370164ab3adc78e3732a8154 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 11:40:02 -0600 Subject: [PATCH 03/23] examples/nettest: Separate out network initialization so that it may, eventually, be used by both a target server and a target client. --- examples/nettest/Makefile | 15 ++- examples/nettest/nettest.h | 4 + examples/nettest/nettest_target1.c | 148 +--------------------- examples/nettest/target_netinit.c | 193 +++++++++++++++++++++++++++++ examples/udp/Makefile | 2 +- 5 files changed, 211 insertions(+), 151 deletions(-) create mode 100644 examples/nettest/target_netinit.c diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index e51da8fdd..2b3e0d99b 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -39,6 +39,15 @@ include $(APPDIR)/Make.defs # Basic TCP networking test +TARGCMN_CSRCS = +ifeq ($(CONFIG_EXAMPLES_NETTEST_INIT),y) +TARGCMN_CSRCS += target_netinit.c +endif + +TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT)) + +# Target 1 + TARG1_CSRCS = ifeq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) TARG1_CSRCS += nettest_server.c nettest_client.c @@ -52,8 +61,8 @@ TARG1_MAINSRC = nettest_target1.c TARG1_COBJS = $(TARG1_CSRCS:.c=$(OBJEXT)) TARG1_MAINOBJ = $(TARG1_MAINSRC:.c=$(OBJEXT)) -TARG_CSRCS = $(TARG1_CSRCS) $(TARG1_CSRCS) -TARG_OBJS = $(TARG1_COBJS) +TARG_CSRCS = $(TARG1_CSRCS) $(TARG1_CSRCS) $(TARGCMN_CSRCS) +TARG_OBJS = $(TARG1_COBJS) $(TARGCMN_COBJS) ifneq ($(CONFIG_BUILD_KERNEL),y) TARG_OBJS += $(TARG1_MAINOBJ) @@ -118,7 +127,7 @@ VPATH = all: .built $(HOST_BIN) .PHONY: clean depend distclean preconfig -$(TARG1_COBJS) $(TARG1_MAINOBJ): %$(OBJEXT): %.c +$(TARG1_COBJS) $(TARG1_MAINOBJ) $(TARGCMN_COBJS) : %$(OBJEXT): %.c $(call COMPILE, $<, $@) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) diff --git a/examples/nettest/nettest.h b/examples/nettest/nettest.h index bf5fc8338..2aa9f61e6 100644 --- a/examples/nettest/nettest.h +++ b/examples/nettest/nettest.h @@ -84,6 +84,10 @@ * Public Function Prototypes ****************************************************************************/ +#ifdef CONFIG_EXAMPLES_NETTEST_INIT +void nettest_initialize(void); +#endif + extern void send_client(void); extern void recv_server(void); diff --git a/examples/nettest/nettest_target1.c b/examples/nettest/nettest_target1.c index 52aed8a13..1d5b05418 100644 --- a/examples/nettest/nettest_target1.c +++ b/examples/nettest/nettest_target1.c @@ -38,7 +38,6 @@ ****************************************************************************/ #include "config.h" -//#include #include #include @@ -48,153 +47,8 @@ #include #include -#include -#include -#include - -#include "netutils/netlib.h" - #include "nettest.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -#if defined(CONFIG_EXAMPLES_NETTEST_INIT) && \ - defined(CONFIG_EXAMPLES_NETTEST_IPv6) && \ - !defined(CONFIG_NET_ICMPv6_AUTOCONF) -/* Our host IPv6 address */ - -static const uint16_t g_ipv6_hostaddr[8] = -{ - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_1), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_2), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_3), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_4), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_5), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_6), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_7), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_8), -}; - -/* Default routine IPv6 address */ - -static const uint16_t g_ipv6_draddr[8] = -{ - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_1), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_2), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_3), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_4), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_5), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_6), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_7), - HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_8), -}; - -/* IPv6 netmask */ - -static const uint16_t g_ipv6_netmask[8] = -{ - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_1), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_2), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_3), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_4), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_5), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_6), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_7), - HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_8), -}; -#endif /* CONFIG_EXAMPLES_NETTEST_INIT && CONFIG_EXAMPLES_NETTEST_IPv6 && !CONFIG_NET_ICMPv6_AUTOCONF */ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -#ifdef CONFIG_EXAMPLES_NETTEST_INIT -static void netest_initialize(void) -{ -#ifndef CONFIG_EXAMPLES_NETTEST_IPv6 - struct in_addr addr; -#endif -#ifdef CONFIG_EXAMPLES_NETTEST_NOMAC - uint8_t mac[IFHWADDRLEN]; -#endif - -/* Many embedded network interfaces must have a software assigned MAC */ - -#ifdef CONFIG_EXAMPLES_NETTEST_NOMAC - mac[0] = 0x00; - mac[1] = 0xe0; - mac[2] = 0xde; - mac[3] = 0xad; - mac[4] = 0xbe; - mac[5] = 0xef; - netlib_setmacaddr("eth0", mac); -#endif - -#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 -#ifdef CONFIG_NET_ICMPv6_AUTOCONF - /* Perform ICMPv6 auto-configuration */ - - netlib_icmpv6_autoconfiguration("eth0"); - -#else /* CONFIG_NET_ICMPv6_AUTOCONF */ - - /* Set up our fixed host address */ - - netlib_set_ipv6addr("eth0", - (FAR const struct in6_addr *)g_ipv6_hostaddr); - - /* Set up the default router address */ - - netlib_set_dripv6addr("eth0", - (FAR const struct in6_addr *)g_ipv6_draddr); - - /* Setup the subnet mask */ - - netlib_set_ipv6netmask("eth0", - (FAR const struct in6_addr *)g_ipv6_netmask); - - /* New versions of netlib_set_ipvXaddr will not bring the network up, - * So ensure the network is really up at this point. - */ - - netlib_ifup("eth0"); - -#endif /* CONFIG_NET_ICMPv6_AUTOCONF */ -#else /* CONFIG_EXAMPLES_NETTEST_IPv6 */ - - /* Set up our host address */ - - addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_IPADDR); - netlib_set_ipv4addr("eth0", &addr); - - /* Set up the default router address */ - - addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_DRIPADDR); - netlib_set_dripv4addr("eth0", &addr); - - /* Setup the subnet mask */ - - addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_NETMASK); - netlib_set_ipv4netmask("eth0", &addr); - -#endif /* CONFIG_EXAMPLES_NETTEST_IPv6 */ -} -#endif /*CONFIG_EXAMPLES_NETTEST_INIT */ - -#ifdef CONFIG_EXAMPLES_NETTEST_LOOPBACK -static int server_child(int argc, char *argv[]) -{ - recv_server(); - return EXIT_SUCCESS; -} -#endif - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -219,7 +73,7 @@ int nettest_main(int argc, char *argv[]) #ifdef CONFIG_EXAMPLES_NETTEST_INIT /* Initialize the network */ - netest_initialize(); + nettest_initialize(); #endif #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) diff --git a/examples/nettest/target_netinit.c b/examples/nettest/target_netinit.c new file mode 100644 index 000000000..e7de618f1 --- /dev/null +++ b/examples/nettest/target_netinit.c @@ -0,0 +1,193 @@ +/**************************************************************************** + * examples/nettest/target_netinit.c + * + * Copyright (C) 2007, 2011, 2015, 2017 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 "config.h" + +#include +#include +#include + +#include +#include +#include + +#include "netutils/netlib.h" + +#include "nettest.h" + +#ifdef CONFIG_EXAMPLES_NETTEST_INIT + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#if defined(CONFIG_EXAMPLES_NETTEST_INIT) && \ + defined(CONFIG_EXAMPLES_NETTEST_IPv6) && \ + !defined(CONFIG_NET_ICMPv6_AUTOCONF) +/* Our host IPv6 address */ + +static const uint16_t g_ipv6_hostaddr[8] = +{ + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_1), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_2), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_3), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_4), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_5), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_6), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_7), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6ADDR_8), +}; + +/* Default routine IPv6 address */ + +static const uint16_t g_ipv6_draddr[8] = +{ + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_1), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_2), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_3), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_4), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_5), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_6), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_7), + HTONS(CONFIG_EXAMPLES_NETTEST_DRIPv6ADDR_8), +}; + +/* IPv6 netmask */ + +static const uint16_t g_ipv6_netmask[8] = +{ + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_1), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_2), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_3), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_4), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_5), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_6), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_7), + HTONS(CONFIG_EXAMPLES_NETTEST_IPv6NETMASK_8), +}; +#endif /* CONFIG_EXAMPLES_NETTEST_INIT && CONFIG_EXAMPLES_NETTEST_IPv6 && !CONFIG_NET_ICMPv6_AUTOCONF */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_NETTEST_INIT +void nettest_initialize(void) +{ +#ifndef CONFIG_EXAMPLES_NETTEST_IPv6 + struct in_addr addr; +#endif +#ifdef CONFIG_EXAMPLES_NETTEST_NOMAC + uint8_t mac[IFHWADDRLEN]; +#endif + +/* Many embedded network interfaces must have a software assigned MAC */ + +#ifdef CONFIG_EXAMPLES_NETTEST_NOMAC + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xde; + mac[3] = 0xad; + mac[4] = 0xbe; + mac[5] = 0xef; + netlib_setmacaddr("eth0", mac); +#endif + +#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 +#ifdef CONFIG_NET_ICMPv6_AUTOCONF + /* Perform ICMPv6 auto-configuration */ + + netlib_icmpv6_autoconfiguration("eth0"); + +#else /* CONFIG_NET_ICMPv6_AUTOCONF */ + + /* Set up our fixed host address */ + + netlib_set_ipv6addr("eth0", + (FAR const struct in6_addr *)g_ipv6_hostaddr); + + /* Set up the default router address */ + + netlib_set_dripv6addr("eth0", + (FAR const struct in6_addr *)g_ipv6_draddr); + + /* Setup the subnet mask */ + + netlib_set_ipv6netmask("eth0", + (FAR const struct in6_addr *)g_ipv6_netmask); + + /* New versions of netlib_set_ipvXaddr will not bring the network up, + * So ensure the network is really up at this point. + */ + + netlib_ifup("eth0"); + +#endif /* CONFIG_NET_ICMPv6_AUTOCONF */ +#else /* CONFIG_EXAMPLES_NETTEST_IPv6 */ + + /* Set up our host address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_IPADDR); + netlib_set_ipv4addr("eth0", &addr); + + /* Set up the default router address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_DRIPADDR); + netlib_set_dripv4addr("eth0", &addr); + + /* Setup the subnet mask */ + + addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_NETMASK); + netlib_set_ipv4netmask("eth0", &addr); + +#endif /* CONFIG_EXAMPLES_NETTEST_IPv6 */ +} +#endif /*CONFIG_EXAMPLES_NETTEST_INIT */ + +#ifdef CONFIG_EXAMPLES_NETTEST_LOOPBACK +static int server_child(int argc, char *argv[]) +{ + recv_server(); + return EXIT_SUCCESS; +} +#endif + + +#endif /* CONFIG_EXAMPLES_NETTEST_INIT */ + diff --git a/examples/udp/Makefile b/examples/udp/Makefile index b53a053c6..f4a7e2994 100644 --- a/examples/udp/Makefile +++ b/examples/udp/Makefile @@ -43,6 +43,7 @@ TARGCMN_CSRCS = udp_cmdline.c ifeq ($(CONFIG_EXAMPLES_UDP_NETINIT),y) TARGCMN_CSRCS += target_netinit.c endif + TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT)) # Target 1 @@ -97,7 +98,6 @@ endif CONFIG_EXAMPLES_UDP_PRIORITY2 ?= 100 CONFIG_EXAMPLES_UDP_STACKSIZE2 ?= 2048 - PROGNAME2 = $(CONFIG_EXAMPLES_UDP_PROGNAME2) PRIORITY2 = $(CONFIG_EXAMPLES_UDP_PRIORITY2) STACKSIZE2 = $(CONFIG_EXAMPLES_UDP_STACKSIZE2) From a0251ae9748d0fc6d9e647a37a09c8f356fc6d64 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 12:21:13 -0600 Subject: [PATCH 04/23] examples/nettest: Fix client/server naming confusion; add command line option to select the server address on the target; examples/udp: Fix naming of a configuration setting --- examples/nettest/Kconfig | 97 ++++++++++++++++-------------- examples/nettest/Makefile | 4 +- examples/nettest/nettest.h | 16 ++++- examples/nettest/nettest_client.c | 63 ++++++------------- examples/nettest/nettest_server.c | 32 +++++----- examples/nettest/nettest_target1.c | 6 +- examples/nettest/target_netinit.c | 2 +- examples/udp/Kconfig | 4 +- examples/udp/udp.h | 8 +-- examples/udp/udp_client.c | 8 +-- examples/udp/udp_server.c | 4 +- 11 files changed, 120 insertions(+), 124 deletions(-) diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index d12d4a618..02105c4ec 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -116,15 +116,15 @@ endif # EXAMPLES_NETTEST_INIT if !EXAMPLES_NETTEST_LOOPBACK -config EXAMPLES_NETTEST_CLIENTIP - hex "Client IP Address" +config EXAMPLES_NETTEST_SERVERIP + hex "Server IP Address" default 0x0a000001 if !EXAMPLES_NETTEST_SERVER default 0x0a000002 if EXAMPLES_NETTEST_SERVER ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -389,15 +389,15 @@ if !EXAMPLES_NETTEST_LOOPBACK || !NET_LOOPBACK comment "Client IPv6 address" -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_1 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_1 hex "[0]" default 0xfc00 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the cleint, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -405,15 +405,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_1 values forming the full IP address must be specified individually. This is the first of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_2 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_2 hex "[1]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -421,15 +421,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_2 values forming the full IP address must be specified individually. This is the second of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_3 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_3 hex "[2]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -437,15 +437,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_3 values forming the full IP address must be specified individually. This is the Third of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_4 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_4 hex "[3]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -453,15 +453,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_4 values forming the full IP address must be specified individually. This is the fourth of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_5 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_5 hex "[4]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -469,15 +469,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_5 values forming the full IP address must be specified individually. This is the fifth of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_6 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_6 hex "[5]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -485,15 +485,15 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_6 values forming the full IP address must be specified individually. This is the sixth of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_7 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_7 hex "[6]" default 0x0000 range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -501,16 +501,16 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_7 values forming the full IP address must be specified individually. This is the seventh of the 8-values. -config EXAMPLES_NETTEST_CLIENTIPv6ADDR_8 +config EXAMPLES_NETTEST_SERVERIPv6ADDR_8 hex "[7]" default 0x0001 if !EXAMPLES_NETTEST_SERVER default 0x0002 if EXAMPLES_NETTEST_SERVER range 0x0 0xffff ---help--- - IP address of the client. If the target is the client, then - EXAMPLES_NETTEST_CLIENTIP should be the same as - EXAMPLES_NETTEST_IPADDR (default). If the target is the server, - then the default value of EXAMPLES_NETTEST_CLIENTIP is set to the + IP address of the server. If the target is the server, then + EXAMPLES_NETTEST_SERVERIP should be the same as + EXAMPLES_NETTEST_IPADDR (default). If the target is the client, + then the default value of EXAMPLES_NETTEST_SERVERIP is set to the host PC IP address (possibly the gateway address, EXAMPLES_NETTEST_DRIPADDR?). @@ -520,4 +520,9 @@ config EXAMPLES_NETTEST_CLIENTIPv6ADDR_8 endif # !EXAMPLES_NETTEST_LOOPBACK endif # EXAMPLES_NETTEST_IPv6 + +config EXAMPLES_NETTEST_SERVER_PORTNO + int "Server port number" + default 5471 + endif # EXAMPLES_NETTEST diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index 2b3e0d99b..173aa4ece 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -39,7 +39,7 @@ include $(APPDIR)/Make.defs # Basic TCP networking test -TARGCMN_CSRCS = +TARGCMN_CSRCS = nettest_cmdline.c ifeq ($(CONFIG_EXAMPLES_NETTEST_INIT),y) TARGCMN_CSRCS += target_netinit.c endif @@ -81,7 +81,7 @@ endif ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) HOSTCFLAGS += -DNETTEST_HOST=1 ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER),y) - HOSTCFLAGS += -DCONFIG_EXAMPLES_NETTEST_SERVER=1 -DCONFIG_EXAMPLES_NETTEST_CLIENTIP="$(CONFIG_EXAMPLES_NETTEST_CLIENTIP)" + HOSTCFLAGS += -DCONFIG_EXAMPLES_NETTEST_SERVER=1 -DCONFIG_EXAMPLES_NETTEST_SERVERIP="$(CONFIG_EXAMPLES_NETTEST_SERVERIP)" endif ifeq ($(CONFIG_EXAMPLES_NETTEST_PERFORMANCE),y) HOSTCFLAGS += -DCONFIG_EXAMPLES_NETTEST_PERFORMANCE=1 diff --git a/examples/nettest/nettest.h b/examples/nettest/nettest.h index 2aa9f61e6..a795e96e9 100644 --- a/examples/nettest/nettest.h +++ b/examples/nettest/nettest.h @@ -77,9 +77,22 @@ # define PF_INETX PF_INET #endif -#define PORTNO 5471 +#ifndef CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO +# define CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO 5471 +#endif + #define SENDSIZE 4096 +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 +uint16_t g_server_ipv6[8]; +#else +uint32_t g_server_ipv4; +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -88,6 +101,7 @@ void nettest_initialize(void); #endif +void parse_cmdline(int argc, char **argv); extern void send_client(void); extern void recv_server(void); diff --git a/examples/nettest/nettest_client.c b/examples/nettest/nettest_client.c index 6a24d68ec..4883504df 100644 --- a/examples/nettest/nettest_client.c +++ b/examples/nettest/nettest_client.c @@ -59,9 +59,9 @@ void send_client(void) { #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 - struct sockaddr_in6 myaddr; + struct sockaddr_in6 server; #else - struct sockaddr_in myaddr; + struct sockaddr_in server; #endif char *outbuf; #ifndef CONFIG_EXAMPLES_NETTEST_PERFORMANCE @@ -100,56 +100,29 @@ void send_client(void) goto errout_with_buffers; } - /* Connect the socket to the server */ + /* Set up the server address */ #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 - myaddr.sin6_family = AF_INET6; - myaddr.sin6_port = HTONS(PORTNO); + server.sin6_family = AF_INET6; + server.sin6_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); + memcpy(server.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); + addrlen = sizeof(struct sockaddr_in6); -#if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) - myaddr.sin6_addr.s6_addr16[0] = 0; /* Use the loopback address */ - myaddr.sin6_addr.s6_addr16[1] = 0; - myaddr.sin6_addr.s6_addr16[2] = 0; - myaddr.sin6_addr.s6_addr16[3] = 0; - myaddr.sin6_addr.s6_addr16[4] = 0; - myaddr.sin6_addr.s6_addr16[5] = 0; - myaddr.sin6_addr.s6_addr16[6] = 0; - myaddr.sin6_addr.s6_addr16[7] = HTONS(1); + printf("Connecting to IPv6 Address: %04x:04x:04x:04x:04x:04x:04x:04x\n", + g_server_ipv6[0], g_server_ipv6[1], g_server_ipv6[2], g_server_ipv6[3], + g_server_ipv6[4], g_server_ipv6[5], g_server_ipv6[6], g_server_ipv6[7]); #else - myaddr.sin6_addr.s6_addr16[0] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_1); - myaddr.sin6_addr.s6_addr16[1] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_2); - myaddr.sin6_addr.s6_addr16[2] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_3); - myaddr.sin6_addr.s6_addr16[3] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_4); - myaddr.sin6_addr.s6_addr16[4] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_5); - myaddr.sin6_addr.s6_addr16[5] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_6); - myaddr.sin6_addr.s6_addr16[6] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_7); - myaddr.sin6_addr.s6_addr16[7] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_8); + server.sin_family = AF_INET; + server.sin_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); + server.sin_addr.s_addr = (in_addr_t)g_server_ipv4; + addrlen = sizeof(struct sockaddr_in); + + printf("Connecting to IPv4 Address: %08lx\n", (unsigned long)g_server_ipv4); #endif - printf("IPv6 Address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", - myaddr.sin6_addr.s6_addr16[0], myaddr.sin6_addr.s6_addr16[1], - myaddr.sin6_addr.s6_addr16[2], myaddr.sin6_addr.s6_addr16[3], - myaddr.sin6_addr.s6_addr16[4], myaddr.sin6_addr.s6_addr16[5], - myaddr.sin6_addr.s6_addr16[6], myaddr.sin6_addr.s6_addr16[7]); + /* Connect the socket to the server */ - addrlen = sizeof(struct sockaddr_in6); -#else - myaddr.sin_family = AF_INET; - myaddr.sin_port = HTONS(PORTNO); - -#if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) - myaddr.sin_addr.s_addr = HTONL(0x7f000001); -#else - myaddr.sin_addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_CLIENTIP); -#endif - - printf("IPv4 Address: %08x\n", myaddr.sin_addr.s_addr); - - addrlen = sizeof(struct sockaddr_in); -#endif - - printf("client: Connecting...\n"); - if (connect( sockfd, (struct sockaddr*)&myaddr, addrlen) < 0) + if (connect( sockfd, (struct sockaddr*)&server, addrlen) < 0) { printf("client: connect failure: %d\n", errno); goto errout_with_socket; diff --git a/examples/nettest/nettest_server.c b/examples/nettest/nettest_server.c index ad55506a1..755fdf096 100644 --- a/examples/nettest/nettest_server.c +++ b/examples/nettest/nettest_server.c @@ -112,33 +112,32 @@ void recv_server(void) #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 myaddr.sin6_family = AF_INET6; - myaddr.sin6_port = HTONS(PORTNO); - + myaddr.sin6_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && !defined(NET_LOOPBACK) - myaddr.sin6_addr.s6_addr16[0] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_1); - myaddr.sin6_addr.s6_addr16[1] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_2); - myaddr.sin6_addr.s6_addr16[2] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_3); - myaddr.sin6_addr.s6_addr16[3] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_4); - myaddr.sin6_addr.s6_addr16[4] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_5); - myaddr.sin6_addr.s6_addr16[5] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_6); - myaddr.sin6_addr.s6_addr16[6] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_7); - myaddr.sin6_addr.s6_addr16[7] = HTONS(CONFIG_EXAMPLES_NETTEST_CLIENTIPv6ADDR_8); + memcpy(myaddr.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); #else - memset(&myaddr.sin6_addr, 0, sizeof(struct in6_addr)); + memset(myaddr.sin6_addr.s6_addr16, 0, 8 * sizeof(uint16_t)); #endif - addrlen = sizeof(struct sockaddr_in6); + + printf("Binding to IPv6 Address: %04x:04x:04x:04x:04x:04x:04x:04x\n", + myaddr.sin6_addr.s6_addr16[0], myaddr.sin6_addr.s6_addr16[1], + myaddr.sin6_addr.s6_addr16[2], myaddr.sin6_addr.s6_addr16[3], + myaddr.sin6_addr.s6_addr16[4], myaddr.sin6_addr.s6_addr16[5], + myaddr.sin6_addr.s6_addr16[6], myaddr.sin6_addr.s6_addr16[7]); #else myaddr.sin_family = AF_INET; - myaddr.sin_port = HTONS(PORTNO); + myaddr.sin_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && !defined(NET_LOOPBACK) - myaddr.sin_addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_CLIENTIP); + myaddr.sin_addr.s_addr = (in_addr_t)g_server_ipv4; #else myaddr.sin_addr.s_addr = INADDR_ANY; #endif - addrlen = sizeof(struct sockaddr_in); + + printf("Binding to IPv4 Address: %08lx\n", + (unsigned long)myaddr.sin_addr.s_addr); #endif if (bind(listensd, (struct sockaddr*)&myaddr, addrlen) < 0) @@ -157,7 +156,8 @@ void recv_server(void) /* Accept only one connection */ - printf("server: Accepting connections on port %d\n", PORTNO); + printf("server: Accepting connections on port %d\n", + CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen); if (acceptsd < 0) { diff --git a/examples/nettest/nettest_target1.c b/examples/nettest/nettest_target1.c index 1d5b05418..784e7e76e 100644 --- a/examples/nettest/nettest_target1.c +++ b/examples/nettest/nettest_target1.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/nettest/nettest_target1.c * - * Copyright (C) 2007, 2009-2011, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009-2011, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -70,6 +70,10 @@ int nettest_main(int argc, char *argv[]) #endif #endif + /* Parse any command line options */ + + parse_cmdline(argc, argv); + #ifdef CONFIG_EXAMPLES_NETTEST_INIT /* Initialize the network */ diff --git a/examples/nettest/target_netinit.c b/examples/nettest/target_netinit.c index e7de618f1..e5c84518e 100644 --- a/examples/nettest/target_netinit.c +++ b/examples/nettest/target_netinit.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/nettest/target_netinit.c * - * Copyright (C) 2007, 2011, 2015, 2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009-2011, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without diff --git a/examples/udp/Kconfig b/examples/udp/Kconfig index 392ab6f73..851094d9a 100644 --- a/examples/udp/Kconfig +++ b/examples/udp/Kconfig @@ -512,11 +512,11 @@ config EXAMPLES_UDP_SERVERIPv6ADDR_8 endif # EXAMPLES_UDP_IPv6 -config EXAMPLES_SERVER_PORTNO +config EXAMPLES_UDP_SERVER_PORTNO int "Server port number" default 5471 -config EXAMPLES_CLIENT_PORTNO +config EXAMPLES_UDP_CLIENT_PORTNO int "Client port number" default 5472 diff --git a/examples/udp/udp.h b/examples/udp/udp.h index 9316240be..193d5e2cb 100644 --- a/examples/udp/udp.h +++ b/examples/udp/udp.h @@ -83,12 +83,12 @@ # define PF_INETX PF_INET #endif -#ifndef CONFIG_EXAMPLES_SERVER_PORTNO -# define CONFIG_EXAMPLES_SERVER_PORTNO 5471 +#ifndef CONFIG_EXAMPLES_UDP_SERVER_PORTNO +# define CONFIG_EXAMPLES_UDP_SERVER_PORTNO 5471 #endif -#ifndef CONFIG_EXAMPLES_CLIENT_PORTNO -# define CONFIG_EXAMPLES_CLIENT_PORTNO 5472 +#ifndef CONFIG_EXAMPLES_UDP_CLIENT_PORTNO +# define CONFIG_EXAMPLES_UDP_CLIENT_PORTNO 5472 #endif #define ASCIISIZE (0x7f - 0x20) diff --git a/examples/udp/udp_client.c b/examples/udp/udp_client.c index 18368c1a3..89742dca8 100644 --- a/examples/udp/udp_client.c +++ b/examples/udp/udp_client.c @@ -77,7 +77,7 @@ static int create_socket(void) /* Bind the UDP socket to a IPv4 port */ addr.sin_family = AF_INET; - addr.sin_port = HTONS(CONFIG_EXAMPLES_CLIENT_PORTNO); + addr.sin_port = HTONS(CONFIG_EXAMPLES_UDP_CLIENT_PORTNO); addr.sin_addr.s_addr = HTONL(INADDR_ANY); addrlen = sizeof(struct sockaddr_in); @@ -96,7 +96,7 @@ static int create_socket(void) /* Bind the UDP socket to a IPv6 port */ addr.sin6_family = AF_INET6; - addr.sin6_port = HTONS(CONFIG_EXAMPLES_CLIENT_PORTNO); + addr.sin6_port = HTONS(CONFIG_EXAMPLES_UDP_CLIENT_PORTNO); memset(addr.sin6_addr.s6_addr, 0, sizeof(struct in6_addr)); addrlen = sizeof(struct sockaddr_in6); #endif @@ -164,12 +164,12 @@ void send_client(void) #ifdef CONFIG_EXAMPLES_UDP_IPv6 server.sin6_family = AF_INET6; - server.sin6_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); + server.sin6_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); memcpy(server.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); addrlen = sizeof(struct sockaddr_in6); #else server.sin_family = AF_INET; - server.sin_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); + server.sin_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); server.sin_addr.s_addr = (in_addr_t)g_server_ipv4; addrlen = sizeof(struct sockaddr_in); #endif diff --git a/examples/udp/udp_server.c b/examples/udp/udp_server.c index 03233d68b..967b493c7 100644 --- a/examples/udp/udp_server.c +++ b/examples/udp/udp_server.c @@ -124,13 +124,13 @@ void recv_server(void) #ifdef CONFIG_EXAMPLES_UDP_IPv6 server.sin6_family = AF_INET6; - server.sin6_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); + server.sin6_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); memset(&server.sin6_addr, 0, sizeof(struct in6_addr)); addrlen = sizeof(struct sockaddr_in6); #else server.sin_family = AF_INET; - server.sin_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); + server.sin_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); server.sin_addr.s_addr = HTONL(INADDR_ANY); addrlen = sizeof(struct sockaddr_in); From cde3cb1544f7654e827b368f6ca51fa46c4b4342 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 12:30:11 -0600 Subject: [PATCH 05/23] Forgot to add files before last commit. --- examples/nettest/nettest_cmdline.c | 137 +++++++++++++++++++++++++++++ examples/nettest/nettest_target2.c | 75 ++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 examples/nettest/nettest_cmdline.c create mode 100644 examples/nettest/nettest_target2.c diff --git a/examples/nettest/nettest_cmdline.c b/examples/nettest/nettest_cmdline.c new file mode 100644 index 000000000..479e83336 --- /dev/null +++ b/examples/nettest/nettest_cmdline.c @@ -0,0 +1,137 @@ +/**************************************************************************** + * examples/nettest/netest_cmdline.c + * + * Copyright (C) 2017 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 "config.h" + +#include +#include +#include + +#include "nettest.h" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 + +uint16_t g_server_ipv6[8] = +{ +#if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) + 0 /* Use the loopback address */ + 0 + 0 + 0 + 0 + 0 + 0 + HTONS(1); +#else + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_1), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_2), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_3), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_4), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_5), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_6), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_7), + HTONS(CONFIG_EXAMPLES_NETTEST_SERVERIPv6ADDR_8) +#endif +}; + +#else + +#if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) +uint32_t g_server_ipv4 = HTONL(0x7f000001); +#else +uint32_t g_server_ipv4 = HTONL(CONFIG_EXAMPLES_NETTEST_SERVERIP); +#endif + +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * show_usage + ****************************************************************************/ + +static void show_usage(FAR const char *progname) +{ + fprintf(stderr, "USAGE: %s []\n", progname); + exit(1); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * parse_cmdline + ****************************************************************************/ + +void parse_cmdline(int argc, char **argv) +{ + /* Currently only a single command line option is supported: The server + * IP address. + */ + + if (argc == 2) + { + int ret; + + /* Convert the argument into a binary address */ + +#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 + ret = inet_pton(AF_INET6, argv[1], g_server_ipv6); +#else + ret = inet_pton(AF_INET, argv[1], &g_server_ipv4); +#endif + if (ret < 0) + { + fprintf(stderr, "ERROR: is invalid\n"); + show_usage(argv[0]); + } + } + else if (argc != 1) + { + fprintf(stderr, "ERROR: Too many arguments\n"); + show_usage(argv[0]); + } +} diff --git a/examples/nettest/nettest_target2.c b/examples/nettest/nettest_target2.c new file mode 100644 index 000000000..660a424fd --- /dev/null +++ b/examples/nettest/nettest_target2.c @@ -0,0 +1,75 @@ +/**************************************************************************** + * examples/nettest/nettest_target2.c + * + * Copyright (C) 2007, 2009-2011, 2015, 2017 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 "config.h" + +#include "nettest.h" + +/**************************************************************************** + * nettest_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int nettest2_main(int argc, char *argv[]) +#endif +{ + /* Parse any command line options */ + + parse_cmdline(argc, argv); + +#ifdef CONFIG_EXAMPLES_NETTEST_INIT + /* Initialize the network */ + + nettest_initialize(); +#endif + +#if defined(CONFIG_EXAMPLES_NETTEST_SERVER) + /* Then perform the client side of the test on this thread */ + + send_client(); +#else + /* Then perform the server side of the test on this thread */ + + recv_server(); +#endif + + return EXIT_SUCCESS; +} From 1c0f237c0604ec7a09218128162cca45e4c81729 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 15:49:27 -0600 Subject: [PATCH 06/23] examples/nettest: More prep for a second target --- examples/nettest/Kconfig | 16 ++++++------ examples/nettest/Makefile | 54 ++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index 02105c4ec..285965787 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -12,20 +12,20 @@ config EXAMPLES_NETTEST if EXAMPLES_NETTEST -config EXAMPLES_NETTEST_PROGNAME - string "Program name" +config EXAMPLES_NETTEST_PROGNAME1 + string "Target1 program name" default "nettest" depends on BUILD_KERNEL ---help--- This is the name of the program that will be use when the Nettest program is installed. -config EXAMPLES_NETTEST_STACKSIZE - int "Nettest stack size" +config EXAMPLES_NETTEST_STACKSIZE1 + int "Target1 stack size" default 2048 -config EXAMPLES_NETTEST_PRIORITY - int "Nettest priority" +config EXAMPLES_NETTEST_PRIORITY1 + int "Target1 priority" default 100 config EXAMPLES_NETTEST_LOOPBACK @@ -48,8 +48,8 @@ config EXAMPLES_NETTEST_SERVER_PRIORITY endif # EXAMPLES_NETTEST_LOOPBACK -config EXAMPLES_NETTEST_SERVER - bool "Target is server" +config EXAMPLES_NETTEST_SERVER1 + bool "Target1 is server" default n depends on !EXAMPLES_NETTEST_LOOPBACK ---help--- diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index 173aa4ece..ef65292d1 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -46,7 +46,7 @@ endif TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT)) -# Target 1 +# Target 1 Files TARG1_CSRCS = ifeq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) @@ -64,6 +64,28 @@ TARG1_MAINOBJ = $(TARG1_MAINSRC:.c=$(OBJEXT)) TARG_CSRCS = $(TARG1_CSRCS) $(TARG1_CSRCS) $(TARGCMN_CSRCS) TARG_OBJS = $(TARG1_COBJS) $(TARGCMN_COBJS) +# Target 1 Application Info + +ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER1),y) +CONFIG_EXAMPLES_NETTEST_PROGNAME1 ?= tcpserver +APPNAME1 = tcpserver +else +CONFIG_EXAMPLES_NETTEST_PROGNAME1 ?= tcpclient +APPNAME1 = tcpclient +endif +CONFIG_EXAMPLES_NETTEST_PRIORITY1 ?= 100 +CONFIG_EXAMPLES_NETTEST_STACKSIZE1 ?= 2048 + +PROGNAME1 = $(CONFIG_EXAMPLES_NETTEST_PROGNAME1) +PRIORITY1 = $(CONFIG_EXAMPLES_NETTEST_PRIORITY1) +STACKSIZE1 = $(CONFIG_EXAMPLES_NETTEST_STACKSIZE1) + +# Target 2 + +# Target 2 Application Info + +# Target common + ifneq ($(CONFIG_BUILD_KERNEL),y) TARG_OBJS += $(TARG1_MAINOBJ) endif @@ -78,6 +100,8 @@ else endif endif +# Host + ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) HOSTCFLAGS += -DNETTEST_HOST=1 ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER),y) @@ -108,18 +132,6 @@ endif ROOTDEPPATH = --dep-path . -# NET test built-in application info - -CONFIG_EXAMPLES_NETTEST_STACKSIZE ?= 2048 -CONFIG_EXAMPLES_NETTEST_PRIORITY ?= 100 - -APPNAME = nettest -PRIORITY = $(CONFIG_EXAMPLES_NETTEST_PRIORITY) -STACKSIZE = $(CONFIG_EXAMPLES_NETTEST_STACKSIZE) - -CONFIG_EXAMPLES_NETTEST_PROGNAME ?= nettest$(EXEEXT) -PROGNAME = $(CONFIG_EXAMPLES_HELLO_PROGNAME) - # Common build VPATH = @@ -151,12 +163,12 @@ endif $(Q) touch .built ifeq ($(CONFIG_BUILD_KERNEL),y) -$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(TARG1_MAINOBJ) - @echo "LD: $(PROGNAME)" - $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(TARG1_MAINOBJ) $(LDLIBS) - $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) +$(BIN_DIR)$(DELIM)$(PROGNAME1): $(OBJS) $(TARG1_MAINOBJ) + @echo "LD: $(PROGNAME1)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME1) $(ARCHCRT0OBJ) $(TARG1_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME1) -install: $(BIN_DIR)$(DELIM)$(PROGNAME) +install: $(BIN_DIR)$(DELIM)$(PROGNAME1) else install: @@ -164,10 +176,10 @@ install: endif ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) -$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile - $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME1),$(PRIORITY1),$(STACKSIZE1),$(APPNAME1)_main) -context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat else context: endif From a3ac695f1701d18e4fbdca00900e58116301d2a8 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 18:12:34 -0600 Subject: [PATCH 07/23] examples/nettest: Add support for both enpoints on target boards vs. one on a target and one on the host PC. --- examples/nettest/Kconfig | 65 ++++++++++++++++++++++++----- examples/nettest/Makefile | 67 ++++++++++++++++++++++++++++-- examples/nettest/nettest_target1.c | 8 ++-- examples/nettest/nettest_target2.c | 2 +- examples/nettest/target_netinit.c | 28 +++++++++---- examples/udp/Kconfig | 2 +- examples/udp/Makefile | 10 ++--- 7 files changed, 149 insertions(+), 33 deletions(-) diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index 285965787..67aef0954 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -36,17 +36,9 @@ config EXAMPLES_NETTEST_LOOPBACK Perform the test using the local loopback device. In this case, both the client and the server reside on the target. -if EXAMPLES_NETTEST_LOOPBACK +# No server if loopback; No second target if loopback -config EXAMPLES_NETTEST_SERVER_STACKSIZE - int "Server stack size" - default 2048 - -config EXAMPLES_NETTEST_SERVER_PRIORITY - int "Server priority" - default 100 - -endif # EXAMPLES_NETTEST_LOOPBACK +if !EXAMPLES_NETTEST_LOOPBACK config EXAMPLES_NETTEST_SERVER1 bool "Target1 is server" @@ -56,6 +48,51 @@ config EXAMPLES_NETTEST_SERVER1 Select to use the host as the client side of the test. Default: The target is the client side of the test +config EXAMPLES_NETTEST_TARGET2 + bool "Second endpoint is a target" + default n + ---help--- + By default, the host PC is configured as the second endpoint of the + NETTEST. If this option is selected, then the second endpoint + will be built into the FLASH image as well. This means that you + can use two target boards to run the test with no host PC + involvement. + +if EXAMPLES_NETTEST_TARGET2 + +config EXAMPLES_NETTEST_PROGNAME2 + string "Target2 program name" + default "udpserver" if !EXAMPLES_NETTEST_SERVER2 + default "udpclient" if EXAMPLES_NETTEST_SERVER2 + depends on BUILD_KERNEL + ---help--- + This is the name of the Target2 program that will be use when the + NSH ELF program is installed. + +config EXAMPLES_NETTEST_PRIORITY2 + int "Target2 task priority" + default 100 + +config EXAMPLES_NETTEST_STACKSIZE2 + int "Target2 stack size" + default 2048 + +endif # EXAMPLES_NETTEST_TARGET2 + +config EXAMPLES_NETTEST_DAEMON_STACKSIZE + int "Server daemon stack size" + default 2048 + +config EXAMPLES_NETTEST_DEAMON_PRIORITY + int "Server daemon priority" + default 100 + +endif # EXAMPLES_NETTEST_LOOPBACK + +config EXAMPLES_NETTEST_DEVNAME + string "Network device" + default "eth0" + config EXAMPLES_NETTEST_PERFORMANCE bool "Test for Performance" default n @@ -78,6 +115,8 @@ config EXAMPLES_NETTEST_IPv6 endchoice # IP Domain +# No hardware initialization if loopback + config EXAMPLES_NETTEST_INIT bool "Initialize network" default n if NSH_BUILTIN_APPS @@ -114,6 +153,8 @@ config EXAMPLES_NETTEST_NETMASK endif # EXAMPLES_NETTEST_INIT +# No server if loopback + if !EXAMPLES_NETTEST_LOOPBACK config EXAMPLES_NETTEST_SERVERIP @@ -385,9 +426,11 @@ config EXAMPLES_NETTEST_IPv6NETMASK_8 endif # NET_ICMPv6_AUTOCONF endif # EXAMPLES_NETTEST_INIT +# Addresses are well known if loopback is used. + if !EXAMPLES_NETTEST_LOOPBACK || !NET_LOOPBACK -comment "Client IPv6 address" +comment "Server IPv6 address" config EXAMPLES_NETTEST_SERVERIPv6ADDR_1 hex "[0]" diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index ef65292d1..b082fe532 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -82,12 +82,51 @@ STACKSIZE1 = $(CONFIG_EXAMPLES_NETTEST_STACKSIZE1) # Target 2 +ifeq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) + +TARG2_CSRCS = +ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER1),y) +TARG2_CSRCS += nettest_client.c +else +TARG2_CSRCS += nettest_server.c +endif +TARG2_MAINSRC = nettest_target2.c + +TARG2_COBJS = $(TARG2_CCRCS:.c=$(OBJEXT)) +TARG2_MAINOBJ = $(TARG2_MAINSRC:.c=$(OBJEXT)) + # Target 2 Application Info -# Target common +ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER1),y) +CONFIG_EXAMPLES_NETTEST_PROGNAME2 ?= tcpclient +APPNAME2 = tcpclient +else +CONFIG_EXAMPLES_NETTEST_PROGNAME2 ?= tcpserver +APPNAME2 = tcpserver +endif +CONFIG_EXAMPLES_NETTEST_PRIORITY2 ?= 100 +CONFIG_EXAMPLES_NETTEST_STACKSIZE2 ?= 2048 + +PROGNAME2 = $(CONFIG_EXAMPLES_NETTEST_PROGNAME2) +PRIORITY2 = $(CONFIG_EXAMPLES_NETTEST_PRIORITY2) +STACKSIZE2 = $(CONFIG_EXAMPLES_NETTEST_STACKSIZE2) + +endif + +# All targets + +TARG_SRCS = $(TARG1_CRCS) $(TARG1_MAINSRC) $(TARG2_CSRCS) $(TARG2_MAINSRC) $(TARGCMN_CSRCS) +TARG_OBJS = $(TARG1_COBJS) $(TARG2_COBJS) $(TARGCMN_COBJS) ifneq ($(CONFIG_BUILD_KERNEL),y) - TARG_OBJS += $(TARG1_MAINOBJ) + TARG_OBJS += $(TARG1_MAINOBJ) $(TARG2_MAINOBJ) +endif + +ifeq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) +MAINNAME1 = nettest1_main +MAINNAME2 = nettest2_main +else +MAINNAME1 = nettest_main endif ifeq ($(CONFIG_WINDOWS_NATIVE),y) @@ -102,7 +141,9 @@ endif # Host +ifneq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) + HOSTCFLAGS += -DNETTEST_HOST=1 ifeq ($(CONFIG_EXAMPLES_NETTEST_SERVER),y) HOSTCFLAGS += -DCONFIG_EXAMPLES_NETTEST_SERVER=1 -DCONFIG_EXAMPLES_NETTEST_SERVERIP="$(CONFIG_EXAMPLES_NETTEST_SERVERIP)" @@ -122,6 +163,8 @@ ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) HOSTOBJEXT ?= hobj HOST_OBJS = $(HOST_SRCS:.c=.$(HOSTOBJEXT)) + +endif endif ifeq ($(WINTOOL),y) @@ -142,20 +185,28 @@ all: .built $(HOST_BIN) $(TARG1_COBJS) $(TARG1_MAINOBJ) $(TARGCMN_COBJS) : %$(OBJEXT): %.c $(call COMPILE, $<, $@) +ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) + $(HOST_OBJS): %.$(HOSTOBJEXT): %.c @echo "CC: $<" $(Q) $(HOSTCC) -c $(HOSTCFLAGS) $< -o $@ + +endif endif config.h: $(TOPDIR)/include/nuttx/config.h @echo "CP: $<" $(Q) cp $< $@ +ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) + $(HOST_BIN): config.h $(HOST_OBJS) @echo "LD: $@" $(Q) $(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@ + +endif endif .built: config.h $(TARG_OBJS) @@ -177,9 +228,17 @@ endif ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat: $(DEPCONFIG) Makefile - $(call REGISTER,$(APPNAME1),$(PRIORITY1),$(STACKSIZE1),$(APPNAME1)_main) + $(call REGISTER,$(APPNAME1),$(PRIORITY1),$(STACKSIZE1),$(MAINNAME1)) +ifeq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME2)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME2),$(PRIORITY2),$(STACKSIZE2),$(MAINNAME2)) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat \ + $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME2)_main.bdat +else context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat +endif else context: endif @@ -191,9 +250,11 @@ endif depend: .depend clean: +ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) $(call DELFILE, *.$(HOSTOBJEXT)) $(call DELFILE, $(HOST_BIN)) +endif endif $(call DELFILE, .built) $(call DELFILE, *.dSYM) diff --git a/examples/nettest/nettest_target1.c b/examples/nettest/nettest_target1.c index 784e7e76e..0a5d968dc 100644 --- a/examples/nettest/nettest_target1.c +++ b/examples/nettest/nettest_target1.c @@ -57,8 +57,10 @@ * nettest_main ****************************************************************************/ -#ifdef CONFIG_BUILD_KERNEL +#if defined(CONFIG_BUILD_KERNEL) int main(int argc, FAR char *argv[]) +#elif defined(CONFIG_EXAMPLES_NETTEST_TARGET2) +int nettest1_main(int argc, char *argv[]) #else int nettest_main(int argc, char *argv[]) #endif @@ -83,8 +85,8 @@ int nettest_main(int argc, char *argv[]) #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) /* Then perform the server side of the test on a child task */ - child = task_create("Nettest Child", CONFIG_EXAMPLES_NETTEST_SERVER_PRIORITY, - CONFIG_EXAMPLES_NETTEST_SERVER_STACKSIZE, server_child, + child = task_create("Nettest Child", CONFIG_EXAMPLES_NETTEST_DAEMON_PRIORITY, + CONFIG_EXAMPLES_NETTEST_DAEMON_STACKSIZE, server_child, NULL); if (child < 0) { diff --git a/examples/nettest/nettest_target2.c b/examples/nettest/nettest_target2.c index 660a424fd..aa246b3c9 100644 --- a/examples/nettest/nettest_target2.c +++ b/examples/nettest/nettest_target2.c @@ -38,7 +38,7 @@ ****************************************************************************/ #include "config.h" - +#include #include "nettest.h" /**************************************************************************** diff --git a/examples/nettest/target_netinit.c b/examples/nettest/target_netinit.c index e5c84518e..78193737b 100644 --- a/examples/nettest/target_netinit.c +++ b/examples/nettest/target_netinit.c @@ -53,6 +53,16 @@ #ifdef CONFIG_EXAMPLES_NETTEST_INIT +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_NETTEST_DEVNAME +# define DEVNAME CONFIG_EXAMPLES_NETTEST_DEVNAME +#else +# define DEVNAME "eth0" +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -126,37 +136,37 @@ void nettest_initialize(void) mac[3] = 0xad; mac[4] = 0xbe; mac[5] = 0xef; - netlib_setmacaddr("eth0", mac); + netlib_setmacaddr(DEVNAME, mac); #endif #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 #ifdef CONFIG_NET_ICMPv6_AUTOCONF /* Perform ICMPv6 auto-configuration */ - netlib_icmpv6_autoconfiguration("eth0"); + netlib_icmpv6_autoconfiguration(DEVNAME); #else /* CONFIG_NET_ICMPv6_AUTOCONF */ /* Set up our fixed host address */ - netlib_set_ipv6addr("eth0", + netlib_set_ipv6addr(DEVNAME, (FAR const struct in6_addr *)g_ipv6_hostaddr); /* Set up the default router address */ - netlib_set_dripv6addr("eth0", + netlib_set_dripv6addr(DEVNAME, (FAR const struct in6_addr *)g_ipv6_draddr); /* Setup the subnet mask */ - netlib_set_ipv6netmask("eth0", + netlib_set_ipv6netmask(DEVNAME, (FAR const struct in6_addr *)g_ipv6_netmask); /* New versions of netlib_set_ipvXaddr will not bring the network up, * So ensure the network is really up at this point. */ - netlib_ifup("eth0"); + netlib_ifup(DEVNAME); #endif /* CONFIG_NET_ICMPv6_AUTOCONF */ #else /* CONFIG_EXAMPLES_NETTEST_IPv6 */ @@ -164,17 +174,17 @@ void nettest_initialize(void) /* Set up our host address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_IPADDR); - netlib_set_ipv4addr("eth0", &addr); + netlib_set_ipv4addr(DEVNAME, &addr); /* Set up the default router address */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_DRIPADDR); - netlib_set_dripv4addr("eth0", &addr); + netlib_set_dripv4addr(DEVNAME, &addr); /* Setup the subnet mask */ addr.s_addr = HTONL(CONFIG_EXAMPLES_NETTEST_NETMASK); - netlib_set_ipv4netmask("eth0", &addr); + netlib_set_ipv4netmask(DEVNAME, &addr); #endif /* CONFIG_EXAMPLES_NETTEST_IPv6 */ } diff --git a/examples/udp/Kconfig b/examples/udp/Kconfig index 851094d9a..ba81ca0de 100644 --- a/examples/udp/Kconfig +++ b/examples/udp/Kconfig @@ -42,7 +42,7 @@ config EXAMPLES_UDP_TARGET2 By default, the host PC is configured as the second endpoint of the UDP test. If this option is selected, then the second endpoint will be built into the FLASH image as well. This means that you - can use two target boards to run the test with not host PC + can use two target boards to run the test with no host PC involvement. if EXAMPLES_UDP_TARGET2 diff --git a/examples/udp/Makefile b/examples/udp/Makefile index f4a7e2994..902d34d66 100644 --- a/examples/udp/Makefile +++ b/examples/udp/Makefile @@ -77,15 +77,15 @@ STACKSIZE1 = $(CONFIG_EXAMPLES_UDP_STACKSIZE1) ifeq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) -TARG2_CRCS = +TARG2_CSRCS = ifeq ($(CONFIG_EXAMPLES_UDP_SERVER1),y) -TARG2_CRCS += udp_client.c +TARG2_CSRCS += udp_client.c else -TARG2_CRCS += udp_server.c +TARG2_CSRCS += udp_server.c endif TARG2_MAINSRC = udp_target2.c -TARG2_COBJS = $(TARG2_CRCS:.c=$(OBJEXT)) +TARG2_COBJS = $(TARG2_CSRCS:.c=$(OBJEXT)) TARG2_MAINOBJ = $(TARG2_MAINSRC:.c=$(OBJEXT)) ifeq ($(CONFIG_EXAMPLES_UDP_SERVER1),y) @@ -104,7 +104,7 @@ STACKSIZE2 = $(CONFIG_EXAMPLES_UDP_STACKSIZE2) endif -TARG_SRCS = $(TARG1_CRCS) $(TARG1_MAINSRC) $(TARG2_CRCS) $(TARG2_MAINSRC) $(TARGCMN_CSRCS) +TARG_SRCS = $(TARG1_CRCS) $(TARG1_MAINSRC) $(TARG2_CSRCS) $(TARG2_MAINSRC) $(TARGCMN_CSRCS) TARG_OBJS = $(TARG1_COBJS) $(TARG2_COBJS) $(TARGCMN_COBJS) ifneq ($(CONFIG_BUILD_KERNEL),y) From 54d55cb307bdc95bd8e358b9c459328e51ec0bc9 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 18:13:51 -0600 Subject: [PATCH 08/23] system/dhcpc: The DHCPC renew command did not build correctly due to naming problems. Noted by Masayuki Ishikawa, --- system/dhcpc/Kconfig | 10 ++-- system/dhcpc/Make.defs | 2 +- system/dhcpc/Makefile | 53 ++++++++++----------- system/dhcpc/{dhcpc_main.c => renew_main.c} | 6 +-- 4 files changed, 34 insertions(+), 37 deletions(-) rename system/dhcpc/{dhcpc_main.c => renew_main.c} (98%) diff --git a/system/dhcpc/Kconfig b/system/dhcpc/Kconfig index 9b88c0392..014e94f49 100644 --- a/system/dhcpc/Kconfig +++ b/system/dhcpc/Kconfig @@ -3,7 +3,7 @@ # see the file kconfig-language.txt in the NuttX tools repository. # -config SYSTEM_DHCPC +config SYSTEM_DHCPC_RENEW bool "DHCP Address Renewal" default n select NETUTILS_DHCPC @@ -11,9 +11,9 @@ config SYSTEM_DHCPC ---help--- Enble the DHCP client 'renew' command -if SYSTEM_DHCPC +if SYSTEM_DHCPC_RENEW -config SYSTEM_DHCPC_PROGNAME +config DHCPC_RENEW_PROGNAME string "Program name" default "renew" depends on BUILD_KERNEL @@ -21,11 +21,11 @@ config SYSTEM_DHCPC_PROGNAME This is the name of the program that will be use when the NSH ELF program is installed. -config SYSTEM_DHCPC_PRIORITY +config DHCPC_RENEW_PRIORITY int "DHCPC task priority" default 100 -config SYSTEM_DHCPC_STACKSIZE +config DHCPC_RENEW_STACKSIZE int "DHCPC stack size" default 2048 diff --git a/system/dhcpc/Make.defs b/system/dhcpc/Make.defs index 7e559c2a2..81c55afe8 100644 --- a/system/dhcpc/Make.defs +++ b/system/dhcpc/Make.defs @@ -34,7 +34,7 @@ # ############################################################################ -ifeq ($(CONFIG_SYSTEM_DHCPC),y) +ifeq ($(CONFIG_SYSTEM_DHCPC_RENEW),y) CONFIGURED_APPS += system/dhcpc endif diff --git a/system/dhcpc/Makefile b/system/dhcpc/Makefile index 935277acf..52e937d6e 100644 --- a/system/dhcpc/Makefile +++ b/system/dhcpc/Makefile @@ -39,31 +39,31 @@ include $(APPDIR)/Make.defs # DHCPC address renewal built-in application info -CONFIG_SYSTEM_DHCPC_PRIORITY ?= SCHED_PRIORITY_DEFAULT -CONFIG_SYSTEM_DHCPC_STACKSIZE ?= 2048 +CONFIG_DHCPC_RENEW_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_DHCPC_RENEW_STACKSIZE ?= 2048 -APPNAME = renew -PRIORITY = $(CONFIG_SYSTEM_DHCPC_PRIORITY) -STACKSIZE = $(CONFIG_SYSTEM_DHCPC_STACKSIZE) +RENEW_APPNAME = renew +RENEW_PRIORITY = $(CONFIG_DHCPC_RENEW_PRIORITY) +RENEW_STACKSIZE = $(CONFIG_DHCPC_RENEW_STACKSIZE) -CONFIG_SYSTEM_DHCPC_PROGNAME ?= renew$(EXEEXT) -PROGNAME = $(CONFIG_SYSTEM_DHCPC_PROGNAME) +CONFIG_DHCPC_RENEW_PROGNAME ?= renew$(EXEEXT) +RENEW_PROGNAME = $(CONFIG_DHCPC_RENEW_PROGNAME) # DHCPC address renewal -ASRCS = -CSRCS = -MAINSRC = dhcpc_main.c +RENEW_SRCS = +RENEW_MAINSRC = renew_main.c -AOBJS = $(ASRCS:.S=$(OBJEXT)) -COBJS = $(CSRCS:.c=$(OBJEXT)) -MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) +RENEW_OBJS = $(RENEW_SRCS:.c=$(OBJEXT)) +RENEW_MAINOBJ = $(RENEW_MAINSRC:.c=$(OBJEXT)) -SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) -OBJS = $(AOBJS) $(COBJS) +# Other DHCPC commands go here + +SRCS = $(RENEW_SRCS) $(RENEW_MAINSRC) +OBJS = $(RENEW_OBJS) ifneq ($(CONFIG_BUILD_KERNEL),y) - OBJS += $(MAINOBJ) + OBJS += $(RENEW_MAINOBJ) endif ifeq ($(CONFIG_WINDOWS_NATIVE),y) @@ -91,10 +91,7 @@ VPATH = all: .built .PHONY: clean depend distclean -$(AOBJS): %$(OBJEXT): %.S - $(call ASSEMBLE, $<, $@) - -$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c +$(OBJS) $(RENEW_MAINOBJ): %$(OBJEXT): %.c $(call COMPILE, $<, $@) .built: $(OBJS) @@ -102,12 +99,12 @@ $(COBJS) $(MAINOBJ): %$(OBJEXT): %.c @touch .built ifeq ($(CONFIG_BUILD_KERNEL),y) -$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) - @echo "LD: $(PROGNAME)" - $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) - $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) +$(BIN_DIR)$(DELIM)$(RENEW_PROGNAME): $(OBJS) $(RENEW_MAINOBJ) + @echo "LD: $(RENEW_PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(RENEW_PROGNAME) $(ARCHCRT0OBJ) $(RENEW_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(RENEW_PROGNAME) -install: $(BIN_DIR)$(DELIM)$(PROGNAME) +install: $(BIN_DIR)$(DELIM)$(RENEW_PROGNAME) else install: @@ -115,10 +112,10 @@ install: endif ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) -$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile - $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) +$(BUILTIN_REGISTRY)$(DELIM)$(RENEW_APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(RENEW_APPNAME),$(RENEW_PRIORITY),$(RENEW_STACKSIZE),$(RENEW_APPNAME)_main) -context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +context: $(BUILTIN_REGISTRY)$(DELIM)$(RENEW_APPNAME)_main.bdat else context: endif diff --git a/system/dhcpc/dhcpc_main.c b/system/dhcpc/renew_main.c similarity index 98% rename from system/dhcpc/dhcpc_main.c rename to system/dhcpc/renew_main.c index 496a9a0e8..0e7d0c15e 100644 --- a/system/dhcpc/dhcpc_main.c +++ b/system/dhcpc/renew_main.c @@ -1,5 +1,5 @@ /**************************************************************************** - * system/dhcpc/dhcpc_main.c + * system/dhcpc/renew_main.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -66,13 +66,13 @@ static void dhcpc_showusage(FAR const char *progname, int exitcode) ****************************************************************************/ /**************************************************************************** - * dhcpc_main + * renew_main ****************************************************************************/ #ifdef CONFIG_BUILD_KERNEL int main(int argc, FAR char *argv[]) #else -int dhcpc_main(int argc, char *argv[]) +int renew_main(int argc, char *argv[]) #endif { FAR const char *devname; From 3c97bfeae6815f56f1694f94f435c07af9e5a0af Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Jun 2017 19:14:39 -0600 Subject: [PATCH 09/23] examples/nettest: Fixes for building with loopback device again after the last changes. --- examples/README.txt | 13 +++++++++++++ examples/nettest/Kconfig | 5 ++++- examples/nettest/nettest_client.c | 2 +- examples/nettest/nettest_server.c | 2 +- examples/nettest/nettest_target1.c | 12 ++++++++++++ examples/nettest/target_netinit.c | 11 ----------- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/examples/README.txt b/examples/README.txt index b777737c9..a0de89082 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -960,6 +960,13 @@ examples/nettest CONFIG_EXAMPLES_NETTEST=y - Enables the nettest example CONFIG_EXAMPLES_NETLIB=y - The networking library in needed. + Configurations: + + - Server on target hardware; client on host + - Client on target hardware; Server on host + - Server and Client on different targets. + - Loopback configuration with both client and server on the same target. + See also examples/tcpecho examples/nrf24l01_term @@ -2106,6 +2113,12 @@ examples/udp CONFIG_NETUTILS_NETLIB=y + Possible configurations: + + - Server on target hardware; client on host + - Client on target hardware; Server on host + - Server and Client on different targets. + examples/udpblaster ^^^^^^^^^^^^^^^^^^^ diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index 67aef0954..eb6de63a8 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -78,14 +78,17 @@ config EXAMPLES_NETTEST_STACKSIZE2 default 2048 endif # EXAMPLES_NETTEST_TARGET2 +endif # !EXAMPLES_NETTEST_LOOPBACK +if EXAMPLES_NETTEST_LOOPBACK config EXAMPLES_NETTEST_DAEMON_STACKSIZE int "Server daemon stack size" default 2048 -config EXAMPLES_NETTEST_DEAMON_PRIORITY +config EXAMPLES_NETTEST_DAEMON_PRIORITY int "Server daemon priority" default 100 +#endif endif # EXAMPLES_NETTEST_LOOPBACK diff --git a/examples/nettest/nettest_client.c b/examples/nettest/nettest_client.c index 4883504df..8792fa527 100644 --- a/examples/nettest/nettest_client.c +++ b/examples/nettest/nettest_client.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/nettest/nettest-client.c + * examples/nettest/nettest_client.c * * Copyright (C) 2007, 2011-2012, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/nettest/nettest_server.c b/examples/nettest/nettest_server.c index 755fdf096..1f74ed2c9 100644 --- a/examples/nettest/nettest_server.c +++ b/examples/nettest/nettest_server.c @@ -120,7 +120,7 @@ void recv_server(void) #endif addrlen = sizeof(struct sockaddr_in6); - printf("Binding to IPv6 Address: %04x:04x:04x:04x:04x:04x:04x:04x\n", + printf("Binding to IPv6 Address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", myaddr.sin6_addr.s6_addr16[0], myaddr.sin6_addr.s6_addr16[1], myaddr.sin6_addr.s6_addr16[2], myaddr.sin6_addr.s6_addr16[3], myaddr.sin6_addr.s6_addr16[4], myaddr.sin6_addr.s6_addr16[5], diff --git a/examples/nettest/nettest_target1.c b/examples/nettest/nettest_target1.c index 0a5d968dc..a5ae33a13 100644 --- a/examples/nettest/nettest_target1.c +++ b/examples/nettest/nettest_target1.c @@ -49,6 +49,18 @@ #include "nettest.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_NETTEST_LOOPBACK +static int server_child(int argc, char *argv[]) +{ + recv_server(); + return EXIT_SUCCESS; +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/examples/nettest/target_netinit.c b/examples/nettest/target_netinit.c index 78193737b..ecf82d673 100644 --- a/examples/nettest/target_netinit.c +++ b/examples/nettest/target_netinit.c @@ -117,7 +117,6 @@ static const uint16_t g_ipv6_netmask[8] = * Public Functions ****************************************************************************/ -#ifdef CONFIG_EXAMPLES_NETTEST_INIT void nettest_initialize(void) { #ifndef CONFIG_EXAMPLES_NETTEST_IPv6 @@ -188,16 +187,6 @@ void nettest_initialize(void) #endif /* CONFIG_EXAMPLES_NETTEST_IPv6 */ } -#endif /*CONFIG_EXAMPLES_NETTEST_INIT */ - -#ifdef CONFIG_EXAMPLES_NETTEST_LOOPBACK -static int server_child(int argc, char *argv[]) -{ - recv_server(); - return EXIT_SUCCESS; -} -#endif - #endif /* CONFIG_EXAMPLES_NETTEST_INIT */ From b868fbe87908a369214e979ede3b21fc44d06ff0 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 24 Jun 2017 09:49:44 -0600 Subject: [PATCH 10/23] examples/nettest: The send buffer size is now a configuration option. --- examples/nettest/Kconfig | 8 ++++++++ examples/nettest/nettest.h | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index eb6de63a8..5cdda3419 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -12,6 +12,14 @@ config EXAMPLES_NETTEST if EXAMPLES_NETTEST +config EXAMPLES_NETTEST_SENDSIZE + int "Payload size" + default 4096 + ---help--- + The test client sends well-known packets to ther server. The server + will receive and verfity those packets. This setting determines + size of each packet. + config EXAMPLES_NETTEST_PROGNAME1 string "Target1 program name" default "nettest" diff --git a/examples/nettest/nettest.h b/examples/nettest/nettest.h index a795e96e9..51996ff66 100644 --- a/examples/nettest/nettest.h +++ b/examples/nettest/nettest.h @@ -81,7 +81,11 @@ # define CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO 5471 #endif -#define SENDSIZE 4096 +#ifdef CONFIG_EXAMPLES_NETTEST_SENDSIZE +# define SENDSIZE CONFIG_EXAMPLES_NETTEST_SENDSIZE +#else +# define SENDSIZE 4096 +#endif /**************************************************************************** * Public Data From 664ab42da7e2947425e4acbfae49333a04e6e9ab Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 25 Jun 2017 09:15:27 -0600 Subject: [PATCH 11/23] examples/udp and examples/nettest: Fix some naming collisions; examples/nettest: Fix some build issues with two targets. --- examples/nettest/Kconfig | 4 ++-- examples/nettest/Makefile | 14 +++++++------- examples/nettest/nettest.h | 10 +++++----- examples/nettest/nettest_client.c | 12 ++++++------ examples/nettest/nettest_cmdline.c | 14 +++++++------- examples/nettest/nettest_host.c | 4 ++-- .../{target_netinit.c => nettest_initinit.c} | 2 +- examples/nettest/nettest_server.c | 6 +++--- examples/nettest/nettest_target1.c | 8 ++++---- examples/nettest/nettest_target2.c | 6 +++--- examples/udp/Makefile | 2 +- examples/udp/udp.h | 12 ++++++------ examples/udp/udp_client.c | 6 +++--- examples/udp/udp_cmdline.c | 12 ++++++------ examples/udp/udp_host.c | 6 +++--- examples/udp/{target_netinit.c => udp_netinit.c} | 6 +++--- examples/udp/udp_server.c | 2 +- examples/udp/udp_target1.c | 8 ++++---- examples/udp/udp_target2.c | 8 ++++---- 19 files changed, 71 insertions(+), 71 deletions(-) rename examples/nettest/{target_netinit.c => nettest_initinit.c} (99%) rename examples/udp/{target_netinit.c => udp_netinit.c} (98%) diff --git a/examples/nettest/Kconfig b/examples/nettest/Kconfig index 5cdda3419..5eea86439 100644 --- a/examples/nettest/Kconfig +++ b/examples/nettest/Kconfig @@ -70,8 +70,8 @@ if EXAMPLES_NETTEST_TARGET2 config EXAMPLES_NETTEST_PROGNAME2 string "Target2 program name" - default "udpserver" if !EXAMPLES_NETTEST_SERVER2 - default "udpclient" if EXAMPLES_NETTEST_SERVER2 + default "tcpserver" if !EXAMPLES_NETTEST_SERVER2 + default "tcpclient" if EXAMPLES_NETTEST_SERVER2 depends on BUILD_KERNEL ---help--- This is the name of the Target2 program that will be use when the diff --git a/examples/nettest/Makefile b/examples/nettest/Makefile index b082fe532..ba2241cea 100644 --- a/examples/nettest/Makefile +++ b/examples/nettest/Makefile @@ -41,7 +41,7 @@ include $(APPDIR)/Make.defs TARGCMN_CSRCS = nettest_cmdline.c ifeq ($(CONFIG_EXAMPLES_NETTEST_INIT),y) -TARGCMN_CSRCS += target_netinit.c +TARGCMN_CSRCS += nettest_netinit.c endif TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT)) @@ -92,7 +92,7 @@ TARG2_CSRCS += nettest_server.c endif TARG2_MAINSRC = nettest_target2.c -TARG2_COBJS = $(TARG2_CCRCS:.c=$(OBJEXT)) +TARG2_COBJS = $(TARG2_CSRCS:.c=$(OBJEXT)) TARG2_MAINOBJ = $(TARG2_MAINSRC:.c=$(OBJEXT)) # Target 2 Application Info @@ -182,10 +182,10 @@ VPATH = all: .built $(HOST_BIN) .PHONY: clean depend distclean preconfig -$(TARG1_COBJS) $(TARG1_MAINOBJ) $(TARGCMN_COBJS) : %$(OBJEXT): %.c +$(TARG1_COBJS) $(TARG1_MAINOBJ) $(TARG2_COBJS) $(TARG2_MAINOBJ) $(TARGCMN_COBJS) : %$(OBJEXT): %.c $(call COMPILE, $<, $@) -ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) +ifneq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) $(HOST_OBJS): %.$(HOSTOBJEXT): %.c @@ -199,7 +199,7 @@ config.h: $(TOPDIR)/include/nuttx/config.h @echo "CP: $<" $(Q) cp $< $@ -ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) +ifneq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) $(HOST_BIN): config.h $(HOST_OBJS) @@ -230,7 +230,7 @@ ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat: $(DEPCONFIG) Makefile $(call REGISTER,$(APPNAME1),$(PRIORITY1),$(STACKSIZE1),$(MAINNAME1)) -ifeq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) +ifeq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME2)_main.bdat: $(DEPCONFIG) Makefile $(call REGISTER,$(APPNAME2),$(PRIORITY2),$(STACKSIZE2),$(MAINNAME2)) @@ -250,7 +250,7 @@ endif depend: .depend clean: -ifneq ($(CONFIG_EXAMPLES_UDP_TARGET2),y) +ifneq ($(CONFIG_EXAMPLES_NETTEST_TARGET2),y) ifneq ($(CONFIG_EXAMPLES_NETTEST_LOOPBACK),y) $(call DELFILE, *.$(HOSTOBJEXT)) $(call DELFILE, $(HOST_BIN)) diff --git a/examples/nettest/nettest.h b/examples/nettest/nettest.h index 51996ff66..18e78f222 100644 --- a/examples/nettest/nettest.h +++ b/examples/nettest/nettest.h @@ -92,9 +92,9 @@ ****************************************************************************/ #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 -uint16_t g_server_ipv6[8]; +uint16_t g_nettestserver_ipv6[8]; #else -uint32_t g_server_ipv4; +uint32_t g_nettestserver_ipv4; #endif /**************************************************************************** @@ -105,8 +105,8 @@ uint32_t g_server_ipv4; void nettest_initialize(void); #endif -void parse_cmdline(int argc, char **argv); -extern void send_client(void); -extern void recv_server(void); +void nettest_cmdline(int argc, char **argv); +extern void nettest_client(void); +extern void nettest_server(void); #endif /* __EXAMPLES_NETTEST_H */ diff --git a/examples/nettest/nettest_client.c b/examples/nettest/nettest_client.c index 8792fa527..00035b14d 100644 --- a/examples/nettest/nettest_client.c +++ b/examples/nettest/nettest_client.c @@ -56,7 +56,7 @@ * Public Functions ****************************************************************************/ -void send_client(void) +void nettest_client(void) { #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 struct sockaddr_in6 server; @@ -105,19 +105,19 @@ void send_client(void) #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 server.sin6_family = AF_INET6; server.sin6_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); - memcpy(server.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); + memcpy(server.sin6_addr.s6_addr16, g_nettestserver_ipv6, 8 * sizeof(uint16_t)); addrlen = sizeof(struct sockaddr_in6); printf("Connecting to IPv6 Address: %04x:04x:04x:04x:04x:04x:04x:04x\n", - g_server_ipv6[0], g_server_ipv6[1], g_server_ipv6[2], g_server_ipv6[3], - g_server_ipv6[4], g_server_ipv6[5], g_server_ipv6[6], g_server_ipv6[7]); + g_nettestserver_ipv6[0], g_nettestserver_ipv6[1], g_nettestserver_ipv6[2], g_nettestserver_ipv6[3], + g_nettestserver_ipv6[4], g_nettestserver_ipv6[5], g_nettestserver_ipv6[6], g_nettestserver_ipv6[7]); #else server.sin_family = AF_INET; server.sin_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); - server.sin_addr.s_addr = (in_addr_t)g_server_ipv4; + server.sin_addr.s_addr = (in_addr_t)g_nettestserver_ipv4; addrlen = sizeof(struct sockaddr_in); - printf("Connecting to IPv4 Address: %08lx\n", (unsigned long)g_server_ipv4); + printf("Connecting to IPv4 Address: %08lx\n", (unsigned long)g_nettestserver_ipv4); #endif /* Connect the socket to the server */ diff --git a/examples/nettest/nettest_cmdline.c b/examples/nettest/nettest_cmdline.c index 479e83336..7ebe57b06 100644 --- a/examples/nettest/nettest_cmdline.c +++ b/examples/nettest/nettest_cmdline.c @@ -51,7 +51,7 @@ #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 -uint16_t g_server_ipv6[8] = +uint16_t g_nettestserver_ipv6[8] = { #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) 0 /* Use the loopback address */ @@ -77,9 +77,9 @@ uint16_t g_server_ipv6[8] = #else #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(NET_LOOPBACK) -uint32_t g_server_ipv4 = HTONL(0x7f000001); +uint32_t g_nettestserver_ipv4 = HTONL(0x7f000001); #else -uint32_t g_server_ipv4 = HTONL(CONFIG_EXAMPLES_NETTEST_SERVERIP); +uint32_t g_nettestserver_ipv4 = HTONL(CONFIG_EXAMPLES_NETTEST_SERVERIP); #endif #endif @@ -103,10 +103,10 @@ static void show_usage(FAR const char *progname) ****************************************************************************/ /**************************************************************************** - * parse_cmdline + * nettest_cmdline ****************************************************************************/ -void parse_cmdline(int argc, char **argv) +void nettest_cmdline(int argc, char **argv) { /* Currently only a single command line option is supported: The server * IP address. @@ -119,9 +119,9 @@ void parse_cmdline(int argc, char **argv) /* Convert the argument into a binary address */ #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 - ret = inet_pton(AF_INET6, argv[1], g_server_ipv6); + ret = inet_pton(AF_INET6, argv[1], g_nettestserver_ipv6); #else - ret = inet_pton(AF_INET, argv[1], &g_server_ipv4); + ret = inet_pton(AF_INET, argv[1], &g_nettestserver_ipv4); #endif if (ret < 0) { diff --git a/examples/nettest/nettest_host.c b/examples/nettest/nettest_host.c index f89477655..00db47603 100644 --- a/examples/nettest/nettest_host.c +++ b/examples/nettest/nettest_host.c @@ -55,9 +55,9 @@ int main(int argc, char **argv, char **envp) { #ifdef CONFIG_EXAMPLES_NETTEST_SERVER - send_client(); + nettest_client(); #else - recv_server(); + nettest_server(); #endif return 0; diff --git a/examples/nettest/target_netinit.c b/examples/nettest/nettest_initinit.c similarity index 99% rename from examples/nettest/target_netinit.c rename to examples/nettest/nettest_initinit.c index ecf82d673..8a5072104 100644 --- a/examples/nettest/target_netinit.c +++ b/examples/nettest/nettest_initinit.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/nettest/target_netinit.c + * examples/nettest/nettest_netinit.c * * Copyright (C) 2007, 2009-2011, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/examples/nettest/nettest_server.c b/examples/nettest/nettest_server.c index 1f74ed2c9..0d690edae 100644 --- a/examples/nettest/nettest_server.c +++ b/examples/nettest/nettest_server.c @@ -57,7 +57,7 @@ * Public Functions ****************************************************************************/ -void recv_server(void) +void nettest_server(void) { #ifdef CONFIG_EXAMPLES_NETTEST_IPv6 struct sockaddr_in6 myaddr; @@ -114,7 +114,7 @@ void recv_server(void) myaddr.sin6_family = AF_INET6; myaddr.sin6_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && !defined(NET_LOOPBACK) - memcpy(myaddr.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); + memcpy(myaddr.sin6_addr.s6_addr16, g_nettestserver_ipv6, 8 * sizeof(uint16_t)); #else memset(myaddr.sin6_addr.s6_addr16, 0, 8 * sizeof(uint16_t)); #endif @@ -130,7 +130,7 @@ void recv_server(void) myaddr.sin_port = HTONS(CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO); #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && !defined(NET_LOOPBACK) - myaddr.sin_addr.s_addr = (in_addr_t)g_server_ipv4; + myaddr.sin_addr.s_addr = (in_addr_t)g_nettestserver_ipv4; #else myaddr.sin_addr.s_addr = INADDR_ANY; #endif diff --git a/examples/nettest/nettest_target1.c b/examples/nettest/nettest_target1.c index a5ae33a13..2d6076708 100644 --- a/examples/nettest/nettest_target1.c +++ b/examples/nettest/nettest_target1.c @@ -56,7 +56,7 @@ #ifdef CONFIG_EXAMPLES_NETTEST_LOOPBACK static int server_child(int argc, char *argv[]) { - recv_server(); + nettest_server(); return EXIT_SUCCESS; } #endif @@ -86,7 +86,7 @@ int nettest_main(int argc, char *argv[]) /* Parse any command line options */ - parse_cmdline(argc, argv); + nettest_cmdline(argc, argv); #ifdef CONFIG_EXAMPLES_NETTEST_INIT /* Initialize the network */ @@ -111,14 +111,14 @@ int nettest_main(int argc, char *argv[]) #elif defined(CONFIG_EXAMPLES_NETTEST_SERVER) /* Then perform the server side of the test on this thread */ - recv_server(); + nettest_server(); #endif #if !defined(CONFIG_EXAMPLES_NETTEST_SERVER) || \ defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) /* Then perform the client side of the test on this thread */ - send_client(); + nettest_client(); #endif #if defined(CONFIG_EXAMPLES_NETTEST_LOOPBACK) && defined(CONFIG_SCHED_WAITPID) diff --git a/examples/nettest/nettest_target2.c b/examples/nettest/nettest_target2.c index aa246b3c9..277b1e1df 100644 --- a/examples/nettest/nettest_target2.c +++ b/examples/nettest/nettest_target2.c @@ -53,7 +53,7 @@ int nettest2_main(int argc, char *argv[]) { /* Parse any command line options */ - parse_cmdline(argc, argv); + nettest_cmdline(argc, argv); #ifdef CONFIG_EXAMPLES_NETTEST_INIT /* Initialize the network */ @@ -64,11 +64,11 @@ int nettest2_main(int argc, char *argv[]) #if defined(CONFIG_EXAMPLES_NETTEST_SERVER) /* Then perform the client side of the test on this thread */ - send_client(); + nettest_client(); #else /* Then perform the server side of the test on this thread */ - recv_server(); + nettest_server(); #endif return EXIT_SUCCESS; diff --git a/examples/udp/Makefile b/examples/udp/Makefile index 902d34d66..e75da5306 100644 --- a/examples/udp/Makefile +++ b/examples/udp/Makefile @@ -41,7 +41,7 @@ include $(APPDIR)/Make.defs TARGCMN_CSRCS = udp_cmdline.c ifeq ($(CONFIG_EXAMPLES_UDP_NETINIT),y) -TARGCMN_CSRCS += target_netinit.c +TARGCMN_CSRCS += udp_netinit.c endif TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT)) diff --git a/examples/udp/udp.h b/examples/udp/udp.h index 193d5e2cb..b00bac9bb 100644 --- a/examples/udp/udp.h +++ b/examples/udp/udp.h @@ -99,9 +99,9 @@ ****************************************************************************/ #ifdef CONFIG_EXAMPLES_UDP_IPv6 -uint16_t g_server_ipv6[8]; +uint16_t g_udpserver_ipv6[8]; #else -uint32_t g_server_ipv4; +uint32_t g_udpserver_ipv4; #endif /**************************************************************************** @@ -109,11 +109,11 @@ uint32_t g_server_ipv4; ****************************************************************************/ #ifdef CONFIG_EXAMPLES_UDP_NETINIT -int target_netinit(void); +int udp_netinit(void); #endif -void parse_cmdline(int argc, char **argv); -void send_client(void); -void recv_server(void); +void udp_cmdline(int argc, char **argv); +void udp_client(void); +void udp_server(void); #endif /* __EXAMPLES_UDP_UDP_H */ diff --git a/examples/udp/udp_client.c b/examples/udp/udp_client.c index 89742dca8..6387963b7 100644 --- a/examples/udp/udp_client.c +++ b/examples/udp/udp_client.c @@ -130,7 +130,7 @@ static inline void fill_buffer(unsigned char *buf, int offset) * Public Functions ****************************************************************************/ -void send_client(void) +void udp_client(void) { #ifdef CONFIG_EXAMPLES_UDP_IPv6 struct sockaddr_in6 server; @@ -165,12 +165,12 @@ void send_client(void) #ifdef CONFIG_EXAMPLES_UDP_IPv6 server.sin6_family = AF_INET6; server.sin6_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); - memcpy(server.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); + memcpy(server.sin6_addr.s6_addr16, g_udpserver_ipv6, 8 * sizeof(uint16_t)); addrlen = sizeof(struct sockaddr_in6); #else server.sin_family = AF_INET; server.sin_port = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO); - server.sin_addr.s_addr = (in_addr_t)g_server_ipv4; + server.sin_addr.s_addr = (in_addr_t)g_udpserver_ipv4; addrlen = sizeof(struct sockaddr_in); #endif diff --git a/examples/udp/udp_cmdline.c b/examples/udp/udp_cmdline.c index 8eb93f8d8..8645b972d 100644 --- a/examples/udp/udp_cmdline.c +++ b/examples/udp/udp_cmdline.c @@ -50,7 +50,7 @@ ****************************************************************************/ #ifdef CONFIG_EXAMPLES_UDP_IPv6 -uint16_t g_server_ipv6[8] = +uint16_t g_udpserver_ipv6[8] = { HTONS(CONFIG_EXAMPLES_UDP_SERVERIPv6ADDR_1), HTONS(CONFIG_EXAMPLES_UDP_SERVERIPv6ADDR_2), @@ -62,7 +62,7 @@ uint16_t g_server_ipv6[8] = HTONS(CONFIG_EXAMPLES_UDP_SERVERIPv6ADDR_8) }; #else -uint32_t g_server_ipv4 = HTONL(CONFIG_EXAMPLES_UDP_SERVERIP); +uint32_t g_udpserver_ipv4 = HTONL(CONFIG_EXAMPLES_UDP_SERVERIP); #endif /**************************************************************************** @@ -84,10 +84,10 @@ static void show_usage(FAR const char *progname) ****************************************************************************/ /**************************************************************************** - * parse_cmdline + * udp_cmdline ****************************************************************************/ -void parse_cmdline(int argc, char **argv) +void udp_cmdline(int argc, char **argv) { /* Currently only a single command line option is supported: The server * IP address. @@ -100,9 +100,9 @@ void parse_cmdline(int argc, char **argv) /* Convert the argument into a binary address */ #ifdef CONFIG_EXAMPLES_UDP_IPv6 - ret = inet_pton(AF_INET6, argv[1], g_server_ipv6); + ret = inet_pton(AF_INET6, argv[1], g_udpserver_ipv6); #else - ret = inet_pton(AF_INET, argv[1], &g_server_ipv4); + ret = inet_pton(AF_INET, argv[1], &g_udpserver_ipv4); #endif if (ret < 0) { diff --git a/examples/udp/udp_host.c b/examples/udp/udp_host.c index 7e1aaacc5..206389205 100644 --- a/examples/udp/udp_host.c +++ b/examples/udp/udp_host.c @@ -56,14 +56,14 @@ int main(int argc, char **argv, char **envp) { /* Parse any command line options */ - parse_cmdline(argc, argv); + udp_cmdline(argc, argv); /* Run the server or client, depending upon how target1 was configured */ #ifdef CONFIG_EXAMPLES_UDP_SERVER1 - send_client(); + udp_client(); #else - recv_server(); + udp_server(); #endif return 0; diff --git a/examples/udp/target_netinit.c b/examples/udp/udp_netinit.c similarity index 98% rename from examples/udp/target_netinit.c rename to examples/udp/udp_netinit.c index d66a6d3ca..52c3d5f9d 100644 --- a/examples/udp/target_netinit.c +++ b/examples/udp/udp_netinit.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/udp/target_netinit.c + * examples/udp/udp_netinit.c * * Copyright (C) 2007, 2011, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -117,10 +117,10 @@ static bool g_initialized; ****************************************************************************/ /**************************************************************************** - * target_netinit + * udp_netinit ****************************************************************************/ -int target_netinit(void) +int udp_netinit(void) { if (!g_initialized) { diff --git a/examples/udp/udp_server.c b/examples/udp/udp_server.c index 967b493c7..08de11a76 100644 --- a/examples/udp/udp_server.c +++ b/examples/udp/udp_server.c @@ -84,7 +84,7 @@ static inline int check_buffer(unsigned char *buf) * Public Functions ****************************************************************************/ -void recv_server(void) +void udp_server(void) { #ifdef CONFIG_EXAMPLES_UDP_IPv6 struct sockaddr_in6 server; diff --git a/examples/udp/udp_target1.c b/examples/udp/udp_target1.c index ce655aec5..2b54bd5e5 100644 --- a/examples/udp/udp_target1.c +++ b/examples/udp/udp_target1.c @@ -58,20 +58,20 @@ int udp_main(int argc, char *argv[]) { /* Parse any command line options */ - parse_cmdline(argc, argv); + udp_cmdline(argc, argv); #ifdef CONFIG_EXAMPLES_UDP_NETINIT /* Initialize the network */ - (void)target_netinit(); + (void)udp_netinit(); #endif /* Run the server or client, depending upon how we are configured */ #ifdef CONFIG_EXAMPLES_UDP_SERVER1 - recv_server(); + udp_server(); #else - send_client(); + udp_client(); #endif return 0; diff --git a/examples/udp/udp_target2.c b/examples/udp/udp_target2.c index 0fcb6e37c..59aeee28c 100644 --- a/examples/udp/udp_target2.c +++ b/examples/udp/udp_target2.c @@ -56,20 +56,20 @@ int udp2_main(int argc, char *argv[]) { /* Parse any command line options */ - parse_cmdline(argc, argv); + udp_cmdline(argc, argv); #ifdef CONFIG_EXAMPLES_UDP_NETINIT /* Initialize the network */ - (void)target_netinit(); + (void)udp_netinit(); #endif /* Run the server or client, depending upon how target1 was configured */ #ifdef CONFIG_EXAMPLES_UDP_SERVER1 - send_client(); + udp_client(); #else - recv_server(); + udp_server(); #endif return 0; From 6b25b0d65e008ebd02fbe4b8857f96e4806b47ab Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 25 Jun 2017 13:01:21 -0600 Subject: [PATCH 12/23] examples/nettest: Fix some printf output --- examples/nettest/nettest_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/nettest/nettest_client.c b/examples/nettest/nettest_client.c index 00035b14d..2d91877ec 100644 --- a/examples/nettest/nettest_client.c +++ b/examples/nettest/nettest_client.c @@ -108,7 +108,7 @@ void nettest_client(void) memcpy(server.sin6_addr.s6_addr16, g_nettestserver_ipv6, 8 * sizeof(uint16_t)); addrlen = sizeof(struct sockaddr_in6); - printf("Connecting to IPv6 Address: %04x:04x:04x:04x:04x:04x:04x:04x\n", + printf("Connecting to IPv6 Address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", g_nettestserver_ipv6[0], g_nettestserver_ipv6[1], g_nettestserver_ipv6[2], g_nettestserver_ipv6[3], g_nettestserver_ipv6[4], g_nettestserver_ipv6[5], g_nettestserver_ipv6[6], g_nettestserver_ipv6[7]); #else From 88e0312897115d41409521a50f4fd3a2b4dc121f Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Wed, 21 Jun 2017 15:01:39 -0400 Subject: [PATCH 13/23] ieee802154: Changes to support beacon-enabled networks --- include/wireless/ieee802154.h | 2 + wireless/ieee802154/i8sak/i8sak_acceptassoc.c | 10 +++ wireless/ieee802154/i8sak/i8sak_startpan.c | 24 +++++-- wireless/ieee802154/libmac/Makefile | 2 +- .../libmac/ieee802154_setassocpermit.c | 64 +++++++++++++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 wireless/ieee802154/libmac/ieee802154_setassocpermit.c diff --git a/include/wireless/ieee802154.h b/include/wireless/ieee802154.h index 7822a4e5c..276c90ad6 100644 --- a/include/wireless/ieee802154.h +++ b/include/wireless/ieee802154.h @@ -112,6 +112,8 @@ int ieee802154_getcca(int fd, FAR struct ieee802154_cca_s *cca); int ieee802154_getdevmode(int fd, FAR enum ieee802154_devmode_e *devmode); +int ieee802154_setassocpermit(int fd, bool assocpermit); + #ifdef CONFIG_NET_6LOWPAN /* Netork driver IOCTL helpers */ diff --git a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c index c68caa466..6a896a76b 100644 --- a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c +++ b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c @@ -76,6 +76,7 @@ void i8sak_acceptassoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[] bool acceptall = true; /* start off assuming we are going to allow all connections */ int option; int optcnt; + int fd; optcnt = 0; while ((option = getopt(argc, argv, ":he:")) != ERROR) @@ -115,6 +116,15 @@ void i8sak_acceptassoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[] } } + fd = open(i8sak->devname, O_RDWR); + if (fd < 0) + { + printf("cannot open %s, errno=%d\n", i8sak->devname, errno); + i8sak_cmd_error(i8sak); + } + + ieee802154_setassocpermit(fd, true); + if (!optcnt) { i8sak->acceptall = acceptall; diff --git a/wireless/ieee802154/i8sak/i8sak_startpan.c b/wireless/ieee802154/i8sak/i8sak_startpan.c index 409e83ea2..5a09a58d7 100644 --- a/wireless/ieee802154/i8sak/i8sak_startpan.c +++ b/wireless/ieee802154/i8sak/i8sak_startpan.c @@ -73,11 +73,12 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) { struct ieee802154_reset_req_s resetreq; struct ieee802154_start_req_s startreq; + bool beaconenabled = false; int option; int fd; int i; - while ((option = getopt(argc, argv, ":h")) != ERROR) + while ((option = getopt(argc, argv, ":hb")) != ERROR) { switch (option) { @@ -85,11 +86,15 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) fprintf(stderr, "Starts PAN as PAN Coordinator\n" "Usage: %s [-h]\n" " -h = this help menu\n" + " -b = start beacon-enabled PAN\n" , argv[0]); /* Must manually reset optind if we are going to exit early */ optind = -1; return; + case 'b': + beaconenabled = true; + break; case ':': fprintf(stderr, "ERROR: missing argument\n"); /* Must manually reset optind if we are going to exit early */ @@ -184,11 +189,22 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) printf("i8sak: starting PAN\n"); IEEE802154_PANIDCOPY(startreq.panid, i8sak->addr.panid); - startreq.chnum = i8sak->chnum; + startreq.chnum = i8sak->chnum; startreq.chpage = i8sak->chpage; - startreq.beaconorder = 15; - startreq.pancoord = true; + + if (beaconenabled) + { + startreq.beaconorder = 6; + startreq.superframeorder = 5; + } + else + { + startreq.beaconorder = 15; + } + + startreq.pancoord = true; startreq.coordrealign = false; + startreq.battlifeext = false; ieee802154_start_req(fd, &startreq); diff --git a/wireless/ieee802154/libmac/Makefile b/wireless/ieee802154/libmac/Makefile index fe4022108..6dbf07cf4 100644 --- a/wireless/ieee802154/libmac/Makefile +++ b/wireless/ieee802154/libmac/Makefile @@ -54,7 +54,7 @@ CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c -CSRCS += ieee802154_getdevmode.c +CSRCS += ieee802154_getdevmode.c ieee802154_setassocpermit.c ifeq ($(CONFIG_NET_6LOWPAN),y) # Add Get/Set Attribute helpers diff --git a/wireless/ieee802154/libmac/ieee802154_setassocpermit.c b/wireless/ieee802154/libmac/ieee802154_setassocpermit.c new file mode 100644 index 000000000..9336e4699 --- /dev/null +++ b/wireless/ieee802154/libmac/ieee802154_setassocpermit.c @@ -0,0 +1,64 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/ieee802154_assocpermit.c + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ieee802154_setassocpermit(int fd, bool assocpermit) +{ + struct ieee802154_set_req_s req; + + req.attr = IEEE802154_ATTR_MAC_ASSOCIATION_PERMIT; + req.attrval.mac.assocpermit = assocpermit; + + return ieee802154_set_req(fd, &req); +} From eee82fbc99ac60bb0a8c3bb0d929a67d95e612de Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Sun, 25 Jun 2017 20:01:16 -0400 Subject: [PATCH 14/23] ieee802154: Adds scan command --- wireless/ieee802154/i8sak/Makefile | 5 +- wireless/ieee802154/i8sak/i8sak.h | 7 +- wireless/ieee802154/i8sak/i8sak_assoc.c | 34 ++- wireless/ieee802154/i8sak/i8sak_chan.c | 139 ++++++++++ wireless/ieee802154/i8sak/i8sak_main.c | 8 +- wireless/ieee802154/i8sak/i8sak_poll.c | 6 - wireless/ieee802154/i8sak/i8sak_scan.c | 253 ++++++++++++++++++ wireless/ieee802154/i8sak/i8sak_startpan.c | 2 +- wireless/ieee802154/i8sak/i8sak_tx.c | 8 +- .../ieee802154/libmac/ieee802154_getchan.c | 4 +- .../ieee802154/libmac/ieee802154_geteaddr.c | 2 +- .../ieee802154/libmac/ieee802154_getsaddr.c | 2 +- .../ieee802154/libmac/ieee802154_setchan.c | 4 +- .../ieee802154/libmac/ieee802154_seteaddr.c | 2 +- .../ieee802154/libmac/ieee802154_setsaddr.c | 2 +- .../ieee802154/libmac/sixlowpan_getchan.c | 4 +- .../ieee802154/libmac/sixlowpan_geteaddr.c | 2 +- .../ieee802154/libmac/sixlowpan_getsaddr.c | 2 +- .../ieee802154/libmac/sixlowpan_setchan.c | 4 +- .../ieee802154/libmac/sixlowpan_seteaddr.c | 2 +- .../ieee802154/libmac/sixlowpan_setsaddr.c | 2 +- 21 files changed, 449 insertions(+), 45 deletions(-) create mode 100644 wireless/ieee802154/i8sak/i8sak_chan.c create mode 100644 wireless/ieee802154/i8sak/i8sak_scan.c diff --git a/wireless/ieee802154/i8sak/Makefile b/wireless/ieee802154/i8sak/Makefile index 530740af5..cef46ecfe 100644 --- a/wireless/ieee802154/i8sak/Makefile +++ b/wireless/ieee802154/i8sak/Makefile @@ -47,8 +47,9 @@ STACKSIZE = 4096 # IEEE 802.15.4 SAK (Swiss Army Knife) ASRCS = -CSRCS = i8sak_acceptassoc.c i8sak_assoc.c i8sak_blaster.c i8sak_poll.c -CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c wpanlistener.c +CSRCS = i8sak_acceptassoc.c i8sak_assoc.c i8sak_scan.c i8sak_blaster.c i8sak_poll.c +CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c i8sak_chan.c +CSRCS += wpanlistener.c MAINSRC = i8sak_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) diff --git a/wireless/ieee802154/i8sak/i8sak.h b/wireless/ieee802154/i8sak/i8sak.h index c9508ce14..0d6d1395a 100644 --- a/wireless/ieee802154/i8sak/i8sak.h +++ b/wireless/ieee802154/i8sak/i8sak.h @@ -146,7 +146,7 @@ struct i8sak_s /* Settings */ - uint8_t chnum; + uint8_t chan; uint8_t chpage; struct ieee802154_addr_s addr; struct ieee802154_addr_s ep; @@ -154,6 +154,9 @@ struct i8sak_s uint8_t payload[IEEE802154_MAX_MAC_PAYLOAD_SIZE]; uint16_t payload_len; int blasterperiod; + + struct ieee802154_pandesc_s pandescs[CONFIG_MAC802154_NPANDESC]; + uint8_t npandesc; }; /**************************************************************************** @@ -175,10 +178,12 @@ bool i8sak_str2bool(FAR const char *str); void i8sak_startpan_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_acceptassoc_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_assoc_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); +void i8sak_scan_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_tx_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_poll_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_sniffer_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_blaster_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); +void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); /**************************************************************************** * Inline Functions diff --git a/wireless/ieee802154/i8sak/i8sak_assoc.c b/wireless/ieee802154/i8sak/i8sak_assoc.c index da34816ab..d8ef39b41 100644 --- a/wireless/ieee802154/i8sak/i8sak_assoc.c +++ b/wireless/ieee802154/i8sak/i8sak_assoc.c @@ -79,10 +79,12 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) { struct ieee802154_assoc_req_s assocreq; struct wpanlistener_eventfilter_s filter; + FAR struct ieee802154_pandesc_s *pandesc; int fd; int option; int optcnt; int ret; + uint8_t resindex; /* If the addresses has never been automatically or manually set before, set * it assuming that we are the default device address and the endpoint is the @@ -96,7 +98,7 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) } optcnt = 0; - while ((option = getopt(argc, argv, ":hs:e:")) != ERROR) + while ((option = getopt(argc, argv, ":hr:s:e:")) != ERROR) { optcnt++; switch (option) @@ -105,12 +107,31 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) fprintf(stderr, "Requests association with endpoint\n" "Usage: %s [-h]\n" " -h = this help menu\n" + " -r = use scan result index\n" , argv[0]); /* Must manually reset optind if we are going to exit early */ optind = -1; return; + case 'r': + resindex = i8sak_str2luint8(optarg); + + if (resindex >= i8sak->npandesc) + { + fprintf(stderr, "ERROR: missing argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + } + + pandesc = &i8sak->pandescs[resindex]; + + i8sak->chan = pandesc->chan; + i8sak->chpage = pandesc->chpage; + memcpy(&i8sak->ep, &pandesc->coordaddr, sizeof(struct ieee802154_addr_s)); + break; case 's': /* Parse extended address and put it into the i8sak instance */ @@ -158,15 +179,6 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) i8sak_cmd_error(i8sak); } - if (argc < 2) - { - /* TODO: Perform a scan operation here, to determine addressing information - * of coordinator. - */ - - fprintf(stderr, "i8sak: scan not implemented. Using default values\n"); - } - /* Register new callback for receiving the association notifications */ memset(&filter, 0, sizeof(struct wpanlistener_eventfilter_s)); @@ -177,7 +189,7 @@ void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) printf("i8sak: issuing ASSOC. request\n"); - assocreq.chnum = i8sak->chnum; + assocreq.chan = i8sak->chan; assocreq.chpage = i8sak->chpage; memcpy(&assocreq.coordaddr, &i8sak->ep, sizeof(struct ieee802154_addr_s)); diff --git a/wireless/ieee802154/i8sak/i8sak_chan.c b/wireless/ieee802154/i8sak/i8sak_chan.c new file mode 100644 index 000000000..0c27999ef --- /dev/null +++ b/wireless/ieee802154/i8sak/i8sak_chan.c @@ -0,0 +1,139 @@ +/**************************************************************************** + * apps/wireless/ieee802154/i8sak/i8sak_chan.c + * IEEE 802.15.4 Swiss Army Knife + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 "i8sak.h" + +#include +#include +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name : i8sak_chan_cmd + * + * Description : + * Try and extract data from the coordinator + ****************************************************************************/ + +void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) +{ + int option; + int fd; + int argind; + bool getchan = false; + uint8_t channel; + + argind = 1; + while ((option = getopt(argc, argv, ":hg")) != ERROR) + { + switch (option) + { + argind++; + case 'h': + fprintf(stderr, "Polls coordinator for data\n" + "Usage: %s [-h]\n" + " -h = this help menu\n" + , argv[0]); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + return; + case 'g': + getchan = true; + break; + + case ':': + fprintf(stderr, "ERROR: missing argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + case '?': + fprintf(stderr, "ERROR: unknown argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + } + } + + if (!getchan) + { + if (argc < argind + 1) + { + fprintf(stderr, "ERROR: missing channel\n"); + i8sak_cmd_error(i8sak); /* This exits for us */ + } + + channel = i8sak_str2luint8(argv[argind]); + } + + fd = open(i8sak->devname, O_RDWR); + if (fd < 0) + { + printf("cannot open %s, errno=%d\n", i8sak->devname, errno); + i8sak_cmd_error(i8sak); + } + + if (getchan) + { + ieee802154_getchan(fd, &channel); + printf("i8sak: Channel: %d\n", (int)channel); + } + else + { + printf("i8sak: Setting Channel: %d\n", (int)channel); + ieee802154_setchan(fd, channel); + } + + close(fd); +} diff --git a/wireless/ieee802154/i8sak/i8sak_main.c b/wireless/ieee802154/i8sak/i8sak_main.c index 96586778f..0e15fe48e 100644 --- a/wireless/ieee802154/i8sak/i8sak_main.c +++ b/wireless/ieee802154/i8sak/i8sak_main.c @@ -104,10 +104,12 @@ static const struct i8sak_command_s g_i8sak_commands[] = {"startpan", (CODE void *)i8sak_startpan_cmd}, {"acceptassoc", (CODE void *)i8sak_acceptassoc_cmd}, {"assoc", (CODE void *)i8sak_assoc_cmd}, + {"scan", (CODE void *)i8sak_scan_cmd}, {"tx", (CODE void *)i8sak_tx_cmd}, {"poll", (CODE void *)i8sak_poll_cmd}, {"sniffer", (CODE void *)i8sak_sniffer_cmd}, {"blaster", (CODE void *)i8sak_blaster_cmd}, + {"chan", (CODE void *)i8sak_chan_cmd}, }; #define NCOMMANDS (sizeof(g_i8sak_commands) / sizeof(struct i8sak_command_s)) @@ -559,7 +561,7 @@ static int i8sak_setup(FAR struct i8sak_s *i8sak, FAR const char *devname) i8sak->daemon_started = false; i8sak->daemon_shutdown = false; - i8sak->chnum = CONFIG_IEEE802154_I8SAK_CHNUM; + i8sak->chan = CONFIG_IEEE802154_I8SAK_CHNUM; i8sak->chpage = CONFIG_IEEE802154_I8SAK_CHPAGE; if (strlen(devname) > I8SAK_MAX_DEVNAME) @@ -730,11 +732,13 @@ static int i8sak_showusage(FAR const char *progname, int exitcode) fprintf(stderr, "Usage: %s\n" " startpan [-h]\n" " acceptassoc [-h|e ]\n" - " assoc [-h] [] \n" + " scan [-h|p|a|e] minch-maxch\n" + " assoc [-h] []\n" " tx [-h|d] \n" " poll [-h]\n" " blaster [-h|q|f |p ]\n" " sniffer [-h|q]\n" + " chan [-h|g] []" , progname); exit(exitcode); } diff --git a/wireless/ieee802154/i8sak/i8sak_poll.c b/wireless/ieee802154/i8sak/i8sak_poll.c index 7db6eaf48..e62a7f7a9 100644 --- a/wireless/ieee802154/i8sak/i8sak_poll.c +++ b/wireless/ieee802154/i8sak/i8sak_poll.c @@ -83,7 +83,6 @@ void i8sak_poll_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) int fd; int ret; - ret = OK; while ((option = getopt(argc, argv, ":h")) != ERROR) { switch (option) @@ -112,11 +111,6 @@ void i8sak_poll_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) } } - if (ret != OK) - { - i8sak_cmd_error(i8sak); - } - fd = open(i8sak->devname, O_RDWR); if (fd < 0) { diff --git a/wireless/ieee802154/i8sak/i8sak_scan.c b/wireless/ieee802154/i8sak/i8sak_scan.c new file mode 100644 index 000000000..3e22b2599 --- /dev/null +++ b/wireless/ieee802154/i8sak/i8sak_scan.c @@ -0,0 +1,253 @@ +/**************************************************************************** + * apps/wireless/ieee802154/i8sak/i8sak_scan.c + * IEEE 802.15.4 Swiss Army Knife + * + * Copyright (C) 2014-2015, 2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2017 Verge Inc. All rights reserved. + * + * Author: Anthony Merlino + * Author: Gregory Nuttx + * + * 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 "i8sak.h" + +#include +#include +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void scan_eventcb(FAR struct ieee802154_notif_s *notif, FAR void *arg); + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name : i8sak_scan_cmd + * + * Description : + * Request association with the Coordinator + ****************************************************************************/ + +void i8sak_scan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) +{ + struct ieee802154_scan_req_s scan; + struct wpanlistener_eventfilter_s filter; + int fd; + int option; + int argind; + int i; + int minchannel; + int maxchannel; + + scan.type = IEEE802154_SCANTYPE_PASSIVE; + + argind = 1; + while ((option = getopt(argc, argv, ":hpae")) != ERROR) + { + argind++; + switch (option) + { + case 'h': + fprintf(stderr, "Requests association with endpoint\n" + "Usage: %s [-h|p|a|e] minCh-maxCh\n" + " -h = this help menu\n" + " -p = passive scan (default)\n" + " -a = active scan\n" + " -e = energy scan\n" + , argv[0]); + + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + return; + + case 'p': + scan.type = IEEE802154_SCANTYPE_PASSIVE; + break; + + case 'a': + scan.type = IEEE802154_SCANTYPE_ACTIVE; + break; + + case 'e': + scan.type = IEEE802154_SCANTYPE_ED; + break; + + case ':': + fprintf(stderr, "ERROR: missing argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + + case '?': + fprintf(stderr, "ERROR: unknown argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + } + } + + /* There should always be one argument after the list option argument */ + + if (argc != argind + 1) + { + fprintf(stderr, "ERROR: invalid channel list\n"); + i8sak_cmd_error(i8sak); + } + + scan.duration = 5; + scan.chpage = i8sak->chpage; + + /* Parse channel list */ + + sscanf(argv[argind], "%d-%d", &minchannel, &maxchannel); + + scan.numchan = maxchannel - minchannel + 1; + + if (scan.numchan > 15) + { + fprintf(stderr, "ERROR: too many channels\n"); + i8sak_cmd_error(i8sak); + } + + for (i = 0; i < scan.numchan; i++) + { + scan.channels[i] = minchannel + i; + } + + fd = open(i8sak->devname, O_RDWR); + if (fd < 0) + { + printf("i8sak: cannot open %s, errno=%d\n", i8sak->devname, errno); + i8sak_cmd_error(i8sak); + } + + /* Register new callback for receiving the scan confirmation notification */ + + memset(&filter, 0, sizeof(struct wpanlistener_eventfilter_s)); + filter.confevents.scan = true; + + wpanlistener_add_eventreceiver(&i8sak->wpanlistener, scan_eventcb, &filter, + (FAR void *)i8sak, true); + + printf("i8sak: starting scan\n"); + + ieee802154_scan_req(fd, &scan); + + close(fd); +} + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void scan_eventcb(FAR struct ieee802154_notif_s *notif, FAR void *arg) +{ + FAR struct i8sak_s *i8sak = (FAR struct i8sak_s *)arg; + FAR struct ieee802154_scan_conf_s *scan = ¬if->u.scanconf; + int i; + + printf("\n\ni8sak: Scan complete: %s\n", + IEEE802154_STATUS_STRING[scan->status]); + + printf("Scan type: "); + + switch (scan->type) + { + case IEEE802154_SCANTYPE_ACTIVE: + printf("Active\n"); + break; + case IEEE802154_SCANTYPE_PASSIVE: + printf("Passive\n"); + break; + case IEEE802154_SCANTYPE_ED: + printf("Energy\n"); + break; + default: + printf("Unknown\n"); + break; + } + + /* Copy the results from the notification */ + + i8sak->npandesc = scan->numdesc; + memcpy(i8sak->pandescs, scan->pandescs, + sizeof(struct ieee802154_pandesc_s) * i8sak->npandesc); + + printf("Scan results: \n"); + + for (i = 0; i < scan->numdesc; i++) + { + printf("Result %d\n", i); + printf(" Channel: %u\n", scan->pandescs[i].chan); + printf(" PAN ID: %02X:%02X\n", + scan->pandescs[i].coordaddr.panid[0], + scan->pandescs[i].coordaddr.panid[1]); + + if (scan->pandescs[i].coordaddr.mode == IEEE802154_ADDRMODE_SHORT) + { + printf(" Coordinator saddr: %02X:%02X\n", + scan->pandescs[i].coordaddr.saddr[0], + scan->pandescs[i].coordaddr.saddr[1]); + } + else + { + printf(" Coordinator eaddr: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", + scan->pandescs[i].coordaddr.eaddr[0], + scan->pandescs[i].coordaddr.eaddr[1], + scan->pandescs[i].coordaddr.eaddr[2], + scan->pandescs[i].coordaddr.eaddr[3], + scan->pandescs[i].coordaddr.eaddr[4], + scan->pandescs[i].coordaddr.eaddr[5], + scan->pandescs[i].coordaddr.eaddr[6], + scan->pandescs[i].coordaddr.eaddr[7]); + } + } +} diff --git a/wireless/ieee802154/i8sak/i8sak_startpan.c b/wireless/ieee802154/i8sak/i8sak_startpan.c index 5a09a58d7..1f45a534b 100644 --- a/wireless/ieee802154/i8sak/i8sak_startpan.c +++ b/wireless/ieee802154/i8sak/i8sak_startpan.c @@ -189,7 +189,7 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) printf("i8sak: starting PAN\n"); IEEE802154_PANIDCOPY(startreq.panid, i8sak->addr.panid); - startreq.chnum = i8sak->chnum; + startreq.chan = i8sak->chan; startreq.chpage = i8sak->chpage; if (beaconenabled) diff --git a/wireless/ieee802154/i8sak/i8sak_tx.c b/wireless/ieee802154/i8sak/i8sak_tx.c index 56fc83454..00a67aabe 100644 --- a/wireless/ieee802154/i8sak/i8sak_tx.c +++ b/wireless/ieee802154/i8sak/i8sak_tx.c @@ -126,15 +126,11 @@ void i8sak_tx_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) case '?': fprintf(stderr, "ERROR: unknown argument\n"); ret = ERROR; - break; + optind = -1; + i8sak_cmd_error(i8sak); } } - if (ret != OK) - { - i8sak_cmd_error(i8sak); - } - if (argc == argind + 1) { i8sak->payload_len = i8sak_str2payload(argv[1], &i8sak->payload[0]); diff --git a/wireless/ieee802154/libmac/ieee802154_getchan.c b/wireless/ieee802154/libmac/ieee802154_getchan.c index c988e373b..132e03bb9 100644 --- a/wireless/ieee802154/libmac/ieee802154_getchan.c +++ b/wireless/ieee802154/libmac/ieee802154_getchan.c @@ -58,10 +58,10 @@ int ieee802154_getchan(int fd, FAR uint8_t *chan) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_PHY_CURRENT_CHANNEL; + req.attr = IEEE802154_ATTR_PHY_CHAN; ret = ieee802154_get_req(fd, &req); - *chan = req.attrval.phy.channel; + *chan = req.attrval.phy.chan; return ret; } diff --git a/wireless/ieee802154/libmac/ieee802154_geteaddr.c b/wireless/ieee802154/libmac/ieee802154_geteaddr.c index 773c7ffe8..0e7b26c41 100644 --- a/wireless/ieee802154/libmac/ieee802154_geteaddr.c +++ b/wireless/ieee802154/libmac/ieee802154_geteaddr.c @@ -58,7 +58,7 @@ int ieee802154_geteaddr(int fd, FAR uint8_t *eaddr) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_MAC_EXTENDED_ADDR; + req.attr = IEEE802154_ATTR_MAC_EADDR; ret = ieee802154_get_req(fd, &req); IEEE802154_EADDRCOPY(eaddr, req.attrval.mac.eaddr); diff --git a/wireless/ieee802154/libmac/ieee802154_getsaddr.c b/wireless/ieee802154/libmac/ieee802154_getsaddr.c index cd20cc126..850e82ead 100644 --- a/wireless/ieee802154/libmac/ieee802154_getsaddr.c +++ b/wireless/ieee802154/libmac/ieee802154_getsaddr.c @@ -58,7 +58,7 @@ int ieee802154_getsaddr(int fd, FAR uint8_t *saddr) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_MAC_SHORT_ADDRESS; + req.attr = IEEE802154_ATTR_MAC_SADDR; ret = ieee802154_get_req(fd, &req); IEEE802154_SADDRCOPY(saddr, req.attrval.mac.saddr); diff --git a/wireless/ieee802154/libmac/ieee802154_setchan.c b/wireless/ieee802154/libmac/ieee802154_setchan.c index bac4bc6f7..4f3488dc2 100644 --- a/wireless/ieee802154/libmac/ieee802154_setchan.c +++ b/wireless/ieee802154/libmac/ieee802154_setchan.c @@ -57,8 +57,8 @@ int ieee802154_setchan(int fd, uint8_t chan) { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_PHY_CURRENT_CHANNEL; - req.attrval.phy.channel = chan; + req.attr = IEEE802154_ATTR_PHY_CHAN; + req.attrval.phy.chan = chan; return ieee802154_set_req(fd, &req); } diff --git a/wireless/ieee802154/libmac/ieee802154_seteaddr.c b/wireless/ieee802154/libmac/ieee802154_seteaddr.c index 19494eafd..d7f22d97a 100644 --- a/wireless/ieee802154/libmac/ieee802154_seteaddr.c +++ b/wireless/ieee802154/libmac/ieee802154_seteaddr.c @@ -58,7 +58,7 @@ int ieee802154_seteaddr(int fd, FAR const uint8_t *eaddr) { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_MAC_EXTENDED_ADDR; + req.attr = IEEE802154_ATTR_MAC_EADDR; IEEE802154_EADDRCOPY(req.attrval.mac.eaddr, eaddr); return ieee802154_set_req(fd, &req); diff --git a/wireless/ieee802154/libmac/ieee802154_setsaddr.c b/wireless/ieee802154/libmac/ieee802154_setsaddr.c index 20f2fa544..31675cce8 100644 --- a/wireless/ieee802154/libmac/ieee802154_setsaddr.c +++ b/wireless/ieee802154/libmac/ieee802154_setsaddr.c @@ -58,7 +58,7 @@ int ieee802154_setsaddr(int fd, FAR const uint8_t *saddr) { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_MAC_SHORT_ADDRESS; + req.attr = IEEE802154_ATTR_MAC_SADDR; IEEE802154_SADDRCOPY(req.attrval.mac.saddr, saddr); return ieee802154_set_req(fd, &req); diff --git a/wireless/ieee802154/libmac/sixlowpan_getchan.c b/wireless/ieee802154/libmac/sixlowpan_getchan.c index f445d421e..fddc17f77 100644 --- a/wireless/ieee802154/libmac/sixlowpan_getchan.c +++ b/wireless/ieee802154/libmac/sixlowpan_getchan.c @@ -58,10 +58,10 @@ int sixlowpan_getchan(int sock, FAR const char *ifname, FAR uint8_t *chan) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_PHY_CURRENT_CHANNEL; + req.attr = IEEE802154_ATTR_PHY_CHAN; ret = sixlowpan_get_req(sock, ifname, &req); - *chan = req.attrval.phy.channel; + *chan = req.attrval.phy.chan; return ret; } diff --git a/wireless/ieee802154/libmac/sixlowpan_geteaddr.c b/wireless/ieee802154/libmac/sixlowpan_geteaddr.c index 0344d43c6..18a23d2df 100644 --- a/wireless/ieee802154/libmac/sixlowpan_geteaddr.c +++ b/wireless/ieee802154/libmac/sixlowpan_geteaddr.c @@ -58,7 +58,7 @@ int sixlowpan_geteaddr(int sock, FAR const char *ifname, FAR uint8_t *eaddr) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_MAC_EXTENDED_ADDR; + req.attr = IEEE802154_ATTR_MAC_EADDR; ret = sixlowpan_get_req(sock, ifname, &req); IEEE802154_EADDRCOPY(eaddr, req.attrval.mac.eaddr); diff --git a/wireless/ieee802154/libmac/sixlowpan_getsaddr.c b/wireless/ieee802154/libmac/sixlowpan_getsaddr.c index ef221190c..e721ca3bb 100644 --- a/wireless/ieee802154/libmac/sixlowpan_getsaddr.c +++ b/wireless/ieee802154/libmac/sixlowpan_getsaddr.c @@ -58,7 +58,7 @@ int sixlowpan_getsaddr(int sock, FAR const char *ifname, FAR uint8_t *saddr) struct ieee802154_get_req_s req; int ret; - req.attr = IEEE802154_ATTR_MAC_SHORT_ADDRESS; + req.attr = IEEE802154_ATTR_MAC_SADDR; ret = sixlowpan_get_req(sock, ifname, &req); IEEE802154_SADDRCOPY(saddr, req.attrval.mac.saddr); diff --git a/wireless/ieee802154/libmac/sixlowpan_setchan.c b/wireless/ieee802154/libmac/sixlowpan_setchan.c index c4e746b8c..04d44f382 100644 --- a/wireless/ieee802154/libmac/sixlowpan_setchan.c +++ b/wireless/ieee802154/libmac/sixlowpan_setchan.c @@ -57,8 +57,8 @@ int sixlowpan_setchan(int sock, FAR const char *ifname, uint8_t chan) { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_PHY_CURRENT_CHANNEL; - req.attrval.phy.channel = chan; + req.attr = IEEE802154_ATTR_PHY_CHAN; + req.attrval.phy.chan = chan; return sixlowpan_set_req(sock, ifname, &req); } diff --git a/wireless/ieee802154/libmac/sixlowpan_seteaddr.c b/wireless/ieee802154/libmac/sixlowpan_seteaddr.c index 9f3dcb46d..3aa114f95 100644 --- a/wireless/ieee802154/libmac/sixlowpan_seteaddr.c +++ b/wireless/ieee802154/libmac/sixlowpan_seteaddr.c @@ -57,7 +57,7 @@ int sixlowpan_seteaddr(int sock, FAR const char *ifname, FAR const uint8_t *eadd { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_MAC_EXTENDED_ADDR; + req.attr = IEEE802154_ATTR_MAC_EADDR; IEEE802154_EADDRCOPY(req.attrval.mac.eaddr, eaddr); return sixlowpan_set_req(sock, ifname, &req); diff --git a/wireless/ieee802154/libmac/sixlowpan_setsaddr.c b/wireless/ieee802154/libmac/sixlowpan_setsaddr.c index 49c46f28b..02260d73c 100644 --- a/wireless/ieee802154/libmac/sixlowpan_setsaddr.c +++ b/wireless/ieee802154/libmac/sixlowpan_setsaddr.c @@ -57,7 +57,7 @@ int sixlowpan_setsaddr(int sock, FAR const char *ifname, FAR const uint8_t *sadd { struct ieee802154_set_req_s req; - req.attr = IEEE802154_ATTR_MAC_SHORT_ADDRESS; + req.attr = IEEE802154_ATTR_MAC_SADDR; IEEE802154_SADDRCOPY(req.attrval.mac.saddr, saddr); return sixlowpan_set_req(sock, ifname, &req); From ace5bdc7c060d6817151417527677ebfc800979c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 10:54:32 -0600 Subject: [PATCH 15/23] examples/nettest: Add more printf output --- examples/nettest/nettest_client.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/nettest/nettest_client.c b/examples/nettest/nettest_client.c index 2d91877ec..c9059d36b 100644 --- a/examples/nettest/nettest_client.c +++ b/examples/nettest/nettest_client.c @@ -213,6 +213,7 @@ void nettest_client(void) goto errout_with_socket; } + printf("client: Terminating\n"); close(sockfd); free(outbuf); #ifndef CONFIG_EXAMPLES_NETTEST_PERFORMANCE From 3563a05fcd9e5cb3c6e7856463bf80fd3cb2946a Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 11:12:53 -0600 Subject: [PATCH 16/23] Remove dangling white space --- wireless/ieee802154/i8sak/i8sak_acceptassoc.c | 2 +- wireless/ieee802154/i8sak/i8sak_chan.c | 6 ++-- wireless/ieee802154/i8sak/i8sak_scan.c | 4 +-- wireless/ieee802154/i8sak/i8sak_tx.c | 4 +-- wireless/ieee802154/libmac/Makefile | 32 +++++++++---------- wireless/iwpan/src/Make.defs | 2 +- wireless/wapi/LICENSE | 8 ++--- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c index 6a896a76b..949ca0dc6 100644 --- a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c +++ b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c @@ -122,7 +122,7 @@ void i8sak_acceptassoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[] printf("cannot open %s, errno=%d\n", i8sak->devname, errno); i8sak_cmd_error(i8sak); } - + ieee802154_setassocpermit(fd, true); if (!optcnt) diff --git a/wireless/ieee802154/i8sak/i8sak_chan.c b/wireless/ieee802154/i8sak/i8sak_chan.c index 0c27999ef..f3f924a42 100644 --- a/wireless/ieee802154/i8sak/i8sak_chan.c +++ b/wireless/ieee802154/i8sak/i8sak_chan.c @@ -105,7 +105,7 @@ void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) i8sak_cmd_error(i8sak); /* This exits for us */ } } - + if (!getchan) { if (argc < argind + 1) @@ -113,7 +113,7 @@ void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) fprintf(stderr, "ERROR: missing channel\n"); i8sak_cmd_error(i8sak); /* This exits for us */ } - + channel = i8sak_str2luint8(argv[argind]); } @@ -124,7 +124,7 @@ void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) i8sak_cmd_error(i8sak); } - if (getchan) + if (getchan) { ieee802154_getchan(fd, &channel); printf("i8sak: Channel: %d\n", (int)channel); diff --git a/wireless/ieee802154/i8sak/i8sak_scan.c b/wireless/ieee802154/i8sak/i8sak_scan.c index 3e22b2599..afacc60a9 100644 --- a/wireless/ieee802154/i8sak/i8sak_scan.c +++ b/wireless/ieee802154/i8sak/i8sak_scan.c @@ -105,7 +105,7 @@ void i8sak_scan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) optind = -1; return; - + case 'p': scan.type = IEEE802154_SCANTYPE_PASSIVE; break; @@ -144,7 +144,7 @@ void i8sak_scan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) scan.duration = 5; scan.chpage = i8sak->chpage; - + /* Parse channel list */ sscanf(argv[argind], "%d-%d", &minchannel, &maxchannel); diff --git a/wireless/ieee802154/i8sak/i8sak_tx.c b/wireless/ieee802154/i8sak/i8sak_tx.c index 00a67aabe..d60668f86 100644 --- a/wireless/ieee802154/i8sak/i8sak_tx.c +++ b/wireless/ieee802154/i8sak/i8sak_tx.c @@ -109,7 +109,7 @@ void i8sak_tx_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) sendasdev = true; argind++; break; - + case 'm': sendmax = true; argind++; @@ -135,7 +135,7 @@ void i8sak_tx_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) { i8sak->payload_len = i8sak_str2payload(argv[1], &i8sak->payload[0]); } - + if (sendmax) { i8sak->payload_len = IEEE802154_MAX_SAFE_MAC_PAYLOAD_SIZE; diff --git a/wireless/ieee802154/libmac/Makefile b/wireless/ieee802154/libmac/Makefile index 6dbf07cf4..76c817573 100644 --- a/wireless/ieee802154/libmac/Makefile +++ b/wireless/ieee802154/libmac/Makefile @@ -47,16 +47,16 @@ CSRCS += ieee802154_resetreq.c ieee802154_rxenabreq.c ieee802154_scanreq.c CSRCS += ieee802154_setreq.c ieee802154_startreq.c ieee802154_syncreq.c CSRCS += ieee802154_pollreq.c ieee802154_enableevents.c # Add Get/Set Attribute helpers -CSRCS += ieee802154_setchan.c ieee802154_getchan.c -CSRCS += ieee802154_setpanid.c ieee802154_getpanid.c -CSRCS += ieee802154_setsaddr.c ieee802154_getsaddr.c -CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c -CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c -CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c -CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c +CSRCS += ieee802154_setchan.c ieee802154_getchan.c +CSRCS += ieee802154_setpanid.c ieee802154_getpanid.c +CSRCS += ieee802154_setsaddr.c ieee802154_getsaddr.c +CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c +CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c +CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c +CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c CSRCS += ieee802154_getdevmode.c ieee802154_setassocpermit.c -ifeq ($(CONFIG_NET_6LOWPAN),y) +ifeq ($(CONFIG_NET_6LOWPAN),y) # Add Get/Set Attribute helpers CSRCS += sixlowpan_assocreq.c sixlowpan_assocresp.c sixlowpan_disassocreq.c CSRCS += sixlowpan_getreq.c sixlowpan_gtsreq.c sixlowpan_orphanresp.c @@ -64,14 +64,14 @@ CSRCS += sixlowpan_resetreq.c sixlowpan_rxenabreq.c sixlowpan_scanreq.c CSRCS += sixlowpan_setreq.c sixlowpan_startreq.c sixlowpan_syncreq.c CSRCS += sixlowpan_pollreq.c # Add IOCTL helpers -CSRCS += sixlowpan_setchan.c sixlowpan_getchan.c -CSRCS += sixlowpan_setpanid.c sixlowpan_getpanid.c -CSRCS += sixlowpan_setsaddr.c sixlowpan_getsaddr.c -CSRCS += sixlowpan_seteaddr.c sixlowpan_geteaddr.c -CSRCS += sixlowpan_setpromisc.c sixlowpan_getpromisc.c -CSRCS += sixlowpan_setrxonidle.c sixlowpan_getrxonidle.c -CSRCS += sixlowpan_settxpwr.c sixlowpan_gettxpwr.c -endif +CSRCS += sixlowpan_setchan.c sixlowpan_getchan.c +CSRCS += sixlowpan_setpanid.c sixlowpan_getpanid.c +CSRCS += sixlowpan_setsaddr.c sixlowpan_getsaddr.c +CSRCS += sixlowpan_seteaddr.c sixlowpan_geteaddr.c +CSRCS += sixlowpan_setpromisc.c sixlowpan_getpromisc.c +CSRCS += sixlowpan_setrxonidle.c sixlowpan_getrxonidle.c +CSRCS += sixlowpan_settxpwr.c sixlowpan_gettxpwr.c +endif AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/wireless/iwpan/src/Make.defs b/wireless/iwpan/src/Make.defs index e7577e3ac..935fcb23d 100644 --- a/wireless/iwpan/src/Make.defs +++ b/wireless/iwpan/src/Make.defs @@ -36,7 +36,7 @@ ifeq ($(CONFIG_WIRELESS_IWPAN),y) -CSRCS = +CSRCS = MAINSRC = iwpan.c DEPPATH += --dep-path src diff --git a/wireless/wapi/LICENSE b/wireless/wapi/LICENSE index eeb3ddaae..cdade89e1 100644 --- a/wireless/wapi/LICENSE +++ b/wireless/wapi/LICENSE @@ -1,16 +1,16 @@ Copyright (c) 2010, Volkan YAZICI All rights reserved. - + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - + - 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. - + 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 From 82aad871220a36c01cfe4e54b040847f33f8804e Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 14:37:54 -0600 Subject: [PATCH 17/23] netutils/telnetc: This a port of libtelnet to NuttX. --- include/netutils/telnetc.h | 850 ++++++++++++ netutils/README.txt | 6 + netutils/telnetc/.gitignore | 6 + netutils/telnetc/telnetc.c | 2410 +++++++++++++++++++++++++++++++++++ 4 files changed, 3272 insertions(+) create mode 100644 include/netutils/telnetc.h create mode 100644 netutils/telnetc/.gitignore create mode 100644 netutils/telnetc/telnetc.c diff --git a/include/netutils/telnetc.h b/include/netutils/telnetc.h new file mode 100644 index 000000000..bf8a4e8a9 --- /dev/null +++ b/include/netutils/telnetc.h @@ -0,0 +1,850 @@ +/**************************************************************************** + * apps/netutils/telnetc/telnetc.c + * + * Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet. + * Modified and re-released under the BSD license: + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * The original authors of libtelnet are listed below. Per their licesne, + * "The author or authors of this code dedicate any and all copyright + * interest in this code to the public domain. We make this dedication for + * the benefit of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * code under copyright law." + * + * Author: Sean Middleditch + * (Also listed in the AUTHORS file are Jack Kelly + * and Katherine Flavel ) + * + * 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. + * + ****************************************************************************/ + +/* libtelnet - TELNET protocol handling library + * + * SUMMARY: + * + * libtelnet is a library for handling the TELNET protocol. It includes + * routines for parsing incoming data from a remote peer as well as formatting + * data to send to the remote peer. + * + * libtelnet uses a callback-oriented API, allowing application-specific + * handling of various events. The callback system is also used for buffering + * outgoing protocol data, allowing the application to maintain control over + * the actual socket connection. + * + * Features supported include the full TELNET protocol, Q-method option + * negotiation, ZMP, MCCP2, MSSP, and NEW-ENVIRON. + * + * CONFORMS TO: + * + * RFC854 - http://www.faqs.org/rfcs/rfc854.html + * RFC855 - http://www.faqs.org/rfcs/rfc855.html + * RFC1091 - http://www.faqs.org/rfcs/rfc1091.html + * RFC1143 - http://www.faqs.org/rfcs/rfc1143.html + * RFC1408 - http://www.faqs.org/rfcs/rfc1408.html + * RFC1572 - http://www.faqs.org/rfcs/rfc1572.html + * + * LICENSE: + * + * The author or authors of this code dedicate any and all copyright interest + * in this code to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and successors. We + * intend this dedication to be an overt act of relinquishment in perpetuity of + * all present and future rights to this code under copyright law. + * + * Author: Sean Middleditch + */ + +#ifndef __APPS_INCLUDE_NETUTILS_TELNETC_H +#define __APPS_INCLUDE_NETUTILS_TELNETC_H 1 + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/* C++ support */ + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/**************************************************************************** + * Pre-proecessor Definitions + ****************************************************************************/ + +/* Telnet commands and special values. */ + +#define TELNET_IAC 255 +#define TELNET_DONT 254 +#define TELNET_DO 253 +#define TELNET_WONT 252 +#define TELNET_WILL 251 +#define TELNET_SB 250 +#define TELNET_GA 249 +#define TELNET_EL 248 +#define TELNET_EC 247 +#define TELNET_AYT 246 +#define TELNET_AO 245 +#define TELNET_IP 244 +#define TELNET_BREAK 243 +#define TELNET_DM 242 +#define TELNET_NOP 241 +#define TELNET_SE 240 +#define TELNET_EOR 239 +#define TELNET_ABORT 238 +#define TELNET_SUSP 237 +#define TELNET_EOF 236 + +/* Telnet options. */ + +#define TELNET_TELOPT_BINARY 0 +#define TELNET_TELOPT_ECHO 1 +#define TELNET_TELOPT_RCP 2 +#define TELNET_TELOPT_SGA 3 +#define TELNET_TELOPT_NAMS 4 +#define TELNET_TELOPT_STATUS 5 +#define TELNET_TELOPT_TM 6 +#define TELNET_TELOPT_RCTE 7 +#define TELNET_TELOPT_NAOL 8 +#define TELNET_TELOPT_NAOP 9 +#define TELNET_TELOPT_NAOCRD 10 +#define TELNET_TELOPT_NAOHTS 11 +#define TELNET_TELOPT_NAOHTD 12 +#define TELNET_TELOPT_NAOFFD 13 +#define TELNET_TELOPT_NAOVTS 14 +#define TELNET_TELOPT_NAOVTD 15 +#define TELNET_TELOPT_NAOLFD 16 +#define TELNET_TELOPT_XASCII 17 +#define TELNET_TELOPT_LOGOUT 18 +#define TELNET_TELOPT_BM 19 +#define TELNET_TELOPT_DET 20 +#define TELNET_TELOPT_SUPDUP 21 +#define TELNET_TELOPT_SUPDUPOUTPUT 22 +#define TELNET_TELOPT_SNDLOC 23 +#define TELNET_TELOPT_TTYPE 24 +#define TELNET_TELOPT_EOR 25 +#define TELNET_TELOPT_TUID 26 +#define TELNET_TELOPT_OUTMRK 27 +#define TELNET_TELOPT_TTYLOC 28 +#define TELNET_TELOPT_3270REGIME 29 +#define TELNET_TELOPT_X3PAD 30 +#define TELNET_TELOPT_NAWS 31 +#define TELNET_TELOPT_TSPEED 32 +#define TELNET_TELOPT_LFLOW 33 +#define TELNET_TELOPT_LINEMODE 34 +#define TELNET_TELOPT_XDISPLOC 35 +#define TELNET_TELOPT_ENVIRON 36 +#define TELNET_TELOPT_AUTHENTICATION 37 +#define TELNET_TELOPT_ENCRYPT 38 +#define TELNET_TELOPT_NEW_ENVIRON 39 +#define TELNET_TELOPT_MSSP 70 +#define TELNET_TELOPT_COMPRESS 85 +#define TELNET_TELOPT_COMPRESS2 86 +#define TELNET_TELOPT_ZMP 93 +#define TELNET_TELOPT_EXOPL 255 + +#define TELNET_TELOPT_MCCP2 86 + +/* TERMINAL-TYPE codes. */ + +#define TELNET_TTYPE_IS 0 +#define TELNET_TTYPE_SEND 1 + +/* NEW-ENVIRON/ENVIRON codes. */ + +#define TELNET_ENVIRON_IS 0 +#define TELNET_ENVIRON_SEND 1 +#define TELNET_ENVIRON_INFO 2 +#define TELNET_ENVIRON_VAR 0 +#define TELNET_ENVIRON_VALUE 1 +#define TELNET_ENVIRON_ESC 2 +#define TELNET_ENVIRON_USERVAR 3 + +/* MSSP codes. */ + +#define TELNET_MSSP_VAR 1 +#define TELNET_MSSP_VAL 2 + +/* Telnet state tracker flags. */ + +#define TELNET_FLAG_PROXY (1 << 0) +#define TELNET_PFLAG_DEFLATE (1 << 7) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* Error codes */ + +enum telnet_error_u +{ + TELNET_EOK = 0, /* No error */ + TELNET_EBADVAL, /* Invalid parameter, or API misuse */ + TELNET_ENOMEM, /* Memory allocation failure */ + TELNET_EOVERFLOW, /* Data exceeds buffer size */ + TELNET_EPROTOCOL, /* Invalid sequence of special bytes */ + TELNET_ECOMPRESS /* Error handling compressed streams */ +}; + +/* Event codes */ + +enum telnet_event_type_e +{ + TELNET_EV_DATA = 0, /* Raw text data has been received */ + TELNET_EV_SEND, /* Data needs to be sent to the peer */ + TELNET_EV_IAC, /* Generic IAC code received */ + TELNET_EV_WILL, /* WILL option negotiation received */ + TELNET_EV_WONT, /* WONT option neogitation received */ + TELNET_EV_DO, /* DO option negotiation received */ + TELNET_EV_DONT, /* DONT option negotiation received */ + TELNET_EV_SUBNEGOTIATION, /* Sub-negotiation data received */ + TELNET_EV_COMPRESS, /* Compression has been enabled */ + TELNET_EV_ZMP, /* ZMP command has been received */ + TELNET_EV_TTYPE, /* TTYPE command has been received */ + TELNET_EV_ENVIRON, /* ENVIRON command has been received */ + TELNET_EV_MSSP, /* MSSP command has been received */ + TELNET_EV_WARNING, /* Recoverable error has occured */ + TELNET_EV_ERROR /* Non-recoverable error has occured */ +}; + +/* Environ/MSSP command information */ + +struct telnet_environ_s +{ + unsigned char type; /* either TELNET_ENVIRON_VAR or + * TELNET_ENVIRON_USERVAR */ + char *var; /* Name of the variable being set */ + char *value; /* value of variable being set; empty string + * if no value */ +}; + +/* State tracker -- private data structure */ + +struct telnet_s; /* Forward reference */ + +/* Event information */ + +union telnet_event_u +{ + /* Event type The type field will determine which of the other + * event structure fields have been filled in. For instance, if the + * event type is TELNET_EV_ZMP, then the zmp event field (and ONLY the + * zmp event field) will be filled in. + */ + + enum telnet_event_type_e type; + + /* Data event: for DATA and SEND events */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const char *buffer; /* Byte buffer */ + size_t size; /* Number of bytes in buffer */ + } data; + + /* WARNING and ERROR events */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const char *file; /* File the error occured in */ + const char *func; /* Function the error occured in */ + const char *msg; /* Error message string */ + int line; /* Line of file error occured on */ + enum telnet_error_u errcode; /* Error code */ + } error; + + /* Command event: for IAC */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + unsigned char cmd; /* Telnet command received */ + } iac; + + /* Negotiation event: WILL, WONT, DO, DONT */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + unsigned char telopt; /* Option being negotiated */ + } neg; + + /* Subnegotiation event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const char *buffer; /* Data of sub-negotiation */ + size_t size; /* Number of bytes in buffer */ + unsigned char telopt; /* Option code for negotiation */ + } sub; + + /* ZMP event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const char **argv; /* Array of argument string */ + size_t argc; /* Number of elements in argv */ + } zmp; + + /* TTYPE event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + unsigned char cmd; /* TELNET_TTYPE_IS or TELNET_TTYPE_SEND */ + const char *name; /* Terminal type name (IS only) */ + } ttype; + + /* COMPRESS event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + unsigned char state; /* 1 if compression is enabled, 0 if + * disabled */ + } compress; + + /* ENVIRON/NEW-ENVIRON event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const struct telnet_environ_s *values; /* Array of variable values */ + size_t size; /* Number of elements in values */ + unsigned char cmd; /* SEND, IS, or INFO */ + } envevent; + + /* MSSP event */ + + struct + { + enum telnet_event_type_e _type; /* Alias for type */ + const struct telnet_environ_s *values; /* Array of variable values */ + size_t size; /* Number of elements in values */ + } mssp; +}; + +/* Name: telnet_event_handler_t + * + * Description: + * This is the type of function that must be passed to + * telnet_init() when creating a new telnet object. The + * function will be invoked once for every event generated + * by the libtelnet protocol parser. + * + * Input Parameters: + * telnet The telnet object that generated the event + * event Event structure with details about the event + * user_data User-supplied pointer + */ + +typedef void (*telnet_event_handler_t)(struct telnet_s *telnet, + union telnet_event_u *event, + void *user_data); + +/* telopt support table element; use telopt of -1 for end marker */ + +struct telnet_telopt_s +{ + short telopt; /* One of the TELOPT codes or -1 */ + unsigned char us; /* TELNET_WILL or TELNET_WONT */ + unsigned char him; /* TELNET_DO or TELNET_DONT */ +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: telnet_init + * + * Description: + * Initialize a telnet state tracker. + * + * This function initializes a new state tracker, which is used for all + * other libtelnet functions. Each connection must have its own + * telnet state tracker object. + * + * Input Parameters: + * telopts Table of TELNET options the application supports. + * eh Event handler function called for every event. + * flags 0 or TELNET_FLAG_PROXY. + * user_data Optional data pointer that will be passsed to eh. + * + * Returned Value: + * Telent state tracker object. + * + ****************************************************************************/ + +struct telnet_s *telnet_init(const struct telnet_telopt_s *telopts, + telnet_event_handler_t eh, unsigned char flags, + void *user_data); + +/**************************************************************************** + * Name: telnet_free + * + * Description: + * Free up any memory allocated by a state tracker. + * + * This function must be called when a telnet state tracker is no + * longer needed (such as after the connection has been closed) to + * release any memory resources used by the state tracker. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_free(struct telnet_s *telnet); + +/**************************************************************************** + * Name: telnet_recv + * + * Description: + * Push a byte buffer into the state tracker. + * + * Passes one or more bytes to the telnet state tracker for + * protocol parsing. The byte buffer is most often going to be + * the buffer that recv() was called for while handling the + * connection. + * + * Input Parameters: + * telnet Telnet state tracker object. + * buffer Pointer to byte buffer. + * size Number of bytes pointed to by buffer. + * + ****************************************************************************/ + +void telnet_recv(struct telnet_s *telnet, const char *buffer, size_t size); + +/**************************************************************************** + * Name: telnet_iac + * + * Description: + * Send a telnet command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd Command to send. + * + ****************************************************************************/ + +void telnet_iac(struct telnet_s *telnet, unsigned char cmd); + +/**************************************************************************** + * Name: telnet_negotiate + * + * Description: + * Send negotiation command. + * + * Internally, libtelnet uses RFC1143 option negotiation rules. + * The negotiation commands sent with this function may be ignored + * if they are determined to be redundant. + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd TELNET_WILL, TELNET_WONT, TELNET_DO, or TELNET_DONT. + * opt One of the TELNET_TELOPT_* values. + * + ****************************************************************************/ + +void telnet_negotiate(struct telnet_s *telnet, unsigned char cmd, + unsigned char opt); + +/**************************************************************************** + * Name: telnet_send + * + * Description: + * Send non-command data (escapes IAC bytes). + * + * Input Parameters: + * telnet Telnet state tracker object. + * buffer Buffer of bytes to send. + * size Number of bytes to send. + * + ****************************************************************************/ + +void telnet_send(struct telnet_s *telnet, const char *buffer, size_t size); + +/**************************************************************************** + * Name: telnet_begin_sb + * + * Description: + * Begin a sub-negotiation command. + * + * Sends IAC SB followed by the telopt code. All following data sent + * will be part of the sub-negotiation, until telnet_finish_sb() is + * called. + * + * Input Parameters: + * telnet Telnet state tracker object. + * telopt One of the TELNET_TELOPT_* values. + * + ****************************************************************************/ + +void telnet_begin_sb(struct telnet_s *telnet, unsigned char telopt); + +/**************************************************************************** + * Name: telnet_finish_sb + * + * Description: + * Finish a sub-negotiation command. + * + * This must be called after a call to telnet_begin_sb() to finish a + * sub-negotiation command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE) + +/**************************************************************************** + * Name: telnet_subnegotiation + * + * Description: + * Send a complete subnegotiation buffer. + * + * Equivalent to: + * telnet_begin_sb(telnet, telopt); + * telnet_send(telnet, buffer, size); + * telnet_finish_sb(telnet); + * + * Input Parameters: + * telnet Telnet state tracker format. + * telopt One of the TELNET_TELOPT_* values. + * buffer Byte buffer for sub-negotiation data. + * size Number of bytes to use for sub-negotiation data. + * + ****************************************************************************/ + +void telnet_subnegotiation(struct telnet_s *telnet, unsigned char telopt, + const char *buffer, size_t size); + +/**************************************************************************** + * Name: telnet_begin_compress2 + * + * Description: + * Begin sending compressed data. + * + * This function will begein sending data using the COMPRESS2 option, + * which enables the use of zlib to compress data sent to the client. + * The client must offer support for COMPRESS2 with option negotiation, + * and zlib support must be compiled into libtelnet. + * + * Only the server may call this command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_begin_compress2(struct telnet_s *telnet); + +/**************************************************************************** + * Name: telnet_printf + * + * Description: + * Send formatted data. + * + * This function is a wrapper around telnet_send(). It allows using + * printf-style formatting. + * + * Additionally, this function will translate \\r to the CR NUL construct + * and \\n with CR LF, as well as automatically escaping IAC bytes like + * telnet_send(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * fmt Format string. + * + * Returned Value: + * Number of bytes sent. + * + ****************************************************************************/ + +int telnet_printf(struct telnet_s *telnet, const char *fmt, ...); + +/**************************************************************************** + * Name: telnet_vprintf + * + * Description: + * Send formatted data with \r and \n translation in addition to IAC IAC + * + * See telnet_printf(). + * + ****************************************************************************/ + +int telnet_vprintf(struct telnet_s *telnet, const char *fmt, va_list va); + +/**************************************************************************** + * Name: telnet_raw_printf + * + * Description: + * Send formatted data (no newline escaping). + * + * This behaves identically to telnet_printf(), except that the \\r and \\n + * characters are not translated. The IAC byte is still escaped as normal + * with telnet_send(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * fmt Format string. + * + * Returned Value: + * Number of bytes sent. + * + ****************************************************************************/ + +int telnet_raw_printf(struct telnet_s *telnet, const char *fmt, ...); + +/**************************************************************************** + * Name: telnet_raw_vprintf + * + * Description: + * Send formatted data (no newline escaping). + * + * See telnet_raw_printf(). + * + ****************************************************************************/ + +int telnet_raw_vprintf(struct telnet_s *telnet, const char *fmt, va_list va); + +/**************************************************************************** + * Name: telnet_begin_newenviron + * + * Description: + * Begin a new set of NEW-ENVIRON values to request or send. + * + * This function will begin the sub-negotiation block for sending or + * requesting NEW-ENVIRON values. + * + * The telnet_finish_newenviron() macro must be called after this + * function to terminate the NEW-ENVIRON command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * type One of TELNET_ENVIRON_SEND, TELNET_ENVIRON_IS, or + * TELNET_ENVIRON_INFO. + * + ****************************************************************************/ + +void telnet_begin_newenviron(struct telnet_s *telnet, unsigned char type); + +/**************************************************************************** + * Name: telnet_newenviron_value + * + * Description: + * Send a NEW-ENVIRON variable name or value. + * + * This can only be called between calls to telnet_begin_newenviron() and + * telnet_finish_newenviron(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * type One of TELNET_ENVIRON_VAR, TELNET_ENVIRON_USERVAR, or + * TELNET_ENVIRON_VALUE. + * string Variable name or value. + * + ****************************************************************************/ + +void telnet_newenviron_value(struct telnet_s *telnet, unsigned char type, + const char *string); + +/**************************************************************************** + * Name: telnet_finish_newenviron + * + * Description: + * Finish a NEW-ENVIRON command. + * + * This must be called after a call to telnet_begin_newenviron() to finish a + * NEW-ENVIRON variable list. + * + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +#define telnet_finish_newenviron(telnet) telnet_finish_sb((telnet)) + +/**************************************************************************** + * Name: telnet_ttype_send + * + * Description: + * Send the TERMINAL-TYPE SEND command. + * + * Sends the sequence IAC TERMINAL-TYPE SEND. + * + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_ttype_send(struct telnet_s * telnet); + +/**************************************************************************** + * Name: telnet_ttype_is + * + * Description: + * Send the TERMINAL-TYPE IS command. + * + * Sends the sequence IAC TERMINAL-TYPE IS "string". + * + * According to the RFC, the recipient of a TERMINAL-TYPE SEND shall + * send the next possible terminal-type the client supports. Upon sending + * the type, the client should switch modes to begin acting as the terminal + * type is just sent. + * + * The server may continue sending TERMINAL-TYPE IS until it receives a + * terminal type is understands. To indicate to the server that it has + * reached the end of the available optoins, the client must send the last + * terminal type a second time. When the server receives the same terminal + * type twice in a row, it knows it has seen all available terminal types. + * + * After the last terminal type is sent, if the client receives another + * TERMINAL-TYPE SEND command, it must begin enumerating the available + * terminal types from the very beginning. This allows the server to + * scan the available types for a preferred terminal type and, if none + * is found, to then ask the client to switch to an acceptable + * alternative. + * + * Note that if the client only supports a single terminal type, then + * simply sending that one type in response to every SEND will satisfy + * the behavior requirements. + * + * Input Parameters: + * telnet Telnet state tracker object. + * ttype Name of the terminal-type being sent. + * + ****************************************************************************/ + +void telnet_ttype_is(struct telnet_s * telnet, const char *ttype); + +/**************************************************************************** + * Name: telnet_send_zmp + * + * Description: + * Send a ZMP command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * argc Number of ZMP commands being sent. + * argv Array of argument strings. + * + ****************************************************************************/ + +void telnet_send_zmp(struct telnet_s * telnet, size_t argc, + const char **argv); + +/**************************************************************************** + * Name: telnet_send_zmpv + * + * Description: + * Send a ZMP command. + * + * Arguments are listed out in var-args style. After the last argument, a + * NULL pointer must be passed in as a sentinel value. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_send_zmpv(struct telnet_s * telnet, ...); + +/**************************************************************************** + * Name: telnet_send_vzmpv + * + * Description: + * Send a ZMP command. + * + * See telnet_send_zmpv(). + * + ****************************************************************************/ + +void telnet_send_vzmpv(struct telnet_s * telnet, va_list va); + +/**************************************************************************** + * Name: telnet_begin_zmp + * + * Description: + * Begin sending a ZMP command + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd The first argument (command name) for the ZMP command. + * + ****************************************************************************/ + +void telnet_begin_zmp(struct telnet_s * telnet, const char *cmd); + +/**************************************************************************** + * Name: telnet_zmp_arg + * + * Description: + * Send a ZMP command argument. + * + * Input Parameters: + * telnet Telnet state tracker object. + * arg Telnet argument string. + * + ****************************************************************************/ + +void telnet_zmp_arg(struct telnet_s * telnet, const char *arg); + +/**************************************************************************** + * Name: telnet_finish_zmp + * + * Description: + * Finish a ZMP command. + * + * This must be called after a call to telnet_begin_zmp() to finish a + * ZMP argument list. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +#define telnet_finish_zmp(telnet) telnet_finish_sb((telnet)) + +#if defined(__cplusplus) +} +#endif + +#endif /* __APPS_INCLUDE_NETUTILS_TELNETC_H */ diff --git a/netutils/README.txt b/netutils/README.txt index c2f3c4391..d0822e3bf 100644 --- a/netutils/README.txt +++ b/netutils/README.txt @@ -55,6 +55,12 @@ highly influenced by uIP) include: and adapted for NuttX by Darcy Gong. tftpc - TFTP client. See apps/include/netutils/tftp.h for interface information. + telnetc - This is a port of libtelnet to NuttX. This is a public domain + Telnet client library available from + https://github.com/seanmiddleditch/libtelnet modified for use + with NuttX. Original Authors: Sean Middleditch , + Jack Kelly , and Katherine Flavel + telnetd - TELNET server. This is the Telnet logic adapted from uIP and generalized for use as the front end to any shell. The telnet daemon creates sessions that are diff --git a/netutils/telnetc/.gitignore b/netutils/telnetc/.gitignore new file mode 100644 index 000000000..f21c854d1 --- /dev/null +++ b/netutils/telnetc/.gitignore @@ -0,0 +1,6 @@ +/.built +/.depend +/Make.dep +/*.src +/*.obj +/*.lst diff --git a/netutils/telnetc/telnetc.c b/netutils/telnetc/telnetc.c new file mode 100644 index 000000000..a5ee4e38e --- /dev/null +++ b/netutils/telnetc/telnetc.c @@ -0,0 +1,2410 @@ +/**************************************************************************** + * apps/netutils/telnetc/telnetc.c + * + * Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet. + * Modified and re-released under the BSD license: + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * The original authors of libtelnet are listed below. Per their licesne, + * "The author or authors of this code dedicate any and all copyright + * interest in this code to the public domain. We make this dedication for + * the benefit of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * code under copyright law." + * + * Author: Sean Middleditch + * (Also listed in the AUTHORS file are Jack Kelly + * and Katherine Flavel ) + * + * 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 + +#if defined(HAVE_ZLIB) +# include +#endif + +#include "netutils/telnetc.h" + +/**************************************************************************** + * Pre-proecessor Definitions + ****************************************************************************/ + +/* Helper for Q-method option tracking */ + +#define Q_US(q) ( (q).state & 0x0F) +#define Q_HIM(q) (((q).state & 0xF0) >> 4) +#define Q_MAKE(us,him) ((us) | ((him) << 4)) + +/* Helper for the negotiation routines */ + +#define NEGOTIATE_EVENT(telnet,cmd,opt) \ + ev.type = (cmd); \ + ev.neg.telopt = (opt); \ + (telnet)->eh((telnet), &ev, (telnet)->ud); + +/* RFC1143 state names */ + +#define Q_NO 0 +#define Q_YES 1 +#define Q_WANTNO 2 +#define Q_WANTYES 3 +#define Q_WANTNO_OP 4 +#define Q_WANTYES_OP 5 + +/* To send bags of unsigned chars */ + +#define _sendu(t, d, s) _send((t), (const char*)(d), (s)) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* Telnet state codes */ + +enum telnet_state_e +{ + TELNET_STATE_DATA = 0, + TELNET_STATE_IAC, + TELNET_STATE_WILL, + TELNET_STATE_WONT, + TELNET_STATE_DO, + TELNET_STATE_DONT, + TELNET_STATE_SB, + TELNET_STATE_SB_DATA, + TELNET_STATE_SB_DATA_IAC +}; + +/* Telnet state tracker */ + +struct telnet_s +{ + /* User data */ + + void *ud; + + /* Telopt support table */ + + const struct telnet_telopt_s *telopts; + + /* Event handler */ + + telnet_event_handler_t eh; + +#if defined(HAVE_ZLIB) + /* zlib (mccp2) compression */ + + z_stream *z; +#endif + + /* RFC1143 option negotiation states */ + + struct telnet_rfc1143_s *q; + + /* Sub-request buffer */ + + char *buffer; + + /* Current size of the buffer */ + + size_t buffer_size; + + /* Current buffer write position (also length of buffer data) */ + + size_t buffer_pos; + + /* Current state */ + + enum telnet_state_e state; + + /* Option flags */ + + unsigned char flags; + + /* Current subnegotiation telopt */ + + unsigned char sb_telopt; + + /* Length of RFC1143 queue */ + + unsigned char q_size; +}; + +/* RFC1143 option negotiation state */ + +struct telnet_rfc1143_s +{ + unsigned char telopt; + unsigned char state; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Buffer sizes */ + +static const size_t _buffer_sizes[] = +{ + 0, 512, 2048, 8192, 16384, +}; + +static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) / + sizeof(_buffer_sizes[0]); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/* Error generation function */ + +static enum telnet_error_u _error(struct telnet_s *telnet, unsigned line, + const char *func, enum telnet_error_u err, + int fatal, const char *fmt, ...) +{ + union telnet_event_u ev; + char buffer[512]; + va_list va; + + /* Format informational text */ + + va_start(va, fmt); + vsnprintf(buffer, sizeof(buffer), fmt, va); + va_end(va); + + /* Send error event to the user */ + + ev.type = fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING; + ev.error.file = __FILE__; + ev.error.func = func; + ev.error.line = line; + ev.error.msg = buffer; + telnet->eh(telnet, &ev, telnet->ud); + + return err; +} + +/* Initialize the zlib box for a telnet box; if deflate is non-zero, it + * initializes zlib for delating (compression), otherwise for inflating + * (decompression). returns TELNET_EOK on success, something else on + * failure. + */ + +#if defined(HAVE_ZLIB) +enum telnet_error_u _init_zlib(struct telnet_s *telnet, int deflate, + int err_fatal) +{ + z_stream *z; + int rs; + + /* If compression is already enabled, fail loudly */ + + if (telnet->z != 0) + { + return _error(telnet, __LINE__, __func__, TELNET_EBADVAL, + err_fatal, "cannot initialize compression twice"); + } + + /* Allocate zstream box */ + + if ((z = (z_stream *) calloc(1, sizeof(z_stream))) == 0) + { + return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal, + "malloc() failed: %s", strerror(errno)); + } + + /* Initialize */ + + if (deflate) + { + if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) + { + free(z); + return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, + err_fatal, "deflateInit() failed: %s", zError(rs)); + } + + telnet->flags |= TELNET_PFLAG_DEFLATE; + } + else + { + if ((rs = inflateInit(z)) != Z_OK) + { + free(z); + return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, + err_fatal, "inflateInit() failed: %s", zError(rs)); + } + + telnet->flags &= ~TELNET_PFLAG_DEFLATE; + } + + telnet->z = z; + return TELNET_EOK; +} +#endif /* HAVE_ZLIB */ + +/* Push bytes out, compressing them first if need be */ + +static void _send(struct telnet_s *telnet, const char *buffer, size_t size) +{ + union telnet_event_u ev; + +#if defined(HAVE_ZLIB) + /* If we have a deflate (compression) zlib box, use it */ + + if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) + { + char deflate_buffer[1024]; + int rs; + + /* Initialize z state */ + + telnet->z->next_in = (unsigned char *)buffer; + telnet->z->avail_in = (unsigned int)size; + telnet->z->next_out = (unsigned char *)deflate_buffer; + telnet->z->avail_out = sizeof(deflate_buffer); + + /* Deflate until buffer exhausted and all output is produced */ + + while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) + { + /* Compress */ + + if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) + { + _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, + "deflate() failed: %s", zError(rs)); + deflateEnd(telnet->z); + free(telnet->z); + telnet->z = 0; + break; + } + + /* Send event */ + + ev.type = TELNET_EV_SEND; + ev.data.buffer = deflate_buffer; + ev.data.size = sizeof(deflate_buffer) - telnet->z->avail_out; + telnet->eh(telnet, &ev, telnet->ud); + + /* Prepare output buffer for next run */ + + telnet->z->next_out = (unsigned char *)deflate_buffer; + telnet->z->avail_out = sizeof(deflate_buffer); + } + + /* Do not continue with remaining code */ + + return; + } +#endif /* HAVE_ZLIB */ + + ev.type = TELNET_EV_SEND; + ev.data.buffer = buffer; + ev.data.size = size; + telnet->eh(telnet, &ev, telnet->ud); +} + +/* Check if we support a particular telopt; if us is non-zero, we + * check if we (local) supports it, otherwise we check if he (remote) + * supports it. return non-zero if supported, zero if not supported. + */ + +static inline int _check_telopt(struct telnet_s *telnet, + unsigned char telopt, int us) +{ + int i; + + /* If we have no telopts table, we obviously don't support it */ + + if (telnet->telopts == 0) + { + return 0; + } + + /* Loop unti found or end marker (us and him both 0) */ + + for (i = 0; telnet->telopts[i].telopt != -1; ++i) + { + if (telnet->telopts[i].telopt == telopt) + { + if (us && telnet->telopts[i].us == TELNET_WILL) + { + return 1; + } + + else if (!us && telnet->telopts[i].him == TELNET_DO) + { + return 1; + } + else + { + return 0; + } + } + } + + /* Not found, so not supported */ + + return 0; +} + +/* Retrieve RFC1143 option state */ + +static inline struct telnet_rfc1143_s _get_rfc1143(struct telnet_s *telnet, + unsigned char telopt) +{ + struct telnet_rfc1143_s empty; + int i; + + /* Search for entry */ + + for (i = 0; i != telnet->q_size; ++i) + { + if (telnet->q[i].telopt == telopt) + { + return telnet->q[i]; + } + } + + /* Not found, return empty value */ + + empty.telopt = telopt; + empty.state = 0; + return empty; +} + +/* Save RFC1143 option state */ + +static inline void _set_rfc1143(struct telnet_s *telnet, unsigned char telopt, + char us, char him) +{ + struct telnet_rfc1143_s *qtmp; + int i; + + /* Search for entry */ + + for (i = 0; i != telnet->q_size; ++i) + { + if (telnet->q[i].telopt == telopt) + { + telnet->q[i].state = Q_MAKE(us, him); + return; + } + } + + /* we're going to need to track state for it, so grow the queue by 4 (four) + * elements and put the telopt into it; bail on allocation error. we go by + * four because it seems like a reasonable guess as to the number of enabled + * options for most simple code, and it allows for an acceptable number of + * reallocations for complex code. + */ + + qtmp = (struct telnet_rfc1143_s *) + realloc(telnet->q, + sizeof(struct telnet_rfc1143_s) * (telnet->q_size + 4)); + if (qtmp == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "realloc() failed: %s", strerror(errno)); + return; + } + + memset(&qtmp[telnet->q_size], 0, sizeof(struct telnet_rfc1143_s) * 4); + telnet->q = qtmp; + telnet->q[telnet->q_size].telopt = telopt; + telnet->q[telnet->q_size].state = Q_MAKE(us, him); + telnet->q_size += 4; +} + +/* Send negotiation bytes */ + +static inline void _send_negotiate(struct telnet_s *telnet, unsigned char cmd, + unsigned char telopt) +{ + unsigned char bytes[3]; + bytes[0] = TELNET_IAC; + bytes[1] = cmd; + bytes[2] = telopt; + _sendu(telnet, bytes, 3); +} + +/* Negotiation handling magic for RFC1143 */ + +static void _negotiate(struct telnet_s *telnet, unsigned char telopt) +{ + union telnet_event_u ev; + struct telnet_rfc1143_s q; + + /* In PROXY mode, just pass it thru and do nothing */ + + if (telnet->flags & TELNET_FLAG_PROXY) + { + switch ((int)telnet->state) + { + case TELNET_STATE_WILL: + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + break; + + case TELNET_STATE_WONT: + NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt); + break; + + case TELNET_STATE_DO: + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + break; + + case TELNET_STATE_DONT: + NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt); + break; + } + + return; + } + + /* Lookup the current state of the option */ + + q = _get_rfc1143(telnet, telopt); + + /* Start processing... */ + + switch ((int)telnet->state) + { + /* Request to enable option on remote end or confirm DO */ + + case TELNET_STATE_WILL: + switch (Q_HIM(q)) + { + case Q_NO: + if (_check_telopt(telnet, telopt, 0)) + { + _set_rfc1143(telnet, telopt, Q_US(q), Q_YES); + _send_negotiate(telnet, TELNET_DO, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + } + else + _send_negotiate(telnet, TELNET_DONT, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_US(q), Q_NO); + NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt); + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "DONT answered by WILL"); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_YES); + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "DONT answered by WILL"); + break; + + case Q_WANTYES: + _set_rfc1143(telnet, telopt, Q_US(q), Q_YES); + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + break; + + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO); + _send_negotiate(telnet, TELNET_DONT, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + break; + } + break; + + /* Request to disable option on remote end, confirm DONT, reject DO */ + + case TELNET_STATE_WONT: + switch (Q_HIM(q)) + { + case Q_YES: + _set_rfc1143(telnet, telopt, Q_US(q), Q_NO); + _send_negotiate(telnet, TELNET_DONT, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_US(q), Q_NO); + NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES); + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + break; + + case Q_WANTYES: + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_NO); + break; + } + break; + + /* Request to enable option on local end or confirm WILL */ + + case TELNET_STATE_DO: + switch (Q_US(q)) + { + case Q_NO: + if (_check_telopt(telnet, telopt, 1)) + { + _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WILL, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + } + else + _send_negotiate(telnet, TELNET_WONT, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q)); + NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt); + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "WONT answered by DO"); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q)); + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "WONT answered by DO"); + break; + + case Q_WANTYES: + _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q)); + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + break; + + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WONT, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt); + break; + } + break; + + /* Request to disable option on local end, confirm WONT, reject WILL */ + + case TELNET_STATE_DONT: + switch (Q_US(q)) + { + case Q_YES: + _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WONT, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q)); + NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WILL, telopt); + NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt); + break; + + case Q_WANTYES: + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q)); + break; + } + break; + } +} + +/* Process an ENVIRON/NEW-ENVIRON subnegotiation buffer + * + * the algorithm and approach used here is kind of a hack, + * but it reduces the number of memory allocations we have + * to make. + * + * we copy the bytes back into the buffer, starting at the very + * beginning, which makes it easy to handle the ENVIRON ESC + * escape mechanism as well as ensure the variable name and + * value strings are NUL-terminated, all while fitting inside + * of the original buffer. + */ + +static int _environ_telnet(struct telnet_s *telnet, unsigned char type, + char *buffer, size_t size) +{ + union telnet_event_u ev; + struct telnet_environ_s *values = 0; + char *c; + char *last; + char *out; + size_t index; + size_t count; + + /* If we have no data, just pass it through */ + + if (size == 0) + { + return 0; + } + + /* First byte must be a valid command */ + + if ((unsigned)buffer[0] != TELNET_ENVIRON_SEND && + (unsigned)buffer[0] != TELNET_ENVIRON_IS && + (unsigned)buffer[0] != TELNET_ENVIRON_INFO) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "telopt %d subneg has invalid command", type); + return 0; + } + + /* Store ENVIRON command */ + + ev.envevent.cmd = buffer[0]; + + /* If we have no arguments, send an event with no data end return */ + + if (size == 1) + { + /* No list of variables given */ + + ev.envevent.values = 0; + ev.envevent.size = 0; + + /* Invoke event with our arguments */ + + ev.type = TELNET_EV_ENVIRON; + telnet->eh(telnet, &ev, telnet->ud); + return 1; + } + + /* Every second byte must be VAR or USERVAR, if present */ + + if ((unsigned)buffer[1] != TELNET_ENVIRON_VAR && + (unsigned)buffer[1] != TELNET_ENVIRON_USERVAR) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "telopt %d subneg missing variable type", type); + return 0; + } + + /* Ensure last byte is not an escape byte (makes parsing later easier) */ + + if ((unsigned)buffer[size - 1] == TELNET_ENVIRON_ESC) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "telopt %d subneg ends with ESC", type); + return 0; + } + + /* Count arguments; each valid entry starts with VAR or USERVAR */ + + count = 0; + for (c = buffer + 1; c < buffer + size; ++c) + { + if (*c == TELNET_ENVIRON_VAR || *c == TELNET_ENVIRON_USERVAR) + { + ++count; + } + else if (*c == TELNET_ENVIRON_ESC) + { + /* Skip the next byte */ + ++c; + } + } + + /* Allocate argument array, bail on error */ + + values = (struct telnet_environ_s *) + calloc(count, sizeof(struct telnet_environ_s)); + if (values == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "calloc() failed: %s", strerror(errno)); + return 0; + } + + /* Parse argument array strings */ + + out = buffer; + c = buffer + 1; + for (index = 0; index != count; ++index) + { + /* Remember the variable type (will be VAR or USERVAR) */ + + values[index].type = *c++; + + /* Scan until we find an end-marker, and buffer up unescaped bytes into + * our buffer. + */ + + last = out; + while (c < buffer + size) + { + /* Stop at the next variable or at the value */ + + if ((unsigned)*c == TELNET_ENVIRON_VAR || + (unsigned)*c == TELNET_ENVIRON_VALUE || + (unsigned)*c == TELNET_ENVIRON_USERVAR) + { + break; + } + + /* Buffer next byte (taking into account ESC) */ + + if (*c == TELNET_ENVIRON_ESC) + { + ++c; + } + + *out++ = *c++; + } + + *out++ = '\0'; + + /* Store the variable name we have just received */ + + values[index].var = last; + values[index].value = ""; + + /* If we got a value, find the next end marker and store the value; + * otherwise, store empty string. + */ + + if (c < buffer + size && *c == TELNET_ENVIRON_VALUE) + { + ++c; + last = out; + while (c < buffer + size) + { + /* Stop when we find the start of the next variable */ + + if ((unsigned)*c == TELNET_ENVIRON_VAR || + (unsigned)*c == TELNET_ENVIRON_USERVAR) + { + break; + } + + /* Buffer next byte (taking into account ESC) */ + + if (*c == TELNET_ENVIRON_ESC) + { + ++c; + } + + *out++ = *c++; + } + + *out++ = '\0'; + + /* Store the variable value */ + + values[index].value = last; + } + } + + /* Pass values array and count to event */ + + ev.envevent.values = values; + ev.envevent.size = count; + + /* Invoke event with our arguments */ + + ev.type = TELNET_EV_ENVIRON; + telnet->eh(telnet, &ev, telnet->ud); + + /* Clean up */ + + free(values); + return 1; +} + +/* Process an MSSP subnegotiation buffer */ + +static int _mssp_telnet(struct telnet_s *telnet, char *buffer, size_t size) +{ + union telnet_event_u ev; + struct telnet_environ_s *values; + char *var = 0; + char *c, *last, *out; + size_t i, count; + unsigned char next_type; + + /* If we have no data, just pass it through */ + + if (size == 0) + { + return 0; + } + + /* First byte must be a VAR */ + + if ((unsigned)buffer[0] != TELNET_MSSP_VAR) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "MSSP subnegotiation has invalid data"); + return 0; + } + + /* Count the arguments, any part that starts with VALUE */ + + for (count = 0, i = 0; i != size; ++i) + { + if ((unsigned)buffer[i] == TELNET_MSSP_VAL) + { + ++count; + } + } + + /* Allocate argument array, bail on error */ + + values = (struct telnet_environ_s *) + calloc(count, sizeof(struct telnet_environ_s)); + if (values == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "calloc() failed: %s", strerror(errno)); + return 0; + } + + ev.mssp.values = values; + ev.mssp.size = count; + + /* Allocate strings in argument array */ + + out = last = buffer; + next_type = buffer[0]; + for (i = 0, c = buffer + 1; c < buffer + size;) + { + /* Search for end marker */ + + while (c < buffer + size && (unsigned)*c != TELNET_MSSP_VAR && + (unsigned)*c != TELNET_MSSP_VAL) + { + *out++ = *c++; + } + + *out++ = '\0'; + + /* If it's a variable name, just store the name for now */ + + if (next_type == TELNET_MSSP_VAR) + { + var = last; + } + else if (next_type == TELNET_MSSP_VAL && var != 0) + { + values[i].var = var; + values[i].value = last; + ++i; + } + else + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "invalid MSSP subnegotiation data"); + free(values); + return 0; + } + + /* Remember our next type and increment c for next loop run */ + + last = out; + next_type = *c++; + } + + /* Invoke event with our arguments */ + + ev.type = TELNET_EV_MSSP; + telnet->eh(telnet, &ev, telnet->ud); + + /* Clean up */ + + free(values); + return 0; +} + +/* Parse ZMP command subnegotiation buffers */ + +static int _zmp_telnet(struct telnet_s *telnet, const char *buffer, size_t size) +{ + union telnet_event_u ev; + char **argv; + const char *c; + size_t i; + size_t argc; + + /* Make sure this is a valid ZMP buffer */ + + if (size == 0 || buffer[size - 1] != 0) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "incomplete ZMP frame"); + return 0; + } + + /* Count arguments */ + + for (argc = 0, c = buffer; c != buffer + size; ++argc) + { + c += strlen(c) + 1; + } + + /* Allocate argument array, bail on error */ + + if ((argv = (char **)calloc(argc, sizeof(char *))) == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "calloc() failed: %s", strerror(errno)); + return 0; + } + + /* Populate argument array */ + + for (i = 0, c = buffer; i != argc; ++i) + { + argv[i] = (char *)c; + c += strlen(c) + 1; + } + + /* Invoke event with our arguments */ + + ev.type = TELNET_EV_ZMP; + ev.zmp.argv = (const char **)argv; + ev.zmp.argc = argc; + telnet->eh(telnet, &ev, telnet->ud); + + /* Clean up */ + + free(argv); + return 0; +} + +/* Parse TERMINAL-TYPE command subnegotiation buffers */ + +static int _ttype_telnet(struct telnet_s *telnet, const char *buffer, + size_t size) +{ + union telnet_event_u ev; + + /* Make sure request is not empty */ + + if (size == 0) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "incomplete TERMINAL-TYPE request"); + return 0; + } + + /* Make sure request has valid command type */ + + if (buffer[0] != TELNET_TTYPE_IS && buffer[0] != TELNET_TTYPE_SEND) + { + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "TERMINAL-TYPE request has invalid type"); + return 0; + } + + /* Send proper event */ + + if (buffer[0] == TELNET_TTYPE_IS) + { + char *name; + + /* Allocate space for name */ + + if ((name = (char *)malloc(size)) == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "malloc() failed: %s", strerror(errno)); + return 0; + } + + memcpy(name, buffer + 1, size - 1); + name[size - 1] = '\0'; + + ev.type = TELNET_EV_TTYPE; + ev.ttype.cmd = TELNET_TTYPE_IS; + ev.ttype.name = name; + telnet->eh(telnet, &ev, telnet->ud); + + /* Clean up */ + + free(name); + } + else + { + ev.type = TELNET_EV_TTYPE; + ev.ttype.cmd = TELNET_TTYPE_SEND; + ev.ttype.name = 0; + telnet->eh(telnet, &ev, telnet->ud); + } + + return 0; +} + +/* Process a subnegotiation buffer; return non-zero if the current buffer + * must be aborted and reprocessed due to COMPRESS2 being activated + */ + +static int _subnegotiate(struct telnet_s *telnet) +{ + union telnet_event_u ev; + + /* Standard subnegotiation event */ + + ev.type = TELNET_EV_SUBNEGOTIATION; + ev.sub.telopt = telnet->sb_telopt; + ev.sub.buffer = telnet->buffer; + ev.sub.size = telnet->buffer_pos; + telnet->eh(telnet, &ev, telnet->ud); + + switch (telnet->sb_telopt) + { +#if defined(HAVE_ZLIB) + /* Received COMPRESS2 begin marker, setup our zlib box and start handling + * the compressed stream if it's not already. + */ + + case TELNET_TELOPT_COMPRESS2: + if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) + { + if (_init_zlib(telnet, 0, 1) != TELNET_EOK) + { + return 0; + } + + /* Notify app that compression was enabled */ + + ev.type = TELNET_EV_COMPRESS; + ev.compress.state = 1; + telnet->eh(telnet, &ev, telnet->ud); + return 1; + } + return 0; +#endif /* HAVE_ZLIB */ + + /* Specially handled subnegotiation telopt types */ + + case TELNET_TELOPT_ZMP: + return _zmp_telnet(telnet, telnet->buffer, telnet->buffer_pos); + + case TELNET_TELOPT_TTYPE: + return _ttype_telnet(telnet, telnet->buffer, telnet->buffer_pos); + + case TELNET_TELOPT_ENVIRON: + case TELNET_TELOPT_NEW_ENVIRON: + return _environ_telnet(telnet, telnet->sb_telopt, telnet->buffer, + telnet->buffer_pos); + + case TELNET_TELOPT_MSSP: + return _mssp_telnet(telnet, telnet->buffer, telnet->buffer_pos); + + default: + return 0; + } +} + +/**************************************************************************** + * Name: telnet_init + * + * Description: + * Initialize a telnet state tracker. + * + * This function initializes a new state tracker, which is used for all + * other libtelnet functions. Each connection must have its own + * telnet state tracker object. + * + * Input Parameters: + * telopts Table of TELNET options the application supports. + * eh Event handler function called for every event. + * flags 0 or TELNET_FLAG_PROXY. + * user_data Optional data pointer that will be passsed to eh. + * + * Returned Value: + * Telent state tracker object. + * + ****************************************************************************/ + +struct telnet_s *telnet_init(const struct telnet_telopt_s *telopts, + telnet_event_handler_t eh, + unsigned char flags, + void *user_data) +{ + /* Allocate structure */ + + struct telnet_s *telnet = (struct telnet_s *) calloc(1, sizeof(struct telnet_s)); + if (telnet == 0) + { + return 0; + } + + /* Initialize data */ + + telnet->ud = user_data; + telnet->telopts = telopts; + telnet->eh = eh; + telnet->flags = flags; + + return telnet; +} + +/**************************************************************************** + * Name: telnet_free + * + * Description: + * Free up any memory allocated by a state tracker. + * + * This function must be called when a telnet state tracker is no + * longer needed (such as after the connection has been closed) to + * release any memory resources used by the state tracker. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_free(struct telnet_s *telnet) +{ + /* Free sub-request buffer */ + + if (telnet->buffer != 0) + { + free(telnet->buffer); + telnet->buffer = 0; + telnet->buffer_size = 0; + telnet->buffer_pos = 0; + } + +#if defined(HAVE_ZLIB) + /* Free zlib box */ + + if (telnet->z != 0) + { + if (telnet->flags & TELNET_PFLAG_DEFLATE) + { + deflateEnd(telnet->z); + } + else + { + inflateEnd(telnet->z); + } + + free(telnet->z); + telnet->z = 0; + } +#endif /* HAVE_ZLIB */ + + /* Free RFC1143 queue */ + + if (telnet->q) + { + free(telnet->q); + telnet->q = 0; + telnet->q_size = 0; + } + + /* Free the telnet structure itself */ + + free(telnet); +} + +/* Push a byte into the telnet buffer */ + +static enum telnet_error_u _buffer_byte(struct telnet_s *telnet, + unsigned char byte) +{ + char *new_buffer; + size_t i; + + /* Check if we're out of room */ + + if (telnet->buffer_pos == telnet->buffer_size) + { + /* Find the next buffer size */ + + for (i = 0; i != _buffer_sizes_count; ++i) + { + if (_buffer_sizes[i] == telnet->buffer_size) + { + break; + } + } + + /* Overflow -- can't grow any more */ + + if (i >= _buffer_sizes_count - 1) + { + _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0, + "subnegotiation buffer size limit reached"); + return TELNET_EOVERFLOW; + } + + /* (Re)allocate buffer */ + + new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]); + if (new_buffer == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "realloc() failed"); + return TELNET_ENOMEM; + } + + telnet->buffer = new_buffer; + telnet->buffer_size = _buffer_sizes[i + 1]; + } + + /* Push the byte, all set */ + + telnet->buffer[telnet->buffer_pos++] = byte; + return TELNET_EOK; +} + +static void _process(struct telnet_s *telnet, const char *buffer, + size_t size) +{ + union telnet_event_u ev; + unsigned char byte; + size_t start; + size_t i; + + for (i = start = 0; i != size; ++i) + { + byte = buffer[i]; + switch (telnet->state) + { + /* Regular data */ + + case TELNET_STATE_DATA: + /* On an IAC byte, pass through all pending bytes and switch states */ + + if (byte == TELNET_IAC) + { + if (i != start) + { + ev.type = TELNET_EV_DATA; + ev.data.buffer = buffer + start; + ev.data.size = i - start; + telnet->eh(telnet, &ev, telnet->ud); + } + + telnet->state = TELNET_STATE_IAC; + } + break; + + /* IAC command */ + + case TELNET_STATE_IAC: + switch (byte) + { + /* Subnegotiation */ + + case TELNET_SB: + telnet->state = TELNET_STATE_SB; + break; + + /* Negotiation commands */ + + case TELNET_WILL: + telnet->state = TELNET_STATE_WILL; + break; + + case TELNET_WONT: + telnet->state = TELNET_STATE_WONT; + break; + + case TELNET_DO: + telnet->state = TELNET_STATE_DO; + break; + + case TELNET_DONT: + telnet->state = TELNET_STATE_DONT; + break; + + /* IAC escaping */ + + case TELNET_IAC: + /* Event */ + + ev.type = TELNET_EV_DATA; + ev.data.buffer = (char *)&byte; + ev.data.size = 1; + telnet->eh(telnet, &ev, telnet->ud); + + /* State update */ + + start = i + 1; + telnet->state = TELNET_STATE_DATA; + break; + + /* Some other command */ + + default: + /* Event */ + + ev.type = TELNET_EV_IAC; + ev.iac.cmd = byte; + telnet->eh(telnet, &ev, telnet->ud); + + /* State update */ + + start = i + 1; + telnet->state = TELNET_STATE_DATA; + } + break; + + /* Negotiation commands */ + + case TELNET_STATE_WILL: + case TELNET_STATE_WONT: + case TELNET_STATE_DO: + case TELNET_STATE_DONT: + _negotiate(telnet, byte); + start = i + 1; + telnet->state = TELNET_STATE_DATA; + break; + + /* Subnegotiation -- determine subnegotiation telopt */ + + case TELNET_STATE_SB: + telnet->sb_telopt = byte; + telnet->buffer_pos = 0; + telnet->state = TELNET_STATE_SB_DATA; + break; + + /* Subnegotiation -- buffer bytes until end request */ + + case TELNET_STATE_SB_DATA: + /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ + + if (byte == TELNET_IAC) + { + telnet->state = TELNET_STATE_SB_DATA_IAC; + } + else if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS && + byte == TELNET_WILL) + { + /* In 1998 MCCP used TELOPT 85 and the protocol defined an + * invalid subnegotiation sequence (IAC SB 85 WILL SE) to start + * compression. Subsequently MCCP version 2 was created in 2000 + * using TELOPT 86 and a valid subnegotiation (IAC SB 86 IAC SE). + * libtelnet for now just captures and discards MCCPv1 sequences. + */ + + start = i + 2; + telnet->state = TELNET_STATE_DATA; + + /* Buffer the byte, or bail if we can't */ + } + else if (_buffer_byte(telnet, byte) != TELNET_EOK) + { + start = i + 1; + telnet->state = TELNET_STATE_DATA; + } + break; + + /* IAC escaping inside a subnegotiation */ + + case TELNET_STATE_SB_DATA_IAC: + switch (byte) + { + /* End subnegotiation */ + + case TELNET_SE: + /* Return to default state */ + + start = i + 1; + telnet->state = TELNET_STATE_DATA; + + /* Process subnegotiation */ + + if (_subnegotiate(telnet) != 0) + { + /* Any remaining bytes in the buffer are compressed. we have + * to re-invoke telnet_recv to get those bytes inflated and + * abort trying to process the remaining compressed bytes in + * the current _process buffer argument. + */ + + telnet_recv(telnet, &buffer[start], size - start); + return; + } + break; + + /* Escaped IAC byte */ + + case TELNET_IAC: + /* Push IAC into buffer */ + + if (_buffer_byte(telnet, TELNET_IAC) != TELNET_EOK) + { + start = i + 1; + telnet->state = TELNET_STATE_DATA; + } + else + { + telnet->state = TELNET_STATE_SB_DATA; + } + break; + + /* Something else -- protocol error. attempt to process content + * in subnegotiation buffer, then evaluate the given command as + * an IAC code. + */ + + default: + _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, + "unexpected byte after IAC inside SB: %d", byte); + + /* Enter IAC state */ + + start = i + 1; + telnet->state = TELNET_STATE_IAC; + + /* Process subnegotiation; see comment in + * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv() + */ + + if (_subnegotiate(telnet) != 0) + { + telnet_recv(telnet, &buffer[start], size - start); + return; + } + else + { + /* Recursive call to get the current input byte processed as + * a regular IAC command. we could use a goto, but that + * would be gross. + */ + + _process(telnet, (char *)&byte, 1); + } + break; + } + break; + } + } + + /* Pass through any remaining bytes */ + + if (telnet->state == TELNET_STATE_DATA && i != start) + { + ev.type = TELNET_EV_DATA; + ev.data.buffer = buffer + start; + ev.data.size = i - start; + telnet->eh(telnet, &ev, telnet->ud); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: telnet_recv + * + * Description: + * Push a byte buffer into the state tracker. + * + * Passes one or more bytes to the telnet state tracker for + * protocol parsing. The byte buffer is most often going to be + * the buffer that recv() was called for while handling the + * connection. + * + * Input Parameters: + * telnet Telnet state tracker object. + * buffer Pointer to byte buffer. + * size Number of bytes pointed to by buffer. + * + ****************************************************************************/ + +void telnet_recv(struct telnet_s *telnet, const char *buffer, size_t size) +{ +#if defined(HAVE_ZLIB) + /* If we have an inflate (decompression) zlib stream, use it */ + + if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) + { + char inflate_buffer[1024]; + int rs; + + /* Initialize zlib state */ + + telnet->z->next_in = (unsigned char *)buffer; + telnet->z->avail_in = (unsigned int)size; + telnet->z->next_out = (unsigned char *)inflate_buffer; + telnet->z->avail_out = sizeof(inflate_buffer); + + /* Inflate until buffer exhausted and all output is produced */ + + while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) + { + /* Reset output buffer */ + /* Decompress */ + + rs = inflate(telnet->z, Z_SYNC_FLUSH); + + /* Process the decompressed bytes on success */ + + if (rs == Z_OK || rs == Z_STREAM_END) + { + _process(telnet, inflate_buffer, sizeof(inflate_buffer) - + telnet->z->avail_out); + } + else + { + _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, + "inflate() failed: %s", zError(rs)); + } + + /* Prepare output buffer for next run */ + + telnet->z->next_out = (unsigned char *)inflate_buffer; + telnet->z->avail_out = sizeof(inflate_buffer); + + /* On error (or on end of stream) disable further inflation */ + + if (rs != Z_OK) + { + union telnet_event_u ev; + + /* Disable compression */ + + inflateEnd(telnet->z); + free(telnet->z); + telnet->z = 0; + + /* Send event */ + + ev.type = TELNET_EV_COMPRESS; + ev.compress.state = 0; + telnet->eh(telnet, &ev, telnet->ud); + break; + } + } + + /* COMPRESS2 is not negotiated, just process */ + } + else +#endif /* HAVE_ZLIB */ + _process(telnet, buffer, size); +} + +/**************************************************************************** + * Name: telnet_iac + * + * Description: + * Send a telnet command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd Command to send. + * + ****************************************************************************/ + +void telnet_iac(struct telnet_s *telnet, unsigned char cmd) +{ + unsigned char bytes[2]; + bytes[0] = TELNET_IAC; + bytes[1] = cmd; + _sendu(telnet, bytes, 2); +} + +/**************************************************************************** + * Name: telnet_negotiate + * + * Description: + * Send negotiation command. + * + * Internally, libtelnet uses RFC1143 option negotiation rules. + * The negotiation commands sent with this function may be ignored + * if they are determined to be redundant. + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd TELNET_WILL, TELNET_WONT, TELNET_DO, or TELNET_DONT. + * opt One of the TELNET_TELOPT_* values. + * + ****************************************************************************/ + +void telnet_negotiate(struct telnet_s *telnet, unsigned char cmd, + unsigned char telopt) +{ + struct telnet_rfc1143_s q; + + /* If we're in proxy mode, just send it now */ + + if (telnet->flags & TELNET_FLAG_PROXY) + { + unsigned char bytes[3]; + bytes[0] = TELNET_IAC; + bytes[1] = cmd; + bytes[2] = telopt; + _sendu(telnet, bytes, 3); + return; + } + + /* Get current option states */ + + q = _get_rfc1143(telnet, telopt); + + switch (cmd) + { + /* Advertise willingess to support an option */ + + case TELNET_WILL: + switch (Q_US(q)) + { + case Q_NO: + _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WILL, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_WANTNO_OP, Q_HIM(q)); + break; + + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q)); + break; + } + break; + + /* Force turn-off of locally enabled option */ + + case TELNET_WONT: + switch (Q_US(q)) + { + case Q_YES: + _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q)); + _send_negotiate(telnet, TELNET_WONT, telopt); + break; + + case Q_WANTYES: + _set_rfc1143(telnet, telopt, Q_WANTYES_OP, Q_HIM(q)); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q)); + break; + } + break; + + /* Ask remote end to enable an option */ + + case TELNET_DO: + switch (Q_HIM(q)) + { + case Q_NO: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES); + _send_negotiate(telnet, TELNET_DO, telopt); + break; + + case Q_WANTNO: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO_OP); + break; + + case Q_WANTYES_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES); + break; + } + break; + + /* Demand remote end disable an option */ + + case TELNET_DONT: + switch (Q_HIM(q)) + { + case Q_YES: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO); + _send_negotiate(telnet, TELNET_DONT, telopt); + break; + + case Q_WANTYES: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES_OP); + break; + + case Q_WANTNO_OP: + _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO); + break; + } + break; + } +} + +/**************************************************************************** + * Name: telnet_send + * + * Description: + * Send non-command data (escapes IAC bytes). + * + * Input Parameters: + * telnet Telnet state tracker object. + * buffer Buffer of bytes to send. + * size Number of bytes to send. + * + ****************************************************************************/ + +void telnet_send(struct telnet_s *telnet, const char *buffer, size_t size) +{ + size_t l; + size_t i; + + for (l = i = 0; i != size; ++i) + { + /* Dump prior portion of text, send escaped bytes */ + + if (buffer[i] == (char)TELNET_IAC) + { + /* Dump prior text if any */ + + if (i != l) + { + _send(telnet, buffer + l, i - l); + } + + l = i + 1; + + /* Send escape */ + + telnet_iac(telnet, TELNET_IAC); + } + } + + /* Send whatever portion of buffer is left */ + + if (i != l) + { + _send(telnet, buffer + l, i - l); + } +} + +/**************************************************************************** + * Name: telnet_begin_sb + * + * Description: + * Begin a sub-negotiation command. + * + * Sends IAC SB followed by the telopt code. All following data sent + * will be part of the sub-negotiation, until telnet_finish_sb() is + * called. + * + * Input Parameters: + * telnet Telnet state tracker object. + * telopt One of the TELNET_TELOPT_* values. + * + ****************************************************************************/ + +void telnet_begin_sb(struct telnet_s *telnet, unsigned char telopt) +{ + unsigned char sb[3]; + sb[0] = TELNET_IAC; + sb[1] = TELNET_SB; + sb[2] = telopt; + _sendu(telnet, sb, 3); +} + +/**************************************************************************** + * Name: telnet_subnegotiation + * + * Description: + * Send a complete subnegotiation buffer. + * + * Equivalent to: + * telnet_begin_sb(telnet, telopt); + * telnet_send(telnet, buffer, size); + * telnet_finish_sb(telnet); + * + * Input Parameters: + * telnet Telnet state tracker format. + * telopt One of the TELNET_TELOPT_* values. + * buffer Byte buffer for sub-negotiation data. + * size Number of bytes to use for sub-negotiation data. + * + ****************************************************************************/ + +void telnet_subnegotiation(struct telnet_s *telnet, unsigned char telopt, + const char *buffer, size_t size) +{ + unsigned char bytes[5]; + bytes[0] = TELNET_IAC; + bytes[1] = TELNET_SB; + bytes[2] = telopt; + bytes[3] = TELNET_IAC; + bytes[4] = TELNET_SE; + + _sendu(telnet, bytes, 3); + telnet_send(telnet, buffer, size); + _sendu(telnet, bytes + 3, 2); + +#if defined(HAVE_ZLIB) + /* If we're a proxy and we just sent the COMPRESS2 marker, we must make sure + * all further data is compressed if not already. + */ + + if (telnet->flags & TELNET_FLAG_PROXY && telopt == TELNET_TELOPT_COMPRESS2) + { + union telnet_event_u ev; + + if (_init_zlib(telnet, 1, 1) != TELNET_EOK) + { + return; + } + + /* Notify app that compression was enabled */ + + ev.type = TELNET_EV_COMPRESS; + ev.compress.state = 1; + telnet->eh(telnet, &ev, telnet->ud); + } +#endif /* HAVE_ZLIB */ +} + +/**************************************************************************** + * Name: telnet_begin_compress2 + * + * Description: + * Begin sending compressed data. + * + * This function will begein sending data using the COMPRESS2 option, + * which enables the use of zlib to compress data sent to the client. + * The client must offer support for COMPRESS2 with option negotiation, + * and zlib support must be compiled into libtelnet. + * + * Only the server may call this command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_begin_compress2(struct telnet_s *telnet) +{ +#if defined(HAVE_ZLIB) + static const unsigned char compress2[] = + { + TELNET_IAC, TELNET_SB, TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE + }; + union telnet_event_u ev; + + /* Attempt to create output stream first, bail if we can't */ + + if (_init_zlib(telnet, 1, 0) != TELNET_EOK) + { + return; + } + + /* Send compression marker. we send directly to the event handler instead of + * passing through _send because _send would result in the compress marker + * itself being compressed. + */ + + ev.type = TELNET_EV_SEND; + ev.data.buffer = (const char *)compress2; + ev.data.size = sizeof(compress2); + telnet->eh(telnet, &ev, telnet->ud); + + /* Notify app that compression was successfully enabled */ + + ev.type = TELNET_EV_COMPRESS; + ev.compress.state = 1; + telnet->eh(telnet, &ev, telnet->ud); +#endif /* HAVE_ZLIB */ +} + +/**************************************************************************** + * Name: telnet_vprintf + * + * Description: + * Send formatted data with \r and \n translation in addition to IAC IAC + * + * See telnet_printf(). + * + ****************************************************************************/ + +int telnet_vprintf(struct telnet_s *telnet, const char *fmt, va_list va) +{ + static const char CRLF[] = { '\r', '\n' }; + static const char CRNUL[] = { '\r', '\0' }; + char buffer[1024]; + char *output = buffer; + int rs; + int l; + int i; + + /* Format */ + + va_list va2; + va_copy(va2, va); + rs = vsnprintf(buffer, sizeof(buffer), fmt, va); + if (rs >= sizeof(buffer)) + { + output = (char *)malloc(rs + 1); + if (output == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "malloc() failed: %s", strerror(errno)); + return -1; + } + + rs = vsnprintf(output, rs + 1, fmt, va2); + } + + va_end(va2); + va_end(va); + + /* Send */ + + for (l = i = 0; i != rs; ++i) + { + /* Special characters */ + + if (output[i] == (char)TELNET_IAC || output[i] == '\r' || + output[i] == '\n') + { + /* Dump prior portion of text */ + + if (i != l) + { + _send(telnet, output + l, i - l); + } + + l = i + 1; + + /* IAC -> IAC IAC */ + + if (output[i] == (char)TELNET_IAC) + { + telnet_iac(telnet, TELNET_IAC); + } + + /* Automatic translation of \r -> CRNUL */ + + else if (output[i] == '\r') + { + _send(telnet, CRNUL, 2); + } + + /* Automatic translation of \n -> CRLF */ + + else if (output[i] == '\n') + { + _send(telnet, CRLF, 2); + } + } + } + + /* Send whatever portion of output is left */ + + if (i != l) + { + _send(telnet, output + l, i - l); + } + + /* Free allocated memory, if any */ + + if (output != buffer) + { + free(output); + } + + return rs; +} + +/**************************************************************************** + * Name: telnet_printf + * + * Description: + * Send formatted data. + * + * This function is a wrapper around telnet_send(). It allows using + * printf-style formatting. + * + * Additionally, this function will translate \\r to the CR NUL construct + * and \\n with CR LF, as well as automatically escaping IAC bytes like + * telnet_send(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * fmt Format string. + * + * Returned Value: + * Number of bytes sent. + * + ****************************************************************************/ + +int telnet_printf(struct telnet_s *telnet, const char *fmt, ...) +{ + va_list va; + int rs; + + va_start(va, fmt); + rs = telnet_vprintf(telnet, fmt, va); + va_end(va); + + return rs; +} + +/**************************************************************************** + * Name: telnet_raw_vprintf + * + * Description: + * Send formatted data (no newline escaping). + * + * See telnet_raw_printf(). + * + ****************************************************************************/ + +int telnet_raw_vprintf(struct telnet_s *telnet, const char *fmt, va_list va) +{ + char buffer[1024]; + char *output = buffer; + int rs; + + /* Format; allocate more space if necessary */ + + va_list va2; + va_copy(va2, va); + rs = vsnprintf(buffer, sizeof(buffer), fmt, va); + if (rs >= sizeof(buffer)) + { + output = (char *)malloc(rs + 1); + if (output == 0) + { + _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, + "malloc() failed: %s", strerror(errno)); + return -1; + } + + rs = vsnprintf(output, rs + 1, fmt, va2); + } + + va_end(va2); + va_end(va); + + /* Send out the formatted data */ + + telnet_send(telnet, output, rs); + + /* Release allocated memory, if any */ + + if (output != buffer) + { + free(output); + } + + return rs; +} + +/**************************************************************************** + * Name: telnet_raw_printf + * + * Description: + * Send formatted data (no newline escaping). + * + * This behaves identically to telnet_printf(), except that the \\r and \\n + * characters are not translated. The IAC byte is still escaped as normal + * with telnet_send(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * fmt Format string. + * + * Returned Value: + * Number of bytes sent. + * + ****************************************************************************/ + +int telnet_raw_printf(struct telnet_s *telnet, const char *fmt, ...) +{ + va_list va; + int rs; + + va_start(va, fmt); + rs = telnet_raw_vprintf(telnet, fmt, va); + va_end(va); + + return rs; +} + +/**************************************************************************** + * Name: telnet_begin_newenviron + * + * Description: + * Begin a new set of NEW-ENVIRON values to request or send. + * + * This function will begin the sub-negotiation block for sending or + * requesting NEW-ENVIRON values. + * + * The telnet_finish_newenviron() macro must be called after this + * function to terminate the NEW-ENVIRON command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * type One of TELNET_ENVIRON_SEND, TELNET_ENVIRON_IS, or + * TELNET_ENVIRON_INFO. + * + ****************************************************************************/ + +void telnet_begin_newenviron(struct telnet_s *telnet, unsigned char cmd) +{ + telnet_begin_sb(telnet, TELNET_TELOPT_NEW_ENVIRON); + telnet_send(telnet, (const char *)&cmd, 1); +} + +/**************************************************************************** + * Name: telnet_newenviron_value + * + * Description: + * Send a NEW-ENVIRON variable name or value. + * + * This can only be called between calls to telnet_begin_newenviron() and + * telnet_finish_newenviron(). + * + * Input Parameters: + * telnet Telnet state tracker object. + * type One of TELNET_ENVIRON_VAR, TELNET_ENVIRON_USERVAR, or + * TELNET_ENVIRON_VALUE. + * string Variable name or value. + * + ****************************************************************************/ + +void telnet_newenviron_value(struct telnet_s *telnet, unsigned char type, + const char *string) +{ + telnet_send(telnet, (const char *)&type, 1); + + if (string != 0) + { + telnet_send(telnet, string, strlen(string)); + } +} + +/**************************************************************************** + * Name: telnet_ttype_send + * + * Description: + * Send the TERMINAL-TYPE SEND command. + * + * Sends the sequence IAC TERMINAL-TYPE SEND. + * + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_ttype_send(struct telnet_s *telnet) +{ + static const unsigned char SEND[] = + { + TELNET_IAC, TELNET_SB, TELNET_TELOPT_TTYPE, TELNET_TTYPE_SEND, + TELNET_IAC, TELNET_SE + }; + + _sendu(telnet, SEND, sizeof(SEND)); +} + +/**************************************************************************** + * Name: telnet_ttype_is + * + * Description: + * Send the TERMINAL-TYPE IS command. + * + * Sends the sequence IAC TERMINAL-TYPE IS "string". + * + * According to the RFC, the recipient of a TERMINAL-TYPE SEND shall + * send the next possible terminal-type the client supports. Upon sending + * the type, the client should switch modes to begin acting as the terminal + * type is just sent. + * + * The server may continue sending TERMINAL-TYPE IS until it receives a + * terminal type is understands. To indicate to the server that it has + * reached the end of the available optoins, the client must send the last + * terminal type a second time. When the server receives the same terminal + * type twice in a row, it knows it has seen all available terminal types. + * + * After the last terminal type is sent, if the client receives another + * TERMINAL-TYPE SEND command, it must begin enumerating the available + * terminal types from the very beginning. This allows the server to + * scan the available types for a preferred terminal type and, if none + * is found, to then ask the client to switch to an acceptable + * alternative. + * + * Note that if the client only supports a single terminal type, then + * simply sending that one type in response to every SEND will satisfy + * the behavior requirements. + * + * Input Parameters: + * telnet Telnet state tracker object. + * ttype Name of the terminal-type being sent. + * + ****************************************************************************/ + +void telnet_ttype_is(struct telnet_s *telnet, const char *ttype) +{ + static const unsigned char IS[] = + { + TELNET_IAC, TELNET_SB, TELNET_TELOPT_TTYPE, TELNET_TTYPE_IS + }; + + _sendu(telnet, IS, sizeof(IS)); + _send(telnet, ttype, strlen(ttype)); + telnet_finish_sb(telnet); +} + +/**************************************************************************** + * Name: telnet_send_zmp + * + * Description: + * Send a ZMP command. + * + * Input Parameters: + * telnet Telnet state tracker object. + * argc Number of ZMP commands being sent. + * argv Array of argument strings. + * + ****************************************************************************/ + +void telnet_send_zmp(struct telnet_s *telnet, size_t argc, const char **argv) +{ + size_t i; + + /* ZMP header */ + + telnet_begin_zmp(telnet, argv[0]); + + /* Send out each argument, including trailing NUL byte */ + + for (i = 1; i != argc; ++i) + { + telnet_zmp_arg(telnet, argv[i]); + } + + /* ZMP footer */ + + telnet_finish_zmp(telnet); +} + +/**************************************************************************** + * Name: telnet_send_vzmpv + * + * Description: + * Send a ZMP command. + * + * See telnet_send_zmpv(). + * + ****************************************************************************/ + +void telnet_send_vzmpv(struct telnet_s *telnet, va_list va) +{ + const char *arg; + + /* ZMP header */ + + telnet_begin_sb(telnet, TELNET_TELOPT_ZMP); + + /* Send out each argument, including trailing NUL byte */ + + while ((arg = va_arg(va, const char *)) != 0) + { + telnet_zmp_arg(telnet, arg); + } + + /* ZMP footer */ + + telnet_finish_zmp(telnet); +} + +/**************************************************************************** + * Name: telnet_send_zmpv + * + * Description: + * Send a ZMP command. + * + * Arguments are listed out in var-args style. After the last argument, a + * NULL pointer must be passed in as a sentinel value. + * + * Input Parameters: + * telnet Telnet state tracker object. + * + ****************************************************************************/ + +void telnet_send_zmpv(struct telnet_s *telnet, ...) +{ + va_list va; + + va_start(va, telnet); + telnet_send_vzmpv(telnet, va); + va_end(va); +} + +/**************************************************************************** + * Name: telnet_begin_zmp + * + * Description: + * Begin sending a ZMP command + * + * Input Parameters: + * telnet Telnet state tracker object. + * cmd The first argument (command name) for the ZMP command. + * + ****************************************************************************/ + +void telnet_begin_zmp(struct telnet_s *telnet, const char *cmd) +{ + telnet_begin_sb(telnet, TELNET_TELOPT_ZMP); + telnet_zmp_arg(telnet, cmd); +} + +/**************************************************************************** + * Name: telnet_zmp_arg + * + * Description: + * Send a ZMP command argument. + * + * Input Parameters: + * telnet Telnet state tracker object. + * arg Telnet argument string. + * + ****************************************************************************/ + +void telnet_zmp_arg(struct telnet_s *telnet, const char *arg) +{ + telnet_send(telnet, arg, strlen(arg) + 1); +} From 666771f18c807c2338c46ecde9e8cb906a49ae80 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 16:39:39 -0600 Subject: [PATCH 18/23] system/telnet: Add Telnet Chat deamon and and client from libtelent. --- include/system/readline.h | 6 +- netutils/telnetc/Kconfig | 20 ++ netutils/telnetc/Make.defs | 39 +++ netutils/telnetc/Makefile | 104 +++++++ netutils/telnetc/telnetc.c | 16 +- system/telnet/.gitignore | 11 + system/telnet/Kconfig | 61 ++++ system/telnet/Make.defs | 41 +++ system/telnet/Makefile | 205 ++++++++++++++ system/telnet/telnet_chatd.c | 510 ++++++++++++++++++++++++++++++++++ system/telnet/telnet_client.c | 425 ++++++++++++++++++++++++++++ 11 files changed, 1425 insertions(+), 13 deletions(-) create mode 100644 netutils/telnetc/Kconfig create mode 100644 netutils/telnetc/Make.defs create mode 100644 netutils/telnetc/Makefile create mode 100644 system/telnet/.gitignore create mode 100644 system/telnet/Kconfig create mode 100644 system/telnet/Make.defs create mode 100644 system/telnet/Makefile create mode 100644 system/telnet/telnet_chatd.c create mode 100644 system/telnet/telnet_client.c diff --git a/include/system/readline.h b/include/system/readline.h index 8020cf2c9..a2ef10bbf 100644 --- a/include/system/readline.h +++ b/include/system/readline.h @@ -216,11 +216,7 @@ ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream); * ****************************************************************************/ -#if CONFIG_NFILE_STREAMS > 0 -# define std_readline(b,s) readline(b,s,stdin,stdout) -#else -ssize_t std_readline(FAR char *buf, int buflen); -#endif +#define std_readline(b,s) readline(b,s,stdin,stdout) #undef EXTERN #ifdef __cplusplus diff --git a/netutils/telnetc/Kconfig b/netutils/telnetc/Kconfig new file mode 100644 index 000000000..ab3d42a7f --- /dev/null +++ b/netutils/telnetc/Kconfig @@ -0,0 +1,20 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config NETUTILS_TELNETC + bool "Telnet client library" + default n + depends on NET && NET_TCP + ---help--- + Enable support for the libtelnet. This is a public domain + Telnet client library available from https://github.com/seanmiddleditch/libtelnet + modified for use with NuttX. Original Authors: + + Sean Middleditch + Jack Kelly + Katherine Flavel + +if NETUTILS_TELNETC +endif diff --git a/netutils/telnetc/Make.defs b/netutils/telnetc/Make.defs new file mode 100644 index 000000000..1d5a07ee0 --- /dev/null +++ b/netutils/telnetc/Make.defs @@ -0,0 +1,39 @@ +# apps/netutils/telnetc/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2017 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. +# +############################################################################ + +ifeq ($(CONFIG_NETUTILS_TELNETC),y) +CONFIGURED_APPS += netutils/telnetc +endif + diff --git a/netutils/telnetc/Makefile b/netutils/telnetc/Makefile new file mode 100644 index 000000000..138fedc08 --- /dev/null +++ b/netutils/telnetc/Makefile @@ -0,0 +1,104 @@ +############################################################################ +# apps/netutils/telnetc/Makefile +# +# Copyright (C) 2017 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 + +# Telnet daemon + +ASRCS = +CSRCS = + +ifeq ($(CONFIG_NET_TCP),y) +CSRCS += telnetc.c +endif + +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 preconfig + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + $(Q) touch .built + +install: + +context: + +.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) + +preconfig: + +-include Make.dep diff --git a/netutils/telnetc/telnetc.c b/netutils/telnetc/telnetc.c index a5ee4e38e..d27e93ced 100644 --- a/netutils/telnetc/telnetc.c +++ b/netutils/telnetc/telnetc.c @@ -249,7 +249,7 @@ enum telnet_error_u _init_zlib(struct telnet_s *telnet, int deflate, if ((z = (z_stream *) calloc(1, sizeof(z_stream))) == 0) { return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal, - "malloc() failed: %s", strerror(errno)); + "malloc() failed: %d", errno); } /* Initialize */ @@ -445,7 +445,7 @@ static inline void _set_rfc1143(struct telnet_s *telnet, unsigned char telopt, if (qtmp == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "realloc() failed: %s", strerror(errno)); + "realloc() failed: %d", errno); return; } @@ -758,7 +758,7 @@ static int _environ_telnet(struct telnet_s *telnet, unsigned char type, if (values == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "calloc() failed: %s", strerror(errno)); + "calloc() failed: %d", errno); return 0; } @@ -901,7 +901,7 @@ static int _mssp_telnet(struct telnet_s *telnet, char *buffer, size_t size) if (values == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "calloc() failed: %s", strerror(errno)); + "calloc() failed: %d", errno); return 0; } @@ -992,7 +992,7 @@ static int _zmp_telnet(struct telnet_s *telnet, const char *buffer, size_t size) if ((argv = (char **)calloc(argc, sizeof(char *))) == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "calloc() failed: %s", strerror(errno)); + "calloc() failed: %d", errno); return 0; } @@ -1053,7 +1053,7 @@ static int _ttype_telnet(struct telnet_s *telnet, const char *buffer, if ((name = (char *)malloc(size)) == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "malloc() failed: %s", strerror(errno)); + "malloc() failed: %d", errno); return 0; } @@ -1980,7 +1980,7 @@ int telnet_vprintf(struct telnet_s *telnet, const char *fmt, va_list va) if (output == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "malloc() failed: %s", strerror(errno)); + "malloc() failed: %d", errno); return -1; } @@ -2109,7 +2109,7 @@ int telnet_raw_vprintf(struct telnet_s *telnet, const char *fmt, va_list va) if (output == 0) { _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, - "malloc() failed: %s", strerror(errno)); + "malloc() failed: %d", errno); return -1; } diff --git a/system/telnet/.gitignore b/system/telnet/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/system/telnet/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/system/telnet/Kconfig b/system/telnet/Kconfig new file mode 100644 index 000000000..51cb9f72e --- /dev/null +++ b/system/telnet/Kconfig @@ -0,0 +1,61 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +menuconfig SYSTEM_TELNET_CHATD + bool "Telnet chat deamon" + default n + depends on NET && NET_TCP + select NETUTILS_TELNETC + ---help--- + Enable the Telnet Chat daemon. + +if SYSTEM_TELNET_CHATD + +config SYSTEM_TELNET_CHATD_PROGNAME + string "Chat daemon program name" + default "chatd" + depends on BUILD_KERNEL + +if NSH_BUILTIN_APPS + +config SYSTEM_TELNET_CHATD_STACKSIZE + int "Chat daemon stacksize" + default 2048 + +config SYSTEM_TELNET_CHATD_PRIORITY + int "Chat daemon priority" + default 100 + +endif # NSH_BUILTIN_APPS +endif # SYSTEM_TELNET_CHATD + +menuconfig SYSTEM_TELNET_CLIENT + bool "Telnet client" + default n + depends on NET && NET_TCP + select NETUTILS_TELNETC + select SYSTEM_READLINE + ---help--- + Enable the Telnet client program. + +if SYSTEM_TELNET_CLIENT + +config SYSTEM_TELNET_CLIENT_PROGNAME + string "Telnet client program name" + default "chatd" + depends on BUILD_KERNEL + +if NSH_BUILTIN_APPS + +config SYSTEM_TELNET_CLIENT_STACKSIZE + int "Telnet client stacksize" + default 2048 + +config SYSTEM_TELNET_CLIENT_PRIORITY + int "Telnet client priority" + default 100 + +endif # NSH_BUILTIN_APPS +endif # SYSTEM_TELNET_CLIENT diff --git a/system/telnet/Make.defs b/system/telnet/Make.defs new file mode 100644 index 000000000..cb9c7a480 --- /dev/null +++ b/system/telnet/Make.defs @@ -0,0 +1,41 @@ +############################################################################ +# apps/system/usbmsc/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2016 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. +# +############################################################################ + +ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y) +CONFIGURED_APPS += system/telnet +else ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y) +CONFIGURED_APPS += system/telnet +endif diff --git a/system/telnet/Makefile b/system/telnet/Makefile new file mode 100644 index 000000000..0ff663985 --- /dev/null +++ b/system/telnet/Makefile @@ -0,0 +1,205 @@ +############################################################################ +# apps/system/telnet/Makefile +# +# Copyright (C) 2017 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 + +# Telnet Programs + +ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y) + +# Chatd files + +CHATD_CSRCS = +CHATD_MAINSRC = telnet_chatd.c + +CHATD_COBJS = $(CHATD_CSRCS:.c=$(OBJEXT)) +CHATD_MAINOBJ = $(CHATD_MAINSRC:.c=$(OBJEXT)) + +# Chatd Application Info + +CONFIG_SYSTEM_TELNET_CHATD_PROGNAME ?= chatd +CONFIG_SYSTEM_TELNET_CHATD_PRIORITY ?= 100 +CONFIG_SYSTEM_TELNET_CHATD_STACKSIZE ?= 2048 + +CHATD_MAINNAME = chatd_main +CHATD_APPNAME = chatd +CHATD_PROGNAME = $(CONFIG_SYSTEM_TELNET_CHATD_PROGNAME) +CHATD_PRIORITY = $(CONFIG_SYSTEM_TELNET_CHATD_PRIORITY) +CHATD_STACKSIZE = $(CONFIG_SYSTEM_TELNET_CHATD_STACKSIZE) + +endif + +ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y) + +# Telnet client files + +CLIENT_CSRCS = +CLIENT_MAINSRC = telnet_client.c + +CLIENT_COBJS = $(CLIENT_CSRCS:.c=$(OBJEXT)) +CLIENT_MAINOBJ = $(CLIENT_MAINSRC:.c=$(OBJEXT)) + +# Telnet client Application Info + +CONFIG_SYSTEM_TELNET_CLIENT_PROGNAME ?= tcpclient +CONFIG_SYSTEM_TELNET_CLIENT_PRIORITY ?= 100 +CONFIG_SYSTEM_TELNET_CLIENT_STACKSIZE ?= 2048 + +CLIENT_MAINNAME = telnet_main +CLIENT_APPNAME = telnet +CLIENT_PROGNAME = $(CONFIG_SYSTEM_TELNET_CLIENT_PROGNAME) +CLIENT_PRIORITY = $(CONFIG_SYSTEM_TELNET_CLIENT_PRIORITY) +CLIENT_STACKSIZE = $(CONFIG_SYSTEM_TELNET_CLIENT_STACKSIZE) + +endif + +# All programns + +TARG_SRCS = $(CHATD_CRCS) $(CHATD_MAINSRC) $(CLIENT_CSRCS) $(CLIENT_MAINSRC) +TARG_OBJS = $(CHATD_COBJS) $(CLIENT_COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + TARG_OBJS += $(CHATD_MAINOBJ) $(CLIENT_MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + TARG_BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + TARG_BIN = ..\\..\\libapps$(LIBEXT) +else + TARG_BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: clean depend distclean preconfig + +$(CHATD_COBJS) $(CHATD_MAINOBJ) $(CLIENT_COBJS) $(CLIENT_MAINOBJ) : %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(TARG_OBJS) + $(call ARCHIVE, $(TARG_BIN), $(TARG_OBJS)) + $(Q) touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y) + +$(BIN_DIR)$(DELIM)$(CHATD_PROGNAME): $(OBJS) $(CHATD_MAINOBJ) + @echo "LD: $(CHATD_PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CHATD_PROGNAME) $(ARCHCRT0OBJ) $(CHATD_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CHATD_PROGNAME) + +chatd_install: $(BIN_DIR)$(DELIM)$(CHATD_PROGNAME) +else +chatd_install: +endif + +ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y) + +$(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME): $(OBJS) $(CLIENT_MAINOBJ) + @echo "LD: $(CLIENT_PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) $(ARCHCRT0OBJ) $(CLIENT_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) + +client_install: $(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME) +else +client_install: +endif + +install: chatd_install client_install + +else +install: + +endif + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +ifeq ($(CONFIG_SYSTEM_TELNET_CHATD),y) + +$(BUILTIN_REGISTRY)$(DELIM)$(CHATD_APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(CHATD_APPNAME),$(CHATD_PRIORITY),$(CHATD_STACKSIZE),$(CHATD_MAINNAME)) + +chatd_register: $(BUILTIN_REGISTRY)$(DELIM)$(CHATD_APPNAME)_main.bdat +else +chatd_register: +endif + +ifeq ($(CONFIG_SYSTEM_TELNET_CLIENT),y) +$(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(CLIENT_APPNAME),$(CLIENT_PRIORITY),$(CLIENT_STACKSIZE),$(CLIENT_MAINNAME)) + +client_register: $(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat +else +client_register: +endif + +context: chatd_register client_register +else +context: +endif + +.depend: Makefile $(TARG_CSRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(TARG_CSRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call DELFILE, *.dSYM) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +preconfig: + +-include Make.dep diff --git a/system/telnet/telnet_chatd.c b/system/telnet/telnet_chatd.c new file mode 100644 index 000000000..e7a560b87 --- /dev/null +++ b/system/telnet/telnet_chatd.c @@ -0,0 +1,510 @@ +/**************************************************************************** + * apps/system/telnet/telnet_chatd.c + * + * Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet. + * Modified and re-released under the BSD license: + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * The original authors of libtelnet are listed below. Per their licesne, + * "The author or authors of this code dedicate any and all copyright + * interest in this code to the public domain. We make this dedication for + * the benefit of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * code under copyright law." + * + * Author: Sean Middleditch + * (Also listed in the AUTHORS file are Jack Kelly + * and Katherine Flavel ) + * + * 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 + +#include +#include + +#include "netutils/telnetc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_USERS 64 +#define LINEBUFFER_SIZE 256 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct user_s +{ + char *name; + int sock; + struct telnet_s *telnet; + char linebuf[256]; + int linepos; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct telnet_telopt_s g_telopts[] = +{ + { + TELNET_TELOPT_COMPRESS2, TELNET_WILL, TELNET_DONT + }, + { + -1, 0, 0 + } +}; + +static struct user_s g_users[MAX_USERS]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void linebuffer_push(char *buffer, size_t size, int *linepos, + char ch, void (*cb) (const char *line, int overflow, + void *ud), void *ud) +{ + /* CRLF -- line terminator */ + + if (ch == '\n' && *linepos > 0 && buffer[*linepos - 1] == '\r') + { + /* NUL terminate (replaces \r in buffer), notify app, clear */ + + buffer[*linepos - 1] = 0; + cb(buffer, 0, ud); + *linepos = 0; + + /* CRNUL -- just a CR */ + } + else if (ch == 0 && *linepos > 0 && buffer[*linepos - 1] == '\r') + { + /* So nothing, the CR is already in the buffer */ + + /* Anything else (including technically invalid CR followed by anything + * besides LF or NUL -- just buffer if we have room \r + */ + } + else if (*linepos != size) + { + buffer[(*linepos)++] = ch; + + /* Buffer overflow */ + } + else + { + /* Terminate (NOTE: eats a byte), notify app, clear buffer */ + + buffer[size - 1] = 0; + cb(buffer, size - 1, ud); + *linepos = 0; + } +} + +static void _message(const char *from, const char *msg) +{ + int i; + + for (i = 0; i != MAX_USERS; ++i) + { + if (g_users[i].sock != -1) + { + telnet_printf(g_users[i].telnet, "%s: %s\n", from, msg); + } + } +} + +static void _send(int sock, const char *buffer, unsigned int size) +{ + int ret; + + /* Ignore on invalid socket */ + + if (sock == -1) + { + return; + } + + /* Send data */ + + while (size > 0) + { + if ((ret = send(sock, buffer, size, 0)) == -1) + { + if (errno != EINTR && errno != ECONNRESET) + { + fprintf(stderr, "send() failed: %d\n", errno); + exit(1); + } + else + { + return; + } + } + else if (ret == 0) + { + fprintf(stderr, "send() unexpectedly returned 0\n"); + exit(1); + } + + /* Update pointer and size to see if we've got more to send */ + + buffer += ret; + size -= ret; + } +} + +/* Process input line */ + +static void _online(const char *line, int overflow, void *ud) +{ + struct user_s *user = (struct user_s *)ud; + int i; + + /* If the user has no name, this is his "login" */ + + if (user->name == 0) + { + /* Must not be empty, must be at least 32 chars */ + + if (strlen(line) == 0 || strlen(line) > 32) + { + telnet_printf(user->telnet, "Invalid name.\nEnter name: "); + return; + } + + /* Must not already be in use */ + + for (i = 0; i != MAX_USERS; ++i) + { + if (g_users[i].name != 0 && strcmp(g_users[i].name, line) == 0) + { + telnet_printf(user->telnet, "Name in use.\nEnter name: "); + return; + } + } + + /* Keep name */ + + user->name = strdup(line); + telnet_printf(user->telnet, "Welcome, %s!\n", line); + return; + } + + /* If line is "quit" then, well, quit */ + + if (strcmp(line, "quit") == 0) + { + close(user->sock); + user->sock = -1; + _message(user->name, "** HAS QUIT **"); + free(user->name); + user->name = 0; + return; + } + + /* Just a message -- send to all users */ + + _message(user->name, line); +} + +static void _input(struct user_s *user, const char *buffer, unsigned int size) +{ + unsigned int i; + + for (i = 0; i != size; ++i) + { + linebuffer_push(user->linebuf, sizeof(user->linebuf), &user->linepos, + (char)buffer[i], _online, user); + } +} + +static void _event_handler(struct telnet_s *telnet, + union telnet_event_u *ev, void *user_data) +{ + struct user_s *user = (struct user_s *)user_data; + + switch (ev->type) + { + /* Data received */ + + case TELNET_EV_DATA: + _input(user, ev->data.buffer, ev->data.size); + telnet_negotiate(telnet, TELNET_WONT, TELNET_TELOPT_ECHO); + telnet_negotiate(telnet, TELNET_WILL, TELNET_TELOPT_ECHO); + break; + + /* Data must be sent */ + + case TELNET_EV_SEND: + _send(user->sock, ev->data.buffer, ev->data.size); + break; + + /* Enable compress2 if accepted by client */ + + case TELNET_EV_DO: + if (ev->neg.telopt == TELNET_TELOPT_COMPRESS2) + { + telnet_begin_compress2(telnet); + } + break; + + /* Error */ + + case TELNET_EV_ERROR: + close(user->sock); + user->sock = -1; + if (user->name != 0) + { + _message(user->name, "** HAS HAD AN ERROR **"); + free(user->name); + user->name = 0; + } + + telnet_free(user->telnet); + break; + + default: + /* Ignore */ + + break; + } +} + +int main(int argc, char **argv) +{ + char buffer[512]; + short listen_port; + int listen_sock; + int ret; + int i; + struct sockaddr_in addr; + socklen_t addrlen; + struct pollfd pfd[MAX_USERS + 1]; + + /* Check usage */ + + if (argc != 2) + { + fprintf(stderr, "Usage:\n %s \n", argv[0]); + return 1; + } + + /* Initialize data structures */ + + memset(&pfd, 0, sizeof(pfd)); + memset(g_users, 0, sizeof(g_users)); + + for (i = 0; i != MAX_USERS; ++i) + { + g_users[i].sock = -1; + } + + /* Parse listening port */ + + listen_port = (short)strtol(argv[1], 0, 10); + + /* Create listening socket */ + + if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + fprintf(stderr, "socket() failed: %d\n", errno); + return 1; + } + + /* Reuse address option */ + + ret = 1; + setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (void *)&ret, sizeof(ret)); + + /* Bind to listening addr/port */ + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(listen_port); + if (bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) + { + fprintf(stderr, "bind() failed: %d\n", errno); + return 1; + } + + /* Listen for clients */ + + if (listen(listen_sock, 5) == -1) + { + fprintf(stderr, "listen() failed: %d\n", errno); + return 1; + } + + printf("LISTENING ON PORT %d\n", listen_port); + + /* Initialize listening descriptors */ + + pfd[MAX_USERS].fd = listen_sock; + pfd[MAX_USERS].events = POLLIN; + + /* Loop for ever */ + + for (;;) + { + /* Prepare for poll */ + + for (i = 0; i != MAX_USERS; ++i) + { + if (g_users[i].sock != -1) + { + pfd[i].fd = g_users[i].sock; + pfd[i].events = POLLIN; + } + else + { + pfd[i].fd = -1; + pfd[i].events = 0; + } + } + + /* Poll */ + + ret = poll(pfd, MAX_USERS + 1, -1); + if (ret == -1 && errno != EINTR) + { + fprintf(stderr, "poll() failed: %d\n", errno); + return 1; + } + + /* New connection */ + + if (pfd[MAX_USERS].revents & POLLIN) + { + /* Accept the sock */ + + addrlen = sizeof(addr); + if ((ret = accept(listen_sock, (struct sockaddr *)&addr, + &addrlen)) == -1) + { + fprintf(stderr, "accept() failed: %d\n", errno); + return 1; + } + + printf("Connection received.\n"); + + /* Find a free user */ + + for (i = 0; i != MAX_USERS; ++i) + { + if (g_users[i].sock == -1) + { + break; + } + } + + if (i == MAX_USERS) + { + printf(" rejected (too many users)\n"); + _send(ret, "Too many users.\r\n", 14); + close(ret); + } + + /* Init, welcome */ + + g_users[i].sock = ret; + g_users[i].telnet = telnet_init(g_telopts, _event_handler, 0, &g_users[i]); + telnet_negotiate(g_users[i].telnet, TELNET_WILL, + TELNET_TELOPT_COMPRESS2); + telnet_printf(g_users[i].telnet, "Enter name: "); + + telnet_negotiate(g_users[i].telnet, TELNET_WILL, TELNET_TELOPT_ECHO); + } + + /* Read from client */ + + for (i = 0; i != MAX_USERS; ++i) + { + /* Skip users that aren't actually connected */ + + if (g_users[i].sock == -1) + { + continue; + } + + if (pfd[i].revents & POLLIN) + { + if ((ret = recv(g_users[i].sock, buffer, sizeof(buffer), 0)) > 0) + { + telnet_recv(g_users[i].telnet, buffer, ret); + } + else if (ret == 0) + { + printf("Connection closed.\n"); + close(g_users[i].sock); + if (g_users[i].name != 0) + { + _message(g_users[i].name, "** HAS DISCONNECTED **"); + free(g_users[i].name); + g_users[i].name = 0; + } + + telnet_free(g_users[i].telnet); + g_users[i].sock = -1; + break; + } + else if (errno != EINTR) + { + fprintf(stderr, "recv(client) failed: %d\n", errno); + exit(1); + } + } + } + } + + /* Not that we can reach this, but GCC will cry if it's not here */ + + return 0; +} diff --git a/system/telnet/telnet_client.c b/system/telnet/telnet_client.c new file mode 100644 index 000000000..3e469fc25 --- /dev/null +++ b/system/telnet/telnet_client.c @@ -0,0 +1,425 @@ +/**************************************************************************** + * apps/system/telnet/telnet_client.c + * + * Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet. + * Modified and re-released under the BSD license: + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * The original authors of libtelnet are listed below. Per their licesne, + * "The author or authors of this code dedicate any and all copyright + * interest in this code to the public domain. We make this dedication for + * the benefit of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * code under copyright law." + * + * Author: Sean Middleditch + * (Also listed in the AUTHORS file are Jack Kelly + * and Katherine Flavel ) + * + * 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 +#include + +#include +#include + +#ifdef HAVE_ZLIB +# include "zlib.h" +#endif + +#include "system/readline.h" +#include "netutils/telnetc.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct telnet_s *g_telnet; +static int g_echo; + +static const struct telnet_telopt_s g_telopts[] = +{ + { + TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO + }, + { + TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT + }, + { + TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO + }, + { + TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO + }, + { + -1, 0, 0 + } +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void send_local_input(char *buffer, int size) +{ + static char crlf[] = { '\r', '\n' }; + int i; + + for (i = 0; i != size; ++i) + { + /* If we got a CR or LF, replace with CRLF NOTE that usually you'd get a + * CR in UNIX, but in raw mode we get LF instead (not sure why). + */ + + if (buffer[i] == '\r' || buffer[i] == '\n') + { + if (g_echo) + { + printf("\r\n"); + } + + telnet_send(g_telnet, crlf, 2); + } + else + { + if (g_echo) + { + putchar(buffer[i]); + } + + telnet_send(g_telnet, buffer + i, 1); + } + } + + fflush(stdout); +} + +static void telnet_ev_send(int sock, const char *buffer, size_t size) +{ + int ret; + + /* Send data */ + + while (size > 0) + { + if ((ret = send(sock, buffer, size, 0)) == -1) + { + fprintf(stderr, "send() failed: %d\n", errno); + exit(1); + } + else if (ret == 0) + { + fprintf(stderr, "send() unexpectedly returned 0\n"); + exit(1); + } + + /* Update pointer and size to see if we've got more to send */ + + buffer += ret; + size -= ret; + } +} + +static void _event_handler(struct telnet_s *telnet, + union telnet_event_u *ev, void *user_data) +{ + int sock = *(int *)user_data; + + switch (ev->type) + { + /* Data received */ + + case TELNET_EV_DATA: + printf("%.*s", (int)ev->data.size, ev->data.buffer); + fflush(stdout); + break; + + /* Data must be sent */ + + case TELNET_EV_SEND: + telnet_ev_send(sock, ev->data.buffer, ev->data.size); + break; + + /* Request to enable remote feature (or receipt) */ + + case TELNET_EV_WILL: + /* We'll agree to turn off our echo if server wants us to stop */ + + if (ev->neg.telopt == TELNET_TELOPT_ECHO) + { + g_echo = 0; + } + + break; + + /* Notification of disabling remote feature (or receipt) */ + + case TELNET_EV_WONT: + if (ev->neg.telopt == TELNET_TELOPT_ECHO) + { + g_echo = 1; + } + break; + + /* Request to enable local feature (or receipt) */ + + case TELNET_EV_DO: + break; + + /* Demand to disable local feature (or receipt) */ + + case TELNET_EV_DONT: + break; + + /* Respond to TTYPE commands */ + + case TELNET_EV_TTYPE: + /* Respond with our terminal type, if requested */ + + if (ev->ttype.cmd == TELNET_TTYPE_SEND) + { + telnet_ttype_is(telnet, getenv("TERM")); + } + break; + + /* Respond to particular subnegotiations */ + + case TELNET_EV_SUBNEGOTIATION: + break; + + /* Error */ + + case TELNET_EV_ERROR: + fprintf(stderr, "ERROR: %s\n", ev->error.msg); + exit(1); + + default: + /* Ignore */ + + break; + } +} + +static void show_usage(const char *progname, int exitcode) +{ + fprintf(stderr, "Usage:\n %s \n", progname); + exit(exitcode) ; +} + +int main(int argc, char **argv) +{ + char buffer[512]; + union + { + struct sockaddr generic; +#ifdef CONFIG_NET_IPv6 + struct sockaddr_in6 ipv6; +#endif +#ifdef CONFIG_NET_IPv4 + struct sockaddr_in ipv4; +#endif + } server; + union + { +#ifdef CONFIG_NET_IPv6 + struct sockaddr_in6 ipv6; +#endif +#ifdef CONFIG_NET_IPv4 + struct sockaddr_in ipv4; +#endif + } local; + struct pollfd pfd[2]; + sa_family_t family; + uint16_t addrlen; + int portno; + int sock; + int ret; + + /* Check usage */ + + if (argc != 3) + { + fprintf(stderr, "Invalid number of arguments\n"); + show_usage(argv[0], 1); + } + + /* Convert the port number to binary */ + + portno = atoi(argv[2]); + if (portno < 0 || portno > UINT16_MAX) + { + fprintf(stderr, "Invalid port number\n"); + show_usage(argv[0], 1); + } + + /* Convert the argument into a binary address */ + + memset(&local, 0, sizeof(local)); + +#ifdef CONFIG_EXAMPLES_NETTEST_IPv6 + addrlen = sizeof(struct sockaddr_in6); + family = AF_INET6; + + local.ipv6.sin6_family = AF_INET6; + server.ipv6.sin6_family = AF_INET6; + server.ipv6.sin6_port = htons(portno); + + ret = inet_pton(AF_INET6, argv[1], server.ipv6.sin6_addr.s6_addr); + if (ret < 0) +#endif +#ifdef CONFIG_EXAMPLES_NETTEST_IPv + { + addrlen = sizeof(struct sockaddr_in); + family = AF_INET; + + local.ipv4.sin_family = AF_INET; + server.ipv4.sin_family = AF_INET; + server.ipv4.sin_port = htons(portno); + + ret = inet_pton(AF_INET, argv[1], &server.ipv4.sin_addr); + } + + if (ret < 0) +#endif + { + fprintf(stderr, "ERROR: is invalid\n"); + show_usage(argv[0], 1); + } + + /* Create server socket */ + + sock = socket(family, SOCK_STREAM, 0); + if (sock < 0) + { + fprintf(stderr, "socket() failed: %d\n", errno); + return 1; + } + + /* Bind server socket */ + + ret = bind(sock, (struct sockaddr *)&local, addrlen); + if (ret < 0) + { + fprintf(stderr, "bind() failed: %d\n", errno); + return 1; + } + + /* Connect */ + + ret = connect(sock, &server.generic, addrlen); + if (ret < 0) + { + fprintf(stderr, "connect() failed: %d\n", errno); + return 1; + } + + /* Set input echoing on by default */ + + g_echo = 1; + + /* Initialize telnet box */ + + g_telnet = telnet_init(g_telopts, _event_handler, 0, &sock); + + /* Initialize poll descriptors */ + + memset(pfd, 0, sizeof(pfd)); + pfd[0].fd = 1; + pfd[0].events = POLLIN; + pfd[1].fd = sock; + pfd[1].events = POLLIN; + + /* Loop while both connections are open */ + + while (poll(pfd, 2, -1) != -1) + { + /* Read from stdin */ + + if (pfd[0].revents & POLLIN) + { + ret = std_readline(buffer, sizeof(buffer)); + if (ret > 0) + { + send_local_input(buffer, ret); + } + else if (ret == 0) + { + break; + } + else + { + fprintf(stderr, "recv(server) failed: %d\n", errno); + exit(1); + } + } + + /* Read from client */ + + if (pfd[1].revents & POLLIN) + { + if ((ret = recv(sock, buffer, sizeof(buffer), 0)) > 0) + { + telnet_recv(g_telnet, buffer, ret); + } + else if (ret == 0) + { + break; + } + else + { + fprintf(stderr, "recv(client) failed: %d\n", errno); + exit(1); + } + } + } + + /* Clean up */ + + telnet_free(g_telnet); + close(sock); + return 0; +} From 342de816416964998d48072580167951cee19879 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 18:00:13 -0600 Subject: [PATCH 19/23] NSH library: Add a Telnetd command. This is needed when network initialization is deferred. In that case, telnet daemon cannot be started until the network is finally initialized. The telnetd command was added just for that case: So that the telnet daemon can be started from the NSH command line after the network is configured. --- examples/nsh/nsh_main.c | 7 ++- nshlib/Kconfig | 5 ++ nshlib/README.txt | 16 ++++++ nshlib/nsh.h | 5 ++ nshlib/nsh_command.c | 4 ++ nshlib/nsh_telnetd.c | 114 ++++++++++++++++++++++++++++++++-------- 6 files changed, 126 insertions(+), 25 deletions(-) diff --git a/examples/nsh/nsh_main.c b/examples/nsh/nsh_main.c index 2a81d5425..f55160c58 100644 --- a/examples/nsh/nsh_main.c +++ b/examples/nsh/nsh_main.c @@ -153,10 +153,13 @@ int nsh_main(int argc, char *argv[]) nsh_initialize(); /* If the Telnet console is selected as a front-end, then start the - * Telnet daemon. + * Telnet daemon UNLESS network initialization is deferred via + * CONFIG_NSH_NETLOCAL. In that case, the telnet daemon must be + * started manually with the telnetd command after the network has + * been initialized */ -#ifdef CONFIG_NSH_TELNET +#if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_NETLOCAL) ret = nsh_telnetstart(); if (ret < 0) { diff --git a/nshlib/Kconfig b/nshlib/Kconfig index cabfd52b2..d5b37672b 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -483,6 +483,11 @@ config NSH_DISABLE_TEST bool "Disable test" default n +config NSH_DISABLE_TELNETD + bool "Disable telnetd" + default n if !NSH_NETLOCAL + default y if NSH_NETLOCAL + config NSH_DISABLE_UMOUNT bool "Disable umount" default n diff --git a/nshlib/README.txt b/nshlib/README.txt index 8edf053b7..0bf4a50f7 100644 --- a/nshlib/README.txt +++ b/nshlib/README.txt @@ -1080,6 +1080,21 @@ o sleep Pause execution (sleep) of seconds. +o telnetd + + The Telnet daemon may be started either programmatically by calling + nsh_telnetstart() or it may be started from the NSH command line using + this telnetd command. + + Normally this command would be suppressed with CONFIG_NSH_DISABLE_TELNETD + because the Telnet daemon is automatically started in nsh_main.c. The + exception is when CONFIG_NSH_NETLOCAL is selected. IN that case, the + network is not enabled at initialization but rather must be enabled from + the NSH command line or via other applications. + + In that case, calling nsh_telnetstart() before the the network is + initialized will fail. + o time "" Perform command timing. This command will execute the following @@ -1274,6 +1289,7 @@ Command Dependencies on Configuration Settings shutdown CONFIG_BOARDCTL_POWEROFF || CONFIG_BOARDCTL_RESET sleep !CONFIG_DISABLE_SIGNALS test !CONFIG_NSH_DISABLESCRIPT + telnetd CONFIG_NSH_TELNET && !CONFIG_NSH_DISABLE_TELNETD time --- umount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE uname !CONFIG_NSH_DISABLE_UNAME diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 73cfe9ec5..637a975cc 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -1187,6 +1187,11 @@ int cmd_lsmod(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # endif +# if defined(CONFIG_NSH_TELNET) +# ifndef CONFIG_NSH_DISABLE_TELNETD + int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +# endif +# endif #endif /* CONFIG_NET */ #if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \ diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 2d8b731d2..9bd8d36f5 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -472,6 +472,10 @@ static const struct cmdmap_s g_cmdmap[] = { "test", cmd_test, 3, CONFIG_NSH_MAXARGUMENTS, "" }, #endif +#if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_DISABLE_TELNETD) + {"telnetd", cmd_telnetd, 1, 1, NULL }, +#endif + #ifndef CONFIG_NSH_DISABLE_TIME { "time", cmd_time, 2, 2, "\"\"" }, #endif diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c index 74a9ab82d..e8fec92c1 100644 --- a/nshlib/nsh_telnetd.c +++ b/nshlib/nsh_telnetd.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/nshlib/nsh_telnetd.c * - * Copyright (C) 2007-2013, 2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2013, 2016-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -39,7 +39,9 @@ #include +#include #include +#include #include #include @@ -51,6 +53,23 @@ #ifdef CONFIG_NSH_TELNET +/**************************************************************************** + * Private Types + ****************************************************************************/ + +enum telnetd_state_e +{ + TELNETD_NOTRUNNING = 0, + TELNETD_STARTED, + TELNETD_RUNNING +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum telnetd_state_e g_telnetd_state; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -64,8 +83,9 @@ static int nsh_telnetmain(int argc, char *argv[]) FAR struct console_stdio_s *pstate = nsh_newconsole(); FAR struct nsh_vtbl_s *vtbl; - DEBUGASSERT(pstate != NULL); - vtbl = &pstate->cn_vtbl; + DEBUGASSERT(pstate != NULL && g_telnetd_state == TELNETD_STARTED); + vtbl = &pstate->cn_vtbl; + g_telnetd_state = TELNETD_RUNNING; _info("Session [%d] Started\n", getpid()); @@ -75,6 +95,7 @@ static int nsh_telnetmain(int argc, char *argv[]) if (nsh_telnetlogin(pstate) != OK) { nsh_exit(vtbl, 1); + g_telnetd_state = TELNETD_NOTRUNNING; return -1; /* nsh_exit does not return */ } #endif /* CONFIG_NSH_TELNET_LOGIN */ @@ -141,12 +162,14 @@ static int nsh_telnetmain(int argc, char *argv[]) { fprintf(pstate->cn_outstream, g_fmtcmdfailed, "nsh_telnetmain", "fgets", NSH_ERRNO); + g_telnetd_state = TELNETD_NOTRUNNING; nsh_exit(vtbl, 1); } } /* Clean up */ + g_telnetd_state = TELNETD_NOTRUNNING; nsh_exit(vtbl, 0); /* We do not get here, but this is necessary to keep some compilers happy */ @@ -178,37 +201,82 @@ static int nsh_telnetmain(int argc, char *argv[]) int nsh_telnetstart(void) { - struct telnetd_config_s config; - int ret; + int ret = OK; - /* Initialize any USB tracing options that were requested. If standard - * console is also defined, then we will defer this step to the standard - * console. - */ + if (g_telnetd_state == TELNETD_NOTRUNNING) + { + struct telnetd_config_s config; + + /* There is a tiny race condition here if two tasks were to try to + * start the Telnet daemon concurrently. + */ + + g_telnetd_state = TELNETD_STARTED; + + /* Initialize any USB tracing options that were requested. If + * standard console is also defined, then we will defer this step to + * the standard console. + */ #if defined(CONFIG_NSH_USBDEV_TRACE) && !defined(CONFIG_NSH_CONSOLE) - usbtrace_enable(TRACE_BITSET); + usbtrace_enable(TRACE_BITSET); #endif - /* Configure the telnet daemon */ + /* Configure the telnet daemon */ - config.d_port = HTONS(CONFIG_NSH_TELNETD_PORT); - config.d_priority = CONFIG_NSH_TELNETD_DAEMONPRIO; - config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE; - config.t_priority = CONFIG_NSH_TELNETD_CLIENTPRIO; - config.t_stacksize = CONFIG_NSH_TELNETD_CLIENTSTACKSIZE; - config.t_entry = nsh_telnetmain; + config.d_port = HTONS(CONFIG_NSH_TELNETD_PORT); + config.d_priority = CONFIG_NSH_TELNETD_DAEMONPRIO; + config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE; + config.t_priority = CONFIG_NSH_TELNETD_CLIENTPRIO; + config.t_stacksize = CONFIG_NSH_TELNETD_CLIENTSTACKSIZE; + config.t_entry = nsh_telnetmain; - /* Start the telnet daemon */ + /* Start the telnet daemon */ - _info("Starting the Telnet daemon\n"); - ret = telnetd_start(&config); - if (ret < 0) - { - _err("ERROR: Failed to tart the Telnet daemon: %d\n", ret); + _info("Starting the Telnet daemon\n"); + + ret = telnetd_start(&config); + if (ret < 0) + { + _err("ERROR: Failed to tart the Telnet daemon: %d\n", ret); + g_telnetd_state = TELNETD_NOTRUNNING; + } } return ret; } +/**************************************************************************** + * Name: cmd_telnetd + * + * Description: + * The Telnet daemon may be started either programmatically by calling + * nsh_telnetstart() or it may be started from the NSH command line using + * this telnetd command. + * + * Normally this command would be suppressed with CONFIG_NSH_DISABLE_TELNETD + * because the Telnet daemon is automatically started in nsh_main.c. The + * exception is when CONFIG_NSH_NETLOCAL is selected. IN that case, the + * network is not enabled at initialization but rather must be enabled from + * the NSH command line or via other applications. + * + * In that case, calling nsh_telnetstart() before the the network is + * initialized will fail. + * + * Input Parameters: + * None. All of the properties of the Telnet daemon are controlled by + * NuttX configuration setting. + * + * Returned Values: + * OK is returned on success; ERROR is return on failure. + * + ****************************************************************************/ + +#ifndef CONFIG_NSH_DISABLE_TELNETD +int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + return nsh_telnetstart() < 0 ? ERROR : OK; +} +#endif + #endif /* CONFIG_NSH_TELNET */ From 9091716419089d93e18123fbbb6e0d0b927eb773 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 26 Jun 2017 18:16:05 -0600 Subject: [PATCH 20/23] revise some of the state handling in the previous commit. --- nshlib/nsh_telnetd.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c index e8fec92c1..18722d8db 100644 --- a/nshlib/nsh_telnetd.c +++ b/nshlib/nsh_telnetd.c @@ -39,7 +39,6 @@ #include -#include #include #include #include @@ -64,12 +63,6 @@ enum telnetd_state_e TELNETD_RUNNING }; -/**************************************************************************** - * Private Data - ****************************************************************************/ - -static enum telnetd_state_e g_telnetd_state; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -83,9 +76,8 @@ static int nsh_telnetmain(int argc, char *argv[]) FAR struct console_stdio_s *pstate = nsh_newconsole(); FAR struct nsh_vtbl_s *vtbl; - DEBUGASSERT(pstate != NULL && g_telnetd_state == TELNETD_STARTED); - vtbl = &pstate->cn_vtbl; - g_telnetd_state = TELNETD_RUNNING; + DEBUGASSERT(pstate != NULL); + vtbl = &pstate->cn_vtbl; _info("Session [%d] Started\n", getpid()); @@ -95,7 +87,6 @@ static int nsh_telnetmain(int argc, char *argv[]) if (nsh_telnetlogin(pstate) != OK) { nsh_exit(vtbl, 1); - g_telnetd_state = TELNETD_NOTRUNNING; return -1; /* nsh_exit does not return */ } #endif /* CONFIG_NSH_TELNET_LOGIN */ @@ -162,14 +153,12 @@ static int nsh_telnetmain(int argc, char *argv[]) { fprintf(pstate->cn_outstream, g_fmtcmdfailed, "nsh_telnetmain", "fgets", NSH_ERRNO); - g_telnetd_state = TELNETD_NOTRUNNING; nsh_exit(vtbl, 1); } } /* Clean up */ - g_telnetd_state = TELNETD_NOTRUNNING; nsh_exit(vtbl, 0); /* We do not get here, but this is necessary to keep some compilers happy */ @@ -201,9 +190,10 @@ static int nsh_telnetmain(int argc, char *argv[]) int nsh_telnetstart(void) { + static enum telnetd_state_e state = TELNETD_NOTRUNNING; int ret = OK; - if (g_telnetd_state == TELNETD_NOTRUNNING) + if (state == TELNETD_NOTRUNNING) { struct telnetd_config_s config; @@ -211,7 +201,7 @@ int nsh_telnetstart(void) * start the Telnet daemon concurrently. */ - g_telnetd_state = TELNETD_STARTED; + state = TELNETD_STARTED; /* Initialize any USB tracing options that were requested. If * standard console is also defined, then we will defer this step to @@ -239,7 +229,11 @@ int nsh_telnetstart(void) if (ret < 0) { _err("ERROR: Failed to tart the Telnet daemon: %d\n", ret); - g_telnetd_state = TELNETD_NOTRUNNING; + state = TELNETD_NOTRUNNING; + } + else + { + state = TELNETD_RUNNING; } } From 98204bad94448c12fda8f5022a532cb8524d541d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 27 Jun 2017 07:46:16 -0600 Subject: [PATCH 21/23] system/telnet: POLL must not be disabled in configuration. Telnet client port argument is no optional. --- nshlib/nsh_telnetd.c | 2 +- system/telnet/Kconfig | 4 +-- system/telnet/telnet_chatd.c | 10 +++++++- system/telnet/telnet_client.c | 48 +++++++++++++++++++++++++++++------ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c index 18722d8db..2f290a204 100644 --- a/nshlib/nsh_telnetd.c +++ b/nshlib/nsh_telnetd.c @@ -229,7 +229,7 @@ int nsh_telnetstart(void) if (ret < 0) { _err("ERROR: Failed to tart the Telnet daemon: %d\n", ret); - state = TELNETD_NOTRUNNING; + state = TELNETD_NOTRUNNING; } else { diff --git a/system/telnet/Kconfig b/system/telnet/Kconfig index 51cb9f72e..7cc99de39 100644 --- a/system/telnet/Kconfig +++ b/system/telnet/Kconfig @@ -6,7 +6,7 @@ menuconfig SYSTEM_TELNET_CHATD bool "Telnet chat deamon" default n - depends on NET && NET_TCP + depends on NET && NET_TCP && !DISABLE_POLL select NETUTILS_TELNETC ---help--- Enable the Telnet Chat daemon. @@ -34,7 +34,7 @@ endif # SYSTEM_TELNET_CHATD menuconfig SYSTEM_TELNET_CLIENT bool "Telnet client" default n - depends on NET && NET_TCP + depends on NET && NET_TCP && !DISABLE_POLL select NETUTILS_TELNETC select SYSTEM_READLINE ---help--- diff --git a/system/telnet/telnet_chatd.c b/system/telnet/telnet_chatd.c index e7a560b87..423724a79 100644 --- a/system/telnet/telnet_chatd.c +++ b/system/telnet/telnet_chatd.c @@ -317,7 +317,15 @@ static void _event_handler(struct telnet_s *telnet, } } -int main(int argc, char **argv) +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int chatd_main(int argc, char *argv[]) +#endif { char buffer[512]; short listen_port; diff --git a/system/telnet/telnet_client.c b/system/telnet/telnet_client.c index 3e469fc25..41f41791b 100644 --- a/system/telnet/telnet_client.c +++ b/system/telnet/telnet_client.c @@ -74,6 +74,16 @@ #include "system/readline.h" #include "netutils/telnetc.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_NSH_TELNETD_PORT +# define DEFAULT_PORT CONFIG_NSH_TELNETD_PORT +#else +# define DEFAULT_PORT 23 +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -246,11 +256,26 @@ static void _event_handler(struct telnet_s *telnet, static void show_usage(const char *progname, int exitcode) { - fprintf(stderr, "Usage:\n %s \n", progname); + fprintf(stderr, "Usage:\n"); + fprintf(stderr, "\t%s []\n", progname); + fprintf(stderr, "Where:\n"); + fprintf(stderr, "\t is the address of the Telnet server. Either\n"); + fprintf(stderr, "\t\tIPv4 form: ddd.ddd.ddd.ddd\n"); + fprintf(stderr, "\t\tIPv6 form: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx\n"); + fprintf(stderr, "\t is the (optional) listening port of the Telnet server.\n"); + fprintf(stderr, "\t\tDefault: %u\n", DEFAULT_PORT); exit(exitcode) ; } -int main(int argc, char **argv) +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int telnet_main(int argc, char *argv[]) +#endif { char buffer[512]; union @@ -281,19 +306,26 @@ int main(int argc, char **argv) /* Check usage */ - if (argc != 3) + if (argc < 2 || argc > 3) { fprintf(stderr, "Invalid number of arguments\n"); show_usage(argv[0], 1); } - /* Convert the port number to binary */ + /* Convert the port number to binary if provided */ - portno = atoi(argv[2]); - if (portno < 0 || portno > UINT16_MAX) + if (argc == 3) { - fprintf(stderr, "Invalid port number\n"); - show_usage(argv[0], 1); + portno = atoi(argv[2]); + if (portno < 0 || portno > UINT16_MAX) + { + fprintf(stderr, "Invalid port number\n"); + show_usage(argv[0], 1); + } + } + else + { + portno = DEFAULT_PORT; } /* Convert the argument into a binary address */ From 28415d662be5dc604c8a36399c19b9f5499dff8e Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 27 Jun 2017 09:14:13 -0600 Subject: [PATCH 22/23] netutils/telnetd and users of telnetd: Add support for IPv6. --- examples/cc3000/shell.c | 3 +- examples/nsh/nsh_main.c | 12 +++++- examples/nxterm/nxterm_main.c | 22 ++++++++++- examples/telnetd/telnetd.c | 7 +--- include/netutils/telnetd.h | 20 ++++++---- include/nshlib/nshlib.h | 12 ++++-- netutils/telnetd/telnetd_daemon.c | 61 ++++++++++++++++++++++++------- nshlib/nsh_command.c | 4 ++ nshlib/nsh_telnetd.c | 28 ++++++++++++-- 9 files changed, 132 insertions(+), 37 deletions(-) diff --git a/examples/cc3000/shell.c b/examples/cc3000/shell.c index aa45973f3..a1d8deb9b 100644 --- a/examples/cc3000/shell.c +++ b/examples/cc3000/shell.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/examples/cc3000/shell.c * - * Copyright (C) 2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2013, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * This is a leverage of similar logic from uIP: @@ -217,6 +217,7 @@ int shell_main(int argc, char *argv[]) /* Configure the telnet daemon */ config.d_port = HTONS(23); + config.d_family = AF_INET; config.d_priority = CONFIG_EXAMPLES_TELNETD_DAEMONPRIO; config.d_stacksize = CONFIG_EXAMPLES_TELNETD_DAEMONSTACKSIZE; config.t_priority = CONFIG_EXAMPLES_TELNETD_CLIENTPRIO; diff --git a/examples/nsh/nsh_main.c b/examples/nsh/nsh_main.c index f55160c58..32ea17ae5 100644 --- a/examples/nsh/nsh_main.c +++ b/examples/nsh/nsh_main.c @@ -73,6 +73,16 @@ # undef CONFIG_NSH_TELNET #endif +/* If Telnet is used and both IPv6 and IPv4 are enabled, then we need to + * pick one. + */ + +#ifdef CONFIG_NET_IPv6 +# define ADDR_FAMILY AF_INET6 +#else +# define ADDR_FAMILY AF_INET +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -160,7 +170,7 @@ int nsh_main(int argc, char *argv[]) */ #if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_NETLOCAL) - ret = nsh_telnetstart(); + ret = nsh_telnetstart(ADDR_FAMILY); if (ret < 0) { /* The daemon is NOT running. Report the error then fail... diff --git a/examples/nxterm/nxterm_main.c b/examples/nxterm/nxterm_main.c index cd06a82b4..6f211135f 100644 --- a/examples/nxterm/nxterm_main.c +++ b/examples/nxterm/nxterm_main.c @@ -71,6 +71,26 @@ #include "nxterm_internal.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The NSH telnet console requires networking support (and TCP/IP) */ + +#ifndef CONFIG_NET +# undef CONFIG_NSH_TELNET +#endif + +/* If Telnet is used and both IPv6 and IPv4 are enabled, then we need to + * pick one. + */ + +#ifdef CONFIG_NET_IPv6 +# define ADDR_FAMILY AF_INET6 +#else +# define ADDR_FAMILY AF_INET +#endif + /**************************************************************************** * Public Data ****************************************************************************/ @@ -234,7 +254,7 @@ int nxterm_main(int argc, char **argv) */ #ifdef CONFIG_NSH_TELNET - ret = nsh_telnetstart(); + ret = nsh_telnetstart(ADDR_FAMILY); if (ret < 0) { /* The daemon is NOT running. Report the error then fail... diff --git a/examples/telnetd/telnetd.c b/examples/telnetd/telnetd.c index 5bb5a4c56..cc03c70f4 100644 --- a/examples/telnetd/telnetd.c +++ b/examples/telnetd/telnetd.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/telnetd/telnetd.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * This is a leverage of similar logic from uIP: @@ -53,10 +53,6 @@ #include "telnetd.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - /**************************************************************************** * Private Types ****************************************************************************/ @@ -247,6 +243,7 @@ int telnetd_main(int argc, char *argv[]) /* Configure the telnet daemon */ config.d_port = HTONS(23); + config.d_family = AF_INET; config.d_priority = CONFIG_EXAMPLES_TELNETD_DAEMONPRIO; config.d_stacksize = CONFIG_EXAMPLES_TELNETD_DAEMONSTACKSIZE; config.t_priority = CONFIG_EXAMPLES_TELNETD_CLIENTPRIO; diff --git a/include/netutils/telnetd.h b/include/netutils/telnetd.h index 1a4000d24..1e385d670 100644 --- a/include/netutils/telnetd.h +++ b/include/netutils/telnetd.h @@ -1,7 +1,7 @@ /**************************************************************************** * apps/include/netutils/telnetd.h * - * Copyright (C) 2007, 2011-2012, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2011-2012, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -61,18 +61,22 @@ struct telnetd_config_s { /* These fields describe the telnet daemon */ - int d_port; /* The port to listen on (in network byte order) */ - int d_priority; /* The execution priority of the Telnet daemon task */ - int d_stacksize; /* The stack size needed by the Telnet daemon task */ + uint8_t d_priority; /* The execution priority of the Telnet daemon task */ + size_t d_stacksize; /* The stack size needed by the Telnet daemon task */ + + /* These fields describe the network connection */ + + uint16_t d_port; /* The port to listen on (in network byte order) */ + sa_family_t d_family; /* Address family */ /* These fields describe the priority of each thread created by the Telnet * daemon. */ - int t_priority; /* The execution priority of the spawned task, */ - int t_stacksize; /* The stack size needed by the spawned task */ - main_t t_entry; /* The entrypoint of the task to spawn when a new - * connection is accepted. */ + uint8_t t_priority; /* The execution priority of the spawned task, */ + size_t t_stacksize; /* The stack size needed by the spawned task */ + main_t t_entry; /* The entrypoint of the task to spawn when a new + * connection is accepted. */ }; /**************************************************************************** diff --git a/include/nshlib/nshlib.h b/include/nshlib/nshlib.h index 109664956..6a8b07612 100644 --- a/include/nshlib/nshlib.h +++ b/include/nshlib/nshlib.h @@ -1,7 +1,7 @@ /**************************************************************************** * apps/include/nshlib/nshlib.h * - * Copyright (C) 2011, 2013, 2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2013, 2016-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -147,8 +147,12 @@ int nsh_consolemain(int argc, char *argv[]); * the daemon has been started. * * Input Parameters: - * None. All of the properties of the Telnet daemon are controlled by - * NuttX configuration setting. + * family - Provides the IP family to use by the server. May be either + * AF_INET or AF_INET6. This is needed because both both may be + * enabled in the configuration. + * + * All of the other properties of the Telnet daemon are controlled by + * NuttX configuration settings. * * Returned Values: * The task ID of the Telnet daemon was successfully started. A negated @@ -156,7 +160,7 @@ int nsh_consolemain(int argc, char *argv[]); * ****************************************************************************/ -int nsh_telnetstart(void); +int nsh_telnetstart(sa_family_t family); /**************************************************************************** * Name: platform_motd diff --git a/netutils/telnetd/telnetd_daemon.c b/netutils/telnetd/telnetd_daemon.c index 2e140b712..c8aabf263 100644 --- a/netutils/telnetd/telnetd_daemon.c +++ b/netutils/telnetd/telnetd_daemon.c @@ -1,7 +1,7 @@ /**************************************************************************** * netutils/telnetd/telnetd_daemon.c * - * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -72,9 +73,10 @@ struct telnetd_s { - int port; /* The port to listen on (in network byte order) */ - int priority; /* The execution priority of the spawned task, */ - int stacksize; /* The stack size needed by the spawned task */ + uint16_t port; /* The port to listen on (in network byte order) */ + sa_family_t family; /* Address family */ + uint8_t priority; /* The execution priority of the spawned task, */ + size_t stacksize; /* The stack size needed by the spawned task */ main_t entry; /* The entrypoint of the task to spawn when a new * connection is accepted. */ }; @@ -126,7 +128,16 @@ static struct telnetd_common_s g_telnetdcommon; static int telnetd_daemon(int argc, char *argv[]) { FAR struct telnetd_s *daemon; - struct sockaddr_in myaddr; + union + { + struct sockaddr generic; +#ifdef CONFIG_NET_IPv4 + struct sockaddr_in ipv4; +#endif +#ifdef CONFIG_NET_IPv6 + struct sockaddr_in6 ipv6; +#endif + } addr; struct telnet_session_s session; #ifdef CONFIG_NET_SOLINGER struct linger ling; @@ -187,11 +198,12 @@ static int telnetd_daemon(int argc, char *argv[]) /* Create a new TCP socket to use to listen for connections */ - listensd = socket(PF_INET, SOCK_STREAM, 0); + listensd = socket(daemon->family, SOCK_STREAM, 0); if (listensd < 0) { int errval = errno; - nerr("ERROR: socket failure: %d\n", errval); + nerr("ERROR: socket() failed for family %u: %d\n", + daemon->family, errval); return -errval; } @@ -208,11 +220,33 @@ static int telnetd_daemon(int argc, char *argv[]) /* Bind the socket to a local address */ - myaddr.sin_family = AF_INET; - myaddr.sin_port = daemon->port; - myaddr.sin_addr.s_addr = INADDR_ANY; +#ifdef CONFIG_NET_IPv4 + if (daemon->family == AF_INET) + { + addr.ipv4.sin_family = AF_INET; + addr.ipv4.sin_port = daemon->port; + addr.ipv4.sin_addr.s_addr = INADDR_ANY; + addrlen = sizeof(struct sockaddr_in); + } + else +#endif +#ifdef CONFIG_NET_IPv6 + if (daemon->family == AF_INET6) + { + addr.ipv6.sin6_family = AF_INET6; + addr.ipv6.sin6_port = daemon->port; + addrlen = sizeof(struct sockaddr_in6); - if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0) + memset(addr.ipv6.sin6_addr.s6_addr, 0, addrlen); + } + else +#endif + { + nerr("ERROR: Unsupported address family: %u", daemon->family); + goto errout_with_socket; + } + + if (bind(listensd, &addr.generic, addrlen) < 0) { nerr("ERROR: bind failure: %d\n", errno); goto errout_with_socket; @@ -240,8 +274,8 @@ static int telnetd_daemon(int argc, char *argv[]) { ninfo("Accepting connections on port %d\n", ntohs(daemon->port)); - addrlen = sizeof(struct sockaddr_in); - acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen); + socklen_t accptlen = sizeof(addr); + acceptsd = accept(listensd, &addr.generic, &accptlen); if (acceptsd < 0) { /* Accept failed */ @@ -390,6 +424,7 @@ int telnetd_start(FAR struct telnetd_config_s *config) /* Initialize the daemon structure */ daemon->port = config->d_port; + daemon->family = config->d_family; daemon->priority = config->t_priority; daemon->stacksize = config->t_stacksize; daemon->entry = config->t_entry; diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 9bd8d36f5..b4e334e0f 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -473,8 +473,12 @@ static const struct cmdmap_s g_cmdmap[] = #endif #if defined(CONFIG_NSH_TELNET) && !defined(CONFIG_NSH_DISABLE_TELNETD) +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + {"telnetd", cmd_telnetd, 2, 2, [ipv4|ipv6] }, +#else {"telnetd", cmd_telnetd, 1, 1, NULL }, #endif +#endif #ifndef CONFIG_NSH_DISABLE_TIME { "time", cmd_time, 2, 2, "\"\"" }, diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c index 2f290a204..ae65dda89 100644 --- a/nshlib/nsh_telnetd.c +++ b/nshlib/nsh_telnetd.c @@ -40,6 +40,7 @@ #include #include +#include #include #include @@ -179,8 +180,12 @@ static int nsh_telnetmain(int argc, char *argv[]) * the daemon has been started. * * Input Parameters: - * None. All of the properties of the Telnet daemon are controlled by - * NuttX configuration setting. + * family - Provides the IP family to use by the server. May be either + * AF_INET or AF_INET6. This is needed because both both may be + * enabled in the configuration. + * + * All of the other properties of the Telnet daemon are controlled by + * NuttX configuration settings. * * Returned Values: * The task ID of the Telnet daemon was successfully started. A negated @@ -188,7 +193,7 @@ static int nsh_telnetmain(int argc, char *argv[]) * ****************************************************************************/ -int nsh_telnetstart(void) +int nsh_telnetstart(sa_family_t family) { static enum telnetd_state_e state = TELNETD_NOTRUNNING; int ret = OK; @@ -215,6 +220,7 @@ int nsh_telnetstart(void) /* Configure the telnet daemon */ config.d_port = HTONS(CONFIG_NSH_TELNETD_PORT); + config.d_family = family; config.d_priority = CONFIG_NSH_TELNETD_DAEMONPRIO; config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE; config.t_priority = CONFIG_NSH_TELNETD_CLIENTPRIO; @@ -269,7 +275,21 @@ int nsh_telnetstart(void) #ifndef CONFIG_NSH_DISABLE_TELNETD int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { - return nsh_telnetstart() < 0 ? ERROR : OK; + sa_family_t family; + + /* If both IPv6 nd IPv4 are enabled, then the address family must + * be specified on the command line. + */ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) + family = (strcmp(argv[1], "ipv6") == 0) ? AF_INET6 : AF_INET; +#elif defined(CONFIG_NET_IPv6) + family = AF_INET6; +#else /* if defined(CONFIG_NET_IPv4) */ + family = AF_INET; +#endif + + return nsh_telnetstart(family) < 0 ? ERROR : OK; } #endif From 680cb2f1fc2c0b57ea12569aa087ffcbd0591a7e Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Tue, 27 Jun 2017 18:37:47 -0400 Subject: [PATCH 23/23] ieee802154/i8sak: Adds coordinfo command allowing you to poll various attributes related to the coordinator --- include/wireless/ieee802154.h | 4 + wireless/ieee802154/i8sak/Makefile | 2 +- wireless/ieee802154/i8sak/i8sak.h | 10 +- wireless/ieee802154/i8sak/i8sak_coordinfo.c | 141 ++++++++++++++++++ wireless/ieee802154/i8sak/i8sak_main.c | 4 +- wireless/ieee802154/i8sak/i8sak_scan.c | 14 +- wireless/ieee802154/libmac/Makefile | 5 +- .../libmac/ieee802154_getcoordeaddr.c | 67 +++++++++ .../libmac/ieee802154_getcoordsaddr.c | 66 ++++++++ 9 files changed, 297 insertions(+), 16 deletions(-) create mode 100644 wireless/ieee802154/i8sak/i8sak_coordinfo.c create mode 100644 wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c create mode 100644 wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c diff --git a/include/wireless/ieee802154.h b/include/wireless/ieee802154.h index 276c90ad6..da84dcfcd 100644 --- a/include/wireless/ieee802154.h +++ b/include/wireless/ieee802154.h @@ -98,6 +98,10 @@ int ieee802154_getsaddr(int fd, FAR uint8_t *saddr); int ieee802154_seteaddr(int fd, FAR const uint8_t *eaddr); int ieee802154_geteaddr(int fd, FAR uint8_t *eaddr); +int ieee802154_getcoordsaddr(int fd, FAR uint8_t *saddr); + +int ieee802154_getcoordeaddr(int fd, FAR uint8_t *eaddr); + int ieee802154_setpromisc(int fd, bool promisc); int ieee802154_getpromisc(int fd, FAR bool *promisc); diff --git a/wireless/ieee802154/i8sak/Makefile b/wireless/ieee802154/i8sak/Makefile index cef46ecfe..d24344887 100644 --- a/wireless/ieee802154/i8sak/Makefile +++ b/wireless/ieee802154/i8sak/Makefile @@ -48,7 +48,7 @@ STACKSIZE = 4096 ASRCS = CSRCS = i8sak_acceptassoc.c i8sak_assoc.c i8sak_scan.c i8sak_blaster.c i8sak_poll.c -CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c i8sak_chan.c +CSRCS += i8sak_sniffer.c i8sak_startpan.c i8sak_tx.c i8sak_chan.c i8sak_coordinfo.c CSRCS += wpanlistener.c MAINSRC = i8sak_main.c diff --git a/wireless/ieee802154/i8sak/i8sak.h b/wireless/ieee802154/i8sak/i8sak.h index 0d6d1395a..cb1eeb3e5 100644 --- a/wireless/ieee802154/i8sak/i8sak.h +++ b/wireless/ieee802154/i8sak/i8sak.h @@ -99,7 +99,14 @@ /* /dev/ is 5 characters */ #define I8SAK_DAEMONNAME_FMTLEN (6 + (I8SAK_MAX_DEVNAME-5) + 1) +/* Helper Macros *************************************************************/ +#define PRINT_COORDEADDR(eaddr) \ + printf(" Coordinator EADDR: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", \ + eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5], eaddr[6], eaddr[7]); + +#define PRINT_COORDSADDR(saddr) \ + printf(" Coordinator SADDR: %02X:%02X\n", saddr[0], saddr[1]); /**************************************************************************** * Public Types @@ -183,7 +190,8 @@ void i8sak_tx_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[ void i8sak_poll_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_sniffer_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); void i8sak_blaster_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); -void i8sak_chan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); +void i8sak_chan_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); +void i8sak_coordinfo_cmd (FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]); /**************************************************************************** * Inline Functions diff --git a/wireless/ieee802154/i8sak/i8sak_coordinfo.c b/wireless/ieee802154/i8sak/i8sak_coordinfo.c new file mode 100644 index 000000000..e359b3228 --- /dev/null +++ b/wireless/ieee802154/i8sak/i8sak_coordinfo.c @@ -0,0 +1,141 @@ +/**************************************************************************** + * apps/wireless/ieee802154/i8sak/i8sak_coordinfo.c + * IEEE 802.15.4 Swiss Army Knife + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 "i8sak.h" + +#include +#include +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name : i8sak_coordinfo_cmd + * + * Description : + * Print various coordinator attributes + ****************************************************************************/ + +void i8sak_coordinfo_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]) +{ + int option; + int fd; + int argind; + bool geteaddr = false; + bool getsaddr = false; + uint8_t eaddr[IEEE802154_EADDRSIZE]; + uint8_t saddr[IEEE802154_SADDRSIZE]; + + argind = 1; + while ((option = getopt(argc, argv, ":haes")) != ERROR) + { + switch (option) + { + argind++; + case 'h': + fprintf(stderr, "Prints coordinator attributes\n" + "Usage: %s [-h]\n" + " -h = this help menu\n" + , argv[0]); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + return; + + case 'a': + geteaddr = true; + getsaddr = true; + break; + + case 'e': + geteaddr = true; + break; + + case 's': + getsaddr = true; + break; + + case ':': + fprintf(stderr, "ERROR: missing argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + case '?': + fprintf(stderr, "ERROR: unknown argument\n"); + /* Must manually reset optind if we are going to exit early */ + + optind = -1; + i8sak_cmd_error(i8sak); /* This exits for us */ + } + } + + fd = open(i8sak->devname, O_RDWR); + if (fd < 0) + { + printf("cannot open %s, errno=%d\n", i8sak->devname, errno); + i8sak_cmd_error(i8sak); + } + + if (geteaddr) + { + ieee802154_getcoordeaddr(fd, eaddr); + PRINT_COORDEADDR(eaddr); + } + + if (getsaddr) + { + ieee802154_getcoordsaddr(fd, saddr); + PRINT_COORDSADDR(saddr); + } + + close(fd); +} diff --git a/wireless/ieee802154/i8sak/i8sak_main.c b/wireless/ieee802154/i8sak/i8sak_main.c index 0e15fe48e..4d74de876 100644 --- a/wireless/ieee802154/i8sak/i8sak_main.c +++ b/wireless/ieee802154/i8sak/i8sak_main.c @@ -110,6 +110,7 @@ static const struct i8sak_command_s g_i8sak_commands[] = {"sniffer", (CODE void *)i8sak_sniffer_cmd}, {"blaster", (CODE void *)i8sak_blaster_cmd}, {"chan", (CODE void *)i8sak_chan_cmd}, + {"coordinfo", (CODE void *)i8sak_coordinfo_cmd}, }; #define NCOMMANDS (sizeof(g_i8sak_commands) / sizeof(struct i8sak_command_s)) @@ -738,7 +739,8 @@ static int i8sak_showusage(FAR const char *progname, int exitcode) " poll [-h]\n" " blaster [-h|q|f |p ]\n" " sniffer [-h|q]\n" - " chan [-h|g] []" + " chan [-h|g] []\n" + " coordinfo [-h|a|e|s]\n" , progname); exit(exitcode); } diff --git a/wireless/ieee802154/i8sak/i8sak_scan.c b/wireless/ieee802154/i8sak/i8sak_scan.c index afacc60a9..069949f33 100644 --- a/wireless/ieee802154/i8sak/i8sak_scan.c +++ b/wireless/ieee802154/i8sak/i8sak_scan.c @@ -233,21 +233,11 @@ static void scan_eventcb(FAR struct ieee802154_notif_s *notif, FAR void *arg) if (scan->pandescs[i].coordaddr.mode == IEEE802154_ADDRMODE_SHORT) { - printf(" Coordinator saddr: %02X:%02X\n", - scan->pandescs[i].coordaddr.saddr[0], - scan->pandescs[i].coordaddr.saddr[1]); + PRINT_COORDSADDR(scan->pandescs[i].coordaddr.saddr); } else { - printf(" Coordinator eaddr: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", - scan->pandescs[i].coordaddr.eaddr[0], - scan->pandescs[i].coordaddr.eaddr[1], - scan->pandescs[i].coordaddr.eaddr[2], - scan->pandescs[i].coordaddr.eaddr[3], - scan->pandescs[i].coordaddr.eaddr[4], - scan->pandescs[i].coordaddr.eaddr[5], - scan->pandescs[i].coordaddr.eaddr[6], - scan->pandescs[i].coordaddr.eaddr[7]); + PRINT_COORDEADDR(scan->pandescs[i].coordaddr.eaddr); } } } diff --git a/wireless/ieee802154/libmac/Makefile b/wireless/ieee802154/libmac/Makefile index 76c817573..11014cfc3 100644 --- a/wireless/ieee802154/libmac/Makefile +++ b/wireless/ieee802154/libmac/Makefile @@ -54,7 +54,10 @@ CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c -CSRCS += ieee802154_getdevmode.c ieee802154_setassocpermit.c +CSRCS += ieee802154_getdevmode.c +CSRCS += ieee802154_setassocpermit.c +CSRCS += ieee802154_getcoordsaddr.c +CSRCS += ieee802154_getcoordeaddr.c ifeq ($(CONFIG_NET_6LOWPAN),y) # Add Get/Set Attribute helpers diff --git a/wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c b/wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c new file mode 100644 index 000000000..31e5e3097 --- /dev/null +++ b/wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c @@ -0,0 +1,67 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/ieee802154_getcoordeaddr.c + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ieee802154_getcoordeaddr(int fd, FAR uint8_t *eaddr) +{ + struct ieee802154_get_req_s req; + int ret; + + req.attr = IEEE802154_ATTR_MAC_COORD_EADDR; + ret = ieee802154_get_req(fd, &req); + + IEEE802154_EADDRCOPY(eaddr, req.attrval.mac.eaddr); + + return ret; +} diff --git a/wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c b/wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c new file mode 100644 index 000000000..81e4bce76 --- /dev/null +++ b/wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c @@ -0,0 +1,66 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/ieee802154_getcoordsaddr.c + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ieee802154_getcoordsaddr(int fd, FAR uint8_t *saddr) +{ + struct ieee802154_get_req_s req; + int ret; + + req.attr = IEEE802154_ATTR_MAC_COORD_SADDR; + ret = ieee802154_get_req(fd, &req); + + IEEE802154_SADDRCOPY(saddr, req.attrval.mac.saddr); + return ret; +}