byname_test.c 8.27 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Bob Halley's avatar
Bob Halley committed
10 11
 */

12
/*! \file
Bob Halley's avatar
Bob Halley committed
13 14
 */

15
#include <stdbool.h>
Bob Halley's avatar
Bob Halley committed
16
#include <stdlib.h>
Brian Wellington's avatar
Brian Wellington committed
17
#include <string.h>
Bob Halley's avatar
Bob Halley committed
18

19
#include <isc/app.h>
Bob Halley's avatar
Bob Halley committed
20
#include <isc/commandline.h>
21
#include <isc/hash.h>
22
#include <isc/netaddr.h>
23
#include <isc/print.h>
Bob Halley's avatar
Bob Halley committed
24 25 26 27 28 29
#include <isc/task.h>
#include <isc/timer.h>
#include <isc/util.h>

#include <dns/adb.h>
#include <dns/cache.h>
30
#include <dns/dispatch.h>
Bob Halley's avatar
Bob Halley committed
31
#include <dns/events.h>
32
#include <dns/forward.h>
Bob Halley's avatar
Bob Halley committed
33
#include <dns/log.h>
34 35
#include <dns/resolver.h>
#include <dns/result.h>
Bob Halley's avatar
Bob Halley committed
36

Evan Hunt's avatar
Evan Hunt committed
37 38 39 40 41 42 43 44
static isc_mem_t *mctx = NULL;
static isc_taskmgr_t *taskmgr;
static dns_view_t *view = NULL;
static dns_adbfind_t *find = NULL;
static isc_task_t *task = NULL;
static dns_fixedname_t fixed;
static dns_fixedname_t target;
static isc_log_t *lctx;
Bob Halley's avatar
Bob Halley committed
45
static isc_logconfig_t *lcfg;
Evan Hunt's avatar
Evan Hunt committed
46
static unsigned int level = 0;
Bob Halley's avatar
Bob Halley committed
47

Ondřej Surý's avatar
Ondřej Surý committed
48 49
static void
adb_callback(isc_task_t *task, isc_event_t *event);
Bob Halley's avatar
Bob Halley committed
50 51

static void
Evan Hunt's avatar
Evan Hunt committed
52
log_init(void) {
Bob Halley's avatar
Bob Halley committed
53
	isc_logdestination_t destination;
Evan Hunt's avatar
Evan Hunt committed
54
	unsigned int flags;
Bob Halley's avatar
Bob Halley committed
55 56 57 58

	/*
	 * Setup a logging context.
	 */
59
	isc_log_create(mctx, &lctx, &lcfg);
60
	isc_log_setcontext(lctx);
Bob Halley's avatar
Bob Halley committed
61
	dns_log_init(lctx);
62
	dns_log_setcontext(lctx);
Bob Halley's avatar
Bob Halley committed
63 64 65 66 67 68 69 70 71

	/*
	 * Create and install the default channel.
	 */
	destination.file.stream = stderr;
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;
	flags = ISC_LOG_PRINTTIME;
72 73 74
	isc_log_createchannel(lcfg, "_default", ISC_LOG_TOFILEDESC,
			      ISC_LOG_DYNAMIC, &destination, flags);

Bob Halley's avatar
Bob Halley committed
75
	RUNTIME_CHECK(isc_log_usechannel(lcfg, "_default", NULL, NULL) ==
Bob Halley's avatar
Bob Halley committed
76 77 78 79 80
		      ISC_R_SUCCESS);
	isc_log_setdebuglevel(lctx, level);
}

static void
Evan Hunt's avatar
Evan Hunt committed
81
print_addresses(dns_adbfind_t *adbfind) {
Bob Halley's avatar
Bob Halley committed
82 83
	dns_adbaddrinfo_t *address;

84
	for (address = ISC_LIST_HEAD(adbfind->list); address != NULL;
Evan Hunt's avatar
Evan Hunt committed
85 86
	     address = ISC_LIST_NEXT(address, publink))
	{
87
		isc_netaddr_t netaddr;
Evan Hunt's avatar
Evan Hunt committed
88
		char text[ISC_NETADDR_FORMATSIZE];
89
		isc_netaddr_fromsockaddr(&netaddr, &address->sockaddr);
90 91
		isc_netaddr_format(&netaddr, text, sizeof(text));
		printf("%s\n", text);
Bob Halley's avatar
Bob Halley committed
92 93 94 95
	}
}

