From 3d6bad8a13cf95b5edd8388eac0b9767905ca31a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 20 Jan 2025 18:11:58 +0100 Subject: [PATCH] Improve FTL connection error logging Signed-off-by: DL6ER --- src/dnsmasq/forward.c | 19 +++++++++++-------- src/dnsmasq_interface.c | 5 ++--- src/dnsmasq_interface.h | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/dnsmasq/forward.c b/src/dnsmasq/forward.c index a0d4842d1..45b7506c4 100644 --- a/src/dnsmasq/forward.c +++ b/src/dnsmasq/forward.c @@ -102,9 +102,10 @@ int send_from(int fd, int nowild, char *packet, size_t len, /* If interface is still in DAD, EINVAL results - ignore that. */ if (errno != EINVAL) { + const int errnum = errno; my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno)); /********** Pi-hole modification **********/ - FTL_connection_error("failed to send UDP reply", to); + FTL_connection_error("failed to send UDP reply", to, errnum); /******************************************/ } #endif @@ -516,7 +517,7 @@ static void forward_query(int udpfd, union mysockaddr *udpaddr, /**** Pi-hole modification ****/ else { - FTL_connection_error("failed to send UDP request", &srv->addr); + FTL_connection_error("failed to send UDP request", &srv->addr, errno); } /******************************/ } @@ -2081,7 +2082,7 @@ static ssize_t tcp_talk(int first, int last, int start, unsigned char *packet, fatal = 1; /**** Pi-hole modification ****/ if (errno != 0) - FTL_connection_error("failed to send TCP(fast-open) packet", &serv->addr); + FTL_connection_error("failed to send TCP(fast-open) packet", &serv->addr, errno); /******************************/ #endif @@ -2090,7 +2091,7 @@ static ssize_t tcp_talk(int first, int last, int start, unsigned char *packet, if (fatal || (!data_sent && connect(serv->tcpfd, &serv->addr.sa, sa_len(&serv->addr)) == -1)) { /**** Pi-hole modification ****/ - FTL_connection_error("failed to send TCP(connect) packet", &serv->addr); + FTL_connection_error("failed to send TCP(connect) packet", &serv->addr, errno); /******************************/ close(serv->tcpfd); serv->tcpfd = -1; @@ -2107,10 +2108,7 @@ static ssize_t tcp_talk(int first, int last, int start, unsigned char *packet, !read_write(serv->tcpfd, (unsigned char *)length, sizeof (*length), RW_READ_ONCE) || !read_write(serv->tcpfd, payload, (rsize = ntohs(*length)), RW_READ_ONCE)) { - /**** Pi-hole modification ****/ - FTL_connection_error("failed to send TCP(read_write) packet", &serv->addr); - /******************************/ - + const int errnum = errno; close(serv->tcpfd); serv->tcpfd = -1; /* We get data then EOF, reopen connection to same server, @@ -2119,7 +2117,12 @@ static ssize_t tcp_talk(int first, int last, int start, unsigned char *packet, if (serv->flags & SERV_GOT_TCP) goto retry; else + /**** Pi-hole modification ****/ + { + FTL_connection_error("failed to send TCP(read_write) packet", &serv->addr, errnum); continue; + } + /******************************/ } /* If the question section of the reply doesn't match the crc we sent, then diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index c181065ff..49da490cf 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -3737,10 +3737,9 @@ void get_dnsmasq_metrics_obj(cJSON *json) cJSON_AddNumberToObject(json, get_metric_name(i), daemon->metrics[i]); } -void FTL_connection_error(const char *reason, const union mysockaddr *addr) +void FTL_connection_error(const char *reason, const union mysockaddr *addr, const int errnum) { - // Make a private copy of the error - const int errnum = errno; + // Get the error message const char *error = strerror(errnum); // Set log priority diff --git a/src/dnsmasq_interface.h b/src/dnsmasq_interface.h index 16e11d1b7..2e99a3116 100644 --- a/src/dnsmasq_interface.h +++ b/src/dnsmasq_interface.h @@ -49,7 +49,7 @@ void FTL_TCP_worker_terminating(bool finished); bool FTL_unlink_DHCP_lease(const char *ipaddr, const char **hint); -void FTL_connection_error(const char *reason, const union mysockaddr *addr); +void FTL_connection_error(const char *reason, const union mysockaddr *addr, const int errnum); bool get_dnsmasq_debug(void) __attribute__ ((pure));