From 3f9b164e67dd32fefab247cbcac7a94b09b7bdd9 Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Tue, 2 Apr 2024 01:38:18 +0200 Subject: [PATCH 1/6] defer debug() / putlog() from dns threads to main thread via dtn.strerror --- src/dns.c | 19 +++++++++++-------- src/eggdrop.h | 2 +- src/net.c | 6 ++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/dns.c b/src/dns.c index 2e5035b88..2ad84a631 100644 --- a/src/dns.c +++ b/src/dns.c @@ -492,6 +492,8 @@ void call_ipbyhost(char *hostn, sockname_t *ip, int ok) * is closed and the thread is ended by return. The other end will make * eggdrops mainloop select() return, read the result from the dns_thread_node * and call call_hostbyip() or call_ipbyhost(). No signal or tcl thread problem. + * + * defer debug() / putlog() from dns threads to main thread via dtn.strerror */ void *thread_dns_hostbyip(void *arg) @@ -502,8 +504,10 @@ void *thread_dns_hostbyip(void *arg) i = getnameinfo((const struct sockaddr *) &addr->addr.sa, addr->addrlen, dtn->host, sizeof dtn->host, NULL, 0, 0); - if (i) { - debug1("dns: thread_dns_hostbyip(): getnameinfo(): error = %s", gai_strerror(i)); + if (!i) + *dtn->strerror = 0; + else { + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_hostbyip(): getnameinfo(): hostname %s: %s", dtn->host, gai_strerror(i)); #ifdef IPV6 if (addr->family == AF_INET6) inet_ntop(AF_INET6, &addr->addr.s6.sin6_addr, dtn->host, sizeof dtn->host); @@ -512,7 +516,6 @@ void *thread_dns_hostbyip(void *arg) inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, dtn->host, sizeof dtn->host); } pthread_mutex_lock(&dtn->mutex); - dtn->ok = !i; close(dtn->fildes[1]); pthread_mutex_unlock(&dtn->mutex); return NULL; @@ -528,6 +531,7 @@ void *thread_dns_ipbyhost(void *arg) error = getaddrinfo(dtn->host, NULL, NULL, &res0); memset(addr, 0, sizeof *addr); if (!error) { + *dtn->strerror = 0; #ifdef IPV6 for (res = res0; res; res = res->ai_next) { if (res == res0 || res->ai_family == (pref_af ? AF_INET6 : AF_INET)) { @@ -539,13 +543,13 @@ void *thread_dns_ipbyhost(void *arg) } } #else - error = 1; + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): hostname %s: no ipv4", dtn->host); for (res = res0; res; res = res->ai_next) { if (res->ai_family == AF_INET) { addr->family = res->ai_family; addr->addrlen = res->ai_addrlen; memcpy(&addr->addr.sa, res->ai_addr, res->ai_addrlen); - error = 0; + *dtn->strerror = 0; break; } } @@ -555,11 +559,10 @@ void *thread_dns_ipbyhost(void *arg) freeaddrinfo(res0); } else if (error == EAI_NONAME) - debug1("dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s not known", dtn->host); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: not known", dtn->host); else - debug1("dns: thread_dns_ipbyhost(): getaddrinfo(): error = %s", gai_strerror(error)); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: %s", dtn->host, gai_strerror(error)); pthread_mutex_lock(&dtn->mutex); - dtn->ok = !error; close(dtn->fildes[1]); pthread_mutex_unlock(&dtn->mutex); return NULL; diff --git a/src/eggdrop.h b/src/eggdrop.h index 076a16252..a2855192c 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -757,7 +757,7 @@ struct dns_thread_node { int type; sockname_t addr; char host[256]; - int ok; + char strerror[319]; struct dns_thread_node *next; }; diff --git a/src/net.c b/src/net.c index c8aa62cd5..c53c70381 100644 --- a/src/net.c +++ b/src/net.c @@ -1053,16 +1053,18 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) #ifdef EGG_TDNS dtn_prev = dns_thread_head; for (dtn = dtn_prev->next; dtn; dtn = dtn->next) { + if (*dtn->strerror) + debug0(dtn->strerror); fd = dtn->fildes[0]; if (FD_ISSET(fd, &fdr)) { if (dtn->type == DTN_TYPE_HOSTBYIP) { pthread_mutex_lock(&dtn->mutex); - call_hostbyip(&dtn->addr, dtn->host, dtn->ok); + call_hostbyip(&dtn->addr, dtn->host, !*dtn->strerror); pthread_mutex_unlock(&dtn->mutex); } else { pthread_mutex_lock(&dtn->mutex); - call_ipbyhost(dtn->host, &dtn->addr, dtn->ok); + call_ipbyhost(dtn->host, &dtn->addr, !*dtn->strerror); pthread_mutex_unlock(&dtn->mutex); } close(dtn->fildes[0]); From 902d224b44e67270b72b38b8b4e44e95e3d0d2bb Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Tue, 2 Apr 2024 03:55:49 +0200 Subject: [PATCH 2/6] Cleanup - call snprintf() only if we have to --- src/dns.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dns.c b/src/dns.c index 2ad84a631..893996db0 100644 --- a/src/dns.c +++ b/src/dns.c @@ -543,16 +543,19 @@ void *thread_dns_ipbyhost(void *arg) } } #else - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): hostname %s: no ipv4", dtn->host); + error = 1; for (res = res0; res; res = res->ai_next) { if (res->ai_family == AF_INET) { addr->family = res->ai_family; addr->addrlen = res->ai_addrlen; memcpy(&addr->addr.sa, res->ai_addr, res->ai_addrlen); + error = 0; *dtn->strerror = 0; break; } } + if (error) + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): hostname %s: no ipv4", dtn->host); #endif if (res0) /* The behavior of freeadrinfo(NULL) is left unspecified by RFCs * 2553 and 3493. Avoid to be compatible with all OSes. */ From c4a3b4db20a471ad3a2f6ec2b363564eea5337bf Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Tue, 2 Apr 2024 04:03:42 +0200 Subject: [PATCH 3/6] fix buf size for long hostnames --- src/eggdrop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eggdrop.h b/src/eggdrop.h index a2855192c..45b8411b4 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -757,7 +757,7 @@ struct dns_thread_node { int type; sockname_t addr; char host[256]; - char strerror[319]; + char strerror[256 + 128]; /* hostname + gai_strerror() + msg */ struct dns_thread_node *next; }; From ba18ffbdb0f0f2f2a9d4cae9bc83b2fdc54f40af Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Sun, 28 Apr 2024 23:33:36 +0200 Subject: [PATCH 4/6] enhance error logging --- src/dns.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dns.c b/src/dns.c index 893996db0..883c29fbb 100644 --- a/src/dns.c +++ b/src/dns.c @@ -563,6 +563,8 @@ void *thread_dns_ipbyhost(void *arg) } else if (error == EAI_NONAME) snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: not known", dtn->host); + else if (error == EAI_SYSTEM) + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: %s: %s", dtn->host, gai_strerror(error), strerror(errno)); else snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: %s", dtn->host, gai_strerror(error)); pthread_mutex_lock(&dtn->mutex); From c72cc1c551d2569e1b1ae4551909ddb9b7f40a89 Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Sun, 28 Apr 2024 23:59:10 +0200 Subject: [PATCH 5/6] Enhance memory usage by reusing dtn->host --- src/dns.c | 10 +++++----- src/eggdrop.h | 2 +- src/net.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/dns.c b/src/dns.c index 883c29fbb..94e41618d 100644 --- a/src/dns.c +++ b/src/dns.c @@ -507,7 +507,7 @@ void *thread_dns_hostbyip(void *arg) if (!i) *dtn->strerror = 0; else { - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_hostbyip(): getnameinfo(): hostname %s: %s", dtn->host, gai_strerror(i)); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_hostbyip(): getnameinfo(): %s", gai_strerror(i)); #ifdef IPV6 if (addr->family == AF_INET6) inet_ntop(AF_INET6, &addr->addr.s6.sin6_addr, dtn->host, sizeof dtn->host); @@ -555,18 +555,18 @@ void *thread_dns_ipbyhost(void *arg) } } if (error) - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): hostname %s: no ipv4", dtn->host); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): no ipv4"); #endif if (res0) /* The behavior of freeadrinfo(NULL) is left unspecified by RFCs * 2553 and 3493. Avoid to be compatible with all OSes. */ freeaddrinfo(res0); } else if (error == EAI_NONAME) - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: not known", dtn->host); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): not known"); else if (error == EAI_SYSTEM) - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: %s: %s", dtn->host, gai_strerror(error), strerror(errno)); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s: %s", gai_strerror(error), strerror(errno)); else - snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): hostname %s: %s", dtn->host, gai_strerror(error)); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s", gai_strerror(error)); pthread_mutex_lock(&dtn->mutex); close(dtn->fildes[1]); pthread_mutex_unlock(&dtn->mutex); diff --git a/src/eggdrop.h b/src/eggdrop.h index 45b8411b4..54babf7b0 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -757,7 +757,7 @@ struct dns_thread_node { int type; sockname_t addr; char host[256]; - char strerror[256 + 128]; /* hostname + gai_strerror() + msg */ + char strerror[256]; /* msg + gai_strerror() + strerror() */ struct dns_thread_node *next; }; diff --git a/src/net.c b/src/net.c index c53c70381..8196d2b6e 100644 --- a/src/net.c +++ b/src/net.c @@ -1054,7 +1054,7 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly) dtn_prev = dns_thread_head; for (dtn = dtn_prev->next; dtn; dtn = dtn->next) { if (*dtn->strerror) - debug0(dtn->strerror); + debug2("%s: hostname %s", dtn->strerror, dtn->host); fd = dtn->fildes[0]; if (FD_ISSET(fd, &fdr)) { if (dtn->type == DTN_TYPE_HOSTBYIP) { From 0103227708c3d9f24a05dd39c340f536ef097f0f Mon Sep 17 00:00:00 2001 From: Michael Ortmann Date: Mon, 29 Apr 2024 00:55:38 +0200 Subject: [PATCH 6/6] enhance memory usage --- src/eggdrop.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eggdrop.h b/src/eggdrop.h index 54babf7b0..04296062d 100644 --- a/src/eggdrop.h +++ b/src/eggdrop.h @@ -757,7 +757,7 @@ struct dns_thread_node { int type; sockname_t addr; char host[256]; - char strerror[256]; /* msg + gai_strerror() + strerror() */ + char strerror[3 * 64]; /* msg + gai_strerror() + strerror() */ struct dns_thread_node *next; };