connman.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Nicky Geerts <nicky.geerts@gmail.com>
To: connman@lists.linux.dev
Cc: Nicky Geerts <nicky.geerts@gmail.com>
Subject: [PATCH 1/2] timeserver: refresh the nameservers before each lookup
Date: Mon,  7 Mar 2022 09:44:31 +0100	[thread overview]
Message-ID: <20220307084432.62446-2-nicky.geerts@gmail.com> (raw)
In-Reply-To: <20220228124930.4010311-1-nicky.geerts@gmail.com>

There is a possibility where the UDP channel connections in the resolv
instance are being closed because of how the external DNS service might
respond, and are never opened again.  The DNS resolve will keep failing,
and there is no automatic recovery from this.

Similar to the behavior of the WISPR module, refresh the nameservers in
the resolv instance before each DNS request.

---
 src/timeserver.c | 91 ++++++++++++++++++++++++++----------------------
 1 file changed, 50 insertions(+), 41 deletions(-)

diff --git a/src/timeserver.c b/src/timeserver.c
index feef8e83..0ea73d63 100644
--- a/src/timeserver.c
+++ b/src/timeserver.c
@@ -47,6 +47,7 @@ static GResolv *resolv = NULL;
 static int resolv_id = 0;
 
 static void sync_next(void);
+static void ts_set_nameservers(struct connman_service *service);
 
 static void resolv_debug(const char *str, void *data)
 {
@@ -183,6 +184,7 @@ static void sync_next(void)
 	}
 
 	__connman_ntp_stop();
+	ts_set_nameservers(ts_service);
 
 	while (ts_list) {
 		ts_current = ts_list->data;
@@ -347,24 +349,35 @@ static void ts_recheck_enable(void)
 			NULL);
 }
 
-static void ts_reset(struct connman_service *service)
+static int ts_setup_resolv(struct connman_service *service)
 {
-	char **nameservers;
 	int i;
 
-	if (!resolv)
-		return;
+	i = __connman_service_get_index(service);
+	if (i < 0)
+		return -EINVAL;
+ 
+	if (resolv) {
+		g_resolv_unref(resolv);
+		resolv = NULL;
+	}
 
-	__connman_timeserver_set_synced(false);
+	resolv = g_resolv_new(i);
+	if (!resolv) {
+		return -ENOMEM;
+	}
 
-	/*
-	 * Before we start creating the new timeserver list we must stop
-	 * any ongoing ntp query and server resolution.
-	 */
+	if (getenv("CONNMAN_RESOLV_DEBUG"))
+		g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
 
-	__connman_ntp_stop();
+	return 0;
+}
 
-	ts_recheck_disable();
+
+static void ts_set_nameservers(struct connman_service *service)
+{
+	char **nameservers;
+	int i;
 
 	if (resolv_id > 0)
 		g_resolv_cancel_lookup(resolv, resolv_id);
@@ -378,6 +391,27 @@ static void ts_reset(struct connman_service *service)
 
 		g_strfreev(nameservers);
 	}
+}
+
+static void ts_reset(struct connman_service *service)
+{
+	int i;
+
+	if (!resolv)
+		return;
+
+	__connman_timeserver_set_synced(false);
+
+	/*
+	 * Before we start creating the new timeserver list we must stop
+	 * any ongoing ntp query and server resolution.
+	 */
+
+	__connman_ntp_stop();
+
+	ts_recheck_disable();
+
+	ts_set_nameservers(service);
 
 	g_slist_free_full(timeservers_list, g_free);
 
@@ -434,42 +468,17 @@ void __connman_timeserver_set_synced(bool status)
 
 static int timeserver_start(struct connman_service *service)
 {
-	char **nameservers;
 	int i;
+	int rv;
 
 	DBG("service %p", service);
 
-	i = __connman_service_get_index(service);
-	if (i < 0)
-		return -EINVAL;
-
-	nameservers = connman_service_get_nameservers(service);
-
-	/* Stop an already ongoing resolution, if there is one */
-	if (resolv && resolv_id > 0)
-		g_resolv_cancel_lookup(resolv, resolv_id);
-
 	/* get rid of the old resolver */
-	if (resolv) {
-		g_resolv_unref(resolv);
-		resolv = NULL;
-	}
-
-	resolv = g_resolv_new(i);
-	if (!resolv) {
-		g_strfreev(nameservers);
-		return -ENOMEM;
-	}
+	rv = ts_setup_resolv(service);
+	if (rv)
+		return rv;
 
-	if (getenv("CONNMAN_RESOLV_DEBUG"))
-		g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
-
-	if (nameservers) {
-		for (i = 0; nameservers[i]; i++)
-			g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
-
-		g_strfreev(nameservers);
-	}
+	ts_set_nameservers(service);
 
 	__connman_timeserver_sync(service);
 
-- 
2.25.1


  parent reply	other threads:[~2022-03-07  8:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 12:49 [PATCH 0/2] timeserver not resolving ntp server Nicky Geerts
2022-02-28 12:49 ` [PATCH 1/2] timeserver: refresh the nameservers before each lookup Nicky Geerts
2022-02-28 12:49 ` [PATCH 2/2] timeserver: include the reason why a timeserver sync is requested Nicky Geerts
2022-03-04  8:56 ` [PATCH 0/2] timeserver not resolving ntp server Daniel Wagner
2022-03-07  8:44 ` Nicky Geerts [this message]
2022-03-07  9:19   ` [PATCH 1/2] timeserver: refresh the nameservers before each lookup Daniel Wagner
2022-03-07  8:44 ` [PATCH 2/2] timeserver: include the reason why a timeserver sync is requested Nicky Geerts
2022-03-07  9:20   ` Daniel Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220307084432.62446-2-nicky.geerts@gmail.com \
    --to=nicky.geerts@gmail.com \
    --cc=connman@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).