Commit 3184f153 authored by Simon Morlat's avatar Simon Morlat

set resolver api public, simplify it, attach it to stack object.

parent f3f343c3
......@@ -19,6 +19,7 @@ bellesip_HEADERS=\
belle-sdp.h \
belle-sip.h \
refresher.h \
defs.h
defs.h \
resolver.h
EXTRA_DIST=$(bellesip_HEADERS)
......@@ -155,6 +155,7 @@ typedef struct belle_sip_hop belle_sip_hop_t;
#include "belle-sip/transaction.h"
#include "belle-sip/dialog.h"
#include "belle-sip/sipstack.h"
#include "belle-sip/resolver.h"
#include "belle-sip/listeningpoint.h"
#include "belle-sip/provider.h"
#include "belle-sip/auth-helper.h"
......
......@@ -20,20 +20,11 @@
#ifndef belle_sip_resolver_h
#define belle_sip_resolver_h
#include "belle_sip_internal.h"
#include "dns.h"
typedef struct belle_sip_resolver_context belle_sip_resolver_context_t;
#define BELLE_SIP_RESOLVER_CONTEXT(obj) BELLE_SIP_CAST(obj,belle_sip_resolver_context_t)
/**
* Callback prototype for asynchronous DNS A and AAAA resolution.
* The ai_list contains addrinfo elements that must be taken and (possibly later) freed by the callee, using freeaddrinfo().
* These elements are linked by their ai_next field.
**/
typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, struct addrinfo *ai_list);
/**
* Callback prototype for asynchronous DNS SRV resolution.
......@@ -41,36 +32,21 @@ typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, stru
*/
typedef void (*belle_sip_resolver_srv_callback_t)(void *data, const char *name, belle_sip_list_t *srv_list);
struct belle_sip_resolver_context{
belle_sip_source_t source;
belle_sip_stack_t *stack;
belle_sip_main_loop_t *ml;
belle_sip_resolver_callback_t cb;
belle_sip_resolver_srv_callback_t srv_cb;
void *cb_data;
struct dns_resolv_conf *resconf;
struct dns_hosts *hosts;
struct dns_resolver *R;
enum dns_type type;
char *name;
int port;
struct addrinfo *ai_list;
belle_sip_list_t *srv_list;
int family;
uint8_t cancelled;
uint8_t started;
uint8_t done;
};
/**
* Callback prototype for asynchronous DNS A and AAAA resolution.
* The ai_list contains addrinfo elements that must be taken and (possibly later) freed by the callee, using freeaddrinfo().
* These elements are linked by their ai_next field.
**/
typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, struct addrinfo *ai_list);
BELLE_SIP_BEGIN_DECLS
int belle_sip_addrinfo_to_ip(const struct addrinfo *ai, char *ip, size_t ip_size, int *port);
BELLESIP_INTERNAL_EXPORT struct addrinfo * belle_sip_ip_address_to_addrinfo(int family, const char *ipaddress, int port);
BELLESIP_INTERNAL_EXPORT unsigned long belle_sip_resolve(belle_sip_stack_t *stack, const char *name, const char *transport, int port, int family, belle_sip_resolver_callback_t cb, void *data, belle_sip_main_loop_t *ml);
BELLESIP_INTERNAL_EXPORT unsigned long belle_sip_resolve_a(belle_sip_stack_t *stack, const char *name, int port, int family, belle_sip_resolver_callback_t cb, void *data, belle_sip_main_loop_t *ml);
BELLESIP_INTERNAL_EXPORT unsigned long belle_sip_resolve_srv(belle_sip_stack_t *stack, const char *name, const char *transport, belle_sip_resolver_srv_callback_t cb, void *data, belle_sip_main_loop_t *ml);
void belle_sip_resolve_cancel(belle_sip_main_loop_t *ml, unsigned long id);
BELLESIP_EXPORT struct addrinfo * belle_sip_ip_address_to_addrinfo(int family, const char *ipaddress, int port);
BELLESIP_EXPORT unsigned long belle_sip_stack_resolve(belle_sip_stack_t *stack, const char *name, const char *transport, int port, int family, belle_sip_resolver_callback_t cb, void *data);
BELLESIP_EXPORT unsigned long 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);
BELLESIP_EXPORT unsigned long belle_sip_stack_resolve_srv(belle_sip_stack_t *stack, const char *name, const char *transport, belle_sip_resolver_srv_callback_t cb, void *data);
BELLESIP_EXPORT void belle_sip_stack_resolve_cancel(belle_sip_stack_t *stack, unsigned long id);
/**
* Lookups the source address from local interface that can be used to connect to a destination address.
......
......@@ -171,6 +171,11 @@ BELLESIP_EXPORT char *belle_sip_unquote_strdup(const char *str);
typedef SOCKET belle_sip_socket_t;
typedef HANDLE belle_sip_fd_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
typedef int belle_sip_socket_t;
typedef int belle_sip_fd_t;
......
......@@ -868,9 +868,6 @@ belle_sip_auth_event_t* belle_sip_auth_event_create(const char* realm,const char
belle_sip_refresher_t* belle_sip_refresher_new(belle_sip_client_transaction_t* transaction);
/*include private headers */
#include "belle_sip_resolver.h"
/*
* returns a char, even if entry is escaped*/
int belle_sip_get_char (const char*a,int n,char*out);
......
......@@ -16,7 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "belle_sip_resolver.h"
#include "belle_sip_internal.h"
#include "dns.h"
#include <stdlib.h>
#ifdef __APPLE__
......@@ -26,6 +27,28 @@
#define DNS_EAGAIN EAGAIN
struct belle_sip_resolver_context{
belle_sip_source_t source;
belle_sip_stack_t *stack;
belle_sip_main_loop_t *ml;
belle_sip_resolver_callback_t cb;
belle_sip_resolver_srv_callback_t srv_cb;
void *cb_data;
struct dns_resolv_conf *resconf;
struct dns_hosts *hosts;
struct dns_resolver *R;
enum dns_type type;
char *name;
int port;
struct addrinfo *ai_list;
belle_sip_list_t *srv_list;
int family;
uint8_t cancelled;
uint8_t started;
uint8_t done;
};
static struct dns_resolv_conf *resconf(belle_sip_resolver_context_t *ctx) {
#if !_WIN32 && !HAVE_RESINIT
/*#if !_WIN32 && (!HAVE_RESINIT || !TARGET_OS_IPHONE)*/
......@@ -454,14 +477,14 @@ static void process_srv_results(void *data, const char *name, belle_sip_list_t *
}
}
unsigned long belle_sip_resolve(belle_sip_stack_t *stack, const char *transport, const char *name, int port, int family, belle_sip_resolver_callback_t cb, void *data, belle_sip_main_loop_t *ml) {
unsigned long 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);
if (res == NULL) {
/* Then perform asynchronous DNS SRV query */
struct belle_sip_recursive_resolve_data *rec_data = belle_sip_malloc0(sizeof(struct belle_sip_recursive_resolve_data));
belle_sip_resolver_context_t *ctx = belle_sip_object_new(belle_sip_resolver_context_t);
ctx->stack = stack;
ctx->ml = ml;
ctx->ml = stack->ml;
ctx->cb_data = rec_data;
ctx->srv_cb = process_srv_results;
ctx->name = belle_sip_concat(srv_prefix_from_transport(transport), name, NULL);
......@@ -481,13 +504,13 @@ unsigned long belle_sip_resolve(belle_sip_stack_t *stack, const char *transport,
}
}
unsigned long belle_sip_resolve_a(belle_sip_stack_t *stack, const char *name, int port, int family, belle_sip_resolver_callback_t cb , void *data, belle_sip_main_loop_t *ml) {
unsigned long 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);
if (res == NULL) {
/* Then perform asynchronous DNS A or AAAA query */
belle_sip_resolver_context_t *ctx = belle_sip_object_new(belle_sip_resolver_context_t);
ctx->stack = stack;
ctx->ml = ml;
ctx->ml = stack->ml;
ctx->cb_data = data;
ctx->cb = cb;
ctx->name = belle_sip_strdup(name);
......@@ -503,10 +526,10 @@ unsigned long belle_sip_resolve_a(belle_sip_stack_t *stack, const char *name, in
}
}
unsigned long belle_sip_resolve_srv(belle_sip_stack_t *stack, const char *name, const char *transport, belle_sip_resolver_srv_callback_t cb, void *data, belle_sip_main_loop_t *ml) {
unsigned long belle_sip_stack_resolve_srv(belle_sip_stack_t *stack, const char *name, const char *transport, belle_sip_resolver_srv_callback_t cb, void *data) {
belle_sip_resolver_context_t *ctx = belle_sip_object_new(belle_sip_resolver_context_t);
ctx->stack = stack;
ctx->ml = ml;
ctx->ml = stack->ml;
ctx->cb_data = data;
ctx->srv_cb = cb;
ctx->name = belle_sip_concat(srv_prefix_from_transport(transport), name, NULL);
......@@ -514,9 +537,9 @@ unsigned long belle_sip_resolve_srv(belle_sip_stack_t *stack, const char *name,
return resolver_start_query(ctx);
}
void belle_sip_resolve_cancel(belle_sip_main_loop_t *ml, unsigned long id){
void belle_sip_stack_resolve_cancel(belle_sip_stack_t *stack, unsigned long id){
if (id!=0){
belle_sip_source_t *s=belle_sip_main_loop_find_source(ml,id);
belle_sip_source_t *s=belle_sip_main_loop_find_source(stack->ml,id);
if (s){
belle_sip_resolver_context_t *res=BELLE_SIP_RESOLVER_CONTEXT(s);
res->cancelled=1;
......
......@@ -60,7 +60,7 @@ static void belle_sip_channel_destroy(belle_sip_channel_t *obj){
belle_sip_free(obj->peer_name);
if (obj->local_ip) belle_sip_free(obj->local_ip);
obj->listeners=for_each_weak_unref_free(obj->listeners,(belle_sip_object_destroy_notify_t)belle_sip_channel_remove_listener,obj);
if (obj->resolver_id>0) belle_sip_resolve_cancel(belle_sip_stack_get_main_loop(obj->stack),obj->resolver_id);
if (obj->resolver_id>0) belle_sip_stack_resolve_cancel(obj->stack,obj->resolver_id);
if (obj->inactivity_timer){
belle_sip_main_loop_remove_source(obj->stack->ml,obj->inactivity_timer);
belle_sip_object_unref(obj->inactivity_timer);
......@@ -728,7 +728,7 @@ static void channel_res_done(void *data, const char *name, struct addrinfo *ai_l
void belle_sip_channel_resolve(belle_sip_channel_t *obj){
channel_set_state(obj,BELLE_SIP_CHANNEL_RES_IN_PROGRESS);
obj->resolver_id=belle_sip_resolve_a(obj->stack, obj->peer_name, obj->peer_port, obj->lp->ai_family, channel_res_done, obj, obj->stack->ml);
obj->resolver_id=belle_sip_stack_resolve_a(obj->stack, obj->peer_name, obj->peer_port, obj->lp->ai_family, channel_res_done, obj);
if (obj->resolver_id==-1){
channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
}
......
......@@ -115,7 +115,7 @@ static void ipv4_a_query(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve_a(client->stack, IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......@@ -139,7 +139,7 @@ static void ipv4_a_query_no_result(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve_a(client->stack, IPV4_SIP_BAD_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, IPV4_SIP_BAD_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_EQUAL(client->ai_list, NULL);
......@@ -153,7 +153,7 @@ static void ipv4_a_query_send_failure(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
belle_sip_stack_set_resolver_send_error(client->stack, -1);
client->resolver_id = belle_sip_resolve_a(client->stack, IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_EQUAL(client->resolver_id, 0);
belle_sip_stack_set_resolver_send_error(client->stack, 0);
......@@ -167,7 +167,7 @@ static void ipv4_a_query_timeout(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
belle_sip_stack_set_dns_timeout(client->stack, 0);
client->resolver_id = belle_sip_resolve_a(client->stack, "toto.com", SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, "toto.com", SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, 200));
CU_ASSERT_PTR_EQUAL(client->ai_list, NULL);
......@@ -182,7 +182,7 @@ static void ipv4_a_query_multiple_results(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve_a(client->stack, IPV4_MULTIRES_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, IPV4_MULTIRES_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......@@ -201,7 +201,7 @@ static void ipv6_aaaa_query(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve_a(client->stack, IPV6_SIP_DOMAIN, SIP_PORT, AF_INET6, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_a(client->stack, IPV6_SIP_DOMAIN, SIP_PORT, AF_INET6, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......@@ -229,7 +229,7 @@ static void srv_query(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve_srv(client->stack, SRV_DOMAIN, "udp", srv_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve_srv(client->stack, SRV_DOMAIN, "udp", srv_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->srv_list, NULL);
......@@ -249,7 +249,7 @@ static void srv_a_query(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve(client->stack, "udp", SRV_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve(client->stack, "udp", SRV_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......@@ -265,7 +265,7 @@ static void srv_a_query_no_srv_result(void) {
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
timeout = belle_sip_stack_get_dns_timeout(client->stack);
client->resolver_id = belle_sip_resolve(client->stack, "udp", IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve(client->stack, "udp", IPV4_SIP_DOMAIN, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_NOT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(wait_for(client->stack, &client->resolve_done, 1, timeout));
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......@@ -288,7 +288,7 @@ static void no_query_needed(void) {
endpoint_t *client = create_endpoint();
CU_ASSERT_PTR_NOT_NULL_FATAL(client);
client->resolver_id = belle_sip_resolve(client->stack, "udp", IPV4_SIP_IP, SIP_PORT, AF_INET, a_resolve_done, client, belle_sip_stack_get_main_loop(client->stack));
client->resolver_id = belle_sip_stack_resolve(client->stack, "udp", IPV4_SIP_IP, SIP_PORT, AF_INET, a_resolve_done, client);
CU_ASSERT_EQUAL(client->resolver_id, 0);
CU_ASSERT_TRUE(client->resolve_done);
CU_ASSERT_PTR_NOT_EQUAL(client->ai_list, NULL);
......
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