Skip to content

Commit

Permalink
Term Update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ejohnstown committed Sep 19, 2023
1 parent 5af49f2 commit 5b094d6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 65 deletions.
9 changes: 0 additions & 9 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
118 changes: 63 additions & 55 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -13073,8 +13076,8 @@ int SendChannelTerminalResize(WOLFSSH* ssh, word32 columns, word32 rows,
#include <sys/ioctl.h>
#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 };
Expand All @@ -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;

Expand All @@ -13108,18 +13112,21 @@ 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()");

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);

Expand All @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion wolfssh/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5b094d6

Please sign in to comment.