From 09d60499af3acef2ba9bd7be15e8d1c44249f8d5 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 9 Feb 2015 15:06:12 +0100 Subject: [PATCH] Always call res_init() before getaddrinfo(). Unfortunately, glibc assumes that /etc/resolv.conf is a static file that never changes. Even on servers, /etc/resolv.conf might be a dynamically generated file, and we never know when it changes. So just call res_init() every time, so glibc uses up-to-date nameserver information. --- src/have.h | 11 +++++++++++ src/net.c | 11 ----------- src/net_setup.c | 4 ++++ src/netutl.c | 4 ++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/have.h b/src/have.h index bcd4612..e83f98f 100644 --- a/src/have.h +++ b/src/have.h @@ -196,4 +196,15 @@ #include #endif +#ifdef HAVE_ARPA_NAMESER_H +#include +#ifdef STATUS +#undef STATUS +#endif +#endif + +#ifdef HAVE_RESOLV_H +#include +#endif + #endif /* __TINC_SYSTEM_H__ */ diff --git a/src/net.c b/src/net.c index 8d0a0cf..08f0a87 100644 --- a/src/net.c +++ b/src/net.c @@ -41,14 +41,6 @@ #include "subnet.h" #include "xalloc.h" -#ifdef HAVE_ARPA_NAMESER_H -#include -#endif - -#ifdef HAVE_RESOLV_H -#include -#endif - bool do_purge = false; volatile bool running = false; #ifdef HAVE_PSELECT @@ -508,9 +500,6 @@ int main_loop(void) { avl_node_t *node; logger(LOG_INFO, "Flushing event queue"); expire_events(); -#if HAVE_DECL_RES_INIT - res_init(); -#endif for(node = connection_tree->head; node; node = node->next) { connection_t *c = node->data; if(c->status.active) diff --git a/src/net_setup.c b/src/net_setup.c index 765a9eb..121b989 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -813,6 +813,10 @@ static bool setup_myself(void) { hint.ai_protocol = IPPROTO_TCP; hint.ai_flags = AI_PASSIVE; +#ifdef HAVE_DECL_RES_INIT + // ensure glibc reloads /etc/resolv.conf. + res_init(); +#endif err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai); free(address); diff --git a/src/netutl.c b/src/netutl.c index c57b24f..fc3cdc9 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -39,6 +39,10 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock hint.ai_family = addressfamily; hint.ai_socktype = socktype; +#ifdef HAVE_DECL_RES_INIT + // ensure glibc reloads /etc/resolv.conf. + res_init(); +#endif err = getaddrinfo(address, service, &hint, &ai); if(err) { -- 2.25.1