1 /* $NetBSD: byname_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 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: byname_test.c,v 1.33 2009/09/02 23:48:01 tbox Exp */
24 * Principal Author: Bob Halley
33 #include <isc/commandline.h>
34 #include <isc/entropy.h>
36 #include <isc/netaddr.h>
38 #include <isc/timer.h>
42 #include <dns/cache.h>
43 #include <dns/dispatch.h>
44 #include <dns/events.h>
45 #include <dns/forward.h>
47 #include <dns/resolver.h>
48 #include <dns/result.h>
50 static isc_mem_t
*mctx
= NULL
;
51 static isc_entropy_t
*ectx
= NULL
;
52 static isc_taskmgr_t
*taskmgr
;
53 static dns_view_t
*view
= NULL
;
54 static dns_adbfind_t
*find
= NULL
;
55 static isc_task_t
*task
= NULL
;
56 static dns_fixedname_t name
;
57 static dns_fixedname_t target
;
58 static isc_log_t
*lctx
;
59 static isc_logconfig_t
*lcfg
;
60 static unsigned int level
= 0;
62 static void adb_callback(isc_task_t
*task
, isc_event_t
*event
);
66 isc_logdestination_t destination
;
70 * Setup a logging context.
72 RUNTIME_CHECK(isc_log_create(mctx
, &lctx
, &lcfg
) == ISC_R_SUCCESS
);
73 isc_log_setcontext(lctx
);
75 dns_log_setcontext(lctx
);
78 * Create and install the default channel.
80 destination
.file
.stream
= stderr
;
81 destination
.file
.name
= NULL
;
82 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
83 destination
.file
.maximum_size
= 0;
84 flags
= ISC_LOG_PRINTTIME
;
85 RUNTIME_CHECK(isc_log_createchannel(lcfg
, "_default",
88 &destination
, flags
) ==
90 RUNTIME_CHECK(isc_log_usechannel(lcfg
, "_default", NULL
, NULL
) ==
92 isc_log_setdebuglevel(lctx
, level
);
96 print_addresses(dns_adbfind_t
*find
) {
97 dns_adbaddrinfo_t
*address
;
99 for (address
= ISC_LIST_HEAD(find
->list
);
101 address
= ISC_LIST_NEXT(address
, publink
)) {
102 isc_netaddr_t netaddr
;
103 char text
[ISC_NETADDR_FORMATSIZE
];
104 isc_netaddr_fromsockaddr(&netaddr
, &address
->sockaddr
);
105 isc_netaddr_format(&netaddr
, text
, sizeof(text
));
106 printf("%s\n", text
);
111 print_name(dns_name_t
*name
) {
112 char text
[DNS_NAME_FORMATSIZE
];
114 dns_name_format(name
, text
, sizeof(text
));
115 printf("%s\n", text
);
119 do_find(isc_boolean_t want_event
) {
121 isc_boolean_t done
= ISC_FALSE
;
122 unsigned int options
;
124 options
= DNS_ADBFIND_INET
| DNS_ADBFIND_INET6
;
126 options
|= DNS_ADBFIND_WANTEVENT
| DNS_ADBFIND_EMPTYEVENT
;
127 dns_fixedname_init(&target
);
128 result
= dns_adb_createfind(view
->adb
, task
, adb_callback
, NULL
,
129 dns_fixedname_name(&name
),
130 dns_rootname
, 0, options
, 0,
131 dns_fixedname_name(&target
), 0,
133 if (result
== ISC_R_SUCCESS
) {
134 if (!ISC_LIST_EMPTY(find
->list
)) {
136 * We have at least some of the addresses for the
139 INSIST((find
->options
& DNS_ADBFIND_WANTEVENT
) == 0);
140 print_addresses(find
);
144 * We don't know any of the addresses for this
147 if ((find
->options
& DNS_ADBFIND_WANTEVENT
) == 0) {
149 * And ADB isn't going to send us any events
150 * either. This query loses.
155 * If the DNS_ADBFIND_WANTEVENT flag was set, we'll
156 * get an event when something happens.
159 } else if (result
== DNS_R_ALIAS
) {
160 print_name(dns_fixedname_name(&target
));
163 printf("dns_adb_createfind() returned %s\n",
164 isc_result_totext(result
));
170 dns_adb_destroyfind(&find
);
176 adb_callback(isc_task_t
*etask
, isc_event_t
*event
) {
177 unsigned int type
= event
->ev_type
;
179 REQUIRE(etask
== task
);
181 isc_event_free(&event
);
182 dns_adb_destroyfind(&find
);
184 if (type
== DNS_EVENT_ADBMOREADDRESSES
)
186 else if (type
== DNS_EVENT_ADBNOMOREADDRESSES
) {
187 printf("no more addresses\n");
190 printf("unexpected ADB event type %u\n", type
);
196 run(isc_task_t
*task
, isc_event_t
*event
) {
199 isc_event_free(&event
);
203 main(int argc
, char *argv
[]) {
204 isc_boolean_t verbose
= ISC_FALSE
;
205 unsigned int workers
= 2;
206 isc_timermgr_t
*timermgr
;
208 isc_socketmgr_t
*socketmgr
;
209 dns_dispatchmgr_t
*dispatchmgr
;
213 RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS
);
215 dns_result_register();
218 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
220 RUNTIME_CHECK(isc_entropy_create(mctx
, &ectx
) == ISC_R_SUCCESS
);
221 RUNTIME_CHECK(isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
)
224 while ((ch
= isc_commandline_parse(argc
, argv
, "d:vw:")) != -1) {
227 level
= (unsigned int)atoi(isc_commandline_argument
);
233 workers
= (unsigned int)atoi(isc_commandline_argument
);
241 printf("%u workers\n", workers
);
242 printf("IPv4: %s\n", isc_result_totext(isc_net_probeipv4()));
243 printf("IPv6: %s\n", isc_result_totext(isc_net_probeipv6()));
247 RUNTIME_CHECK(isc_taskmgr_create(mctx
, workers
, 0, &taskmgr
) ==
250 RUNTIME_CHECK(isc_task_create(taskmgr
, 0, &task
) ==
252 isc_task_setname(task
, "byname", NULL
);
255 RUNTIME_CHECK(dns_dispatchmgr_create(mctx
, NULL
, &dispatchmgr
)
259 RUNTIME_CHECK(isc_timermgr_create(mctx
, &timermgr
) == ISC_R_SUCCESS
);
261 RUNTIME_CHECK(isc_socketmgr_create(mctx
, &socketmgr
) == ISC_R_SUCCESS
);
264 RUNTIME_CHECK(dns_cache_create(mctx
, taskmgr
, timermgr
,
265 dns_rdataclass_in
, "rbt", 0, NULL
,
266 &cache
) == ISC_R_SUCCESS
);
269 RUNTIME_CHECK(dns_view_create(mctx
, dns_rdataclass_in
, "default",
270 &view
) == ISC_R_SUCCESS
);
274 dns_dispatch_t
*disp4
= NULL
;
275 dns_dispatch_t
*disp6
= NULL
;
277 if (isc_net_probeipv4() == ISC_R_SUCCESS
) {
279 isc_sockaddr_any(&any4
);
281 attrs
= DNS_DISPATCHATTR_IPV4
| DNS_DISPATCHATTR_UDP
;
282 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
,
289 INSIST(disp4
!= NULL
);
292 if (isc_net_probeipv6() == ISC_R_SUCCESS
) {
295 isc_sockaddr_any6(&any6
);
297 attrs
= DNS_DISPATCHATTR_IPV6
| DNS_DISPATCHATTR_UDP
;
298 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
,
305 INSIST(disp6
!= NULL
);
308 RUNTIME_CHECK(dns_view_createresolver(view
, taskmgr
, 10, 1,
316 dns_dispatch_detach(&disp4
);
318 dns_dispatch_detach(&disp6
);
324 isc_sockaddrlist_t sal
;
327 ina
.s_addr
= inet_addr("127.0.0.1");
328 isc_sockaddr_fromin(&sa
, &ina
, 53);
329 ISC_LIST_APPEND(sal
, &sa
, link
);
331 RUNTIME_CHECK(dns_fwdtable_add(view
->fwdtable
, dns_rootname
,
332 &sal
, dns_fwdpolicy_only
)
336 dns_view_setcache(view
, cache
);
337 dns_view_freeze(view
);
339 dns_cache_detach(&cache
);
341 printf("name = %s\n", argv
[isc_commandline_index
]);
342 isc_buffer_init(&b
, argv
[isc_commandline_index
],
343 strlen(argv
[isc_commandline_index
]));
344 isc_buffer_add(&b
, strlen(argv
[isc_commandline_index
]));
345 dns_fixedname_init(&name
);
346 dns_fixedname_init(&target
);
347 RUNTIME_CHECK(dns_name_fromtext(dns_fixedname_name(&name
), &b
,
348 dns_rootname
, 0, NULL
) ==
351 RUNTIME_CHECK(isc_app_onrun(mctx
, task
, run
, NULL
) == ISC_R_SUCCESS
);
355 dns_view_detach(&view
);
356 isc_task_shutdown(task
);
357 isc_task_detach(&task
);
359 dns_dispatchmgr_destroy(&dispatchmgr
);
361 isc_taskmgr_destroy(&taskmgr
);
363 isc_socketmgr_destroy(&socketmgr
);
364 isc_timermgr_destroy(&timermgr
);
366 isc_log_destroy(&lctx
);
369 isc_entropy_detach(&ectx
);
372 isc_mem_stats(mctx
, stdout
);
373 isc_mem_destroy(&mctx
);