2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 02:28:55 +00:00
bind/bin/tests/zone_test.c

317 lines
8.0 KiB
C
Raw Normal View History

1999-09-15 23:53:18 +00:00
/*
2012-10-29 23:46:34 +00:00
* Copyright (C) 2004, 2005, 2007, 2009, 2012 Internet Systems Consortium, Inc. ("ISC")
2004-03-05 05:14:21 +00:00
* Copyright (C) 1999-2002 Internet Software Consortium.
*
2007-06-18 23:47:57 +00:00
* Permission to use, copy, modify, and/or distribute this software for any
1999-09-15 23:53:18 +00:00
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 05:14:21 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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.
1999-09-15 23:53:18 +00:00
*/
2009-09-02 23:48:03 +00:00
/* $Id: zone_test.c,v 1.35 2009/09/02 23:48:01 tbox Exp $ */
2000-06-22 22:00:42 +00:00
#include <config.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <isc/app.h>
#include <isc/commandline.h>
#include <isc/mem.h>
2000-01-31 18:46:41 +00:00
#include <isc/socket.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/timer.h>
2000-04-28 02:12:16 +00:00
#include <isc/util.h>
#include <dns/db.h>
#include <dns/fixedname.h>
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/result.h>
#include <dns/zone.h>
#ifdef ISC_PLATFORM_NEEDSYSSELECTH
#include <sys/select.h>
#endif
static int debug = 0;
static int quiet = 0;
static int stats = 0;
static isc_mem_t *mctx = NULL;
dns_zone_t *zone = NULL;
isc_taskmgr_t *taskmgr = NULL;
isc_timermgr_t *timermgr = NULL;
isc_socketmgr_t *socketmgr = NULL;
dns_zonemgr_t *zonemgr = NULL;
dns_zonetype_t zonetype = dns_zone_master;
isc_sockaddr_t addr;
#define ERRRET(result, function) \
do { \
if (result != ISC_R_SUCCESS) { \
fprintf(stderr, "%s() returned %s\n", \
function, dns_result_totext(result)); \
return; \
} \
} while (0)
#define ERRCONT(result, function) \
if (result != ISC_R_SUCCESS) { \
fprintf(stderr, "%s() returned %s\n", \
function, dns_result_totext(result)); \
continue; \
} else \
(void)NULL
static void
usage() {
fprintf(stderr,
"usage: zone_test [-dqsSM] [-c class] [-f file] zone\n");
exit(1);
}
static void
2001-01-18 02:36:57 +00:00
setup(const char *zonename, const char *filename, const char *classname) {
isc_result_t result;
dns_rdataclass_t rdclass;
2001-01-18 02:36:57 +00:00
isc_consttextregion_t region;
isc_buffer_t buffer;
dns_fixedname_t fixorigin;
dns_name_t *origin;
2001-01-18 02:36:57 +00:00
const char *rbt = "rbt";
if (debug)
fprintf(stderr, "loading \"%s\" from \"%s\" class \"%s\"\n",
zonename, filename, classname);
result = dns_zone_create(&zone, mctx);
ERRRET(result, "dns_zone_new");
dns_zone_settype(zone, zonetype);
isc_buffer_constinit(&buffer, zonename, strlen(zonename));
isc_buffer_add(&buffer, strlen(zonename));
dns_fixedname_init(&fixorigin);
result = dns_name_fromtext(dns_fixedname_name(&fixorigin),
2009-09-02 23:48:03 +00:00
&buffer, dns_rootname, 0, NULL);
ERRRET(result, "dns_name_fromtext");
origin = dns_fixedname_name(&fixorigin);
result = dns_zone_setorigin(zone, origin);
ERRRET(result, "dns_zone_setorigin");
2001-01-18 02:36:57 +00:00
result = dns_zone_setdbtype(zone, 1, &rbt);
ERRRET(result, "dns_zone_setdatabase");
2001-01-18 02:36:57 +00:00
result = dns_zone_setfile(zone, filename);
ERRRET(result, "dns_zone_setfile");
region.base = classname;
region.length = strlen(classname);
2001-01-18 02:36:57 +00:00
result = dns_rdataclass_fromtext(&rdclass,
2005-03-16 22:22:31 +00:00
(isc_textregion_t *)(void*)&region);
ERRRET(result, "dns_rdataclass_fromtext");
dns_zone_setclass(zone, rdclass);
2000-04-24 22:47:14 +00:00
if (zonetype == dns_zone_slave)
dns_zone_setmasters(zone, &addr, 1);
result = dns_zone_load(zone);
ERRRET(result, "dns_zone_load");
result = dns_zonemgr_managezone(zonemgr, zone);
ERRRET(result, "dns_zonemgr_managezone");
}
static void
print_rdataset(dns_name_t *name, dns_rdataset_t *rdataset) {
2009-09-02 23:48:03 +00:00
isc_buffer_t text;
char t[1000];
isc_result_t result;
isc_region_t r;
2009-09-02 23:48:03 +00:00
isc_buffer_init(&text, t, sizeof(t));
result = dns_rdataset_totext(rdataset, name, ISC_FALSE, ISC_FALSE,
&text);
2009-09-02 23:48:03 +00:00
isc_buffer_usedregion(&text, &r);
if (result == ISC_R_SUCCESS)
printf("%.*s", (int)r.length, (char *)r.base);
else
printf("%s\n", dns_result_totext(result));
}
static void
query(void) {
char buf[1024];
dns_fixedname_t name;
dns_fixedname_t found;
dns_db_t *db;
char *s;
isc_buffer_t buffer;
isc_result_t result;
dns_rdataset_t rdataset;
dns_rdataset_t sigset;
fd_set rfdset;
1999-10-13 23:05:17 +00:00
db = NULL;
result = dns_zone_getdb(zone, &db);
if (result != ISC_R_SUCCESS) {
1999-10-13 23:05:17 +00:00
fprintf(stderr, "%s() returned %s\n", "dns_zone_getdb",
dns_result_totext(result));
return;
}
dns_fixedname_init(&found);
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);
2001-11-27 01:56:32 +00:00
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 (strcmp(buf, "dump") == 0) {
dns_zone_dumptostream(zone, stdout);
continue;
}
2005-03-16 22:22:31 +00:00
if (strlen(buf) == 0U)
continue;
dns_fixedname_init(&name);
isc_buffer_init(&buffer, buf, strlen(buf));
isc_buffer_add(&buffer, strlen(buf));
result = dns_name_fromtext(dns_fixedname_name(&name),
&buffer, dns_rootname, 0, NULL);
ERRCONT(result, "dns_name_fromtext");
result = dns_db_find(db, dns_fixedname_name(&name),
NULL /*vesion*/,
dns_rdatatype_a,
0 /*options*/,
0 /*time*/,
NULL /*nodep*/,
dns_fixedname_name(&found),
&rdataset, &sigset);
fprintf(stderr, "%s() returned %s\n", "dns_db_find",
dns_result_totext(result));
switch (result) {
case DNS_R_DELEGATION:
print_rdataset(dns_fixedname_name(&found), &rdataset);
break;
case ISC_R_SUCCESS:
print_rdataset(dns_fixedname_name(&name), &rdataset);
break;
default:
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
if (dns_rdataset_isassociated(&sigset))
dns_rdataset_disassociate(&sigset);
} while (1);
dns_rdataset_invalidate(&rdataset);
1999-10-13 23:05:17 +00:00
dns_db_detach(&db);
}
int
main(int argc, char **argv) {
int c;
char *filename = NULL;
2001-01-18 02:36:57 +00:00
const char *classname = "IN";
while ((c = isc_commandline_parse(argc, argv, "cdf:m:qsMS")) != EOF) {
switch (c) {
case 'c':
classname = isc_commandline_argument;
break;
case 'd':
debug++;
break;
case 'f':
if (filename != NULL)
usage();
filename = isc_commandline_argument;
break;
case 'm':
2001-11-27 01:56:32 +00:00
memset(&addr, 0, sizeof(addr));
addr.type.sin.sin_family = AF_INET;
3410. [bug] Addressed Coverity warnings. [RT #31626] Squashed commit of the following: commit bce2efe66d69d60b746b85df49974ca341723169 Author: Mark Andrews <marka@isc.org> Date: Mon Oct 29 12:59:25 2012 +1100 use 'static dns_rdata_xxxx_t xxxx' commit 704d3c29acbf2dd350a26f2df82a57cb077ba72e Author: Mark Andrews <marka@isc.org> Date: Mon Oct 29 12:35:16 2012 +1100 return ISC_R_NOTFOUND if private record length does not make sense commit 7596610c12c5685336fc0909860173d2fae359af Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 21:41:17 2012 +1100 check private->length == 5 commit 3836365a3e3e83b057bd940350f032279e080296 Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 21:40:50 2012 +1100 properly set private->length commit a295778ac53109d39ef3a8b233751100edae678b Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 21:13:30 2012 +1100 check dns_rdata_tostruct result commit e33c37ca9112159e0b2363615bb018d27fa7d1a5 Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 21:10:43 2012 +1100 check remove/fopen/chmod return values commit 3a675e0666aae25d1c51f51ec7bd3fbe25545aae Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:59:10 2012 +1100 check isc_socket_accept result commit 696923344f4b07ce0dba4cf2675b1cbb6eba7e8e Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:55:40 2012 +1100 change variable scopes commit b9e9d9ad58270271003e463f10744e0ceaf9ad97 Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:53:19 2012 +1100 check inet_pton return value commit 70698e9589da77e3745efb6ea24b8830addd6ae4 Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:52:40 2012 +1100 break -> /* NOTREACHED */ commit 88de9de2e8e201ab2fef16a868f241e8206ea826 Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:52:06 2012 +1100 strcpy -> strlcpy commit 6ba79c7cec0e48014cdfa76e8a9406b7a921556e Author: Mark Andrews <marka@isc.org> Date: Sat Oct 27 20:51:26 2012 +1100 check dns_rdata_tostruct return values
2012-10-29 20:04:59 +11:00
if (inet_pton(AF_INET, isc_commandline_argument,
&addr.type.sin.sin_addr) != 1) {
fprintf(stderr, "bad master address '%s'\n",
isc_commandline_argument);
exit(1);
}
addr.type.sin.sin_port = htons(53);
break;
case 'q':
quiet++;
break;
case 's':
stats++;
break;
case 'S':
zonetype = dns_zone_slave;
break;
case 'M':
zonetype = dns_zone_master;
break;
default:
usage();
}
}
if (argv[isc_commandline_index] == NULL)
usage();
RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_taskmgr_create(mctx, 2, 0, &taskmgr) ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
RUNTIME_CHECK(dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
&zonemgr) == ISC_R_SUCCESS);
if (filename == NULL)
filename = argv[isc_commandline_index];
setup(argv[isc_commandline_index], filename, classname);
query();
2001-01-18 02:53:57 +00:00
if (zone != NULL)
dns_zone_detach(&zone);
dns_zonemgr_shutdown(zonemgr);
dns_zonemgr_detach(&zonemgr);
isc_socketmgr_destroy(&socketmgr);
isc_taskmgr_destroy(&taskmgr);
2001-01-18 02:53:57 +00:00
isc_timermgr_destroy(&timermgr);
if (!quiet && stats)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
2000-02-02 01:02:59 +00:00
return (0);
}