Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libssh2-tunnel-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ int forward_tunnel(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel) {
}
wr = 0;
do {
i = libssh2_channel_write(channel, buf, len);
i = LIBSSH2_ERROR_EAGAIN;
while (i == LIBSSH2_ERROR_EAGAIN) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can cause an infinite loop, blocking the tunnel on write.
The correct way to handle this is to check if the socket is alive before sending it again: https://stackoverflow.com/a/9811061/2052076

Some code examples that I found:
https://github.com/allisrc/libgit2-allisrc/blob/62ab0e1fc2a471db960b0bd881aab116b350a961/src/transports/ssh.c#L266

https://github.com/u35s/xtools/blob/99fe76fffb5d893573b1aa21e8032dc5385c2837/src/common/ssh2_client.cc#L185

i = libssh2_channel_write(channel, buf, len);
}
if (i < 0) {
fprintf(stderr, "Error writing on the SSH channel: %d\n", i);
goto shutdown;
Expand Down