regress_dns.c revision 1.2 1 1.2 plunky /* $NetBSD: regress_dns.c,v 1.2 2010/06/03 07:08:41 plunky Exp $ */
2 1.1 plunky /*
3 1.1 plunky * Copyright (c) 2003-2006 Niels Provos <provos (at) citi.umich.edu>
4 1.1 plunky * All rights reserved.
5 1.1 plunky *
6 1.1 plunky * Redistribution and use in source and binary forms, with or without
7 1.1 plunky * modification, are permitted provided that the following conditions
8 1.1 plunky * are met:
9 1.1 plunky * 1. Redistributions of source code must retain the above copyright
10 1.1 plunky * notice, this list of conditions and the following disclaimer.
11 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 plunky * notice, this list of conditions and the following disclaimer in the
13 1.1 plunky * documentation and/or other materials provided with the distribution.
14 1.1 plunky * 3. The name of the author may not be used to endorse or promote products
15 1.1 plunky * derived from this software without specific prior written permission.
16 1.1 plunky *
17 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 plunky */
28 1.1 plunky
29 1.1 plunky #ifdef WIN32
30 1.1 plunky #include <winsock2.h>
31 1.1 plunky #include <windows.h>
32 1.1 plunky #endif
33 1.1 plunky
34 1.1 plunky #ifdef HAVE_CONFIG_H
35 1.1 plunky #include "config.h"
36 1.1 plunky #endif
37 1.1 plunky
38 1.1 plunky #include <sys/types.h>
39 1.1 plunky #include <sys/stat.h>
40 1.1 plunky #ifdef HAVE_SYS_TIME_H
41 1.1 plunky #include <sys/time.h>
42 1.1 plunky #endif
43 1.1 plunky #include <sys/queue.h>
44 1.1 plunky #ifndef WIN32
45 1.1 plunky #include <sys/socket.h>
46 1.1 plunky #include <signal.h>
47 1.1 plunky #include <netinet/in.h>
48 1.1 plunky #include <arpa/inet.h>
49 1.1 plunky #include <unistd.h>
50 1.1 plunky #endif
51 1.1 plunky #ifdef HAVE_NETINET_IN6_H
52 1.1 plunky #include <netinet/in6.h>
53 1.1 plunky #endif
54 1.1 plunky #ifdef HAVE_NETDB_H
55 1.1 plunky #include <netdb.h>
56 1.1 plunky #endif
57 1.1 plunky #include <fcntl.h>
58 1.1 plunky #include <stdlib.h>
59 1.1 plunky #include <stdio.h>
60 1.1 plunky #include <string.h>
61 1.1 plunky #include <errno.h>
62 1.1 plunky
63 1.1 plunky #include "event.h"
64 1.1 plunky #include "evdns.h"
65 1.1 plunky #include "log.h"
66 1.1 plunky
67 1.1 plunky static int dns_ok = 0;
68 1.2 plunky #if 0
69 1.1 plunky static int dns_err = 0;
70 1.2 plunky #endif
71 1.1 plunky
72 1.1 plunky void dns_suite(void);
73 1.1 plunky
74 1.2 plunky #if 0
75 1.1 plunky static void
76 1.1 plunky dns_gethostbyname_cb(int result, char type, int count, int ttl,
77 1.1 plunky void *addresses, void *arg)
78 1.1 plunky {
79 1.1 plunky dns_ok = dns_err = 0;
80 1.1 plunky
81 1.1 plunky if (result == DNS_ERR_TIMEOUT) {
82 1.1 plunky fprintf(stdout, "[Timed out] ");
83 1.1 plunky dns_err = result;
84 1.1 plunky goto out;
85 1.1 plunky }
86 1.1 plunky
87 1.1 plunky if (result != DNS_ERR_NONE) {
88 1.1 plunky fprintf(stdout, "[Error code %d] ", result);
89 1.1 plunky goto out;
90 1.1 plunky }
91 1.1 plunky
92 1.1 plunky fprintf(stderr, "type: %d, count: %d, ttl: %d: ", type, count, ttl);
93 1.1 plunky
94 1.1 plunky switch (type) {
95 1.1 plunky case DNS_IPv6_AAAA: {
96 1.1 plunky #if defined(HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
97 1.1 plunky struct in6_addr *in6_addrs = addresses;
98 1.1 plunky char buf[INET6_ADDRSTRLEN+1];
99 1.1 plunky int i;
100 1.1 plunky /* a resolution that's not valid does not help */
101 1.1 plunky if (ttl < 0)
102 1.1 plunky goto out;
103 1.1 plunky for (i = 0; i < count; ++i) {
104 1.1 plunky const char *b = inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf));
105 1.1 plunky if (b)
106 1.1 plunky fprintf(stderr, "%s ", b);
107 1.1 plunky else
108 1.1 plunky fprintf(stderr, "%s ", strerror(errno));
109 1.1 plunky }
110 1.1 plunky #endif
111 1.1 plunky break;
112 1.1 plunky }
113 1.1 plunky case DNS_IPv4_A: {
114 1.1 plunky struct in_addr *in_addrs = addresses;
115 1.1 plunky int i;
116 1.1 plunky /* a resolution that's not valid does not help */
117 1.1 plunky if (ttl < 0)
118 1.1 plunky goto out;
119 1.1 plunky for (i = 0; i < count; ++i)
120 1.1 plunky fprintf(stderr, "%s ", inet_ntoa(in_addrs[i]));
121 1.1 plunky break;
122 1.1 plunky }
123 1.1 plunky case DNS_PTR:
124 1.1 plunky /* may get at most one PTR */
125 1.1 plunky if (count != 1)
126 1.1 plunky goto out;
127 1.1 plunky
128 1.1 plunky fprintf(stderr, "%s ", *(char **)addresses);
129 1.1 plunky break;
130 1.1 plunky default:
131 1.1 plunky goto out;
132 1.1 plunky }
133 1.1 plunky
134 1.1 plunky dns_ok = type;
135 1.1 plunky
136 1.1 plunky out:
137 1.1 plunky event_loopexit(NULL);
138 1.1 plunky }
139 1.1 plunky
140 1.1 plunky static void
141 1.1 plunky dns_gethostbyname(void)
142 1.1 plunky {
143 1.1 plunky fprintf(stdout, "Simple DNS resolve: ");
144 1.1 plunky dns_ok = 0;
145 1.1 plunky evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
146 1.1 plunky event_dispatch();
147 1.1 plunky
148 1.1 plunky if (dns_ok == DNS_IPv4_A) {
149 1.1 plunky fprintf(stdout, "OK\n");
150 1.1 plunky } else {
151 1.1 plunky fprintf(stdout, "FAILED\n");
152 1.1 plunky exit(1);
153 1.1 plunky }
154 1.1 plunky }
155 1.1 plunky
156 1.1 plunky static void
157 1.1 plunky dns_gethostbyname6(void)
158 1.1 plunky {
159 1.1 plunky fprintf(stdout, "IPv6 DNS resolve: ");
160 1.1 plunky dns_ok = 0;
161 1.1 plunky evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL);
162 1.1 plunky event_dispatch();
163 1.1 plunky
164 1.1 plunky if (dns_ok == DNS_IPv6_AAAA) {
165 1.1 plunky fprintf(stdout, "OK\n");
166 1.1 plunky } else if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) {
167 1.1 plunky fprintf(stdout, "SKIPPED\n");
168 1.1 plunky } else {
169 1.1 plunky fprintf(stdout, "FAILED (%d)\n", dns_ok);
170 1.1 plunky exit(1);
171 1.1 plunky }
172 1.1 plunky }
173 1.1 plunky
174 1.1 plunky static void
175 1.1 plunky dns_gethostbyaddr(void)
176 1.1 plunky {
177 1.1 plunky struct in_addr in;
178 1.1 plunky in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
179 1.1 plunky fprintf(stdout, "Simple reverse DNS resolve: ");
180 1.1 plunky dns_ok = 0;
181 1.1 plunky evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL);
182 1.1 plunky event_dispatch();
183 1.1 plunky
184 1.1 plunky if (dns_ok == DNS_PTR) {
185 1.1 plunky fprintf(stdout, "OK\n");
186 1.1 plunky } else {
187 1.1 plunky fprintf(stdout, "FAILED\n");
188 1.1 plunky exit(1);
189 1.1 plunky }
190 1.1 plunky }
191 1.2 plunky #endif
192 1.1 plunky
193 1.1 plunky static int n_server_responses = 0;
194 1.1 plunky
195 1.1 plunky static void
196 1.1 plunky dns_server_request_cb(struct evdns_server_request *req, void *data)
197 1.1 plunky {
198 1.1 plunky int i, r;
199 1.1 plunky const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa";
200 1.1 plunky for (i = 0; i < req->nquestions; ++i) {
201 1.1 plunky struct in_addr ans;
202 1.1 plunky ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
203 1.1 plunky if (req->questions[i]->type == EVDNS_TYPE_A &&
204 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
205 1.1 plunky !strcmp(req->questions[i]->name, "zz.example.com")) {
206 1.1 plunky r = evdns_server_request_add_a_reply(req, "zz.example.com",
207 1.1 plunky 1, &ans.s_addr, 12345);
208 1.1 plunky if (r<0)
209 1.1 plunky dns_ok = 0;
210 1.1 plunky } else if (req->questions[i]->type == EVDNS_TYPE_AAAA &&
211 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
212 1.1 plunky !strcmp(req->questions[i]->name, "zz.example.com")) {
213 1.1 plunky char addr6[17] = "abcdefghijklmnop";
214 1.1 plunky r = evdns_server_request_add_aaaa_reply(req, "zz.example.com",
215 1.1 plunky 1, addr6, 123);
216 1.1 plunky if (r<0)
217 1.1 plunky dns_ok = 0;
218 1.1 plunky } else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
219 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
220 1.1 plunky !strcmp(req->questions[i]->name, TEST_ARPA)) {
221 1.1 plunky r = evdns_server_request_add_ptr_reply(req, NULL, TEST_ARPA,
222 1.1 plunky "ZZ.EXAMPLE.COM", 54321);
223 1.1 plunky if (r<0)
224 1.1 plunky dns_ok = 0;
225 1.1 plunky } else {
226 1.1 plunky fprintf(stdout, "Unexpected question %d %d \"%s\" ",
227 1.1 plunky req->questions[i]->type,
228 1.1 plunky req->questions[i]->dns_question_class,
229 1.1 plunky req->questions[i]->name);
230 1.1 plunky dns_ok = 0;
231 1.1 plunky }
232 1.1 plunky }
233 1.1 plunky r = evdns_server_request_respond(req, 0);
234 1.1 plunky if (r<0) {
235 1.1 plunky fprintf(stdout, "Couldn't send reply. ");
236 1.1 plunky dns_ok = 0;
237 1.1 plunky }
238 1.1 plunky }
239 1.1 plunky
240 1.1 plunky static void
241 1.1 plunky dns_server_gethostbyname_cb(int result, char type, int count, int ttl,
242 1.1 plunky void *addresses, void *arg)
243 1.1 plunky {
244 1.1 plunky if (result != DNS_ERR_NONE) {
245 1.1 plunky fprintf(stdout, "Unexpected result %d. ", result);
246 1.1 plunky dns_ok = 0;
247 1.1 plunky goto out;
248 1.1 plunky }
249 1.1 plunky if (count != 1) {
250 1.1 plunky fprintf(stdout, "Unexpected answer count %d. ", count);
251 1.1 plunky dns_ok = 0;
252 1.1 plunky goto out;
253 1.1 plunky }
254 1.1 plunky switch (type) {
255 1.1 plunky case DNS_IPv4_A: {
256 1.1 plunky struct in_addr *in_addrs = addresses;
257 1.1 plunky if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) {
258 1.1 plunky fprintf(stdout, "Bad IPv4 response \"%s\" %d. ",
259 1.1 plunky inet_ntoa(in_addrs[0]), ttl);
260 1.1 plunky dns_ok = 0;
261 1.1 plunky goto out;
262 1.1 plunky }
263 1.1 plunky break;
264 1.1 plunky }
265 1.1 plunky case DNS_IPv6_AAAA: {
266 1.1 plunky #if defined (HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
267 1.1 plunky struct in6_addr *in6_addrs = addresses;
268 1.1 plunky char buf[INET6_ADDRSTRLEN+1];
269 1.1 plunky if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16)
270 1.1 plunky || ttl != 123) {
271 1.1 plunky const char *b = inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf));
272 1.1 plunky fprintf(stdout, "Bad IPv6 response \"%s\" %d. ", b, ttl);
273 1.1 plunky dns_ok = 0;
274 1.1 plunky goto out;
275 1.1 plunky }
276 1.1 plunky #endif
277 1.1 plunky break;
278 1.1 plunky }
279 1.1 plunky case DNS_PTR: {
280 1.1 plunky char **addrs = addresses;
281 1.1 plunky if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") || ttl != 54321) {
282 1.1 plunky fprintf(stdout, "Bad PTR response \"%s\" %d. ",
283 1.1 plunky addrs[0], ttl);
284 1.1 plunky dns_ok = 0;
285 1.1 plunky goto out;
286 1.1 plunky }
287 1.1 plunky break;
288 1.1 plunky }
289 1.1 plunky default:
290 1.1 plunky fprintf(stdout, "Bad response type %d. ", type);
291 1.1 plunky dns_ok = 0;
292 1.1 plunky }
293 1.1 plunky
294 1.1 plunky out:
295 1.1 plunky if (++n_server_responses == 3) {
296 1.1 plunky event_loopexit(NULL);
297 1.1 plunky }
298 1.1 plunky }
299 1.1 plunky
300 1.1 plunky static void
301 1.1 plunky dns_server(void)
302 1.1 plunky {
303 1.1 plunky int sock;
304 1.1 plunky struct sockaddr_in my_addr;
305 1.1 plunky struct evdns_server_port *port;
306 1.1 plunky struct in_addr resolve_addr;
307 1.1 plunky
308 1.1 plunky dns_ok = 1;
309 1.1 plunky fprintf(stdout, "DNS server support: ");
310 1.1 plunky
311 1.1 plunky /* Add ourself as the only nameserver, and make sure we really are
312 1.1 plunky * the only nameserver. */
313 1.1 plunky evdns_nameserver_ip_add("127.0.0.1:35353");
314 1.1 plunky if (evdns_count_nameservers() != 1) {
315 1.1 plunky fprintf(stdout, "Couldn't set up.\n");
316 1.1 plunky exit(1);
317 1.1 plunky }
318 1.1 plunky
319 1.1 plunky /* Now configure a nameserver port. */
320 1.1 plunky sock = socket(AF_INET, SOCK_DGRAM, 0);
321 1.1 plunky if (sock == -1) {
322 1.1 plunky perror("socket");
323 1.1 plunky exit(1);
324 1.1 plunky }
325 1.1 plunky #ifdef WIN32
326 1.1 plunky {
327 1.1 plunky u_long nonblocking = 1;
328 1.1 plunky ioctlsocket(sock, FIONBIO, &nonblocking);
329 1.1 plunky }
330 1.1 plunky #else
331 1.1 plunky fcntl(sock, F_SETFL, O_NONBLOCK);
332 1.1 plunky #endif
333 1.1 plunky memset(&my_addr, 0, sizeof(my_addr));
334 1.1 plunky my_addr.sin_family = AF_INET;
335 1.1 plunky my_addr.sin_port = htons(35353);
336 1.1 plunky my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
337 1.1 plunky if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
338 1.1 plunky perror("bind");
339 1.1 plunky exit (1);
340 1.1 plunky }
341 1.1 plunky port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL);
342 1.1 plunky
343 1.1 plunky /* Send two queries. */
344 1.1 plunky evdns_resolve_ipv4("zz.example.com", DNS_QUERY_NO_SEARCH,
345 1.1 plunky dns_server_gethostbyname_cb, NULL);
346 1.1 plunky evdns_resolve_ipv6("zz.example.com", DNS_QUERY_NO_SEARCH,
347 1.1 plunky dns_server_gethostbyname_cb, NULL);
348 1.1 plunky resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
349 1.1 plunky evdns_resolve_reverse(&resolve_addr, 0,
350 1.1 plunky dns_server_gethostbyname_cb, NULL);
351 1.1 plunky
352 1.1 plunky event_dispatch();
353 1.1 plunky
354 1.1 plunky if (dns_ok) {
355 1.1 plunky fprintf(stdout, "OK\n");
356 1.1 plunky } else {
357 1.1 plunky fprintf(stdout, "FAILED\n");
358 1.1 plunky exit(1);
359 1.1 plunky }
360 1.1 plunky
361 1.1 plunky evdns_close_server_port(port);
362 1.1 plunky evdns_shutdown(0); /* remove ourself as nameserver. */
363 1.1 plunky #ifdef WIN32
364 1.1 plunky closesocket(sock);
365 1.1 plunky #else
366 1.1 plunky close(sock);
367 1.1 plunky #endif
368 1.1 plunky }
369 1.1 plunky
370 1.1 plunky void
371 1.1 plunky dns_suite(void)
372 1.1 plunky {
373 1.1 plunky dns_server(); /* Do this before we call evdns_init. */
374 1.1 plunky
375 1.2 plunky #if 0
376 1.1 plunky evdns_init();
377 1.1 plunky dns_gethostbyname();
378 1.1 plunky dns_gethostbyname6();
379 1.1 plunky dns_gethostbyaddr();
380 1.1 plunky
381 1.1 plunky evdns_shutdown(0);
382 1.2 plunky #endif
383 1.1 plunky }
384