jam at McQuil.Com
2003-Feb-22 16:37 UTC
Patch for a port forwarding problem in serverloop.c
Hello all, I encountered a problem when trying to setup a remote portforward using OpenSSH 3.5p1. If the port that I was trying to listen to on the remote side was already in use, a message 'cannot bind to port: 5000' was written to the syslog, but the connection still persisted. I had no way on the client side to know that the listening wasn't taking place. that is, the portforwarding of port 5000 failed, but the ssh connection on port 22 was still there. I found the spot in serverloop.c, where it seemed to be ignoring the return status from the 'channel_setup_remote_fwd_listener' call. The fix for me was a simple testing of the success, and calling fatal() to indicate that the port forwarding failed. Here is the patch: --- serverloop.c.orig Sat Feb 22 11:35:39 2003 +++ serverloop.c Sat Feb 22 11:19:29 2003 @@ -989,6 +989,9 @@ /* Start listening on the port */ success = channel_setup_remote_fwd_listener( listen_address, listen_port, options.gateway_ports); + if( !success ){ + fatal("Cannot bind to the remote port"); + } } xfree(listen_address); } And, here is the commandline that I used to initiate the connection: while :; do ssh -N -R 5000:localhost:5000 user at remotesystem sleep 10 done My patch is not terribly robust. I suppose that if you didn't use the '-N' option, you may not want the connection to disappear, but in my case, if the port forwarding fails, I really need the connection to drop, so I can go into a loop and retry it. The reason I need this, is that i'm setting up a forwarded port when I boot the client. If I reboot the client, the old connection lingers for a couple of minutes on the remote machine, before it realizes that the client has gone away. When the client comes back up, and tries to re-establish the forwarded port, it can't. So, now with my patch, the client will realize that the port forward request failed, and it will sit in a loop, and try every 10 seconds, until the connection succeeds. Anyway, thanks, and I hope you find my patch useful. Jim McQuillan jam at Ltsp.org -------------- next part -------------- --- serverloop.c.orig Sat Feb 22 11:35:39 2003 +++ serverloop.c Sat Feb 22 11:19:29 2003 @@ -989,6 +989,9 @@ /* Start listening on the port */ success = channel_setup_remote_fwd_listener( listen_address, listen_port, options.gateway_ports); + if( !success ){ + fatal("Cannot bind to the remote port"); + } } xfree(listen_address); }
hm, ssh protocol messages should be used and the client should close the connection, not the server. thanks! On Sat, Feb 22, 2003 at 11:37:31AM -0500, jam at McQuil.Com wrote:> Hello all, > > I encountered a problem when trying to setup a remote portforward > using OpenSSH 3.5p1. > > If the port that I was trying to listen to on the remote side > was already in use, a message 'cannot bind to port: 5000' was > written to the syslog, but the connection still persisted. > > I had no way on the client side to know that the listening > wasn't taking place. that is, the portforwarding of port 5000 > failed, but the ssh connection on port 22 was still there. > > I found the spot in serverloop.c, where it seemed to be > ignoring the return status from the 'channel_setup_remote_fwd_listener' > call. > > The fix for me was a simple testing of the success, and calling > fatal() to indicate that the port forwarding failed. > > Here is the patch: > > > --- serverloop.c.orig Sat Feb 22 11:35:39 2003 > +++ serverloop.c Sat Feb 22 11:19:29 2003 > @@ -989,6 +989,9 @@ > /* Start listening on the port */ > success = channel_setup_remote_fwd_listener( > listen_address, listen_port, options.gateway_ports); > + if( !success ){ > + fatal("Cannot bind to the remote port"); > + } > } > xfree(listen_address); > } > > > And, here is the commandline that I used to initiate the connection: > > > while :; do > > ssh -N -R 5000:localhost:5000 user at remotesystem > > sleep 10 > > done > > > My patch is not terribly robust. I suppose that if you didn't use > the '-N' option, you may not want the connection to disappear, but > in my case, if the port forwarding fails, I really need the > connection to drop, so I can go into a loop and retry it. > > The reason I need this, is that i'm setting up a forwarded port > when I boot the client. If I reboot the client, the old connection > lingers for a couple of minutes on the remote machine, before > it realizes that the client has gone away. When the client comes > back up, and tries to re-establish the forwarded port, it can't. > > So, now with my patch, the client will realize that the port forward > request failed, and it will sit in a loop, and try every 10 seconds, > until the connection succeeds. > > Anyway, thanks, and I hope you find my patch useful. > > Jim McQuillan > jam at Ltsp.org> --- serverloop.c.orig Sat Feb 22 11:35:39 2003 > +++ serverloop.c Sat Feb 22 11:19:29 2003 > @@ -989,6 +989,9 @@ > /* Start listening on the port */ > success = channel_setup_remote_fwd_listener( > listen_address, listen_port, options.gateway_ports); > + if( !success ){ > + fatal("Cannot bind to the remote port"); > + } > } > xfree(listen_address); > }