Direct Connection instead of port forwarding

Apr 17, 2013 at 3:29 PM
I am writing a client application that needs to connect to multiple servers simultaneously using port forwarding. How can I decide what local ports to use. Other SSH Clients that I have investigated have a direct connect method so that you don't have to specify a local IP and port. Is this what the ForwardedPortDynamic is used for?

Any help is appreciated
Apr 18, 2013 at 11:44 AM
ForwardedPortDynamic is like a SOCKS 5 Proxy you can use where you do not have to do a 1:1 mapping, but your application must be proxy aware to be able to use the ForwardedPortDynamic.
Apr 18, 2013 at 2:15 PM
Thanks. I have since figured out that I will need to find the first available port and pass that to the ForwardedPortLocal constructor.

I was looking at the code for the local port forward and see that a TCPListener is being used with the address and port that is passed in. To my knowledge, if the TCPListener is given port 0, it will find the first available. This is the method I use to find the first available port. The trouble is that trying to find the first availabale port outside of the port forward class results in a race condition (the port could be taken in the time between when it is discovered as free and when the port forward class creates the listener).

I was thinking it would be nice to have the BoundPort property on the ForwardedPortLocal class be used to get the port from the IPEndPoint that is created when Start() is called on the ForwardedPortClass. I can then use the resulting port when creating a socket connection, which is what I am doing.

The code for getting the port from the listener is simply: BoundPort = ((IPEndPoint) listener.LocalEndpoint).Port

In cases where the port passed in is not zero, then the endpoint port should be the same as what was passed in. If the port passed in is zero, then BoundPort will return the actual port that is being used.

To go a step further, a new constructor could be created that doesn't take a port parameter at all. In this case zero would be the default.

Anybody have any thoughts about this.
Apr 19, 2013 at 2:44 AM
Edited Apr 19, 2013 at 3:10 AM
pmilin wrote:
Thanks. I have since figured out that I will need to find the first available port and pass that to the ForwardedPortLocal constructor.

I was looking at the code for the local port forward and see that a TCPListener is being used with the address and port that is passed in. To my knowledge, if the TCPListener is given port 0, it will find the first available. This is the method I use to find the first available port. The trouble is that trying to find the first availabale port outside of the port forward class results in a race condition (the port could be taken in the time between when it is discovered as free and when the port forward class creates the listener).

I was thinking it would be nice to have the BoundPort property on the ForwardedPortLocal class be used to get the port from the IPEndPoint that is created when Start() is called on the ForwardedPortClass. I can then use the resulting port when creating a socket connection, which is what I am doing.

The code for getting the port from the listener is simply: BoundPort = ((IPEndPoint) listener.LocalEndpoint).Port

In cases where the port passed in is not zero, then the endpoint port should be the same as what was passed in. If the port passed in is zero, then BoundPort will return the actual port that is being used.

To go a step further, a new constructor could be created that doesn't take a port parameter at all. In this case zero would be the default.

Anybody have any thoughts about this.
Hi,
I think you miss understood the concept, like Darkoperator said ForwardedPortDynamic is like a SOCKS 5 Proxy, so you just specify one localport (eg. 8080) and every application that proxy aware (eg. web browser) use that port as proxy to connect to any host and let SSH net handle the data exchange.

In ForwardedPortLocal you have to specify one port for every single remote host you want to forward. For example you give localport 3306 to forward connection to somehost:3306, so localport 3306 is only connected to somehost:3306. If you want to connect to other host, just add more ForwardedPortLocal in your application.

Regards
Apr 19, 2013 at 1:46 PM
Hi gums,

I didn't misunderstand. In my first post I merely asked if dynamic port forward would solve my problem.

I now know that dynamic port forwarding is not what I want.

Like I said in my last post, I am using local port forwarding. I am creating additional port forwards as needed; however, the issue is that I cannot simply pick a port at random because it might be in use. So then I would need code that found an available port.

Since the system has a way of using the next available port (pass 0 in for the port to the TcpListener) I was suggesting that the API to the ForwardedPortLocal change so that 0 can be specified as the port and the ForwardedPortLocal class could return the port that the system selected.

I will sumbit a patch with the changes I'm talking about and see if the author agrees.

Thanks