From 5b094d6f4394dc527565ad5019b5dd040db55aa8 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 19 Sep 2023 11:40:23 -0700 Subject: [PATCH] Term Update 1. Change GetTerminalSize() to GetTerminalInfo(). 2. Add the term variable to the things GetTerminalInfo() looks up. 3. Return the actual value of the environment variable TERM to the server. If one isn't present, default to "xterm". 4. Clean up the whitespace in SendChannelTerminalRequest(). Misc other changes: 1. Cleanup whitespace in the recently added function SendChannelOpenFail(). 2. Add labels to the parameters in the prototype for SendChannelOpenFail(). 3. Remove the CR addition for Windows in the example client. Causes the enter key to double-strike. --- examples/client/client.c | 9 --- src/internal.c | 118 +++++++++++++++++++++------------------ wolfssh/internal.h | 3 +- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index d38d316d..262621a0 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -316,15 +316,6 @@ static THREAD_RET readInput(void* in) #else ret = (int)read(STDIN_FILENO, buf, bufSz -1); sz = (word32)ret; - - /* add carriage returns for interop with windows server */ - if (ret > 0) { - if (ret == 1 && buf[0] == '\n') { - buf[1] = '\r'; - ret++; - sz++; - } - } #endif if (ret <= 0) { fprintf(stderr, "Error reading stdin\n"); diff --git a/src/internal.c b/src/internal.c index 85850e24..6f60d7b3 100644 --- a/src/internal.c +++ b/src/internal.c @@ -12343,57 +12343,60 @@ int SendChannelOpenConf(WOLFSSH* ssh, WOLFSSH_CHANNEL* channel) return ret; } -int SendChannelOpenFail(WOLFSSH* ssh, word32 channel, word32 reason, const char *description, const char *language) +int SendChannelOpenFail(WOLFSSH* ssh, word32 channel, word32 reason, + const char* description, const char* language) { - byte* output; - word32 idx; - word32 descriptionSz = (word32)WSTRLEN(description); - word32 languageSz = (word32)WSTRLEN(language); - int ret = WS_SUCCESS; + byte* output; + word32 idx; + word32 descriptionSz = (word32)WSTRLEN(description); + word32 languageSz = (word32)WSTRLEN(language); + int ret = WS_SUCCESS; - WLOG(WS_LOG_DEBUG, "Entering SendChannelOpenFail()"); + WLOG(WS_LOG_DEBUG, "Entering SendChannelOpenFail()"); - if (ssh == NULL) - ret = WS_BAD_ARGUMENT; + if (ssh == NULL) + ret = WS_BAD_ARGUMENT; - if (ret == WS_SUCCESS) { - WLOG(WS_LOG_INFO, " channelId = %u", channel); - WLOG(WS_LOG_INFO, " reason = %u", reason); - WLOG(WS_LOG_INFO, " description = %s", description); - WLOG(WS_LOG_INFO, " language = %s", language); - } + if (ret == WS_SUCCESS) { + WLOG(WS_LOG_INFO, " channelId = %u", channel); + WLOG(WS_LOG_INFO, " reason = %u", reason); + WLOG(WS_LOG_INFO, " description = %s", description); + WLOG(WS_LOG_INFO, " language = %s", language); + } - if (ret == WS_SUCCESS) - ret = PreparePacket(ssh, MSG_ID_SZ + UINT32_SZ + UINT32_SZ + LENGTH_SZ + descriptionSz + LENGTH_SZ + languageSz); + if (ret == WS_SUCCESS) { + ret = PreparePacket(ssh, MSG_ID_SZ + UINT32_SZ + UINT32_SZ + + LENGTH_SZ + descriptionSz + LENGTH_SZ + languageSz); + } - if (ret == WS_SUCCESS) { - output = ssh->outputBuffer.buffer; - idx = ssh->outputBuffer.length; + if (ret == WS_SUCCESS) { + output = ssh->outputBuffer.buffer; + idx = ssh->outputBuffer.length; - output[idx++] = MSGID_CHANNEL_OPEN_FAIL; - c32toa(channel, output + idx); - idx += UINT32_SZ; - c32toa(reason, output + idx); - idx += UINT32_SZ; - c32toa(descriptionSz, output + idx); - idx += UINT32_SZ; - WMEMCPY(output + idx, description, descriptionSz); - idx += descriptionSz; - c32toa(languageSz, output + idx); - idx += UINT32_SZ; - WMEMCPY(output + idx, language, languageSz); - idx += languageSz; + output[idx++] = MSGID_CHANNEL_OPEN_FAIL; + c32toa(channel, output + idx); + idx += UINT32_SZ; + c32toa(reason, output + idx); + idx += UINT32_SZ; + c32toa(descriptionSz, output + idx); + idx += LENGTH_SZ; + WMEMCPY(output + idx, description, descriptionSz); + idx += descriptionSz; + c32toa(languageSz, output + idx); + idx += LENGTH_SZ; + WMEMCPY(output + idx, language, languageSz); + idx += languageSz; - ssh->outputBuffer.length = idx; + ssh->outputBuffer.length = idx; - ret = BundlePacket(ssh); - } + ret = BundlePacket(ssh); + } - if (ret == WS_SUCCESS) - ret = wolfSSH_SendPacket(ssh); + if (ret == WS_SUCCESS) + ret = wolfSSH_SendPacket(ssh); - WLOG(WS_LOG_DEBUG, "Leaving SendChannelOpenFail(), ret = %d", ret); - return ret; + WLOG(WS_LOG_DEBUG, "Leaving SendChannelOpenFail(), ret = %d", ret); + return ret; } int SendChannelEof(WOLFSSH* ssh, word32 peerChannelId) @@ -13073,8 +13076,8 @@ int SendChannelTerminalResize(WOLFSSH* ssh, word32 columns, word32 rows, #include #endif -static void GetTerminalSize(word32* width, word32* height, - word32* pixWidth, word32* pixHeight) +static void GetTerminalInfo(word32* width, word32* height, + word32* pixWidth, word32* pixHeight, const char** term) { #ifdef HAVE_SYS_IOCTL_H struct winsize windowSize = { 0,0,0,0 }; @@ -13084,6 +13087,7 @@ static void GetTerminalSize(word32* width, word32* height, *height = (word32)windowSize.ws_row; *pixWidth = (word32)windowSize.ws_xpixel; *pixHeight = (word32)windowSize.ws_ypixel; + *term = getenv("TERM"); #elif defined(_MSC_VER) CONSOLE_SCREEN_BUFFER_INFO cs; @@ -13108,9 +13112,9 @@ int SendChannelTerminalRequest(WOLFSSH* ssh) int ret = WS_SUCCESS; WOLFSSH_CHANNEL* channel; const char cType[] = "pty-req"; - const char envVar[] = "xterm"; + const char* term = NULL; byte mode[4096]; - word32 envSz, typeSz, modeSz; + word32 termSz, typeSz, modeSz; word32 w = 0, h = 0, pxW = 0, pxH = 0; WLOG(WS_LOG_DEBUG, "Entering SendChannelTerminalRequest()"); @@ -13118,8 +13122,11 @@ int SendChannelTerminalRequest(WOLFSSH* ssh) if (ssh == NULL) ret = WS_BAD_ARGUMENT; - GetTerminalSize(&w, &h, &pxW, &pxH); - envSz = (word32)WSTRLEN(envVar); + GetTerminalInfo(&w, &h, &pxW, &pxH, &term); + if (term == NULL) { + term = "xterm"; + } + termSz = (word32)WSTRLEN(term); typeSz = (word32)WSTRLEN(cType); modeSz = CreateMode(ssh, mode); @@ -13143,12 +13150,11 @@ int SendChannelTerminalRequest(WOLFSSH* ssh) * string encoded terminal modes */ - if (ret == WS_SUCCESS) - ret = PreparePacket(ssh, MSG_ID_SZ + UINT32_SZ + LENGTH_SZ + - typeSz + BOOLEAN_SZ + - ((envSz > 0)? UINT32_SZ : 0) + envSz + - UINT32_SZ * 4 + - ((modeSz > 0)? UINT32_SZ : 0) + modeSz); + if (ret == WS_SUCCESS) { + ret = PreparePacket(ssh, MSG_ID_SZ + UINT32_SZ + LENGTH_SZ + typeSz + + BOOLEAN_SZ + LENGTH_SZ + termSz + UINT32_SZ * 4 + + LENGTH_SZ + modeSz); + } if (ret == WS_SUCCESS) { output = ssh->outputBuffer.buffer; @@ -13160,9 +13166,11 @@ int SendChannelTerminalRequest(WOLFSSH* ssh) WMEMCPY(output + idx, cType, typeSz); idx += typeSz; output[idx++] = 1; /* want reply */ - if (envSz > 0) { - c32toa(envSz, output + idx); idx += UINT32_SZ; - WMEMCPY(output + idx, envVar, envSz); idx += envSz; + c32toa(termSz, output + idx); + idx += UINT32_SZ; + if (termSz > 0) { + WMEMCPY(output + idx, term, termSz); + idx += termSz; } c32toa(w, output + idx); idx += UINT32_SZ; diff --git a/wolfssh/internal.h b/wolfssh/internal.h index f0d7b63f..12226be9 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -920,7 +920,8 @@ WOLFSSH_LOCAL int SendRequestSuccess(WOLFSSH*, int); WOLFSSH_LOCAL int SendChannelOpenSession(WOLFSSH*, WOLFSSH_CHANNEL*); WOLFSSH_LOCAL int SendChannelOpenForward(WOLFSSH*, WOLFSSH_CHANNEL*); WOLFSSH_LOCAL int SendChannelOpenConf(WOLFSSH*, WOLFSSH_CHANNEL*); -WOLFSSH_LOCAL int SendChannelOpenFail(WOLFSSH*, word32, word32, const char*, const char*); +WOLFSSH_LOCAL int SendChannelOpenFail(WOLFSSH* ssh, word32 channel, + word32 reason, const char* description, const char* language); WOLFSSH_LOCAL int SendChannelEof(WOLFSSH*, word32); WOLFSSH_LOCAL int SendChannelEow(WOLFSSH*, word32); WOLFSSH_LOCAL int SendChannelClose(WOLFSSH*, word32);