diff --git a/src/dns.c b/src/dns.c index 69b3a03e5..7d6658954 100644 --- a/src/dns.c +++ b/src/dns.c @@ -504,6 +504,7 @@ void *thread_dns_hostbyip(void *arg) i = getnameinfo((const struct sockaddr *) &addr->addr.sa, addr->addrlen, dtn->host, sizeof dtn->host, NULL, 0, 0); + pthread_mutex_lock(&dtn->mutex); if (!i) *dtn->strerror = 0; else { @@ -515,7 +516,6 @@ void *thread_dns_hostbyip(void *arg) #endif inet_ntop(AF_INET, &addr->addr.s4.sin_addr.s_addr, dtn->host, sizeof dtn->host); } - pthread_mutex_lock(&dtn->mutex); close(dtn->fildes[1]); pthread_mutex_unlock(&dtn->mutex); return NULL; @@ -530,6 +530,7 @@ void *thread_dns_ipbyhost(void *arg) error = getaddrinfo(dtn->host, NULL, NULL, &res0); memset(addr, 0, sizeof *addr); + pthread_mutex_lock(&dtn->mutex); if (!error) { *dtn->strerror = 0; #ifdef IPV6 @@ -563,11 +564,13 @@ void *thread_dns_ipbyhost(void *arg) } else if (error == EAI_NONAME) 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 + else if (error == EAI_SYSTEM) { + char ebuf[2048]; + if (strerror_r(errno, ebuf, sizeof ebuf)) + strcpy(ebuf, "strerror_r()"); + snprintf(dtn->strerror, sizeof dtn->strerror, "dns: thread_dns_ipbyhost(): getaddrinfo(): %s: %s", gai_strerror(error), ebuf); + } else 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); return NULL; diff --git a/src/net.c b/src/net.c index 6ec1d5b3a..daf8df025 100644 --- a/src/net.c +++ b/src/net.c @@ -1056,27 +1056,24 @@ 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) { + pthread_mutex_lock(&dtn->mutex); 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); + if (dtn->type == DTN_TYPE_HOSTBYIP) call_hostbyip(&dtn->addr, dtn->host, !*dtn->strerror); - pthread_mutex_unlock(&dtn->mutex); - } - else { - pthread_mutex_lock(&dtn->mutex); + else call_ipbyhost(dtn->host, &dtn->addr, !*dtn->strerror); - pthread_mutex_unlock(&dtn->mutex); - } - close(dtn->fildes[0]); + pthread_mutex_unlock(&dtn->mutex); + close(fd); if (pthread_join(dtn->thread_id, &res)) putlog(LOG_MISC, "*", "sockread(): pthread_join(): error = %s", strerror(errno)); dtn_prev->next = dtn->next; nfree(dtn); dtn = dtn_prev; - } + } else + pthread_mutex_unlock(&dtn->mutex); dtn_prev = dtn; } #endif