1999-02-22 07:24:05 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-02-22 07:24:05 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-04-27 21:46:31 +00:00
|
|
|
|
|
|
|
#define DNS_NAME_USEINLINE 1
|
1999-02-22 07:24:05 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-02-22 07:24:05 +00:00
|
|
|
|
|
|
|
#include <dns/compress.h>
|
1999-04-14 06:03:15 +00:00
|
|
|
#include <dns/fixedname.h>
|
2000-04-12 21:25:10 +00:00
|
|
|
#include <dns/rbt.h>
|
1999-02-22 07:24:05 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define CCTX_MAGIC ISC_MAGIC('C', 'C', 'T', 'X')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_CCTX(x) ISC_MAGIC_VALID(x, CCTX_MAGIC)
|
1999-02-22 07:24:05 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define DCTX_MAGIC ISC_MAGIC('D', 'C', 'T', 'X')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_DCTX(x) ISC_MAGIC_VALID(x, DCTX_MAGIC)
|
1999-02-24 06:31:35 +00:00
|
|
|
|
2017-04-22 08:25:10 +05:30
|
|
|
static unsigned char maptolower[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
|
|
|
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
|
|
|
|
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
|
|
|
|
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
|
|
|
|
0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
|
|
|
|
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
|
|
|
|
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
|
|
|
|
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
|
|
|
|
0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
|
|
|
|
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
|
|
|
|
0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
|
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
|
|
|
|
0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
|
|
|
|
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
|
|
|
|
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
|
|
|
|
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
|
|
|
|
0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
|
|
|
|
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
|
|
|
|
0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
|
|
|
|
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
|
|
|
|
0xfc, 0xfd, 0xfe, 0xff
|
2017-04-22 08:25:10 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The tableindex array below is of size 256, one entry for each
|
|
|
|
* unsigned char value. The tableindex array elements are dependent on
|
|
|
|
* DNS_COMPRESS_TABLESIZE. The table was created using the following
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* static void
|
|
|
|
* gentable(unsigned char *table) {
|
|
|
|
* unsigned int i;
|
|
|
|
* const unsigned int left = DNS_COMPRESS_TABLESIZE - 38;
|
|
|
|
* long r;
|
|
|
|
*
|
|
|
|
* for (i = 0; i < 26; i++) {
|
|
|
|
* table['A' + i] = i;
|
|
|
|
* table['a' + i] = i;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* for (i = 0; i <= 9; i++)
|
|
|
|
* table['0' + i] = i + 26;
|
|
|
|
*
|
|
|
|
* table['-'] = 36;
|
|
|
|
* table['_'] = 37;
|
|
|
|
*
|
|
|
|
* for (i = 0; i < 256; i++) {
|
|
|
|
* if ((i >= 'a' && i <= 'z') ||
|
|
|
|
* (i >= 'A' && i <= 'Z') ||
|
|
|
|
* (i >= '0' && i <= '9') ||
|
|
|
|
* (i == '-') ||
|
|
|
|
* (i == '_'))
|
|
|
|
* continue;
|
|
|
|
* r = random() % left;
|
|
|
|
* table[i] = 38 + r;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static unsigned char tableindex[256] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
0x3e, 0x3e, 0x33, 0x2d, 0x30, 0x38, 0x31, 0x3c, 0x2b, 0x33, 0x30, 0x3f,
|
|
|
|
0x2d, 0x3c, 0x36, 0x3a, 0x28, 0x2c, 0x2a, 0x37, 0x3d, 0x34, 0x35, 0x2d,
|
|
|
|
0x39, 0x2b, 0x2f, 0x2c, 0x3b, 0x32, 0x2b, 0x39, 0x30, 0x38, 0x28, 0x3c,
|
|
|
|
0x32, 0x33, 0x39, 0x38, 0x27, 0x2b, 0x39, 0x30, 0x27, 0x24, 0x2f, 0x2b,
|
|
|
|
0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x3a, 0x29, 0x36,
|
|
|
|
0x31, 0x3c, 0x35, 0x26, 0x31, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
|
|
|
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
|
|
|
|
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x3e, 0x3b, 0x39, 0x2f, 0x25,
|
|
|
|
0x27, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
|
|
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
|
|
|
0x17, 0x18, 0x19, 0x36, 0x3b, 0x2f, 0x2f, 0x2e, 0x29, 0x33, 0x2a, 0x36,
|
|
|
|
0x28, 0x3f, 0x2e, 0x29, 0x2c, 0x29, 0x36, 0x2d, 0x32, 0x3d, 0x33, 0x2a,
|
|
|
|
0x2e, 0x2f, 0x3b, 0x30, 0x3d, 0x39, 0x2b, 0x36, 0x2a, 0x2f, 0x2c, 0x26,
|
|
|
|
0x3a, 0x37, 0x30, 0x3d, 0x2a, 0x36, 0x33, 0x2c, 0x38, 0x3d, 0x32, 0x3e,
|
|
|
|
0x26, 0x2a, 0x2c, 0x35, 0x27, 0x39, 0x3b, 0x31, 0x2a, 0x37, 0x3c, 0x27,
|
|
|
|
0x32, 0x29, 0x39, 0x37, 0x34, 0x3f, 0x39, 0x2e, 0x38, 0x2b, 0x2c, 0x3e,
|
|
|
|
0x3b, 0x3b, 0x2d, 0x33, 0x3b, 0x3b, 0x32, 0x3d, 0x3f, 0x3a, 0x34, 0x26,
|
|
|
|
0x35, 0x30, 0x31, 0x39, 0x27, 0x2f, 0x3d, 0x35, 0x35, 0x36, 0x2e, 0x29,
|
|
|
|
0x38, 0x27, 0x34, 0x32, 0x2c, 0x3c, 0x31, 0x28, 0x37, 0x38, 0x37, 0x34,
|
|
|
|
0x33, 0x29, 0x32, 0x34, 0x3f, 0x26, 0x34, 0x34, 0x32, 0x27, 0x30, 0x33,
|
|
|
|
0x33, 0x2d, 0x2b, 0x28, 0x3f, 0x33, 0x2b, 0x39, 0x37, 0x39, 0x2c, 0x3d,
|
|
|
|
0x35, 0x39, 0x27, 0x2f
|
2017-04-22 08:25:10 +05:30
|
|
|
};
|
2015-12-07 12:48:57 +05:30
|
|
|
|
1999-02-24 06:31:35 +00:00
|
|
|
/***
|
|
|
|
*** Compression
|
|
|
|
***/
|
1999-02-22 07:24:05 +00:00
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_init(dns_compress_t *cctx, int edns, isc_mem_t *mctx) {
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(cctx != NULL);
|
2020-02-12 13:59:18 +01:00
|
|
|
REQUIRE(mctx != NULL); /* See: rdataset.c:towiresorted(). */
|
1999-02-22 07:24:05 +00:00
|
|
|
|
|
|
|
cctx->edns = edns;
|
|
|
|
cctx->mctx = mctx;
|
2000-12-29 00:59:41 +00:00
|
|
|
cctx->count = 0;
|
2015-12-07 12:48:57 +05:30
|
|
|
cctx->allowed = DNS_COMPRESS_ENABLED;
|
2020-02-03 11:17:32 +01:00
|
|
|
cctx->arena_off = 0;
|
2017-04-22 08:25:10 +05:30
|
|
|
|
|
|
|
memset(&cctx->table[0], 0, sizeof(cctx->table));
|
|
|
|
|
1999-02-22 07:24:05 +00:00
|
|
|
cctx->magic = CCTX_MAGIC;
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_invalidate(dns_compress_t *cctx) {
|
2000-12-29 00:59:41 +00:00
|
|
|
dns_compressnode_t *node;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int i;
|
1999-02-22 07:24:05 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
|
2017-04-22 08:25:10 +05:30
|
|
|
for (i = 0; i < DNS_COMPRESS_TABLESIZE; i++) {
|
|
|
|
while (cctx->table[i] != NULL) {
|
|
|
|
node = cctx->table[i];
|
|
|
|
cctx->table[i] = cctx->table[i]->next;
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((node->offset & 0x8000) != 0) {
|
2017-04-22 08:25:10 +05:30
|
|
|
isc_mem_put(cctx->mctx, node->r.base,
|
|
|
|
node->r.length);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (node->count < DNS_COMPRESS_INITIALNODES) {
|
2017-04-22 08:25:10 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
isc_mem_put(cctx->mctx, node, sizeof(*node));
|
2000-12-29 00:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2015-12-07 12:48:57 +05:30
|
|
|
cctx->magic = 0;
|
1999-02-22 07:24:05 +00:00
|
|
|
cctx->allowed = 0;
|
|
|
|
cctx->edns = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_setmethods(dns_compress_t *cctx, unsigned int allowed) {
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
|
2005-03-04 02:56:21 +00:00
|
|
|
cctx->allowed &= ~DNS_COMPRESS_ALL;
|
|
|
|
cctx->allowed |= (allowed & DNS_COMPRESS_ALL);
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_getmethods(dns_compress_t *cctx) {
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
2005-03-04 02:56:21 +00:00
|
|
|
return (cctx->allowed & DNS_COMPRESS_ALL);
|
|
|
|
}
|
|
|
|
|
2015-11-05 12:19:04 +01:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_disable(dns_compress_t *cctx) {
|
2015-11-05 12:19:04 +01:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
2015-12-07 12:48:57 +05:30
|
|
|
cctx->allowed &= ~DNS_COMPRESS_ENABLED;
|
2015-11-05 12:19:04 +01:00
|
|
|
}
|
|
|
|
|
2005-03-04 02:56:21 +00:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_setsensitive(dns_compress_t *cctx, bool sensitive) {
|
2005-03-04 02:56:21 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sensitive) {
|
2005-03-04 02:56:21 +00:00
|
|
|
cctx->allowed |= DNS_COMPRESS_CASESENSITIVE;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2005-03-04 02:56:21 +00:00
|
|
|
cctx->allowed &= ~DNS_COMPRESS_CASESENSITIVE;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2005-03-04 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_getsensitive(dns_compress_t *cctx) {
|
2005-03-04 02:56:21 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (cctx->allowed & DNS_COMPRESS_CASESENSITIVE);
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_getedns(dns_compress_t *cctx) {
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
return (cctx->edns);
|
|
|
|
}
|
|
|
|
|
2000-12-29 19:38:50 +00:00
|
|
|
/*
|
|
|
|
* Find the longest match of name in the table.
|
2018-04-17 08:29:14 -07:00
|
|
|
* If match is found return true. prefix, suffix and offset are updated.
|
|
|
|
* If no match is found return false.
|
2000-12-29 19:38:50 +00:00
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2006-02-28 02:39:52 +00:00
|
|
|
dns_compress_findglobal(dns_compress_t *cctx, const dns_name_t *name,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *prefix, uint16_t *offset) {
|
|
|
|
dns_name_t tname;
|
2000-12-29 19:38:50 +00:00
|
|
|
dns_compressnode_t *node = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int labels, i, n;
|
|
|
|
unsigned int numlabels;
|
|
|
|
unsigned char *p;
|
2000-12-29 19:38:50 +00:00
|
|
|
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
2020-03-30 13:47:58 -07:00
|
|
|
REQUIRE(dns_name_isabsolute(name));
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(offset != NULL);
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((cctx->allowed & DNS_COMPRESS_ENABLED) == 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-11-05 12:19:04 +01:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (cctx->count == 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-02-10 01:18:19 +00:00
|
|
|
|
2000-12-29 19:38:50 +00:00
|
|
|
labels = dns_name_countlabels(name);
|
|
|
|
INSIST(labels > 0);
|
|
|
|
|
|
|
|
dns_name_init(&tname, NULL);
|
|
|
|
|
2017-04-22 08:25:10 +05:30
|
|
|
numlabels = labels > 3U ? 3U : labels;
|
|
|
|
p = name->ndata;
|
|
|
|
|
|
|
|
for (n = 0; n < numlabels - 1; n++) {
|
|
|
|
unsigned char ch, llen;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int firstoffset, length;
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2017-09-07 12:57:55 +10:00
|
|
|
firstoffset = (unsigned int)(p - name->ndata);
|
2017-04-22 08:25:10 +05:30
|
|
|
length = name->length - firstoffset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We calculate the table index using the first
|
|
|
|
* character in the first label of the suffix name.
|
|
|
|
*/
|
|
|
|
ch = p[1];
|
2017-05-03 12:36:54 -07:00
|
|
|
i = tableindex[ch];
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((cctx->allowed & DNS_COMPRESS_CASESENSITIVE) != 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
for (node = cctx->table[i]; node != NULL;
|
|
|
|
node = node->next) {
|
2021-10-14 10:33:24 +02:00
|
|
|
if (node->name.length != length) {
|
2017-04-22 08:25:10 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (memcmp(node->name.ndata, p, length) == 0) {
|
2017-04-22 08:25:10 +05:30
|
|
|
goto found;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
}
|
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
for (node = cctx->table[i]; node != NULL;
|
|
|
|
node = node->next) {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int l, count;
|
|
|
|
unsigned char c;
|
2017-04-22 08:25:10 +05:30
|
|
|
unsigned char *label1, *label2;
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if (node->name.length != length) {
|
2017-04-22 08:25:10 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
|
|
|
l = labels - n;
|
2021-10-14 10:33:24 +02:00
|
|
|
if (node->name.labels != l) {
|
2017-04-22 08:25:10 +05:30
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
|
|
|
label1 = node->name.ndata;
|
|
|
|
label2 = p;
|
2021-10-14 10:33:24 +02:00
|
|
|
while (l-- > 0) {
|
2017-04-22 08:25:10 +05:30
|
|
|
count = *label1++;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (count != *label2++) {
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
|
|
|
/* no bitstring support */
|
|
|
|
INSIST(count <= 63);
|
|
|
|
|
|
|
|
/* Loop unrolled for performance */
|
2021-10-14 10:33:24 +02:00
|
|
|
while (count > 3) {
|
2017-04-22 08:25:10 +05:30
|
|
|
c = maptolower[label1[0]];
|
2020-02-13 14:44:37 -08:00
|
|
|
if (c != maptolower[label2[0]])
|
|
|
|
{
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
c = maptolower[label1[1]];
|
2020-02-13 14:44:37 -08:00
|
|
|
if (c != maptolower[label2[1]])
|
|
|
|
{
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
c = maptolower[label1[2]];
|
2020-02-13 14:44:37 -08:00
|
|
|
if (c != maptolower[label2[2]])
|
|
|
|
{
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
c = maptolower[label1[3]];
|
2020-02-13 14:44:37 -08:00
|
|
|
if (c != maptolower[label2[3]])
|
|
|
|
{
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
count -= 4;
|
|
|
|
label1 += 4;
|
|
|
|
label2 += 4;
|
|
|
|
}
|
2021-10-14 10:33:24 +02:00
|
|
|
while (count-- > 0) {
|
2017-04-22 08:25:10 +05:30
|
|
|
c = maptolower[*label1++];
|
2020-02-13 14:44:37 -08:00
|
|
|
if (c != maptolower[*label2++])
|
|
|
|
{
|
2017-04-22 08:25:10 +05:30
|
|
|
goto cont1;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-02-12 13:59:18 +01:00
|
|
|
cont1:
|
2017-04-22 08:25:10 +05:30
|
|
|
continue;
|
2005-03-04 02:56:21 +00:00
|
|
|
}
|
2000-12-29 19:38:50 +00:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (node != NULL) {
|
2000-12-29 19:38:50 +00:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
|
|
|
llen = *p;
|
|
|
|
p += llen + 1;
|
2000-12-29 19:38:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
found:
|
2000-12-29 19:38:50 +00:00
|
|
|
/*
|
|
|
|
* If node == NULL, we found no match at all.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (node == NULL) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-29 19:38:50 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (n == 0) {
|
2001-02-10 02:55:05 +00:00
|
|
|
dns_name_reset(prefix);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2001-02-10 02:55:05 +00:00
|
|
|
dns_name_getlabelsequence(name, 0, n, prefix);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-02-12 18:07:52 +00:00
|
|
|
|
2015-02-27 15:08:38 +11:00
|
|
|
*offset = (node->offset & 0x7fff);
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
2001-01-23 02:19:48 +00:00
|
|
|
static inline unsigned int
|
2020-02-13 14:44:37 -08:00
|
|
|
name_length(const dns_name_t *name) {
|
2001-01-23 02:19:48 +00:00
|
|
|
isc_region_t r;
|
|
|
|
dns_name_toregion(name, &r);
|
|
|
|
return (r.length);
|
|
|
|
}
|
|
|
|
|
1999-02-22 07:24:05 +00:00
|
|
|
void
|
2006-02-28 02:39:52 +00:00
|
|
|
dns_compress_add(dns_compress_t *cctx, const dns_name_t *name,
|
2020-02-13 14:44:37 -08:00
|
|
|
const dns_name_t *prefix, uint16_t offset) {
|
|
|
|
dns_name_t tname, xname;
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int n;
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int i;
|
2000-12-29 19:38:50 +00:00
|
|
|
dns_compressnode_t *node;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int length;
|
|
|
|
unsigned int tlength;
|
|
|
|
uint16_t toffset;
|
|
|
|
unsigned char *tmp;
|
|
|
|
isc_region_t r;
|
2020-02-03 11:17:32 +01:00
|
|
|
bool allocated = false;
|
2000-12-29 19:38:50 +00:00
|
|
|
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
2000-12-29 00:59:41 +00:00
|
|
|
REQUIRE(dns_name_isabsolute(name));
|
1999-02-22 07:24:05 +00:00
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((cctx->allowed & DNS_COMPRESS_ENABLED) == 0) {
|
2015-11-05 12:19:04 +01:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-11-05 12:19:04 +01:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (offset >= 0x4000) {
|
2015-02-27 15:08:38 +11:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-29 19:38:50 +00:00
|
|
|
dns_name_init(&tname, NULL);
|
2015-02-27 15:08:38 +11:00
|
|
|
dns_name_init(&xname, NULL);
|
2000-12-29 19:38:50 +00:00
|
|
|
|
|
|
|
n = dns_name_countlabels(name);
|
2001-01-04 19:25:59 +00:00
|
|
|
count = dns_name_countlabels(prefix);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (dns_name_isabsolute(prefix)) {
|
2001-01-04 19:25:59 +00:00
|
|
|
count--;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (count == 0) {
|
2015-02-27 15:08:38 +11:00
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-29 19:38:50 +00:00
|
|
|
start = 0;
|
2015-02-27 15:08:38 +11:00
|
|
|
dns_name_toregion(name, &r);
|
|
|
|
length = r.length;
|
2020-02-03 11:17:32 +01:00
|
|
|
if (cctx->arena_off + length < DNS_COMPRESS_ARENA_SIZE) {
|
|
|
|
tmp = &cctx->arena[cctx->arena_off];
|
|
|
|
cctx->arena_off += length;
|
|
|
|
} else {
|
|
|
|
allocated = true;
|
|
|
|
tmp = isc_mem_get(cctx->mctx, length);
|
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
/*
|
|
|
|
* Copy name data to 'tmp' and make 'r' use 'tmp'.
|
|
|
|
*/
|
2015-02-27 15:08:38 +11:00
|
|
|
memmove(tmp, r.base, r.length);
|
|
|
|
r.base = tmp;
|
|
|
|
dns_name_fromregion(&xname, &r);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (count > 2U) {
|
2017-04-22 08:25:10 +05:30
|
|
|
count = 2U;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2001-01-04 19:25:59 +00:00
|
|
|
while (count > 0) {
|
2017-04-22 08:25:10 +05:30
|
|
|
unsigned char ch;
|
|
|
|
|
2015-02-27 15:08:38 +11:00
|
|
|
dns_name_getlabelsequence(&xname, start, n, &tname);
|
2017-04-22 08:25:10 +05:30
|
|
|
/*
|
|
|
|
* We calculate the table index using the first
|
|
|
|
* character in the first label of tname.
|
|
|
|
*/
|
|
|
|
ch = tname.ndata[1];
|
2017-05-03 12:36:54 -07:00
|
|
|
i = tableindex[ch];
|
2001-01-23 02:19:48 +00:00
|
|
|
tlength = name_length(&tname);
|
2018-03-28 14:19:37 +02:00
|
|
|
toffset = (uint16_t)(offset + (length - tlength));
|
2020-02-13 21:48:23 +01:00
|
|
|
if (toffset >= 0x4000) {
|
2015-08-17 22:41:44 -07:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-29 19:38:50 +00:00
|
|
|
/*
|
2001-02-09 02:13:07 +00:00
|
|
|
* Create a new node and add it.
|
2000-12-29 19:38:50 +00:00
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (cctx->count < DNS_COMPRESS_INITIALNODES) {
|
2000-12-29 19:38:50 +00:00
|
|
|
node = &cctx->initialnodes[cctx->count];
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2000-12-29 19:38:50 +00:00
|
|
|
node = isc_mem_get(cctx->mctx,
|
|
|
|
sizeof(dns_compressnode_t));
|
|
|
|
}
|
|
|
|
node->count = cctx->count++;
|
2015-08-17 22:41:44 -07:00
|
|
|
/*
|
|
|
|
* 'node->r.base' becomes 'tmp' when start == 0.
|
|
|
|
* Record this by setting 0x8000 so it can be freed later.
|
|
|
|
*/
|
2020-02-03 11:17:32 +01:00
|
|
|
if (start == 0 && allocated) {
|
2015-02-27 15:08:38 +11:00
|
|
|
toffset |= 0x8000;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-01-23 02:19:48 +00:00
|
|
|
node->offset = toffset;
|
2000-12-29 19:38:50 +00:00
|
|
|
dns_name_toregion(&tname, &node->r);
|
2017-04-22 08:25:10 +05:30
|
|
|
dns_name_init(&node->name, NULL);
|
|
|
|
node->name.length = node->r.length;
|
|
|
|
node->name.ndata = node->r.base;
|
|
|
|
node->name.labels = tname.labels;
|
|
|
|
node->name.attributes = DNS_NAMEATTR_ABSOLUTE;
|
2017-05-03 12:36:54 -07:00
|
|
|
node->next = cctx->table[i];
|
|
|
|
cctx->table[i] = node;
|
2000-12-29 19:38:50 +00:00
|
|
|
start++;
|
|
|
|
n--;
|
2001-01-04 19:25:59 +00:00
|
|
|
count--;
|
2000-12-29 19:38:50 +00:00
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (start == 0) {
|
2020-02-03 11:17:32 +01:00
|
|
|
if (!allocated) {
|
|
|
|
cctx->arena_off -= length;
|
|
|
|
} else {
|
|
|
|
isc_mem_put(cctx->mctx, tmp, length);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_compress_rollback(dns_compress_t *cctx, uint16_t offset) {
|
|
|
|
unsigned int i;
|
2000-12-29 00:59:41 +00:00
|
|
|
dns_compressnode_t *node;
|
1999-05-03 03:07:16 +00:00
|
|
|
|
1999-02-22 07:24:05 +00:00
|
|
|
REQUIRE(VALID_CCTX(cctx));
|
|
|
|
|
2021-10-14 10:33:24 +02:00
|
|
|
if ((cctx->allowed & DNS_COMPRESS_ENABLED) == 0) {
|
2015-12-07 12:48:57 +05:30
|
|
|
return;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2015-12-07 12:48:57 +05:30
|
|
|
|
2000-12-29 00:59:41 +00:00
|
|
|
for (i = 0; i < DNS_COMPRESS_TABLESIZE; i++) {
|
|
|
|
node = cctx->table[i];
|
2001-01-07 22:01:12 +00:00
|
|
|
/*
|
|
|
|
* This relies on nodes with greater offsets being
|
|
|
|
* closer to the beginning of the list, and the
|
2015-03-01 23:45:20 +00:00
|
|
|
* items with the greatest offsets being at the end
|
2001-01-07 22:01:12 +00:00
|
|
|
* of the initialnodes[] array.
|
|
|
|
*/
|
2015-02-27 15:08:38 +11:00
|
|
|
while (node != NULL && (node->offset & 0x7fff) >= offset) {
|
2000-12-29 00:59:41 +00:00
|
|
|
cctx->table[i] = node->next;
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((node->offset & 0x8000) != 0) {
|
2015-02-27 15:08:38 +11:00
|
|
|
isc_mem_put(cctx->mctx, node->r.base,
|
|
|
|
node->r.length);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (node->count >= DNS_COMPRESS_INITIALNODES) {
|
2001-01-07 22:01:12 +00:00
|
|
|
isc_mem_put(cctx->mctx, node, sizeof(*node));
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-12-29 00:59:41 +00:00
|
|
|
cctx->count--;
|
|
|
|
node = cctx->table[i];
|
1999-05-03 03:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
1999-02-22 07:24:05 +00:00
|
|
|
}
|
|
|
|
|
1999-02-24 06:31:35 +00:00
|
|
|
/***
|
|
|
|
*** Decompression
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_init(dns_decompress_t *dctx, int edns,
|
|
|
|
dns_decompresstype_t type) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(dctx != NULL);
|
|
|
|
REQUIRE(edns >= -1 && edns <= 255);
|
|
|
|
|
|
|
|
dctx->allowed = DNS_COMPRESS_NONE;
|
|
|
|
dctx->edns = edns;
|
2000-11-14 23:29:55 +00:00
|
|
|
dctx->type = type;
|
1999-02-24 06:31:35 +00:00
|
|
|
dctx->magic = DCTX_MAGIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_invalidate(dns_decompress_t *dctx) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(VALID_DCTX(dctx));
|
|
|
|
|
|
|
|
dctx->magic = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_setmethods(dns_decompress_t *dctx, unsigned int allowed) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(VALID_DCTX(dctx));
|
|
|
|
|
2000-11-14 23:29:55 +00:00
|
|
|
switch (dctx->type) {
|
|
|
|
case DNS_DECOMPRESS_ANY:
|
2000-05-04 22:19:34 +00:00
|
|
|
dctx->allowed = DNS_COMPRESS_ALL;
|
2000-11-14 23:29:55 +00:00
|
|
|
break;
|
|
|
|
case DNS_DECOMPRESS_NONE:
|
|
|
|
dctx->allowed = DNS_COMPRESS_NONE;
|
|
|
|
break;
|
|
|
|
case DNS_DECOMPRESS_STRICT:
|
|
|
|
dctx->allowed = allowed;
|
|
|
|
break;
|
|
|
|
}
|
1999-02-24 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_getmethods(dns_decompress_t *dctx) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(VALID_DCTX(dctx));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-02-24 06:31:35 +00:00
|
|
|
return (dctx->allowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_edns(dns_decompress_t *dctx) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(VALID_DCTX(dctx));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-02-24 06:31:35 +00:00
|
|
|
return (dctx->edns);
|
|
|
|
}
|
|
|
|
|
2000-11-14 23:29:55 +00:00
|
|
|
dns_decompresstype_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_decompress_type(dns_decompress_t *dctx) {
|
1999-02-24 06:31:35 +00:00
|
|
|
REQUIRE(VALID_DCTX(dctx));
|
|
|
|
|
2000-11-14 23:29:55 +00:00
|
|
|
return (dctx->type);
|
1999-02-24 06:31:35 +00:00
|
|
|
}
|