diff --git a/examples/client/client.c b/examples/client/client.c index bf270cf..13a4d47 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -512,6 +512,14 @@ static THREAD_RET readInput(void* in) } +#ifdef WOLFSSH_AGENT +static inline void ato32(const byte* c, word32* u32) +{ + *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; +} +#endif + + static THREAD_RET readPeer(void* in) { byte buf[80]; @@ -555,18 +563,30 @@ static THREAD_RET readPeer(void* in) if (ret == WS_CHAN_RXD) { byte agentBuf[512]; int rxd, txd; + word32 channel = 0; - rxd = wolfSSH_ChannelIdRead(args->ssh, 1, + wolfSSH_GetLastRxId(args->ssh, &channel); + rxd = wolfSSH_ChannelIdRead(args->ssh, channel, agentBuf, sizeof(agentBuf)); - if (rxd > 0) { + if (rxd > 4) { + word32 msgSz = 0; + + ato32(agentBuf, &msgSz); + if (msgSz > (word32)rxd - 4) { + rxd += wolfSSH_ChannelIdRead(args->ssh, channel, + agentBuf + rxd, + sizeof(agentBuf) - rxd); + } + txd = rxd; rxd = sizeof(agentBuf); ret = wolfSSH_AGENT_Relay(args->ssh, agentBuf, (word32*)&txd, agentBuf, (word32*)&rxd); - if (ret == WS_SUCCESS) - wolfSSH_ChannelIdSend(args->ssh, 1, + if (ret == WS_SUCCESS) { + ret = wolfSSH_ChannelIdSend(args->ssh, channel, agentBuf, rxd); + } } WMEMSET(agentBuf, 0, sizeof(agentBuf)); continue; diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 574b237..f4d2d49 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -562,6 +562,7 @@ static int shell_worker(thread_ctx_t* threadCtx) BUF_T agent_buf; int agentFd = -1; int listenFd = threadCtx->agentCbCtx.listenFd; + word32 agentChannelId = 0; #endif #ifdef SHELL_DEBUG @@ -789,7 +790,8 @@ static int shell_worker(thread_ctx_t* threadCtx) } #ifdef WOLFSSH_AGENT if (rc == WS_CHAN_RXD) { - cnt_r = wolfSSH_ChannelIdRead(ssh, 1, + wolfSSH_GetLastRxId(ssh, &agentChannelId); + cnt_r = wolfSSH_ChannelIdRead(ssh, agentChannelId, (byte*)agent_buf.buf, cnt_r); if (cnt_r <= 0) break; @@ -839,7 +841,7 @@ static int shell_worker(thread_ctx_t* threadCtx) #ifdef SHELL_DEBUG buf_dump(agent_buf.buf+agent_buf.rdidx, cnt_r); #endif - cnt_w = wolfSSH_ChannelIdSend(ssh, 1, + cnt_w = wolfSSH_ChannelIdSend(ssh, agentChannelId, (byte*)agent_buf.buf + agent_buf.rdidx, cnt_r); if (cnt_w > 0) { agent_buf.rdidx += cnt_r; diff --git a/src/internal.c b/src/internal.c index 767f11c..c6fc2c3 100644 --- a/src/internal.c +++ b/src/internal.c @@ -8462,7 +8462,7 @@ int SendChannelClose(WOLFSSH* ssh, word32 peerChannelId) } -int SendChannelData(WOLFSSH* ssh, word32 peerChannel, +int SendChannelData(WOLFSSH* ssh, word32 channelId, byte* data, word32 dataSz) { byte* output; @@ -8486,9 +8486,9 @@ int SendChannelData(WOLFSSH* ssh, word32 peerChannel, } if (ret == WS_SUCCESS) { - channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER); + channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF); if (channel == NULL) { - WLOG(WS_LOG_DEBUG, "Invalid peer channel"); + WLOG(WS_LOG_DEBUG, "Invalid channel"); ret = WS_INVALID_CHANID; } } @@ -8552,7 +8552,7 @@ int SendChannelData(WOLFSSH* ssh, word32 peerChannel, } -int SendChannelWindowAdjust(WOLFSSH* ssh, word32 peerChannel, +int SendChannelWindowAdjust(WOLFSSH* ssh, word32 channelId, word32 bytesToAdd) { byte* output; @@ -8565,9 +8565,9 @@ int SendChannelWindowAdjust(WOLFSSH* ssh, word32 peerChannel, if (ssh == NULL) ret = WS_BAD_ARGUMENT; - channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER); + channel = ChannelFind(ssh, channelId, WS_CHANNEL_ID_SELF); if (channel == NULL) { - WLOG(WS_LOG_DEBUG, "Invalid peer channel"); + WLOG(WS_LOG_DEBUG, "Invalid channel"); ret = WS_INVALID_CHANID; } if (ret == WS_SUCCESS) diff --git a/src/ssh.c b/src/ssh.c index d5cb25c..14557d7 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -1715,6 +1715,20 @@ int wolfSSH_worker(WOLFSSH* ssh, word32* channelId) } +int wolfSSH_GetLastRxId(WOLFSSH* ssh, word32* channelId) +{ + int ret = WS_SUCCESS; + + if (ssh == NULL || channelId == NULL) + ret = WS_ERROR; + + if (ret == WS_SUCCESS) + *channelId = ssh->lastRxId; + + return ret; +} + + #ifdef WOLFSSH_FWD WOLFSSH_CHANNEL* wolfSSH_ChannelFwdNew(WOLFSSH* ssh, diff --git a/wolfssh/ssh.h b/wolfssh/ssh.h index 5cf43e2..9ac4023 100644 --- a/wolfssh/ssh.h +++ b/wolfssh/ssh.h @@ -63,6 +63,7 @@ WOLFSSH_API WOLFSSH* wolfSSH_new(WOLFSSH_CTX*); WOLFSSH_API void wolfSSH_free(WOLFSSH*); WOLFSSH_API int wolfSSH_worker(WOLFSSH*, word32*); +WOLFSSH_API int wolfSSH_GetLastRxId(WOLFSSH*, word32*); WOLFSSH_API int wolfSSH_set_fd(WOLFSSH*, WS_SOCKET_T); WOLFSSH_API WS_SOCKET_T wolfSSH_get_fd(const WOLFSSH*);