2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-28 04:58:04 +00:00
bind/bin/tests/name_test.c

345 lines
8.0 KiB
C
Raw Normal View History

1998-12-12 20:48:14 +00:00
/*
* Copyright (C) 1998-2001, 2003-2005, 2007, 2009, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* 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/.
1998-12-12 20:48:14 +00:00
*/
1998-12-04 02:27:01 +00:00
2009-09-02 23:48:03 +00:00
/* $Id: name_test.c,v 1.43 2009/09/02 23:48:01 tbox Exp $ */
2000-06-22 22:00:42 +00:00
1998-12-12 19:25:20 +00:00
#include <config.h>
1998-12-04 02:27:01 +00:00
#include <stdlib.h>
#include <isc/commandline.h>
#include <isc/print.h>
#include <isc/string.h>
2000-04-28 02:12:16 +00:00
#include <isc/util.h>
1998-12-04 02:27:01 +00:00
1999-03-08 18:55:58 +00:00
#include <dns/fixedname.h>
#include <dns/result.h>
1998-12-04 02:27:01 +00:00
static void
1998-12-13 23:45:21 +00:00
print_wirename(isc_region_t *name) {
1998-12-04 02:27:01 +00:00
unsigned char *ccurr, *cend;
1999-03-11 00:45:03 +00:00
if (name->length == 0) {
printf("<empty wire name>\n");
return;
}
1998-12-04 02:27:01 +00:00
ccurr = name->base;
cend = ccurr + name->length;
while (ccurr != cend)
printf("%02x ", *ccurr++);
printf("\n");
}
static void
print_name(dns_name_t *name) {
isc_result_t result;
isc_buffer_t source;
isc_region_t r;
char s[1000];
isc_buffer_init(&source, s, sizeof(s));
if (dns_name_countlabels(name) > 0)
result = dns_name_totext(name, ISC_FALSE, &source);
else
result = ISC_R_SUCCESS;
if (result == ISC_R_SUCCESS) {
isc_buffer_usedregion(&source, &r);
if (r.length > 0)
printf("%.*s\n", (int)r.length, r.base);
else
printf("<empty text name>\n");
} else
printf("error: %s\n", dns_result_totext(result));
}
1998-12-04 02:27:01 +00:00
int
main(int argc, char *argv[]) {
char s[1000];
isc_result_t result;
1999-08-26 21:08:13 +00:00
dns_fixedname_t wname, wname2, oname, compname, downname;
1999-03-08 18:55:58 +00:00
isc_buffer_t source;
1999-01-06 05:42:42 +00:00
isc_region_t r;
dns_name_t *name, *comp, *down;
const dns_name_t *origin;
unsigned int downcase = 0;
1999-01-06 05:42:42 +00:00
size_t len;
1999-01-09 01:21:11 +00:00
isc_boolean_t quiet = ISC_FALSE;
1999-03-11 00:45:03 +00:00
isc_boolean_t concatenate = ISC_FALSE;
isc_boolean_t got_name = ISC_FALSE;
isc_boolean_t check_absolute = ISC_FALSE;
1999-08-12 07:52:09 +00:00
isc_boolean_t check_wildcard = ISC_FALSE;
1999-08-26 21:08:13 +00:00
isc_boolean_t test_downcase = ISC_FALSE;
isc_boolean_t inplace = ISC_FALSE;
isc_boolean_t want_split = ISC_FALSE;
unsigned int labels, split_label = 0;
dns_fixedname_t fprefix, fsuffix;
dns_name_t *prefix, *suffix;
1999-01-09 01:21:11 +00:00
int ch;
1998-12-04 02:27:01 +00:00
while ((ch = isc_commandline_parse(argc, argv, "acdiqs:w")) != -1) {
1999-01-09 01:21:11 +00:00
switch (ch) {
1999-03-11 00:45:03 +00:00
case 'a':
check_absolute = ISC_TRUE;
break;
case 'c':
concatenate = ISC_TRUE;
break;
1999-08-26 21:08:13 +00:00
case 'd':
test_downcase = ISC_TRUE;
break;
case 'i':
inplace = ISC_TRUE;
break;
1999-01-09 01:21:11 +00:00
case 'q':
quiet = ISC_TRUE;
break;
case 's':
want_split = ISC_TRUE;
split_label = atoi(isc_commandline_argument);
break;
1999-08-12 07:52:09 +00:00
case 'w':
check_wildcard = ISC_TRUE;
break;
1999-01-09 01:21:11 +00:00
}
}
argc -= isc_commandline_index;
argv += isc_commandline_index;
1998-12-04 02:27:01 +00:00
if (argc > 0) {
if (strcasecmp("none", argv[0]) == 0)
origin = NULL;
else {
1999-01-06 05:42:42 +00:00
len = strlen(argv[0]);
isc_buffer_init(&source, argv[0], len);
1999-01-06 05:42:42 +00:00
isc_buffer_add(&source, len);
1999-03-08 18:55:58 +00:00
dns_fixedname_init(&oname);
result = dns_name_fromtext(&oname.name, &source,
dns_rootname, 0, NULL);
1998-12-04 02:27:01 +00:00
if (result != 0) {
fprintf(stderr,
"dns_name_fromtext() failed: %d\n",
result);
exit(1);
}
origin = &oname.name;
1998-12-04 02:27:01 +00:00
}
1999-03-11 00:45:03 +00:00
} else if (concatenate)
origin = NULL;
else
1998-12-04 02:27:01 +00:00
origin = dns_rootname;
1999-05-18 22:06:27 +00:00
if (argc >= 1) {
2000-11-22 00:14:55 +00:00
if (strcasecmp("none", argv[1]) == 0)
1998-12-04 02:27:01 +00:00
comp = NULL;
else {
2000-11-22 00:14:55 +00:00
len = strlen(argv[1]);
isc_buffer_init(&source, argv[1], len);
1999-01-06 05:42:42 +00:00
isc_buffer_add(&source, len);
1999-03-08 18:55:58 +00:00
dns_fixedname_init(&compname);
comp = &compname.name;
result = dns_name_fromtext(comp, &source, origin,
0, NULL);
1998-12-04 02:27:01 +00:00
if (result != 0) {
fprintf(stderr,
"dns_name_fromtext() failed: %d\n",
result);
exit(1);
}
}
} else
comp = NULL;
1999-03-08 18:55:58 +00:00
dns_fixedname_init(&wname);
1999-04-27 00:05:33 +00:00
name = dns_fixedname_name(&wname);
1999-03-11 00:45:03 +00:00
dns_fixedname_init(&wname2);
2001-11-27 01:56:32 +00:00
while (fgets(s, sizeof(s), stdin) != NULL) {
1999-01-06 05:42:42 +00:00
len = strlen(s);
2005-03-16 22:22:31 +00:00
if (len > 0U && s[len - 1] == '\n') {
1999-04-27 00:05:33 +00:00
s[len - 1] = '\0';
len--;
}
isc_buffer_init(&source, s, len);
1999-01-06 05:42:42 +00:00
isc_buffer_add(&source, len);
1999-03-11 00:45:03 +00:00
2005-03-16 22:22:31 +00:00
if (len > 0U)
1999-03-11 00:45:03 +00:00
result = dns_name_fromtext(name, &source, origin,
downcase, NULL);
else {
1999-04-27 00:05:33 +00:00
if (name == dns_fixedname_name(&wname))
1999-03-11 00:45:03 +00:00
dns_fixedname_init(&wname);
else
dns_fixedname_init(&wname2);
result = ISC_R_SUCCESS;
1999-03-11 00:45:03 +00:00
}
if (result != ISC_R_SUCCESS) {
1998-12-04 02:27:01 +00:00
printf("%s\n", dns_result_totext(result));
1999-04-27 00:05:33 +00:00
if (name == dns_fixedname_name(&wname))
dns_fixedname_init(&wname);
else
dns_fixedname_init(&wname2);
continue;
}
1999-04-27 00:05:33 +00:00
if (check_absolute && dns_name_countlabels(name) > 0) {
if (dns_name_isabsolute(name))
printf("absolute\n");
else
printf("relative\n");
}
1999-08-12 07:52:09 +00:00
if (check_wildcard && dns_name_countlabels(name) > 0) {
if (dns_name_iswildcard(name))
printf("wildcard\n");
else
printf("not wildcard\n");
}
1999-04-27 00:05:33 +00:00
dns_name_toregion(name, &r);
if (!quiet) {
print_wirename(&r);
printf("%u labels, %u bytes.\n",
dns_name_countlabels(name), r.length);
}
1998-12-04 02:27:01 +00:00
1999-04-27 00:05:33 +00:00
if (concatenate) {
if (got_name) {
printf("Concatenating.\n");
result = dns_name_concatenate(&wname.name,
1999-03-11 00:45:03 +00:00
&wname2.name,
&wname2.name,
NULL);
1999-04-27 00:05:33 +00:00
name = &wname2.name;
if (result == ISC_R_SUCCESS) {
1999-04-27 00:05:33 +00:00
if (check_absolute &&
1999-03-11 00:45:03 +00:00
dns_name_countlabels(name) > 0) {
1999-04-27 00:05:33 +00:00
if (dns_name_isabsolute(name))
printf("absolute\n");
else
printf("relative\n");
}
1999-08-12 07:52:09 +00:00
if (check_wildcard &&
dns_name_countlabels(name) > 0) {
if (dns_name_iswildcard(name))
printf("wildcard\n");
else
printf("not "
"wildcard\n");
}
1999-04-27 00:05:33 +00:00
dns_name_toregion(name, &r);
if (!quiet) {
print_wirename(&r);
printf("%u labels, "
"%u bytes.\n",
1999-03-11 00:45:03 +00:00
dns_name_countlabels(name),
1999-04-27 00:05:33 +00:00
r.length);
}
1999-03-11 00:45:03 +00:00
} else
1999-04-27 00:05:33 +00:00
printf("%s\n",
dns_result_totext(result));
got_name = ISC_FALSE;
1998-12-04 02:27:01 +00:00
} else
1999-04-27 00:05:33 +00:00
got_name = ISC_TRUE;
1998-12-04 02:27:01 +00:00
}
isc_buffer_init(&source, s, sizeof(s));
1999-04-27 00:05:33 +00:00
if (dns_name_countlabels(name) > 0)
result = dns_name_totext(name, ISC_FALSE, &source);
else
result = ISC_R_SUCCESS;
if (result == ISC_R_SUCCESS) {
isc_buffer_usedregion(&source, &r);
1999-04-27 00:05:33 +00:00
if (r.length > 0)
printf("%.*s\n", (int)r.length, r.base);
else
printf("<empty text name>\n");
if (!quiet) {
printf("%u bytes.\n", source.used);
}
} else
printf("%s\n", dns_result_totext(result));
1998-12-04 02:27:01 +00:00
1999-08-26 21:08:13 +00:00
if (test_downcase) {
if (inplace) {
down = name;
} else {
dns_fixedname_init(&downname);
down = dns_fixedname_name(&downname);
}
result = dns_name_downcase(name, down, NULL);
INSIST(result == ISC_R_SUCCESS);
if (!quiet) {
dns_name_toregion(down, &r);
print_wirename(&r);
printf("%u labels, %u bytes.\n",
dns_name_countlabels(down),
r.length);
}
isc_buffer_init(&source, s, sizeof(s));
print_name(down);
1999-08-26 21:08:13 +00:00
}
1999-03-11 00:45:03 +00:00
if (comp != NULL && dns_name_countlabels(name) > 0) {
1999-02-16 08:19:18 +00:00
int order;
unsigned int nlabels;
1999-02-16 08:19:18 +00:00
dns_namereln_t namereln;
1998-12-04 02:27:01 +00:00
1999-03-08 18:55:58 +00:00
namereln = dns_name_fullcompare(name, comp, &order,
&nlabels);
1999-01-09 01:21:11 +00:00
if (!quiet) {
1999-02-16 08:19:18 +00:00
if (order < 0)
printf("<");
else if (order > 0)
printf(">");
1999-01-09 01:21:11 +00:00
else
1999-02-16 08:19:18 +00:00
printf("=");
switch (namereln) {
case dns_namereln_contains:
printf(", contains");
break;
case dns_namereln_subdomain:
printf(", subdomain");
break;
case dns_namereln_commonancestor:
printf(", common ancestor");
break;
default:
break;
}
if (namereln != dns_namereln_none &&
namereln != dns_namereln_equal)
2004-04-13 03:31:14 +00:00
printf(", nlabels = %u", nlabels);
1999-02-16 08:19:18 +00:00
printf("\n");
1999-01-09 01:21:11 +00:00
}
1999-05-18 22:06:27 +00:00
printf("dns_name_equal() returns %s\n",
dns_name_equal(name, comp) ? "TRUE" : "FALSE");
1999-01-09 00:34:18 +00:00
}
1999-03-11 00:45:03 +00:00
labels = dns_name_countlabels(name);
if (want_split && split_label < labels) {
dns_fixedname_init(&fprefix);
prefix = dns_fixedname_name(&fprefix);
dns_fixedname_init(&fsuffix);
suffix = dns_fixedname_name(&fsuffix);
printf("splitting at label %u: ", split_label);
dns_name_split(name, split_label, prefix, suffix);
printf("\n prefix = ");
print_name(prefix);
printf(" suffix = ");
print_name(suffix);
}
1999-03-11 00:45:03 +00:00
if (concatenate) {
if (got_name)
name = &wname2.name;
else
name = &wname.name;
}
1998-12-04 02:27:01 +00:00
}
1998-12-04 02:27:01 +00:00
return (0);
}