Explorar el Código

ssh: notify on exceeding the max amount of established connections

Sergey Alirzaev hace 5 años
padre
commit
60dc49c6d9
Se han modificado 4 ficheros con 33 adiciones y 26 borrados
  1. 28 24
      modules/SSH_Server/server.c
  2. 1 2
      modules/Telnet_Server/telnet_server.c
  3. 2 0
      modules/cli/cli.c
  4. 2 0
      modules/cli/cli.h

+ 28 - 24
modules/SSH_Server/server.c

@@ -145,33 +145,37 @@ static void *server_worker(void* vArgs)
 
     cli_state_t *cli_state;
     // create the new CLI context
-    if (ret == WS_SUCCESS && (cli_state = alloc_state())) {
-        cli_state->num_connect = threadCtx->ssh;
-        cli_state->input_state = CLI_CMD;
-        cli_state->send = cli_send;
-        cli_hello(cli_state);
-
-        bool stop = false;
-        do {
-            uint8_t buf[EXAMPLE_BUFFER_SZ];
-            int rxSz = 0;
+    if (ret == WS_SUCCESS) {
+        if ((cli_state = alloc_state())) {
+            cli_state->num_connect = threadCtx->ssh;
+            cli_state->input_state = CLI_CMD;
+            cli_state->send = cli_send;
+            cli_hello(cli_state);
+
+            bool stop = false;
             do {
-                rxSz = wolfSSH_stream_read(threadCtx->ssh, buf, sizeof(buf));
-                if (rxSz <= 0) {
-                    rxSz = wolfSSH_get_error(threadCtx->ssh);
-                }
-            } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
-
-            if (rxSz > 0) {
-                cli_getchar(cli_state, buf[0]);    // TODO handle rxSz > 1
-                if (buf[0] == 3 || buf[0] == 4 || cli_state->state == STATE_CLOSE) {
+                uint8_t buf[EXAMPLE_BUFFER_SZ];
+                int rxSz = 0;
+                do {
+                    rxSz = wolfSSH_stream_read(threadCtx->ssh, buf, sizeof(buf));
+                    if (rxSz <= 0) {
+                        rxSz = wolfSSH_get_error(threadCtx->ssh);
+                    }
+                } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
+
+                if (rxSz > 0) {
+                    cli_getchar(cli_state, buf[0]);    // TODO handle rxSz > 1
+                    if (buf[0] == 3 || buf[0] == 4 || cli_state->state == STATE_CLOSE) {
+                        stop = 1;
+                    }
+                } else {
                     stop = 1;
                 }
-            } else {
-                stop = 1;
-            }
-        } while (!stop);
-        free_state(cli_state);
+            } while (!stop);
+            free_state(cli_state);
+        } else {
+            cli_send(threadCtx->ssh, pcWarningMessage, pcWarningMessageLen);
+        }
     } else if (ret == WS_SCP_COMPLETE) {
         printf("scp file transfer completed\n");
     } else if (ret == WS_SFTP_COMPLETE) {

+ 1 - 2
modules/Telnet_Server/telnet_server.c

@@ -182,7 +182,6 @@ static struct fd_set master_set, read_set;
 static int max_sd;
 static struct sockaddr_in sa;
 
-const int8_t * const pcWarningMessage = ( const int8_t * ) "Количество соединенений превышено. Данное соединение будет закрыто\r\n";
 #ifdef HARDWARE_BT6709
 const int8_t * const pcWelcomeMessage = ( const int8_t * ) "BT-6709 command server - connection accepted.\r\nlogin:";
 #else
@@ -735,7 +734,7 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 							not_enough_memory = true;
 						}
 						if (not_enough_memory) {
-							send( new_sd, pcWarningMessage, strlen( ( const char * ) pcWarningMessage ), 0 );
+							send(new_sd, pcWarningMessage, pcWarningMessageLen, 0);
 							closesocket(new_sd);
 							FD_CLR(new_sd, &master_set);
 							while (FD_ISSET(max_sd, &master_set) == false) {

+ 2 - 0
modules/cli/cli.c

@@ -27,6 +27,8 @@
 extern SETTINGS_t sSettings;
 
 static const char * const pcEndOfCommandOutputString = "\r\n[Нажмите клавишу ENTER для повторного выполнения предыдущей команды]\r\n>";
+const char pcWarningMessage[] = "Количество соединенений превышено. Данное соединение будет закрыто\r\n";
+const unsigned pcWarningMessageLen = array_len(pcWarningMessage);
 extern int8_t cOutputBuffer[ configCOMMAND_INT_MAX_OUTPUT_SIZE ];
 cli_state_t cli_states[MAX_SESSIONS];
 

+ 2 - 0
modules/cli/cli.h

@@ -49,6 +49,8 @@ typedef struct {
 } cli_state_t;
 
 extern cli_state_t cli_states[MAX_SESSIONS];
+extern const char pcWarningMessage[];
+extern const unsigned pcWarningMessageLen;
 
 void cli_init(void);
 void cli_getchar(cli_state_t *s, char incoming_char);