Commit 5669d5d6 authored by Simon Morlat's avatar Simon Morlat
Browse files

DNS lookup ready

parent 5f7197f4
......@@ -10,8 +10,10 @@
#include <errno.h>
#include <unistd.h>
/* include all public headers*/
#include "belle-sip/belle-sip.h"
struct _belle_sip_list {
struct _belle_sip_list *next;
struct _belle_sip_list *prev;
......@@ -174,4 +176,7 @@ uint64_t belle_sip_time_ms(void);
}
#endif
/*include private headers */
#include "belle_sip_resolver.h"
#endif
......@@ -36,6 +36,7 @@ void belle_sip_resolver_context_destroy(belle_sip_resolver_context_t *ctx){
static int resolver_callback(belle_sip_resolver_context_t *ctx){
ctx->cb(ctx->cb_data, ctx->name, ctx->ai);
ctx->ai=NULL;
return 0;
}
......@@ -51,17 +52,35 @@ belle_sip_resolver_context_t *belle_sip_resolver_context_new(){
static void *belle_sip_resolver_thread(void *ptr){
belle_sip_resolver_context_t *ctx=(belle_sip_resolver_context_t *)ptr;
struct addrinfo *res=NULL;
struct addrinfo hints={0};
char serv[10];
int err;
snprintf(serv,sizeof(serv),"%i",ctx->port);
hints.ai_family=(ctx->hints & BELLE_SIP_RESOLVER_HINT_IPV6) ? AF_INET6 : AF_INET;
hints.ai_flags=AI_NUMERICSERV;
err=getaddrinfo(ctx->name,serv,&hints,&res);
if (err!=0){
belle_sip_error("DNS resolution of %s failed: %s",ctx->name,gai_strerror(err));
}else{
char tmp[64];
belle_sip_message("%s has address %s.",ctx->name,inet_ntop(res->ai_family,res->ai_addr,tmp,sizeof(tmp)));
ctx->ai=res;
}
if (write(ctx->ctlpipe[1],"q",1)==-1){
belle_sip_error("belle_sip_resolver_thread(): Fail to write on pipe.");
}
return NULL;
}
unsigned long belle_sip_resolve(const char *name, unsigned int hints, belle_sip_resolver_callback_t cb , void *data, belle_sip_main_loop_t *ml){
unsigned long belle_sip_resolve(const char *name, int port, unsigned int hints, belle_sip_resolver_callback_t cb , void *data, belle_sip_main_loop_t *ml){
belle_sip_resolver_context_t *ctx=belle_sip_resolver_context_new();
ctx->cb_data=data;
ctx->cb=cb;
ctx->name=belle_sip_strdup(name);
ctx->port=port;
ctx->hints=hints;
belle_sip_main_loop_add_source(ml,(belle_sip_source_t*)ctx);
pthread_create(&ctx->thread,NULL,belle_sip_resolver_thread,ctx);
......
......@@ -25,7 +25,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BELLE_SIP_RESOLVER_HINT_IPV6 (1)
......@@ -34,7 +34,11 @@
typedef struct belle_sip_resolver_context belle_sip_resolver_context_t;
typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, const struct addrinfo *result);
/**
* Callback prototype for asynchronous DNS resolution. The result addrinfo must be taken and (possibly later) freed by
* the callee, using freeaddrinfo().
**/
typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, struct addrinfo *result);
struct belle_sip_resolver_context{
......@@ -42,6 +46,7 @@ struct belle_sip_resolver_context{
belle_sip_resolver_callback_t cb;
void *cb_data;
char *name;
int port;
struct addrinfo *ai;
unsigned int hints;
pthread_t thread;
......@@ -51,7 +56,7 @@ struct belle_sip_resolver_context{
};
unsigned long belle_sip_resolve(const char *name, unsigned int hints, belle_sip_resolver_callback_t cb , void *data, belle_sip_main_loop_t *ml);
unsigned long belle_sip_resolve(const char *name, int port, unsigned int hints, belle_sip_resolver_callback_t cb , void *data, belle_sip_main_loop_t *ml);
......
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