From d5e4684b3defdd79fe4b366c2da88bdcd7a4c5d9 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Thu, 20 Nov 2025 23:33:29 -0800 Subject: [PATCH] remove dns_message_buildopt now that the EDNS state is stored within dns_message_t, it's no longer necessary to have a public API call to build an opt rdataset; we can just have dns_message_setopt() build the opt record internally. --- bin/delv/delv.c | 4 +-- bin/dig/dighost.c | 5 +--- bin/nsupdate/nsupdate.c | 6 ++--- bin/tools/mdig.c | 5 +--- lib/dns/include/dns/message.h | 30 +++++---------------- lib/dns/message.c | 21 ++++++++------- lib/dns/resolver.c | 9 +------ lib/dns/xfrin.c | 8 +----- lib/dns/zone.c | 8 +----- lib/ns/client.c | 50 +++++++---------------------------- lib/ns/include/ns/client.h | 16 +++++------ lib/ns/xfrout.c | 5 +--- 12 files changed, 43 insertions(+), 124 deletions(-) diff --git a/bin/delv/delv.c b/bin/delv/delv.c index a4c262b53e3..0ceda06e334 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -2060,7 +2060,6 @@ sendquery(void *arg) { dns_message_t *message = NULL; dns_name_t *query_name = NULL, *mname = NULL; dns_rdataset_t *mrdataset = NULL; - dns_rdataset_t *opt = NULL; dns_request_t *request = NULL; /* Construct query message */ @@ -2086,8 +2085,7 @@ sendquery(void *arg) { mname = NULL; dns_message_ednsinit(message, 0, 0, DNS_MESSAGEEXTFLAG_DO, 0); - CHECK(dns_message_buildopt(message, &opt)); - CHECK(dns_message_setopt(message, opt)); + CHECK(dns_message_setopt(message)); CHECK(dns_requestmgr_create(isc_g_mctx, dispatchmgr, NULL, NULL, &requestmgr)); diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c index 1499761f789..5045ef6cb7b 100644 --- a/bin/dig/dighost.c +++ b/bin/dig/dighost.c @@ -2115,7 +2115,6 @@ setup_lookup(dig_lookup_t *lookup) { char cookiebuf[256]; char *origin = NULL; char *textname = NULL; - dns_rdataset_t *rdataset = NULL; REQUIRE(lookup != NULL); @@ -2564,9 +2563,7 @@ setup_lookup(dig_lookup_t *lookup) { lookup->padding); } - result = dns_message_buildopt(lookup->sendmsg, &rdataset); - check_result(result, "dns_message_buildopt"); - result = dns_message_setopt(lookup->sendmsg, rdataset); + result = dns_message_setopt(lookup->sendmsg); check_result(result, "dns_message_setopt"); } diff --git a/bin/nsupdate/nsupdate.c b/bin/nsupdate/nsupdate.c index f0f1a613a94..318c8dd96ab 100644 --- a/bin/nsupdate/nsupdate.c +++ b/bin/nsupdate/nsupdate.c @@ -1493,7 +1493,6 @@ evaluate_prereq(char *cmdline) { static void updateopt(void) { isc_result_t result; - dns_rdataset_t *opt = NULL; unsigned char ul[8]; isc_buffer_t b; dns_ednsopt_t option = { @@ -1512,9 +1511,8 @@ updateopt(void) { dns_message_ednsinit(updatemsg, 0, DEFAULT_EDNS_BUFSIZE, 0, 0); dns_message_ednsaddopt(updatemsg, &option); - result = dns_message_buildopt(updatemsg, &opt); - check_result(result, "dns_message_buildopt"); - result = dns_message_setopt(updatemsg, opt); + result = dns_message_setopt(updatemsg); + check_result(result, "dns_message_setopt"); } static uint16_t diff --git a/bin/tools/mdig.c b/bin/tools/mdig.c index 2f4e79b20e3..4aa31d9c294 100644 --- a/bin/tools/mdig.c +++ b/bin/tools/mdig.c @@ -588,7 +588,6 @@ sendquery(struct query *query) { unsigned int flags; char ecsbuf[20]; unsigned char cookie[40]; - dns_rdataset_t *rdataset = NULL; flags = query->ednsflags; flags &= ~DNS_MESSAGEEXTFLAG_DO; @@ -702,9 +701,7 @@ sendquery(struct query *query) { } } - result = dns_message_buildopt(message, &rdataset); - CHECK("dns_message_buildopt", result); - result = dns_message_setopt(message, rdataset); + result = dns_message_setopt(message); CHECK("dns_message_setopt", result); } diff --git a/lib/dns/include/dns/message.h b/lib/dns/include/dns/message.h index ef35346cb6f..17f7f97d118 100644 --- a/lib/dns/include/dns/message.h +++ b/lib/dns/include/dns/message.h @@ -1075,22 +1075,19 @@ dns_message_getopt(dns_message_t *msg); */ isc_result_t -dns_message_setopt(dns_message_t *msg, dns_rdataset_t *opt); +dns_message_setopt(dns_message_t *msg); /*%< - * Set/clear the OPT record for 'msg'. + * Constructs an OPT record for 'msg', based on previous calls to + * dns_message_ednsinit() and dns_message_ednsaddopt(). * * Requires: * - *\li 'msg' is a valid message with rendering intent - * and no sections have been rendered. - * - *\li 'opt' is a valid OPT rdataset or NULL. + *\li 'msg' is a valid message with rendering intent, + * no sections have been rendered., and dns_message_ednsinit() + * has been run. * * Ensures: * - *\li The OPT record has either been freed or ownership of it has - * been transferred to the message. - * *\li If ISC_R_SUCCESS was returned, the OPT record will be rendered * when dns_message_renderend() is called. * @@ -1439,21 +1436,6 @@ dns_message_ednsaddopt(dns_message_t *msg, dns_ednsopt_t *ednsopt); *\li ISC_R_NOSPACE */ -isc_result_t -dns_message_buildopt(dns_message_t *msg, dns_rdataset_t **opt); - -/*%< - * Build an opt record. - * - * Requires: - *\li msg be a valid message. - *\li opt to be a non NULL and *opt to be NULL. - * - * Returns: - *\li ISC_R_SUCCESS - *\li ISC_R_NOSPACE - */ - void dns_message_setclass(dns_message_t *msg, dns_rdataclass_t rdclass); /*%< diff --git a/lib/dns/message.c b/lib/dns/message.c index 5951487af49..4b094b04cba 100644 --- a/lib/dns/message.c +++ b/lib/dns/message.c @@ -227,6 +227,9 @@ logfmtpacket(dns_message_t *message, const char *description, isc_logcategory_t category, isc_logmodule_t module, const dns_master_style_t *style, int level, isc_mem_t *mctx); +static isc_result_t +buildopt(dns_message_t *message, dns_rdataset_t **rdatasetp); + /* * Allocate a new dns_msgblock_t, and return a pointer to it. If no memory * is free, return NULL. @@ -2629,8 +2632,9 @@ dns_message_getopt(dns_message_t *msg) { } isc_result_t -dns_message_setopt(dns_message_t *msg, dns_rdataset_t *opt) { +dns_message_setopt(dns_message_t *msg) { isc_result_t result; + dns_rdataset_t *opt = NULL; dns_rdata_t rdata = DNS_RDATA_INIT; /* @@ -2652,17 +2656,16 @@ dns_message_setopt(dns_message_t *msg, dns_rdataset_t *opt) { */ REQUIRE(DNS_MESSAGE_VALID(msg)); - REQUIRE(opt == NULL || DNS_RDATASET_VALID(opt)); - REQUIRE(opt == NULL || opt->type == dns_rdatatype_opt); REQUIRE(msg->from_to_wire == DNS_MESSAGE_INTENTRENDER); REQUIRE(msg->state == DNS_SECTION_ANY); - msgresetopt(msg); - - if (opt == NULL) { - return ISC_R_SUCCESS; + result = buildopt(msg, &opt); + if (result != ISC_R_SUCCESS) { + return result; } + msgresetopt(msg); + result = dns_rdataset_first(opt); if (result != ISC_R_SUCCESS) { goto cleanup; @@ -4897,8 +4900,8 @@ dns_message_ednsaddopt(dns_message_t *msg, dns_ednsopt_t *option) { return ISC_R_SUCCESS; } -isc_result_t -dns_message_buildopt(dns_message_t *message, dns_rdataset_t **rdatasetp) { +static isc_result_t +buildopt(dns_message_t *message, dns_rdataset_t **rdatasetp) { dns_rdataset_t *rdataset = NULL; dns_rdatalist_t *rdatalist = NULL; dns_rdata_t *rdata = NULL; diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 86efcb00786..c9c19bf6d51 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -2464,7 +2464,6 @@ resquery_send(resquery_t *query) { bool tcpkeepalive = false; unsigned char cookie[COOKIE_BUFFER_SIZE]; uint16_t padding = 0; - dns_rdataset_t *rdataset = NULL; /* * Set the default UDP size to what was @@ -2605,13 +2604,7 @@ resquery_send(resquery_t *query) { dns_message_setpadding(fctx->qmessage, padding); } - result = dns_message_buildopt(fctx->qmessage, - &rdataset); - if (result == ISC_R_SUCCESS) { - result = dns_message_setopt(fctx->qmessage, - rdataset); - } - + result = dns_message_setopt(fctx->qmessage); if (result == ISC_R_SUCCESS) { if (reqnsid) { query->options |= DNS_FETCHOPT_WANTNSID; diff --git a/lib/dns/xfrin.c b/lib/dns/xfrin.c index e06b466f28f..f68f63b5154 100644 --- a/lib/dns/xfrin.c +++ b/lib/dns/xfrin.c @@ -1552,7 +1552,6 @@ static isc_result_t add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid, bool reqexpire) { isc_result_t result; - dns_rdataset_t *rdataset = NULL; dns_message_ednsinit(message, 0, udpsize, 0, 0); @@ -1572,12 +1571,7 @@ add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid, } } - result = dns_message_buildopt(message, &rdataset); - if (result != ISC_R_SUCCESS) { - return result; - } - - return dns_message_setopt(message, rdataset); + return dns_message_setopt(message); } /* diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 8850844682a..a46969b3e8e 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -12862,7 +12862,6 @@ static isc_result_t add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid, bool reqexpire) { isc_result_t result; - dns_rdataset_t *rdataset = NULL; dns_message_ednsinit(message, 0, udpsize, 0, 0); @@ -12882,12 +12881,7 @@ add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid, } } - result = dns_message_buildopt(message, &rdataset); - if (result != ISC_R_SUCCESS) { - return result; - } - - return dns_message_setopt(message, rdataset); + return dns_message_setopt(message); } /* diff --git a/lib/ns/client.c b/lib/ns/client.c index 054bfc8deb4..d4bb08bc825 100644 --- a/lib/ns/client.c +++ b/lib/ns/client.c @@ -266,12 +266,6 @@ ns_client_endrequest(ns_client_t *client) { #endif /* ifdef ENABLE_AFL */ dns_view_detach(&client->inner.view); } - if (client->inner.opt != NULL) { - INSIST(dns_rdataset_isassociated(client->inner.opt)); - dns_rdataset_disassociate(client->inner.opt); - dns_message_puttemprdataset(client->message, - &client->inner.opt); - } client_zoneversion_reset(client); client->inner.signer = NULL; @@ -616,8 +610,7 @@ ns_client_send(ns_client_t *client) { * Create an OPT for our reply. */ if ((client->inner.attributes & NS_CLIENTATTR_WANTOPT) != 0) { - result = ns_client_addopt(client, client->message, - &client->inner.opt); + result = ns_client_addopt(client, client->message); if (result != ISC_R_SUCCESS) { goto cleanup; } @@ -653,14 +646,6 @@ ns_client_send(ns_client_t *client) { goto cleanup; } - if (client->inner.opt != NULL) { - result = dns_message_setopt(client->message, client->inner.opt); - opt_included = true; - client->inner.opt = NULL; - if (result != ISC_R_SUCCESS) { - goto cleanup; - } - } result = dns_message_rendersection(client->message, DNS_SECTION_QUESTION, 0); if (result == ISC_R_NOSPACE) { @@ -1053,8 +1038,7 @@ ns_client_error(ns_client_t *client, isc_result_t result) { } isc_result_t -ns_client_addopt(ns_client_t *client, dns_message_t *message, - dns_rdataset_t **opt) { +ns_client_addopt(ns_client_t *client, dns_message_t *message) { unsigned char ecs[ECS_SIZE]; unsigned char cookie[COOKIE_SIZE]; isc_result_t result; @@ -1066,7 +1050,6 @@ ns_client_addopt(ns_client_t *client, dns_message_t *message, dns_aclenv_t *env = NULL; REQUIRE(NS_CLIENT_VALID(client)); - REQUIRE(opt != NULL && *opt == NULL); REQUIRE(message != NULL); env = client->manager->aclenv; @@ -1282,7 +1265,7 @@ ns_client_addopt(ns_client_t *client, dns_message_t *message, } } - result = dns_message_buildopt(message, opt); + result = dns_message_setopt(message); return result; } @@ -1723,11 +1706,8 @@ process_opt(ns_client_t *client, dns_rdataset_t *opt) { if (client->inner.ednsversion > DNS_EDNS_VERSION) { ns_stats_increment(client->manager->sctx->nsstats, ns_statscounter_badednsver); - result = ns_client_addopt(client, client->message, - &client->inner.opt); - if (result == ISC_R_SUCCESS) { - result = DNS_R_BADVERS; - } + result = DNS_R_BADVERS; + client->inner.attributes |= NS_CLIENTATTR_WANTOPT; ns_client_error(client, result); return result; } @@ -1739,13 +1719,10 @@ process_opt(ns_client_t *client, dns_rdataset_t *opt) { return result; formerr: - if (result == DNS_R_FORMERR || result == DNS_R_OPTERR) { - result = ns_client_addopt(client, client->message, - &client->inner.opt); - if (result == ISC_R_SUCCESS) { - result = DNS_R_FORMERR; - } + if (result == DNS_R_OPTERR) { + result = DNS_R_FORMERR; } + client->inner.attributes |= NS_CLIENTATTR_WANTOPT; ns_client_error(client, result); return result; } @@ -1816,13 +1793,6 @@ ns__client_put_cb(void *client0) { client->magic = 0; - if (client->inner.opt != NULL) { - INSIST(dns_rdataset_isassociated(client->inner.opt)); - dns_rdataset_disassociate(client->inner.opt); - dns_message_puttemprdataset(client->message, - &client->inner.opt); - } - ns_client_async_reset(client); dns_message_detach(&client->message); @@ -2060,10 +2030,8 @@ ns_client_request(isc_nmhandle_t *handle, isc_result_t eresult, * (typically FORMERR or SERVFAIL). */ if (result == DNS_R_OPTERR) { - (void)ns_client_addopt(client, client->message, - &client->inner.opt); + client->inner.attributes |= NS_CLIENTATTR_WANTOPT; } - ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1), "message parsing failed: %s", diff --git a/lib/ns/include/ns/client.h b/lib/ns/include/ns/client.h index 70344180277..41d52b54b0a 100644 --- a/lib/ns/include/ns/client.h +++ b/lib/ns/include/ns/client.h @@ -177,13 +177,12 @@ struct ns_client { isc_nmhandle_t *updatehandle; /* Waiting for update callback */ isc_nmhandle_t *restarthandle; /* Waiting for restart callback */ - unsigned char *tcpbuf; - size_t tcpbuf_size; - dns_rdataset_t *opt; - uint16_t udpsize; - uint16_t extflags; - int16_t ednsversion; /* -1 noedns */ - uint16_t additionaldepth; + unsigned char *tcpbuf; + size_t tcpbuf_size; + uint16_t udpsize; + uint16_t extflags; + int16_t ednsversion; /* -1 noedns */ + uint16_t additionaldepth; void (*cleanup)(ns_client_t *); isc_time_t requesttime; isc_stdtime_t now; @@ -455,8 +454,7 @@ isc_result_t ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp); isc_result_t -ns_client_addopt(ns_client_t *client, dns_message_t *message, - dns_rdataset_t **opt); +ns_client_addopt(ns_client_t *client, dns_message_t *message); /*%< * Get a client object from the inactive queue, or create one, as needed. diff --git a/lib/ns/xfrout.c b/lib/ns/xfrout.c index 685ce22a05c..af9273f7b4b 100644 --- a/lib/ns/xfrout.c +++ b/lib/ns/xfrout.c @@ -1413,10 +1413,7 @@ sendstream(xfrout_ctx_t *xfr) { if ((xfr->client->inner.attributes & NS_CLIENTATTR_WANTOPT) != 0) { - dns_rdataset_t *opt = NULL; - - CHECK(ns_client_addopt(xfr->client, msg, &opt)); - CHECK(dns_message_setopt(msg, opt)); + CHECK(ns_client_addopt(xfr->client, msg)); /* * Add to first message only. */ -- 2.47.3