Преглед изворни кода

ssh: avoid producing extra echo

also move the ssh data buffer onto the thread stack
Sergey Alirzaev пре 5 година
родитељ
комит
d220f4cb8d
1 измењених фајлова са 14 додато и 68 уклоњено
  1. 14 68
      modules/SSH_Server/server.c

+ 14 - 68
modules/SSH_Server/server.c

@@ -61,7 +61,7 @@ typedef struct {
     #define EXAMPLE_HIGHWATER_MARK 0x3FFF8000 /* 1GB - 32kB */
 #endif
 #ifndef EXAMPLE_BUFFER_SZ
-    #define EXAMPLE_BUFFER_SZ 4096
+    #define EXAMPLE_BUFFER_SZ 256
 #endif
 #define SCRATCH_BUFFER_SZ 1200
 
@@ -167,77 +167,23 @@ static void *server_worker(void* vArgs)
         cli_states[threadCtx->fd].state = STATE_NORMAL;
         cli_states[threadCtx->fd].send = cli_send;
 
-        byte* buf = NULL;
-        byte* tmpBuf;
-        int bufSz, backlogSz = 0, rxSz, txSz, stop = 0, txSum;
-
+        bool stop = false;
         do {
-            bufSz = EXAMPLE_BUFFER_SZ + backlogSz;
+            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);
 
-            tmpBuf = (byte*)realloc(buf, bufSz);
-            if (tmpBuf == NULL)
+            if (rxSz > 0) {
+                cli_getchar(cli_states + threadCtx->fd, buf[0]);    // TODO handle rxSz > 1
+            } else {
                 stop = 1;
-            else
-                buf = tmpBuf;
-
-            if (!stop) {
-                do {
-                    rxSz = wolfSSH_stream_read(threadCtx->ssh,
-                                               buf + backlogSz,
-                                               EXAMPLE_BUFFER_SZ);
-                    if (rxSz <= 0)
-                        rxSz = wolfSSH_get_error(threadCtx->ssh);
-                } while (rxSz == WS_WANT_READ || rxSz == WS_WANT_WRITE);
-
-                if (rxSz > 0) {
-                    cli_getchar(cli_states + threadCtx->fd, buf[backlogSz]);    // TODO handle rxSz > 1
-                    backlogSz += rxSz;
-                    txSum = 0;
-                    txSz = 0;
-
-                    //wolfSSH_stream_send(threadCtx->ssh, "loh", 3);
-                    while (backlogSz != txSum && txSz >= 0 && !stop) {
-                        txSz = wolfSSH_stream_send(threadCtx->ssh,
-                                                   buf + txSum,
-                                                   backlogSz - txSum);
-
-                        if (txSz > 0) {
-                            byte c;
-                            const byte matches[] = { 0x03, 0x05, 0x06, 0x00 };
-
-                            c = find_char(matches, buf + txSum, txSz);
-                            switch (c) {
-                                case 0x03:
-                                    stop = 1;
-                                    break;
-                                case 0x06:
-                                    if (wolfSSH_TriggerKeyExchange(threadCtx->ssh)
-                                            != WS_SUCCESS)
-                                        stop = 1;
-                                    break;
-                                    /*
-                                case 0x05:
-                                    if (dump_stats(threadCtx) <= 0)
-                                        stop = 1;
-                                    break;
-                                    */
-                            }
-                            txSum += txSz;
-                        }
-                        else if (txSz != WS_REKEYING)
-                            stop = 1;
-                    }
-
-                    if (txSum < backlogSz)
-                        memmove(buf, buf + txSum, backlogSz - txSum);
-                    backlogSz -= txSum;
-                }
-                else
-                    stop = 1;
             }
         } while (!stop);
-
-        free(buf);
     } else if (ret == WS_SCP_COMPLETE) {
         printf("scp file transfer completed\n");
     } else if (ret == WS_SFTP_COMPLETE) {
@@ -727,5 +673,5 @@ static void ssh_server(void *arg)
 void ssh_server_init(void)
 {
     vRegisterCLICommands();
-    xTaskCreate(ssh_server, ( char * ) "ssh_server", 16*configMINIMAL_STACK_SIZE , NULL, tskIDLE_PRIORITY + 1, NULL);
+    xTaskCreate(ssh_server, ( char * ) "ssh_server", 16*configMINIMAL_STACK_SIZE + EXAMPLE_BUFFER_SZ, NULL, tskIDLE_PRIORITY + 1, NULL);
 }