static void
Evan Hunt's avatar
Evan Hunt committed
96
print_name(dns_name_t *name) {
97
	char text[DNS_NAME_FORMATSIZE];
Bob Halley's avatar
Bob Halley committed
98

99 100
	dns_name_format(name, text, sizeof(text));
	printf("%s\n", text);
Bob Halley's avatar
Bob Halley committed
101 102 103
}

static void
Evan Hunt's avatar
Evan Hunt committed
104
do_find(bool want_event) {
Bob Halley's avatar
Bob Halley committed
105
	isc_result_t result;
Evan Hunt's avatar
Evan Hunt committed
106
	bool done = false;
Bob Halley's avatar
Bob Halley committed
107 108 109
	unsigned int options;

	options = DNS_ADBFIND_INET | DNS_ADBFIND_INET6;
110
	if (want_event) {
Bob Halley's avatar
Bob Halley committed
111
		options |= DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
112
	}
Bob Halley's avatar
Bob Halley committed
113 114
	dns_fixedname_init(&target);
	result = dns_adb_createfind(view->adb, task, adb_callback, NULL,
115 116
				    dns_fixedname_name(&fixed), dns_rootname, 0,
				    options, 0, dns_fixedname_name(&target), 0,
117
				    0, NULL, &find);
Bob Halley's avatar
Bob Halley committed
118 119 120 121 122 123 124 125
	if (result == ISC_R_SUCCESS) {
		if (!ISC_LIST_EMPTY(find->list)) {
			/*
			 * We have at least some of the addresses for the
			 * name.
			 */
			INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
			print_addresses(find);
126
			done = true;
Bob Halley's avatar
Bob Halley committed
127 128 129 130 131 132 133 134 135 136
		} else {
			/*
			 * We don't know any of the addresses for this
			 * name.
			 */
			if ((find->options & DNS_ADBFIND_WANTEVENT) == 0) {
				/*
				 * And ADB isn't going to send us any events
				 * either.  This query loses.
				 */
137
				done = true;
Bob Halley's avatar
Bob Halley committed
138 139 140 141 142 143 144 145
			}
			/*
			 * If the DNS_ADBFIND_WANTEVENT flag was set, we'll
			 * get an event when something happens.
			 */
		}
	} else if (result == DNS_R_ALIAS) {
		print_name(dns_fixedname_name(&target));
146
		done = true;
Bob Halley's avatar
Bob Halley committed
147 148 149
	} else {
		printf("dns_adb_createfind() returned %s\n",
		       isc_result_totext(result));
150
		done = true;
Bob Halley's avatar
Bob Halley committed
151 152 153
	}

	if (done) {
154
		if (find != NULL) {
Bob Halley's avatar
Bob Halley committed
155
			dns_adb_destroyfind(&find);
156
		}
Bob Halley's avatar
Bob Halley committed
157 158 159 160 161
		isc_app_shutdown();
	}
}

static void
Evan Hunt's avatar
Evan Hunt committed
162
adb_callback(isc_task_t *etask, isc_event_t *event) {
163
	unsigned int type = event->ev_type;
Bob Halley's avatar
Bob Halley committed
164 165 166 167 168 169

	REQUIRE(etask == task);

	isc_event_free(&event);
	dns_adb_destroyfind(&find);

170
	if (type == DNS_EVENT_ADBMOREADDRESSES) {
171
		do_find(false);
172
	} else if (type == DNS_EVENT_ADBNOMOREADDRESSES) {
Bob Halley's avatar
Bob Halley committed
173
		printf("no more addresses\n");
174 175
		isc_app_shutdown();
	} else {
Bob Halley's avatar
Bob Halley committed
176
		printf("unexpected ADB event type %u\n", type);
177 178
		isc_app_shutdown();
	}
Bob Halley's avatar
Bob Halley committed
179 180 181
}

