2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: adb_test.c,v 1.68 2007/06/19 23:46:59 tbox Exp $ */
29 #include <isc/buffer.h>
30 #include <isc/entropy.h>
32 #include <isc/socket.h>
34 #include <isc/timer.h>
38 #include <dns/cache.h>
39 #include <dns/dispatch.h>
42 #include <dns/rootns.h>
43 #include <dns/result.h>
45 typedef struct client client_t;
49 ISC_LINK(client_t) link;
53 static isc_mem_t *mctx = NULL;
54 static isc_entropy_t *ectx = NULL;
55 static isc_mempool_t *cmp;
56 static isc_log_t *lctx;
57 static isc_logconfig_t *lcfg;
58 static isc_taskmgr_t *taskmgr;
59 static isc_socketmgr_t *socketmgr;
60 static isc_timermgr_t *timermgr;
61 static dns_dispatchmgr_t *dispatchmgr;
62 static isc_task_t *t1, *t2;
63 static dns_view_t *view;
64 static dns_db_t *rootdb;
65 static ISC_LIST(client_t) clients;
66 static isc_mutex_t client_lock;
67 static isc_stdtime_t now;
68 static dns_adb_t *adb;
71 check_result(isc_result_t result, const char *format, ...)
72 ISC_FORMAT_PRINTF(2, 3);
75 check_result(isc_result_t result, const char *format, ...) {
78 if (result == ISC_R_SUCCESS)
81 va_start(args, format);
82 vfprintf(stderr, format, args);
84 fprintf(stderr, ": %s\n", isc_result_totext(result));
92 client = isc_mempool_get(cmp);
93 INSIST(client != NULL);
94 dns_name_init(&client->name, NULL);
95 ISC_LINK_INIT(client, link);
102 free_client(client_t **c) {
108 INSIST(client != NULL);
109 dns_name_free(&client->name, mctx);
110 INSIST(!ISC_LINK_LINKED(client, link));
111 INSIST(client->find == NULL);
113 isc_mempool_put(cmp, client);
118 RUNTIME_CHECK(isc_mutex_lock(&client_lock) == ISC_R_SUCCESS);
123 RUNTIME_CHECK(isc_mutex_unlock(&client_lock) == ISC_R_SUCCESS);
127 lookup_callback(isc_task_t *task, isc_event_t *ev) {
131 INSIST(client->find == ev->ev_sender);
133 printf("NAME %s:\n\tTask %p got event %p type %08x from %p, client %p\n\terr4: %s err6: %s\n",
135 task, ev, ev->ev_type, client->find, client,
136 isc_result_totext(client->find->result_v4),
137 isc_result_totext(client->find->result_v6));
144 dns_adb_dumpfind(client->find, stderr);
145 dns_adb_destroyfind(&client->find);
147 ISC_LIST_UNLINK(clients, client, link);
148 free_client(&client);
154 create_managers(void) {
158 result = isc_taskmgr_create(mctx, 5, 0, &taskmgr);
159 check_result(result, "isc_taskmgr_create");
162 result = isc_timermgr_create(mctx, &timermgr);
163 check_result(result, "isc_timermgr_create");
166 result = isc_socketmgr_create(mctx, &socketmgr);
167 check_result(result, "isc_socketmgr_create");
170 result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
171 check_result(result, "dns_dispatchmgr_create");
183 result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
184 check_result(result, "dns_view_create");
190 result = dns_cache_create(mctx, taskmgr, timermgr, dns_rdataclass_in,
191 "rbt", 0, NULL, &cache);
192 check_result(result, "dns_cache_create");
193 dns_view_setcache(view, cache);
194 dns_cache_detach(&cache);
198 isc_sockaddr_t any4, any6;
199 dns_dispatch_t *disp4 = NULL;
200 dns_dispatch_t *disp6 = NULL;
202 isc_sockaddr_any(&any4);
203 isc_sockaddr_any6(&any6);
205 attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP;
206 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
207 taskmgr, &any4, 512, 6, 1024,
208 17, 19, attrs, attrs, &disp4)
210 INSIST(disp4 != NULL);
212 attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
213 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
214 taskmgr, &any6, 512, 6, 1024,
215 17, 19, attrs, attrs, &disp6)
217 INSIST(disp6 != NULL);
219 RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10,
228 result = dns_rootns_create(mctx, dns_rdataclass_in, NULL, &rootdb);
229 check_result(result, "dns_rootns_create()");
230 dns_view_sethints(view, rootdb);
231 dns_db_detach(&rootdb);
233 dns_view_freeze(view);
237 lookup(const char *target) {
239 unsigned char namedata[256];
241 isc_buffer_t t, namebuf;
243 unsigned int options;
245 INSIST(target != NULL);
247 client = new_client();
248 isc_buffer_init(&t, target, strlen(target));
249 isc_buffer_add(&t, strlen(target));
250 isc_buffer_init(&namebuf, namedata, sizeof(namedata));
251 dns_name_init(&name, NULL);
252 result = dns_name_fromtext(&name, &t, dns_rootname, ISC_FALSE,
254 check_result(result, "dns_name_fromtext %s", target);
256 result = dns_name_dup(&name, mctx, &client->name);
257 check_result(result, "dns_name_dup %s", target);
260 options |= DNS_ADBFIND_INET;
261 options |= DNS_ADBFIND_INET6;
262 options |= DNS_ADBFIND_WANTEVENT;
263 options |= DNS_ADBFIND_HINTOK;
264 options |= DNS_ADBFIND_GLUEOK;
265 result = dns_adb_createfind(adb, t2, lookup_callback, client,
266 &client->name, dns_rootname, 0, options,
267 now, NULL, view->dstport, &client->find);
269 check_result(result, "dns_adb_createfind()");
271 dns_adb_dumpfind(client->find, stderr);
273 if ((client->find->options & DNS_ADBFIND_WANTEVENT) != 0) {
274 client->target = target;
275 ISC_LIST_APPEND(clients, client, link);
277 printf("NAME %s: err4 %s, err6 %s\n",
278 target, isc_result_totext(client->find->result_v4),
279 isc_result_totext(client->find->result_v6));
281 dns_adb_destroyfind(&client->find);
282 free_client(&client);
287 main(int argc, char **argv) {
289 isc_logdestination_t destination;
294 dns_result_register();
295 result = isc_app_start();
296 check_result(result, "isc_app_start()");
298 isc_stdtime_get(&now);
300 result = isc_mutex_init(&client_lock);
301 check_result(result, "isc_mutex_init(&client_lock)");
302 ISC_LIST_INIT(clients);
305 * EVERYTHING needs a memory context.
307 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
310 RUNTIME_CHECK(isc_mempool_create(mctx, sizeof(client_t), &cmp)
312 isc_mempool_setname(cmp, "adb test clients");
314 result = isc_entropy_create(mctx, &ectx);
315 check_result(result, "isc_entropy_create()");
316 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
317 check_result(result, "isc_hash_create()");
319 result = isc_log_create(mctx, &lctx, &lcfg);
320 check_result(result, "isc_log_create()");
321 isc_log_setcontext(lctx);
323 dns_log_setcontext(lctx);
326 * Create and install the default channel.
328 destination.file.stream = stderr;
329 destination.file.name = NULL;
330 destination.file.versions = ISC_LOG_ROLLNEVER;
331 destination.file.maximum_size = 0;
332 result = isc_log_createchannel(lcfg, "_default",
335 &destination, ISC_LOG_PRINTTIME);
336 check_result(result, "isc_log_createchannel()");
337 result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
338 check_result(result, "isc_log_usechannel()");
341 * Set the initial debug level.
343 isc_log_setdebuglevel(lctx, 2);
348 result = isc_task_create(taskmgr, 0, &t1);
349 check_result(result, "isc_task_create t1");
351 result = isc_task_create(taskmgr, 0, &t2);
352 check_result(result, "isc_task_create t2");
354 printf("task 1 = %p\n", t1);
355 printf("task 2 = %p\n", t2);
362 * Lock the entire client list here. This will cause all events
363 * for found names to block as well.
366 lookup("f.root-servers.net."); /* Should be in hints */
367 lookup("www.iengines.com"); /* should fetch */
368 lookup("www.isc.org"); /* should fetch */
369 lookup("www.flame.org"); /* should fetch */
370 lookup("kechara.flame.org."); /* should fetch */
371 lookup("moghedien.flame.org."); /* should fetch */
372 lookup("mailrelay.flame.org."); /* should fetch */
373 lookup("ipv4v6.flame.org."); /* should fetch */
374 lookup("nonexistant.flame.org."); /* should fail to be found */
375 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
376 lookup("i.root-servers.net."); /* Should be in hints */
377 lookup("www.firstcard.com.");
378 lookup("dns04.flame.org.");
383 dns_adb_dump(adb, stderr);
388 lookup("f.root-servers.net."); /* Should be in hints */
389 lookup("www.iengines.com"); /* should fetch */
390 lookup("www.isc.org"); /* should fetch */
391 lookup("www.flame.org"); /* should fetch */
392 lookup("kechara.flame.org."); /* should fetch */
393 lookup("moghedien.flame.org."); /* should fetch */
394 lookup("mailrelay.flame.org."); /* should fetch */
395 lookup("ipv4v6.flame.org."); /* should fetch */
396 lookup("nonexistant.flame.org."); /* should fail to be found */
397 lookup("foobar.badns.flame.org."); /* should fail utterly (NS) */
398 lookup("i.root-servers.net."); /* Should be in hints */
403 dns_adb_dump(adb, stderr);
405 isc_task_detach(&t1);
406 isc_task_detach(&t2);
408 isc_mem_stats(mctx, stdout);
409 dns_adb_dump(adb, stderr);
413 dns_adb_dump(adb, stderr);
415 dns_view_detach(&view);
418 isc_socketmgr_destroy(&socketmgr);
419 isc_timermgr_destroy(&timermgr);
421 fprintf(stderr, "Destroying task manager\n");
422 isc_taskmgr_destroy(&taskmgr);
424 isc_log_destroy(&lctx);
427 isc_entropy_detach(&ectx);
429 isc_mempool_destroy(&cmp);
430 isc_mem_stats(mctx, stdout);
431 isc_mem_destroy(&mctx);