name_test.c revision 8afa9d1fcbf58f514e59a94f626e57d3f928f67d
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews/*
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Copyright (C) 1998 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Permission to use, copy, modify, and distribute this software for any
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * purpose with or without fee is hereby granted, provided that the above
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
fa5a42fbad42f4e033376d5d4624e29d018d97b7Brian Wellington#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <ctype.h>
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <stdio.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <stdlib.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <string.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/assertions.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <isc/boolean.h>
8c8360e5c3e4b27641633c22d0fcfd3f1f3af2b5Michael Graff#include <isc/region.h>
8c8360e5c3e4b27641633c22d0fcfd3f1f3af2b5Michael Graff
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/types.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/result.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/name.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graffstatic void
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halleyprint_wirename(isc_region_t *name) {
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff unsigned char *ccurr, *cend;
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff ccurr = name->base;
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff cend = ccurr + name->length;
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews while (ccurr != cend)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("%02x ", *ccurr++);
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews printf("\n");
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrewsint
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsmain(int argc, char *argv[]) {
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence char s[1000];
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence unsigned char b[255];
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence unsigned char o[255];
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley unsigned char c[255];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int tbytes;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence dns_name_t name, oname, compname;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence dns_textregion_t source, ttarget;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_region_t target, r;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_t *origin, *comp;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_boolean_t downcase = ISC_FALSE;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews argc--;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews argv++;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (argc > 0) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (strcasecmp("none", argv[0]) == 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews origin = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews source.base = argv[0];
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence source.length = strlen(argv[0]);
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence target.base = o;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews target.length = 255;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_name_fromtext(&oname, &source,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rootname, 0,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews &target);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != 0) {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence fprintf(stderr,
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence "dns_name_fromtext() failed: %d\n",
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence exit(1);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence origin = &oname;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence } else
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence origin = dns_rootname;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (argc > 1) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (strcasecmp("none", argv[0]) == 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews comp = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews source.base = argv[1];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews source.length = strlen(argv[1]);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews target.base = c;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews target.length = 255;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_name_fromtext(&compname, &source,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rootname, 0,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews &target);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != 0) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fprintf(stderr,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "dns_name_fromtext() failed: %d\n",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews exit(1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews comp = &compname;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews } else
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews comp = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (gets(s) != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews source.base = s;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews source.length = strlen(s);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews target.base = b;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews target.length = 255;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result = dns_name_fromtext(&name, &source, origin, downcase,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews &target);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result == DNS_R_SUCCESS) {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence dns_name_toregion(&name, &r);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence#ifndef QUIET
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence print_wirename(&r);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("%u labels, %u bytes.\n",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_countlabels(&name),
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews r.length);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("%s\n", dns_result_totext(result));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley if (result == 0) {
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley ttarget.base = s;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ttarget.length = sizeof s;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result = dns_name_totext(&name, 0, &ttarget, &tbytes);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (result == DNS_R_SUCCESS) {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence printf("%.*s\n", (int)tbytes, s);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#ifndef QUIET
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("%u bytes.\n", tbytes);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("%s\n", dns_result_totext(result));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#ifndef QUIET
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (comp != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews int i;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_boolean_t b;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence i = dns_name_compare(&name, comp);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence b = dns_name_issubdomain(&name, comp);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (i < 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("<, ");
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else if (i > 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf(">, ");
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence else
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence printf("=, ");
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (!b)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence printf("not ");
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews printf("subdomain\n");
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews