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 #if defined TIZEN_EXT
104 gchar *server = NULL;
106 server = g_strdup((gchar *)user_data);
107 ts_list = g_slist_append(ts_list, server);
109 DBG("added server %s", server);
112 DBG("status %d", status);
114 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
116 /* prepend the results in reverse order */
118 for (i = 0; results[i]; i++)
122 for (; i >= 0; i--) {
123 DBG("result[%d]: %s", i, results[i]);
125 ts_list = __connman_timeserver_add_list(
126 ts_list, results[i]);
135 * Once the timeserver list (timeserver_list) is created, we start
136 * querying the servers one by one. If resolving fails on one of them,
137 * we move to the next one. The user can enter either an IP address or
138 * a URL for the timeserver. We only resolve the URLs. Once we have an
139 * IP for the NTP server, we start querying it for time corrections.
141 static void timeserver_sync_start(void)
145 for (list = timeservers_list; list; list = list->next) {
146 char *timeserver = list->data;
148 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
150 ts_list = g_slist_reverse(ts_list);
155 static gboolean timeserver_sync_restart(gpointer user_data)
157 timeserver_sync_start();
164 * Select the next time server from the working list (ts_list) because
165 * for some reason the first time server in the list didn't work. If
166 * none of the server did work we start over with the first server
169 static void sync_next(void)
176 __connman_ntp_stop();
179 ts_current = ts_list->data;
180 ts_list = g_slist_delete_link(ts_list, ts_list);
182 /* if it's an IP, directly query it. */
183 if (connman_inet_check_ipaddress(ts_current) > 0) {
184 DBG("Using timeserver %s", ts_current);
185 __connman_ntp_start(ts_current, ntp_callback, NULL);
189 DBG("Resolving timeserver %s", ts_current);
190 #if defined TIZEN_EXT
191 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
192 resolv_result, ts_current);
194 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
195 resolv_result, NULL);
200 DBG("No timeserver could be used, restart probing in 5 seconds");
201 ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
204 GSList *__connman_timeserver_add_list(GSList *server_list,
205 const char *timeserver)
207 GSList *list = server_list;
213 char *existing_server = list->data;
214 if (strcmp(timeserver, existing_server) == 0)
216 list = g_slist_next(list);
218 return g_slist_prepend(server_list, g_strdup(timeserver));
222 * __connman_timeserver_get_all function creates the timeserver
223 * list which will be used to determine NTP server for time corrections.
224 * The service settings take priority over the global timeservers.
226 GSList *__connman_timeserver_get_all(struct connman_service *service)
229 struct connman_network *network;
232 char **service_ts_config;
233 const char *service_gw;
237 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
240 service_ts_config = connman_service_get_timeservers_config(service);
242 /* First add Service Timeservers.Configuration to the list */
243 for (i = 0; service_ts_config && service_ts_config[i];
245 list = __connman_timeserver_add_list(list,
246 service_ts_config[i]);
248 service_ts = connman_service_get_timeservers(service);
250 /* Then add Service Timeservers via DHCP to the list */
251 for (i = 0; service_ts && service_ts[i]; i++)
252 list = __connman_timeserver_add_list(list, service_ts[i]);
255 * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
256 * configuration option is set to true.
258 if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
259 network = __connman_service_get_network(service);
261 index = connman_network_get_index(network);
262 service_gw = __connman_ipconfig_get_gateway_from_index(index,
263 CONNMAN_IPCONFIG_TYPE_ALL);
266 list = __connman_timeserver_add_list(list, service_gw);
270 /* Then add Global Timeservers to the list */
271 timeservers = load_timeservers();
273 for (i = 0; timeservers && timeservers[i]; i++)
274 list = __connman_timeserver_add_list(list, timeservers[i]);
276 g_strfreev(timeservers);
278 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
280 /* Lastly add the fallback servers */
281 for (i = 0; fallback_ts && fallback_ts[i]; i++)
282 list = __connman_timeserver_add_list(list, fallback_ts[i]);
284 return g_slist_reverse(list);
287 static gboolean ts_recheck(gpointer user_data)
291 ts = __connman_timeserver_get_all(connman_service_get_default());
294 DBG("timeservers disabled");
299 if (g_strcmp0(ts_current, ts->data) != 0) {
300 DBG("current %s preferred %s", ts_current, (char *)ts->data);
302 g_slist_free_full(ts, g_free);
304 __connman_timeserver_sync(NULL);
311 g_slist_free_full(ts, g_free);
316 static void ts_recheck_disable(void)
318 if (ts_recheck_id == 0)
321 g_source_remove(ts_recheck_id);
325 g_source_remove(ts_backoff_id);
335 static void ts_recheck_enable(void)
337 if (ts_recheck_id > 0)
340 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
345 * This function must be called everytime the default service changes, the
346 * service timeserver(s) or gateway changes or the global timeserver(s) changes.
348 int __connman_timeserver_sync(struct connman_service *default_service)
350 struct connman_service *service;
355 service = default_service;
357 service = connman_service_get_default();
362 #if !defined TIZEN_EXT
363 if (service == ts_service)
370 * Before we start creating the new timeserver list we must stop
371 * any ongoing ntp query and server resolution.
374 __connman_ntp_stop();
376 ts_recheck_disable();
379 g_resolv_cancel_lookup(resolv, resolv_id);
381 g_resolv_flush_nameservers(resolv);
383 nameservers = connman_service_get_nameservers(service);
387 for (i = 0; nameservers[i]; i++)
388 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
390 g_strfreev(nameservers);
392 g_slist_free_full(timeservers_list, g_free);
394 timeservers_list = __connman_timeserver_get_all(service);
396 __connman_service_timeserver_changed(service, timeservers_list);
398 if (!timeservers_list) {
399 DBG("No timeservers set.");
405 ts_service = service;
406 timeserver_sync_start();
411 static int timeserver_start(struct connman_service *service)
416 DBG("service %p", service);
418 i = __connman_service_get_index(service);
422 nameservers = connman_service_get_nameservers(service);
424 /* Stop an already ongoing resolution, if there is one */
425 if (resolv && resolv_id > 0)
426 g_resolv_cancel_lookup(resolv, resolv_id);
428 /* get rid of the old resolver */
430 g_resolv_unref(resolv);
434 resolv = g_resolv_new(i);
436 g_strfreev(nameservers);
440 if (getenv("CONNMAN_RESOLV_DEBUG"))
441 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
444 for (i = 0; nameservers[i]; i++)
445 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
447 g_strfreev(nameservers);
450 return __connman_timeserver_sync(service);
453 static void timeserver_stop(void)
460 g_resolv_unref(resolv);
464 g_slist_free_full(timeservers_list, g_free);
465 timeservers_list = NULL;
467 g_slist_free_full(ts_list, g_free);
470 __connman_ntp_stop();
472 ts_recheck_disable();
475 int __connman_timeserver_system_set(char **servers)
477 save_timeservers(servers);
479 __connman_timeserver_sync(NULL);
484 char **__connman_timeserver_system_get()
488 servers = load_timeservers();
492 static void default_changed(struct connman_service *default_service)
495 timeserver_start(default_service);
500 static const struct connman_notifier timeserver_notifier = {
501 .name = "timeserver",
502 .default_changed = default_changed,
505 int __connman_timeserver_init(void)
509 connman_notifier_register(×erver_notifier);
514 void __connman_timeserver_cleanup(void)
518 connman_notifier_unregister(×erver_notifier);