Discussion:
Reverse port forwarding requests server implementation
Marco Ganci
2013-10-13 10:26:00 UTC
Permalink
Hi,
I'm trying to implement a ssh server, obiousvly using libssh, that accept
reverse port forwarding requests coming from clients.

For the client implementation I'm using with success the example available
in the tutorial ( http://api.libssh.org/master/libssh_tutor_forwarding.html ),
but for server I'm able to find only example about authentication, channel
open/close/write/read, and no references about how to accept incoming
reverse port forwading requests.

I see that in the source code there are some undocumented functions that
talks about general request and so on, but also after several attempes I'm
not able to find a solution.

Below some code.

Client:
...
if ( rc == SSH_AUTH_SUCCESS )
{
DebugStream() << "Authentication completed with success";
//forward connection
rc = ssh_forward_listen(ssh_session, NULL, 3863 , NULL);
if (rc != SSH_OK)
{
DebugStream() << "Error opening remote port:" << ssh_get_error(ssh_session);
DebugStream() << "Error opening remote port:" <<
ssh_get_error_code(ssh_session);
}

DebugStream() << "Waiting forward connections";
ssh_channel channel = ssh_forward_accept(ssh_session, 60000);
if (channel == NULL)
{
DebugStream() << "Error waiting for incoming connection:" <<
ssh_get_error(ssh_session);
}
DebugStream() << "Connection accepted";

}
...

Server:
...
do { //waiting for open channel request message = ssh_message_get(session);
log("Message received"); if(message){ switch(ssh_message_type(message)){
case SSH_REQUEST_GLOBAL:
if(ssh_message_subtype(message)==SSH_GLOBAL_REQUEST_TCPIP_FORWARD){
ssh_message_global_request_reply_success(message,3863);
ssh_message_free(message); break; } default:
ssh_message_reply_default(message); ssh_message_free(message); } } }
while(message); log("Forward request ok!");
...

Is this code correct?
Any suggestion?
Thank you in advance

Best Regards
--
-- Marco Ganci
-- mak82-VGgt2q2+T+***@public.gmane.org
-- marco.ganci-***@public.gmane.org
Andreas Schneider
2013-10-19 16:57:34 UTC
Permalink
Post by Marco Ganci
Hi,
Hey,
Post by Marco Ganci
I'm trying to implement a ssh server, obiousvly using libssh, that accept
reverse port forwarding requests coming from clients.
I suggest you take a look at the libssh 0.6.0rc1 example directory. Especially
the samplesshd-cb.c example using the new callback system. You need to
implement the callbacks for accepting forward connections and implement them.
Post by Marco Ganci
For the client implementation I'm using with success the example available
in the tutorial ( http://api.libssh.org/master/libssh_tutor_forwarding.html
), but for server I'm able to find only example about authentication,
channel open/close/write/read, and no references about how to accept
incoming reverse port forwading requests.
There is none. But look also at the examples/proxy.c file.
Post by Marco Ganci
I see that in the source code there are some undocumented functions that
talks about general request and so on, but also after several attempes I'm
not able to find a solution.
Which functions are you talking about?
Post by Marco Ganci
Below some code.
...
if ( rc == SSH_AUTH_SUCCESS )
{
DebugStream() << "Authentication completed with success";
//forward connection
rc = ssh_forward_listen(ssh_session, NULL, 3863 , NULL);
if (rc != SSH_OK)
{
DebugStream() << "Error opening remote port:" << ssh_get_error(ssh_session);
DebugStream() << "Error opening remote port:" <<
ssh_get_error_code(ssh_session);
}
DebugStream() << "Waiting forward connections";
ssh_channel channel = ssh_forward_accept(ssh_session, 60000);
if (channel == NULL)
{
DebugStream() << "Error waiting for incoming connection:" <<
ssh_get_error(ssh_session);
}
DebugStream() << "Connection accepted";
}
...
...
do { //waiting for open channel request message = ssh_message_get(session);
log("Message received"); if(message){ switch(ssh_message_type(message)){
if(ssh_message_subtype(message)==SSH_GLOBAL_REQUEST_TCPIP_FORWARD){
ssh_message_global_request_reply_success(message,3863);
ssh_message_reply_default(message); ssh_message_free(message); } } }
while(message); log("Forward request ok!");
..
Use the new callback system which is much easier.


Sorry for the late reply.



-- andreas
--
Andreas Schneider GPG-ID: CC014E3D
www.cryptomilk.org asn-***@public.gmane.org
Loading...