Subject: libssh2 port forwarding in multithreaded environment

libssh2 port forwarding in multithreaded environment

From: Eric Genevier <>
Date: Wed, 05 Feb 2014 09:59:05 +0100


I am using libssh2 for port forwarding, based on the sample
direct_tcpip.c. I have implemented it as a standard server, where a
thread opens the forwarded port for listening and on connection launches
another thread to deal with it (It is important for http requests where
many requests are made simultaneously) I got many crashes inside
libssh2, even after implementing CRYPTO_set_locking_callback, and after
intense googling, it seemed I had to implement it so that I had a unique
valid session per thread. It now works beautifully.

But I have hit another problem, each thread having its own session means
that each thread needs to authenticate with the ssh server
independently, and if several requests are made quickly, then some get
rejected by the server's firewall for suspected DOS attack. (I have a
thread pool of 4 threads, 4 requests within the same couple of seconds
and I get rejected).

Now, have I missed something? Could one 'main' session be used to
authenticate and then be cloned for each subsequent thread? Any pointer
greatly appreciated


Received on 2014-02-05