1 /* $NetBSD: adb_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: adb_test.c,v 1.73 2011/08/30 23:46:51 tbox Exp */
31 #include <isc/buffer.h>
32 #include <isc/entropy.h>
34 #include <isc/socket.h>
36 #include <isc/timer.h>
40 #include <dns/cache.h>
41 #include <dns/dispatch.h>
44 #include <dns/rootns.h>
45 #include <dns/result.h>
47 typedef struct client client_t
;
51 ISC_LINK(client_t
) link
;
55 static isc_mem_t
*mctx
= NULL
;
56 static isc_entropy_t
*ectx
= NULL
;
57 static isc_mempool_t
*cmp
;
58 static isc_log_t
*lctx
;
59 static isc_logconfig_t
*lcfg
;
60 static isc_taskmgr_t
*taskmgr
;
61 static isc_socketmgr_t
*socketmgr
;
62 static isc_timermgr_t
*timermgr
;
63 static dns_dispatchmgr_t
*dispatchmgr
;
64 static isc_task_t
*t1
, *t2
;
65 static dns_view_t
*view
;
66 static dns_db_t
*rootdb
;
67 static ISC_LIST(client_t
) clients
;
68 static isc_mutex_t client_lock
;
69 static isc_stdtime_t now
;
70 static dns_adb_t
*adb
;
73 check_result(isc_result_t result
, const char *format
, ...)
74 ISC_FORMAT_PRINTF(2, 3);
77 check_result(isc_result_t result
, const char *format
, ...) {
80 if (result
== ISC_R_SUCCESS
)
83 va_start(args
, format
);
84 vfprintf(stderr
, format
, args
);
86 fprintf(stderr
, ": %s\n", isc_result_totext(result
));
94 client
= isc_mempool_get(cmp
);
95 INSIST(client
!= NULL
);
96 dns_name_init(&client
->name
, NULL
);
97 ISC_LINK_INIT(client
, link
);
104 free_client(client_t
**c
) {
110 INSIST(client
!= NULL
);
111 dns_name_free(&client
->name
, mctx
);
112 INSIST(!ISC_LINK_LINKED(client
, link
));
113 INSIST(client
->find
== NULL
);
115 isc_mempool_put(cmp
, client
);
120 RUNTIME_CHECK(isc_mutex_lock(&client_lock
) == ISC_R_SUCCESS
);
125 RUNTIME_CHECK(isc_mutex_unlock(&client_lock
) == ISC_R_SUCCESS
);
129 lookup_callback(isc_task_t
*task
, isc_event_t
*ev
) {
133 INSIST(client
->find
== ev
->ev_sender
);
135 printf("NAME %s:\n\tTask %p got event %p type %08x from %p, client %p\n\terr4: %s err6: %s\n",
137 task
, ev
, ev
->ev_type
, client
->find
, client
,
138 isc_result_totext(client
->find
->result_v4
),
139 isc_result_totext(client
->find
->result_v6
));
146 dns_adb_dumpfind(client
->find
, stderr
);
147 dns_adb_destroyfind(&client
->find
);
149 ISC_LIST_UNLINK(clients
, client
, link
);
150 free_client(&client
);
156 create_managers(void) {
160 result
= isc_taskmgr_create(mctx
, 5, 0, &taskmgr
);
161 check_result(result
, "isc_taskmgr_create");
164 result
= isc_timermgr_create(mctx
, &timermgr
);
165 check_result(result
, "isc_timermgr_create");
168 result
= isc_socketmgr_create(mctx
, &socketmgr
);
169 check_result(result
, "isc_socketmgr_create");
172 result
= dns_dispatchmgr_create(mctx
, NULL
, &dispatchmgr
);
173 check_result(result
, "dns_dispatchmgr_create");
185 result
= dns_view_create(mctx
, dns_rdataclass_in
, "_default", &view
);
186 check_result(result
, "dns_view_create");
192 result
= dns_cache_create(mctx
, taskmgr
, timermgr
, dns_rdataclass_in
,
193 "rbt", 0, NULL
, &cache
);
194 check_result(result
, "dns_cache_create");
195 dns_view_setcache(view
, cache
);
196 dns_cache_detach(&cache
);
200 isc_sockaddr_t any4
, any6
;
201 dns_dispatch_t
*disp4
= NULL
;
202 dns_dispatch_t
*disp6
= NULL
;
204 isc_sockaddr_any(&any4
);
205 isc_sockaddr_any6(&any6
);
207 attrs
= DNS_DISPATCHATTR_IPV4
| DNS_DISPATCHATTR_UDP
;
208 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
, socketmgr
,
210 512, 6, 1024, 17, 19,
211 attrs
, attrs
, &disp4
)
213 INSIST(disp4
!= NULL
);
215 attrs
= DNS_DISPATCHATTR_IPV6
| DNS_DISPATCHATTR_UDP
;
216 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
, socketmgr
,
218 512, 6, 1024, 17, 19,
219 attrs
, attrs
, &disp6
)
221 INSIST(disp6
!= NULL
);
223 RUNTIME_CHECK(dns_view_createresolver(view
, taskmgr
, 10, 1,
232 result
= dns_rootns_create(mctx
, dns_rdataclass_in
, NULL
, &rootdb
);
233 check_result(result
, "dns_rootns_create()");
234 dns_view_sethints(view
, rootdb
);
235 dns_db_detach(&rootdb
);
237 dns_view_freeze(view
);
241 lookup(const char *target
) {
243 unsigned char namedata
[256];
245 isc_buffer_t t
, namebuf
;
247 unsigned int options
;
249 INSIST(target
!= NULL
);
251 client
= new_client();
252 isc_buffer_constinit(&t
, target
, strlen(target
));
253 isc_buffer_add(&t
, strlen(target
));
254 isc_buffer_init(&namebuf
, namedata
, sizeof(namedata
));
255 dns_name_init(&name
, NULL
);
256 result
= dns_name_fromtext(&name
, &t
, dns_rootname
, 0, &namebuf
);
257 check_result(result
, "dns_name_fromtext %s", target
);
259 result
= dns_name_dup(&name
, mctx
, &client
->name
);
260 check_result(result
, "dns_name_dup %s", target
);
263 options
|= DNS_ADBFIND_INET
;
264 options
|= DNS_ADBFIND_INET6
;
265 options
|= DNS_ADBFIND_WANTEVENT
;
266 options
|= DNS_ADBFIND_HINTOK
;
267 options
|= DNS_ADBFIND_GLUEOK
;
268 result
= dns_adb_createfind(adb
, t2
, lookup_callback
, client
,
269 &client
->name
, dns_rootname
, 0, options
,
270 now
, NULL
, view
->dstport
, &client
->find
);
271 if (result
!= ISC_R_SUCCESS
)
272 printf("DNS_ADB_CREATEFIND -> %s\n", dns_result_totext(result
));
273 dns_adb_dumpfind(client
->find
, stderr
);
275 if ((client
->find
->options
& DNS_ADBFIND_WANTEVENT
) != 0) {
276 client
->target
= target
;
277 ISC_LIST_APPEND(clients
, client
, link
);
279 printf("NAME %s: err4 %s, err6 %s\n",
280 target
, isc_result_totext(client
->find
->result_v4
),
281 isc_result_totext(client
->find
->result_v6
));
283 dns_adb_destroyfind(&client
->find
);
284 free_client(&client
);
289 main(int argc
, char **argv
) {
291 isc_logdestination_t destination
;
296 dns_result_register();
297 result
= isc_app_start();
298 check_result(result
, "isc_app_start()");
300 isc_stdtime_get(&now
);
302 result
= isc_mutex_init(&client_lock
);
303 check_result(result
, "isc_mutex_init(&client_lock)");
304 ISC_LIST_INIT(clients
);
307 * EVERYTHING needs a memory context.
309 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
312 RUNTIME_CHECK(isc_mempool_create(mctx
, sizeof(client_t
), &cmp
)
314 isc_mempool_setname(cmp
, "adb test clients");
316 result
= isc_entropy_create(mctx
, &ectx
);
317 check_result(result
, "isc_entropy_create()");
318 result
= isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
);
319 check_result(result
, "isc_hash_create()");
321 result
= isc_log_create(mctx
, &lctx
, &lcfg
);
322 check_result(result
, "isc_log_create()");
323 isc_log_setcontext(lctx
);
325 dns_log_setcontext(lctx
);
328 * Create and install the default channel.
330 destination
.file
.stream
= stderr
;
331 destination
.file
.name
= NULL
;
332 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
333 destination
.file
.maximum_size
= 0;
334 result
= isc_log_createchannel(lcfg
, "_default",
337 &destination
, ISC_LOG_PRINTTIME
);
338 check_result(result
, "isc_log_createchannel()");
339 result
= isc_log_usechannel(lcfg
, "_default", NULL
, NULL
);
340 check_result(result
, "isc_log_usechannel()");
343 * Set the initial debug level.
345 isc_log_setdebuglevel(lctx
, 2);
350 result
= isc_task_create(taskmgr
, 0, &t1
);
351 check_result(result
, "isc_task_create t1");
353 result
= isc_task_create(taskmgr
, 0, &t2
);
354 check_result(result
, "isc_task_create t2");
356 printf("task 1 = %p\n", t1
);
357 printf("task 2 = %p\n", t2
);
364 * Lock the entire client list here. This will cause all events
365 * for found names to block as well.
368 lookup("f.root-servers.net."); /* Should be in hints */
369 lookup("www.iengines.com"); /* should fetch */
370 lookup("www.isc.org"); /* should fetch */
371 lookup("www.flame.org"); /* should fetch */
372 lookup("kechara.flame.org."); /* should fetch */
373 lookup("moghedien.flame.org."); /* should fetch */
374 lookup("mailrelay.flame.org."); /* should fetch */
375 lookup("ipv4v6.flame.org."); /* should fetch */
376 lookup("nonexistant.flame.org."); /* should fail to be found */
377 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
378 lookup("i.root-servers.net."); /* Should be in hints */
379 lookup("www.firstcard.com.");
380 lookup("dns04.flame.org.");
385 dns_adb_dump(adb
, stderr
);
390 lookup("f.root-servers.net."); /* Should be in hints */
391 lookup("www.iengines.com"); /* should fetch */
392 lookup("www.isc.org"); /* should fetch */
393 lookup("www.flame.org"); /* should fetch */
394 lookup("kechara.flame.org."); /* should fetch */
395 lookup("moghedien.flame.org."); /* should fetch */
396 lookup("mailrelay.flame.org."); /* should fetch */
397 lookup("ipv4v6.flame.org."); /* should fetch */
398 lookup("nonexistant.flame.org."); /* should fail to be found */
399 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
400 lookup("i.root-servers.net."); /* Should be in hints */
405 dns_adb_dump(adb
, stderr
);
407 isc_task_detach(&t1
);
408 isc_task_detach(&t2
);
410 isc_mem_stats(mctx
, stdout
);
411 dns_adb_dump(adb
, stderr
);
415 dns_adb_dump(adb
, stderr
);
417 dns_view_detach(&view
);
420 fprintf(stderr
, "Destroying socket manager\n");
421 isc_socketmgr_destroy(&socketmgr
);
422 fprintf(stderr
, "Destroying timer manager\n");
423 isc_timermgr_destroy(&timermgr
);
425 fprintf(stderr
, "Destroying task manager\n");
426 isc_taskmgr_destroy(&taskmgr
);
428 isc_log_destroy(&lctx
);
431 isc_entropy_detach(&ectx
);
433 isc_mempool_destroy(&cmp
);
434 isc_mem_stats(mctx
, stdout
);
435 isc_mem_destroy(&mctx
);