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)
78 fprintf(stdout,
"[Timed out] ");
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];
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;
115 for (i = 0; i < count; ++
i)
116 fprintf(stderr,
"%s ", inet_ntoa(in_addrs[i]));
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");
163 fprintf(stdout,
"SKIPPED\n");
165 fprintf(stdout,
"FAILED (%d)\n", dns_ok);
171 dns_gethostbyaddr(
void)
174 in.s_addr = htonl(0x7f000001ul);
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;
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);
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)
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;
301 struct in_addr resolve_addr;
304 fprintf(stdout,
"DNS server support: ");
308 evdns_nameserver_ip_add(
"127.0.0.1:35353");
309 if (evdns_count_nameservers() != 1) {
310 fprintf(stdout,
"Couldn't set up.\n");
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);
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);
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);
372 dns_gethostbyname6();