etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / adb_test.c
blob00872aac07d6fca23dd4f689624475c75dfd042c
1 /* $NetBSD: adb_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
3 /*
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 */
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,
210 512, 6, 1024, 17, 19,
211 attrs, attrs, &disp4)
212 == ISC_R_SUCCESS);
213 INSIST(disp4 != NULL);
215 attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
216 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr, socketmgr,
217 taskmgr, &any6,
218 512, 6, 1024, 17, 19,
219 attrs, attrs, &disp6)
220 == ISC_R_SUCCESS);
221 INSIST(disp6 != NULL);
223 RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
224 socketmgr,
225 timermgr, 0,
226 dispatchmgr,
227 disp4, disp6) ==
228 ISC_R_SUCCESS);
231 rootdb = NULL;
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);
240 static void
241 lookup(const char *target) {
242 dns_name_t name;
243 unsigned char namedata[256];
244 client_t *client;
245 isc_buffer_t t, namebuf;
246 isc_result_t result;
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);
262 options = 0;
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);
278 } else {
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) {
290 isc_result_t result;
291 isc_logdestination_t destination;
293 UNUSED(argc);
294 UNUSED(argv);
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);
311 cmp = NULL;
312 RUNTIME_CHECK(isc_mempool_create(mctx, sizeof(client_t), &cmp)
313 == ISC_R_SUCCESS);
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);
324 dns_log_init(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",
335 ISC_LOG_TOFILEDESC,
336 ISC_LOG_DYNAMIC,
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);
347 create_managers();
349 t1 = NULL;
350 result = isc_task_create(taskmgr, 0, &t1);
351 check_result(result, "isc_task_create t1");
352 t2 = NULL;
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);
359 create_view();
361 adb = view->adb;
364 * Lock the entire client list here. This will cause all events
365 * for found names to block as well.
367 CLOCK();
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.");
381 CUNLOCK();
383 sleep(10);
385 dns_adb_dump(adb, stderr);
387 sleep(10);
389 CLOCK();
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 */
401 CUNLOCK();
403 sleep(20);
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);
413 isc_app_run();
415 dns_adb_dump(adb, stderr);
417 dns_view_detach(&view);
418 adb = NULL;
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);
430 isc_hash_destroy();
431 isc_entropy_detach(&ectx);
433 isc_mempool_destroy(&cmp);
434 isc_mem_stats(mctx, stdout);
435 isc_mem_destroy(&mctx);
437 isc_app_finish();
439 return (0);