1 /* $NetBSD: adb_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20 /* Id: adb_test.c,v 1.73 2011/08/30 23:46:51 tbox Exp */
21
22 /*! \file */
23
24 #include <config.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
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>
38
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>
46
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;
53 };
54
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;
71
72 static void
73 check_result(isc_result_t result, const char *format, ...)
74 ISC_FORMAT_PRINTF(2, 3);
75
76 static void
check_result(isc_result_t result,const char * format,...)77 check_result(isc_result_t result, const char *format, ...) {
78 va_list args;
79
80 if (result == ISC_R_SUCCESS)
81 return;
82
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);
88 }
89
90 static client_t *
new_client(void)91 new_client(void) {
92 client_t *client;
93
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;
99
100 return (client);
101 }
102
103 static void
free_client(client_t ** c)104 free_client(client_t **c) {
105 client_t *client;
106
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);
114
115 isc_mempool_put(cmp, client);
116 }
117
118 static inline void
CLOCK(void)119 CLOCK(void) {
120 RUNTIME_CHECK(isc_mutex_lock(&client_lock) == ISC_R_SUCCESS);
121 }
122
123 static inline void
CUNLOCK(void)124 CUNLOCK(void) {
125 RUNTIME_CHECK(isc_mutex_unlock(&client_lock) == ISC_R_SUCCESS);
126 }
127
128 static void
lookup_callback(isc_task_t * task,isc_event_t * ev)129 lookup_callback(isc_task_t *task, isc_event_t *ev) {
130 client_t *client;
131
132 client = ev->ev_arg;
133 INSIST(client->find == ev->ev_sender);
134
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));
140
141 isc_event_free(&ev);
142 ev = NULL;
143
144 CLOCK();
145
146 dns_adb_dumpfind(client->find, stderr);
147 dns_adb_destroyfind(&client->find);
148
149 ISC_LIST_UNLINK(clients, client, link);
150 free_client(&client);
151
152 CUNLOCK();
153 }
154
155 static void
create_managers(void)156 create_managers(void) {
157 isc_result_t result;
158
159 taskmgr = NULL;
160 result = isc_taskmgr_create(mctx, 5, 0, &taskmgr);
161 check_result(result, "isc_taskmgr_create");
162
163 timermgr = NULL;
164 result = isc_timermgr_create(mctx, &timermgr);
165 check_result(result, "isc_timermgr_create");
166
167 socketmgr = NULL;
168 result = isc_socketmgr_create(mctx, &socketmgr);
169 check_result(result, "isc_socketmgr_create");
170
171 dispatchmgr = NULL;
172 result = dns_dispatchmgr_create(mctx, NULL, &dispatchmgr);
173 check_result(result, "dns_dispatchmgr_create");
174 }
175
176 static void
create_view(void)177 create_view(void) {
178 dns_cache_t *cache;
179 isc_result_t result;
180
181 /*
182 * View.
183 */
184 view = NULL;
185 result = dns_view_create(mctx, dns_rdataclass_in, "_default", &view);
186 check_result(result, "dns_view_create");
187
188 /*
189 * Cache.
190 */
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);
197
198 {
199 unsigned int attrs;
200 isc_sockaddr_t any4, any6;
201 dns_dispatch_t *disp4 = NULL;
202 dns_dispatch_t *disp6 = NULL;
203
204 isc_sockaddr_any(&any4);
205 isc_sockaddr_any6(&any6);
206
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);
214
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);
222
223 RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
224 socketmgr,
225 timermgr, 0,
226 dispatchmgr,
227 disp4, disp6) ==
228 ISC_R_SUCCESS);
229 }
230
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);
236
237 dns_view_freeze(view);
238 }
239
240 static void
lookup(const char * target)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;
248
249 INSIST(target != NULL);
250
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);
258
259 result = dns_name_dup(&name, mctx, &client->name);
260 check_result(result, "dns_name_dup %s", target);
261
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);
274
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));
282
283 dns_adb_destroyfind(&client->find);
284 free_client(&client);
285 }
286 }
287
288 int
main(int argc,char ** argv)289 main(int argc, char **argv) {
290 isc_result_t result;
291 isc_logdestination_t destination;
292
293 UNUSED(argc);
294 UNUSED(argv);
295
296 dns_result_register();
297 result = isc_app_start();
298 check_result(result, "isc_app_start()");
299
300 isc_stdtime_get(&now);
301
302 result = isc_mutex_init(&client_lock);
303 check_result(result, "isc_mutex_init(&client_lock)");
304 ISC_LIST_INIT(clients);
305
306 /*
307 * EVERYTHING needs a memory context.
308 */
309 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
310
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");
315
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()");
320
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);
326
327 /*
328 * Create and install the default channel.
329 */
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()");
341
342 /*
343 * Set the initial debug level.
344 */
345 isc_log_setdebuglevel(lctx, 2);
346
347 create_managers();
348
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");
355
356 printf("task 1 = %p\n", t1);
357 printf("task 2 = %p\n", t2);
358
359 create_view();
360
361 adb = view->adb;
362
363 /*
364 * Lock the entire client list here. This will cause all events
365 * for found names to block as well.
366 */
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();
382
383 sleep(10);
384
385 dns_adb_dump(adb, stderr);
386
387 sleep(10);
388
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();
402
403 sleep(20);
404
405 dns_adb_dump(adb, stderr);
406
407 isc_task_detach(&t1);
408 isc_task_detach(&t2);
409
410 isc_mem_stats(mctx, stdout);
411 dns_adb_dump(adb, stderr);
412
413 isc_app_run();
414
415 dns_adb_dump(adb, stderr);
416
417 dns_view_detach(&view);
418 adb = NULL;
419
420 fprintf(stderr, "Destroying socket manager\n");
421 isc_socketmgr_destroy(&socketmgr);
422 fprintf(stderr, "Destroying timer manager\n");
423 isc_timermgr_destroy(&timermgr);
424
425 fprintf(stderr, "Destroying task manager\n");
426 isc_taskmgr_destroy(&taskmgr);
427
428 isc_log_destroy(&lctx);
429
430 isc_hash_destroy();
431 isc_entropy_detach(&ectx);
432
433 isc_mempool_destroy(&cmp);
434 isc_mem_stats(mctx, stdout);
435 isc_mem_destroy(&mctx);
436
437 isc_app_finish();
438
439 return (0);
440 }
441