regress_dns.c revision 1.1.1.1 1 1.1 plunky /* $NetBSD: regress_dns.c,v 1.1.1.1 2009/11/02 10:01:03 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.1 plunky static int dns_err = 0;
69 1.1 plunky
70 1.1 plunky void dns_suite(void);
71 1.1 plunky
72 1.1 plunky static void
73 1.1 plunky dns_gethostbyname_cb(int result, char type, int count, int ttl,
74 1.1 plunky void *addresses, void *arg)
75 1.1 plunky {
76 1.1 plunky dns_ok = dns_err = 0;
77 1.1 plunky
78 1.1 plunky if (result == DNS_ERR_TIMEOUT) {
79 1.1 plunky fprintf(stdout, "[Timed out] ");
80 1.1 plunky dns_err = result;
81 1.1 plunky goto out;
82 1.1 plunky }
83 1.1 plunky
84 1.1 plunky if (result != DNS_ERR_NONE) {
85 1.1 plunky fprintf(stdout, "[Error code %d] ", result);
86 1.1 plunky goto out;
87 1.1 plunky }
88 1.1 plunky
89 1.1 plunky fprintf(stderr, "type: %d, count: %d, ttl: %d: ", type, count, ttl);
90 1.1 plunky
91 1.1 plunky switch (type) {
92 1.1 plunky case DNS_IPv6_AAAA: {
93 1.1 plunky #if defined(HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
94 1.1 plunky struct in6_addr *in6_addrs = addresses;
95 1.1 plunky char buf[INET6_ADDRSTRLEN+1];
96 1.1 plunky int i;
97 1.1 plunky /* a resolution that's not valid does not help */
98 1.1 plunky if (ttl < 0)
99 1.1 plunky goto out;
100 1.1 plunky for (i = 0; i < count; ++i) {
101 1.1 plunky const char *b = inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf));
102 1.1 plunky if (b)
103 1.1 plunky fprintf(stderr, "%s ", b);
104 1.1 plunky else
105 1.1 plunky fprintf(stderr, "%s ", strerror(errno));
106 1.1 plunky }
107 1.1 plunky #endif
108 1.1 plunky break;
109 1.1 plunky }
110 1.1 plunky case DNS_IPv4_A: {
111 1.1 plunky struct in_addr *in_addrs = addresses;
112 1.1 plunky int i;
113 1.1 plunky /* a resolution that's not valid does not help */
114 1.1 plunky if (ttl < 0)
115 1.1 plunky goto out;
116 1.1 plunky for (i = 0; i < count; ++i)
117 1.1 plunky fprintf(stderr, "%s ", inet_ntoa(in_addrs[i]));
118 1.1 plunky break;
119 1.1 plunky }
120 1.1 plunky case DNS_PTR:
121 1.1 plunky /* may get at most one PTR */
122 1.1 plunky if (count != 1)
123 1.1 plunky goto out;
124 1.1 plunky
125 1.1 plunky fprintf(stderr, "%s ", *(char **)addresses);
126 1.1 plunky break;
127 1.1 plunky default:
128 1.1 plunky goto out;
129 1.1 plunky }
130 1.1 plunky
131 1.1 plunky dns_ok = type;
132 1.1 plunky
133 1.1 plunky out:
134 1.1 plunky event_loopexit(NULL);
135 1.1 plunky }
136 1.1 plunky
137 1.1 plunky static void
138 1.1 plunky dns_gethostbyname(void)
139 1.1 plunky {
140 1.1 plunky fprintf(stdout, "Simple DNS resolve: ");
141 1.1 plunky dns_ok = 0;
142 1.1 plunky evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
143 1.1 plunky event_dispatch();
144 1.1 plunky
145 1.1 plunky if (dns_ok == DNS_IPv4_A) {
146 1.1 plunky fprintf(stdout, "OK\n");
147 1.1 plunky } else {
148 1.1 plunky fprintf(stdout, "FAILED\n");
149 1.1 plunky exit(1);
150 1.1 plunky }
151 1.1 plunky }
152 1.1 plunky
153 1.1 plunky static void
154 1.1 plunky dns_gethostbyname6(void)
155 1.1 plunky {
156 1.1 plunky fprintf(stdout, "IPv6 DNS resolve: ");
157 1.1 plunky dns_ok = 0;
158 1.1 plunky evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL);
159 1.1 plunky event_dispatch();
160 1.1 plunky
161 1.1 plunky if (dns_ok == DNS_IPv6_AAAA) {
162 1.1 plunky fprintf(stdout, "OK\n");
163 1.1 plunky } else if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) {
164 1.1 plunky fprintf(stdout, "SKIPPED\n");
165 1.1 plunky } else {
166 1.1 plunky fprintf(stdout, "FAILED (%d)\n", dns_ok);
167 1.1 plunky exit(1);
168 1.1 plunky }
169 1.1 plunky }
170 1.1 plunky
171 1.1 plunky static void
172 1.1 plunky dns_gethostbyaddr(void)
173 1.1 plunky {
174 1.1 plunky struct in_addr in;
175 1.1 plunky in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
176 1.1 plunky fprintf(stdout, "Simple reverse DNS resolve: ");
177 1.1 plunky dns_ok = 0;
178 1.1 plunky evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL);
179 1.1 plunky event_dispatch();
180 1.1 plunky
181 1.1 plunky if (dns_ok == DNS_PTR) {
182 1.1 plunky fprintf(stdout, "OK\n");
183 1.1 plunky } else {
184 1.1 plunky fprintf(stdout, "FAILED\n");
185 1.1 plunky exit(1);
186 1.1 plunky }
187 1.1 plunky }
188 1.1 plunky
189 1.1 plunky static int n_server_responses = 0;
190 1.1 plunky
191 1.1 plunky static void
192 1.1 plunky dns_server_request_cb(struct evdns_server_request *req, void *data)
193 1.1 plunky {
194 1.1 plunky int i, r;
195 1.1 plunky const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa";
196 1.1 plunky for (i = 0; i < req->nquestions; ++i) {
197 1.1 plunky struct in_addr ans;
198 1.1 plunky ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
199 1.1 plunky if (req->questions[i]->type == EVDNS_TYPE_A &&
200 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
201 1.1 plunky !strcmp(req->questions[i]->name, "zz.example.com")) {
202 1.1 plunky r = evdns_server_request_add_a_reply(req, "zz.example.com",
203 1.1 plunky 1, &ans.s_addr, 12345);
204 1.1 plunky if (r<0)
205 1.1 plunky dns_ok = 0;
206 1.1 plunky } else if (req->questions[i]->type == EVDNS_TYPE_AAAA &&
207 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
208 1.1 plunky !strcmp(req->questions[i]->name, "zz.example.com")) {
209 1.1 plunky char addr6[17] = "abcdefghijklmnop";
210 1.1 plunky r = evdns_server_request_add_aaaa_reply(req, "zz.example.com",
211 1.1 plunky 1, addr6, 123);
212 1.1 plunky if (r<0)
213 1.1 plunky dns_ok = 0;
214 1.1 plunky } else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
215 1.1 plunky req->questions[i]->dns_question_class == EVDNS_CLASS_INET &&
216 1.1 plunky !strcmp(req->questions[i]->name, TEST_ARPA)) {
217 1.1 plunky r = evdns_server_request_add_ptr_reply(req, NULL, TEST_ARPA,
218 1.1 plunky "ZZ.EXAMPLE.COM", 54321);
219 1.1 plunky if (r<0)
220 1.1 plunky dns_ok = 0;
221 1.1 plunky } else {
222 1.1 plunky fprintf(stdout, "Unexpected question %d %d \"%s\" ",
223 1.1 plunky req->questions[i]->type,
224 1.1 plunky req->questions[i]->dns_question_class,
225 1.1 plunky req->questions[i]->name);
226 1.1 plunky dns_ok = 0;
227 1.1 plunky }
228 1.1 plunky }
229 1.1 plunky r = evdns_server_request_respond(req, 0);
230 1.1 plunky if (r<0) {
231 1.1 plunky fprintf(stdout, "Couldn't send reply. ");
232 1.1 plunky dns_ok = 0;
233 1.1 plunky }
234 1.1 plunky }
235 1.1 plunky
236 1.1 plunky static void
237 1.1 plunky dns_server_gethostbyname_cb(int result, char type, int count, int ttl,
238 1.1 plunky void *addresses, void *arg)
239 1.1 plunky {
240 1.1 plunky if (result != DNS_ERR_NONE) {
241 1.1 plunky fprintf(stdout, "Unexpected result %d. ", result);
242 1.1 plunky dns_ok = 0;
243 1.1 plunky goto out;
244 1.1 plunky }
245 1.1 plunky if (count != 1) {
246 1.1 plunky fprintf(stdout, "Unexpected answer count %d. ", count);
247 1.1 plunky dns_ok = 0;
248 1.1 plunky goto out;
249 1.1 plunky }
250 1.1 plunky switch (type) {
251 1.1 plunky case DNS_IPv4_A: {
252 1.1 plunky struct in_addr *in_addrs = addresses;
253 1.1 plunky if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) {
254 1.1 plunky fprintf(stdout, "Bad IPv4 response \"%s\" %d. ",
255 1.1 plunky inet_ntoa(in_addrs[0]), ttl);
256 1.1 plunky dns_ok = 0;
257 1.1 plunky goto out;
258 1.1 plunky }
259 1.1 plunky break;
260 1.1 plunky }
261 1.1 plunky case DNS_IPv6_AAAA: {
262 1.1 plunky #if defined (HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
263 1.1 plunky struct in6_addr *in6_addrs = addresses;
264 1.1 plunky char buf[INET6_ADDRSTRLEN+1];
265 1.1 plunky if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16)
266 1.1 plunky || ttl != 123) {
267 1.1 plunky const char *b = inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf));
268 1.1 plunky fprintf(stdout, "Bad IPv6 response \"%s\" %d. ", b, ttl);
269 1.1 plunky dns_ok = 0;
270 1.1 plunky goto out;
271 1.1 plunky }
272 1.1 plunky #endif
273 1.1 plunky break;
274 1.1 plunky }
275 1.1 plunky case DNS_PTR: {
276 1.1 plunky char **addrs = addresses;
277 1.1 plunky if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") || ttl != 54321) {
278 1.1 plunky fprintf(stdout, "Bad PTR response \"%s\" %d. ",
279 1.1 plunky addrs[0], ttl);
280 1.1 plunky dns_ok = 0;
281 1.1 plunky goto out;
282 1.1 plunky }
283 1.1 plunky break;
284 1.1 plunky }
285 1.1 plunky default:
286 1.1 plunky fprintf(stdout, "Bad response type %d. ", type);
287 1.1 plunky dns_ok = 0;
288 1.1 plunky }
289 1.1 plunky
290 1.1 plunky out:
291 1.1 plunky if (++n_server_responses == 3) {
292 1.1 plunky event_loopexit(NULL);
293 1.1 plunky }
294 1.1 plunky }
295 1.1 plunky
296 1.1 plunky static void
297 1.1 plunky dns_server(void)
298 1.1 plunky {
299 1.1 plunky int sock;
300 1.1 plunky struct sockaddr_in my_addr;
301 1.1 plunky struct evdns_server_port *port;
302 1.1 plunky struct in_addr resolve_addr;
303 1.1 plunky
304 1.1 plunky dns_ok = 1;
305 1.1 plunky fprintf(stdout, "DNS server support: ");
306 1.1 plunky
307 1.1 plunky /* Add ourself as the only nameserver, and make sure we really are
308 1.1 plunky * the only nameserver. */
309 1.1 plunky evdns_nameserver_ip_add("127.0.0.1:35353");
310 1.1 plunky if (evdns_count_nameservers() != 1) {
311 1.1 plunky fprintf(stdout, "Couldn't set up.\n");
312 1.1 plunky exit(1);
313 1.1 plunky }
314 1.1 plunky
315 1.1 plunky /* Now configure a nameserver port. */
316 1.1 plunky sock = socket(AF_INET, SOCK_DGRAM, 0);
317 1.1 plunky if (sock == -1) {
318 1.1 plunky perror("socket");
319 1.1 plunky exit(1);
320 1.1 plunky }
321 1.1 plunky #ifdef WIN32
322 1.1 plunky {
323 1.1 plunky u_long nonblocking = 1;
324 1.1 plunky ioctlsocket(sock, FIONBIO, &nonblocking);
325 1.1 plunky }
326 1.1 plunky #else
327 1.1 plunky fcntl(sock, F_SETFL, O_NONBLOCK);
328 1.1 plunky #endif
329 1.1 plunky memset(&my_addr, 0, sizeof(my_addr));
330 1.1 plunky my_addr.sin_family = AF_INET;
331 1.1 plunky my_addr.sin_port = htons(35353);
332 1.1 plunky my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
333 1.1 plunky if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
334 1.1 plunky perror("bind");
335 1.1 plunky exit (1);
336 1.1 plunky }
337 1.1 plunky port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL);
338 1.1 plunky
339 1.1 plunky /* Send two queries. */
340 1.1 plunky evdns_resolve_ipv4("zz.example.com", DNS_QUERY_NO_SEARCH,
341 1.1 plunky dns_server_gethostbyname_cb, NULL);
342 1.1 plunky evdns_resolve_ipv6("zz.example.com", DNS_QUERY_NO_SEARCH,
343 1.1 plunky dns_server_gethostbyname_cb, NULL);
344 1.1 plunky resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
345 1.1 plunky evdns_resolve_reverse(&resolve_addr, 0,
346 1.1 plunky dns_server_gethostbyname_cb, NULL);
347 1.1 plunky
348 1.1 plunky event_dispatch();
349 1.1 plunky
350 1.1 plunky if (dns_ok) {
351 1.1 plunky fprintf(stdout, "OK\n");
352 1.1 plunky } else {
353 1.1 plunky fprintf(stdout, "FAILED\n");
354 1.1 plunky exit(1);
355 1.1 plunky }
356 1.1 plunky
357 1.1 plunky evdns_close_server_port(port);
358 1.1 plunky evdns_shutdown(0); /* remove ourself as nameserver. */
359 1.1 plunky #ifdef WIN32
360 1.1 plunky closesocket(sock);
361 1.1 plunky #else
362 1.1 plunky close(sock);
363 1.1 plunky #endif
364 1.1 plunky }
365 1.1 plunky
366 1.1 plunky void
367 1.1 plunky dns_suite(void)
368 1.1 plunky {
369 1.1 plunky dns_server(); /* Do this before we call evdns_init. */
370 1.1 plunky
371 1.1 plunky evdns_init();
372 1.1 plunky dns_gethostbyname();
373 1.1 plunky dns_gethostbyname6();
374 1.1 plunky dns_gethostbyaddr();
375 1.1 plunky
376 1.1 plunky evdns_shutdown(0);
377 1.1 plunky }
378