From bafa5d3c2eae962eb2ccd58ea8196149371cb974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Tue, 31 Dec 2024 05:40:48 +0100 Subject: [PATCH] Enable logging both "from" and "to" socket Change the function prototype for dns_message_logfmtpacket() so that it takes two isc_sockaddr_t parameters: one for the sending side and another one for the receiving side. This enables debug messages to be more precise. Also adjust the function prototype for logfmtpacket() accordingly. Unlike dns_message_logfmtpacket(), this function must not require both 'from' and 'to' parameters to be non-NULL as it is still going to be used by dns_message_logpacket(), which only provides a single socket address. Adjust its log format to handle both of these cases properly. Adjust both dns_message_logfmtpacket() call sites accordingly, without actually providing the second socket address yet. (This causes the revised REQUIRE() assertion in dns_message_logfmtpacket() to fail; the issue will be addressed in a separate commit.) --- lib/dns/include/dns/message.h | 3 ++- lib/dns/message.c | 36 ++++++++++++++++++++--------------- lib/dns/resolver.c | 4 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/dns/include/dns/message.h b/lib/dns/include/dns/message.h index 67c9640d9a..e711dacbee 100644 --- a/lib/dns/include/dns/message.h +++ b/lib/dns/include/dns/message.h @@ -1402,7 +1402,7 @@ dns_message_logpacket(dns_message_t *message, const char *description, void dns_message_logfmtpacket(dns_message_t *message, const char *description, - const isc_sockaddr_t *address, + const isc_sockaddr_t *from, const isc_sockaddr_t *to, isc_logcategory_t category, isc_logmodule_t module, int level, isc_mem_t *mctx); /*%< @@ -1419,6 +1419,7 @@ dns_message_logfmtpacket(dns_message_t *message, const char *description, * \li message be a valid. * \li description to be non NULL. * \li address to be non NULL. + * \li from and to to be non NULL. * \li category to be valid. * \li module to be valid. * \li mctx to be a valid. diff --git a/lib/dns/message.c b/lib/dns/message.c index e4631839fa..eeba3e1fa3 100644 --- a/lib/dns/message.c +++ b/lib/dns/message.c @@ -216,9 +216,9 @@ msgblock_free(isc_mem_t *, dns_msgblock_t *, unsigned int); static void logfmtpacket(dns_message_t *message, const char *description, - const isc_sockaddr_t *address, isc_logcategory_t category, - isc_logmodule_t module, const dns_master_style_t *style, int level, - isc_mem_t *mctx); + const isc_sockaddr_t *from, const isc_sockaddr_t *to, + isc_logcategory_t category, isc_logmodule_t module, + const dns_master_style_t *style, int level, isc_mem_t *mctx); /* * Allocate a new dns_msgblock_t, and return a pointer to it. If no memory @@ -4733,27 +4733,28 @@ dns_message_logpacket(dns_message_t *message, const char *description, isc_logmodule_t module, int level, isc_mem_t *mctx) { REQUIRE(address != NULL); - logfmtpacket(message, description, address, category, module, + logfmtpacket(message, description, address, NULL, category, module, &dns_master_style_debug, level, mctx); } void dns_message_logfmtpacket(dns_message_t *message, const char *description, - const isc_sockaddr_t *address, + const isc_sockaddr_t *from, const isc_sockaddr_t *to, isc_logcategory_t category, isc_logmodule_t module, int level, isc_mem_t *mctx) { - REQUIRE(address != NULL); + REQUIRE(from != NULL && to != NULL); - logfmtpacket(message, description, address, category, module, + logfmtpacket(message, description, from, to, category, module, &dns_master_style_comment, level, mctx); } static void logfmtpacket(dns_message_t *message, const char *description, - const isc_sockaddr_t *address, isc_logcategory_t category, - isc_logmodule_t module, const dns_master_style_t *style, int level, - isc_mem_t *mctx) { - char addrbuf[ISC_SOCKADDR_FORMATSIZE] = { 0 }; + const isc_sockaddr_t *from, const isc_sockaddr_t *to, + isc_logcategory_t category, isc_logmodule_t module, + const dns_master_style_t *style, int level, isc_mem_t *mctx) { + char frombuf[ISC_SOCKADDR_FORMATSIZE] = { 0 }; + char tobuf[ISC_SOCKADDR_FORMATSIZE] = { 0 }; isc_buffer_t buffer; char *buf = NULL; int len = 1024; @@ -4768,8 +4769,11 @@ logfmtpacket(dns_message_t *message, const char *description, * to appear in the log after each message. */ - if (address != NULL) { - isc_sockaddr_format(address, addrbuf, sizeof(addrbuf)); + if (from != NULL) { + isc_sockaddr_format(from, frombuf, sizeof(frombuf)); + } + if (to != NULL) { + isc_sockaddr_format(to, tobuf, sizeof(tobuf)); } do { @@ -4780,8 +4784,10 @@ logfmtpacket(dns_message_t *message, const char *description, isc_mem_put(mctx, buf, len); len += 1024; } else if (result == ISC_R_SUCCESS) { - isc_log_write(category, module, level, "%s %s\n%.*s", - description, addrbuf, + isc_log_write(category, module, level, + "%s%s%s%s%s\n%.*s", description, + (from != NULL ? " from " : ""), frombuf, + (to != NULL ? " to " : ""), tobuf, (int)isc_buffer_usedlength(&buffer), buf); } } while (result == ISC_R_NOSPACE); diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c index 0b0f87a146..2ce4d52cbb 100644 --- a/lib/dns/resolver.c +++ b/lib/dns/resolver.c @@ -2710,7 +2710,7 @@ resquery_send(resquery_t *query) { * Log the outgoing packet. */ dns_message_logfmtpacket( - fctx->qmessage, "sending packet to", &query->addrinfo->sockaddr, + fctx->qmessage, "sending packet to", NULL, &query->addrinfo->sockaddr, DNS_LOGCATEGORY_RESOLVER, DNS_LOGMODULE_PACKETS, ISC_LOG_DEBUG(11), fctx->mctx); @@ -9731,7 +9731,7 @@ rctx_logpacket(respctx_t *rctx) { dns_message_logfmtpacket( rctx->query->rmessage, "received packet from", - &rctx->query->addrinfo->sockaddr, DNS_LOGCATEGORY_RESOLVER, + &rctx->query->addrinfo->sockaddr, NULL, DNS_LOGCATEGORY_RESOLVER, DNS_LOGMODULE_PACKETS, ISC_LOG_DEBUG(10), fctx->mctx); #ifdef HAVE_DNSTAP