static void
Evan Hunt's avatar
Evan Hunt committed
182
run(isc_task_t *xtask, isc_event_t *event) {
183
	UNUSED(xtask);
184
	do_find(true);
Bob Halley's avatar
Bob Halley committed
185 186 187 188
	isc_event_free(&event);
}

int
Evan Hunt's avatar
Evan Hunt committed
189 190 191 192 193 194
main(int argc, char *argv[]) {
	bool verbose = false;
	unsigned int workers = 2;
	isc_timermgr_t *timermgr;
	int ch;
	isc_socketmgr_t *socketmgr;
195
	dns_dispatchmgr_t *dispatchmgr;
Evan Hunt's avatar
Evan Hunt committed
196 197
	dns_cache_t *cache;
	isc_buffer_t b;
Bob Halley's avatar
Bob Halley committed
198 199 200 201 202 203

	RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS);

	dns_result_register();

	mctx = NULL;
204
	isc_mem_create(&mctx);
Bob Halley's avatar
Bob Halley committed
205 206 207 208 209 210 211

	while ((ch = isc_commandline_parse(argc, argv, "d:vw:")) != -1) {
		switch (ch) {
		case 'd':
			level = (unsigned int)atoi(isc_commandline_argument);
			break;
		case 'v':
212
			verbose = true;
Bob Halley's avatar
Bob Halley committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
			break;
		case 'w':
			workers = (unsigned int)atoi(isc_commandline_argument);
			break;
		}
	}

	log_init();

	if (verbose) {
		printf("%u workers\n", workers);
		printf("IPv4: %s\n", isc_result_totext(isc_net_probeipv4()));
		printf("IPv6: %s\n", isc_result_totext(isc_net_probeipv6()));
	}

	taskmgr = NULL;
229
	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, NULL, &taskmgr) ==
Bob Halley's avatar
Bob Halley committed
230 231
		      ISC_R_SUCCESS);
	task = NULL;
232
	RUNTIME_CHECK(isc_task_create(taskmgr, 0, &task) == ISC_R_SUCCESS);
233
	isc_task_setname(task, "byname", NULL);
Bob Halley's avatar
Bob Halley committed
234

235
	dispatchmgr = NULL;
236 237
	RUNTIME_CHECK(dns_dispatchmgr_create(mctx, &dispatchmgr) ==
		      ISC_R_SUCCESS);
238

Bob Halley's avatar
Bob Halley committed
239 240 241 242 243 244
	timermgr = NULL;
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS);
	socketmgr = NULL;
	RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);

	cache = NULL;
245 246
	RUNTIME_CHECK(dns_cache_create(mctx, mctx, taskmgr, timermgr,
				       dns_rdataclass_in, "", "rbt", 0, NULL,
Bob Halley's avatar
Bob Halley committed
247 248 249 250 251 252
				       &cache) == ISC_R_SUCCESS);

	view = NULL;
	RUNTIME_CHECK(dns_view_create(mctx, dns_rdataclass_in, "default",
				      &view) == ISC_R_SUCCESS);

