zone2_test.c 10.3 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1999, 2000  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

18
19
20
21
22
23
24
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <config.h>

#include <isc/app.h>
Mark Andrews's avatar
Mark Andrews committed
25
#include <isc/assertions.h>
26
27
28
29
#include <isc/error.h>
#include <isc/mem.h>

#include <dns/confparser.h>
Mark Andrews's avatar
Mark Andrews committed
30
#include <dns/journal.h>
31
32
#include <dns/view.h>
#include <dns/zone.h>
Mark Andrews's avatar
Mark Andrews committed
33
#include <dns/zoneconf.h>
34
#include <dns/zt.h>
35
36
37

#define ERRRET(result, function) \
	do { \
38
		if (result != ISC_R_SUCCESS) { \
39
40
41
42
43
44
45
			fprintf(stdout, "%s() returned %s\n", \
				function, dns_result_totext(result)); \
			return; \
		} \
	} while (0)

#define ERRCONT(result, function) \
46
		if (result != ISC_R_SUCCESS) { \
47
48
49
50
51
			fprintf(stdout, "%s() returned %s\n", \
				function, dns_result_totext(result)); \
			continue; \
		} else \
			(void)NULL
Mark Andrews's avatar
Mark Andrews committed
52

53
54
typedef struct dns_zone_callbackarg dns_zone_callbackarg_t;

Mark Andrews's avatar
Mark Andrews committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
struct dns_zone_callbackarg {
        isc_mem_t *mctx;
	dns_viewlist_t oldviews;
	dns_viewlist_t newviews;
};

static isc_result_t
dns_zone_callback(dns_c_ctx_t *cfg, dns_c_zone_t *czone, dns_c_view_t *cview,
		  void *uap) {
	dns_zone_callbackarg_t *cba = uap;
	dns_name_t *name = NULL;
	dns_view_t *oldview = NULL;
	dns_zone_t *oldzone = NULL;
	dns_view_t *newview = NULL;
	dns_zone_t *newzone = NULL;
	dns_zone_t *tmpzone = NULL;
	isc_result_t result;
	isc_boolean_t boolean;
	const char *viewname;

	REQUIRE(czone != NULL);
	REQUIRE(cba != NULL);

	/*
	 * Find views by name.
	 */
	if (cview != NULL)
		dns_c_view_getname(cview, &viewname);
	else
		viewname = "default";

	printf("view %s\n", viewname);

	result = dns_viewlist_find(&cba->oldviews, viewname, czone->zclass,
				   &oldview);
	result = dns_viewlist_find(&cba->newviews, viewname, czone->zclass,
				   &newview);

	if (newview == NULL) {
		result = dns_view_create(cba->mctx, czone->zclass, viewname,
					 &newview);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
		ISC_LIST_APPEND(cba->newviews, newview, link);
	}

	/*
	 * Create and populate a new zone structure.
	 */
	result = dns_zone_create(&newzone, cba->mctx);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	result = dns_zone_configure(cfg, NULL, czone, NULL, newzone);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

#if 0
	/* XXX hints should be a zone */
	if (dns_zone_gettype(newzone) == dns_zone_hint) {
		dns_view_sethints(newview, newzone);
		goto cleanup;
	}
#endif

	/*
	 * Find zone in mount table.
	 */
	name = dns_zone_getorigin(newzone);
	dns_zone_print(newzone);

	result = dns_zt_find(newview->zonetable, name, 0, NULL, &tmpzone);
	if (result == ISC_R_SUCCESS) {
		printf("zone already exists=\n");
		result = ISC_R_EXISTS;
		goto cleanup;
	} else if (result != DNS_R_PARTIALMATCH && result != ISC_R_NOTFOUND)
		goto cleanup;

	if (oldview != NULL)
		result = dns_zt_find(oldview->zonetable, name, 0, NULL,
				     &oldzone);
	else
		result = ISC_R_NOTFOUND;

	printf("dns_zt_find() returned %s\n", dns_result_totext(result));

	if (result == ISC_R_NOTFOUND || result == DNS_R_PARTIALMATCH) {
		if (result == DNS_R_PARTIALMATCH) {
			dns_zone_print(oldzone);
		}
		result = dns_view_addzone(newview, newzone);
		if (result != ISC_R_SUCCESS)
			goto cleanup;
	} else if (result == ISC_R_SUCCESS) {
		dns_zone_print(oldzone);
		/* Does the new configuration match the existing one? */
		boolean = dns_zone_equal(newzone, oldzone);
	printf("dns_zone_equal() returned %s\n", boolean ? "TRUE" : "FALSE");
		if (boolean)
			result = dns_view_addzone(newview, oldzone);
		else
			result = dns_view_addzone(newview, newzone);
	}

 cleanup:
	if (tmpzone != NULL)
		dns_zone_detach(&tmpzone);
	if (newzone != NULL)
		dns_zone_detach(&newzone);
	if (oldzone != NULL)
		dns_zone_detach(&oldzone);
	return (result);
}

