Skip to content

Commit

Permalink
Expose the real ports (src/dst) used by TC-based write op
Browse files Browse the repository at this point in the history
As src/dst ports may be ephemeral, expose (via holders in socket_info struct) the real used ports during the last write operation

Related to #1692

(cherry picked from commit 9dde6d5)
  • Loading branch information
bogdan-iancu committed Jun 11, 2019
1 parent d698b42 commit fdbdcf1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
7 changes: 7 additions & 0 deletions modules/proto_bin/proto_bin.c
Expand Up @@ -556,6 +556,9 @@ static int proto_bin_send(struct socket_info* send_sock,
if (n==0) {
/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

/* connect is still in progress, break the sending
* flow now (the actual write will be done when
* connect will be completed */
Expand Down Expand Up @@ -594,6 +597,8 @@ static int proto_bin_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

/* we successfully added our write chunk - success */
tcp_conn_release(c, 0);
Expand Down Expand Up @@ -631,6 +636,8 @@ static int proto_bin_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

tcp_conn_release(c, (n<len)?1:0/*pending data in async mode?*/ );
return n;
Expand Down
6 changes: 6 additions & 0 deletions modules/proto_hep/proto_hep.c
Expand Up @@ -727,6 +727,8 @@ static int hep_tcp_send (struct socket_info* send_sock,
if (n==0) {
/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;
/* connect is still in progress, break the sending
* flow now (the actual write will be done when
* connect will be completed */
Expand Down Expand Up @@ -766,6 +768,8 @@ static int hep_tcp_send (struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

/* we successfully added our write chunk - success */
tcp_conn_release(c, 0);
Expand Down Expand Up @@ -803,6 +807,8 @@ static int hep_tcp_send (struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

tcp_conn_release(c, (n<len)?1:0/*pending data in async mode?*/ );

Expand Down
2 changes: 2 additions & 0 deletions modules/proto_tls/proto_tls.c
Expand Up @@ -494,6 +494,8 @@ static int proto_tls_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

tcp_conn_release(c, 0);
return n;
Expand Down
2 changes: 2 additions & 0 deletions modules/proto_ws/proto_ws.c
Expand Up @@ -487,6 +487,8 @@ static int proto_ws_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

tcp_conn_release(c, 0);
return n;
Expand Down
2 changes: 2 additions & 0 deletions modules/proto_wss/proto_wss.c
Expand Up @@ -520,6 +520,8 @@ static int proto_wss_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

tcp_conn_release(c, 0);
return n;
Expand Down
14 changes: 10 additions & 4 deletions net/proto_tcp/proto_tcp.c
Expand Up @@ -793,7 +793,8 @@ static int proto_tcp_send(struct socket_info* send_sock,
LM_ERR("Unknown destination - cannot open new tcp connection\n");
return -1;
}
LM_DBG("no open tcp connection found, opening new one, async = %d\n",tcp_async);
LM_DBG("no open tcp connection found, opening new one, async = %d\n",
tcp_async);
/* create tcp connection */
if (tcp_async) {
n = tcpconn_async_connect(send_sock, to, buf, len, &c, &fd);
Expand All @@ -807,23 +808,24 @@ static int proto_tcp_send(struct socket_info* send_sock,
ip_addr2a( &c->rcv.src_ip ), c->rcv.src_port,
ip_addr2a( &c->rcv.dst_ip ), c->rcv.dst_port );



if (n==0) {
/* trace the message */
if ( TRACE_ON( c->flags ) &&
check_trace_route( trace_filter_route_id, c) ) {
if ( tcpconn2su( c, &src_su, &dst_su) < 0 ) {
LM_ERR("can't create su structures for tracing!\n");
} else {
trace_message_atonce( PROTO_TCP, c->cid, &src_su, &dst_su,
trace_message_atonce( PROTO_TCP, c->cid,
&src_su, &dst_su,
TRANS_TRACE_CONNECT_START, TRANS_TRACE_SUCCESS,
&AS_CONNECT_INIT, t_dst );
}
}

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

/* connect is still in progress, break the sending
* flow now (the actual write will be done when
Expand Down Expand Up @@ -912,6 +914,8 @@ static int proto_tcp_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

/* we successfully added our write chunk - success */
sh_log(c->hist, TCP_SEND2MAIN, "send 3, (%d)", c->refcnt);
Expand Down Expand Up @@ -958,6 +962,8 @@ static int proto_tcp_send(struct socket_info* send_sock,

/* mark the ID of the used connection (tracing purposes) */
last_outgoing_tcp_id = c->id;
send_sock->last_local_real_port = c->rcv.dst_port;
send_sock->last_remote_real_port = c->rcv.src_port;

sh_log(c->hist, TCP_SEND2MAIN, "send 6, (%d, async: %d)", c->refcnt, n < len);
tcp_conn_release(c, (n<len)?1:0/*pending data in async mode?*/ );
Expand Down
10 changes: 10 additions & 0 deletions socket_info.h
Expand Up @@ -54,6 +54,16 @@ struct socket_info {
struct ip_addr adv_address; /* Advertised address in ip_addr form (for find_si) */
unsigned short adv_port; /* optimization for grep_sock_info() */
unsigned short children;
/* these are IP-level local/remote ports used during the last write op via
* this sock (or a connection belonging to this sock). These values are
* optional (populated only by the TCP-based protocol, for ephemeral ports.
* Note: they are populate ONLY by a write op and they are not ever reset,
* they are simply overwritten by the next write op on this socket/conn.
* IMPORTANT: when reading them, be sure you are just after a write ops,
* otherwise you may read old data here */
unsigned short last_local_real_port;
unsigned short last_remote_real_port;

struct socket_info* next;
struct socket_info* prev;
};
Expand Down

0 comments on commit fdbdcf1

Please sign in to comment.