Problem
I try to do local port forwarding using libssh with the libssh-C++-wrapper. My intention is to forward port localhost:3306
on a server to localhost:3307
on my machine via SSH to connect via MySQL to localhost:3307
.
void ssh_session::forward(){
ssh::Channel channel(this->session);
//remotehost, remoteport, localhost, localport
channel.openForward("localhost",3306,"localhost",3307);
std::cout<< "Channel is " << (channel.isOpen()?"open!":"closed!") << std::endl;
}
with session
in the constructor of ssh::Channel
being of type ssh::Session
.
The code above prints Channel is open!
. If I try to connect to localhost:3307
using the MySQL Connector/C++ I get
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
Observations
- If I use the shell command
$ ssh -L 3307:localhost:3306 [email protected]
everything works fine and I can connect. - If I use
ssh::Session session
used in the constructor orssh::Channel channel
to execute remote shell commands everything works therefore the session is fine! - The documentation of libssh (which is total crap for the C++ wrapper
libsshpp.hpp
since a lot of public member functions are not documented and you have to look into the source code) shows thatssh::Channel::openForward()
is a wrapper for the C functionssh_channel_open_forward()
- The documentation of
ssh_channel_open_forward()
statesWarning
This function does not bind the local port and does not automatically forward the content of a socket to the channel. You still have to use channel_read and channel_write for this.
I think that could cause the problem. I have no problem by reading and writing in to the ssh:Channel
but thats not how the MySQL Connector/C++ works.
Question
How can I achieve the same behaviour produced by the common shell command
$ ssh -L 3307:localhost:3306 [email protected]
using libssh?
int mycppfunc(){return mycfunc()}
. Sincelibssh
is one of only two reliable libraries for SSH written in C I think a lot of C programmers are familiar with it. I see no reason to remove the C tag. – Dup