2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00

remove the 'name_coff' parameter in dns_name_towire()

this parameter was added as a (minor) optimization for
cases where dns_name_towire() is run repeatedly with the
same compression context, as when rendering all of the rdatas
in an rdataset. it is currently only used in one place.

we now simplify the interface by removing the extra parameter.
the compression offset value is now part of the compression
context, and can be activated when needed by calling
dns_compress_setmultiuse(). multiuse mode is automatically
deactivated by any subsequent call to dns_compress_permitted().
This commit is contained in:
Evan Hunt 2025-02-21 18:14:55 -08:00
parent 1d7a9ebeda
commit 2edefbad4a
41 changed files with 114 additions and 68 deletions

View File

@ -57,6 +57,7 @@ dns_compress_init(dns_compress_t *cctx, isc_mem_t *mctx,
.mctx = mctx,
.mask = mask,
.set = set,
.coff = 0xffff,
};
}
@ -69,6 +70,23 @@ dns_compress_invalidate(dns_compress_t *cctx) {
*cctx = (dns_compress_t){ 0 };
}
void
dns_compress_setmultiuse(dns_compress_t *cctx, bool multi) {
REQUIRE(CCTX_VALID(cctx));
if (multi) {
cctx->flags |= DNS_COMPRESS_MULTIUSE;
} else {
cctx->flags &= ~DNS_COMPRESS_MULTIUSE;
}
cctx->coff = 0xffff;
}
bool
dns_compress_getmultiuse(dns_compress_t *cctx) {
REQUIRE(CCTX_VALID(cctx));
return (cctx->flags & DNS_COMPRESS_MULTIUSE) != 0;
}
void
dns_compress_setpermitted(dns_compress_t *cctx, bool permitted) {
REQUIRE(CCTX_VALID(cctx));
@ -77,6 +95,7 @@ dns_compress_setpermitted(dns_compress_t *cctx, bool permitted) {
} else {
cctx->flags &= ~DNS_COMPRESS_PERMITTED;
}
dns_compress_setmultiuse(cctx, false);
}
bool

View File

@ -75,6 +75,7 @@ enum dns_compress_flags {
DNS_COMPRESS_LARGE = 0x00000004U,
/* can toggle while rendering a message */
DNS_COMPRESS_PERMITTED = 0x00000008U,
DNS_COMPRESS_MULTIUSE = 0x00000010U,
};
/*
@ -91,6 +92,7 @@ struct dns_compress {
dns_compress_flags_t flags;
uint16_t mask;
uint16_t count;
uint16_t coff;
isc_mem_t *mctx;
dns_compress_slot_t *set;
dns_compress_slot_t smallset[1 << DNS_COMPRESS_SMALLBITS];
@ -140,11 +142,36 @@ dns_compress_invalidate(dns_compress_t *cctx);
*/
void
dns_compress_setmultiuse(dns_compress_t *cctx, bool multi);
/*%<
* Indicates this compression context is to be used for
* multiple calls to dns_name_towire(), for example when
* rendering the rdata in an rdataset. This causes the
* compression offset to be reusable across calls.
*
* Requires:
*\li 'cctx' is not NULL.
*/
bool
dns_compress_getmultiuse(dns_compress_t *cctx);
/*%<
* Find out whether multiuse is enabled.
*
* Requires:
*\li 'cctx' to be initialized.
*
* Returns:
*\li allowed compression bitmap.
*/
void
dns_compress_setpermitted(dns_compress_t *cctx, bool permitted);
/*%<
* Sets whether compression is allowed, according to RFC 3597.
* This can vary depending on the rdata type.
* This can vary depending on the rdata type. This will also
* reset multiuse to false.
*
* Requires:
*\li 'cctx' to be initialized.

View File

@ -768,7 +768,7 @@ dns_name_fromwire(dns_name_t *name, isc_buffer_t *source, dns_decompress_t dctx,
isc_result_t
dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
isc_buffer_t *target, uint16_t *comp_offsetp);
isc_buffer_t *target);
/*%<
* Convert 'name' into wire format, compressing it as specified by the
* compression context 'cctx', and storing the result in 'target'.

View File

@ -1436,8 +1436,8 @@ root_label:;
isc_result_t
dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
isc_buffer_t *target, uint16_t *name_coff) {
bool compress;
isc_buffer_t *target) {
bool compress, multi;
unsigned int here;
unsigned int prefix_length;
unsigned int suffix_coff;
@ -1453,16 +1453,17 @@ dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
compress = !name->attributes.nocompress &&
dns_compress_getpermitted(cctx);
multi = compress && dns_compress_getmultiuse(cctx);
/*
* Write a compression pointer directly if the caller passed us
* a pointer to this name's offset that we saved previously.
*/
if (compress && name_coff != NULL && *name_coff < 0x4000) {
if (multi && cctx->coff < 0x4000) {
if (isc_buffer_availablelength(target) < 2) {
return ISC_R_NOSPACE;
}
isc_buffer_putuint16(target, *name_coff | 0xc000);
isc_buffer_putuint16(target, cctx->coff | 0xc000);
return ISC_R_SUCCESS;
}
@ -1483,8 +1484,8 @@ dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
* it isn't too short for compression to help (i.e. it's the root)
*/
here = isc_buffer_usedlength(target);
if (name_coff != NULL && here < 0x4000 && prefix_length > 1) {
*name_coff = (uint16_t)here;
if (multi && here < 0x4000 && prefix_length > 1) {
cctx->coff = (uint16_t)here;
}
if (prefix_length > 0) {
@ -1496,8 +1497,8 @@ dns_name_towire(const dns_name_t *name, dns_compress_t *cctx,
}
if (suffix_coff > 0) {
if (name_coff != NULL && prefix_length == 0) {
*name_coff = suffix_coff;
if (multi && prefix_length == 0) {
cctx->coff = suffix_coff;
}
if (isc_buffer_availablelength(target) < 2) {
return ISC_R_NOSPACE;

View File

@ -360,7 +360,7 @@ dns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
* Write the name.
*/
dns_compress_setpermitted(cctx, true);
result = dns_name_towire(&name, cctx, target, NULL);
result = dns_name_towire(&name, cctx, target);
if (result != ISC_R_SUCCESS) {
goto rollback;
}

View File

@ -335,7 +335,7 @@ towire_any_tsig(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &sr);
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
isc_region_consume(&sr, name_length(&name));
return mem_tobuffer(target, sr.base, sr.length);
}

View File

@ -146,7 +146,7 @@ towire_ch_a(ARGS_TOWIRE) {
dns_name_fromregion(&name, &sregion);
isc_region_consume(&sregion, name_length(&name));
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
isc_buffer_availableregion(target, &tregion);
if (tregion.length < 2) {

View File

@ -141,7 +141,7 @@ towire_afsdb(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -91,7 +91,7 @@ towire_cname(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -91,7 +91,7 @@ towire_dname(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -90,7 +90,7 @@ towire_mb(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -90,7 +90,7 @@ towire_md(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -90,7 +90,7 @@ towire_mf(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -90,7 +90,7 @@ towire_mg(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -129,12 +129,12 @@ towire_minfo(ARGS_TOWIRE) {
dns_name_fromregion(&rmail, &region);
isc_region_consume(&region, name_length(&rmail));
RETERR(dns_name_towire(&rmail, cctx, target, NULL));
RETERR(dns_name_towire(&rmail, cctx, target));
dns_name_fromregion(&rmail, &region);
isc_region_consume(&region, rmail.length);
return dns_name_towire(&rmail, cctx, target, NULL);
return dns_name_towire(&rmail, cctx, target);
}
static int

View File

@ -90,7 +90,7 @@ towire_mr(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -165,7 +165,7 @@ towire_mx(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -388,7 +388,7 @@ towire_naptr(ARGS_TOWIRE) {
*/
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -101,7 +101,7 @@ towire_ns(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -110,7 +110,7 @@ towire_nsec(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &sr);
dns_name_fromregion(&name, &sr);
isc_region_consume(&sr, name_length(&name));
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
return mem_tobuffer(target, sr.base, sr.length);
}

View File

@ -173,7 +173,7 @@ towire_nxt(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &sr);
dns_name_fromregion(&name, &sr);
isc_region_consume(&sr, name_length(&name));
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
return mem_tobuffer(target, sr.base, sr.length);
}

View File

@ -103,7 +103,7 @@ towire_ptr(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -130,12 +130,12 @@ towire_rp(ARGS_TOWIRE) {
dns_name_fromregion(&rmail, &region);
isc_region_consume(&region, rmail.length);
RETERR(dns_name_towire(&rmail, cctx, target, NULL));
RETERR(dns_name_towire(&rmail, cctx, target));
dns_name_fromregion(&rmail, &region);
isc_region_consume(&region, rmail.length);
return dns_name_towire(&rmail, cctx, target, NULL);
return dns_name_towire(&rmail, cctx, target);
}
static int

View File

@ -377,7 +377,7 @@ towire_rrsig(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
isc_region_consume(&sr, name_length(&name));
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
/*
* Signature.

View File

@ -138,7 +138,7 @@ towire_rt(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -341,7 +341,7 @@ towire_sig(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
isc_region_consume(&sr, name_length(&name));
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
/*
* Signature.

View File

@ -213,11 +213,11 @@ towire_soa(ARGS_TOWIRE) {
dns_name_fromregion(&mname, &sregion);
isc_region_consume(&sregion, name_length(&mname));
RETERR(dns_name_towire(&mname, cctx, target, NULL));
RETERR(dns_name_towire(&mname, cctx, target));
dns_name_fromregion(&rname, &sregion);
isc_region_consume(&sregion, name_length(&rname));
RETERR(dns_name_towire(&rname, cctx, target, NULL));
RETERR(dns_name_towire(&rname, cctx, target));
isc_buffer_availableregion(target, &tregion);
if (tregion.length < 20) {

View File

@ -117,11 +117,11 @@ towire_talink(ARGS_TOWIRE) {
dns_name_fromregion(&prev, &sregion);
isc_region_consume(&sregion, name_length(&prev));
RETERR(dns_name_towire(&prev, cctx, target, NULL));
RETERR(dns_name_towire(&prev, cctx, target));
dns_name_fromregion(&next, &sregion);
isc_region_consume(&sregion, name_length(&next));
return dns_name_towire(&next, cctx, target, NULL);
return dns_name_towire(&next, cctx, target);
}
static int

View File

@ -320,7 +320,7 @@ towire_tkey(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &sr);
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
isc_region_consume(&sr, name_length(&name));
return mem_tobuffer(target, sr.base, sr.length);

View File

@ -226,7 +226,7 @@ towire_in_a6(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -120,7 +120,7 @@ towire_in_kx(ARGS_TOWIRE) {
dns_name_init(&name);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -95,7 +95,7 @@ towire_in_nsap_ptr(ARGS_TOWIRE) {
dns_rdata_toregion(rdata, &region);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -168,7 +168,7 @@ towire_in_px(ARGS_TOWIRE) {
*/
dns_name_init(&name);
dns_name_fromregion(&name, &region);
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
isc_region_consume(&region, name_length(&name));
/*
@ -176,7 +176,7 @@ towire_in_px(ARGS_TOWIRE) {
*/
dns_name_init(&name);
dns_name_fromregion(&name, &region);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -191,7 +191,7 @@ towire_in_srv(ARGS_TOWIRE) {
*/
dns_name_init(&name);
dns_name_fromregion(&name, &sr);
return dns_name_towire(&name, cctx, target, NULL);
return dns_name_towire(&name, cctx, target);
}
static int

View File

@ -940,7 +940,7 @@ generic_towire_in_svcb(ARGS_TOWIRE) {
*/
dns_name_init(&name);
dns_name_fromregion(&name, &region);
RETERR(dns_name_towire(&name, cctx, target, NULL));
RETERR(dns_name_towire(&name, cctx, target));
isc_region_consume(&region, name_length(&name));
/*

View File

@ -247,7 +247,6 @@ towire(dns_rdataset_t *rdataset, const dns_name_t *owner_name,
struct towire_sort *out = out_fixed;
dns_fixedname_t fixed;
dns_name_t *name = NULL;
uint16_t offset;
/*
* Convert 'rdataset' to wire format, compressing names as specified
@ -357,7 +356,7 @@ towire(dns_rdataset_t *rdataset, const dns_name_t *owner_name,
name = dns_fixedname_initname(&fixed);
dns_name_copy(owner_name, name);
dns_rdataset_getownercase(rdataset, name);
offset = 0xffff;
dns_compress_setmultiuse(cctx, true);
name->attributes.nocompress |= owner_name->attributes.nocompress;
@ -368,15 +367,14 @@ towire(dns_rdataset_t *rdataset, const dns_name_t *owner_name,
rrbuffer = *target;
dns_compress_setpermitted(cctx, true);
result = dns_name_towire(name, cctx, target, &offset);
result = dns_name_towire(name, cctx, target);
if (result != ISC_R_SUCCESS) {
goto rollback;
}
headlen = sizeof(dns_rdataclass_t) + sizeof(dns_rdatatype_t);
if (!question) {
headlen += sizeof(dns_ttl_t) + 2;
} /* XXX 2 for rdata len
*/
} /* XXX 2 for rdata len */
isc_buffer_availableregion(target, &r);
if (r.length < headlen) {
result = ISC_R_NOSPACE;

View File

@ -2703,7 +2703,7 @@ resquery_send(resquery_t *query) {
memset(&zr, 0, sizeof(zr));
isc_buffer_init(&zb, zone, sizeof(zone));
dns_compress_setpermitted(&cctx, false);
result = dns_name_towire(fctx->domain, &cctx, &zb, NULL);
result = dns_name_towire(fctx->domain, &cctx, &zb);
if (result == ISC_R_SUCCESS) {
isc_buffer_usedregion(&zb, &zr);
}
@ -9730,7 +9730,7 @@ rctx_logpacket(respctx_t *rctx) {
dns_compress_init(&cctx, fctx->mctx, 0);
dns_compress_setpermitted(&cctx, false);
isc_buffer_init(&zb, zone, sizeof(zone));
result = dns_name_towire(fctx->domain, &cctx, &zb, NULL);
result = dns_name_towire(fctx->domain, &cctx, &zb);
if (result == ISC_R_SUCCESS) {
isc_buffer_usedregion(&zb, &zr);
}

View File

@ -699,7 +699,7 @@ renderend:
isc_buffer_init(&b, zone, sizeof(zone));
dns_compress_setpermitted(&cctx, false);
eresult = dns_name_towire(zo, &cctx, &b, NULL);
eresult = dns_name_towire(zo, &cctx, &b);
if (eresult == ISC_R_SUCCESS) {
isc_buffer_usedregion(&b, &zr);
}

View File

@ -80,7 +80,7 @@ main(void) {
for (unsigned int i = 0; i < count; i++) {
dns_name_t *name = dns_fixedname_name(&fixedname[i]);
result = dns_name_towire(name, &cctx, &buf, NULL);
result = dns_name_towire(name, &cctx, &buf);
if (result == ISC_R_NOSPACE) {
dns_compress_invalidate(&cctx);
dns_compress_init(&cctx, mctx, 0);

View File

@ -187,7 +187,7 @@ ISC_LOOP_TEST_IMPL(dns_dt_send) {
isc_buffer_init(&zb, zone, sizeof(zone));
dns_compress_init(&cctx, mctx, 0);
dns_compress_setpermitted(&cctx, false);
result = dns_name_towire(zname, &cctx, &zb, NULL);
result = dns_name_towire(zname, &cctx, &zb);
assert_int_equal(result, ISC_R_SUCCESS);
dns_compress_invalidate(&cctx);
isc_buffer_usedregion(&zb, &zr);

View File

@ -148,28 +148,28 @@ compress_test(const dns_name_t *name1, const dns_name_t *name2,
if (rdata) {
/* RDATA compression */
assert_int_equal(dns_name_towire(name1, cctx, &source, NULL),
assert_int_equal(dns_name_towire(name1, cctx, &source),
ISC_R_SUCCESS);
assert_int_equal(dns_name_towire(name2, cctx, &source, NULL),
assert_int_equal(dns_name_towire(name2, cctx, &source),
ISC_R_SUCCESS);
assert_int_equal(dns_name_towire(name2, cctx, &source, NULL),
assert_int_equal(dns_name_towire(name2, cctx, &source),
ISC_R_SUCCESS);
assert_int_equal(dns_name_towire(name3, cctx, &source, NULL),
assert_int_equal(dns_name_towire(name3, cctx, &source),
ISC_R_SUCCESS);
} else {
/* Owner name compression */
uint16_t offset = 0xffff;
assert_int_equal(dns_name_towire(name1, cctx, &source, &offset),
dns_compress_setmultiuse(cctx, true);
assert_int_equal(dns_name_towire(name1, cctx, &source),
ISC_R_SUCCESS);
offset = 0xffff;
assert_int_equal(dns_name_towire(name2, cctx, &source, &offset),
dns_compress_setmultiuse(cctx, true);
assert_int_equal(dns_name_towire(name2, cctx, &source),
ISC_R_SUCCESS);
assert_int_equal(dns_name_towire(name2, cctx, &source, &offset),
assert_int_equal(dns_name_towire(name2, cctx, &source),
ISC_R_SUCCESS);
offset = 0xffff;
assert_int_equal(dns_name_towire(name3, cctx, &source, &offset),
dns_compress_setmultiuse(cctx, true);
assert_int_equal(dns_name_towire(name3, cctx, &source),
ISC_R_SUCCESS);
}
assert_int_equal(source.used, compressed_length);
@ -433,7 +433,8 @@ ISC_RUN_TEST_IMPL(collision) {
}
dns_compress_rollback(&cctx, coff);
result = dns_name_towire(&name, &cctx, &message, NULL);
dns_compress_setmultiuse(&cctx, true);
result = dns_name_towire(&name, &cctx, &message);
assert_int_equal(result, ISC_R_SUCCESS);
/* we must be able to find the name we just added */