diff --git a/src/internal.c b/src/internal.c index ed5ffb4..3f29dc9 100644 --- a/src/internal.c +++ b/src/internal.c @@ -7051,58 +7051,63 @@ int SendChannelData(WOLFSSH* ssh, word32 peerChannel, ret = WS_REKEYING; } - if (ssh->outputBuffer.length == 0) { - if (ret == WS_SUCCESS) { - channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER); - if (channel == NULL) { - WLOG(WS_LOG_DEBUG, "Invalid peer channel"); - ret = WS_INVALID_CHANID; - } - } + if (ret == WS_SUCCESS) { + if (ssh->outputBuffer.length != 0) + ret = wolfSSH_SendPacket(ssh); + } - if (ret == WS_SUCCESS) { - word32 bound = min(channel->peerWindowSz, channel->peerMaxPacketSz); - - if (dataSz > bound) { - WLOG(WS_LOG_DEBUG, - "Trying to send %u, client will only accept %u, limiting", - dataSz, bound); - dataSz = bound; - } - - ret = PreparePacket(ssh, - MSG_ID_SZ + UINT32_SZ + LENGTH_SZ + dataSz); - } - - if (ret == WS_SUCCESS) { - output = ssh->outputBuffer.buffer; - idx = ssh->outputBuffer.length; - - output[idx++] = MSGID_CHANNEL_DATA; - c32toa(channel->peerChannel, output + idx); - idx += UINT32_SZ; - c32toa(dataSz, output + idx); - idx += LENGTH_SZ; - WMEMCPY(output + idx, data, dataSz); - idx += dataSz; - - ssh->outputBuffer.length = idx; - - ret = BundlePacket(ssh); + if (ret == WS_SUCCESS) { + channel = ChannelFind(ssh, peerChannel, WS_CHANNEL_ID_PEER); + if (channel == NULL) { + WLOG(WS_LOG_DEBUG, "Invalid peer channel"); + ret = WS_INVALID_CHANID; } } - if (ret == WS_SUCCESS) - ret = wolfSSH_SendPacket(ssh); + if (ret == WS_SUCCESS) { + word32 bound = min(channel->peerWindowSz, channel->peerMaxPacketSz); + + if (dataSz > bound) { + WLOG(WS_LOG_DEBUG, + "Trying to send %u, client will only accept %u, limiting", + dataSz, bound); + dataSz = bound; + } + + ret = PreparePacket(ssh, + MSG_ID_SZ + UINT32_SZ + LENGTH_SZ + dataSz); + } + + if (ret == WS_SUCCESS) { + output = ssh->outputBuffer.buffer; + idx = ssh->outputBuffer.length; + + output[idx++] = MSGID_CHANNEL_DATA; + c32toa(channel->peerChannel, output + idx); + idx += UINT32_SZ; + c32toa(dataSz, output + idx); + idx += LENGTH_SZ; + WMEMCPY(output + idx, data, dataSz); + idx += dataSz; + + ssh->outputBuffer.length = idx; + + ret = BundlePacket(ssh); + } if (ret == WS_SUCCESS) { WLOG(WS_LOG_INFO, " dataSz = %u", dataSz); WLOG(WS_LOG_INFO, " peerWindowSz = %u", channel->peerWindowSz); channel->peerWindowSz -= dataSz; WLOG(WS_LOG_INFO, " update peerWindowSz = %u", channel->peerWindowSz); - ret = dataSz; } + if (ret == WS_SUCCESS) + ret = wolfSSH_SendPacket(ssh); + + if (ret == WS_SUCCESS) + ret = dataSz; + WLOG(WS_LOG_DEBUG, "Leaving SendChannelData(), ret = %d", ret); return ret; }