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

2427 lines
53 KiB
C
Raw Normal View History

1998-12-12 20:48:14 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* 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 https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1998-12-12 20:48:14 +00:00
*/
1998-12-12 19:25:20 +00:00
/*! \file */
2000-06-22 22:00:42 +00:00
1998-12-04 02:27:01 +00:00
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
2005-09-10 01:02:08 +00:00
#include <stdlib.h>
1998-12-04 02:27:01 +00:00
#include <isc/ascii.h>
2000-04-04 20:10:29 +00:00
#include <isc/buffer.h>
#include <isc/hash.h>
#include <isc/hex.h>
#include <isc/mem.h>
#include <isc/once.h>
#include <isc/print.h>
2015-12-09 19:07:20 +05:30
#include <isc/random.h>
#include <isc/result.h>
#include <isc/string.h>
#include <isc/thread.h>
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
1998-12-04 02:27:01 +00:00
1999-01-06 20:04:41 +00:00
#include <dns/compress.h>
#include <dns/fixedname.h>
#include <dns/name.h>
1998-12-04 02:27:01 +00:00
#define VALID_NAME(n) ISC_MAGIC_VALID(n, DNS_NAME_MAGIC)
1998-12-04 02:27:01 +00:00
typedef enum {
ft_init = 0,
ft_start,
ft_ordinary,
ft_initialescape,
ft_escape,
ft_escdecimal,
1999-01-27 23:39:40 +00:00
ft_at
} ft_state;
typedef enum { fw_start = 0, fw_ordinary, fw_newcurrent } fw_state;
1998-12-04 02:27:01 +00:00
#define INIT_OFFSETS(name, var, default_offsets) \
if ((name)->offsets != NULL) \
var = (name)->offsets; \
else \
var = (default_offsets);
#define SETUP_OFFSETS(name, var, default_offsets) \
2020-02-16 17:34:24 +01:00
if ((name)->offsets != NULL) { \
var = (name)->offsets; \
2020-02-16 17:34:24 +01:00
} else { \
var = (default_offsets); \
set_offsets(name, var, NULL); \
}
/*%
* Note: If additional attributes are added that should not be set for
* empty names, MAKE_EMPTY() must be changed so it clears them.
*/
#define MAKE_EMPTY(name) \
do { \
name->ndata = NULL; \
name->length = 0; \
name->labels = 0; \
name->attributes.absolute = false; \
} while (0);
/*%
* A name is "bindable" if it can be set to point to a new value, i.e.
* name->ndata and name->length may be changed.
*/
#define BINDABLE(name) (!name->attributes.readonly && !name->attributes.dynamic)
/*%
* Note that the name data must be a char array, not a string
* literal, to avoid compiler warnings about discarding
* the const attribute of a string.
*/
static unsigned char root_ndata[] = { "" };
static unsigned char root_offsets[] = { 0 };
static dns_name_t root = DNS_NAME_INITABSOLUTE(root_ndata, root_offsets);
const dns_name_t *dns_rootname = &root;
static unsigned char wild_ndata[] = { "\001*" };
static unsigned char wild_offsets[] = { 0 };
2020-02-13 14:44:37 -08:00
static dns_name_t const wild = DNS_NAME_INITNONABSOLUTE(wild_ndata,
wild_offsets);
1999-08-12 07:48:16 +00:00
const dns_name_t *dns_wildcardname = &wild;
1999-08-12 07:48:16 +00:00
/*
* dns_name_t to text post-conversion procedure.
*/
Complete rewrite the BIND 9 build system The rewrite of BIND 9 build system is a large work and cannot be reasonable split into separate merge requests. Addition of the automake has a positive effect on the readability and maintainability of the build system as it is more declarative, it allows conditional and we are able to drop all of the custom make code that BIND 9 developed over the years to overcome the deficiencies of autoconf + custom Makefile.in files. This squashed commit contains following changes: - conversion (or rather fresh rewrite) of all Makefile.in files to Makefile.am by using automake - the libtool is now properly integrated with automake (the way we used it was rather hackish as the only official way how to use libtool is via automake - the dynamic module loading was rewritten from a custom patchwork to libtool's libltdl (which includes the patchwork to support module loading on different systems internally) - conversion of the unit test executor from kyua to automake parallel driver - conversion of the system test executor from custom make/shell to automake parallel driver - The GSSAPI has been refactored, the custom SPNEGO on the basis that all major KRB5/GSSAPI (mit-krb5, heimdal and Windows) implementations support SPNEGO mechanism. - The various defunct tests from bin/tests have been removed: bin/tests/optional and bin/tests/pkcs11 - The text files generated from the MD files have been removed, the MarkDown has been designed to be readable by both humans and computers - The xsl header is now generated by a simple sed command instead of perl helper - The <irs/platform.h> header has been removed - cleanups of configure.ac script to make it more simpler, addition of multiple macros (there's still work to be done though) - the tarball can now be prepared with `make dist` - the system tests are partially able to run in oot build Here's a list of unfinished work that needs to be completed in subsequent merge requests: - `make distcheck` doesn't yet work (because of system tests oot run is not yet finished) - documentation is not yet built, there's a different merge request with docbook to sphinx-build rst conversion that needs to be rebased and adapted on top of the automake - msvc build is non functional yet and we need to decide whether we will just cross-compile bind9 using mingw-w64 or fix the msvc build - contributed dlz modules are not included neither in the autoconf nor automake
2018-08-07 16:46:53 +02:00
static thread_local dns_name_totextfilter_t *totext_filter_proc = NULL;
2020-02-14 08:14:03 +01:00
static void
set_offsets(const dns_name_t *name, unsigned char *offsets,
dns_name_t *set_name);
1998-12-04 02:27:01 +00:00
void
2020-02-13 14:44:37 -08:00
dns_name_init(dns_name_t *name, unsigned char *offsets) {
1998-12-04 02:27:01 +00:00
/*
* Initialize 'name'.
1998-12-04 02:27:01 +00:00
*/
DNS_NAME_INIT(name, offsets);
}
void
2020-02-13 14:44:37 -08:00
dns_name_reset(dns_name_t *name) {
REQUIRE(VALID_NAME(name));
REQUIRE(BINDABLE(name));
DNS_NAME_RESET(name);
}
void
2020-02-13 14:44:37 -08:00
dns_name_invalidate(dns_name_t *name) {
/*
* Make 'name' invalid.
*/
REQUIRE(VALID_NAME(name));
name->magic = 0;
1998-12-04 02:27:01 +00:00
name->ndata = NULL;
name->length = 0;
name->labels = 0;
name->attributes = (struct dns_name_attrs){};
name->offsets = NULL;
name->buffer = NULL;
ISC_LINK_INIT(name, link);
1998-12-04 02:27:01 +00:00
}
bool
2020-02-13 14:44:37 -08:00
dns_name_isvalid(const dns_name_t *name) {
unsigned char *ndata, *offsets;
2020-02-13 14:44:37 -08:00
unsigned int offset, count, length, nlabels;
if (!VALID_NAME(name)) {
return (false);
}
if (name->length > 255U || name->labels > 127U) {
return (false);
}
ndata = name->ndata;
length = name->length;
offsets = name->offsets;
offset = 0;
nlabels = 0;
while (offset != length) {
count = *ndata;
if (count > 63U) {
return (false);
}
if (offsets != NULL && offsets[nlabels] != offset) {
return (false);
}
nlabels++;
offset += count + 1;
ndata += count + 1;
if (offset > length) {
return (false);
}
if (count == 0) {
break;
}
}
if (nlabels != name->labels || offset != name->length) {
return (false);
}
return (true);
}
void
2020-02-13 14:44:37 -08:00
dns_name_setbuffer(dns_name_t *name, isc_buffer_t *buffer) {
/*
* Dedicate a buffer for use with 'name'.
*/
REQUIRE(VALID_NAME(name));
REQUIRE((buffer != NULL && name->buffer == NULL) || (buffer == NULL));
name->buffer = buffer;
}
bool
2020-02-13 14:44:37 -08:00
dns_name_hasbuffer(const dns_name_t *name) {
/*
* Does 'name' have a dedicated buffer?
*/
REQUIRE(VALID_NAME(name));
if (name->buffer != NULL) {
return (true);
}
return (false);
}
bool
2020-02-13 14:44:37 -08:00
dns_name_isabsolute(const dns_name_t *name) {
1998-12-04 02:27:01 +00:00
/*
* Does 'name' end in the root label?
*/
REQUIRE(VALID_NAME(name));
return name->attributes.absolute;
1998-12-04 02:27:01 +00:00
}
#define hyphenchar(c) ((c) == 0x2d)
2020-02-13 14:44:37 -08:00
#define asterchar(c) ((c) == 0x2a)
#define alphachar(c) \
(((c) >= 0x41 && (c) <= 0x5a) || ((c) >= 0x61 && (c) <= 0x7a))
2020-02-13 14:44:37 -08:00
#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
#define borderchar(c) (alphachar(c) || digitchar(c))
#define middlechar(c) (borderchar(c) || hyphenchar(c))
#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
bool
2020-02-13 14:44:37 -08:00
dns_name_ismailbox(const dns_name_t *name) {
unsigned char *ndata, ch;
2020-02-13 14:44:37 -08:00
unsigned int n;
bool first;
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
REQUIRE(name->attributes.absolute);
2008-04-01 23:47:10 +00:00
/*
* Root label.
*/
if (name->length == 1) {
return (true);
}
ndata = name->ndata;
n = *ndata++;
INSIST(n <= 63);
while (n--) {
ch = *ndata++;
if (!domainchar(ch)) {
return (false);
}
}
2008-04-01 23:47:10 +00:00
if (ndata == name->ndata + name->length) {
return (false);
}
/*
2021-05-04 17:21:52 +02:00
* RFC952/RFC1123 hostname.
*/
while (ndata < (name->ndata + name->length)) {
n = *ndata++;
INSIST(n <= 63);
first = true;
while (n--) {
ch = *ndata++;
if (first || n == 0) {
if (!borderchar(ch)) {
return (false);
}
} else {
if (!middlechar(ch)) {
return (false);
}
}
first = false;
}
}
return (true);
}
bool
2020-02-13 14:44:37 -08:00
dns_name_ishostname(const dns_name_t *name, bool wildcard) {
unsigned char *ndata, ch;
2020-02-13 14:44:37 -08:00
unsigned int n;
bool first;
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
REQUIRE(name->attributes.absolute);
2008-04-01 23:47:10 +00:00
/*
* Root label.
*/
if (name->length == 1) {
return (true);
}
/*
* Skip wildcard if this is a ownername.
*/
ndata = name->ndata;
if (wildcard && ndata[0] == 1 && ndata[1] == '*') {
ndata += 2;
}
/*
2021-05-04 17:21:52 +02:00
* RFC952/RFC1123 hostname.
*/
while (ndata < (name->ndata + name->length)) {
n = *ndata++;
INSIST(n <= 63);
first = true;
while (n--) {
ch = *ndata++;
if (first || n == 0) {
if (!borderchar(ch)) {
return (false);
}
} else {
if (!middlechar(ch)) {
return (false);
}
}
first = false;
}
}
return (true);
}
bool
2020-02-13 14:44:37 -08:00
dns_name_iswildcard(const dns_name_t *name) {
1999-08-12 01:29:03 +00:00
unsigned char *ndata;
/*
* Is 'name' a wildcard name?
*/
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
if (name->length >= 2) {
ndata = name->ndata;
if (ndata[0] == 1 && ndata[1] == '*') {
return (true);
}
1999-08-12 01:29:03 +00:00
}
return (false);
1999-08-12 01:29:03 +00:00
}
bool
2020-02-13 14:44:37 -08:00
dns_name_internalwildcard(const dns_name_t *name) {
unsigned char *ndata;
2020-02-13 14:44:37 -08:00
unsigned int count;
unsigned int label;
/*
* Does 'name' contain a internal wildcard?
*/
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
/*
* Skip first label.
*/
ndata = name->ndata;
count = *ndata++;
INSIST(count <= 63);
ndata += count;
label = 1;
/*
* Check all but the last of the remaining labels.
*/
while (label + 1 < name->labels) {
count = *ndata++;
INSIST(count <= 63);
if (count == 1 && *ndata == '*') {
return (true);
}
ndata += count;
label++;
}
return (false);
}
unsigned int
2020-02-13 14:44:37 -08:00
dns_name_hash(const dns_name_t *name, bool case_sensitive) {
2015-12-09 19:07:20 +05:30
unsigned int length;
/*
* Provide a hash value for 'name'.
*/
REQUIRE(VALID_NAME(name));
2019-05-09 15:50:57 +07:00
if (name->labels == 0) {
return (0);
2019-05-09 15:50:57 +07:00
}
2015-12-09 19:07:20 +05:30
length = name->length;
2019-05-09 15:50:57 +07:00
if (length > 16) {
2015-12-09 19:07:20 +05:30
length = 16;
2019-05-09 15:50:57 +07:00
}
2015-12-09 19:07:20 +05:30
return (isc_hash32(name->ndata, length, case_sensitive));
}
unsigned int
2020-02-13 14:44:37 -08:00
dns_name_fullhash(const dns_name_t *name, bool case_sensitive) {
/*
* Provide a hash value for 'name'.
*/
REQUIRE(VALID_NAME(name));
2019-05-09 15:50:57 +07:00
if (name->labels == 0) {
return (0);
2019-05-09 15:50:57 +07:00
}
return (isc_hash32(name->ndata, name->length, case_sensitive));
}
dns_namereln_t
dns_name_fullcompare(const dns_name_t *name1, const dns_name_t *name2,
2020-02-13 14:44:37 -08:00
int *orderp, unsigned int *nlabelsp) {
unsigned int l1, l2, l, count1, count2, count, nlabels;
int cdiff, ldiff, diff;
1998-12-04 02:27:01 +00:00
unsigned char *label1, *label2;
unsigned char *offsets1, *offsets2;
2020-02-13 14:44:37 -08:00
dns_offsets_t odata1, odata2;
dns_namereln_t namereln = dns_namereln_none;
1998-12-04 02:27:01 +00:00
/*
* Determine the relative ordering under the DNSSEC order relation of
* 'name1' and 'name2', and also determine the hierarchical
* relationship of the names.
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
1998-12-04 02:27:01 +00:00
*/
REQUIRE(VALID_NAME(name1));
REQUIRE(VALID_NAME(name2));
REQUIRE(orderp != NULL);
REQUIRE(nlabelsp != NULL);
/*
* Either name1 is absolute and name2 is absolute, or neither is.
*/
REQUIRE((name1->attributes.absolute) == (name2->attributes.absolute));
1998-12-04 02:27:01 +00:00
if (name1 == name2) {
*orderp = 0;
*nlabelsp = name1->labels;
return (dns_namereln_equal);
}
SETUP_OFFSETS(name1, offsets1, odata1);
SETUP_OFFSETS(name2, offsets2, odata2);
nlabels = 0;
1998-12-04 02:27:01 +00:00
l1 = name1->labels;
l2 = name2->labels;
if (l2 > l1) {
1998-12-04 02:27:01 +00:00
l = l1;
ldiff = 0 - (l2 - l1);
} else {
1998-12-04 02:27:01 +00:00
l = l2;
ldiff = l1 - l2;
}
1998-12-04 02:27:01 +00:00
2015-12-09 19:07:20 +05:30
offsets1 += l1;
offsets2 += l2;
while (l-- > 0) {
2015-12-09 19:07:20 +05:30
offsets1--;
offsets2--;
label1 = &name1->ndata[*offsets1];
label2 = &name2->ndata[*offsets2];
1998-12-04 02:27:01 +00:00
count1 = *label1++;
count2 = *label2++;
cdiff = (int)count1 - (int)count2;
if (cdiff < 0) {
count = count1;
} else {
count = count2;
}
diff = isc_ascii_lowercmp(label1, label2, count);
if (diff != 0) {
*orderp = diff;
goto done;
}
if (cdiff != 0) {
*orderp = cdiff;
goto done;
1998-12-04 02:27:01 +00:00
}
nlabels++;
1998-12-04 02:27:01 +00:00
}
*orderp = ldiff;
if (ldiff < 0) {
namereln = dns_namereln_contains;
} else if (ldiff > 0) {
namereln = dns_namereln_subdomain;
} else {
namereln = dns_namereln_equal;
}
2015-12-09 19:07:20 +05:30
*nlabelsp = nlabels;
return (namereln);
done:
*nlabelsp = nlabels;
if (nlabels > 0) {
namereln = dns_namereln_commonancestor;
}
return (namereln);
}
int
2020-02-13 14:44:37 -08:00
dns_name_compare(const dns_name_t *name1, const dns_name_t *name2) {
int order;
unsigned int nlabels;
/*
* Determine the relative ordering under the DNSSEC order relation of
* 'name1' and 'name2'.
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
*/
(void)dns_name_fullcompare(name1, name2, &order, &nlabels);
return (order);
1998-12-04 02:27:01 +00:00
}
bool
2020-02-13 14:44:37 -08:00
dns_name_equal(const dns_name_t *name1, const dns_name_t *name2) {
unsigned int length;
1999-05-18 22:05:40 +00:00
/*
* Are 'name1' and 'name2' equal?
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
*/
REQUIRE(VALID_NAME(name1));
REQUIRE(VALID_NAME(name2));
/*
* Either name1 is absolute and name2 is absolute, or neither is.
*/
REQUIRE((name1->attributes.absolute) == (name2->attributes.absolute));
1999-05-18 22:05:40 +00:00
if (name1 == name2) {
return (true);
}
length = name1->length;
if (length != name2->length) {
return (false);
}
1999-05-18 22:05:40 +00:00
/* label lengths are < 64 so tolower() does not affect them */
return (isc_ascii_lowerequal(name1->ndata, name2->ndata, length));
1999-05-18 22:05:40 +00:00
}
bool
2020-02-13 14:44:37 -08:00
dns_name_caseequal(const dns_name_t *name1, const dns_name_t *name2) {
/*
* Are 'name1' and 'name2' equal?
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
*/
REQUIRE(VALID_NAME(name1));
REQUIRE(VALID_NAME(name2));
/*
* Either name1 is absolute and name2 is absolute, or neither is.
*/
REQUIRE((name1->attributes.absolute) == (name2->attributes.absolute));
if (name1->length != name2->length) {
return (false);
}
if (memcmp(name1->ndata, name2->ndata, name1->length) != 0) {
return (false);
}
return (true);
}
int
2020-02-13 14:44:37 -08:00
dns_name_rdatacompare(const dns_name_t *name1, const dns_name_t *name2) {
/*
* Compare two absolute names as rdata.
*/
REQUIRE(VALID_NAME(name1));
REQUIRE(name1->labels > 0);
REQUIRE(name1->attributes.absolute);
REQUIRE(VALID_NAME(name2));
REQUIRE(name2->labels > 0);
REQUIRE(name2->attributes.absolute);
/* label lengths are < 64 so tolower() does not affect them */
return (isc_ascii_lowercmp(name1->ndata, name2->ndata,
ISC_MIN(name1->length, name2->length)));
}
bool
2020-02-13 14:44:37 -08:00
dns_name_issubdomain(const dns_name_t *name1, const dns_name_t *name2) {
int order;
unsigned int nlabels;
dns_namereln_t namereln;
1998-12-04 02:27:01 +00:00
/*
* Is 'name1' a subdomain of 'name2'?
*
* Note: It makes no sense for one of the names to be relative and the
* other absolute. If both names are relative, then to be meaningfully
* compared the caller must ensure that they are both relative to the
* same domain.
*/
namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
if (namereln == dns_namereln_subdomain ||
namereln == dns_namereln_equal) {
return (true);
}
1998-12-04 02:27:01 +00:00
return (false);
1998-12-04 02:27:01 +00:00
}
bool
2020-02-13 14:44:37 -08:00
dns_name_matcheswildcard(const dns_name_t *name, const dns_name_t *wname) {
int order;
unsigned int nlabels, labels;
2020-02-13 14:44:37 -08:00
dns_name_t tname;
2000-02-02 20:11:55 +00:00
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
2000-02-02 20:11:55 +00:00
REQUIRE(VALID_NAME(wname));
labels = wname->labels;
REQUIRE(labels > 0);
2000-02-02 20:11:55 +00:00
REQUIRE(dns_name_iswildcard(wname));
DNS_NAME_INIT(&tname, NULL);
2000-02-02 20:11:55 +00:00
dns_name_getlabelsequence(wname, 1, labels - 1, &tname);
if (dns_name_fullcompare(name, &tname, &order, &nlabels) ==
2020-02-13 14:44:37 -08:00
dns_namereln_subdomain)
{
return (true);
}
return (false);
2000-02-02 20:11:55 +00:00
}
1998-12-04 02:27:01 +00:00
unsigned int
2020-02-13 14:44:37 -08:00
dns_name_countlabels(const dns_name_t *name) {
1998-12-04 02:27:01 +00:00
/*
* How many labels does 'name' have?
*/
REQUIRE(VALID_NAME(name));
ENSURE(name->labels <= 128);
return (name->labels);
}
void
2020-02-13 14:44:37 -08:00
dns_name_getlabel(const dns_name_t *name, unsigned int n, dns_label_t *label) {
unsigned char *offsets;
2020-02-13 14:44:37 -08:00
dns_offsets_t odata;
1998-12-04 02:27:01 +00:00
/*
* Make 'label' refer to the 'n'th least significant label of 'name'.
*/
1998-12-04 02:27:01 +00:00
REQUIRE(VALID_NAME(name));
REQUIRE(name->labels > 0);
1998-12-04 02:27:01 +00:00
REQUIRE(n < name->labels);
REQUIRE(label != NULL);
SETUP_OFFSETS(name, offsets, odata);
label->base = &name->ndata[offsets[n]];
if (n == name->labels - 1) {
label->length = name->length - offsets[n];
} else {
label->length = offsets[n + 1] - offsets[n];
}
1998-12-04 02:27:01 +00:00
}
void
dns_name_getlabelsequence(const dns_name_t *source, unsigned int first,
2020-02-13 14:44:37 -08:00
unsigned int n, dns_name_t *target) {
unsigned char *p, l;
2020-02-13 14:44:37 -08:00
unsigned int firstoffset, endoffset;
unsigned int i;
1998-12-04 02:27:01 +00:00
/*
* Make 'target' refer to the 'n' labels including and following
* 'first' in 'source'.
*/
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(target));
REQUIRE(first <= source->labels);
REQUIRE(n <= source->labels - first); /* note first+n could overflow */
REQUIRE(BINDABLE(target));
1998-12-04 02:27:01 +00:00
p = source->ndata;
if (first == source->labels) {
firstoffset = source->length;
} else {
for (i = 0; i < first; i++) {
l = *p;
p += l + 1;
}
firstoffset = (unsigned int)(p - source->ndata);
}
if (first + n == source->labels) {
endoffset = source->length;
} else {
for (i = 0; i < n; i++) {
l = *p;
p += l + 1;
}
endoffset = (unsigned int)(p - source->ndata);
}
target->ndata = &source->ndata[firstoffset];
target->length = endoffset - firstoffset;
2008-04-01 23:47:10 +00:00
if (first + n == source->labels && n > 0 && source->attributes.absolute)
2020-02-13 14:44:37 -08:00
{
target->attributes.absolute = true;
} else {
target->attributes.absolute = false;
}
1998-12-04 02:27:01 +00:00
target->labels = n;
2000-04-26 23:22:53 +00:00
/*
* If source and target are the same, and we're making target
* a prefix of source, the offsets table is correct already
* so we don't need to call set_offsets().
*/
if (target->offsets != NULL && (target != source || first != 0)) {
set_offsets(target, target->offsets, NULL);
}
1998-12-04 02:27:01 +00:00
}
1999-04-24 02:03:07 +00:00
void
2020-02-13 14:44:37 -08:00
dns_name_clone(const dns_name_t *source, dns_name_t *target) {
1999-04-24 02:03:07 +00:00
/*
* Make 'target' refer to the same name as 'source'.
*/
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(target));
REQUIRE(BINDABLE(target));
1999-04-24 02:03:07 +00:00
target->ndata = source->ndata;
target->length = source->length;
target->labels = source->labels;
target->attributes = source->attributes;
target->attributes.readonly = false;
target->attributes.dynamic = false;
target->attributes.dynoffsets = false;
1999-04-24 02:03:07 +00:00
if (target->offsets != NULL && source->labels > 0) {
if (source->offsets != NULL) {
memmove(target->offsets, source->offsets,
source->labels);
} else {
set_offsets(target, target->offsets, NULL);
}
1999-04-24 02:03:07 +00:00
}
}
1998-12-04 02:27:01 +00:00
void
2020-02-13 14:44:37 -08:00
dns_name_fromregion(dns_name_t *name, const isc_region_t *r) {
unsigned char *offsets;
2020-02-13 14:44:37 -08:00
dns_offsets_t odata;
unsigned int len;
isc_region_t r2;
1998-12-04 02:27:01 +00:00
/*
* Make 'name' refer to region 'r'.
*/
REQUIRE(VALID_NAME(name));
1998-12-04 02:27:01 +00:00
REQUIRE(r != NULL);
REQUIRE(BINDABLE(name));
1998-12-04 02:27:01 +00:00
INIT_OFFSETS(name, offsets, odata);
if (name->buffer != NULL) {
isc_buffer_clear(name->buffer);
isc_buffer_availableregion(name->buffer, &r2);
len = (r->length < r2.length) ? r->length : r2.length;
if (len > DNS_NAME_MAXWIRE) {
len = DNS_NAME_MAXWIRE;
}
if (len != 0) {
memmove(r2.base, r->base, len);
}
name->ndata = r2.base;
name->length = len;
} else {
name->ndata = r->base;
name->length = (r->length <= DNS_NAME_MAXWIRE)
? r->length
: DNS_NAME_MAXWIRE;
}
1998-12-04 02:27:01 +00:00
if (r->length > 0) {
set_offsets(name, offsets, name);
} else {
1998-12-04 02:27:01 +00:00
name->labels = 0;
name->attributes.absolute = false;
}
if (name->buffer != NULL) {
isc_buffer_add(name->buffer, name->length);
}
1998-12-04 02:27:01 +00:00
}
void
2020-02-13 14:44:37 -08:00
dns_name_toregion(const dns_name_t *name, isc_region_t *r) {
1998-12-04 02:27:01 +00:00
/*
* Make 'r' refer to 'name'.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(r != NULL);
DNS_NAME_TOREGION(name, r);
1998-12-04 02:27:01 +00:00
}
isc_result_t
dns_name_fromtext(dns_name_t *name, isc_buffer_t *source,
const dns_name_t *origin, unsigned int options,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
unsigned char *ndata, *label = NULL;
2020-02-13 14:44:37 -08:00
char *tdata;
char c;
ft_state state;
unsigned int value = 0, count = 0;
unsigned int n1 = 0, n2 = 0;
unsigned int tlen, nrem, nused, digits = 0, labels, tused;
bool done;
unsigned char *offsets;
2020-02-13 14:44:37 -08:00
dns_offsets_t odata;
bool downcase;
1998-12-04 02:27:01 +00:00
/*
* Convert the textual representation of a DNS name at source
* into uncompressed wire form stored in target.
*
* Notes:
* Relative domain names will have 'origin' appended to them
* unless 'origin' is NULL, in which case relative domain names
* will remain relative.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(ISC_BUFFER_VALID(source));
REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) ||
(target == NULL && ISC_BUFFER_VALID(name->buffer)));
2008-04-01 23:47:10 +00:00
downcase = ((options & DNS_NAME_DOWNCASE) != 0);
if (target == NULL && name->buffer != NULL) {
target = name->buffer;
isc_buffer_clear(target);
}
REQUIRE(BINDABLE(name));
INIT_OFFSETS(name, offsets, odata);
offsets[0] = 0;
1998-12-04 02:27:01 +00:00
/*
* Make 'name' empty in case of failure.
1998-12-04 02:27:01 +00:00
*/
MAKE_EMPTY(name);
1998-12-04 02:27:01 +00:00
/*
* Set up the state machine.
*/
tdata = (char *)source->base + source->current;
tlen = isc_buffer_remaininglength(source);
tused = 0;
ndata = isc_buffer_used(target);
nrem = isc_buffer_availablelength(target);
if (nrem > 255) {
1998-12-04 02:27:01 +00:00
nrem = 255;
}
nused = 0;
1998-12-04 02:27:01 +00:00
labels = 0;
done = false;
state = ft_init;
1998-12-04 02:27:01 +00:00
while (nrem > 0 && tlen > 0 && !done) {
c = *tdata++;
tlen--;
tused++;
1998-12-04 02:27:01 +00:00
switch (state) {
case ft_init:
1998-12-04 02:27:01 +00:00
/*
* Is this the root name?
*/
if (c == '.') {
if (tlen != 0) {
1998-12-04 02:27:01 +00:00
return (DNS_R_EMPTYLABEL);
}
1998-12-04 02:27:01 +00:00
labels++;
*ndata++ = 0;
nrem--;
nused++;
done = true;
1998-12-04 02:27:01 +00:00
break;
}
if (c == '@' && tlen == 0) {
state = ft_at;
break;
1999-01-27 23:39:40 +00:00
}
FALLTHROUGH;
case ft_start:
1998-12-04 02:27:01 +00:00
label = ndata;
ndata++;
nrem--;
nused++;
1998-12-04 02:27:01 +00:00
count = 0;
if (c == '\\') {
state = ft_initialescape;
1998-12-04 02:27:01 +00:00
break;
}
state = ft_ordinary;
if (nrem == 0) {
return (ISC_R_NOSPACE);
}
FALLTHROUGH;
case ft_ordinary:
1998-12-04 02:27:01 +00:00
if (c == '.') {
if (count == 0) {
1998-12-04 02:27:01 +00:00
return (DNS_R_EMPTYLABEL);
}
1998-12-04 02:27:01 +00:00
*label = count;
labels++;
INSIST(labels <= 127);
offsets[labels] = nused;
1998-12-04 02:27:01 +00:00
if (tlen == 0) {
labels++;
*ndata++ = 0;
nrem--;
nused++;
done = true;
1998-12-04 02:27:01 +00:00
}
state = ft_start;
1998-12-04 02:27:01 +00:00
} else if (c == '\\') {
state = ft_escape;
1998-12-04 02:27:01 +00:00
} else {
if (count >= 63) {
1998-12-04 02:27:01 +00:00
return (DNS_R_LABELTOOLONG);
}
1998-12-04 02:27:01 +00:00
count++;
if (downcase) {
c = isc_ascii_tolower(c);
}
1998-12-04 02:27:01 +00:00
*ndata++ = c;
nrem--;
nused++;
1998-12-04 02:27:01 +00:00
}
break;
case ft_initialescape:
1998-12-04 02:27:01 +00:00
if (c == '[') {
/*
* This looks like a bitstring label, which
* was deprecated. Intentionally drop it.
*/
return (DNS_R_BADLABELTYPE);
1998-12-04 02:27:01 +00:00
}
state = ft_escape;
POST(state);
FALLTHROUGH;
case ft_escape:
if (!isdigit((unsigned char)c)) {
if (count >= 63) {
1998-12-04 02:27:01 +00:00
return (DNS_R_LABELTOOLONG);
}
1998-12-04 02:27:01 +00:00
count++;
if (downcase) {
c = isc_ascii_tolower(c);
}
1998-12-04 02:27:01 +00:00
*ndata++ = c;
nrem--;
nused++;
state = ft_ordinary;
1998-12-04 02:27:01 +00:00
break;
}
digits = 0;
value = 0;
state = ft_escdecimal;
FALLTHROUGH;
case ft_escdecimal:
if (!isdigit((unsigned char)c)) {
1998-12-04 02:27:01 +00:00
return (DNS_R_BADESCAPE);
}
value = 10 * value + c - '0';
1998-12-04 02:27:01 +00:00
digits++;
if (digits == 3) {
if (value > 255) {
1998-12-04 02:27:01 +00:00
return (DNS_R_BADESCAPE);
}
if (count >= 63) {
1998-12-04 02:27:01 +00:00
return (DNS_R_LABELTOOLONG);
}
1998-12-04 02:27:01 +00:00
count++;
if (downcase) {
value = isc_ascii_tolower(value);
}
1998-12-04 02:27:01 +00:00
*ndata++ = value;
nrem--;
nused++;
state = ft_ordinary;
1998-12-04 02:27:01 +00:00
}
break;
default:
FATAL_ERROR(__FILE__, __LINE__, "Unexpected state %d",
state);
/* Does not return. */
1998-12-04 02:27:01 +00:00
}
}
1998-12-04 02:27:01 +00:00
if (!done) {
if (nrem == 0) {
return (ISC_R_NOSPACE);
}
INSIST(tlen == 0);
if (state != ft_ordinary && state != ft_at) {
return (ISC_R_UNEXPECTEDEND);
}
if (state == ft_ordinary) {
INSIST(count != 0);
INSIST(label != NULL);
1998-12-04 02:27:01 +00:00
*label = count;
labels++;
INSIST(labels <= 127);
offsets[labels] = nused;
1998-12-04 02:27:01 +00:00
}
if (origin != NULL) {
if (nrem < origin->length) {
return (ISC_R_NOSPACE);
}
1998-12-04 02:27:01 +00:00
label = origin->ndata;
n1 = origin->length;
nrem -= n1;
POST(nrem);
1998-12-04 02:27:01 +00:00
while (n1 > 0) {
n2 = *label++;
INSIST(n2 <= 63); /* no bitstring support */
*ndata++ = n2;
n1 -= n2 + 1;
nused += n2 + 1;
while (n2 > 0) {
c = *label++;
if (downcase) {
c = isc_ascii_tolower(c);
}
*ndata++ = c;
n2--;
}
labels++;
if (n1 > 0) {
INSIST(labels <= 127);
offsets[labels] = nused;
}
1998-12-04 02:27:01 +00:00
}
if (origin->attributes.absolute) {
name->attributes.absolute = true;
}
}
} else {
name->attributes.absolute = true;
}
1998-12-04 02:27:01 +00:00
name->ndata = (unsigned char *)target->base + target->used;
1998-12-04 02:27:01 +00:00
name->labels = labels;
name->length = nused;
1998-12-04 02:27:01 +00:00
isc_buffer_forward(source, tused);
isc_buffer_add(target, name->length);
return (ISC_R_SUCCESS);
1998-12-04 02:27:01 +00:00
}
isc_result_t
dns_name_totext(const dns_name_t *name, bool omit_final_dot,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
unsigned int options = DNS_NAME_MASTERFILE;
if (omit_final_dot) {
options |= DNS_NAME_OMITFINALDOT;
}
return (dns_name_totext2(name, options, target));
}
isc_result_t
2020-02-13 14:44:37 -08:00
dns_name_toprincipal(const dns_name_t *name, isc_buffer_t *target) {
return (dns_name_totext2(name, DNS_NAME_OMITFINALDOT, target));
}
isc_result_t
dns_name_totext2(const dns_name_t *name, unsigned int options,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
1998-12-04 02:27:01 +00:00
unsigned char *ndata;
2020-02-13 14:44:37 -08:00
char *tdata;
unsigned int nlen, tlen;
unsigned char c;
unsigned int trem, count;
unsigned int labels;
bool saw_root = false;
unsigned int oused;
bool omit_final_dot = ((options & DNS_NAME_OMITFINALDOT) != 0);
1998-12-04 02:27:01 +00:00
/*
* This function assumes the name is in proper uncompressed
* wire format.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(ISC_BUFFER_VALID(target));
1998-12-04 02:27:01 +00:00
oused = target->used;
1998-12-04 02:27:01 +00:00
ndata = name->ndata;
nlen = name->length;
labels = name->labels;
tdata = isc_buffer_used(target);
tlen = isc_buffer_availablelength(target);
1998-12-04 02:27:01 +00:00
trem = tlen;
if (labels == 0 && nlen == 0) {
/*
* Special handling for an empty name.
*/
if (trem == 0) {
return (ISC_R_NOSPACE);
}
/*
* The names of these booleans are misleading in this case.
* This empty name is not necessarily from the root node of
* the DNS root zone, nor is a final dot going to be included.
* They need to be set this way, though, to keep the "@"
* from being trounced.
*/
saw_root = true;
omit_final_dot = false;
*tdata++ = '@';
trem--;
/*
* Skip the while() loop.
*/
1998-12-04 02:27:01 +00:00
nlen = 0;
} else if (nlen == 1 && labels == 1 && *ndata == '\0') {
/*
* Special handling for the root label.
*/
if (trem == 0) {
return (ISC_R_NOSPACE);
}
saw_root = true;
omit_final_dot = false;
1998-12-04 02:27:01 +00:00
*tdata++ = '.';
trem--;
/*
* Skip the while() loop.
*/
nlen = 0;
1998-12-04 02:27:01 +00:00
}
1998-12-04 02:27:01 +00:00
while (labels > 0 && nlen > 0 && trem > 0) {
labels--;
count = *ndata++;
nlen--;
if (count == 0) {
saw_root = true;
1998-12-04 02:27:01 +00:00
break;
}
if (count < 64) {
INSIST(nlen >= count);
while (count > 0) {
c = *ndata;
switch (c) {
/* Special modifiers in zone files. */
case 0x40: /* '@' */
case 0x24: /* '$' */
if ((options & DNS_NAME_MASTERFILE) ==
0) {
goto no_escape;
}
FALLTHROUGH;
1998-12-04 02:27:01 +00:00
case 0x22: /* '"' */
case 0x28: /* '(' */
case 0x29: /* ')' */
1998-12-04 02:27:01 +00:00
case 0x2E: /* '.' */
case 0x3B: /* ';' */
case 0x5C: /* '\\' */
if (trem < 2) {
return (ISC_R_NOSPACE);
}
1998-12-04 02:27:01 +00:00
*tdata++ = '\\';
*tdata++ = c;
ndata++;
trem -= 2;
nlen--;
break;
no_escape:
1998-12-04 02:27:01 +00:00
default:
if (c > 0x20 && c < 0x7f) {
if (trem == 0) {
return (ISC_R_NOSPACE);
}
1998-12-04 02:27:01 +00:00
*tdata++ = c;
ndata++;
trem--;
nlen--;
} else {
if (trem < 4) {
return (ISC_R_NOSPACE);
}
*tdata++ = 0x5c;
2020-02-13 14:44:37 -08:00
*tdata++ = 0x30 +
((c / 100) % 10);
*tdata++ = 0x30 +
((c / 10) % 10);
*tdata++ = 0x30 + (c % 10);
1998-12-04 02:27:01 +00:00
trem -= 4;
ndata++;
nlen--;
}
}
count--;
}
2003-10-17 03:46:46 +00:00
} else {
FATAL_ERROR(__FILE__, __LINE__,
"Unexpected label type %02x", count);
UNREACHABLE();
}
1998-12-04 02:27:01 +00:00
/*
* The following assumes names are absolute. If not, we
* fix things up later. Note that this means that in some
* cases one more byte of text buffer is required than is
* needed in the final output.
*/
if (trem == 0) {
return (ISC_R_NOSPACE);
}
1998-12-04 02:27:01 +00:00
*tdata++ = '.';
trem--;
}
if (nlen != 0 && trem == 0) {
return (ISC_R_NOSPACE);
}
if (!saw_root || omit_final_dot) {
1998-12-04 02:27:01 +00:00
trem++;
tdata--;
}
if (trem > 0) {
*tdata = 0;
}
isc_buffer_add(target, tlen - trem);
1998-12-04 02:27:01 +00:00
if (totext_filter_proc != NULL) {
return ((totext_filter_proc)(target, oused));
}
return (ISC_R_SUCCESS);
1998-12-04 02:27:01 +00:00
}
isc_result_t
dns_name_tofilenametext(const dns_name_t *name, bool omit_final_dot,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
unsigned char *ndata;
2020-02-13 14:44:37 -08:00
char *tdata;
unsigned int nlen, tlen;
unsigned char c;
unsigned int trem, count;
unsigned int labels;
/*
* This function assumes the name is in proper uncompressed
* wire format.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(name->attributes.absolute);
REQUIRE(ISC_BUFFER_VALID(target));
ndata = name->ndata;
nlen = name->length;
labels = name->labels;
tdata = isc_buffer_used(target);
tlen = isc_buffer_availablelength(target);
trem = tlen;
if (nlen == 1 && labels == 1 && *ndata == '\0') {
/*
* Special handling for the root label.
*/
if (trem == 0) {
return (ISC_R_NOSPACE);
}
omit_final_dot = false;
*tdata++ = '.';
trem--;
/*
* Skip the while() loop.
*/
nlen = 0;
}
while (labels > 0 && nlen > 0 && trem > 0) {
labels--;
count = *ndata++;
nlen--;
if (count == 0) {
break;
}
if (count < 64) {
INSIST(nlen >= count);
while (count > 0) {
c = *ndata;
if ((c >= 0x30 && c <= 0x39) || /* digit */
(c >= 0x41 && c <= 0x5A) || /* uppercase */
(c >= 0x61 && c <= 0x7A) || /* lowercase */
c == 0x2D || /* hyphen */
c == 0x5F) /* underscore */
{
if (trem == 0) {
return (ISC_R_NOSPACE);
}
/* downcase */
if (c >= 0x41 && c <= 0x5A) {
c += 0x20;
}
*tdata++ = c;
ndata++;
trem--;
nlen--;
} else {
if (trem < 4) {
return (ISC_R_NOSPACE);
}
snprintf(tdata, trem, "%%%02X", c);
tdata += 3;
trem -= 3;
ndata++;
nlen--;
}
count--;
}
} else {
FATAL_ERROR(__FILE__, __LINE__,
"Unexpected label type %02x", count);
UNREACHABLE();
}
/*
* The following assumes names are absolute. If not, we
* fix things up later. Note that this means that in some
* cases one more byte of text buffer is required than is
* needed in the final output.
*/
if (trem == 0) {
return (ISC_R_NOSPACE);
}
*tdata++ = '.';
trem--;
}
if (nlen != 0 && trem == 0) {
return (ISC_R_NOSPACE);
}
if (omit_final_dot) {
trem++;
}
isc_buffer_add(target, tlen - trem);
return (ISC_R_SUCCESS);
}
isc_result_t
dns_name_downcase(const dns_name_t *source, dns_name_t *name,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
unsigned char *ndata;
2020-02-13 14:44:37 -08:00
isc_buffer_t buffer;
/*
* Downcase 'source'.
*/
REQUIRE(VALID_NAME(source));
1999-08-20 17:01:06 +00:00
REQUIRE(VALID_NAME(name));
if (source == name) {
REQUIRE(!name->attributes.readonly);
isc_buffer_init(&buffer, source->ndata, source->length);
target = &buffer;
ndata = source->ndata;
} else {
REQUIRE(BINDABLE(name));
REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) ||
(target == NULL && ISC_BUFFER_VALID(name->buffer)));
if (target == NULL) {
target = name->buffer;
isc_buffer_clear(name->buffer);
}
ndata = (unsigned char *)target->base + target->used;
name->ndata = ndata;
}
1999-08-20 17:01:06 +00:00
if (source->length > (target->length - target->used)) {
MAKE_EMPTY(name);
return (ISC_R_NOSPACE);
}
1999-08-20 17:01:06 +00:00
/* label lengths are < 64 so tolower() does not affect them */
isc_ascii_lowercopy(ndata, source->ndata, source->length);
if (source != name) {
name->labels = source->labels;
name->length = source->length;
name->attributes = (struct dns_name_attrs){
.absolute = source->attributes.absolute
};
if (name->labels > 0 && name->offsets != NULL) {
set_offsets(name, name->offsets, NULL);
}
}
isc_buffer_add(target, name->length);
return (ISC_R_SUCCESS);
1999-08-20 17:01:06 +00:00
}
1998-12-04 02:27:01 +00:00
static void
set_offsets(const dns_name_t *name, unsigned char *offsets,
2020-02-13 14:44:37 -08:00
dns_name_t *set_name) {
unsigned int offset, count, length, nlabels;
1998-12-04 02:27:01 +00:00
unsigned char *ndata;
2020-02-13 14:44:37 -08:00
bool absolute;
1998-12-04 02:27:01 +00:00
ndata = name->ndata;
2000-04-26 23:22:53 +00:00
length = name->length;
1998-12-04 02:27:01 +00:00
offset = 0;
nlabels = 0;
absolute = false;
while (offset != length) {
1998-12-04 02:27:01 +00:00
INSIST(nlabels < 128);
offsets[nlabels++] = offset;
count = *ndata;
INSIST(count <= 63);
offset += count + 1;
ndata += count + 1;
INSIST(offset <= length);
if (count == 0) {
absolute = true;
break;
1998-12-04 02:27:01 +00:00
}
}
if (set_name != NULL) {
INSIST(set_name == name);
set_name->labels = nlabels;
set_name->length = offset;
set_name->attributes.absolute = absolute;
}
1998-12-04 02:27:01 +00:00
INSIST(nlabels == name->labels);
INSIST(offset == name->length);
1998-12-04 02:27:01 +00:00
}
isc_result_t
dns_name_fromwire(dns_name_t *name, isc_buffer_t *source, dns_decompress_t dctx,
unsigned int options, isc_buffer_t *target) {
unsigned char *cdata, *ndata;
2020-02-13 14:44:37 -08:00
unsigned int cused; /* Bytes of compressed name data used */
unsigned int nused, labels, n, nmax;
unsigned int current, new_current, biggest_pointer;
bool done;
fw_state state = fw_start;
unsigned int c;
unsigned char *offsets;
2020-02-13 14:44:37 -08:00
dns_offsets_t odata;
bool downcase;
bool seen_pointer;
/*
* Copy the possibly-compressed name at source into target,
* decompressing it. Loop prevention is performed by checking
* the new pointer against biggest_pointer.
*/
REQUIRE(VALID_NAME(name));
REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) ||
(target == NULL && ISC_BUFFER_VALID(name->buffer)));
downcase = ((options & DNS_NAME_DOWNCASE) != 0);
if (target == NULL && name->buffer != NULL) {
target = name->buffer;
isc_buffer_clear(target);
}
REQUIRE(BINDABLE(name));
INIT_OFFSETS(name, offsets, odata);
/*
* Make 'name' empty in case of failure.
*/
MAKE_EMPTY(name);
/*
* Initialize things to make the compiler happy; they're not required.
*/
n = 0;
new_current = 0;
/*
* Set up.
*/
labels = 0;
done = false;
ndata = isc_buffer_used(target);
nused = 0;
seen_pointer = false;
/*
* Find the maximum number of uncompressed target name
* bytes we are willing to generate. This is the smaller
* of the available target buffer length and the
* maximum legal domain name length (255).
*/
nmax = isc_buffer_availablelength(target);
if (nmax > DNS_NAME_MAXWIRE) {
nmax = DNS_NAME_MAXWIRE;
}
cdata = isc_buffer_current(source);
cused = 0;
current = source->current;
biggest_pointer = current;
/*
* Note: The following code is not optimized for speed, but
* rather for correctness. Speed will be addressed in the future.
*/
while (current < source->active && !done) {
c = *cdata++;
current++;
if (!seen_pointer) {
cused++;
}
switch (state) {
case fw_start:
if (c < 64) {
offsets[labels] = nused;
labels++;
if (nused + c + 1 > nmax) {
goto full;
}
nused += c + 1;
*ndata++ = c;
if (c == 0) {
done = true;
}
n = c;
state = fw_ordinary;
} else if (c >= 192) {
1999-01-06 20:04:41 +00:00
/*
* 14-bit compression pointer
1999-01-06 20:04:41 +00:00
*/
if (!dns_decompress_getpermitted(dctx)) {
1999-01-06 20:04:41 +00:00
return (DNS_R_DISALLOWED);
}
new_current = c & 0x3F;
state = fw_newcurrent;
} else {
return (DNS_R_BADLABELTYPE);
}
break;
case fw_ordinary:
if (downcase) {
c = isc_ascii_tolower(c);
}
*ndata++ = c;
n--;
if (n == 0) {
state = fw_start;
}
break;
case fw_newcurrent:
new_current *= 256;
new_current += c;
if (new_current >= biggest_pointer) {
1999-02-24 06:31:35 +00:00
return (DNS_R_BADPOINTER);
}
1999-02-24 06:31:35 +00:00
biggest_pointer = new_current;
current = new_current;
cdata = (unsigned char *)source->base + current;
seen_pointer = true;
1999-02-24 06:31:35 +00:00
state = fw_start;
break;
default:
FATAL_ERROR(__FILE__, __LINE__, "Unknown state %d",
state);
/* Does not return. */
}
}
if (!done) {
return (ISC_R_UNEXPECTEDEND);
}
name->ndata = (unsigned char *)target->base + target->used;
name->labels = labels;
name->length = nused;
name->attributes.absolute = true;
isc_buffer_forward(source, cused);
isc_buffer_add(target, name->length);
return (ISC_R_SUCCESS);
full:
if (nmax == DNS_NAME_MAXWIRE) {
/*
* The name did not fit even though we had a buffer
* big enough to fit a maximum-length name.
*/
return (DNS_R_NAMETOOLONG);
} else {
/*
* The name might fit if only the caller could give us a
* big enough buffer.
*/
return (ISC_R_NOSPACE);
}
}
isc_result_t
dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target) {
return (dns_name_towire2(name, cctx, target, NULL));
}
isc_result_t
dns_name_towire2(const dns_name_t *name, dns_compress_t *cctx,
2020-02-13 14:44:37 -08:00
isc_buffer_t *target, uint16_t *comp_offsetp) {
bool compress;
bool found;
uint16_t here; /* start of the name we are adding to the message */
uint16_t there; /* target of the compression pointer */
dns_name_t prefix;
dns_offsets_t clo;
2020-02-13 14:44:37 -08:00
dns_name_t clname;
1999-02-22 07:24:05 +00:00
/*
* Convert 'name' into wire format, compressing it as specified by the
* compression context 'cctx', and storing the result in 'target'.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(cctx != NULL);
REQUIRE(ISC_BUFFER_VALID(target));
compress = !name->attributes.nocompress &&
dns_compress_getpermitted(cctx);
/*
* If this exact name was already rendered before, and the
* offset of the previously rendered name is passed to us, write
* a compression pointer directly.
*/
if (comp_offsetp != NULL && *comp_offsetp < 0x4000 && compress) {
if (target->length - target->used < 2) {
return (ISC_R_NOSPACE);
}
here = *comp_offsetp;
isc_buffer_putuint16(target, here | 0xc000);
return (ISC_R_SUCCESS);
}
2000-04-26 23:22:53 +00:00
/*
* If 'name' doesn't have an offsets table, make a clone which
* has one.
*/
if (name->offsets == NULL) {
DNS_NAME_INIT(&clname, clo);
2000-04-26 23:22:53 +00:00
dns_name_clone(name, &clname);
name = &clname;
}
DNS_NAME_INIT(&prefix, NULL);
1999-02-22 07:24:05 +00:00
here = target->used; /*XXX*/
1999-02-22 07:24:05 +00:00
/*
* Never compress the root name.
*/
if (name->length == 1) {
found = false;
compress = false;
} else {
found = dns_compress_find(cctx, name, &prefix, &there);
}
1999-02-22 07:24:05 +00:00
/*
* If the offset does not fit in a 14 bit compression pointer,
* we're out of luck.
*/
if (found && there >= 0x4000) {
compress = false;
}
/*
* Will the compression pointer reduce the message size?
*/
if (found && (prefix.length + 2) >= name->length) {
compress = false;
}
if (found && compress) {
if (target->length - target->used < prefix.length) {
return (ISC_R_NOSPACE);
}
if (prefix.length != 0) {
unsigned char *base = target->base;
(void)memmove(base + target->used, prefix.ndata,
(size_t)prefix.length);
}
isc_buffer_add(target, prefix.length);
if (target->length - target->used < 2) {
return (ISC_R_NOSPACE);
}
isc_buffer_putuint16(target, there | 0xc000);
1999-02-22 07:24:05 +00:00
} else {
if (target->length - target->used < name->length) {
return (ISC_R_NOSPACE);
}
if (name->length != 0) {
unsigned char *base = target->base;
(void)memmove(base + target->used, name->ndata,
(size_t)name->length);
}
1999-02-22 07:24:05 +00:00
isc_buffer_add(target, name->length);
}
if (found && prefix.length == 0) {
here = there;
}
if (here >= 0x4000) {
return (ISC_R_SUCCESS);
}
if (found) {
dns_compress_add(cctx, name, &prefix, here);
} else {
dns_compress_add(cctx, name, name, here);
}
/*
* Don't set the offset of the previously rendered name if the
* compression has been disabled.
*/
if (compress && comp_offsetp != NULL) {
*comp_offsetp = here;
1999-02-22 07:24:05 +00:00
}
return (ISC_R_SUCCESS);
1999-02-22 07:24:05 +00:00
}
isc_result_t
dns_name_concatenate(const dns_name_t *prefix, const dns_name_t *suffix,
2020-02-13 14:44:37 -08:00
dns_name_t *name, isc_buffer_t *target) {
1999-03-11 00:44:17 +00:00
unsigned char *ndata, *offsets;
2020-02-13 14:44:37 -08:00
unsigned int nrem, labels, prefix_length, length;
bool copy_prefix = true;
bool copy_suffix = true;
bool absolute = false;
dns_name_t tmp_name;
dns_offsets_t odata;
/*
1999-03-11 00:44:17 +00:00
* Concatenate 'prefix' and 'suffix'.
*/
1999-02-22 07:24:05 +00:00
1999-03-11 00:44:17 +00:00
REQUIRE(prefix == NULL || VALID_NAME(prefix));
REQUIRE(suffix == NULL || VALID_NAME(suffix));
REQUIRE(name == NULL || VALID_NAME(name));
REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) ||
(target == NULL && name != NULL &&
ISC_BUFFER_VALID(name->buffer)));
if (prefix == NULL || prefix->labels == 0) {
copy_prefix = false;
}
if (suffix == NULL || suffix->labels == 0) {
copy_suffix = false;
}
if (copy_prefix && prefix->attributes.absolute) {
absolute = true;
1999-03-11 00:44:17 +00:00
REQUIRE(!copy_suffix);
}
if (name == NULL) {
DNS_NAME_INIT(&tmp_name, odata);
1999-03-11 00:44:17 +00:00
name = &tmp_name;
}
if (target == NULL) {
INSIST(name->buffer != NULL);
target = name->buffer;
1999-03-11 00:44:17 +00:00
isc_buffer_clear(name->buffer);
}
REQUIRE(BINDABLE(name));
1999-02-22 07:24:05 +00:00
/*
* Set up.
*/
1999-02-22 07:24:05 +00:00
nrem = target->length - target->used;
ndata = (unsigned char *)target->base + target->used;
if (nrem > DNS_NAME_MAXWIRE) {
nrem = DNS_NAME_MAXWIRE;
}
1999-03-11 00:44:17 +00:00
length = 0;
prefix_length = 0;
labels = 0;
if (copy_prefix) {
prefix_length = prefix->length;
length += prefix_length;
labels += prefix->labels;
}
if (copy_suffix) {
length += suffix->length;
labels += suffix->labels;
}
if (length > DNS_NAME_MAXWIRE) {
MAKE_EMPTY(name);
return (DNS_R_NAMETOOLONG);
}
1999-03-11 00:44:17 +00:00
if (length > nrem) {
MAKE_EMPTY(name);
return (ISC_R_NOSPACE);
1999-03-11 00:44:17 +00:00
}
if (copy_suffix) {
if (suffix->attributes.absolute) {
absolute = true;
}
memmove(ndata + prefix_length, suffix->ndata, suffix->length);
1999-03-11 00:44:17 +00:00
}
1999-02-22 07:24:05 +00:00
/*
1999-03-11 00:44:17 +00:00
* If 'prefix' and 'name' are the same object, and the object has
* a dedicated buffer, and we're using it, then we don't have to
* copy anything.
*/
if (copy_prefix && (prefix != name || prefix->buffer != target)) {
memmove(ndata, prefix->ndata, prefix_length);
}
1999-03-11 00:44:17 +00:00
name->ndata = ndata;
1999-02-22 07:24:05 +00:00
name->labels = labels;
1999-03-11 00:44:17 +00:00
name->length = length;
name->attributes.absolute = absolute;
1999-02-22 07:24:05 +00:00
if (name->labels > 0 && name->offsets != NULL) {
1999-03-11 00:44:17 +00:00
INIT_OFFSETS(name, offsets, odata);
set_offsets(name, offsets, NULL);
}
1999-02-22 07:24:05 +00:00
isc_buffer_add(target, name->length);
return (ISC_R_SUCCESS);
}
1999-04-23 04:58:43 +00:00
void
dns_name_split(const dns_name_t *name, unsigned int suffixlabels,
1999-04-23 04:58:43 +00:00
dns_name_t *prefix, dns_name_t *suffix)
{
unsigned int splitlabel;
1999-04-23 04:58:43 +00:00
REQUIRE(VALID_NAME(name));
REQUIRE(suffixlabels > 0);
REQUIRE(suffixlabels <= name->labels);
1999-04-23 04:58:43 +00:00
REQUIRE(prefix != NULL || suffix != NULL);
REQUIRE(prefix == NULL || (VALID_NAME(prefix) && BINDABLE(prefix)));
REQUIRE(suffix == NULL || (VALID_NAME(suffix) && BINDABLE(suffix)));
1999-04-23 04:58:43 +00:00
splitlabel = name->labels - suffixlabels;
2000-11-27 19:45:45 +00:00
if (prefix != NULL) {
dns_name_getlabelsequence(name, 0, splitlabel, prefix);
}
1999-04-23 04:58:43 +00:00
if (suffix != NULL) {
dns_name_getlabelsequence(name, splitlabel, suffixlabels,
suffix);
}
1999-04-23 04:58:43 +00:00
return;
}
void
2020-02-13 14:44:37 -08:00
dns_name_dup(const dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
1999-08-31 22:09:52 +00:00
/*
* Make 'target' a dynamically allocated copy of 'source'.
*/
1999-06-12 01:08:16 +00:00
REQUIRE(VALID_NAME(source));
REQUIRE(source->length > 0);
REQUIRE(VALID_NAME(target));
REQUIRE(BINDABLE(target));
1999-06-12 01:08:16 +00:00
/*
* Make 'target' empty in case of failure.
*/
MAKE_EMPTY(target);
1999-06-12 01:08:16 +00:00
target->ndata = isc_mem_get(mctx, source->length);
memmove(target->ndata, source->ndata, source->length);
1999-06-12 01:08:16 +00:00
target->length = source->length;
target->labels = source->labels;
target->attributes = (struct dns_name_attrs){ .dynamic = true };
target->attributes.absolute = source->attributes.absolute;
if (target->offsets != NULL) {
if (source->offsets != NULL) {
memmove(target->offsets, source->offsets,
source->labels);
} else {
set_offsets(target, target->offsets, NULL);
}
}
}
isc_result_t
dns_name_dupwithoffsets(const dns_name_t *source, isc_mem_t *mctx,
2020-02-13 14:44:37 -08:00
dns_name_t *target) {
/*
* Make 'target' a read-only dynamically allocated copy of 'source'.
* 'target' will also have a dynamically allocated offsets table.
*/
REQUIRE(VALID_NAME(source));
REQUIRE(source->length > 0);
REQUIRE(VALID_NAME(target));
REQUIRE(BINDABLE(target));
REQUIRE(target->offsets == NULL);
/*
* Make 'target' empty in case of failure.
*/
MAKE_EMPTY(target);
target->ndata = isc_mem_get(mctx, source->length + source->labels);
memmove(target->ndata, source->ndata, source->length);
target->length = source->length;
target->labels = source->labels;
target->attributes = (struct dns_name_attrs){ .dynamic = true,
.dynoffsets = true,
.readonly = true };
target->attributes.absolute = source->attributes.absolute;
target->offsets = target->ndata + source->length;
if (source->offsets != NULL) {
memmove(target->offsets, source->offsets, source->labels);
} else {
set_offsets(target, target->offsets, NULL);
}
1999-06-12 01:08:16 +00:00
return (ISC_R_SUCCESS);
1999-06-12 01:08:16 +00:00
}
void
2020-02-13 14:44:37 -08:00
dns_name_free(dns_name_t *name, isc_mem_t *mctx) {
size_t size;
1999-06-12 01:08:16 +00:00
/*
* Free 'name'.
*/
1999-08-31 22:09:52 +00:00
REQUIRE(VALID_NAME(name));
REQUIRE(name->attributes.dynamic);
1999-08-31 22:09:52 +00:00
size = name->length;
if (name->attributes.dynoffsets) {
size += name->labels;
}
isc_mem_put(mctx, name->ndata, size);
1999-06-12 01:08:16 +00:00
dns_name_invalidate(name);
}
1999-08-31 22:09:52 +00:00
isc_result_t
2020-02-13 14:44:37 -08:00
dns_name_digest(const dns_name_t *name, dns_digestfunc_t digest, void *arg) {
dns_name_t downname;
1999-08-31 22:09:52 +00:00
unsigned char data[256];
2020-02-13 14:44:37 -08:00
isc_buffer_t buffer;
isc_result_t result;
isc_region_t r;
1999-08-31 22:09:52 +00:00
/*
* Send 'name' in DNSSEC canonical form to 'digest'.
*/
REQUIRE(VALID_NAME(name));
REQUIRE(digest != NULL);
DNS_NAME_INIT(&downname, NULL);
isc_buffer_init(&buffer, data, sizeof(data));
1999-08-31 22:09:52 +00:00
result = dns_name_downcase(name, &downname, &buffer);
if (result != ISC_R_SUCCESS) {
1999-08-31 22:09:52 +00:00
return (result);
}
isc_buffer_usedregion(&buffer, &r);
1999-08-31 22:09:52 +00:00
return ((digest)(arg, &r));
}
1999-10-13 22:45:17 +00:00
bool
2020-02-13 14:44:37 -08:00
dns_name_dynamic(const dns_name_t *name) {
1999-10-13 22:45:17 +00:00
REQUIRE(VALID_NAME(name));
1999-10-17 19:22:50 +00:00
/*
* Returns whether there is dynamic memory associated with this name.
*/
return (name->attributes.dynamic);
1999-10-13 22:45:17 +00:00
}
1999-10-17 19:22:50 +00:00
isc_result_t
2020-02-13 14:44:37 -08:00
dns_name_print(const dns_name_t *name, FILE *stream) {
1999-10-17 19:22:50 +00:00
isc_result_t result;
isc_buffer_t b;
isc_region_t r;
2020-02-13 14:44:37 -08:00
char t[1024];
1999-10-17 19:22:50 +00:00
/*
* Print 'name' on 'stream'.
*/
REQUIRE(VALID_NAME(name));
isc_buffer_init(&b, t, sizeof(t));
result = dns_name_totext(name, false, &b);
if (result != ISC_R_SUCCESS) {
1999-10-17 19:22:50 +00:00
return (result);
}
isc_buffer_usedregion(&b, &r);
1999-10-17 19:22:50 +00:00
fprintf(stream, "%.*s", (int)r.length, (char *)r.base);
return (ISC_R_SUCCESS);
}
isc_result_t
2020-02-13 14:44:37 -08:00
dns_name_settotextfilter(dns_name_totextfilter_t *proc) {
2005-09-10 01:02:08 +00:00
/*
* If we already have been here set / clear as appropriate.
*/
if (totext_filter_proc != NULL && proc != NULL) {
if (totext_filter_proc == proc) {
return (ISC_R_SUCCESS);
}
2005-09-10 01:02:08 +00:00
}
if (proc == NULL && totext_filter_proc != NULL) {
totext_filter_proc = NULL;
return (ISC_R_SUCCESS);
2005-09-10 01:02:08 +00:00
}
2008-04-01 23:47:10 +00:00
totext_filter_proc = proc;
return (ISC_R_SUCCESS);
}
void
2020-02-13 14:44:37 -08:00
dns_name_format(const dns_name_t *name, char *cp, unsigned int size) {
isc_result_t result;
isc_buffer_t buf;
REQUIRE(size > 0);
/*
* Leave room for null termination after buffer.
*/
isc_buffer_init(&buf, cp, size - 1);
result = dns_name_totext(name, true, &buf);
if (result == ISC_R_SUCCESS) {
2019-08-08 13:52:44 +10:00
isc_buffer_putuint8(&buf, (uint8_t)'\0');
} else {
snprintf(cp, size, "<unknown>");
2019-08-08 13:52:44 +10:00
}
}
/*
* dns_name_tostring() -- similar to dns_name_format() but allocates its own
* memory.
*/
isc_result_t
2020-02-13 14:44:37 -08:00
dns_name_tostring(const dns_name_t *name, char **target, isc_mem_t *mctx) {
isc_result_t result;
isc_buffer_t buf;
isc_region_t reg;
2020-02-13 14:44:37 -08:00
char *p, txt[DNS_NAME_FORMATSIZE];
REQUIRE(VALID_NAME(name));
2009-03-11 23:47:35 +00:00
REQUIRE(target != NULL && *target == NULL);
isc_buffer_init(&buf, txt, sizeof(txt));
result = dns_name_totext(name, false, &buf);
if (result != ISC_R_SUCCESS) {
return (result);
}
isc_buffer_usedregion(&buf, &reg);
p = isc_mem_allocate(mctx, reg.length + 1);
memmove(p, (char *)reg.base, (int)reg.length);
p[reg.length] = '\0';
*target = p;
return (ISC_R_SUCCESS);
}
/*
* dns_name_fromstring() -- convert directly from a string to a name,
* allocating memory as needed
*/
isc_result_t
dns_name_fromstring(dns_name_t *target, const char *src, unsigned int options,
2020-02-13 14:44:37 -08:00
isc_mem_t *mctx) {
return (dns_name_fromstring2(target, src, dns_rootname, options, mctx));
}
isc_result_t
dns_name_fromstring2(dns_name_t *target, const char *src,
const dns_name_t *origin, unsigned int options,
2020-02-13 14:44:37 -08:00
isc_mem_t *mctx) {
isc_result_t result;
isc_buffer_t buf;
dns_fixedname_t fn;
2020-02-13 14:44:37 -08:00
dns_name_t *name;
REQUIRE(src != NULL);
isc_buffer_constinit(&buf, src, strlen(src));
isc_buffer_add(&buf, strlen(src));
if (BINDABLE(target) && target->buffer != NULL) {
name = target;
} else {
name = dns_fixedname_initname(&fn);
}
result = dns_name_fromtext(name, &buf, origin, options, NULL);
if (result != ISC_R_SUCCESS) {
return (result);
}
if (name != target) {
result = dns_name_dupwithoffsets(name, mctx, target);
}
return (result);
}
void
dns_name_copy(const dns_name_t *source, dns_name_t *dest) {
isc_buffer_t *target = NULL;
unsigned char *ndata = NULL;
REQUIRE(VALID_NAME(source));
REQUIRE(VALID_NAME(dest));
REQUIRE(BINDABLE(dest));
target = dest->buffer;
REQUIRE(target != NULL);
REQUIRE(target->length >= source->length);
isc_buffer_clear(target);
ndata = (unsigned char *)target->base;
dest->ndata = target->base;
if (source->length != 0) {
memmove(ndata, source->ndata, source->length);
}
dest->ndata = ndata;
dest->labels = source->labels;
dest->length = source->length;
dest->attributes.absolute = source->attributes.absolute;
if (dest->labels > 0 && dest->offsets != NULL) {
if (source->offsets != NULL && source->labels != 0) {
memmove(dest->offsets, source->offsets, source->labels);
} else {
set_offsets(dest, dest->offsets, NULL);
}
}
isc_buffer_add(target, dest->length);
}
/*
* Service Discovery Prefixes RFC 6763.
*/
static unsigned char b_dns_sd_udp_data[] = "\001b\007_dns-sd\004_udp";
static unsigned char b_dns_sd_udp_offsets[] = { 0, 2, 10 };
static unsigned char db_dns_sd_udp_data[] = "\002db\007_dns-sd\004_udp";
static unsigned char db_dns_sd_udp_offsets[] = { 0, 3, 11 };
static unsigned char r_dns_sd_udp_data[] = "\001r\007_dns-sd\004_udp";
static unsigned char r_dns_sd_udp_offsets[] = { 0, 2, 10 };
static unsigned char dr_dns_sd_udp_data[] = "\002dr\007_dns-sd\004_udp";
static unsigned char dr_dns_sd_udp_offsets[] = { 0, 3, 11 };
static unsigned char lb_dns_sd_udp_data[] = "\002lb\007_dns-sd\004_udp";
static unsigned char lb_dns_sd_udp_offsets[] = { 0, 3, 11 };
static dns_name_t const dns_sd[] = {
2017-11-14 14:48:41 +11:00
DNS_NAME_INITNONABSOLUTE(b_dns_sd_udp_data, b_dns_sd_udp_offsets),
DNS_NAME_INITNONABSOLUTE(db_dns_sd_udp_data, db_dns_sd_udp_offsets),
DNS_NAME_INITNONABSOLUTE(r_dns_sd_udp_data, r_dns_sd_udp_offsets),
DNS_NAME_INITNONABSOLUTE(dr_dns_sd_udp_data, dr_dns_sd_udp_offsets),
DNS_NAME_INITNONABSOLUTE(lb_dns_sd_udp_data, lb_dns_sd_udp_offsets)
};
bool
2020-02-13 14:44:37 -08:00
dns_name_isdnssd(const dns_name_t *name) {
size_t i;
dns_name_t prefix;
if (dns_name_countlabels(name) > 3U) {
dns_name_init(&prefix, NULL);
dns_name_getlabelsequence(name, 0, 3, &prefix);
for (i = 0; i < (sizeof(dns_sd) / sizeof(dns_sd[0])); i++) {
if (dns_name_equal(&prefix, &dns_sd[i])) {
return (true);
}
}
}
return (false);
}
static unsigned char inaddr10_offsets[] = { 0, 3, 11, 16 };
static unsigned char inaddr172_offsets[] = { 0, 3, 7, 15, 20 };
static unsigned char inaddr192_offsets[] = { 0, 4, 8, 16, 21 };
static unsigned char inaddr10[] = "\00210\007IN-ADDR\004ARPA";
static unsigned char inaddr16172[] = "\00216\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr17172[] = "\00217\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr18172[] = "\00218\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr19172[] = "\00219\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr20172[] = "\00220\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr21172[] = "\00221\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr22172[] = "\00222\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr23172[] = "\00223\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr24172[] = "\00224\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr25172[] = "\00225\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr26172[] = "\00226\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr27172[] = "\00227\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr28172[] = "\00228\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr29172[] = "\00229\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr30172[] = "\00230\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr31172[] = "\00231\003172\007IN-ADDR\004ARPA";
static unsigned char inaddr168192[] = "\003168\003192\007IN-ADDR\004ARPA";
static dns_name_t const rfc1918names[] = {
DNS_NAME_INITABSOLUTE(inaddr10, inaddr10_offsets),
DNS_NAME_INITABSOLUTE(inaddr16172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr17172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr18172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr19172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr20172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr21172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr22172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr23172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr24172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr25172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr26172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr27172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr28172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr29172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr30172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr31172, inaddr172_offsets),
DNS_NAME_INITABSOLUTE(inaddr168192, inaddr192_offsets)
};
bool
2020-02-13 14:44:37 -08:00
dns_name_isrfc1918(const dns_name_t *name) {
size_t i;
for (i = 0; i < (sizeof(rfc1918names) / sizeof(*rfc1918names)); i++) {
if (dns_name_issubdomain(name, &rfc1918names[i])) {
return (true);
}
}
return (false);
}
static unsigned char ulaoffsets[] = { 0, 2, 4, 8, 13 };
static unsigned char ip6fc[] = "\001c\001f\003ip6\004ARPA";
static unsigned char ip6fd[] = "\001d\001f\003ip6\004ARPA";
static dns_name_t const ulanames[] = { DNS_NAME_INITABSOLUTE(ip6fc, ulaoffsets),
DNS_NAME_INITABSOLUTE(ip6fd,
ulaoffsets) };
bool
2020-02-13 14:44:37 -08:00
dns_name_isula(const dns_name_t *name) {
size_t i;
for (i = 0; i < (sizeof(ulanames) / sizeof(*ulanames)); i++) {
if (dns_name_issubdomain(name, &ulanames[i])) {
return (true);
}
}
return (false);
}
bool
2020-02-13 14:44:37 -08:00
dns_name_istat(const dns_name_t *name) {
unsigned char len;
const unsigned char *ndata;
REQUIRE(VALID_NAME(name));
if (name->labels < 1) {
return (false);
}
ndata = name->ndata;
len = ndata[0];
INSIST(len <= name->length);
ndata++;
/*
* Is there at least one trust anchor reported and is the
2018-07-04 10:19:53 +10:00
* label length consistent with a trust-anchor-telemetry label.
*/
if ((len < 8) || (len - 3) % 5 != 0) {
return (false);
}
if (ndata[0] != '_' || isc_ascii_tolower(ndata[1]) != 't' ||
isc_ascii_tolower(ndata[2]) != 'a')
2020-02-13 14:44:37 -08:00
{
return (false);
}
ndata += 3;
len -= 3;
while (len > 0) {
INSIST(len >= 5);
if (ndata[0] != '-' || !isc_hex_char(ndata[1]) ||
!isc_hex_char(ndata[2]) || !isc_hex_char(ndata[3]) ||
!isc_hex_char(ndata[4]))
2020-02-13 14:44:37 -08:00
{
return (false);
}
ndata += 5;
len -= 5;
}
return (true);
}