2 * Copyright (c) 2003-2006 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIME_H
42 #include <sys/queue.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #ifdef HAVE_NETINET_IN6_H
51 #include <netinet/in6.h>
66 static int dns_ok
= 0;
67 static int dns_err
= 0;
72 dns_gethostbyname_cb(int result
, char type
, int count
, int ttl
,
73 void *addresses
, void *arg
)
77 if (result
== DNS_ERR_TIMEOUT
) {
78 fprintf(stdout
, "[Timed out] ");
83 if (result
!= DNS_ERR_NONE
) {
84 fprintf(stdout
, "[Error code %d] ", result
);
88 fprintf(stderr
, "type: %d, count: %d, ttl: %d: ", type
, count
, ttl
);
92 #if defined(HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
93 struct in6_addr
*in6_addrs
= addresses
;
94 char buf
[INET6_ADDRSTRLEN
+1];
96 /* a resolution that's not valid does not help */
99 for (i
= 0; i
< count
; ++i
) {
100 const char *b
= inet_ntop(AF_INET6
, &in6_addrs
[i
], buf
,sizeof(buf
));
102 fprintf(stderr
, "%s ", b
);
104 fprintf(stderr
, "%s ", strerror(errno
));
110 struct in_addr
*in_addrs
= addresses
;
112 /* a resolution that's not valid does not help */
115 for (i
= 0; i
< count
; ++i
)
116 fprintf(stderr
, "%s ", inet_ntoa(in_addrs
[i
]));
120 /* may get at most one PTR */
124 fprintf(stderr
, "%s ", *(char **)addresses
);
133 event_loopexit(NULL
);
137 dns_gethostbyname(void)
139 fprintf(stdout
, "Simple DNS resolve: ");
141 evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb
, NULL
);
144 if (dns_ok
== DNS_IPv4_A
) {
145 fprintf(stdout
, "OK\n");
147 fprintf(stdout
, "FAILED\n");
153 dns_gethostbyname6(void)
155 fprintf(stdout
, "IPv6 DNS resolve: ");
157 evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb
, NULL
);
160 if (dns_ok
== DNS_IPv6_AAAA
) {
161 fprintf(stdout
, "OK\n");
162 } else if (!dns_ok
&& dns_err
== DNS_ERR_TIMEOUT
) {
163 fprintf(stdout
, "SKIPPED\n");
165 fprintf(stdout
, "FAILED (%d)\n", dns_ok
);
171 dns_gethostbyaddr(void)
174 in
.s_addr
= htonl(0x7f000001ul
); /* 127.0.0.1 */
175 fprintf(stdout
, "Simple reverse DNS resolve: ");
177 evdns_resolve_reverse(&in
, 0, dns_gethostbyname_cb
, NULL
);
180 if (dns_ok
== DNS_PTR
) {
181 fprintf(stdout
, "OK\n");
183 fprintf(stdout
, "FAILED\n");
188 static int n_server_responses
= 0;
191 dns_server_request_cb(struct evdns_server_request
*req
, void *data
)
194 const char TEST_ARPA
[] = "11.11.168.192.in-addr.arpa";
195 for (i
= 0; i
< req
->nquestions
; ++i
) {
197 ans
.s_addr
= htonl(0xc0a80b0bUL
); /* 192.168.11.11 */
198 if (req
->questions
[i
]->type
== EVDNS_TYPE_A
&&
199 req
->questions
[i
]->dns_question_class
== EVDNS_CLASS_INET
&&
200 !strcmp(req
->questions
[i
]->name
, "zz.example.com")) {
201 r
= evdns_server_request_add_a_reply(req
, "zz.example.com",
202 1, &ans
.s_addr
, 12345);
205 } else if (req
->questions
[i
]->type
== EVDNS_TYPE_AAAA
&&
206 req
->questions
[i
]->dns_question_class
== EVDNS_CLASS_INET
&&
207 !strcmp(req
->questions
[i
]->name
, "zz.example.com")) {
208 char addr6
[17] = "abcdefghijklmnop";
209 r
= evdns_server_request_add_aaaa_reply(req
, "zz.example.com",
213 } else if (req
->questions
[i
]->type
== EVDNS_TYPE_PTR
&&
214 req
->questions
[i
]->dns_question_class
== EVDNS_CLASS_INET
&&
215 !strcmp(req
->questions
[i
]->name
, TEST_ARPA
)) {
216 r
= evdns_server_request_add_ptr_reply(req
, NULL
, TEST_ARPA
,
217 "ZZ.EXAMPLE.COM", 54321);
221 fprintf(stdout
, "Unexpected question %d %d \"%s\" ",
222 req
->questions
[i
]->type
,
223 req
->questions
[i
]->dns_question_class
,
224 req
->questions
[i
]->name
);
228 r
= evdns_server_request_respond(req
, 0);
230 fprintf(stdout
, "Couldn't send reply. ");
236 dns_server_gethostbyname_cb(int result
, char type
, int count
, int ttl
,
237 void *addresses
, void *arg
)
239 if (result
!= DNS_ERR_NONE
) {
240 fprintf(stdout
, "Unexpected result %d. ", result
);
245 fprintf(stdout
, "Unexpected answer count %d. ", count
);
251 struct in_addr
*in_addrs
= addresses
;
252 if (in_addrs
[0].s_addr
!= htonl(0xc0a80b0bUL
) || ttl
!= 12345) {
253 fprintf(stdout
, "Bad IPv4 response \"%s\" %d. ",
254 inet_ntoa(in_addrs
[0]), ttl
);
260 case DNS_IPv6_AAAA
: {
261 #if defined (HAVE_STRUCT_IN6_ADDR) && defined(HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
262 struct in6_addr
*in6_addrs
= addresses
;
263 char buf
[INET6_ADDRSTRLEN
+1];
264 if (memcmp(&in6_addrs
[0].s6_addr
, "abcdefghijklmnop", 16)
266 const char *b
= inet_ntop(AF_INET6
, &in6_addrs
[0],buf
,sizeof(buf
));
267 fprintf(stdout
, "Bad IPv6 response \"%s\" %d. ", b
, ttl
);
275 char **addrs
= addresses
;
276 if (strcmp(addrs
[0], "ZZ.EXAMPLE.COM") || ttl
!= 54321) {
277 fprintf(stdout
, "Bad PTR response \"%s\" %d. ",
285 fprintf(stdout
, "Bad response type %d. ", type
);
290 if (++n_server_responses
== 3) {
291 event_loopexit(NULL
);
299 struct sockaddr_in my_addr
;
300 struct evdns_server_port
*port
;
301 struct in_addr resolve_addr
;
304 fprintf(stdout
, "DNS server support: ");
306 /* Add ourself as the only nameserver, and make sure we really are
307 * the only nameserver. */
308 evdns_nameserver_ip_add("127.0.0.1:35353");
309 if (evdns_count_nameservers() != 1) {
310 fprintf(stdout
, "Couldn't set up.\n");
314 /* Now configure a nameserver port. */
315 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
322 u_long nonblocking
= 1;
323 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
326 fcntl(sock
, F_SETFL
, O_NONBLOCK
);
328 memset(&my_addr
, 0, sizeof(my_addr
));
329 my_addr
.sin_family
= AF_INET
;
330 my_addr
.sin_port
= htons(35353);
331 my_addr
.sin_addr
.s_addr
= htonl(0x7f000001UL
);
332 if (bind(sock
, (struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0) {
336 port
= evdns_add_server_port(sock
, 0, dns_server_request_cb
, NULL
);
338 /* Send two queries. */
339 evdns_resolve_ipv4("zz.example.com", DNS_QUERY_NO_SEARCH
,
340 dns_server_gethostbyname_cb
, NULL
);
341 evdns_resolve_ipv6("zz.example.com", DNS_QUERY_NO_SEARCH
,
342 dns_server_gethostbyname_cb
, NULL
);
343 resolve_addr
.s_addr
= htonl(0xc0a80b0bUL
); /* 192.168.11.11 */
344 evdns_resolve_reverse(&resolve_addr
, 0,
345 dns_server_gethostbyname_cb
, NULL
);
350 fprintf(stdout
, "OK\n");
352 fprintf(stdout
, "FAILED\n");
356 evdns_close_server_port(port
);
357 evdns_shutdown(0); /* remove ourself as nameserver. */
368 dns_server(); /* Do this before we call evdns_init. */
372 dns_gethostbyname6();