How to detect a tunnel disconnect?

Jan 8, 2015 at 7:13 AM
Hello,

I have this piece of code which establish a remote Port an a Linux mashine:
namespace SSHTest
{
    using System;
    using System.Threading;

    using Renci.SshNet;
    using Renci.SshNet.Common;

    using SSHTest.Properties;

    class Program
    {
        static void Main(string[] args)
        {
            Thread thread1 = new Thread(A);
            thread1.Start();

            Console.WriteLine("Run SSH Thread");
            Console.ReadKey();
        }

        private static void A()
        {
            try
            {
                var ci = new ConnectionInfo("192.168.206.133", 22, "dominik", new PasswordAuthenticationMethod("dominik", Resources.passwd));

                using (var client = new SshClient(ci))
                {
                    client.Connect();
                    client.KeepAliveInterval = new TimeSpan(0, 0, 0, 5);
                    client.ErrorOccurred += client_ErrorOccurred;
                    var fport = new ForwardedPortRemote("192.168.206.133", 8013, "172.24.224.125", 22);
                    client.AddForwardedPort(fport);
                    fport.Start();
                    fport.Exception += fPort_Exception;
                    Console.WriteLine(fport.IsStarted ? "port ok" : "port failed");
                    while (client.IsConnected)
                    {
                        Thread.Sleep(200);
                    }
                }

                Console.WriteLine("Thread Ended !");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Thread Ended Error!");
            }
        }

        static void client_ErrorOccurred(object sender, ExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }

        static void fPort_Exception(object sender, ExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.Message);
        }
    }
}
It is only for testing and the code is not the best.
Anyway ... If I disconnect the Linux machine from the network nothing happens. Same problem is I just disable the network card. It seems that the tunnel still exist.

So how can I detect that the SSH tunnel is broken / disconnected?

I would have expected that I got some kind of exception. But nothing happend.

So is there a general mistake in my code? Or can´t ssh.net detect a broken ssh tunnel?

Any help would be much appreciated.

Dominik
Jan 8, 2015 at 7:39 AM
Edited Jan 8, 2015 at 7:49 AM
Hi,

The ssh connection is still up?

As long as the ssh connection is connected, the tunnel is also OK from the ssh point of view.
If the packets can reach their destination after they leave the tunnel is not business of SSH.
Which SSH.NET version do you use?

Is the usage of ForwardedPortRemote() intended? Your forwarding looks like you meant ForwardedPortLocal()
But maybe I need another coffee :)
Jan 8, 2015 at 7:52 AM
Hi !
The ssh connection is still up?
Well if I shut down the remote Server the SSH connection should be down. Hopefully :-)

Which SSH.NET version do you use?
I used the latest downloadable version 2014.4.6-beta2.
Dominik
Jan 8, 2015 at 9:05 AM
Edited Jan 8, 2015 at 9:06 AM
Sorry, I don't know your setup, especially which machine you meant with "linux machine"...

The remote forwarding is started on the linux machine, so it is the job of the ssh server to manage the tunnel.

If you disconnect the server, the ssh connection is kinda still there till timeout, which depends on your ssh server config AFAIR.
Then the server will close it's sockets, including your forwarding.

There is no way the client can see whats happening on the server without having a connection to it.
Cause there is no socket on the ssh client side for remote forwarding. It's all happening on the server side.

I can easily reproduce this with OpenSSH
ssh -R 2222:test1:22 test2 -l root

# netstat -ant | grep 2222
tcp          0      0  127.0.0.1.2222         *.*                    LISTEN
tcp6         0      0  ::1.2222               *.*                    LISTEN

# ifconfig em0 down
Connection hangs, but tunnel on 2222 is still there on test2
# netstat -ant | grep 2222
tcp          0      0  127.0.0.1.2222         *.*                    LISTEN
tcp6         0      0  ::1.2222               *.*                    LISTEN
Wait a few seconds, and up em0
# ifconfig em0 up
# netstat -ant | grep 2222
tcp          0      0  127.0.0.1.2222         *.*                    LISTEN
tcp6         0      0  ::1.2222               *.*                    LISTEN
Same ssh connection still works.