No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / adb_test.c
blob31c09b0f1439b5b5eeb7a758dcf37095e152e9c0
1 /* $NetBSD$ */
3 /*
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 */
22 /*! \file */
24 #include <config.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <isc/app.h>
31 #include <isc/buffer.h>
32 #include <isc/entropy.h>
33 #include <isc/hash.h>
34 #include <isc/socket.h>
35 #include <isc/task.h>
36 #include <isc/timer.h>
37 #include <isc/util.h>
39 #include <dns/adb.h>
40 #include <dns/cache.h>
41 #include <dns/dispatch.h>
42 #include <dns/db.h>
43 #include <dns/log.h>
44 #include <dns/rootns.h>
45 #include <dns/result.h>
47 typedef struct client client_t;
48 struct client {
49 dns_name_t name;
50 const char *target;
51 ISC_LINK(client_t) link;
52 dns_adbfind_t *find;
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;
72 static void
73 check_result(isc_result_t result, const char *format, ...)
74 ISC_FORMAT_PRINTF(2, 3);
76 static void
77 check_result(isc_result_t result, const char *format, ...) {
78 va_list args;
80 if (result == ISC_R_SUCCESS)
81 return;
83 va_start(args, format);
84 vfprintf(stderr, format, args);
85 va_end(args);
86 fprintf(stderr, ": %s\n", isc_result_totext(result));
87 exit(1);
90 static client_t *
91 new_client(void) {
92 client_t *client;
94 client = isc_mempool_get(cmp);
95 INSIST(client != NULL);
96 dns_name_init(&client->name, NULL);
97 ISC_LINK_INIT(client, link);
98 client->find = NULL;
100 return (client);
103 static void
104 free_client(client_t **c) {
105 client_t *client;
107 INSIST(c != NULL);
108 client = *c;
109 *c = NULL;
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);
118 static inline void
119 CLOCK(void) {
120 RUNTIME_CHECK(isc_mutex_lock(&client_lock) == ISC_R_SUCCESS);
123 static inline void
124 CUNLOCK(void) {
125 RUNTIME_CHECK(isc_mutex_unlock(&client_lock) == ISC_R_SUCCESS);
128 static void
129 lookup_callback(isc_task_t *task, isc_event_t *ev) {
130 client_t *client;
132 client = ev->ev_arg;
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",
136 client->target,
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));
141 isc_event_free(&ev);
142 ev = NULL;
144 CLOCK();
146 dns_adb_dumpfind(client->find, stderr);
147 dns_adb_destroyfind(&client->find);
149 ISC_LIST_UNLINK(clients, client, link);
150 free_client(&client);
152 CUNLOCK();
155 static void
156 create_managers(void) {
157 isc_result_t result;
159 taskmgr = NULL;
160 result = isc_taskmgr_create(mctx, 5, 0, &taskmgr);
161 check_result(result, "isc_taskmgr_create");
163 timermgr = NULL;
164 result = isc_timermgr_create(mctx, &timermgr);
165 check_result(result, "isc_timermgr_create");
167 socketmgr = NULL;
168 result = isc_socketmgr_create(mctx, &socketmgr);
169 check_result(result, "isc_socketmgr_create");
171 dispatchmgr = NULL;
172 result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
173 check_result(result, "dns_dispatchmgr_create");
176 static void
177 create_view(void) {
178 dns_cache_t *cache;
179 isc_result_t result;
182 * View.
184 view = NULL;
185 result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
186 check_result(result, "dns_view_create");
189 * Cache.
191 cache = NULL;
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);
199 unsigned int attrs;
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)
211 == ISC_R_SUCCESS);
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)
218 == ISC_R_SUCCESS);
219 INSIST(disp6 != NULL);
221 RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10,
222 socketmgr,
223 timermgr, 0,
224 dispatchmgr,
225 disp4, disp6) ==
226 ISC_R_SUCCESS);
229 rootdb = NULL;
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);
238 static void
239 lookup(const char *target) {
240 dns_name_t name;
241 unsigned char namedata[256];
242 client_t *client;
243 isc_buffer_t t, namebuf;
244 isc_result_t result;
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);
260 options = 0;
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);
269 #if 0
270 check_result(result, "dns_adb_createfind()");
271 #endif
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);
277 } else {
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) {
289 isc_result_t result;
290 isc_logdestination_t destination;
292 UNUSED(argc);
293 UNUSED(argv);
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);
310 cmp = NULL;
311 RUNTIME_CHECK(isc_mempool_create(mctx, sizeof(client_t), &cmp)
312 == ISC_R_SUCCESS);
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);
323 dns_log_init(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",
334 ISC_LOG_TOFILEDESC,
335 ISC_LOG_DYNAMIC,
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);
346 create_managers();
348 t1 = NULL;
349 result = isc_task_create(taskmgr, 0, &t1);
350 check_result(result, "isc_task_create t1");
351 t2 = NULL;
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);
358 create_view();
360 adb = view->adb;
363 * Lock the entire client list here. This will cause all events
364 * for found names to block as well.
366 CLOCK();
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.");
380 CUNLOCK();
382 sleep(10);
384 dns_adb_dump(adb, stderr);
386 sleep(10);
388 CLOCK();
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 */
400 CUNLOCK();
402 sleep(20);
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);
412 isc_app_run();
414 dns_adb_dump(adb, stderr);
416 dns_view_detach(&view);
417 adb = NULL;
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);
427 isc_hash_destroy();
428 isc_entropy_detach(&ectx);
430 isc_mempool_destroy(&cmp);
431 isc_mem_stats(mctx, stdout);
432 isc_mem_destroy(&mctx);
434 isc_app_finish();
436 return (0);