etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / byaddr_test.c
blob027a916f9d5c8407ed108056da3f9492384d60df
1 /* $NetBSD: byaddr_test.c,v 1.7 2014/12/10 04:37:53 christos Exp $ */
3 /*
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 */
22 /*! \file
23 * \author
24 * Principal Author: Bob Halley
27 #include <config.h>
29 #include <stdlib.h>
31 #include <isc/app.h>
32 #include <isc/commandline.h>
33 #include <isc/mem.h>
34 #include <isc/netaddr.h>
35 #include <isc/task.h>
36 #include <isc/timer.h>
37 #include <isc/util.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>
46 #include <dns/view.h>
48 static void
49 done(isc_task_t *task, isc_event_t *event) {
50 dns_byaddrevent_t *bevent;
51 dns_byaddr_t *byaddr;
52 dns_name_t *name;
54 REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
55 bevent = (dns_byaddrevent_t *)event;
57 UNUSED(task);
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);
64 name != NULL;
65 name = ISC_LIST_NEXT(name, link)) {
66 char text[DNS_NAME_FORMATSIZE];
67 dns_name_format(name, text, sizeof(text));
68 printf("%s\n", text);
72 byaddr = event->ev_sender;
73 dns_byaddr_destroy(&byaddr);
74 isc_event_free(&event);
76 isc_app_shutdown();
79 int
80 main(int argc, char *argv[]) {
81 isc_mem_t *mctx;
82 isc_boolean_t verbose = ISC_FALSE;
83 unsigned int workers = 2;
84 isc_taskmgr_t *taskmgr;
85 isc_task_t *task;
86 isc_timermgr_t *timermgr;
87 dns_view_t *view;
88 int ch;
89 isc_socketmgr_t *socketmgr;
90 dns_dispatchmgr_t *dispatchmgr;
91 isc_netaddr_t na;
92 dns_byaddr_t *byaddr;
93 isc_result_t result;
94 unsigned int options = 0;
95 dns_cache_t *cache;
97 RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS);
99 dns_result_register();
101 mctx = NULL;
102 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
104 while ((ch = isc_commandline_parse(argc, argv, "nvw:")) != -1) {
105 switch (ch) {
106 case 'n':
108 * We only try nibbles, so do nothing for this option.
110 break;
111 case 'v':
112 verbose = ISC_TRUE;
113 break;
114 case 'w':
115 workers = (unsigned int)atoi(isc_commandline_argument);
116 break;
120 if (verbose) {
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()));
126 taskmgr = NULL;
127 RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &taskmgr)
128 == ISC_R_SUCCESS);
129 task = NULL;
130 RUNTIME_CHECK(isc_task_create(taskmgr, 0, &task)
131 == ISC_R_SUCCESS);
132 isc_task_setname(task, "byaddr", NULL);
134 dispatchmgr = NULL;
135 RUNTIME_CHECK(dns_dispatchmgr_create(mctx, NULL, &dispatchmgr)
136 == ISC_R_SUCCESS);
138 timermgr = NULL;
139 RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS);
140 socketmgr = NULL;
141 RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
143 cache = NULL;
144 RUNTIME_CHECK(dns_cache_create(mctx, taskmgr, timermgr,
145 dns_rdataclass_in, "rbt", 0, NULL,
146 &cache) == ISC_R_SUCCESS);
148 view = NULL;
149 RUNTIME_CHECK(dns_view_create(mctx, dns_rdataclass_in, "default",
150 &view) == ISC_R_SUCCESS);
153 unsigned int attrs;
154 dns_dispatch_t *disp4 = NULL;
155 dns_dispatch_t *disp6 = NULL;
157 if (isc_net_probeipv4() == ISC_R_SUCCESS) {
158 isc_sockaddr_t any4;
160 isc_sockaddr_any(&any4);
162 attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP;
163 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr,
164 socketmgr,
165 taskmgr, &any4,
166 512, 6, 1024,
167 17, 19, attrs,
168 attrs, &disp4)
169 == ISC_R_SUCCESS);
170 INSIST(disp4 != NULL);
173 if (isc_net_probeipv6() == ISC_R_SUCCESS) {
174 isc_sockaddr_t any6;
176 isc_sockaddr_any6(&any6);
178 attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
179 RUNTIME_CHECK(dns_dispatch_getudp(dispatchmgr,
180 socketmgr,
181 taskmgr, &any6,
182 512, 6, 1024,
183 17, 19, attrs,
184 attrs, &disp6)
185 == ISC_R_SUCCESS);
186 INSIST(disp6 != NULL);
189 RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
190 socketmgr,
191 timermgr, 0,
192 dispatchmgr,
193 disp4, disp6) ==
194 ISC_R_SUCCESS);
196 if (disp4 != NULL)
197 dns_dispatch_detach(&disp4);
198 if (disp6 != NULL)
199 dns_dispatch_detach(&disp6);
203 struct in_addr ina;
204 isc_sockaddr_t sa;
205 isc_sockaddrlist_t sal;
207 ISC_LIST_INIT(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)
214 == ISC_R_SUCCESS);
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]);
223 na.family = AF_INET;
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");
230 exit(1);
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));
239 RUNTIME_CHECK(0);
242 (void)isc_app_run();
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);
262 if (verbose)
263 isc_mem_stats(mctx, stdout);
264 isc_mem_destroy(&mctx);
266 isc_app_finish();
268 return (0);