xref: /libevent-2.1.12/sample/dns-example.c (revision fc51bf2c)
1d60a1bd5SNick Mathewson /*
2d60a1bd5SNick Mathewson   This example code shows how to use the high-level, low-level, and
3d60a1bd5SNick Mathewson   server-level interfaces of evdns.
4d60a1bd5SNick Mathewson 
5d60a1bd5SNick Mathewson   XXX It's pretty ugly and should probably be cleaned up.
6d60a1bd5SNick Mathewson  */
7ae5fbf49SNick Mathewson 
8ec347b92SNick Mathewson #include <event2/event-config.h>
9ae5fbf49SNick Mathewson 
104eb281c8SNick Mathewson /* Compatibility for possible missing IPv6 declarations */
11bbf55150SHarlan Stenn #include "../ipv6-internal.h"
12bbf55150SHarlan Stenn 
13625a261aSNick Mathewson #include <sys/types.h>
14625a261aSNick Mathewson 
1532f8592cSAzat Khuzhin #ifdef EVENT__HAVE_UNISTD_H
1632f8592cSAzat Khuzhin #include <unistd.h>
1732f8592cSAzat Khuzhin #endif
1832f8592cSAzat Khuzhin 
199f560bfaSNick Mathewson #ifdef _WIN32
20ae5fbf49SNick Mathewson #include <winsock2.h>
21ae5fbf49SNick Mathewson #include <ws2tcpip.h>
2232f8592cSAzat Khuzhin #include <getopt.h>
23ae5fbf49SNick Mathewson #else
24ae5fbf49SNick Mathewson #include <sys/socket.h>
25ae5fbf49SNick Mathewson #include <netinet/in.h>
26ae5fbf49SNick Mathewson #include <arpa/inet.h>
27ae5fbf49SNick Mathewson #endif
28ae5fbf49SNick Mathewson 
29ae5fbf49SNick Mathewson #include <event2/event.h>
30ae5fbf49SNick Mathewson #include <event2/dns.h>
31ae5fbf49SNick Mathewson #include <event2/dns_struct.h>
32ae5fbf49SNick Mathewson #include <event2/util.h>
33ae5fbf49SNick Mathewson 
3468120d9bSNick Mathewson #ifdef EVENT__HAVE_NETINET_IN6_H
35ae5fbf49SNick Mathewson #include <netinet/in6.h>
36ae5fbf49SNick Mathewson #endif
37ae5fbf49SNick Mathewson 
38ae5fbf49SNick Mathewson #include <stdio.h>
39ae5fbf49SNick Mathewson #include <stdlib.h>
40ae5fbf49SNick Mathewson #include <string.h>
41ae5fbf49SNick Mathewson 
42ae5fbf49SNick Mathewson #define u32 ev_uint32_t
43ae5fbf49SNick Mathewson #define u8 ev_uint8_t
44ae5fbf49SNick Mathewson 
45ae5fbf49SNick Mathewson static const char *
debug_ntoa(u32 address)46ae5fbf49SNick Mathewson debug_ntoa(u32 address)
47ae5fbf49SNick Mathewson {
48ae5fbf49SNick Mathewson 	static char buf[32];
49ae5fbf49SNick Mathewson 	u32 a = ntohl(address);
50ae5fbf49SNick Mathewson 	evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
51ae5fbf49SNick Mathewson 					(int)(u8)((a>>24)&0xff),
52ae5fbf49SNick Mathewson 					(int)(u8)((a>>16)&0xff),
53ae5fbf49SNick Mathewson 					(int)(u8)((a>>8 )&0xff),
54ae5fbf49SNick Mathewson 					(int)(u8)((a	)&0xff));
55ae5fbf49SNick Mathewson 	return buf;
56ae5fbf49SNick Mathewson }
57ae5fbf49SNick Mathewson 
58ae5fbf49SNick Mathewson static void
main_callback(int result,char type,int count,int ttl,void * addrs,void * orig)59ae5fbf49SNick Mathewson main_callback(int result, char type, int count, int ttl,
60ae5fbf49SNick Mathewson 			  void *addrs, void *orig) {
61ae5fbf49SNick Mathewson 	char *n = (char*)orig;
62ae5fbf49SNick Mathewson 	int i;
63ae5fbf49SNick Mathewson 	for (i = 0; i < count; ++i) {
64ae5fbf49SNick Mathewson 		if (type == DNS_IPv4_A) {
65ae5fbf49SNick Mathewson 			printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
66ae5fbf49SNick Mathewson 		} else if (type == DNS_PTR) {
67ae5fbf49SNick Mathewson 			printf("%s: %s\n", n, ((char**)addrs)[i]);
68ae5fbf49SNick Mathewson 		}
69ae5fbf49SNick Mathewson 	}
70ae5fbf49SNick Mathewson 	if (!count) {
71ae5fbf49SNick Mathewson 		printf("%s: No answer (%d)\n", n, result);
72ae5fbf49SNick Mathewson 	}
73ae5fbf49SNick Mathewson 	fflush(stdout);
74ae5fbf49SNick Mathewson }
75ae5fbf49SNick Mathewson 
76ae5fbf49SNick Mathewson static void
gai_callback(int err,struct evutil_addrinfo * ai,void * arg)7786f57420SNick Mathewson gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
7886f57420SNick Mathewson {
7986f57420SNick Mathewson 	const char *name = arg;
8086f57420SNick Mathewson 	int i;
81d0cde454SBogdan Harjoc 	struct evutil_addrinfo *first_ai = ai;
82d0cde454SBogdan Harjoc 
8386f57420SNick Mathewson 	if (err) {
8486f57420SNick Mathewson 		printf("%s: %s\n", name, evutil_gai_strerror(err));
8586f57420SNick Mathewson 	}
8686f57420SNick Mathewson 	if (ai && ai->ai_canonname)
8786f57420SNick Mathewson 		printf("    %s ==> %s\n", name, ai->ai_canonname);
8886f57420SNick Mathewson 	for (i=0; ai; ai = ai->ai_next, ++i) {
8986f57420SNick Mathewson 		char buf[128];
9086f57420SNick Mathewson 		if (ai->ai_family == PF_INET) {
9186f57420SNick Mathewson 			struct sockaddr_in *sin =
9286f57420SNick Mathewson 			    (struct sockaddr_in*)ai->ai_addr;
9386f57420SNick Mathewson 			evutil_inet_ntop(AF_INET, &sin->sin_addr, buf,
9486f57420SNick Mathewson 			    sizeof(buf));
9586f57420SNick Mathewson 			printf("[%d] %s: %s\n",i,name,buf);
9686f57420SNick Mathewson 		} else {
9786f57420SNick Mathewson 			struct sockaddr_in6 *sin6 =
9886f57420SNick Mathewson 			    (struct sockaddr_in6*)ai->ai_addr;
9986f57420SNick Mathewson 			evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
10086f57420SNick Mathewson 			    sizeof(buf));
10186f57420SNick Mathewson 			printf("[%d] %s: %s\n",i,name,buf);
10286f57420SNick Mathewson 		}
10386f57420SNick Mathewson 	}
104d0cde454SBogdan Harjoc 
105d0cde454SBogdan Harjoc 	if (first_ai)
106d0cde454SBogdan Harjoc 		evutil_freeaddrinfo(first_ai);
10786f57420SNick Mathewson }
10886f57420SNick Mathewson 
10986f57420SNick Mathewson static void
evdns_server_callback(struct evdns_server_request * req,void * data)110ae5fbf49SNick Mathewson evdns_server_callback(struct evdns_server_request *req, void *data)
111ae5fbf49SNick Mathewson {
112ae5fbf49SNick Mathewson 	int i, r;
113ae5fbf49SNick Mathewson 	(void)data;
114ae5fbf49SNick Mathewson 	/* dummy; give 192.168.11.11 as an answer for all A questions,
115ae5fbf49SNick Mathewson 	 *	give foo.bar.example.com as an answer for all PTR questions. */
116ae5fbf49SNick Mathewson 	for (i = 0; i < req->nquestions; ++i) {
117ae5fbf49SNick Mathewson 		u32 ans = htonl(0xc0a80b0bUL);
118ae5fbf49SNick Mathewson 		if (req->questions[i]->type == EVDNS_TYPE_A &&
119ae5fbf49SNick Mathewson 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
120ae5fbf49SNick Mathewson 			printf(" -- replying for %s (A)\n", req->questions[i]->name);
121ae5fbf49SNick Mathewson 			r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
122ae5fbf49SNick Mathewson 										  1, &ans, 10);
123ae5fbf49SNick Mathewson 			if (r<0)
124ae5fbf49SNick Mathewson 				printf("eeep, didn't work.\n");
125ae5fbf49SNick Mathewson 		} else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
126ae5fbf49SNick Mathewson 		    req->questions[i]->dns_question_class == EVDNS_CLASS_INET) {
127ae5fbf49SNick Mathewson 			printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
128ae5fbf49SNick Mathewson 			r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
129ae5fbf49SNick Mathewson 											"foo.bar.example.com", 10);
1304bac793eSSebastian Hahn 			if (r<0)
1314bac793eSSebastian Hahn 				printf("ugh, no luck");
132ae5fbf49SNick Mathewson 		} else {
133ae5fbf49SNick Mathewson 			printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
134ae5fbf49SNick Mathewson 				   req->questions[i]->type, req->questions[i]->dns_question_class);
135ae5fbf49SNick Mathewson 		}
136ae5fbf49SNick Mathewson 	}
137ae5fbf49SNick Mathewson 
138ae5fbf49SNick Mathewson 	r = evdns_server_request_respond(req, 0);
139ae5fbf49SNick Mathewson 	if (r<0)
140ae5fbf49SNick Mathewson 		printf("eeek, couldn't send reply.\n");
141ae5fbf49SNick Mathewson }
142ae5fbf49SNick Mathewson 
143ae5fbf49SNick Mathewson static int verbose = 0;
144ae5fbf49SNick Mathewson 
145ae5fbf49SNick Mathewson static void
logfn(int is_warn,const char * msg)146ae5fbf49SNick Mathewson logfn(int is_warn, const char *msg) {
147ae5fbf49SNick Mathewson 	if (!is_warn && !verbose)
148ae5fbf49SNick Mathewson 		return;
149ae5fbf49SNick Mathewson 	fprintf(stderr, "%s: %s\n", is_warn?"WARN":"INFO", msg);
150ae5fbf49SNick Mathewson }
151ae5fbf49SNick Mathewson 
152ae5fbf49SNick Mathewson int
main(int c,char ** v)153ae5fbf49SNick Mathewson main(int c, char **v) {
15432f8592cSAzat Khuzhin 	struct options {
15532f8592cSAzat Khuzhin 		int reverse;
15632f8592cSAzat Khuzhin 		int use_getaddrinfo;
15732f8592cSAzat Khuzhin 		int servertest;
15832f8592cSAzat Khuzhin 		const char *resolv_conf;
159df19a970SAzat Khuzhin 		const char *ns;
160620ff243SMark Ellzey 	};
161620ff243SMark Ellzey 	struct options o;
162d3e1c440SDavid Disseldorp 	int opt;
163ae5fbf49SNick Mathewson 	struct event_base *event_base = NULL;
164ae5fbf49SNick Mathewson 	struct evdns_base *evdns_base = NULL;
16532f8592cSAzat Khuzhin 
166620ff243SMark Ellzey 	memset(&o, 0, sizeof(o));
167620ff243SMark Ellzey 
168ae5fbf49SNick Mathewson 	if (c < 2) {
169df19a970SAzat Khuzhin 		fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] [-s ns] hostname\n", v[0]);
17032f8592cSAzat Khuzhin 		fprintf(stderr, "syntax: %s [-T]\n", v[0]);
171ae5fbf49SNick Mathewson 		return 1;
172ae5fbf49SNick Mathewson 	}
17332f8592cSAzat Khuzhin 
174d0cde454SBogdan Harjoc 	while ((opt = getopt(c, v, "xvc:Ts:g")) != -1) {
17532f8592cSAzat Khuzhin 		switch (opt) {
17632f8592cSAzat Khuzhin 			case 'x': o.reverse = 1; break;
17732f8592cSAzat Khuzhin 			case 'v': ++verbose; break;
17832f8592cSAzat Khuzhin 			case 'g': o.use_getaddrinfo = 1; break;
17932f8592cSAzat Khuzhin 			case 'T': o.servertest = 1; break;
18032f8592cSAzat Khuzhin 			case 'c': o.resolv_conf = optarg; break;
181df19a970SAzat Khuzhin 			case 's': o.ns = optarg; break;
18232f8592cSAzat Khuzhin 			default : fprintf(stderr, "Unknown option %c\n", opt); break;
18332f8592cSAzat Khuzhin 		}
184ae5fbf49SNick Mathewson 	}
185ae5fbf49SNick Mathewson 
18688ecda3bSNick Mathewson #ifdef _WIN32
187a3f320e8SGisle Vanem 	{
188a3f320e8SGisle Vanem 		WSADATA WSAData;
189a3f320e8SGisle Vanem 		WSAStartup(0x101, &WSAData);
190a3f320e8SGisle Vanem 	}
191a3f320e8SGisle Vanem #endif
192a3f320e8SGisle Vanem 
193ae5fbf49SNick Mathewson 	event_base = event_base_new();
1946b7fa620SAzat Khuzhin 	evdns_base = evdns_base_new(event_base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
195ae5fbf49SNick Mathewson 	evdns_set_log_fn(logfn);
196ae5fbf49SNick Mathewson 
19732f8592cSAzat Khuzhin 	if (o.servertest) {
198c7cf6f00SNick Mathewson 		evutil_socket_t sock;
199ae5fbf49SNick Mathewson 		struct sockaddr_in my_addr;
200ae5fbf49SNick Mathewson 		sock = socket(PF_INET, SOCK_DGRAM, 0);
2017c11e51eSHarlan Stenn 		if (sock == -1) {
2027c11e51eSHarlan Stenn 			perror("socket");
2037c11e51eSHarlan Stenn 			exit(1);
2047c11e51eSHarlan Stenn 		}
205ae5fbf49SNick Mathewson 		evutil_make_socket_nonblocking(sock);
206ae5fbf49SNick Mathewson 		my_addr.sin_family = AF_INET;
207ae5fbf49SNick Mathewson 		my_addr.sin_port = htons(10053);
208ae5fbf49SNick Mathewson 		my_addr.sin_addr.s_addr = INADDR_ANY;
209ae5fbf49SNick Mathewson 		if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
210ae5fbf49SNick Mathewson 			perror("bind");
211ae5fbf49SNick Mathewson 			exit(1);
212ae5fbf49SNick Mathewson 		}
213ae5fbf49SNick Mathewson 		evdns_add_server_port_with_base(event_base, sock, 0, evdns_server_callback, NULL);
214ae5fbf49SNick Mathewson 	}
21532f8592cSAzat Khuzhin 	if (optind < c) {
216a3f320e8SGisle Vanem 		int res;
2179f560bfaSNick Mathewson #ifdef _WIN32
218df19a970SAzat Khuzhin 		if (o.resolv_conf == NULL && !o.ns)
219a3f320e8SGisle Vanem 			res = evdns_base_config_windows_nameservers(evdns_base);
2206610fa5aSNick Mathewson 		else
221ae5fbf49SNick Mathewson #endif
222df19a970SAzat Khuzhin 		if (o.ns)
223df19a970SAzat Khuzhin 			res = evdns_base_nameserver_ip_add(evdns_base, o.ns);
224df19a970SAzat Khuzhin 		else
2256610fa5aSNick Mathewson 			res = evdns_base_resolv_conf_parse(evdns_base,
22632f8592cSAzat Khuzhin 			    DNS_OPTION_NAMESERVERS, o.resolv_conf);
2276610fa5aSNick Mathewson 
228*fc51bf2cSAzat Khuzhin 		if (res) {
229*fc51bf2cSAzat Khuzhin 			fprintf(stderr, "Couldn't configure nameservers\n");
230a3f320e8SGisle Vanem 			return 1;
231a3f320e8SGisle Vanem 		}
232ae5fbf49SNick Mathewson 	}
23386f57420SNick Mathewson 
23486f57420SNick Mathewson 	printf("EVUTIL_AI_CANONNAME in example = %d\n", EVUTIL_AI_CANONNAME);
23532f8592cSAzat Khuzhin 	for (; optind < c; ++optind) {
23632f8592cSAzat Khuzhin 		if (o.reverse) {
237ae5fbf49SNick Mathewson 			struct in_addr addr;
23832f8592cSAzat Khuzhin 			if (evutil_inet_pton(AF_INET, v[optind], &addr)!=1) {
23932f8592cSAzat Khuzhin 				fprintf(stderr, "Skipping non-IP %s\n", v[optind]);
240ae5fbf49SNick Mathewson 				continue;
241ae5fbf49SNick Mathewson 			}
24232f8592cSAzat Khuzhin 			fprintf(stderr, "resolving %s...\n",v[optind]);
24332f8592cSAzat Khuzhin 			evdns_base_resolve_reverse(evdns_base, &addr, 0, main_callback, v[optind]);
24432f8592cSAzat Khuzhin 		} else if (o.use_getaddrinfo) {
24586f57420SNick Mathewson 			struct evutil_addrinfo hints;
24686f57420SNick Mathewson 			memset(&hints, 0, sizeof(hints));
24786f57420SNick Mathewson 			hints.ai_family = PF_UNSPEC;
24886f57420SNick Mathewson 			hints.ai_protocol = IPPROTO_TCP;
24986f57420SNick Mathewson 			hints.ai_flags = EVUTIL_AI_CANONNAME;
25032f8592cSAzat Khuzhin 			fprintf(stderr, "resolving (fwd) %s...\n",v[optind]);
25132f8592cSAzat Khuzhin 			evdns_getaddrinfo(evdns_base, v[optind], NULL, &hints,
25232f8592cSAzat Khuzhin 			    gai_callback, v[optind]);
253ae5fbf49SNick Mathewson 		} else {
25432f8592cSAzat Khuzhin 			fprintf(stderr, "resolving (fwd) %s...\n",v[optind]);
25532f8592cSAzat Khuzhin 			evdns_base_resolve_ipv4(evdns_base, v[optind], 0, main_callback, v[optind]);
256ae5fbf49SNick Mathewson 		}
257ae5fbf49SNick Mathewson 	}
258ae5fbf49SNick Mathewson 	fflush(stdout);
259ae5fbf49SNick Mathewson 	event_base_dispatch(event_base);
26027b59783SAzat Khuzhin 	evdns_base_free(evdns_base, 1);
26127b59783SAzat Khuzhin 	event_base_free(event_base);
262ae5fbf49SNick Mathewson 	return 0;
263ae5fbf49SNick Mathewson }
264ae5fbf49SNick Mathewson 
265