253
	{
Evan Hunt's avatar
Evan Hunt committed
254
		unsigned int attrs;
255
		dns_dispatch_t *disp4 = NULL;
256
		dns_dispatch_t *disp6 = NULL;
257 258 259 260

		if (isc_net_probeipv4() == ISC_R_SUCCESS) {
			isc_sockaddr_t any4;
			isc_sockaddr_any(&any4);
261

262
			attrs = DNS_DISPATCHATTR_IPV4 | DNS_DISPATCHATTR_UDP;
263 264 265 266 267
			RUNTIME_CHECK(
				dns_dispatch_getudp(dispatchmgr, socketmgr,
						    taskmgr, &any4, 512, 6,
						    1024, 17, 19, attrs, attrs,
						    &disp4) == ISC_R_SUCCESS);
268 269 270 271 272
			INSIST(disp4 != NULL);
		}

		if (isc_net_probeipv6() == ISC_R_SUCCESS) {
			isc_sockaddr_t any6;
273

274
			isc_sockaddr_any6(&any6);
275

276
			attrs = DNS_DISPATCHATTR_IPV6 | DNS_DISPATCHATTR_UDP;
277 278 279 280 281
			RUNTIME_CHECK(
				dns_dispatch_getudp(dispatchmgr, socketmgr,
						    taskmgr, &any6, 512, 6,
						    1024, 17, 19, attrs, attrs,
						    &disp6) == ISC_R_SUCCESS);
282 283
			INSIST(disp6 != NULL);
		}
284

285
		RUNTIME_CHECK(dns_view_createresolver(view, taskmgr, 10, 1,
286 287 288
						      socketmgr, timermgr, 0,
						      dispatchmgr, disp4,
						      disp6) == ISC_R_SUCCESS);
289

290
		if (disp4 != NULL) {
291
			dns_dispatch_detach(&disp4);
292 293
		}
		if (disp6 != NULL) {
294
			dns_dispatch_detach(&disp6);
295
		}
296
	}
Bob Halley's avatar
Bob Halley committed
297 298

	{
Evan Hunt's avatar
Evan Hunt committed
299 300
		struct in_addr ina;
		isc_sockaddr_t sa;
Bob Halley's avatar
Bob Halley committed
301 302 303 304 305 306 307
		isc_sockaddrlist_t sal;

		ISC_LIST_INIT(sal);
		ina.s_addr = inet_addr("127.0.0.1");
		isc_sockaddr_fromin(&sa, &ina, 53);
		ISC_LIST_APPEND(sal, &sa, link);

308
		REQUIRE(DNS_VIEW_VALID(view));
309
		RUNTIME_CHECK(dns_fwdtable_add(view->fwdtable, dns_rootname,
310 311
					       &sal, dns_fwdpolicy_only) ==
			      ISC_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
312 313
	}

314
	dns_view_setcache(view, cache, false);
Bob Halley's avatar
Bob Halley committed
315 316 317 318 319 320
	dns_view_freeze(view);

	dns_cache_detach(&cache);

	printf("name = %s\n", argv[isc_commandline_index]);
	isc_buffer_init(&b, argv[isc_commandline_index],
321
			strlen(argv[isc_commandline_index]));
Bob Halley's avatar
Bob Halley committed
322
	isc_buffer_add(&b, strlen(argv[isc_commandline_index]));
323
	dns_fixedname_init(&fixed);
Bob Halley's avatar
Bob Halley committed
324
	dns_fixedname_init(&target);
325
	RUNTIME_CHECK(dns_name_fromtext(dns_fixedname_name(&fixed), &b,
326 327
					dns_rootname, 0,
					NULL) == ISC_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
328 329 330 331 332 333 334 335

	RUNTIME_CHECK(isc_app_onrun(mctx, task, run, NULL) == ISC_R_SUCCESS);

	(void)isc_app_run();

	dns_view_detach(&view);
	isc_task_shutdown(task);
	isc_task_detach(&task);
336 337

	dns_dispatchmgr_destroy(&dispatchmgr);
338

Bob Halley's avatar
Bob Halley committed
339 340 341 342
	isc_taskmgr_destroy(&taskmgr);

	isc_socketmgr_destroy(&socketmgr);
	isc_timermgr_destroy(&timermgr);
343

Bob Halley's avatar
Bob Halley committed
344 345
	isc_log_destroy(&lctx);

346
	if (verbose) {
Bob Halley's avatar
Bob Halley committed
347
		isc_mem_stats(mctx, stdout);
348
	}
Bob Halley's avatar
Bob Halley committed
349 350 351 352 353 354
	isc_mem_destroy(&mctx);

	isc_app_finish();

	return (0);
}