Skip to content

Commit

Permalink
Refactoring Channels
Browse files Browse the repository at this point in the history
1. Remove the unused channel status callbacks from the echoserver.
2. Add a flag to the threadCtx to be set if the channel is to be SFTP.
3. Modify the subsystem start callback to set the SFTP flag if the
   subsystem is sftp.
4. ssh_worker to return WS_SFTP_COMPLETE if the SFTP flag is set.
5. If worker returns SFTP complete, run the SFTP accept and start
   stfp_worker.
  • Loading branch information
ejohnstown committed Nov 4, 2024
1 parent eb4027d commit 9746fa3
Showing 1 changed file with 17 additions and 62 deletions.
79 changes: 17 additions & 62 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ typedef struct {
#ifdef WOLFSSH_SHELL
WS_ShellCtx shellCtx;
byte shellBuffer[EXAMPLE_BUFFER_SZ];
#endif
#ifdef WOLFSSH_SFTP
int doSftp;
#endif
byte channelBuffer[EXAMPLE_BUFFER_SZ];
char statsBuffer[EXAMPLE_BUFFER_SZ];
Expand Down Expand Up @@ -748,66 +751,16 @@ static int wsSubsysStartCb(WOLFSSH_CHANNEL* channel, void* vCtx)
cmd = wolfSSH_ChannelGetSessionCommand(channel);
type = wolfSSH_ChannelGetSessionType(channel);

WOLFSSH_UNUSED(threadCtx);

if (type == WOLFSSH_SESSION_SUBSYSTEM
&& WSTRCMP(cmd, "sftp") == 0) {
/* do nothing */;
threadCtx->doSftp = 1;
}

return 0;
}
#endif /* WOLFSSH_SFTP */


static int wsChannelOpenCb(WOLFSSH_CHANNEL* channel, void* ctx)
{
word32 id = 0;

wolfSSH_ChannelGetId(channel, &id, WS_CHANNEL_ID_PEER);

if (ctx != NULL) {
printf("Channel %u open attempt, CTX is %p.\n", id, ctx);
}
else {
printf("Channel %u open attempt, but no CTX.\n", id);
}
return 0;
}


static int wsChannelEofCb(WOLFSSH_CHANNEL* channel, void* ctx)
{
word32 id = 0;

wolfSSH_ChannelGetId(channel, &id, WS_CHANNEL_ID_PEER);

if (ctx != NULL) {
printf("Channel %u end of file, CTX is %p.\n", id, ctx);
}
else {
printf("Channel %u end of file, but no CTX.\n", id);
}
return 0;
}


static int wsChannelCloseCb(WOLFSSH_CHANNEL* channel, void* ctx)
{
word32 id = 0;

wolfSSH_ChannelGetId(channel, &id, WS_CHANNEL_ID_PEER);

if (ctx != NULL) {
printf("Channel %u closed, CTX is %p.\n", id, ctx);
}
else {
printf("Channel %u closed, but no CTX.\n", id);
}
return 0;
}


#ifdef SHELL_DEBUG

static void display_ascii(char *p_buf,
Expand Down Expand Up @@ -1039,6 +992,11 @@ static int ssh_worker(thread_ctx_t* threadCtx)
channel. The additional channel is only used with the
agent. */
cnt_r = wolfSSH_worker(ssh, &lastChannel);
#ifdef WOLFSSH_SFTP
if (threadCtx->doSftp) {
return WS_SFTP_COMPLETE;
}
#endif
if (cnt_r < 0) {
rc = wolfSSH_get_error(ssh);
if (rc == WS_CHAN_RXD) {
Expand Down Expand Up @@ -1603,14 +1561,16 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
ret = 0;
break;

#ifdef WOLFSSH_SFTP
case WS_SFTP_COMPLETE:
ret = sftp_worker(threadCtx);
break;
#endif

case WS_SUCCESS:
ret = ssh_worker(threadCtx);
#ifdef WOLFSSH_SFTP
if (ret == WS_SFTP_COMPLETE) {
ret = wolfSSH_SFTP_accept(threadCtx->ssh);
}
if (ret == WS_SFTP_COMPLETE) {
ret = sftp_worker(threadCtx);
}
#endif
break;
}

Expand Down Expand Up @@ -2655,9 +2615,6 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
wolfSSH_SetUserAuth(ctx, ((func_args*)args)->user_auth);
wolfSSH_SetUserAuthResult(ctx, wsUserAuthResult);
wolfSSH_CTX_SetBanner(ctx, echoserverBanner);
wolfSSH_CTX_SetChannelOpenCb(ctx, wsChannelOpenCb);
wolfSSH_CTX_SetChannelEofCb(ctx, wsChannelEofCb);
wolfSSH_CTX_SetChannelCloseCb(ctx, wsChannelCloseCb);
#ifdef WOLFSSH_SHELL
wolfSSH_CTX_SetChannelReqShellCb(ctx, wsShellStartCb);
#endif
Expand Down Expand Up @@ -2897,8 +2854,6 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
wolfSSH_SetKeyingCompletionCbCtx(ssh, (void*)ssh);
wolfSSH_SetChannelOpenCtx(ssh, (void*)threadCtx);
wolfSSH_SetChannelReqCtx(ssh, (void*)threadCtx);
wolfSSH_SetChannelEofCtx(ssh, (void*)threadCtx);
wolfSSH_SetChannelCloseCtx(ssh, (void*)threadCtx);
/* Use the session object for its own highwater callback ctx */
if (defaultHighwater > 0) {
wolfSSH_SetHighwaterCtx(ssh, (void*)ssh);
Expand Down

0 comments on commit 9746fa3

Please sign in to comment.