170
171
172
173
static void
print_rdataset(dns_name_t *name, dns_rdataset_t *rdataset) {
        isc_buffer_t text;
        char t[1000];
174
        isc_result_t result;
175
176
177
178
179
180
        isc_region_t r;

        isc_buffer_init(&text, t, sizeof t, ISC_BUFFERTYPE_TEXT);
        result = dns_rdataset_totext(rdataset, name, ISC_FALSE, ISC_FALSE,
				     &text);
        isc_buffer_used(&text, &r);
181
        if (result == ISC_R_SUCCESS)
182
183
184
185
186
187
188
189
190
191
192
                printf("%.*s", (int)r.length, (char *)r.base);
        else
                printf("%s\n", dns_result_totext(result));
}

static void
query(dns_view_t *view) {
	char buf[1024];
	dns_fixedname_t name;
	char *s;
	isc_buffer_t buffer;
193
	isc_result_t result;
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
	dns_rdataset_t rdataset;
	dns_rdataset_t sigset;
	fd_set rfdset;
	int reload = 0;


	dns_rdataset_init(&rdataset);
	dns_rdataset_init(&sigset);

	do {
		
		fprintf(stdout, "zone_test ");
		fflush(stdout);
		FD_ZERO(&rfdset);
		FD_SET(0, &rfdset);
		select(1, &rfdset, NULL, NULL, NULL);
		if (fgets(buf, sizeof buf, stdin) == NULL) {
			fprintf(stdout, "\n");
			break;
		}
		buf[sizeof(buf) - 1] = '\0';
		
		s = strchr(buf, '\n');
		if (s != NULL)
			*s = '\0';
		s = strchr(buf, '\r');
		if (s != NULL)
			*s = '\0';
		if (strlen(buf) == 0) {
			reload = 0;
			continue;
		}
		if (strcasecmp(buf, "exit") == 0 ||
		    strcasecmp(buf, "quit") == 0)
			break;
		if (strcasecmp(buf, "reload") == 0) {
			reload = 1;
			continue;
		}
		if (strcasecmp(buf, "journal") == 0) {
234
			dns_journal_print(view->mctx, "dv.isc.org.ixfr", stdout);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
			reload = 0;
			continue;
		}

		dns_fixedname_init(&name);
		isc_buffer_init(&buffer, buf, strlen(buf), ISC_BUFFERTYPE_TEXT);
		isc_buffer_add(&buffer, strlen(buf));
		result = dns_name_fromtext(dns_fixedname_name(&name),
				  &buffer, dns_rootname, ISC_FALSE, NULL);
		ERRCONT(result, "dns_name_fromtext");
		
		if (reload) {
			dns_zone_t *zone = NULL;
			result = dns_zt_find(view->zonetable,
Bob Halley's avatar
Bob Halley committed
249
250
					     dns_fixedname_name(&name), 0,
					     NULL, &zone);
251
			if (result != ISC_R_SUCCESS) {
252
253
254
255
256
257
258
259
260
261
262
				if (result == DNS_R_PARTIALMATCH)
					dns_zone_detach(&zone);
				reload = 0;
				continue;
			}
			result = dns_zone_load(zone);
			fprintf(stdout, "%s() returned %s\n", "dns_zone_reload",
				dns_result_totext(result));
			reload = 0;
			dns_zone_detach(&zone);
		} else {
Bob Halley's avatar
Bob Halley committed
263
264
			result = dns_view_simplefind(view,
				       dns_fixedname_name(&name),
265
266
				       dns_rdatatype_a, 0, 0,
				       ISC_FALSE, &rdataset, &sigset);
Bob Halley's avatar
Bob Halley committed
267
268
			fprintf(stdout, "%s() returned %s\n",
				"dns_view_simplefind",
269
270
				dns_result_totext(result));
			switch (result) {
271
			case ISC_R_SUCCESS:
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
				print_rdataset(dns_fixedname_name(&name), 
					       &rdataset);
				break;
			default:
				continue;
			}
			dns_rdataset_disassociate(&rdataset);
		}
	} while (1);
	dns_rdataset_invalidate(&rdataset);
}

