You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/03 13:17:36 UTC

[incubator-nuttx-apps] 06/06: telnetd should listen both IPv4 and IPv6 for the dual stack

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr45
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 37135e5dfe7da4ad11add0dbb11c3b6838ce5e20
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Feb 3 17:50:16 2020 +0800

    telnetd should listen both IPv4 and IPv6 for the dual stack
    
    Change-Id: Ic1c2878f2eda721ccdf667b0a634289c643f5220
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 examples/nxterm/nxterm_main.c    | 12 +---------
 graphics/nxwm/src/cnxterm.cxx    | 11 +--------
 graphics/twm4nx/apps/cnxterm.cxx | 12 +---------
 nshlib/nsh_telnetd.c             | 50 +++++++++++++++++++++++++++++-----------
 system/nsh/nsh_main.c            | 12 +---------
 5 files changed, 41 insertions(+), 56 deletions(-)

diff --git a/examples/nxterm/nxterm_main.c b/examples/nxterm/nxterm_main.c
index db973ad..578836d 100644
--- a/examples/nxterm/nxterm_main.c
+++ b/examples/nxterm/nxterm_main.c
@@ -78,16 +78,6 @@
 #  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
  ****************************************************************************/
@@ -255,7 +245,7 @@ int main(int argc, FAR char *argv[])
    */
 
 #ifdef CONFIG_NSH_TELNET
-  ret = nsh_telnetstart(ADDR_FAMILY);
+  ret = nsh_telnetstart(AF_UNSPEC);
   if (ret < 0)
     {
      /* The daemon is NOT running.  Report the error then fail...
diff --git a/graphics/nxwm/src/cnxterm.cxx b/graphics/nxwm/src/cnxterm.cxx
index 9247e3b..a20fb01 100644
--- a/graphics/nxwm/src/cnxterm.cxx
+++ b/graphics/nxwm/src/cnxterm.cxx
@@ -68,15 +68,6 @@
 #  warning You probably do not really want CONFIG_NSH_USBKBD, try CONFIG_NXWM_KEYBOARD_USBHOST
 #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
  ********************************************************************************************/
@@ -694,7 +685,7 @@ bool NxWM::nshlibInitialize(void)
   // Telnet daemon.
 
 #ifdef CONFIG_NSH_TELNET
-  int ret = nsh_telnetstart(ADDR_FAMILY);
+  int ret = nsh_telnetstart(AF_UNSPEC);
   if (ret < 0)
     {
       // The daemon is NOT running!
diff --git a/graphics/twm4nx/apps/cnxterm.cxx b/graphics/twm4nx/apps/cnxterm.cxx
index 7a4863a..74e83dd 100644
--- a/graphics/twm4nx/apps/cnxterm.cxx
+++ b/graphics/twm4nx/apps/cnxterm.cxx
@@ -75,16 +75,6 @@
 #  warning You probably do not really want CONFIG_NSH_USBKBD, try CONFIG_TWM4NX_KEYBOARD_USBHOST
 #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
 /////////////////////////////////////////////////////////////////////////////
@@ -617,7 +607,7 @@ bool CNxTermFactory::nshlibInitialize(void)
   // Telnet daemon.
 
 #ifdef CONFIG_NSH_TELNET
-  int ret = nsh_telnetstart(ADDR_FAMILY);
+  int ret = nsh_telnetstart(AF_UNSPEC);
   if (ret < 0)
     {
       // The daemon is NOT running!
diff --git a/nshlib/nsh_telnetd.c b/nshlib/nsh_telnetd.c
index e82f6ad..e8f9f19 100644
--- a/nshlib/nsh_telnetd.c
+++ b/nshlib/nsh_telnetd.c
@@ -269,7 +269,6 @@ int nsh_telnetstart(sa_family_t family)
       /* 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;
@@ -280,15 +279,41 @@ int nsh_telnetstart(sa_family_t family)
 
       ninfo("Starting the Telnet daemon\n");
 
-      ret = telnetd_start(&config);
-      if (ret < 0)
+#ifdef CONFIG_NET_IPv4
+      if (family == AF_UNSPEC || family == AF_INET)
         {
-          _err("ERROR: Failed to start the Telnet daemon: %d\n", ret);
-          state = TELNETD_NOTRUNNING;
+          config.d_family = AF_INET;
+          ret = telnetd_start(&config);
+          if (ret < 0)
+            {
+              _err("ERROR: Failed to start the Telnet IPv4 daemon: %d\n", ret);
+            }
+          else
+            {
+              state = TELNETD_RUNNING;
+            }
         }
-      else
+#endif
+
+#ifdef CONFIG_NET_IPv6
+      if (family == AF_UNSPEC || family == AF_INET6)
         {
-          state = TELNETD_RUNNING;
+          config.d_family = AF_INET6;
+          ret = telnetd_start(&config);
+          if (ret < 0)
+            {
+              _err("ERROR: Failed to start the Telnet IPv6 daemon: %d\n", ret);
+            }
+          else
+            {
+              state = TELNETD_RUNNING;
+            }
+        }
+#endif
+
+      if (state == TELNETD_STARTED)
+        {
+          state = TELNETD_NOTRUNNING;
         }
     }
 
@@ -324,18 +349,17 @@ int nsh_telnetstart(sa_family_t family)
 #ifndef CONFIG_NSH_DISABLE_TELNETD
 int cmd_telnetd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
 {
-  sa_family_t family;
+  sa_family_t family = AF_UNSPEC;
 
   /* 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;
+  if (argc >= 2)
+    {
+      family = (strcmp(argv[1], "ipv6") == 0) ? AF_INET6 : AF_INET;
+    }
 #endif
 
   return nsh_telnetstart(family) < 0 ? ERROR : OK;
diff --git a/system/nsh/nsh_main.c b/system/nsh/nsh_main.c
index c888dbe..eeb9cb9 100644
--- a/system/nsh/nsh_main.c
+++ b/system/nsh/nsh_main.c
@@ -103,16 +103,6 @@
 #  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 Data
  ****************************************************************************/
@@ -173,7 +163,7 @@ static int nsh_task(void)
    * been initialized
    */
 
-  ret = nsh_telnetstart(ADDR_FAMILY);
+  ret = nsh_telnetstart(AF_UNSPEC);
   if (ret < 0)
     {
      /* The daemon is NOT running.  Report the error then fail...