Commit 1af7a94d authored by Sandrine Avakian's avatar Sandrine Avakian

Merge branch 'socket_api'

* socket_api:
  Updating error messages with new functions name.
  First commit for belle-sip using bctoolbox socket api.
parents 1ff6f716 aa3fa3f0
......@@ -36,6 +36,7 @@
#include "bctoolbox/logging.h"
#include "bctoolbox/vconnect.h"
BELLE_SIP_BEGIN_DECLS
......@@ -136,6 +137,9 @@ BELLESIP_EXPORT uint64_t belle_sip_time_ms(void);
BELLESIP_EXPORT unsigned int belle_sip_random(void);
/** Connect API */
BELLESIP_EXPORT void belle_sip_set_socket_api(bctbx_vsocket_api_t* my_api);
#if defined(_WIN32)
#include <winsock2.h>
......
......@@ -30,6 +30,7 @@
#include "port.h"
#include <bctoolbox/port.h>
#include <bctoolbox/vconnect.h>
#ifdef HAVE_CONFIG_H
......
......@@ -1148,7 +1148,7 @@ It works on all platform except for windows using ipv6 sockets. TODO: find a wor
*/
int belle_sip_get_src_addr_for(const struct sockaddr *dest, socklen_t destlen, struct sockaddr *src, socklen_t *srclen, int local_port){
int af_type=dest->sa_family;
int sock=(int)socket(af_type,SOCK_DGRAM,IPPROTO_UDP);
int sock=(int)bctbx_socket(af_type,SOCK_DGRAM,IPPROTO_UDP);
int ret = 0;
if (sock==(belle_sip_socket_t)-1){
......@@ -1163,15 +1163,15 @@ int belle_sip_get_src_addr_for(const struct sockaddr *dest, socklen_t destlen, s
and unable to provide a correct local address if the remote address is true ipv6 address when in dual stack mode*/
belle_sip_socket_enable_dual_stack(sock);
}
if (connect(sock,dest,destlen)==-1){
if (bctbx_connect(sock,dest,destlen)==-1){
//if (connect(sock,dest,destlen)==-1){
ret = -get_socket_error();
belle_sip_error("belle_sip_get_src_addr_for: connect() failed: %s",belle_sip_get_socket_error_string_from_code(-ret));
belle_sip_error("belle_sip_get_src_addr_for: bctbx_connect() failed: %s",belle_sip_get_socket_error_string_from_code(-ret));
goto fail;
}
if (getsockname(sock,src,srclen)==-1){
if (bctbx_getsockname(sock,src,srclen)==-1){
ret = -get_socket_error();
belle_sip_error("belle_sip_get_src_addr_for: getsockname() failed: %s",belle_sip_get_socket_error_string_from_code(-ret));
belle_sip_error("belle_sip_get_src_addr_for: bctbx_getsockname() failed: %s",belle_sip_get_socket_error_string_from_code(-ret));
goto fail;
}
......
......@@ -258,6 +258,12 @@ unsigned int belle_sip_random(void){
#endif
}
void belle_sip_set_socket_api(bctbx_vsocket_api_t* my_api){
bctbx_vsocket_api_set_default(my_api);
}
static const char *symbols="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789-~";
/**
......
......@@ -244,7 +244,7 @@ int belle_sip_socket_set_dscp(belle_sip_socket_t sock, int ai_family, int dscp){
belle_sip_error("Cannot set DSCP because socket family is unspecified.");
return -1;
}
retval = setsockopt(sock, proto, value_type, (const char*)&tos, sizeof(tos));
retval = bctbx_setsockopt(sock, proto, value_type, (const char*)&tos, sizeof(tos));
if (retval==-1)
belle_sip_error("Fail to set DSCP value on socket: %s",belle_sip_get_socket_error_string());
return retval;
......@@ -255,9 +255,9 @@ int belle_sip_socket_set_dscp(belle_sip_socket_t sock, int ai_family, int dscp){
int belle_sip_socket_enable_dual_stack(belle_sip_socket_t sock){
int value=0;
int err=setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&value, sizeof(value));
int err=bctbx_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&value, sizeof(value));
if (err==-1){
belle_sip_warning("belle_sip_socket_enable_dual_stack: setsockopt(IPV6_ONLY) failed: %s",belle_sip_get_socket_error_string());
belle_sip_warning("belle_sip_socket_enable_dual_stack: bctbx_setsockopt(IPV6_ONLY) failed: %s",belle_sip_get_socket_error_string());
}
return err;
}
......@@ -22,7 +22,7 @@
static void set_tcp_nodelay(belle_sip_socket_t sock){
int tmp=1;
int err=setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
int err=bctbx_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
if (err == -1){
belle_sip_warning ("Fail to set TCP_NODELAY: %s.", belle_sip_get_socket_error_string());
}
......@@ -124,16 +124,16 @@ int stream_channel_connect(belle_sip_stream_channel_t *obj, const struct addrinf
tmp=1;
obj->base.ai_family=ai->ai_family;
sock=socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
sock=bctbx_socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
if (sock==(belle_sip_socket_t)-1){
belle_sip_error("Could not create socket: %s",belle_sip_get_socket_error_string());
return -1;
}
err=setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
err=bctbx_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof(tmp));
if (err!=0){
belle_sip_error("setsockopt TCP_NODELAY failed: [%s]",belle_sip_get_socket_error_string());
belle_sip_error("bctbx_setsockopt TCP_NODELAY failed: [%s]",belle_sip_get_socket_error_string());
}
belle_sip_socket_set_nonblocking(sock);
if (ai->ai_family==AF_INET6){
......@@ -187,14 +187,14 @@ int finalize_stream_connection(belle_sip_stream_channel_t *obj, unsigned int rev
return -1;
}
err=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void*)&errnum,&optlen);
err=bctbx_getsockopt(sock,SOL_SOCKET,SO_ERROR,(void*)&errnum,&optlen);
if (err!=0){
belle_sip_error("Failed to retrieve connection status for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
return -1;
}else{
if (errnum==0){
/*obtain bind address for client*/
err=getsockname(sock,addr,slen);
err=bctbx_getsockname(sock,addr,slen);
if (err<0){
belle_sip_error("Failed to retrieve sockname for fd [%i]: cause [%s]",sock,belle_sip_get_socket_error_string());
return -1;
......@@ -261,7 +261,7 @@ belle_sip_channel_t * belle_sip_stream_channel_new_child(belle_sip_stack_t *stac
int err;
int optval=1;
err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
err = bctbx_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char*)&optval, sizeof (optval));
if (err == -1){
belle_sip_warning ("Fail to set SIP/TCP address reusable: %s.", belle_sip_get_socket_error_string());
......@@ -269,8 +269,8 @@ belle_sip_channel_t * belle_sip_stream_channel_new_child(belle_sip_stack_t *stac
set_tcp_nodelay(sock);
if (getsockname(sock,(struct sockaddr*)&localaddr,&local_len)==-1){
belle_sip_error("getsockname() failed: %s",belle_sip_get_socket_error_string());
if (bctbx_getsockname(sock,(struct sockaddr*)&localaddr,&local_len)==-1){
belle_sip_error("bctbx_getsockname() failed: %s",belle_sip_get_socket_error_string());
return NULL;
}
......
......@@ -70,6 +70,8 @@ static belle_sip_socket_t create_server_socket(const char *addr, int * port, int
if (*port==-1) *port=0; /*random port for bind()*/
belle_sip_set_socket_api(NULL);
snprintf(portnum,sizeof(portnum),"%i",*port);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
......@@ -81,13 +83,13 @@ static belle_sip_socket_t create_server_socket(const char *addr, int * port, int
return -1;
}
*family=res->ai_family;
sock=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
sock=bctbx_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
if (sock==(belle_sip_socket_t)-1){
belle_sip_error("Cannot create TCP socket: %s",belle_sip_get_socket_error_string());
freeaddrinfo(res);
return -1;
}
err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
err = bctbx_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char*)&optval, sizeof (optval));
if (err == -1){
belle_sip_warning ("Fail to set SIP/TCP address reusable: %s.", belle_sip_get_socket_error_string());
......@@ -108,14 +110,14 @@ static belle_sip_socket_t create_server_socket(const char *addr, int * port, int
if (*port==0){
struct sockaddr_storage saddr;
socklen_t saddr_len=sizeof(saddr);
err=getsockname(sock,(struct sockaddr*)&saddr,&saddr_len);
err=bctbx_getsockname(sock,(struct sockaddr*)&saddr,&saddr_len);
if (err==0){
err=bctbx_getnameinfo((struct sockaddr*)&saddr,saddr_len,NULL,0,portnum,sizeof(portnum),NI_NUMERICSERV|NI_NUMERICHOST);
if (err==0){
*port=atoi(portnum);
belle_sip_message("Random TCP port is %i",*port);
}else belle_sip_error("TCP bind failed, getnameinfo(): %s",gai_strerror(err));
}else belle_sip_error("TCP bind failed, getsockname(): %s",belle_sip_get_socket_error_string());
}else belle_sip_error("TCP bind failed, bctbx_getsockname(): %s",belle_sip_get_socket_error_string());
}
err=listen(sock,64);
......
......@@ -70,6 +70,9 @@ static belle_sip_socket_t create_udp_socket(const char *addr, int *port, int *fa
if (*port==-1) *port=0; /*random port for bind()*/
belle_sip_set_socket_api(NULL);
snprintf(portnum,sizeof(portnum),"%i",*port);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_DGRAM;
......@@ -81,13 +84,13 @@ static belle_sip_socket_t create_udp_socket(const char *addr, int *port, int *fa
return -1;
}
*family=res->ai_family;
sock=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
sock=bctbx_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
if (sock==-1){
belle_sip_error("Cannot create UDP socket: %s",belle_sip_get_socket_error_string());
freeaddrinfo(res);
return -1;
}
err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
err = bctbx_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char*)&optval, sizeof (optval));
if (err == -1){
belle_sip_warning ("Fail to set SIP/UDP address reusable: %s.", belle_sip_get_socket_error_string());
......@@ -96,7 +99,7 @@ static belle_sip_socket_t create_udp_socket(const char *addr, int *port, int *fa
belle_sip_socket_enable_dual_stack(sock);
}
err=bctbx_bind(sock,res->ai_addr,(socklen_t)res->ai_addrlen);
err=bctbx_bind(sock,res->ai_addr,(socklen_t)res->ai_addrlen);
if (err==-1){
belle_sip_error("udp bind() failed for %s port %i: %s",addr,*port,belle_sip_get_socket_error_string());
belle_sip_close_socket(sock);
......@@ -107,14 +110,14 @@ static belle_sip_socket_t create_udp_socket(const char *addr, int *port, int *fa
if (*port==0){
struct sockaddr_storage saddr;
socklen_t saddr_len=sizeof(saddr);
err=getsockname(sock,(struct sockaddr*)&saddr,&saddr_len);
err=bctbx_getsockname(sock,(struct sockaddr*)&saddr,&saddr_len);
if (err==0){
err=bctbx_getnameinfo((struct sockaddr*)&saddr,saddr_len,NULL,0,portnum,sizeof(portnum),NI_NUMERICSERV|NI_NUMERICHOST);
if (err==0){
*port=atoi(portnum);
belle_sip_message("Random UDP port is %i",*port);
}else belle_sip_error("udp bind failed, getnameinfo(): %s",gai_strerror(err));
}else belle_sip_error("udp bind failed, getsockname(): %s",belle_sip_get_socket_error_string());
}else belle_sip_error("udp bind failed, bctbx_getsockname(): %s",belle_sip_get_socket_error_string());
}
return sock;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment