From 761c5e2d112acc01bb8a779ca2678baf37ecfdf8 Mon Sep 17 00:00:00 2001 From: Raghavendra Prabhu Date: Sat, 6 Aug 2016 01:52:06 +0100 Subject: [PATCH] core/plugins: Use res_init before getaddrinfo. With weechat, if the network changes in anyway that causes resolv.conf to be updated, that is not picked up, resulting in irc: connecting to server XXXX.com/1026 (SSL)... irc: address "XXX.com" not found irc: error: Name or service not known irc: reconnecting to server in 30 seconds This happens, especially when one connects to an irc server behind a VPN. This can also happen when there is no network connectivity weechat was started but later network is connected to. The fix is simple, it relies on calling res_init (3) before getaddrinfo in network_connect_child and other places. res_init reloads the resolv.conf and getaddrinfo does not fail with address resolution error. --- src/core/wee-network.c | 9 ++++++++- src/plugins/irc/irc-server.c | 4 +++- src/plugins/xfer/xfer.c | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/core/wee-network.c b/src/core/wee-network.c index cb2fa52d2..f4b9c2540 100644 --- a/src/core/wee-network.c +++ b/src/core/wee-network.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -311,6 +312,9 @@ network_resolve (const char *hostname, char *ip, int *version) res = NULL; + if (res_init() != 0) + return 0; + if (getaddrinfo (hostname, NULL, NULL, &res) != 0) return 0; @@ -696,6 +700,8 @@ network_connect_to (const char *proxy, struct sockaddr *address, hints.ai_flags = AI_NUMERICSERV; snprintf (str_port, sizeof (str_port), "%d", CONFIG_INTEGER(ptr_proxy->options[PROXY_OPTION_PORT])); + if (res_init() != 0) + goto error; if (getaddrinfo (CONFIG_STRING(ptr_proxy->options[PROXY_OPTION_ADDRESS]), str_port, &hints, &proxy_addrinfo) != 0) { @@ -806,7 +812,8 @@ network_connect_child (struct t_hook *hook_connect) #ifdef AI_ADDRCONFIG hints.ai_flags = AI_ADDRCONFIG; #endif /* AI_ADDRCONFIG */ - if (ptr_proxy) + rc = res_init(); + if (!rc && ptr_proxy) { hints.ai_family = (CONFIG_BOOLEAN(ptr_proxy->options[PROXY_OPTION_IPV6])) ? AF_UNSPEC : AF_INET; diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c index 0f35155ba..a2ec91220 100644 --- a/src/plugins/irc/irc-server.c +++ b/src/plugins/irc/irc-server.c @@ -38,6 +38,7 @@ #endif /* _WIN32 */ #include #include +#include #include #include @@ -5132,7 +5133,8 @@ irc_server_xfer_send_ready_cb (const void *pointer, void *data, "local_address"); if (local_address) { - rc = getaddrinfo (local_address, NULL, NULL, &ainfo); + if ((rc = res_init()) == 0) + rc = getaddrinfo (local_address, NULL, NULL, &ainfo); if ((rc == 0) && ainfo && ainfo->ai_addr) { if (ainfo->ai_family == AF_INET) diff --git a/src/plugins/xfer/xfer.c b/src/plugins/xfer/xfer.c index 25b855124..d01b457b9 100644 --- a/src/plugins/xfer/xfer.c +++ b/src/plugins/xfer/xfer.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -966,6 +967,8 @@ xfer_resolve_addr (const char *str_address, const char *str_port, hints.ai_addr = NULL; hints.ai_next = NULL; + if (res_init() != 0) + return 0; rc = getaddrinfo (str_address, str_port, &hints, &ainfo); if ((rc == 0) && ainfo && ainfo->ai_addr) {