belle_sip_resolver.c 5.04 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
void belle_sip_resolver_context_destroy(belle_sip_resolver_context_t *ctx){
	if (ctx->thread!=0){
		if (!ctx->exited){
			ctx->cancelled=1;
Simon Morlat's avatar
Simon Morlat committed
54
			belle_sip_thread_cancel(ctx->thread);
Simon Morlat's avatar
Simon Morlat committed
55
		}
Simon Morlat's avatar
Simon Morlat committed
56
		belle_sip_thread_join(ctx->thread,NULL);
Simon Morlat's avatar
Simon Morlat committed
57 58 59 60 61 62
	}
	if (ctx->name)
		belle_sip_free(ctx->name);
	if (ctx->ai){
		freeaddrinfo(ctx->ai);
	}
Simon Morlat's avatar
Simon Morlat committed
63
#ifndef WIN32
Simon Morlat's avatar
Simon Morlat committed
64 65
	close(ctx->ctlpipe[0]);
	close(ctx->ctlpipe[1]);
Simon Morlat's avatar
Simon Morlat committed
66 67 68
#else
	CloseEvent(ctx->ctlevent);
#endif
Simon Morlat's avatar
Simon Morlat committed
69 70
}

71
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_resolver_context_t);
Simon Morlat's avatar
Simon Morlat committed
72
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
73

Simon Morlat's avatar
Simon Morlat committed
74
static int resolver_callback(belle_sip_resolver_context_t *ctx){
Simon Morlat's avatar
Simon Morlat committed
75
	char tmp;
Simon Morlat's avatar
Simon Morlat committed
76
	ctx->cb(ctx->cb_data, ctx->name, ctx->ai);
Simon Morlat's avatar
Simon Morlat committed
77
	ctx->ai=NULL;
Simon Morlat's avatar
Simon Morlat committed
78
#ifndef WIN32
Simon Morlat's avatar
Simon Morlat committed
79 80 81
	if (read(ctx->source.fd,&tmp,1)!=1){
		belle_sip_fatal("Unexpected read from resolver_callback");
	}
Simon Morlat's avatar
Simon Morlat committed
82 83
#else
#endif
Simon Morlat's avatar
Simon Morlat committed
84
	return BELLE_SIP_STOP;
Simon Morlat's avatar
Simon Morlat committed
85 86 87
}

belle_sip_resolver_context_t *belle_sip_resolver_context_new(){
Simon Morlat's avatar
Simon Morlat committed
88
	belle_sip_resolver_context_t *ctx=belle_sip_object_new(belle_sip_resolver_context_t);
Simon Morlat's avatar
Simon Morlat committed
89
#ifndef WIN32
Simon Morlat's avatar
Simon Morlat committed
90 91 92
	if (pipe(ctx->ctlpipe)==-1){
		belle_sip_fatal("pipe() failed: %s",strerror(errno));
	}
Simon Morlat's avatar
Simon Morlat committed
93 94
#else
#endif
Simon Morlat's avatar
Simon Morlat committed
95 96 97 98 99 100
	belle_sip_fd_source_init(&ctx->source,(belle_sip_source_func_t)resolver_callback,ctx,ctx->ctlpipe[0],BELLE_SIP_EVENT_READ,-1);
	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
101 102 103 104 105
	struct addrinfo *res=NULL;
	struct addrinfo hints={0};
	char serv[10];
	int err;

Simon Morlat's avatar
Simon Morlat committed
106
	belle_sip_message("Resolver thread started.");
Simon Morlat's avatar
Simon Morlat committed
107 108 109 110 111 112 113
	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
114 115 116
		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
117 118
		ctx->ai=res;
	}
Simon Morlat's avatar
Simon Morlat committed
119
#ifndef WIN32
Simon Morlat's avatar
Simon Morlat committed
120 121 122
	if (write(ctx->ctlpipe[1],"q",1)==-1){
		belle_sip_error("belle_sip_resolver_thread(): Fail to write on pipe.");
	}
Simon Morlat's avatar
Simon Morlat committed
123 124 125
#else
	SetEvent(ctx->ctlevent);
#endif
Simon Morlat's avatar
Simon Morlat committed
126 127 128
	return NULL;
}

Simon Morlat's avatar
Simon Morlat committed
129
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){
130 131 132 133 134 135 136 137 138 139
	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);
Simon Morlat's avatar
Simon Morlat committed
140
		belle_sip_object_unref(ctx);
Simon Morlat's avatar
Simon Morlat committed
141
		belle_sip_thread_create(&ctx->thread,NULL,belle_sip_resolver_thread,ctx);
142 143 144 145 146
		return ctx->source.id;
	}else{
		cb(data,name,res);
		return 0;
	}
Simon Morlat's avatar
Simon Morlat committed
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
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;
	}
}