From f97c14e2606c64df0ae23c81a8a4066d27367091 Mon Sep 17 00:00:00 2001 From: Alok Barsode Date: Fri, 3 Feb 2012 20:26:01 +0200 Subject: [PATCH] ntp: Add timeserver switch logic Add a timeout mechanism to switch to next system timeserver if the current one does not respond or is a bogus one. --- src/ntp.c | 29 ++++++++++++++++++++++++++++- src/timeserver.c | 4 +++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/ntp.c b/src/ntp.c index 4d3cd88..cbe5a3b 100644 --- a/src/ntp.c +++ b/src/ntp.c @@ -75,6 +75,7 @@ static int transmit_fd = 0; static char *timeserver = NULL; static gint poll_id = 0; +static gint timeout_id = 0; static void send_packet(int fd, const char *server) { @@ -109,6 +110,18 @@ static void send_packet(int fd, const char *server) } } +static gboolean next_server(gpointer user_data) +{ + if (timeserver != NULL) { + g_free(timeserver); + timeserver = NULL; + } + + __connman_timeserver_sync_next(); + + return FALSE; +} + static gboolean next_poll(gpointer user_data) { if (timeserver == NULL || transmit_fd == 0) @@ -168,7 +181,11 @@ static void decode_msg(void *base, size_t len, struct timeval *tv) DBG("offset=%f delay=%f", offset, delay); - /* Timeserver has responded. + /* Remove the timeout, as timeserver has responded */ + if (timeout_id > 0) + g_source_remove(timeout_id); + + /* * Now poll the server every transmit_delay seconds * for time correction. */ @@ -341,6 +358,13 @@ int __connman_ntp_start(char *server) start_ntp(timeserver); + /* + * Add a fallback timeout , preferably longer, 16 sec here, + * to fallback on the next server. + */ + + timeout_id = g_timeout_add_seconds(16, next_server, NULL); + return 0; } @@ -351,6 +375,9 @@ void __connman_ntp_stop() if (poll_id > 0) g_source_remove(poll_id); + if (timeout_id > 0) + g_source_remove(timeout_id); + if (channel_watch > 0) { g_source_remove(channel_watch); channel_watch = 0; diff --git a/src/timeserver.c b/src/timeserver.c index 2af173d..a361433 100644 --- a/src/timeserver.c +++ b/src/timeserver.c @@ -230,6 +230,8 @@ static void resolv_result(GResolvResultStatus status, char **results, gpointer u DBG("status %d", status); + __sync_fetch_and_add(&count, 1); + if (status == G_RESOLV_RESULT_STATUS_SUCCESS) { if (results != NULL) { for (i = 0; results[i]; i++) @@ -240,7 +242,7 @@ static void resolv_result(GResolvResultStatus status, char **results, gpointer u return; } } - __sync_fetch_and_add(&count, 1); + __connman_timeserver_sync_next(); } -- 2.7.4