Skip to content

Commit

Permalink
net: allow tcp connection creation without sending to main
Browse files Browse the repository at this point in the history
This is needed by proto WS and WSS
  • Loading branch information
razvancrainea committed Jan 15, 2021
1 parent 46a3b42 commit 7d12ba1
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 44 deletions.
5 changes: 3 additions & 2 deletions modules/proto_bin/proto_bin.c
Expand Up @@ -176,7 +176,8 @@ static int proto_bin_send(struct socket_info* send_sock,
LM_DBG("no open tcp connection found, opening new one, async = %d\n",bin_async);
/* create tcp connection */
if (bin_async) {
n = tcp_async_connect(send_sock, to, bin_async_local_connect_timeout, &c, &fd);
n = tcp_async_connect(send_sock, to,
bin_async_local_connect_timeout, &c, &fd, 1);
if ( n<0 ) {
LM_ERR("async TCP connect failed\n");
return -1;
Expand All @@ -202,7 +203,7 @@ static int proto_bin_send(struct socket_info* send_sock,
return len;
}
/* our first connect attempt succeeded - go ahead as normal */
} else if ((c=tcp_sync_connect(send_sock, to, &fd))==0) {
} else if ((c=tcp_sync_connect(send_sock, to, &fd, 1))==0) {
LM_ERR("connect failed\n");
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/proto_hep/proto_hep.c
Expand Up @@ -355,7 +355,7 @@ static int hep_tcp_send (struct socket_info* send_sock,
LM_DBG("no open tcp connection found, opening new one, async = %d\n",hep_async);
/* create tcp connection */
if (hep_async) {
n = tcp_async_connect(send_sock, to, hep_async_local_connect_timeout, &c, &fd);
n = tcp_async_connect(send_sock, to, hep_async_local_connect_timeout, &c, &fd, 1);
if ( n<0 ) {
LM_ERR("async TCP connect failed\n");
return -1;
Expand All @@ -380,7 +380,7 @@ static int hep_tcp_send (struct socket_info* send_sock,
return len;
}
/* our first connect attempt succeeded - go ahead as normal */
} else if ((c=tcp_sync_connect(send_sock, to, &fd))==0) {
} else if ((c=tcp_sync_connect(send_sock, to, &fd, 1))==0) {
LM_ERR("connect failed\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/proto_smpp/smpp.c
Expand Up @@ -807,7 +807,7 @@ static struct tcp_connection *smpp_connect(smpp_session_t *session, int *fd)
LM_ERR("error getting send socket\n");
return NULL;
}
return tcp_sync_connect(send_socket, &server, fd);
return tcp_sync_connect(send_socket, &server, fd, 1);
}

static int smpp_send_msg(smpp_session_t *smsc, str *buffer)
Expand Down
2 changes: 1 addition & 1 deletion modules/proto_tls/proto_tls.c
Expand Up @@ -439,7 +439,7 @@ static int proto_tls_send(struct socket_info* send_sock,
}
LM_DBG("no open tcp connection found, opening new one\n");
/* create tcp connection */
if ((c=tcp_sync_connect(send_sock, to, &fd))==0) {
if ((c=tcp_sync_connect(send_sock, to, &fd, 1))==0) {
LM_ERR("connect failed\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/proto_ws/ws_common.h
Expand Up @@ -662,7 +662,7 @@ static struct tcp_connection* ws_sync_connect(struct socket_info* send_sock,
LM_ERR("tcp_blocking_connect failed\n");
goto error;
}
con=tcp_conn_new(s, server, send_sock, S_CONN_OK);
con=tcp_conn_create(s, server, send_sock, S_CONN_OK, 0);
if (con==NULL){
LM_ERR("tcp_conn_create failed, closing the socket\n");
goto error;
Expand Down
1 change: 0 additions & 1 deletion modules/proto_wss/proto_wss.c
Expand Up @@ -357,7 +357,6 @@ static void wss_report(int type, unsigned long long conn_id, int conn_flags,



>>>>>> tcp: merge similar code in a common tcp file
/************** WRITE related functions ***************/


Expand Down
30 changes: 10 additions & 20 deletions net/net_tcp.c
Expand Up @@ -815,26 +815,14 @@ static struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
}


/* creates a new tcp connection structure and informs the TCP Main on that
/* creates a new tcp connection structure
* if send2main is 1, the function informs the TCP Main about the new conn
* a +1 ref is set for the new conn !
* IMPORTANT - the function assumes you want to create a new TCP conn as
* a result of a connect operation - the conn will be set as connect !!
* Accepted connection are triggered internally only */
struct tcp_connection* tcp_conn_create(int sock, union sockaddr_union* su,
struct socket_info* si, int state)
{
struct tcp_connection *c;

/* create the connection structure */
c = tcp_conn_new(sock, su, si, state);
if (c==NULL)
return NULL;

return (tcp_conn_send(c) == 0 ? c : NULL);
}

struct tcp_connection* tcp_conn_new(int sock, union sockaddr_union* su,
struct socket_info* si, int state)
struct socket_info* si, int state, int send2main)
{
struct tcp_connection *c;

Expand All @@ -844,9 +832,6 @@ struct tcp_connection* tcp_conn_new(int sock, union sockaddr_union* su,
LM_ERR("tcpconn_new failed\n");
return NULL;
}
c->refcnt++; /* safe to do it w/o locking, it's not yet
available to the rest of the world */
sh_log(c->hist, TCP_REF, "connect, (%d)", c->refcnt);

if (protos[c->type].net.conn_init &&
protos[c->type].net.conn_init(c) < 0) {
Expand All @@ -857,9 +842,14 @@ struct tcp_connection* tcp_conn_new(int sock, union sockaddr_union* su,
}
c->flags |= F_CONN_INIT;

return c;
}
c->refcnt++; /* safe to do it w/o locking, it's not yet
available to the rest of the world */
sh_log(c->hist, TCP_REF, "connect, (%d)", c->refcnt);
if (!send2main)
return c;

return (tcp_conn_send(c) == 0 ? c : NULL);
}

/* sends a new connection from a worker to main */
int tcp_conn_send(struct tcp_connection *c)
Expand Down
9 changes: 2 additions & 7 deletions net/net_tcp.h
Expand Up @@ -85,14 +85,9 @@ int tcp_init_sock_opt(int s);
int tcp_conn_get(int id, struct ip_addr* ip, int port, enum sip_protos proto,
void *proto_extra_id, struct tcp_connection** conn, int* conn_fd);

/* creates a new tcp conn around a newly connected socket
* and sends it to the master */
struct tcp_connection* tcp_conn_create(int sock, union sockaddr_union* su,
struct socket_info* si, int state);

/* creates a new tcp conn around a newly connected socket */
struct tcp_connection* tcp_conn_new(int sock, union sockaddr_union* su,
struct socket_info* si, int state);
struct tcp_connection* tcp_conn_create(int sock, union sockaddr_union* su,
struct socket_info* si, int state, int send2main);

/* sends a connected connection to the master */
int tcp_conn_send(struct tcp_connection *con);
Expand Down
5 changes: 3 additions & 2 deletions net/proto_tcp/proto_tcp.c
Expand Up @@ -395,7 +395,8 @@ static int proto_tcp_send(struct socket_info* send_sock,
tcp_async);
/* create tcp connection */
if (tcp_async) {
n = tcp_async_connect(send_sock, to, tcp_async_local_connect_timeout, &c, &fd);
n = tcp_async_connect(send_sock, to,
tcp_async_local_connect_timeout, &c, &fd, 1);
if ( n<0 ) {
LM_ERR("async TCP connect failed\n");
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
Expand Down Expand Up @@ -456,7 +457,7 @@ static int proto_tcp_send(struct socket_info* send_sock,
}
}
} else {
if ((c=tcp_sync_connect(send_sock, to, &fd))==0) {
if ((c=tcp_sync_connect(send_sock, to, &fd, 1))==0) {
LM_ERR("connect failed\n");
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
return -1;
Expand Down
10 changes: 5 additions & 5 deletions net/tcp_common.c
Expand Up @@ -176,7 +176,7 @@ int tcp_sync_connect_fd(union sockaddr_union* src, union sockaddr_union* dst)
}

struct tcp_connection* tcp_sync_connect(struct socket_info* send_sock,
union sockaddr_union* server, int *fd)
union sockaddr_union* server, int *fd, int send2main)
{
struct tcp_connection* con;
int s;
Expand All @@ -185,7 +185,7 @@ struct tcp_connection* tcp_sync_connect(struct socket_info* send_sock,
if (s < 0)
return NULL;

con=tcp_conn_create(s, server, send_sock, S_CONN_OK);
con=tcp_conn_create(s, server, send_sock, S_CONN_OK, send2main);
if (con==NULL){
LM_ERR("tcp_conn_create failed, closing the socket\n");
close(s);
Expand All @@ -197,7 +197,7 @@ struct tcp_connection* tcp_sync_connect(struct socket_info* send_sock,

int tcp_async_connect(struct socket_info* send_sock,
union sockaddr_union* server, int timeout,
struct tcp_connection** c, int *ret_fd)
struct tcp_connection** c, int *ret_fd, int send2main)
{
int fd, n;
union sockaddr_union my_name;
Expand Down Expand Up @@ -320,7 +320,7 @@ int tcp_async_connect(struct socket_info* send_sock,
async_connect:
LM_DBG("Create connection for async connect\n");
/* create a new dummy connection */
con=tcp_conn_create(fd, server, send_sock, S_CONN_CONNECTING);
con=tcp_conn_create(fd, server, send_sock, S_CONN_CONNECTING, send2main);
if (con==NULL) {
LM_ERR("tcp_conn_create failed\n");
goto error;
Expand All @@ -330,7 +330,7 @@ int tcp_async_connect(struct socket_info* send_sock,
return 0;

local_connect:
con=tcp_conn_create(fd, server, send_sock, S_CONN_OK);
con=tcp_conn_create(fd, server, send_sock, S_CONN_OK, send2main);
if (con==NULL) {
LM_ERR("tcp_conn_create failed, closing the socket\n");
goto error;
Expand Down
4 changes: 2 additions & 2 deletions net/tcp_common.h
Expand Up @@ -32,7 +32,7 @@ int tcp_connect_blocking_timeout(int s, const struct sockaddr *servaddr,
int tcp_sync_connect_fd(union sockaddr_union* src, union sockaddr_union* dst);

struct tcp_connection* tcp_sync_connect(struct socket_info* send_sock,
union sockaddr_union* server, int *fd);
union sockaddr_union* server, int *fd, int send2main);

/* Attempts do a connect to the given destination. It returns:
* 1 - connect was done local (completed)
Expand All @@ -41,7 +41,7 @@ struct tcp_connection* tcp_sync_connect(struct socket_info* send_sock,
*/
int tcp_async_connect(struct socket_info* send_sock,
union sockaddr_union* server, int timeout,
struct tcp_connection** c, int *ret_fd);
struct tcp_connection** c, int *ret_fd, int send2main);

/* Responsible for writing the TCP send chunks - called under con write lock
* * if returns = 1 : the connection will be released for more writting
Expand Down

0 comments on commit 7d12ba1

Please sign in to comment.