浏览代码

ssh: allocate cli contexts

reject the connection if none are available
Sergey Alirzaev 5 年之前
父节点
当前提交
5e0bd039ab
共有 3 个文件被更改,包括 26 次插入11 次删除
  1. 13 0
      modules/SSH_Server/cli.c
  2. 2 0
      modules/SSH_Server/cli.h
  3. 11 11
      modules/SSH_Server/server.c

+ 13 - 0
modules/SSH_Server/cli.c

@@ -334,3 +334,16 @@ void cli_getchar(cli_state_t *s, char incoming_char)
     ++s->bufptr;
   }
 }
+
+#define array_len(x) (sizeof(x)/sizeof(x[0]))
+
+cli_state_t *alloc_state(void)
+{
+  for (unsigned i = 0; i < array_len(cli_states); ++i) {
+    if (cli_states[i].state == STATE_UNUSED) {
+      cli_states[i].state = STATE_CLOSE;
+      return &cli_states[i];
+    }
+  }
+  return 0;
+}

+ 2 - 0
modules/SSH_Server/cli.h

@@ -20,6 +20,7 @@ typedef enum{
 } input_state_t;
 
 typedef enum {
+	STATE_UNUSED,
 	STATE_NORMAL,
 	STATE_IAC,
 	STATE_OPT,
@@ -54,5 +55,6 @@ typedef struct {
 } cli_state_t;
 
 void cli_getchar(cli_state_t *s, char incoming_char);
+cli_state_t *alloc_state(void);
 
 #endif

+ 11 - 11
modules/SSH_Server/server.c

@@ -148,8 +148,6 @@ static void cli_send(intptr_t fd, const char *str, unsigned len)
     wolfSSH_stream_send((WOLFSSH *)fd, str, len);
 }
 
-extern cli_state_t cli_states[MAX_SESSIONS];
-
 static void *server_worker(void* vArgs)
 {
     int ret;
@@ -160,12 +158,13 @@ static void *server_worker(void* vArgs)
     else
         ret = NonBlockSSH_accept(threadCtx->ssh);
 
-    if (ret == WS_SUCCESS) {
-        // create the new CLI context
-        cli_states[threadCtx->fd].num_connect = threadCtx->ssh;
-        cli_states[threadCtx->fd].input_state = CLI_CMD;
-        cli_states[threadCtx->fd].state = STATE_NORMAL;
-        cli_states[threadCtx->fd].send = cli_send;
+    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_state->state = STATE_NORMAL;
 
         bool stop = false;
         do {
@@ -179,14 +178,15 @@ static void *server_worker(void* vArgs)
             } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
 
             if (rxSz > 0) {
-                cli_getchar(cli_states + threadCtx->fd, buf[0]);    // TODO handle rxSz > 1
-                if (buf[0] == 3 || buf[0] == 4) {
+                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;
             }
         } while (!stop);
+        cli_state->state = STATE_UNUSED;
     } else if (ret == WS_SCP_COMPLETE) {
         printf("scp file transfer completed\n");
     } else if (ret == WS_SFTP_COMPLETE) {
@@ -676,5 +676,5 @@ static void ssh_server(void *arg)
 void ssh_server_init(void)
 {
     vRegisterCLICommands();
-    xTaskCreate(ssh_server, ( char * ) "ssh_server", 16*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
+    xTaskCreate(ssh_server, ( char * ) "ssh_server", 24*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
 }