Home | History | Annotate | Line # | Download | only in sample
dns-example.c revision 1.1
      1  1.1  christos /*	$NetBSD: dns-example.c,v 1.1 2013/04/11 16:43:31 christos Exp $	*/
      2  1.1  christos /*
      3  1.1  christos   This example code shows how to use the high-level, low-level, and
      4  1.1  christos   server-level interfaces of evdns.
      5  1.1  christos 
      6  1.1  christos   XXX It's pretty ugly and should probably be cleaned up.
      7  1.1  christos  */
      8  1.1  christos 
      9  1.1  christos #include <event2/event-config.h>
     10  1.1  christos 
     11  1.1  christos /* Compatibility for possible missing IPv6 declarations */
     12  1.1  christos #include "../ipv6-internal.h"
     13  1.1  christos 
     14  1.1  christos #include <sys/types.h>
     15  1.1  christos 
     16  1.1  christos #ifdef WIN32
     17  1.1  christos #include <winsock2.h>
     18  1.1  christos #include <ws2tcpip.h>
     19  1.1  christos #else
     20  1.1  christos #include <sys/socket.h>
     21  1.1  christos #include <netinet/in.h>
     22  1.1  christos #include <arpa/inet.h>
     23  1.1  christos #endif
     24  1.1  christos 
     25  1.1  christos #include <event2/event.h>
     26  1.1  christos #include <event2/dns.h>
     27  1.1  christos #include <event2/dns_struct.h>
     28  1.1  christos #include <event2/util.h>
     29  1.1  christos 
     30  1.1  christos #ifdef _EVENT_HAVE_NETINET_IN6_H
     31  1.1  christos #include <netinet/in6.h>
     32  1.1  christos #endif
     33  1.1  christos 
     34  1.1  christos #include <stdio.h>
     35  1.1  christos #include <stdlib.h>
     36  1.1  christos #include <string.h>
     37  1.1  christos 
     38  1.1  christos #define u32 ev_uint32_t
     39  1.1  christos #define u8 ev_uint8_t
     40  1.1  christos 
     41  1.1  christos static const char *
     42  1.1  christos debug_ntoa(u32 address)
     43  1.1  christos {
     44  1.1  christos 	static char buf[32];
     45  1.1  christos 	u32 a = ntohl(address);
     46  1.1  christos 	evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
     47  1.1  christos 					(int)(u8)((a>>24)&0xff),
     48  1.1  christos 					(int)(u8)((a>>16)&0xff),
     49  1.1  christos 					(int)(u8)((a>>8 )&0xff),
     50  1.1  christos 					(int)(u8)((a	)&0xff));
     51  1.1  christos 	return buf;
     52  1.1  christos }
     53  1.1  christos 
     54  1.1  christos static void
     55  1.1  christos main_callback(int result, char type, int count, int ttl,
     56  1.1  christos 			  void *addrs, void *orig) {
     57  1.1  christos 	char *n = (char*)orig;
     58  1.1  christos 	int i;
     59  1.1  christos 	for (i = 0; i < count; ++i) {
     60  1.1  christos 		if (type == DNS_IPv4_A) {
     61  1.1  christos 			printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
     62  1.1  christos 		} else if (type == DNS_PTR) {
     63  1.1  christos 			printf("%s: %s\n", n, ((char**)addrs)[i]);
     64  1.1  christos 		}
     65  1.1  christos 	}
     66  1.1  christos 	if (!count) {
     67  1.1  christos 		printf("%s: No answer (%d)\n", n, result);
     68  1.1  christos 	}
     69  1.1  christos 	fflush(stdout);
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos static void
     73  1.1  christos gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
     74  1.1  christos {
     75  1.1  christos 	const char *name = arg;
     76  1.1  christos 	int i;
     77  1.1  christos 	if (err) {
     78  1.1  christos 		printf("%s: %s\n", name, evutil_gai_strerror(err));
     79  1.1  christos 	}
     80  1.1  christos 	if (ai && ai->ai_canonname)
     81  1.1  christos 		printf("    %s ==> %s\n", name, ai->ai_canonname);
     82  1.1  christos 	for (i=0; ai; ai = ai->ai_next, ++i) {
     83  1.1  christos 		char buf[128];
     84  1.1  christos 		if (ai->ai_family == PF_INET) {
     85  1.1  christos 			struct sockaddr_in *sin =
     86  1.1  christos 			    (struct sockaddr_in*)ai->ai_addr;
     87  1.1  christos 			evutil_inet_ntop(AF_INET, &sin->sin_addr, buf,
     88  1.1  christos 			    sizeof(buf));
     89  1.1  christos 			printf("[%d] %s: %s\n",i,name,buf);
     90  1.1  christos 		} else {
     91  1.1  christos 			struct sockaddr_in6 *sin6 =
     92  1.1  christos 			    (struct sockaddr_in6*)ai->ai_addr;
     93  1.1  christos 			evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
     94  1.1  christos 			    sizeof(buf));
     95  1.1  christos 			printf("[%d] %s: %s\n",i,name,buf);
     96  1.1  christos 		}
     97  1.1  christos 	}
     98  1.1  christos }
     99  1.1  christos 
    100  1.1  christos static void
    101  1.1  christos evdns_server_callback(struct evdns_server_request *req, void *data)
    102  1.1  christos {
    103  1.1  christos 	int i, r;
    104  1.1  christos 	(void)data;
    105  1.1  christos 	/* dummy; give 192.168.11.11 as an answer for all A questions,
    106  1.1  christos 	 *	give foo.bar.example.com as an answer for all PTR questions. */
    107  1.1  christos 	for (i = 0; i < req->nquestions; ++i) {
    108  1.1  christos 		u32 ans = htonl(0xc0a80b0bUL);
    109  1.1  christos 		if (req->questions[i]->type == EVDNS_TYPE_A &&
    110  1.1  christos 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
    111  1.1  christos 			printf(" -- replying for %s (A)\n", req->questions[i]->name);
    112  1.1  christos 			r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
    113  1.1  christos 										  1, &ans, 10);
    114  1.1  christos 			if (r<0)
    115  1.1  christos 				printf("eeep, didn't work.\n");
    116  1.1  christos 		} else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
    117  1.1  christos 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
    118  1.1  christos 			printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
    119  1.1  christos 			r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
    120  1.1  christos 											"foo.bar.example.com", 10);
    121  1.1  christos 			if (r<0)
    122  1.1  christos 				printf("ugh, no luck");
    123  1.1  christos 		} else {
    124  1.1  christos 			printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
    125  1.1  christos 				   req->questions[i]->type, req->questions[i]->dns_question_class);
    126  1.1  christos 		}
    127  1.1  christos 	}
    128  1.1  christos 
    129  1.1  christos 	r = evdns_server_request_respond(req, 0);
    130  1.1  christos 	if (r<0)
    131  1.1  christos 		printf("eeek, couldn't send reply.\n");
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos static int verbose = 0;
    135  1.1  christos 
    136  1.1  christos static void
    137  1.1  christos logfn(int is_warn, const char *msg) {
    138  1.1  christos 	if (!is_warn && !verbose)
    139  1.1  christos 		return;
    140  1.1  christos 	fprintf(stderr, "%s: %s\n", is_warn?"WARN":"INFO", msg);
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos int
    144  1.1  christos main(int c, char **v) {
    145  1.1  christos 	int idx;
    146  1.1  christos 	int reverse = 0, servertest = 0, use_getaddrinfo = 0;
    147  1.1  christos 	struct event_base *event_base = NULL;
    148  1.1  christos 	struct evdns_base *evdns_base = NULL;
    149  1.1  christos 	const char *resolv_conf = NULL;
    150  1.1  christos 	if (c<2) {
    151  1.1  christos 		fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] hostname\n", v[0]);
    152  1.1  christos 		fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
    153  1.1  christos 		return 1;
    154  1.1  christos 	}
    155  1.1  christos 	idx = 1;
    156  1.1  christos 	while (idx < c && v[idx][0] == '-') {
    157  1.1  christos 		if (!strcmp(v[idx], "-x"))
    158  1.1  christos 			reverse = 1;
    159  1.1  christos 		else if (!strcmp(v[idx], "-v"))
    160  1.1  christos 			verbose = 1;
    161  1.1  christos 		else if (!strcmp(v[idx], "-g"))
    162  1.1  christos 			use_getaddrinfo = 1;
    163  1.1  christos 		else if (!strcmp(v[idx], "-servertest"))
    164  1.1  christos 			servertest = 1;
    165  1.1  christos 		else if (!strcmp(v[idx], "-c")) {
    166  1.1  christos 			if (idx + 1 < c)
    167  1.1  christos 				resolv_conf = v[++idx];
    168  1.1  christos 			else
    169  1.1  christos 				fprintf(stderr, "-c needs an argument\n");
    170  1.1  christos 		} else
    171  1.1  christos 			fprintf(stderr, "Unknown option %s\n", v[idx]);
    172  1.1  christos 		++idx;
    173  1.1  christos 	}
    174  1.1  christos 
    175  1.1  christos #ifdef WIN32
    176  1.1  christos 	{
    177  1.1  christos 		WSADATA WSAData;
    178  1.1  christos 		WSAStartup(0x101, &WSAData);
    179  1.1  christos 	}
    180  1.1  christos #endif
    181  1.1  christos 
    182  1.1  christos 	event_base = event_base_new();
    183  1.1  christos 	evdns_base = evdns_base_new(event_base, 0);
    184  1.1  christos 	evdns_set_log_fn(logfn);
    185  1.1  christos 
    186  1.1  christos 	if (servertest) {
    187  1.1  christos 		evutil_socket_t sock;
    188  1.1  christos 		struct sockaddr_in my_addr;
    189  1.1  christos 		sock = socket(PF_INET, SOCK_DGRAM, 0);
    190  1.1  christos 		if (sock == -1) {
    191  1.1  christos 			perror("socket");
    192  1.1  christos 			exit(1);
    193  1.1  christos 		}
    194  1.1  christos 		evutil_make_socket_nonblocking(sock);
    195  1.1  christos 		my_addr.sin_family = AF_INET;
    196  1.1  christos 		my_addr.sin_port = htons(10053);
    197  1.1  christos 		my_addr.sin_addr.s_addr = INADDR_ANY;
    198  1.1  christos 		if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
    199  1.1  christos 			perror("bind");
    200  1.1  christos 			exit(1);
    201  1.1  christos 		}
    202  1.1  christos 		evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL);
    203  1.1  christos 	}
    204  1.1  christos 	if (idx < c) {
    205  1.1  christos 		int res;
    206  1.1  christos #ifdef WIN32
    207  1.1  christos 		if (resolv_conf == NULL)
    208  1.1  christos 			res = evdns_base_config_windows_nameservers(evdns_base);
    209  1.1  christos 		else
    210  1.1  christos #endif
    211  1.1  christos 			res = evdns_base_resolv_conf_parse(evdns_base,
    212  1.1  christos 			    DNS_OPTION_NAMESERVERS,
    213  1.1  christos 			    resolv_conf ? resolv_conf : "/etc/resolv.conf");
    214  1.1  christos 
    215  1.1  christos 		if (res < 0) {
    216  1.1  christos 			fprintf(stderr, "Couldn't configure nameservers");
    217  1.1  christos 			return 1;
    218  1.1  christos 		}
    219  1.1  christos 	}
    220  1.1  christos 
    221  1.1  christos 	printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME);
    222  1.1  christos 	for (; idx < c; ++idx) {
    223  1.1  christos 		if (reverse) {
    224  1.1  christos 			struct in_addr addr;
    225  1.1  christos 			if (evutil_inet_pton(AF_INET, v[idx], &addr)!=1) {
    226  1.1  christos 				fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
    227  1.1  christos 				continue;
    228  1.1  christos 			}
    229  1.1  christos 			fprintf(stderr, "resolving %s...\n",v[idx]);
    230  1.1  christos 			evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[idx]);
    231  1.1  christos 		} else if (use_getaddrinfo) {
    232  1.1  christos 			struct evutil_addrinfo hints;
    233  1.1  christos 			memset(&hints, 0, sizeof(hints));
    234  1.1  christos 			hints.ai_family = PF_UNSPEC;
    235  1.1  christos 			hints.ai_protocol = IPPROTO_TCP;
    236  1.1  christos 			hints.ai_flags = EVUTIL_AI_CANONNAME;
    237  1.1  christos 			fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
    238  1.1  christos 			evdns_getaddrinfo(evdns_base, v[idx], NULL, &hints,
    239  1.1  christos 			    gai_callback, v[idx]);
    240  1.1  christos 		} else {
    241  1.1  christos 			fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
    242  1.1  christos 			evdns_base_resolve_ipv4(evdns_base, v[idx], 0, main_callback, v[idx]);
    243  1.1  christos 		}
    244  1.1  christos 	}
    245  1.1  christos 	fflush(stdout);
    246  1.1  christos 	event_base_dispatch(event_base);
    247  1.1  christos 	return 0;
    248  1.1  christos }
    249  1.1  christos 
    250