Commit 2d107266 authored by Ghislain MARY's avatar Ghislain MARY

Move conversion functions between IP address as string and addrinfo to bctoolbox.

parent 8b6ea1ad
......@@ -51,9 +51,6 @@ BELLESIP_EXPORT unsigned short belle_sip_dns_srv_get_weight(const belle_sip_dns_
BELLESIP_EXPORT unsigned short belle_sip_dns_srv_get_port(const belle_sip_dns_srv_t *obj);
BELLESIP_EXPORT int belle_sip_addrinfo_to_ip(const struct addrinfo *ai, char *ip, size_t ip_size, int *port);
BELLESIP_EXPORT struct addrinfo * belle_sip_ip_address_to_addrinfo(int family, const char *ipaddress, int port);
/**
* Asynchronously performs DNS SRV followed A/AAAA query. Automatically fallbacks to A/AAAA if SRV isn't found.
......
......@@ -436,7 +436,7 @@ static void append_dns_result(belle_sip_simple_resolver_context_t *ctx, struct a
return;
}
if (ctx->flags & AI_V4MAPPED) family=AF_INET6;
*ai_list = ai_list_append(*ai_list, belle_sip_ip_address_to_addrinfo(family, host, ctx->port));
*ai_list = ai_list_append(*ai_list, bctbx_ip_address_to_addrinfo(family, host, ctx->port));
belle_sip_message("%s resolved to %s", ctx->name, host);
}
......@@ -688,41 +688,6 @@ static belle_sip_simple_resolver_context_t * resolver_start_query(belle_sip_simp
}
int belle_sip_addrinfo_to_ip(const struct addrinfo *ai, char *ip, size_t ip_size, int *port){
char serv[16];
int err=getnameinfo(ai->ai_addr,ai->ai_addrlen,ip,ip_size,serv,sizeof(serv),NI_NUMERICHOST|NI_NUMERICSERV);
if (err!=0){
belle_sip_error("getnameinfo() error: %s",gai_strerror(err));
strncpy(ip,"<bug!!>",ip_size);
}
if (port) *port=atoi(serv);
return 0;
}
struct addrinfo * belle_sip_ip_address_to_addrinfo(int family, const char *ipaddress, int port){
struct addrinfo *res=NULL;
struct addrinfo hints={0};
char serv[10];
int err;
snprintf(serv,sizeof(serv),"%i",port);
hints.ai_family=family;
hints.ai_flags=AI_NUMERICSERV|AI_NUMERICHOST;
hints.ai_socktype=SOCK_STREAM; //not used but it's needed to specify it because otherwise getaddrinfo returns one struct addrinfo per socktype.
if (family==AF_INET6 && strchr(ipaddress,':')==NULL) {
hints.ai_flags|=AI_V4MAPPED;
}
err=bctbx_getaddrinfo(ipaddress,serv,&hints,&res);
if (err!=0){
if (err!=EAI_NONAME)
belle_sip_error("belle_sip_ip_address_to_addrinfo(): getaddrinfo() error: %s",gai_strerror(err));
return NULL;
}
return res;
}
static void belle_sip_combined_resolver_context_destroy(belle_sip_combined_resolver_context_t *obj){
if (obj->name != NULL) {
belle_sip_free(obj->name);
......@@ -994,7 +959,7 @@ static void process_srv_results(void *data, const char *name, belle_sip_list_t *
* Perform combined SRV + A / AAAA resolution.
**/
belle_sip_resolver_context_t * belle_sip_stack_resolve(belle_sip_stack_t *stack, const char *transport, const char *name, int port, int family, belle_sip_resolver_callback_t cb, void *data) {
struct addrinfo *res = belle_sip_ip_address_to_addrinfo(family, name, port);
struct addrinfo *res = bctbx_ip_address_to_addrinfo(family, name, port);
if (res == NULL) {
/* First perform asynchronous DNS SRV query */
belle_sip_combined_resolver_context_t *ctx = belle_sip_object_new(belle_sip_combined_resolver_context_t);
......@@ -1091,7 +1056,7 @@ static belle_sip_resolver_context_t * belle_sip_stack_resolve_dual(belle_sip_sta
}
belle_sip_resolver_context_t * belle_sip_stack_resolve_a(belle_sip_stack_t *stack, const char *name, int port, int family, belle_sip_resolver_callback_t cb , void *data) {
struct addrinfo *res = belle_sip_ip_address_to_addrinfo(family, name, port);
struct addrinfo *res = bctbx_ip_address_to_addrinfo(family, name, port);
if (res == NULL) {
switch(family){
case AF_UNSPEC:
......@@ -1169,7 +1134,7 @@ void belle_sip_get_src_addr_for(const struct sockaddr *dest, socklen_t destlen,
return;
fail:
{
struct addrinfo *res = belle_sip_ip_address_to_addrinfo(af_type, af_type == AF_INET ? "127.0.0.1" : "::1", local_port);
struct addrinfo *res = bctbx_ip_address_to_addrinfo(af_type, af_type == AF_INET ? "127.0.0.1" : "::1", local_port);
if (res != NULL) {
memcpy(src,res->ai_addr,MIN((size_t)*srclen,res->ai_addrlen));
*srclen=res->ai_addrlen;
......
......@@ -736,7 +736,7 @@ void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_stack_t *stack,c
obj->simulated_recv_return=1;/*not set*/
if (peername){
/*check if we are given a real dns name or just an ip address*/
struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_UNSPEC,peername,peer_port);
struct addrinfo *ai=bctbx_ip_address_to_addrinfo(AF_UNSPEC,peername,peer_port);
if (ai) bctbx_freeaddrinfo(ai);
else obj->has_name=TRUE;
}
......@@ -754,9 +754,9 @@ void belle_sip_channel_init_with_addr(belle_sip_channel_t *obj, belle_sip_stack_
ai.ai_family=peer_addr->sa_family;
ai.ai_addr=(struct sockaddr*)peer_addr;
ai.ai_addrlen=addrlen;
belle_sip_addrinfo_to_ip(&ai,remoteip,sizeof(remoteip),&peer_port);
bctbx_addrinfo_to_ip_address(&ai,remoteip,sizeof(remoteip),&peer_port);
belle_sip_channel_init(obj,stack,bindip,localport,NULL,remoteip,peer_port);
obj->peer_list=obj->current_peer=belle_sip_ip_address_to_addrinfo(ai.ai_family, obj->peer_name,obj->peer_port);
obj->peer_list=obj->current_peer=bctbx_ip_address_to_addrinfo(ai.ai_family, obj->peer_name,obj->peer_port);
obj->ai_family=ai.ai_family;
}
......@@ -1395,7 +1395,7 @@ void belle_sip_channel_connect(belle_sip_channel_t *obj){
int port=obj->peer_port;
channel_set_state(obj,BELLE_SIP_CHANNEL_CONNECTING);
belle_sip_addrinfo_to_ip(obj->current_peer,ip,sizeof(ip),&port);
bctbx_addrinfo_to_ip_address(obj->current_peer,ip,sizeof(ip),&port);
/* update peer_port as it may have been overriden by SRV resolution*/
if (port!=obj->peer_port){
/*the SRV resolution provided a port number that must be used*/
......
......@@ -522,7 +522,7 @@ static int tls_process_http_connect(belle_sip_tls_channel_t *obj) {
int err;
char ip[64];
int port;
belle_sip_addrinfo_to_ip(channel->current_peer,ip,sizeof(ip),&port);
bctbx_addrinfo_to_ip_address(channel->current_peer,ip,sizeof(ip),&port);
request = belle_sip_strdup_printf("CONNECT %s:%i HTTP/1.1\r\nProxy-Connection: keep-alive\r\nConnection: keep-alive\r\nHost: %s\r\nUser-Agent: Mozilla/5.0\r\n"
,ip
......
......@@ -123,7 +123,7 @@ static void ipv4_a_query(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -150,7 +150,7 @@ static void ipv4_cname_a_query(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_CNAME_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_CNAME_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -287,7 +287,7 @@ static void ipv6_aaaa_query(void) {
/*the IPv6 address shall return first, and must be a real ipv6 address*/
BC_ASSERT_EQUAL(client->ai_list->ai_family,AF_INET6,int,"%d");
BC_ASSERT_FALSE(IN6_IS_ADDR_V4MAPPED(&sock_in6->sin6_addr));
ai = belle_sip_ip_address_to_addrinfo(AF_INET6, IPV6_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET6, IPV6_SIP_IP, SIP_PORT);
BC_ASSERT_PTR_NOT_NULL(ai);
if (ai) {
struct in6_addr *ipv6_address = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
......@@ -305,7 +305,7 @@ static void ipv6_aaaa_query(void) {
BC_ASSERT_EQUAL(next->ai_family,AF_INET6,int,"%d");
BC_ASSERT_TRUE(IN6_IS_ADDR_V4MAPPED(&sock_in6->sin6_addr));
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET6, IPV6_SIP_IPV4, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET6, IPV6_SIP_IPV4, SIP_PORT);
BC_ASSERT_PTR_NOT_NULL(ai);
if (ai) {
struct in6_addr *ipv6_address = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
......@@ -371,7 +371,7 @@ static void srv_a_query_no_srv_result(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_CNAME_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_CNAME_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -413,7 +413,7 @@ static void no_query_needed(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -461,7 +461,7 @@ static void dns_fallback(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -497,7 +497,7 @@ static void ipv6_dns_server(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......@@ -533,7 +533,7 @@ static void ipv4_and_ipv6_dns_server(void) {
struct sockaddr_in *sock_in = (struct sockaddr_in *)client->ai_list->ai_addr;
int ntohsi = (int)ntohs(sock_in->sin_port);
BC_ASSERT_EQUAL(ntohsi, SIP_PORT, int, "%d");
ai = belle_sip_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
ai = bctbx_ip_address_to_addrinfo(AF_INET, IPV4_SIP_IP, SIP_PORT);
if (ai) {
BC_ASSERT_EQUAL(sock_in->sin_addr.s_addr, ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr, int, "%d");
bctbx_freeaddrinfo(ai);
......
......@@ -37,7 +37,7 @@ static belle_sip_object_pool_t *pool;
static int leaked_objects_count;
static int _belle_sip_tester_ipv6_available(void){
struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_INET6,"2a01:e00::2",53);
struct addrinfo *ai=bctbx_ip_address_to_addrinfo(AF_INET6,"2a01:e00::2",53);
if (ai){
struct sockaddr_storage ss;
struct addrinfo src;
......@@ -47,7 +47,7 @@ static int _belle_sip_tester_ipv6_available(void){
belle_sip_get_src_addr_for(ai->ai_addr,ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
src.ai_addr=(struct sockaddr*) &ss;
src.ai_addrlen=slen;
belle_sip_addrinfo_to_ip(&src,localip, sizeof(localip),&port);
bctbx_addrinfo_to_ip_address(&src,localip, sizeof(localip),&port);
bctbx_freeaddrinfo(ai);
return strcmp(localip,"::1")!=0;
}
......
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