Skip to content

Commit 1a02329

Browse files
authored
Merge pull request #1 from shaddybaddah/sb.name/contribs-rtunnfix
Fix reverse tunnel connect/data handler race condition
2 parents 7617785 + dd1b7f7 commit 1a02329

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

client/channel.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,27 @@ unsigned char channel_request_tunnel(
308308
return tid;
309309
}
310310

311+
/**
312+
* acknowledge to the server that the client successfully
313+
* connected the reverse tunnel, so that it may now send
314+
* data down the tunnel
315+
* @param[in] tid the tunnel ID
316+
*/
317+
void channel_rconnect_tunnel(unsigned char tid)
318+
{
319+
r2tmsg_t *msg;
320+
321+
assert(tid != 0xff);
322+
trace_chan("tid=0x%02x", tid);
323+
324+
msg = write_reserve(2, NULL);
325+
if (msg) {
326+
msg->cmd = R2TCMD_RCONN;
327+
msg->id = tid;
328+
write_commit(2);
329+
}
330+
}
331+
311332
/**
312333
* notify the server a tunnel has been closed
313334
* @param[in] tid the tunnel ID

client/r2tcli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void channel_pong(void);
126126
unsigned char channel_request_tunnel(unsigned char, const char *, unsigned short, int);
127127
int channel_forward_recv(netsock_t *);
128128
int channel_forward_iobuf(iobuf_t *, unsigned char);
129+
void channel_rconnect_tunnel(unsigned char);
129130
void channel_close_tunnel(unsigned char);
130131

131132
// controller.c

client/tunnel.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ int tunnel_write(netsock_t *ns, const void *buf, unsigned int len)
411411
*/
412412
int tunnel_write_event(netsock_t *ns)
413413
{
414-
if ((ns->type == NETSOCK_RTUNCLI) && (ns->state != NETSTATE_CONNECTED))
414+
if ((ns->type == NETSOCK_RTUNCLI) && (ns->state != NETSTATE_CONNECTED)) {
415415
ns->state = NETSTATE_CONNECTED;
416+
channel_rconnect_tunnel(ns->tid);
417+
}
416418

417419
return netsock_write(ns, NULL, 0);
418420
}

common/print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ int error(const char *fmt, ...)
169169
*/
170170
void print_xfer(const char *name, char rw, unsigned int size)
171171
{
172-
debug(1, (rw=='r'?"%-6s < %-8u":"%-6s %8u >"), name, size);
172+
debug(PRINT_DBG, (rw=='r'?"%-6s < %-8u":"%-6s %8u >"), name, size);
173173
}
174174

175175
#ifdef DEBUG

server/commands.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,27 @@ static int cmd_data(const r2tmsg_t *msg, unsigned int len)
100100
return tunnel_write(tun, ((const char *)msg)+2, len-2);
101101
}
102102

103+
static int cmd_rconn(const r2tmsg_t *msg, unsigned int len)
104+
{
105+
tunnel_t *tun;
106+
107+
trace_chan("len=%u, id=0x%02x", len, msg->id);
108+
tun = tunnel_lookup(msg->id);
109+
if (!tun) {
110+
error("invalid tunnel id 0x%02x", msg->id);
111+
return 0;
112+
}
113+
tun->connected = 1;
114+
115+
return 0;
116+
}
117+
103118
const cmdhandler_t cmd_handlers[R2TCMD_MAX] = {
104119
(cmdhandler_t) cmd_conn, /* R2TCMD_CONN */
105120
(cmdhandler_t) cmd_close, /* R2TCMD_CLOSE */
106121
(cmdhandler_t) cmd_data, /* R2TCMD_DATA */
107122
NULL,
108123
(cmdhandler_t) cmd_bind, /* R2TCMD_BIND */
109-
NULL
124+
(cmdhandler_t) cmd_rconn /* R2TCMD_RCONN */
110125
};
111126

server/tunnel.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ static int tunnel_accept_event(tunnel_t *tun)
416416
}
417417
cli->sock.fd = cli_sock.fd;
418418
cli->sock.evt = cli_sock.evt;
419-
cli->connected = 1;
419+
cli->connected = 0;
420420
cli->id = tid;
421421
iobuf_init2(&cli->rio.buf, &cli->wio.buf, "tcp");
422422
list_add_tail(&cli->list, &all_tunnels);
@@ -501,7 +501,8 @@ int tunnel_event(tunnel_t *tun, HANDLE h)
501501

502502
if ((ret >= 0) && (evt & FD_READ)) {
503503
debug(0, "FD_READ");
504-
ret = tunnel_sockrecv_event(tun);
504+
if (tun->connected)
505+
ret = tunnel_sockrecv_event(tun);
505506
if (evt & FD_CLOSE)
506507
while ((ret = tunnel_sockrecv_event(tun)) > 0);
507508
}

0 commit comments

Comments
 (0)