[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: TCP/IP Port Forwarding + Patch


Hello Andreas,
patch generated by "git format-patch -M origin/master" is attached.

regards,
Alex

Am 10.01.2014 19:54, schrieb Andreas Schneider:
> On Friday 10 January 2014 17:50:24 Oleksandr Shneyder wrote:
>> Hello,
>> here is a patch to fix problem with port forwarding.
> 
> Hi,
> 
> that's great. Thank you very much for your contribution.
> 
> Could you create a patch for master and send a git format-patch?
> 
> It is described under "Public Large Project" here:
> 
> http://www.git-scm.com/book/en/Distributed-Git-Contributing-to-a-Project
> 
> 
> Please name the function ssh_channel_accept_forward()
> 
> 
> Thanks!
> 
>> Problem description:
>>
>> //forward port 9080
>> ssh_forward_listen(session, NULL, 9080, NULL);
>> //forward port 9090
>> ssh_forward_listen(session, NULL, 9090, NULL);
>> //accept connections
>> while(1){
>> ssh_channel channel = ssh_forward_accept(session, 1000);
>> //channel created, but we don't know if it from port 9080 or 9090 !!!
>> }
>>
>> Solution - use function, that return destination port (see patch)
>>
>> //forward port 9080
>> ssh_forward_listen(session, NULL, 9080, NULL);
>> //forward port 9090
>> ssh_forward_listen(session, NULL, 9090, NULL);
>> //accept connections
>> while(1){
>> int port;
>> ssh_channel channel = ssh_forward_accept_ex(session, 1000, &port);
>> fprintf(stderr, "forwarding channel from port %d\n", port);
>> }
>>
>> As we need multiply port forwarding in our project (X2Go), please accept
>> my patch or suggest your own solution to solve this problem. We will
>> take care about patching old versions of libssh used by Debian and
>> possible other Linux distributions.
>>
>> regards,
>> Alex
> 


-- 
----------------------------------------------------
Oleksandr Shneyder  | Email: o.shneyder@xxxxxxxxxxxxx
phoca GmbH          | Tel. : 0911 - 14870374 0
Bräuhausgasse 9     | Fax. : 0911 - 14870374 9
D-82205 Gilching    | Mobil: 0163 - 49 64 461

Geschäftsführung:
Dipl.-Inf. Oleksandr Shneyder

Amtsgericht München | http://www.phoca-gmbh.de
HRB 196 658         | http://www.x2go.org
USt-IdNr.: DE281977973
----------------------------------------------------

From 133c67503643f932cb790fdfe3f42b4eb41bd449 Mon Sep 17 00:00:00 2001
From: Oleksandr Shneyder <o.shneyder@xxxxxxxxxxxxx>
Date: Sat, 11 Jan 2014 00:33:33 +0100
Subject: [PATCH] add a function ssh_channel_accept_forward, which work same
 way as ssh_forward_accept but can return a destination port
 of channel (useful if SSH connection forwarding several
 TCP/IP ports)

---
 include/libssh/libssh.h |    1 +
 src/channels.c          |   26 +++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 46aafae..2b125f2 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -406,6 +406,7 @@ LIBSSH_API void ssh_disconnect(ssh_session session);
 LIBSSH_API char *ssh_dirname (const char *path);
 LIBSSH_API int ssh_finalize(void);
 LIBSSH_API ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms);
+LIBSSH_API ssh_channel ssh_channel_accept_forward(ssh_session session, int timeout_ms, int *destination_port);
 LIBSSH_API int ssh_forward_cancel(ssh_session session, const char *address, int port);
 LIBSSH_API int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port);
 LIBSSH_API void ssh_free(ssh_session session);
diff --git a/src/channels.c b/src/channels.c
index 0547889..dc51d95 100644
--- a/src/channels.c
+++ b/src/channels.c
@@ -1989,7 +1989,7 @@ error:
 }
 
 static ssh_channel ssh_channel_accept(ssh_session session, int channeltype,
-    int timeout_ms) {
+    int timeout_ms, int *destination_port) {
 #ifndef _WIN32
   static const struct timespec ts = {
     .tv_sec = 0,
@@ -2016,6 +2016,10 @@ static ssh_channel ssh_channel_accept(ssh_session session, int channeltype,
             ssh_message_subtype(msg) == channeltype) {
           ssh_list_remove(session->ssh_message_list, iterator);
           channel = ssh_message_channel_request_open_reply_accept(msg);
+          if(destination_port) {
+            *destination_port=msg->channel_request_open.destination_port;
+          }
+
           ssh_message_free(msg);
           return channel;
         }
@@ -2046,7 +2050,7 @@ static ssh_channel ssh_channel_accept(ssh_session session, int channeltype,
  *                      the server.
  */
 ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms) {
-  return ssh_channel_accept(channel->session, SSH_CHANNEL_X11, timeout_ms);
+  return ssh_channel_accept(channel->session, SSH_CHANNEL_X11, timeout_ms, NULL);
 }
 
 /**
@@ -2300,7 +2304,23 @@ error:
  *         the server
  */
 ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms) {
-  return ssh_channel_accept(session, SSH_CHANNEL_FORWARDED_TCPIP, timeout_ms);
+  return ssh_channel_accept(session, SSH_CHANNEL_FORWARDED_TCPIP, timeout_ms, NULL);
+}
+
+/**
+ * @brief Accept an incoming TCP/IP forwarding channel and get information
+ * about incomming connection
+ * @param[in]  session    The ssh session to use.
+ *
+ * @param[in]  timeout_ms A timeout in milliseconds.
+ *
+ * @param[in]  destination_port A pointer to destination port or NULL.
+ *
+ * @return Newly created channel, or NULL if no incoming channel request from
+ *         the server
+ */
+ssh_channel ssh_channel_accept_forward(ssh_session session, int timeout_ms, int* destination_port) {
+  return ssh_channel_accept(session, SSH_CHANNEL_FORWARDED_TCPIP, timeout_ms, destination_port);
 }
 
 /**
-- 
1.7.10.4


Attachment: signature.asc
Description: OpenPGP digital signature


Follow-Ups:
Re: TCP/IP Port Forwarding + PatchDustin Oprea <myselfasunder@xxxxxxxxx>
References:
TCP/IP Port Forwarding + PatchOleksandr Shneyder <o.shneyder@xxxxxxxxxxxxx>
Re: TCP/IP Port Forwarding + PatchAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org