No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / db_test.c
blob282cccbb7ce0b6d11de0b23e21a135550851d3c6
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-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: db_test.c,v 1.68 2009/09/02 23:48:01 tbox Exp */
22 /*! \file
23 * \author
24 * Principal Author: Bob Halley
27 #include <config.h>
29 #include <stdlib.h>
31 #include <isc/commandline.h>
32 #include <isc/log.h>
33 #include <isc/mem.h>
34 #include <isc/time.h>
35 #include <isc/string.h>
36 #include <isc/util.h>
38 #include <dns/db.h>
39 #include <dns/dbiterator.h>
40 #include <dns/dbtable.h>
41 #include <dns/fixedname.h>
42 #include <dns/log.h>
43 #include <dns/rdataset.h>
44 #include <dns/rdatasetiter.h>
45 #include <dns/result.h>
47 #define MAXHOLD 100
48 #define MAXVERSIONS 100
50 typedef struct dbinfo {
51 dns_db_t * db;
52 dns_dbversion_t * version;
53 dns_dbversion_t * wversion;
54 dns_dbversion_t * rversions[MAXVERSIONS];
55 int rcount;
56 dns_dbnode_t * hold_nodes[MAXHOLD];
57 int hold_count;
58 dns_dbiterator_t * dbiterator;
59 dns_dbversion_t * iversion;
60 int pause_every;
61 isc_boolean_t ascending;
62 ISC_LINK(struct dbinfo) link;
63 } dbinfo;
65 static isc_mem_t * mctx = NULL;
66 static char dbtype[128];
67 static dns_dbtable_t * dbtable;
68 static ISC_LIST(dbinfo) dbs;
69 static dbinfo * cache_dbi = NULL;
70 static int pause_every = 0;
71 static isc_boolean_t ascending = ISC_TRUE;
73 static void
74 print_result(const char *message, isc_result_t result) {
75 size_t len;
77 if (message == NULL) {
78 len = 0;
79 message = "";
81 len = strlen(message);
82 printf("%s%sresult %08x: %s\n", message, (len == 0U) ? "" : " ",
83 result, isc_result_totext(result));
86 static void
87 print_rdataset(dns_name_t *name, dns_rdataset_t *rdataset) {
88 isc_buffer_t text;
89 char t[1000];
90 isc_result_t result;
91 isc_region_t r;
93 isc_buffer_init(&text, t, sizeof(t));
94 result = dns_rdataset_totext(rdataset, name, ISC_FALSE, ISC_FALSE,
95 &text);
96 isc_buffer_usedregion(&text, &r);
97 if (result == ISC_R_SUCCESS)
98 printf("%.*s", (int)r.length, (char *)r.base);
99 else
100 print_result("", result);
103 static void
104 print_rdatasets(dns_name_t *name, dns_rdatasetiter_t *rdsiter) {
105 isc_result_t result;
106 dns_rdataset_t rdataset;
108 dns_rdataset_init(&rdataset);
109 result = dns_rdatasetiter_first(rdsiter);
110 while (result == ISC_R_SUCCESS) {
111 dns_rdatasetiter_current(rdsiter, &rdataset);
112 print_rdataset(name, &rdataset);
113 dns_rdataset_disassociate(&rdataset);
114 result = dns_rdatasetiter_next(rdsiter);
116 if (result != ISC_R_NOMORE)
117 print_result("", result);
120 static dbinfo *
121 select_db(char *origintext) {
122 dns_fixedname_t forigin;
123 dns_name_t *origin;
124 isc_buffer_t source;
125 size_t len;
126 dbinfo *dbi;
127 isc_result_t result;
129 if (strcasecmp(origintext, "cache") == 0) {
130 if (cache_dbi == NULL)
131 printf("the cache does not exist\n");
132 return (cache_dbi);
134 len = strlen(origintext);
135 isc_buffer_init(&source, origintext, len);
136 isc_buffer_add(&source, len);
137 dns_fixedname_init(&forigin);
138 origin = dns_fixedname_name(&forigin);
139 result = dns_name_fromtext(origin, &source, dns_rootname, 0, NULL);
140 if (result != ISC_R_SUCCESS) {
141 print_result("bad name", result);
142 return (NULL);
145 for (dbi = ISC_LIST_HEAD(dbs);
146 dbi != NULL;
147 dbi = ISC_LIST_NEXT(dbi, link)) {
148 if (dns_name_compare(dns_db_origin(dbi->db), origin) == 0)
149 break;
152 return (dbi);
155 static void
156 list(dbinfo *dbi, char *seektext) {
157 dns_fixedname_t fname;
158 dns_name_t *name;
159 dns_dbnode_t *node;
160 dns_rdatasetiter_t *rdsiter;
161 isc_result_t result;
162 int i;
163 size_t len;
164 dns_fixedname_t fseekname;
165 dns_name_t *seekname;
166 isc_buffer_t source;
168 dns_fixedname_init(&fname);
169 name = dns_fixedname_name(&fname);
171 if (dbi->dbiterator == NULL) {
172 INSIST(dbi->iversion == NULL);
173 if (dns_db_iszone(dbi->db)) {
174 if (dbi->version != NULL)
175 dns_db_attachversion(dbi->db, dbi->version,
176 &dbi->iversion);
177 else
178 dns_db_currentversion(dbi->db, &dbi->iversion);
181 result = dns_db_createiterator(dbi->db, 0, &dbi->dbiterator);
182 if (result == ISC_R_SUCCESS) {
183 if (seektext != NULL) {
184 len = strlen(seektext);
185 isc_buffer_init(&source, seektext, len);
186 isc_buffer_add(&source, len);
187 dns_fixedname_init(&fseekname);
188 seekname = dns_fixedname_name(&fseekname);
189 result = dns_name_fromtext(seekname, &source,
190 dns_db_origin(
191 dbi->db),
192 0, NULL);
193 if (result == ISC_R_SUCCESS)
194 result = dns_dbiterator_seek(
195 dbi->dbiterator,
196 seekname);
197 } else if (dbi->ascending)
198 result = dns_dbiterator_first(dbi->dbiterator);
199 else
200 result = dns_dbiterator_last(dbi->dbiterator);
202 } else
203 result = ISC_R_SUCCESS;
205 node = NULL;
206 rdsiter = NULL;
207 i = 0;
208 while (result == ISC_R_SUCCESS) {
209 result = dns_dbiterator_current(dbi->dbiterator, &node, name);
210 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
211 break;
212 result = dns_db_allrdatasets(dbi->db, node, dbi->iversion, 0,
213 &rdsiter);
214 if (result != ISC_R_SUCCESS) {
215 dns_db_detachnode(dbi->db, &node);
216 break;
218 print_rdatasets(name, rdsiter);
219 dns_rdatasetiter_destroy(&rdsiter);
220 dns_db_detachnode(dbi->db, &node);
221 if (dbi->ascending)
222 result = dns_dbiterator_next(dbi->dbiterator);
223 else
224 result = dns_dbiterator_prev(dbi->dbiterator);
225 i++;
226 if (result == ISC_R_SUCCESS && i == dbi->pause_every) {
227 printf("[more...]\n");
228 result = dns_dbiterator_pause(dbi->dbiterator);
229 if (result == ISC_R_SUCCESS)
230 return;
233 if (result != ISC_R_NOMORE)
234 print_result("", result);
236 dns_dbiterator_destroy(&dbi->dbiterator);
237 if (dbi->iversion != NULL)
238 dns_db_closeversion(dbi->db, &dbi->iversion, ISC_FALSE);
241 static isc_result_t
242 load(const char *filename, const char *origintext, isc_boolean_t cache) {
243 dns_fixedname_t forigin;
244 dns_name_t *origin;
245 isc_result_t result;
246 isc_buffer_t source;
247 size_t len;
248 dbinfo *dbi;
249 unsigned int i;
251 dbi = isc_mem_get(mctx, sizeof(*dbi));
252 if (dbi == NULL)
253 return (ISC_R_NOMEMORY);
255 dbi->db = NULL;
256 dbi->version = NULL;
257 dbi->wversion = NULL;
258 for (i = 0; i < MAXVERSIONS; i++)
259 dbi->rversions[i] = NULL;
260 dbi->hold_count = 0;
261 for (i = 0; i < MAXHOLD; i++)
262 dbi->hold_nodes[i] = NULL;
263 dbi->dbiterator = NULL;
264 dbi->iversion = NULL;
265 dbi->pause_every = pause_every;
266 dbi->ascending = ascending;
267 ISC_LINK_INIT(dbi, link);
269 len = strlen(origintext);
270 isc_buffer_init(&source, origintext, len);
271 isc_buffer_add(&source, len);
272 dns_fixedname_init(&forigin);
273 origin = dns_fixedname_name(&forigin);
274 result = dns_name_fromtext(origin, &source, dns_rootname, 0, NULL);
275 if (result != ISC_R_SUCCESS)
276 return (result);
278 result = dns_db_create(mctx, dbtype, origin,
279 cache ? dns_dbtype_cache : dns_dbtype_zone,
280 dns_rdataclass_in,
281 0, NULL, &dbi->db);
282 if (result != ISC_R_SUCCESS) {
283 isc_mem_put(mctx, dbi, sizeof(*dbi));
284 return (result);
287 printf("loading %s (%s)\n", filename, origintext);
288 result = dns_db_load(dbi->db, filename);
289 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
290 dns_db_detach(&dbi->db);
291 isc_mem_put(mctx, dbi, sizeof(*dbi));
292 return (result);
294 printf("loaded\n");
296 if (cache) {
297 INSIST(cache_dbi == NULL);
298 dns_dbtable_adddefault(dbtable, dbi->db);
299 cache_dbi = dbi;
300 } else {
301 if (dns_dbtable_add(dbtable, dbi->db) != ISC_R_SUCCESS) {
302 dns_db_detach(&dbi->db);
303 isc_mem_put(mctx, dbi, sizeof(*dbi));
304 return (result);
307 ISC_LIST_APPEND(dbs, dbi, link);
309 return (ISC_R_SUCCESS);
312 static void
313 unload_all(void) {
314 dbinfo *dbi, *dbi_next;
316 for (dbi = ISC_LIST_HEAD(dbs); dbi != NULL; dbi = dbi_next) {
317 dbi_next = ISC_LIST_NEXT(dbi, link);
318 if (dns_db_iszone(dbi->db))
319 dns_dbtable_remove(dbtable, dbi->db);
320 else {
321 INSIST(dbi == cache_dbi);
322 dns_dbtable_removedefault(dbtable);
323 cache_dbi = NULL;
325 dns_db_detach(&dbi->db);
326 ISC_LIST_UNLINK(dbs, dbi, link);
327 isc_mem_put(mctx, dbi, sizeof(*dbi));
331 #define DBI_CHECK(dbi) \
332 if ((dbi) == NULL) { \
333 printf("You must first select a database with !DB\n"); \
334 continue; \
338 main(int argc, char *argv[]) {
339 dns_db_t *db;
340 dns_dbnode_t *node;
341 isc_result_t result;
342 dns_name_t name;
343 dns_offsets_t offsets;
344 size_t len;
345 isc_buffer_t source, target;
346 char s[1000];
347 char b[255];
348 dns_rdataset_t rdataset, sigrdataset;
349 int ch;
350 dns_rdatatype_t type = 1;
351 isc_boolean_t printnode = ISC_FALSE;
352 isc_boolean_t addmode = ISC_FALSE;
353 isc_boolean_t delmode = ISC_FALSE;
354 isc_boolean_t holdmode = ISC_FALSE;
355 isc_boolean_t verbose = ISC_FALSE;
356 isc_boolean_t done = ISC_FALSE;
357 isc_boolean_t quiet = ISC_FALSE;
358 isc_boolean_t time_lookups = ISC_FALSE;
359 isc_boolean_t found_as;
360 isc_boolean_t find_zonecut = ISC_FALSE;
361 isc_boolean_t noexact_zonecut = ISC_FALSE;
362 int i, v;
363 dns_rdatasetiter_t *rdsiter;
364 char t1[256];
365 char t2[256];
366 isc_buffer_t tb1, tb2;
367 isc_region_t r1, r2;
368 dns_fixedname_t foundname;
369 dns_name_t *fname;
370 unsigned int options = 0, zcoptions;
371 isc_time_t start, finish;
372 char *origintext;
373 dbinfo *dbi;
374 dns_dbversion_t *version;
375 dns_name_t *origin;
376 size_t memory_quota = 0;
377 dns_trust_t trust = 0;
378 unsigned int addopts;
379 isc_log_t *lctx = NULL;
381 dns_result_register();
383 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
384 RUNTIME_CHECK(dns_dbtable_create(mctx, dns_rdataclass_in, &dbtable) ==
385 ISC_R_SUCCESS);
389 strcpy(dbtype, "rbt");
390 while ((ch = isc_commandline_parse(argc, argv, "c:d:t:z:P:Q:glpqvT"))
391 != -1) {
392 switch (ch) {
393 case 'c':
394 result = load(isc_commandline_argument, ".", ISC_TRUE);
395 if (result != ISC_R_SUCCESS)
396 printf("cache load(%s) %08x: %s\n",
397 isc_commandline_argument, result,
398 isc_result_totext(result));
399 break;
400 case 'd':
401 strcpy(dbtype, isc_commandline_argument);
402 break;
403 case 'g':
404 options |= (DNS_DBFIND_GLUEOK|DNS_DBFIND_VALIDATEGLUE);
405 break;
406 case 'l':
407 RUNTIME_CHECK(isc_log_create(mctx, &lctx,
408 NULL) == ISC_R_SUCCESS);
409 isc_log_setcontext(lctx);
410 dns_log_init(lctx);
411 dns_log_setcontext(lctx);
412 break;
413 case 'q':
414 quiet = ISC_TRUE;
415 verbose = ISC_FALSE;
416 break;
417 case 'p':
418 printnode = ISC_TRUE;
419 break;
420 case 'P':
421 pause_every = atoi(isc_commandline_argument);
422 break;
423 case 'Q':
424 memory_quota = atoi(isc_commandline_argument);
425 isc_mem_setquota(mctx, memory_quota);
426 break;
427 case 't':
428 type = atoi(isc_commandline_argument);
429 break;
430 case 'T':
431 time_lookups = ISC_TRUE;
432 break;
433 case 'v':
434 verbose = ISC_TRUE;
435 break;
436 case 'z':
437 origintext = strrchr(isc_commandline_argument, '/');
438 if (origintext == NULL)
439 origintext = isc_commandline_argument;
440 else
441 origintext++; /* Skip '/'. */
442 result = load(isc_commandline_argument, origintext,
443 ISC_FALSE);
444 if (result != ISC_R_SUCCESS)
445 printf("zone load(%s) %08x: %s\n",
446 isc_commandline_argument, result,
447 isc_result_totext(result));
448 break;
452 argc -= isc_commandline_index;
453 argv += isc_commandline_index;
455 if (argc != 0)
456 printf("ignoring trailing arguments\n");
459 * Some final initialization...
461 dns_fixedname_init(&foundname);
462 fname = dns_fixedname_name(&foundname);
463 dbi = NULL;
464 origin = dns_rootname;
465 version = NULL;
467 if (time_lookups) {
468 TIME_NOW(&start);
471 while (!done) {
472 if (!quiet)
473 printf("\n");
474 if (fgets(s, sizeof(s), stdin) == NULL) {
475 done = ISC_TRUE;
476 continue;
478 len = strlen(s);
479 if (len > 0U && s[len - 1] == '\n') {
480 s[len - 1] = '\0';
481 len--;
483 if (verbose && dbi != NULL) {
484 if (dbi->wversion != NULL)
485 printf("future version (%p)\n", dbi->wversion);
486 for (i = 0; i < dbi->rcount; i++)
487 if (dbi->rversions[i] != NULL)
488 printf("open version %d (%p)\n", i,
489 dbi->rversions[i]);
491 dns_name_init(&name, offsets);
492 if (strcmp(s, "!R") == 0) {
493 DBI_CHECK(dbi);
494 if (dbi->rcount == MAXVERSIONS) {
495 printf("too many open versions\n");
496 continue;
498 dns_db_currentversion(dbi->db,
499 &dbi->rversions[dbi->rcount]);
500 printf("opened version %d\n", dbi->rcount);
501 dbi->version = dbi->rversions[dbi->rcount];
502 version = dbi->version;
503 dbi->rcount++;
504 continue;
505 } else if (strcmp(s, "!W") == 0) {
506 DBI_CHECK(dbi);
507 if (dbi->wversion != NULL) {
508 printf("using existing future version\n");
509 dbi->version = dbi->wversion;
510 version = dbi->version;
511 continue;
513 result = dns_db_newversion(dbi->db, &dbi->wversion);
514 if (result != ISC_R_SUCCESS)
515 print_result("", result);
516 else
517 printf("newversion\n");
518 dbi->version = dbi->wversion;
519 version = dbi->version;
520 continue;
521 } else if (strcmp(s, "!C") == 0) {
522 DBI_CHECK(dbi);
523 addmode = ISC_FALSE;
524 delmode = ISC_FALSE;
525 if (dbi->version == NULL)
526 continue;
527 if (dbi->version == dbi->wversion) {
528 printf("closing future version\n");
529 dbi->wversion = NULL;
530 } else {
531 for (i = 0; i < dbi->rcount; i++) {
532 if (dbi->version ==
533 dbi->rversions[i]) {
534 dbi->rversions[i] = NULL;
535 printf("closing open version %d\n",
537 break;
541 dns_db_closeversion(dbi->db, &dbi->version, ISC_TRUE);
542 version = NULL;
543 continue;
544 } else if (strcmp(s, "!X") == 0) {
545 DBI_CHECK(dbi);
546 addmode = ISC_FALSE;
547 delmode = ISC_FALSE;
548 if (dbi->version == NULL)
549 continue;
550 if (dbi->version == dbi->wversion) {
551 printf("aborting future version\n");
552 dbi->wversion = NULL;
553 } else {
554 for (i = 0; i < dbi->rcount; i++) {
555 if (dbi->version ==
556 dbi->rversions[i]) {
557 dbi->rversions[i] = NULL;
558 printf("closing open version %d\n",
560 break;
564 dns_db_closeversion(dbi->db, &dbi->version, ISC_FALSE);
565 version = NULL;
566 continue;
567 } else if (strcmp(s, "!A") == 0) {
568 DBI_CHECK(dbi);
569 delmode = ISC_FALSE;
570 if (addmode)
571 addmode = ISC_FALSE;
572 else
573 addmode = ISC_TRUE;
574 printf("addmode = %s\n", addmode ? "TRUE" : "FALSE");
575 continue;
576 } else if (strcmp(s, "!D") == 0) {
577 DBI_CHECK(dbi);
578 addmode = ISC_FALSE;
579 if (delmode)
580 delmode = ISC_FALSE;
581 else
582 delmode = ISC_TRUE;
583 printf("delmode = %s\n", delmode ? "TRUE" : "FALSE");
584 continue;
585 } else if (strcmp(s, "!H") == 0) {
586 DBI_CHECK(dbi);
587 if (holdmode)
588 holdmode = ISC_FALSE;
589 else
590 holdmode = ISC_TRUE;
591 printf("holdmode = %s\n", holdmode ? "TRUE" : "FALSE");
592 continue;
593 } else if (strcmp(s, "!HR") == 0) {
594 DBI_CHECK(dbi);
595 for (i = 0; i < dbi->hold_count; i++)
596 dns_db_detachnode(dbi->db,
597 &dbi->hold_nodes[i]);
598 dbi->hold_count = 0;
599 holdmode = ISC_FALSE;
600 printf("held nodes have been detached\n");
601 continue;
602 } else if (strcmp(s, "!VC") == 0) {
603 DBI_CHECK(dbi);
604 printf("switching to current version\n");
605 dbi->version = NULL;
606 version = NULL;
607 continue;
608 } else if (strstr(s, "!V") == s) {
609 DBI_CHECK(dbi);
610 v = atoi(&s[2]);
611 if (v >= dbi->rcount) {
612 printf("unknown open version %d\n", v);
613 continue;
614 } else if (dbi->rversions[v] == NULL) {
615 printf("version %d is not open\n", v);
616 continue;
618 printf("switching to open version %d\n", v);
619 dbi->version = dbi->rversions[v];
620 version = dbi->version;
621 continue;
622 } else if (strstr(s, "!TR") == s) {
623 trust = (unsigned int)atoi(&s[3]);
624 printf("trust level is now %u\n", (unsigned int)trust);
625 continue;
626 } else if (strstr(s, "!T") == s) {
627 type = (unsigned int)atoi(&s[2]);
628 printf("now searching for type %u\n", type);
629 continue;
630 } else if (strcmp(s, "!G") == 0) {
631 if ((options & DNS_DBFIND_GLUEOK) != 0)
632 options &= ~DNS_DBFIND_GLUEOK;
633 else
634 options |= DNS_DBFIND_GLUEOK;
635 printf("glue ok = %s\n",
636 ((options & DNS_DBFIND_GLUEOK) != 0) ?
637 "TRUE" : "FALSE");
638 continue;
639 } else if (strcmp(s, "!GV") == 0) {
640 if ((options & DNS_DBFIND_VALIDATEGLUE) != 0)
641 options &= ~DNS_DBFIND_VALIDATEGLUE;
642 else
643 options |= DNS_DBFIND_VALIDATEGLUE;
644 printf("validate glue = %s\n",
645 ((options & DNS_DBFIND_VALIDATEGLUE) != 0) ?
646 "TRUE" : "FALSE");
647 continue;
648 } else if (strcmp(s, "!WC") == 0) {
649 if ((options & DNS_DBFIND_NOWILD) != 0)
650 options &= ~DNS_DBFIND_NOWILD;
651 else
652 options |= DNS_DBFIND_NOWILD;
653 printf("wildcard matching = %s\n",
654 ((options & DNS_DBFIND_NOWILD) == 0) ?
655 "TRUE" : "FALSE");
656 continue;
657 } else if (strstr(s, "!LS ") == s) {
658 DBI_CHECK(dbi);
659 list(dbi, &s[4]);
660 continue;
661 } else if (strcmp(s, "!LS") == 0) {
662 DBI_CHECK(dbi);
663 list(dbi, NULL);
664 continue;
665 } else if (strstr(s, "!DU ") == s) {
666 DBI_CHECK(dbi);
667 result = dns_db_dump(dbi->db, dbi->version, s+4);
668 if (result != ISC_R_SUCCESS) {
669 printf("\n");
670 print_result("", result);
672 continue;
673 } else if (strcmp(s, "!PN") == 0) {
674 if (printnode)
675 printnode = ISC_FALSE;
676 else
677 printnode = ISC_TRUE;
678 printf("printnode = %s\n",
679 printnode ? "TRUE" : "FALSE");
680 continue;
681 } else if (strstr(s, "!P") == s) {
682 DBI_CHECK(dbi);
683 v = atoi(&s[2]);
684 dbi->pause_every = v;
685 continue;
686 } else if (strcmp(s, "!+") == 0) {
687 DBI_CHECK(dbi);
688 dbi->ascending = ISC_TRUE;
689 continue;
690 } else if (strcmp(s, "!-") == 0) {
691 DBI_CHECK(dbi);
692 dbi->ascending = ISC_FALSE;
693 continue;
694 } else if (strcmp(s, "!DB") == 0) {
695 dbi = NULL;
696 origin = dns_rootname;
697 version = NULL;
698 printf("now searching all databases\n");
699 continue;
700 } else if (strncmp(s, "!DB ", 4) == 0) {
701 dbi = select_db(s+4);
702 if (dbi != NULL) {
703 db = dbi->db;
704 origin = dns_db_origin(dbi->db);
705 version = dbi->version;
706 addmode = ISC_FALSE;
707 delmode = ISC_FALSE;
708 holdmode = ISC_FALSE;
709 } else {
710 db = NULL;
711 version = NULL;
712 origin = dns_rootname;
713 printf("database not found; "
714 "now searching all databases\n");
716 continue;
717 } else if (strcmp(s, "!ZC") == 0) {
718 if (find_zonecut)
719 find_zonecut = ISC_FALSE;
720 else
721 find_zonecut = ISC_TRUE;
722 printf("find_zonecut = %s\n",
723 find_zonecut ? "TRUE" : "FALSE");
724 continue;
725 } else if (strcmp(s, "!NZ") == 0) {
726 if (noexact_zonecut)
727 noexact_zonecut = ISC_FALSE;
728 else
729 noexact_zonecut = ISC_TRUE;
730 printf("noexact_zonecut = %s\n",
731 noexact_zonecut ? "TRUE" : "FALSE");
732 continue;
735 isc_buffer_init(&source, s, len);
736 isc_buffer_add(&source, len);
737 isc_buffer_init(&target, b, sizeof(b));
738 result = dns_name_fromtext(&name, &source, origin, 0, &target);
739 if (result != ISC_R_SUCCESS) {
740 print_result("bad name: ", result);
741 continue;
744 if (dbi == NULL) {
745 zcoptions = 0;
746 if (noexact_zonecut)
747 zcoptions |= DNS_DBTABLEFIND_NOEXACT;
748 db = NULL;
749 result = dns_dbtable_find(dbtable, &name, zcoptions,
750 &db);
751 if (result != ISC_R_SUCCESS &&
752 result != DNS_R_PARTIALMATCH) {
753 if (!quiet) {
754 printf("\n");
755 print_result("", result);
757 continue;
759 isc_buffer_init(&tb1, t1, sizeof(t1));
760 result = dns_name_totext(dns_db_origin(db), ISC_FALSE,
761 &tb1);
762 if (result != ISC_R_SUCCESS) {
763 printf("\n");
764 print_result("", result);
765 dns_db_detach(&db);
766 continue;
768 isc_buffer_usedregion(&tb1, &r1);
769 printf("\ndatabase = %.*s (%s)\n",
770 (int)r1.length, r1.base,
771 (dns_db_iszone(db)) ? "zone" : "cache");
773 node = NULL;
774 dns_rdataset_init(&rdataset);
775 dns_rdataset_init(&sigrdataset);
777 if (find_zonecut && dns_db_iscache(db)) {
778 zcoptions = options;
779 if (noexact_zonecut)
780 zcoptions |= DNS_DBFIND_NOEXACT;
781 result = dns_db_findzonecut(db, &name, zcoptions,
782 0, &node, fname,
783 &rdataset, &sigrdataset);
784 } else {
785 result = dns_db_find(db, &name, version, type,
786 options, 0, &node, fname,
787 &rdataset, &sigrdataset);
790 if (!quiet) {
791 if (dbi != NULL)
792 printf("\n");
793 print_result("", result);
796 found_as = ISC_FALSE;
797 switch (result) {
798 case ISC_R_SUCCESS:
799 case DNS_R_GLUE:
800 case DNS_R_CNAME:
801 case DNS_R_ZONECUT:
802 break;
803 case DNS_R_DNAME:
804 case DNS_R_DELEGATION:
805 found_as = ISC_TRUE;
806 break;
807 case DNS_R_NXRRSET:
808 if (dns_rdataset_isassociated(&rdataset))
809 break;
810 if (dbi != NULL) {
811 if (holdmode) {
812 RUNTIME_CHECK(dbi->hold_count <
813 MAXHOLD);
814 dbi->hold_nodes[dbi->hold_count++] =
815 node;
816 node = NULL;
817 } else
818 dns_db_detachnode(db, &node);
819 } else {
820 dns_db_detachnode(db, &node);
821 dns_db_detach(&db);
823 continue;
824 case DNS_R_NXDOMAIN:
825 if (dns_rdataset_isassociated(&rdataset))
826 break;
827 /* FALLTHROUGH */
828 default:
829 if (dbi == NULL)
830 dns_db_detach(&db);
831 if (quiet)
832 print_result("", result);
833 continue;
835 if (found_as && !quiet) {
836 isc_buffer_init(&tb1, t1, sizeof(t1));
837 isc_buffer_init(&tb2, t2, sizeof(t2));
838 result = dns_name_totext(&name, ISC_FALSE, &tb1);
839 if (result != ISC_R_SUCCESS) {
840 print_result("", result);
841 dns_db_detachnode(db, &node);
842 if (dbi == NULL)
843 dns_db_detach(&db);
844 continue;
846 result = dns_name_totext(fname, ISC_FALSE, &tb2);
847 if (result != ISC_R_SUCCESS) {
848 print_result("", result);
849 dns_db_detachnode(db, &node);
850 if (dbi == NULL)
851 dns_db_detach(&db);
852 continue;
854 isc_buffer_usedregion(&tb1, &r1);
855 isc_buffer_usedregion(&tb2, &r2);
856 printf("found %.*s as %.*s\n",
857 (int)r1.length, r1.base,
858 (int)r2.length, r2.base);
861 if (printnode)
862 dns_db_printnode(db, node, stdout);
864 if (!found_as && type == dns_rdatatype_any) {
865 rdsiter = NULL;
866 result = dns_db_allrdatasets(db, node, version, 0,
867 &rdsiter);
868 if (result == ISC_R_SUCCESS) {
869 if (!quiet)
870 print_rdatasets(fname, rdsiter);
871 dns_rdatasetiter_destroy(&rdsiter);
872 } else
873 print_result("", result);
874 } else {
875 if (!quiet)
876 print_rdataset(fname, &rdataset);
877 if (dns_rdataset_isassociated(&sigrdataset)) {
878 if (!quiet)
879 print_rdataset(fname, &sigrdataset);
880 dns_rdataset_disassociate(&sigrdataset);
882 if (dbi != NULL && addmode && !found_as) {
883 rdataset.ttl++;
884 rdataset.trust = trust;
885 if (dns_db_iszone(db))
886 addopts = DNS_DBADD_MERGE;
887 else
888 addopts = 0;
889 result = dns_db_addrdataset(db, node, version,
890 0, &rdataset,
891 addopts, NULL);
892 if (result != ISC_R_SUCCESS)
893 print_result("", result);
894 if (printnode)
895 dns_db_printnode(db, node, stdout);
896 } else if (dbi != NULL && delmode && !found_as) {
897 result = dns_db_deleterdataset(db, node,
898 version, type,
900 if (result != ISC_R_SUCCESS)
901 print_result("", result);
902 if (printnode)
903 dns_db_printnode(db, node, stdout);
905 dns_rdataset_disassociate(&rdataset);
908 if (dbi != NULL) {
909 if (holdmode) {
910 RUNTIME_CHECK(dbi->hold_count < MAXHOLD);
911 dbi->hold_nodes[dbi->hold_count++] = node;
912 node = NULL;
913 } else
914 dns_db_detachnode(db, &node);
915 } else {
916 dns_db_detachnode(db, &node);
917 dns_db_detach(&db);
921 if (time_lookups) {
922 isc_uint64_t usec;
924 TIME_NOW(&finish);
926 usec = isc_time_microdiff(&finish, &start);
928 printf("elapsed time: %lu.%06lu seconds\n",
929 (unsigned long)(usec / 1000000),
930 (unsigned long)(usec % 1000000));
933 unload_all();
935 dns_dbtable_detach(&dbtable);
937 if (lctx != NULL)
938 isc_log_destroy(&lctx);
940 if (!quiet)
941 isc_mem_stats(mctx, stdout);
943 return (0);