Skip to content

Commit

Permalink
Fix possible memleak from dns threads to main
Browse files Browse the repository at this point in the history
Patch by: michaelortmann
  • Loading branch information
michaelortmann authored May 5, 2024
1 parent d7d3c0c commit d0d0754
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(): %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);
Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -545,21 +549,25 @@ void *thread_dns_ipbyhost(void *arg)
addr->family = res->ai_family;
addr->addrlen = res->ai_addrlen;
memcpy(&addr->addr.sa, res->ai_addr, res->ai_addrlen);
error = 0;
error = 0;
*dtn->strerror = 0;
break;
}
}
if (error)
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)
debug1("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(): %s: %s", gai_strerror(error), strerror(errno));
else
debug1("dns: thread_dns_ipbyhost(): getaddrinfo(): error = %s", gai_strerror(error));
snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s", gai_strerror(error));
pthread_mutex_lock(&dtn->mutex);
dtn->ok = !error;
close(dtn->fildes[1]);
pthread_mutex_unlock(&dtn->mutex);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/eggdrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ struct dns_thread_node {
int type;
sockname_t addr;
char host[256];
int ok;
char strerror[3 * 64]; /* msg + gai_strerror() + strerror() */
struct dns_thread_node *next;
};

Expand Down
6 changes: 4 additions & 2 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
debug2("%s: hostname %s", dtn->strerror, dtn->host);
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]);
Expand Down

0 comments on commit d0d0754

Please sign in to comment.