4 * Copyright (C) 2004, 2005, 2007, 2009 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.70 2009/09/02 23:48:01 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
,
209 taskmgr
, &any4
, 512, 6, 1024,
210 17, 19, attrs
, attrs
, &disp4
)
212 INSIST(disp4
!= NULL
);
214 attrs
= DNS_DISPATCHATTR_IPV6
| DNS_DISPATCHATTR_UDP
;
215 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
, socketmgr
,
216 taskmgr
, &any6
, 512, 6, 1024,
217 17, 19, attrs
, attrs
, &disp6
)
219 INSIST(disp6
!= NULL
);
221 RUNTIME_CHECK(dns_view_createresolver(view
, taskmgr
, 10,
230 result
= dns_rootns_create(mctx
, dns_rdataclass_in
, NULL
, &rootdb
);
231 check_result(result
, "dns_rootns_create()");
232 dns_view_sethints(view
, rootdb
);
233 dns_db_detach(&rootdb
);
235 dns_view_freeze(view
);
239 lookup(const char *target
) {
241 unsigned char namedata
[256];
243 isc_buffer_t t
, namebuf
;
245 unsigned int options
;
247 INSIST(target
!= NULL
);
249 client
= new_client();
250 isc_buffer_init(&t
, target
, strlen(target
));
251 isc_buffer_add(&t
, strlen(target
));
252 isc_buffer_init(&namebuf
, namedata
, sizeof(namedata
));
253 dns_name_init(&name
, NULL
);
254 result
= dns_name_fromtext(&name
, &t
, dns_rootname
, 0, &namebuf
);
255 check_result(result
, "dns_name_fromtext %s", target
);
257 result
= dns_name_dup(&name
, mctx
, &client
->name
);
258 check_result(result
, "dns_name_dup %s", target
);
261 options
|= DNS_ADBFIND_INET
;
262 options
|= DNS_ADBFIND_INET6
;
263 options
|= DNS_ADBFIND_WANTEVENT
;
264 options
|= DNS_ADBFIND_HINTOK
;
265 options
|= DNS_ADBFIND_GLUEOK
;
266 result
= dns_adb_createfind(adb
, t2
, lookup_callback
, client
,
267 &client
->name
, dns_rootname
, 0, options
,
268 now
, NULL
, view
->dstport
, &client
->find
);
270 check_result(result
, "dns_adb_createfind()");
272 dns_adb_dumpfind(client
->find
, stderr
);
274 if ((client
->find
->options
& DNS_ADBFIND_WANTEVENT
) != 0) {
275 client
->target
= target
;
276 ISC_LIST_APPEND(clients
, client
, link
);
278 printf("NAME %s: err4 %s, err6 %s\n",
279 target
, isc_result_totext(client
->find
->result_v4
),
280 isc_result_totext(client
->find
->result_v6
));
282 dns_adb_destroyfind(&client
->find
);
283 free_client(&client
);
288 main(int argc
, char **argv
) {
290 isc_logdestination_t destination
;
295 dns_result_register();
296 result
= isc_app_start();
297 check_result(result
, "isc_app_start()");
299 isc_stdtime_get(&now
);
301 result
= isc_mutex_init(&client_lock
);
302 check_result(result
, "isc_mutex_init(&client_lock)");
303 ISC_LIST_INIT(clients
);
306 * EVERYTHING needs a memory context.
308 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
311 RUNTIME_CHECK(isc_mempool_create(mctx
, sizeof(client_t
), &cmp
)
313 isc_mempool_setname(cmp
, "adb test clients");
315 result
= isc_entropy_create(mctx
, &ectx
);
316 check_result(result
, "isc_entropy_create()");
317 result
= isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
);
318 check_result(result
, "isc_hash_create()");
320 result
= isc_log_create(mctx
, &lctx
, &lcfg
);
321 check_result(result
, "isc_log_create()");
322 isc_log_setcontext(lctx
);
324 dns_log_setcontext(lctx
);
327 * Create and install the default channel.
329 destination
.file
.stream
= stderr
;
330 destination
.file
.name
= NULL
;
331 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
332 destination
.file
.maximum_size
= 0;
333 result
= isc_log_createchannel(lcfg
, "_default",
336 &destination
, ISC_LOG_PRINTTIME
);
337 check_result(result
, "isc_log_createchannel()");
338 result
= isc_log_usechannel(lcfg
, "_default", NULL
, NULL
);
339 check_result(result
, "isc_log_usechannel()");
342 * Set the initial debug level.
344 isc_log_setdebuglevel(lctx
, 2);
349 result
= isc_task_create(taskmgr
, 0, &t1
);
350 check_result(result
, "isc_task_create t1");
352 result
= isc_task_create(taskmgr
, 0, &t2
);
353 check_result(result
, "isc_task_create t2");
355 printf("task 1 = %p\n", t1
);
356 printf("task 2 = %p\n", t2
);
363 * Lock the entire client list here. This will cause all events
364 * for found names to block as well.
367 lookup("f.root-servers.net."); /* Should be in hints */
368 lookup("www.iengines.com"); /* should fetch */
369 lookup("www.isc.org"); /* should fetch */
370 lookup("www.flame.org"); /* should fetch */
371 lookup("kechara.flame.org."); /* should fetch */
372 lookup("moghedien.flame.org."); /* should fetch */
373 lookup("mailrelay.flame.org."); /* should fetch */
374 lookup("ipv4v6.flame.org."); /* should fetch */
375 lookup("nonexistant.flame.org."); /* should fail to be found */
376 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
377 lookup("i.root-servers.net."); /* Should be in hints */
378 lookup("www.firstcard.com.");
379 lookup("dns04.flame.org.");
384 dns_adb_dump(adb
, stderr
);
389 lookup("f.root-servers.net."); /* Should be in hints */
390 lookup("www.iengines.com"); /* should fetch */
391 lookup("www.isc.org"); /* should fetch */
392 lookup("www.flame.org"); /* should fetch */
393 lookup("kechara.flame.org."); /* should fetch */
394 lookup("moghedien.flame.org."); /* should fetch */
395 lookup("mailrelay.flame.org."); /* should fetch */
396 lookup("ipv4v6.flame.org."); /* should fetch */
397 lookup("nonexistant.flame.org."); /* should fail to be found */
398 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
399 lookup("i.root-servers.net."); /* Should be in hints */
404 dns_adb_dump(adb
, stderr
);
406 isc_task_detach(&t1
);
407 isc_task_detach(&t2
);
409 isc_mem_stats(mctx
, stdout
);
410 dns_adb_dump(adb
, stderr
);
414 dns_adb_dump(adb
, stderr
);
416 dns_view_detach(&view
);
419 isc_socketmgr_destroy(&socketmgr
);
420 isc_timermgr_destroy(&timermgr
);
422 fprintf(stderr
, "Destroying task manager\n");
423 isc_taskmgr_destroy(&taskmgr
);
425 isc_log_destroy(&lctx
);
428 isc_entropy_detach(&ectx
);
430 isc_mempool_destroy(&cmp
);
431 isc_mem_stats(mctx
, stdout
);
432 isc_mem_destroy(&mctx
);