Archived Forum Post

Index of archived forum posts

Question:

iOS Local Port Forwarding

Dec 28 '12 at 06:46

Hi guys, I am trying to local port forward in my iOS app. I am using cksshtunnel class for that. It returns true in the end but there is no log in my SSH server which shows that ssh tunnel has been established. I am using FreeSSHD for my ssh server on windows 7. Here I include a snippet of code from my app. I have seen the example given on chilkat site for port forwarding but that only returns a channel. I want my another application to listen to my local forwarded port (RDP in my case).

[tunnel setDestHostname: @"172.29.43.47"];  //pc i want to connect to
[tunnel setDestPort: [NSNumber numberWithInt:3389]];  //rdp port
NSLog(@"%@",tunnel.ListenPort);

/*my ssh server credentials*/
[tunnel setSshHostname:@"172.29.37.64"];
[tunnel setSshLogin:@"test"];
[tunnel setSshPassword:@"test"];
[tunnel setSshPort:[NSNumber numberWithInt:222]];
NSNumber * listenPort = [[NSNumber alloc]initWithInt:45678];

BOOL isTunnelmade = [tunnel BeginAccepting:listenPort];
NSLog(@"%d",isTunnelmade);  //prints YES
NSLog(@"%@", tunnel.TunnelErrors); //Nothing here
NSLog(@"%@", tunnel.TunnelThreadSessionLogPath); //Nothing here
NSLog(@"%d", tunnel.IsAccepting); // Prints YES
NSLog(@"%@", tunnel.GetTunnelsXml); 
NSLog(@"%@", tunnel.ConnectLog);
NSLog(@"%@", tunnel.ListenPort);   //45678

Answer

The call to BeginAccepting starts a background thread that will begin accepting incoming connections. No connection is made to the SSH server at that point. The background thread is simply waiting for incoming connections -- which may come from your foreground thread (such as a newly created TCP socket that makes a connection to localhost:listenPort, or some external program that connects to the listenPort.

When the background thread accepts a new connection on listenPort, it establishes a new connection to the SSH server. Incoming data is forwarded through the SSH server to the DestHostname:DestPort. Data arriving from the other direction (i.e. from DestHostname:DestPort, through the SSH server, and received in the background thread), is forwarded back to the connected socket.


Answer

For more information, see this: SSH Tunneling (Port Forwarding)