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