Browse Source

ssh: listen on a new port after the current connection is closed

Sergey Alirzaev 5 years ago
parent
commit
b303ecf39d
2 changed files with 48 additions and 43 deletions
  1. 47 43
      modules/SSH_Server/server.c
  2. 1 0
      modules/settings_api.h

+ 47 - 43
modules/SSH_Server/server.c

@@ -28,6 +28,7 @@
 #include "wolfssh_test.h"
 #include "cli.h"
 #include "CLI_Commands.h"
+#include "settings_api.h"
 
 #ifdef WOLFSSL_USER_SETTINGS
     #include <wolfssl/wolfcrypt/settings.h>
@@ -295,8 +296,7 @@ static void ssh_server(void *arg)
     SOCKET_T listenFd = 0;
     word32 defaultHighwater = EXAMPLE_HIGHWATER_MARK;
     word32 threadCount = 0;
-    word16 port = 22;
-    const char multipleConnections = 1;
+    const char multipleConnections = 0;
     char useEcc = 1;
     char nonBlock = 0;
 
@@ -330,61 +330,65 @@ static void ssh_server(void *arg)
         }
     }
 
-    tcp_listen(&listenFd, &port, 1, false, false);
+    while (sSettings.sSSH.SSHEnable) {
+        word16 port = sSettings.sSSH.port;
+        tcp_listen(&listenFd, &port, 1, false, false);
 
-    do {
-        SOCKET_T      clientFd = 0;
-        SOCKADDR_IN_T clientAddr;
-        socklen_t     clientAddrSz = sizeof(clientAddr);
+        do {
+            SOCKET_T      clientFd = 0;
+            SOCKADDR_IN_T clientAddr;
+            socklen_t     clientAddrSz = sizeof(clientAddr);
 #ifndef SINGLE_THREADED
-        THREAD_TYPE   thread;
+            THREAD_TYPE   thread;
 #endif
-        WOLFSSH*      ssh;
-        thread_ctx_t* threadCtx;
+            WOLFSSH*      ssh;
+            thread_ctx_t* threadCtx;
 
-        threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
-        if (threadCtx == NULL) {
-            printf("Couldn't allocate thread context data.\n");
-            exit(EXIT_FAILURE);
-        }
+            threadCtx = (thread_ctx_t*)malloc(sizeof(thread_ctx_t));
+            if (threadCtx == NULL) {
+                printf("Couldn't allocate thread context data.\n");
+                exit(EXIT_FAILURE);
+            }
 
-        ssh = wolfSSH_new(ctx);
-        if (ssh == NULL) {
-            printf("Couldn't allocate SSH data.\n");
-            exit(EXIT_FAILURE);
-        }
-        /* Use the session object for its own highwater callback ctx */
-        if (defaultHighwater > 0) {
-            wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
-            wolfSSH_SetHighwater(ssh, defaultHighwater);
-        }
+            ssh = wolfSSH_new(ctx);
+            if (ssh == NULL) {
+                printf("Couldn't allocate SSH data.\n");
+                exit(EXIT_FAILURE);
+            }
+            /* Use the session object for its own highwater callback ctx */
+            if (defaultHighwater > 0) {
+                wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
+                wolfSSH_SetHighwater(ssh, defaultHighwater);
+            }
 
-        clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
-                                                                 &clientAddrSz);
-        if (clientFd == -1)
-            err_sys("tcp accept failed");
+            clientFd = accept(listenFd, (struct sockaddr*)&clientAddr,
+                                                                     &clientAddrSz);
+            if (clientFd == -1)
+                err_sys("tcp accept failed");
 
-        if (nonBlock)
-            tcp_set_nonblocking(&clientFd);
+            if (nonBlock)
+                tcp_set_nonblocking(&clientFd);
 
-        wolfSSH_set_fd(ssh, (int)clientFd);
+            wolfSSH_set_fd(ssh, (int)clientFd);
 
-        threadCtx->ssh = ssh;
-        threadCtx->fd = clientFd;
-        threadCtx->id = threadCount++;
-        threadCtx->nonBlock = nonBlock;
+            threadCtx->ssh = ssh;
+            threadCtx->fd = clientFd;
+            threadCtx->id = threadCount++;
+            threadCtx->nonBlock = nonBlock;
 
 #ifndef SINGLE_THREADED
-        ThreadStart(server_worker, threadCtx, &thread);
+            ThreadStart(server_worker, threadCtx, &thread);
 
-        if (multipleConnections)
-            ThreadDetach(thread);
-        else
-            ThreadJoin(thread);
+            if (multipleConnections)
+                ThreadDetach(thread);
+            else
+                ThreadJoin(thread);
 #else
-        server_worker(threadCtx);
+            server_worker(threadCtx);
 #endif /* SINGLE_THREADED */
-    } while (multipleConnections);
+        } while (multipleConnections);
+        WCLOSESOCKET(listenFd);
+    }
 
     wolfSSH_CTX_free(ctx);
     if (wolfSSH_Cleanup() != WS_SUCCESS) {

+ 1 - 0
modules/settings_api.h

@@ -362,6 +362,7 @@ typedef struct
   
 } SETTINGS_t;
 
+extern SETTINGS_t sSettings;
 
 /**
   * @brief