belle_sip_resolver.c 4.86 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_resolver.h"

Simon Morlat's avatar
Simon Morlat committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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(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=AF_UNSPEC;
	hints.ai_flags=AI_NUMERICSERV|AI_NUMERICHOST;
	err=getaddrinfo(ipaddress,serv,&hints,&res);
	if (err!=0){
		return NULL;
	}
	return res;
}


Simon Morlat's avatar
Simon Morlat committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
void belle_sip_resolver_context_destroy(belle_sip_resolver_context_t *ctx){
	if (ctx->thread!=0){
		if (!ctx->exited){
			ctx->cancelled=1;
			pthread_cancel(ctx->thread);
		}
		pthread_join(ctx->thread,NULL);
	}
	if (ctx->name)
		belle_sip_free(ctx->name);
	if (ctx->ai){
		freeaddrinfo(ctx->ai);
	}
}

65
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_resolver_context_t);
Simon Morlat's avatar
Simon Morlat committed
66
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_resolver_context_t, belle_sip_source_t,belle_sip_resolver_context_destroy, NULL, NULL,FALSE);
Simon Morlat's avatar
Simon Morlat committed
67

Simon Morlat's avatar
Simon Morlat committed
68
static int resolver_callback(belle_sip_resolver_context_t *ctx){
Simon Morlat's avatar
Simon Morlat committed
69
	char tmp;
Simon Morlat's avatar
Simon Morlat committed
70
	ctx->cb(ctx->cb_data, ctx->name, ctx->ai);
Simon Morlat's avatar
Simon Morlat committed
71
	ctx->ai=NULL;
Simon Morlat's avatar
Simon Morlat committed
72 73 74 75
	if (read(ctx->source.fd,&tmp,1)!=1){
		belle_sip_fatal("Unexpected read from resolver_callback");
	}
	return BELLE_SIP_STOP;
Simon Morlat's avatar
Simon Morlat committed
76 77 78
}

belle_sip_resolver_context_t *belle_sip_resolver_context_new(){
Simon Morlat's avatar
Simon Morlat committed
79
	belle_sip_resolver_context_t *ctx=belle_sip_object_new(belle_sip_resolver_context_t);
Simon Morlat's avatar
Simon Morlat committed
80 81 82 83
	if (pipe(ctx->ctlpipe)==-1){
		belle_sip_fatal("pipe() failed: %s",strerror(errno));
	}
	belle_sip_fd_source_init(&ctx->source,(belle_sip_source_func_t)resolver_callback,ctx,ctx->ctlpipe[0],BELLE_SIP_EVENT_READ,-1);
Simon Morlat's avatar
Simon Morlat committed
84
	ctx->source.on_remove=(belle_sip_source_remove_callback_t)belle_sip_object_unref;
Simon Morlat's avatar
Simon Morlat committed
85 86 87 88 89
	return ctx;
}

static void *belle_sip_resolver_thread(void *ptr){
	belle_sip_resolver_context_t *ctx=(belle_sip_resolver_context_t *)ptr;
Simon Morlat's avatar
Simon Morlat committed
90 91 92 93 94
	struct addrinfo *res=NULL;
	struct addrinfo hints={0};
	char serv[10];
	int err;

Simon Morlat's avatar
Simon Morlat committed
95
	belle_sip_message("Resolver thread started.");
Simon Morlat's avatar
Simon Morlat committed
96 97 98 99 100 101 102
	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{
Simon Morlat's avatar
Simon Morlat committed
103 104 105
		char host[64];
		belle_sip_addrinfo_to_ip(res,host,sizeof(host),NULL);
		belle_sip_message("%s has address %s.",ctx->name,host);
Simon Morlat's avatar
Simon Morlat committed
106 107 108
		ctx->ai=res;
	}
	
Simon Morlat's avatar
Simon Morlat committed
109 110 111 112 113 114
	if (write(ctx->ctlpipe[1],"q",1)==-1){
		belle_sip_error("belle_sip_resolver_thread(): Fail to write on pipe.");
	}
	return NULL;
}

Simon Morlat's avatar
Simon Morlat committed
115
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){
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	struct addrinfo *res=belle_sip_ip_address_to_addrinfo (name, port);
	if (res==NULL){
		/*then perform asynchronous DNS query */
		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);
		return ctx->source.id;
	}else{
		cb(data,name,res);
		return 0;
	}
Simon Morlat's avatar
Simon Morlat committed
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
void belle_sip_get_src_addr_for(const struct sockaddr *dest, socklen_t destlen, struct sockaddr *src, socklen_t *srclen){
	int af_type=(destlen==sizeof(struct sockaddr_in6)) ? AF_INET6 : AF_INET;
	int sock=socket(af_type,SOCK_DGRAM,IPPROTO_UDP);

	memset(src,0,*srclen);
	
	if (sock==-1){
		belle_sip_fatal("Could not create socket: %s",strerror(errno));
		return;
	}
	if (connect(sock,dest,destlen)==-1){
		belle_sip_error("belle_sip_get_src_addr_for: connect() failed: %s",strerror(errno));
		close(sock);
		return;
	}
	if (getsockname(sock,src,srclen)==-1){
		belle_sip_error("belle_sip_get_src_addr_for: getsockname() failed: %s",strerror(errno));
		close(sock);
		return;
	}
}