resolve.c 2.17 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 21 22 23 24 25 26 27 28 29 30
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2013  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 2 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 <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include "belle-sip/belle-sip.h"

static belle_sip_stack_t *stack;

Simon Morlat's avatar
Simon Morlat committed
31
static void resolver_callback(void *data, const char *queried_name, struct addrinfo *ai_list, unsigned int ttl){
Simon Morlat's avatar
Simon Morlat committed
32 33 34 35 36 37
	int err;
	struct addrinfo *ai_it;
	char name[NI_MAXHOST];
	char port[NI_MAXSERV];
	
	for(ai_it=ai_list;ai_it!=NULL;ai_it=ai_it->ai_next){
38
		err=bctbx_getnameinfo(ai_it->ai_addr,ai_list->ai_addrlen,name,sizeof(name),port,sizeof(port),NI_NUMERICSERV|NI_NUMERICHOST);
Simon Morlat's avatar
Simon Morlat committed
39 40 41
		if (err!=0){
			fprintf(stderr,"getnameinfo error: %s",gai_strerror(err));
		}else{
Simon Morlat's avatar
Simon Morlat committed
42
			printf("\t%s %s  (ttl:%u)\n",name,port,ttl);
Simon Morlat's avatar
Simon Morlat committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		}
	}
	belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));
}

int main(int argc, char *argv[]){
	int i;
	const char *domain=NULL;
	const char *transport="udp";
	
	if (argc<2){
		fprintf(stderr,"Usage:\n%s <domain name> [transport] [--debug]\n",argv[0]);
		return -1;
	}
	domain=argv[1];
	for (i=2;i<argc;i++){
59
		if (strcmp(argv[i],"--debug")==0){
Simon Morlat's avatar
Simon Morlat committed
60 61 62 63
			belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
		}else if (strstr(argv[i],"--")!=argv[i]) transport=argv[i];
	}
	stack=belle_sip_stack_new(NULL);
64
	printf("Trying to resolve domain '%s', with transport hint '%s'\n",domain,transport);
Simon Morlat's avatar
Simon Morlat committed
65
	belle_sip_stack_resolve(stack,"sip", transport,domain,5060,AF_INET6,resolver_callback,NULL);
Simon Morlat's avatar
Simon Morlat committed
66 67 68 69
	belle_sip_stack_main(stack);
	belle_sip_object_unref(stack);
	return 0;
}