]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Enable logging both "from" and "to" socket
authorMichał Kępień <michal@isc.org>
Tue, 31 Dec 2024 04:40:48 +0000 (05:40 +0100)
committerMichał Kępień <michal@isc.org>
Tue, 31 Dec 2024 04:40:48 +0000 (05:40 +0100)
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
lib/dns/message.c
lib/dns/resolver.c

index 67c9640d9a6fb05308d0ee814879e407aca4b0eb..e711dacbee931fc80050d41ef6dc73c3e74b718a 100644 (file)
@@ -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.
index e4631839fac9311f01560055aaa2abc1209a78a2..eeba3e1fa3bee1aec9f4ca2c0f91b22c157ac6be 100644 (file)
@@ -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);
index 0b0f87a1466071727ee87087888df3e471a3afd3..2ce4d52cbbd33edf99b8c91ddb96cf83f476d6fb 100644 (file)
@@ -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