1 /* $NetBSD: byaddr_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2012 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2002 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: byaddr_test.c,v 1.28 2007/06/19 23:46:59 tbox Exp */
24 * Principal Author: Bob Halley
32 #include <isc/commandline.h>
34 #include <isc/netaddr.h>
36 #include <isc/timer.h>
39 #include <dns/byaddr.h>
40 #include <dns/cache.h>
41 #include <dns/dispatch.h>
42 #include <dns/events.h>
43 #include <dns/forward.h>
44 #include <dns/resolver.h>
45 #include <dns/result.h>
49 done(isc_task_t
*task
, isc_event_t
*event
) {
50 dns_byaddrevent_t
*bevent
;
54 REQUIRE(event
->ev_type
== DNS_EVENT_BYADDRDONE
);
55 bevent
= (dns_byaddrevent_t
*)event
;
59 printf("byaddr event result = %s\n",
60 isc_result_totext(bevent
->result
));
62 if (bevent
->result
== ISC_R_SUCCESS
) {
63 for (name
= ISC_LIST_HEAD(bevent
->names
);
65 name
= ISC_LIST_NEXT(name
, link
)) {
66 char text
[DNS_NAME_FORMATSIZE
];
67 dns_name_format(name
, text
, sizeof(text
));
72 byaddr
= event
->ev_sender
;
73 dns_byaddr_destroy(&byaddr
);
74 isc_event_free(&event
);
80 main(int argc
, char *argv
[]) {
82 isc_boolean_t verbose
= ISC_FALSE
;
83 unsigned int workers
= 2;
84 isc_taskmgr_t
*taskmgr
;
86 isc_timermgr_t
*timermgr
;
89 isc_socketmgr_t
*socketmgr
;
90 dns_dispatchmgr_t
*dispatchmgr
;
94 unsigned int options
= 0;
97 RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS
);
99 dns_result_register();
102 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
104 while ((ch
= isc_commandline_parse(argc
, argv
, "nvw:")) != -1) {
108 * We only try nibbles, so do nothing for this option.
115 workers
= (unsigned int)atoi(isc_commandline_argument
);
121 printf("%u workers\n", workers
);
122 printf("IPv4: %s\n", isc_result_totext(isc_net_probeipv4()));
123 printf("IPv6: %s\n", isc_result_totext(isc_net_probeipv6()));
127 RUNTIME_CHECK(isc_taskmgr_create(mctx
, workers
, 0, &taskmgr
)
130 RUNTIME_CHECK(isc_task_create(taskmgr
, 0, &task
)
132 isc_task_setname(task
, "byaddr", NULL
);
135 RUNTIME_CHECK(dns_dispatchmgr_create(mctx
, NULL
, &dispatchmgr
)
139 RUNTIME_CHECK(isc_timermgr_create(mctx
, &timermgr
) == ISC_R_SUCCESS
);
141 RUNTIME_CHECK(isc_socketmgr_create(mctx
, &socketmgr
) == ISC_R_SUCCESS
);
144 RUNTIME_CHECK(dns_cache_create(mctx
, taskmgr
, timermgr
,
145 dns_rdataclass_in
, "rbt", 0, NULL
,
146 &cache
) == ISC_R_SUCCESS
);
149 RUNTIME_CHECK(dns_view_create(mctx
, dns_rdataclass_in
, "default",
150 &view
) == ISC_R_SUCCESS
);
154 dns_dispatch_t
*disp4
= NULL
;
155 dns_dispatch_t
*disp6
= NULL
;
157 if (isc_net_probeipv4() == ISC_R_SUCCESS
) {
160 isc_sockaddr_any(&any4
);
162 attrs
= DNS_DISPATCHATTR_IPV4
| DNS_DISPATCHATTR_UDP
;
163 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
,
170 INSIST(disp4
!= NULL
);
173 if (isc_net_probeipv6() == ISC_R_SUCCESS
) {
176 isc_sockaddr_any6(&any6
);
178 attrs
= DNS_DISPATCHATTR_IPV6
| DNS_DISPATCHATTR_UDP
;
179 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr
,
186 INSIST(disp6
!= NULL
);
189 RUNTIME_CHECK(dns_view_createresolver(view
, taskmgr
, 10, 1,
197 dns_dispatch_detach(&disp4
);
199 dns_dispatch_detach(&disp6
);
205 isc_sockaddrlist_t sal
;
208 ina
.s_addr
= inet_addr("127.0.0.1");
209 isc_sockaddr_fromin(&sa
, &ina
, 53);
210 ISC_LIST_APPEND(sal
, &sa
, link
);
212 RUNTIME_CHECK(dns_fwdtable_add(view
->fwdtable
, dns_rootname
,
213 &sal
, dns_fwdpolicy_only
)
217 dns_view_setcache(view
, cache
);
218 dns_view_freeze(view
);
220 dns_cache_detach(&cache
);
222 printf("address = %s\n", argv
[isc_commandline_index
]);
224 if (inet_pton(AF_INET
, argv
[isc_commandline_index
],
225 (char *)&na
.type
.in
) != 1) {
226 na
.family
= AF_INET6
;
227 if (inet_pton(AF_INET6
, argv
[isc_commandline_index
],
228 (char *)&na
.type
.in6
) != 1) {
229 printf("unknown address format\n");
234 result
= dns_byaddr_create(mctx
, &na
, view
, options
, task
,
235 done
, NULL
, &byaddr
);
236 if (result
!= ISC_R_SUCCESS
) {
237 printf("dns_byaddr_create() returned %s\n",
238 isc_result_totext(result
));
245 * XXXRTH if we get a control-C before we get to isc_app_run(),
246 * we're in trouble (because we might try to destroy things before
247 * they've been created.
250 dns_view_detach(&view
);
252 isc_task_shutdown(task
);
253 isc_task_detach(&task
);
255 dns_dispatchmgr_destroy(&dispatchmgr
);
257 isc_taskmgr_destroy(&taskmgr
);
259 isc_socketmgr_destroy(&socketmgr
);
260 isc_timermgr_destroy(&timermgr
);
263 isc_mem_stats(mctx
, stdout
);
264 isc_mem_destroy(&mctx
);