int
main(int argc, char **argv) {
	char *conf = "named.conf";
	isc_mem_t *mctx = NULL;
	dns_c_ctx_t *configctx = NULL;
	dns_view_t *view1 = NULL;
	dns_view_t *view2 = NULL;
	dns_view_t *view = NULL;
	dns_c_cbks_t cbks;
	int quiet = 0, stats = 0;
294
	isc_result_t result;
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
	int c;
	char *dir;
	dns_zone_callbackarg_t cba;

	while ((c = getopt(argc, argv, "c:qs")) != EOF) {
		switch (c) {
		case 'c':
			conf = optarg;
			break;
		case 'q':
			quiet = 1;
			break;
		case 's':
			stats = 1;
			break;
		default:
			break;
		}
	}

        RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
#if 0
	RUNTIME_CHECK(isc_taskmgr_create(mctx, 2, 0, &manager) ==
					      ISC_R_SUCCESS);
#endif

/*
	RUNTIME_CHECK(dns_view_create(mctx, dns_rdataclass_in,
324
				      "default/IN", &view1) == ISC_R_SUCCESS);
325
	RUNTIME_CHECK(dns_view_create(mctx, dns_rdataclass_in,
326
				      "default/IN", &view2) == ISC_R_SUCCESS);
327
328
329
330
331
332
333
334
335
336
337
 */

	cba.mctx = mctx;
	ISC_LIST_INIT(cba.oldviews);
	ISC_LIST_INIT(cba.newviews);

	cbks.zonecbk = dns_zone_callback;
	cbks.zonecbkuap = &cba;
	cbks.optscbk = NULL;
	cbks.optscbkuap = NULL;

338
	result = dns_c_parse_namedconf(conf, mctx, &configctx, &cbks);
339
340
341
	fprintf(stdout, "%s() returned %s\n", "dns_c_parse_namedconf",
		dns_result_totext(result));
	if (configctx != NULL)
342
		dns_c_ctx_delete(&configctx);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

	view = ISC_LIST_HEAD(cba.newviews);

	while (view != NULL) {
		dns_zt_print(view->zonetable);
		view = ISC_LIST_NEXT(view, link);
	}

	/* mv new ->old */
	cba.oldviews = cba.newviews;
	ISC_LIST_INIT(cba.newviews);

	/*
	view = ISC_LIST_HEAD(cba.newviews);
	while (view != NULL) {
		dns_view_t *next;
		next = ISC_LIST_NEXT(view, link);
		ISC_LIST_UNLINK(cba.newviews, view, link);
		ISC_LIST_APPEND(cba.oldviews, view, link);
		view = next;
	}
	*/

366
	result = dns_c_parse_namedconf(conf, mctx, &configctx, &cbks);
367
368
	fprintf(stdout, "%s() returned %s\n", "dns_c_parse_namedconf",
		dns_result_totext(result));
369
	if (result == ISC_R_SUCCESS) {
370
		result = dns_c_ctx_getdirectory(configctx, &dir);
371
		if (result == ISC_R_SUCCESS)
372
373
374
375
			chdir(dir);
		view = ISC_LIST_HEAD(cba.newviews);
		while (view != NULL) {
			dns_zt_print(view->zonetable);
Mark Andrews's avatar
Mark Andrews committed
376
			dns_zt_load(view->zonetable, ISC_FALSE);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
			dns_view_freeze(view);
			view = ISC_LIST_NEXT(view, link);
		}
		view = ISC_LIST_HEAD(cba.newviews);
		query(view);
	} else {
		view = ISC_LIST_HEAD(cba.oldviews);
		while (view != NULL) {
			dns_view_t *next;
			next = ISC_LIST_NEXT(view, link);
			ISC_LIST_UNLINK(cba.oldviews, view, link);
			dns_view_detach(&view);
		}
	}

	/* cleanup */
	view = ISC_LIST_HEAD(cba.oldviews);
	while (view != NULL) {
		dns_view_t *next;
		next = ISC_LIST_NEXT(view, link);
		ISC_LIST_UNLINK(cba.oldviews, view, link);
		dns_view_detach(&view);
		view = next;
	}

	view = ISC_LIST_HEAD(cba.newviews);
	while (view != NULL) {
		dns_view_t *next;
		next = ISC_LIST_NEXT(view, link);
		ISC_LIST_UNLINK(cba.newviews, view, link);
		dns_view_detach(&view);
		view = next;
	}

	if (configctx != NULL)
Mark Andrews's avatar
Mark Andrews committed
412
		dns_c_ctx_delete(&configctx);
413
414
415
416
417
418
419
420
421
422
423
	if (view2 != NULL)
		dns_view_detach(&view2);
	if (view1 != NULL)
		dns_view_detach(&view1);

	if (!quiet && stats)
		isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);

	exit(0);
}