Skip to content

Commit

Permalink
ws & wss: merge duplicate ws_connect code
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Feb 6, 2020
1 parent da22c08 commit 30a7f47
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 164 deletions.
82 changes: 0 additions & 82 deletions modules/proto_ws/proto_ws.c
Expand Up @@ -314,88 +314,6 @@ static void ws_report(int type, unsigned long long conn_id, int conn_flags,



static struct tcp_connection* ws_sync_connect(struct socket_info* send_sock,
union sockaddr_union* server)
{
int s;
union sockaddr_union my_name;
socklen_t my_name_len;
struct tcp_connection* con;

s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
if (s==-1){
LM_ERR("socket: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (tcp_init_sock_opt(s)<0){
LM_ERR("tcp_init_sock_opt failed\n");
goto error;
}
my_name_len = sockaddru_len(send_sock->su);
memcpy( &my_name, &send_sock->su, my_name_len);
su_setport( &my_name, 0);
if (bind(s, &my_name.s, my_name_len )!=0) {
LM_ERR("bind failed (%d) %s\n", errno,strerror(errno));
goto error;
}

if (tcp_connect_blocking(s, &server->s, sockaddru_len(*server))<0){
LM_ERR("tcp_blocking_connect failed\n");
goto error;
}
con=tcp_conn_new(s, server, send_sock, S_CONN_OK);
if (con==NULL){
LM_ERR("tcp_conn_create failed, closing the socket\n");
goto error;
}
/* it is safe to move this here and clear it after we complete the
* handshake, just before sending the fd to main */
con->fd = s;
return con;
error:
/* close the opened socket */
if (s!=-1) close(s);
return 0;
}

static struct tcp_connection* ws_connect(struct socket_info* send_sock,
union sockaddr_union* to, int *fd)
{
struct tcp_connection *c;

if ((c=ws_sync_connect(send_sock, to))==0) {
LM_ERR("connect failed\n");
return NULL;
}
/* the state of the connection should be NONE, otherwise something is
* wrong */
if (WS_TYPE(c) != WS_NONE) {
LM_BUG("invalid type for connection %d\n", WS_TYPE(c));
goto error;
}
WS_TYPE(c) = WS_CLIENT;

if (ws_client_handshake(c) < 0) {
LM_ERR("cannot complete WebSocket handshake\n");
goto error;
}

*fd = c->fd;
/* clear the fd, just in case */
c->fd = -1;
/* handshake done - send the socket to main */
if (tcp_conn_send(c) < 0) {
LM_ERR("cannot send socket to main\n");
goto error;
}

return c;
error:
tcp_conn_destroy(c);
return NULL;
}


/************** WRITE related functions ***************/


Expand Down
84 changes: 84 additions & 0 deletions modules/proto_ws/ws_common.h
Expand Up @@ -633,4 +633,88 @@ static void ws_close(struct tcp_connection *c)
ws_send_close(c);
}

static struct tcp_connection* ws_sync_connect(struct socket_info* send_sock,
union sockaddr_union* server)
{
int s;
union sockaddr_union my_name;
socklen_t my_name_len;
struct tcp_connection* con;

s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
if (s==-1){
LM_ERR("socket: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (tcp_init_sock_opt(s)<0){
LM_ERR("tcp_init_sock_opt failed\n");
goto error;
}
my_name_len = sockaddru_len(send_sock->su);
memcpy( &my_name, &send_sock->su, my_name_len);
su_setport( &my_name, 0);
if (bind(s, &my_name.s, my_name_len )!=0) {
LM_ERR("bind failed (%d) %s\n", errno,strerror(errno));
goto error;
}

if (tcp_connect_blocking(s, &server->s, sockaddru_len(*server))<0){
LM_ERR("tcp_blocking_connect failed\n");
goto error;
}
con=tcp_conn_new(s, server, send_sock, S_CONN_OK);
if (con==NULL){
LM_ERR("tcp_conn_create failed, closing the socket\n");
goto error;
}
/* it is safe to move this here and clear it after we complete the
* handshake, just before sending the fd to main */
con->fd = s;
return con;
error:
/* close the opened socket */
if (s!=-1) close(s);
return 0;
}


static struct tcp_connection* ws_connect(struct socket_info* send_sock,
union sockaddr_union* to, int *fd)
{
struct tcp_connection *c;

if ((c=ws_sync_connect(send_sock, to))==0) {
LM_ERR("connect failed\n");
return NULL;
}
/* the state of the connection should be NONE, otherwise something is
* wrong */
if (WS_TYPE(c) != WS_NONE) {
LM_BUG("invalid type for connection %d\n", WS_TYPE(c));
goto error;
}
WS_TYPE(c) = WS_CLIENT;

if (ws_client_handshake(c) < 0) {
LM_ERR("cannot complete WebSocket handshake\n");
goto error;
}

*fd = c->fd;
/* clear the fd, just in case */
c->fd = -1;
/* handshake done - send the socket to main */
if (tcp_conn_send(c) < 0) {
LM_ERR("cannot send socket to main\n");
goto error;
}

return c;
error:
tcp_conn_destroy(c);
return NULL;
}



#endif /* _WS_COMMON_H_ */
82 changes: 0 additions & 82 deletions modules/proto_wss/proto_wss.c
Expand Up @@ -354,88 +354,6 @@ static void wss_report(int type, unsigned long long conn_id, int conn_flags,



static struct tcp_connection* ws_sync_connect(struct socket_info* send_sock,
union sockaddr_union* server)
{
int s;
union sockaddr_union my_name;
socklen_t my_name_len;
struct tcp_connection* con;

s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
if (s==-1){
LM_ERR("socket: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (tcp_init_sock_opt(s)<0){
LM_ERR("tcp_init_sock_opt failed\n");
goto error;
}
my_name_len = sockaddru_len(send_sock->su);
memcpy( &my_name, &send_sock->su, my_name_len);
su_setport( &my_name, 0);
if (bind(s, &my_name.s, my_name_len )!=0) {
LM_ERR("bind failed (%d) %s\n", errno,strerror(errno));
goto error;
}

if (tcp_connect_blocking(s, &server->s, sockaddru_len(*server))<0){
LM_ERR("tcp_blocking_connect failed\n");
goto error;
}
con=tcp_conn_new(s, server, send_sock, S_CONN_OK);
if (con==NULL){
LM_ERR("tcp_conn_create failed, closing the socket\n");
goto error;
}
/* it is safe to move this here and clear it after we complete the
* handshake, just before sending the fd to main */
con->fd = s;
return con;
error:
/* close the opened socket */
if (s!=-1) close(s);
return 0;
}

static struct tcp_connection* ws_connect(struct socket_info* send_sock,
union sockaddr_union* to, int *fd)
{
struct tcp_connection *c;

if ((c=ws_sync_connect(send_sock, to))==0) {
LM_ERR("connect failed\n");
return NULL;
}
/* the state of the connection should be NONE, otherwise something is
* wrong */
if (WS_TYPE(c) != WS_NONE) {
LM_BUG("invalid type for connection %d\n", WS_TYPE(c));
goto error;
}
WS_TYPE(c) = WS_CLIENT;

if (ws_client_handshake(c) < 0) {
LM_ERR("cannot complete WebSocket handshake\n");
goto error;
}

*fd = c->fd;
/* clear the fd, just in case */
c->fd = -1;
/* handshake done - send the socket to main */
if (tcp_conn_send(c) < 0) {
LM_ERR("cannot send socket to main\n");
goto error;
}

return c;
error:
tcp_conn_destroy(c);
return NULL;
}


/************** WRITE related functions ***************/


Expand Down

0 comments on commit 30a7f47

Please sign in to comment.