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