5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <gweb/gresolv.h>
35 #define TS_RECHECK_INTERVAL 7200
37 static struct connman_service *ts_service;
38 static GSList *timeservers_list = NULL;
39 static GSList *ts_list = NULL;
40 static char *ts_current = NULL;
41 static int ts_recheck_id = 0;
42 static int ts_backoff_id = 0;
44 static GResolv *resolv = NULL;
45 static int resolv_id = 0;
47 static void sync_next(void);
49 static void resolv_debug(const char *str, void *data)
51 connman_info("%s: %s\n", (const char *) data, str);
54 static void ntp_callback(bool success, void *user_data)
56 DBG("success %d", success);
62 static void save_timeservers(char **servers)
67 keyfile = __connman_storage_load_global();
69 keyfile = g_key_file_new();
71 for (cnt = 0; servers && servers[cnt]; cnt++);
73 g_key_file_set_string_list(keyfile, "global", "Timeservers",
74 (const gchar **)servers, cnt);
76 __connman_storage_save_global(keyfile);
78 g_key_file_free(keyfile);
81 static char **load_timeservers(void)
84 char **servers = NULL;
86 keyfile = __connman_storage_load_global();
90 servers = g_key_file_get_string_list(keyfile, "global",
91 "Timeservers", NULL, NULL);
93 g_key_file_free(keyfile);
98 static void resolv_result(GResolvResultStatus status, char **results,
103 DBG("status %d", status);
105 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
107 /* prepend the results in reverse order */
109 for (i = 0; results[i]; i++)
113 for (; i >= 0; i--) {
114 DBG("result[%d]: %s", i, results[i]);
116 ts_list = __connman_timeserver_add_list(
117 ts_list, results[i]);
126 * Once the timeserver list (timeserver_list) is created, we start
127 * querying the servers one by one. If resolving fails on one of them,
128 * we move to the next one. The user can enter either an IP address or
129 * a URL for the timeserver. We only resolve the URLs. Once we have an
130 * IP for the NTP server, we start querying it for time corrections.
132 static void timeserver_sync_start(void)
136 for (list = timeservers_list; list; list = list->next) {
137 char *timeserver = list->data;
139 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
141 ts_list = g_slist_reverse(ts_list);
146 static gboolean timeserver_sync_restart(gpointer user_data)
148 timeserver_sync_start();
155 * Select the next time server from the working list (ts_list) because
156 * for some reason the first time server in the list didn't work. If
157 * none of the server did work we start over with the first server
160 static void sync_next(void)
167 __connman_ntp_stop();
170 ts_current = ts_list->data;
171 ts_list = g_slist_delete_link(ts_list, ts_list);
173 /* if it's an IP, directly query it. */
174 if (connman_inet_check_ipaddress(ts_current) > 0) {
175 DBG("Using timeserver %s", ts_current);
176 __connman_ntp_start(ts_current, ntp_callback, NULL);
180 DBG("Resolving timeserver %s", ts_current);
181 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
182 resolv_result, NULL);
186 DBG("No timeserver could be used, restart probing in 5 seconds");
187 ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
190 GSList *__connman_timeserver_add_list(GSList *server_list,
191 const char *timeserver)
193 GSList *list = server_list;
199 char *existing_server = list->data;
200 if (strcmp(timeserver, existing_server) == 0)
202 list = g_slist_next(list);
204 return g_slist_prepend(server_list, g_strdup(timeserver));
208 * __connman_timeserver_get_all function creates the timeserver
209 * list which will be used to determine NTP server for time corrections.
210 * The service settings take priority over the global timeservers.
212 GSList *__connman_timeserver_get_all(struct connman_service *service)
215 struct connman_network *network;
218 char **service_ts_config;
219 const char *service_gw;
223 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
226 service_ts_config = connman_service_get_timeservers_config(service);
228 /* First add Service Timeservers.Configuration to the list */
229 for (i = 0; service_ts_config && service_ts_config[i];
231 list = __connman_timeserver_add_list(list,
232 service_ts_config[i]);
234 service_ts = connman_service_get_timeservers(service);
236 /* Then add Service Timeservers via DHCP to the list */
237 for (i = 0; service_ts && service_ts[i]; i++)
238 list = __connman_timeserver_add_list(list, service_ts[i]);
241 * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
242 * configuration option is set to true.
244 if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
245 network = __connman_service_get_network(service);
247 index = connman_network_get_index(network);
248 service_gw = __connman_ipconfig_get_gateway_from_index(index,
249 CONNMAN_IPCONFIG_TYPE_ALL);
252 list = __connman_timeserver_add_list(list, service_gw);
256 /* Then add Global Timeservers to the list */
257 timeservers = load_timeservers();
259 for (i = 0; timeservers && timeservers[i]; i++)
260 list = __connman_timeserver_add_list(list, timeservers[i]);
262 g_strfreev(timeservers);
264 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
266 /* Lastly add the fallback servers */
267 for (i = 0; fallback_ts && fallback_ts[i]; i++)
268 list = __connman_timeserver_add_list(list, fallback_ts[i]);
270 return g_slist_reverse(list);
273 static gboolean ts_recheck(gpointer user_data)
277 ts = __connman_timeserver_get_all(connman_service_get_default());
280 DBG("timeservers disabled");
285 if (g_strcmp0(ts_current, ts->data) != 0) {
286 DBG("current %s preferred %s", ts_current, (char *)ts->data);
288 g_slist_free_full(ts, g_free);
290 __connman_timeserver_sync(NULL);
297 g_slist_free_full(ts, g_free);
302 static void ts_recheck_disable(void)
304 if (ts_recheck_id == 0)
307 g_source_remove(ts_recheck_id);
311 g_source_remove(ts_backoff_id);
321 static void ts_recheck_enable(void)
323 if (ts_recheck_id > 0)
326 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
331 * This function must be called everytime the default service changes, the
332 * service timeserver(s) or gateway changes or the global timeserver(s) changes.
334 int __connman_timeserver_sync(struct connman_service *default_service)
336 struct connman_service *service;
341 service = default_service;
343 service = connman_service_get_default();
348 if (service == ts_service)
354 * Before we start creating the new timeserver list we must stop
355 * any ongoing ntp query and server resolution.
358 __connman_ntp_stop();
360 ts_recheck_disable();
363 g_resolv_cancel_lookup(resolv, resolv_id);
365 g_resolv_flush_nameservers(resolv);
367 nameservers = connman_service_get_nameservers(service);
371 for (i = 0; nameservers[i]; i++)
372 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
374 g_strfreev(nameservers);
376 g_slist_free_full(timeservers_list, g_free);
378 timeservers_list = __connman_timeserver_get_all(service);
380 __connman_service_timeserver_changed(service, timeservers_list);
382 if (!timeservers_list) {
383 DBG("No timeservers set.");
389 ts_service = service;
390 timeserver_sync_start();
395 static int timeserver_start(struct connman_service *service)
400 DBG("service %p", service);
402 i = __connman_service_get_index(service);
406 nameservers = connman_service_get_nameservers(service);
408 /* Stop an already ongoing resolution, if there is one */
409 if (resolv && resolv_id > 0)
410 g_resolv_cancel_lookup(resolv, resolv_id);
412 /* get rid of the old resolver */
414 g_resolv_unref(resolv);
418 resolv = g_resolv_new(i);
420 g_strfreev(nameservers);
424 if (getenv("CONNMAN_RESOLV_DEBUG"))
425 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
428 for (i = 0; nameservers[i]; i++)
429 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
431 g_strfreev(nameservers);
434 return __connman_timeserver_sync(service);
437 static void timeserver_stop(void)
444 g_resolv_unref(resolv);
448 g_slist_free_full(timeservers_list, g_free);
449 timeservers_list = NULL;
451 g_slist_free_full(ts_list, g_free);
454 __connman_ntp_stop();
456 ts_recheck_disable();
459 int __connman_timeserver_system_set(char **servers)
461 save_timeservers(servers);
463 __connman_timeserver_sync(NULL);
468 char **__connman_timeserver_system_get()
472 servers = load_timeservers();
476 static void default_changed(struct connman_service *default_service)
479 timeserver_start(default_service);
484 static const struct connman_notifier timeserver_notifier = {
485 .name = "timeserver",
486 .default_changed = default_changed,
489 int __connman_timeserver_init(void)
493 connman_notifier_register(×erver_notifier);
498 void __connman_timeserver_cleanup(void)
502 connman_notifier_unregister(×erver_notifier);