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