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>
36 #define TS_RECHECK_INTERVAL 7200
38 static struct connman_service *ts_service;
39 static GSList *timeservers_list = NULL;
40 static GSList *ts_list = NULL;
41 static char *ts_current = NULL;
42 static int ts_recheck_id = 0;
43 static int ts_backoff_id = 0;
44 static bool ts_is_synced = false;
46 static GResolv *resolv = NULL;
47 static int resolv_id = 0;
49 static void sync_next(void);
51 static void resolv_debug(const char *str, void *data)
53 connman_info("%s: %s\n", (const char *) data, str);
56 static void ntp_callback(bool success, void *user_data)
58 dbus_uint64_t timestamp;
61 DBG("success %d", success);
63 __connman_timeserver_set_synced(success);
69 if (gettimeofday(&tv, NULL) < 0) {
70 connman_warn("Failed to get current time");
73 timestamp = tv.tv_sec;
74 connman_dbus_property_changed_basic(
76 CONNMAN_CLOCK_INTERFACE, "Time",
77 DBUS_TYPE_UINT64, ×tamp);
80 static void save_timeservers(char **servers)
85 keyfile = __connman_storage_load_global();
87 keyfile = g_key_file_new();
89 for (cnt = 0; servers && servers[cnt]; cnt++);
91 g_key_file_set_string_list(keyfile, "global", "Timeservers",
92 (const gchar **)servers, cnt);
94 __connman_storage_save_global(keyfile);
96 g_key_file_free(keyfile);
99 static char **load_timeservers(void)
102 char **servers = NULL;
104 keyfile = __connman_storage_load_global();
108 servers = g_key_file_get_string_list(keyfile, "global",
109 "Timeservers", NULL, NULL);
111 g_key_file_free(keyfile);
116 static void resolv_result(GResolvResultStatus status, char **results,
121 #if defined TIZEN_EXT
122 gchar *server = NULL;
124 server = g_strdup((gchar *)user_data);
125 ts_list = g_slist_append(ts_list, server);
128 DBG("added server %s", server);
132 DBG("status %d", status);
134 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
136 /* prepend the results in reverse order */
138 for (i = 0; results[i]; i++)
142 for (; i >= 0; i--) {
143 DBG("result[%d]: %s", i, results[i]);
145 ts_list = __connman_timeserver_add_list(
146 ts_list, results[i]);
155 * Once the timeserver list (timeserver_list) is created, we start
156 * querying the servers one by one. If resolving fails on one of them,
157 * we move to the next one. The user can enter either an IP address or
158 * a URL for the timeserver. We only resolve the URLs. Once we have an
159 * IP for the NTP server, we start querying it for time corrections.
161 static void timeserver_sync_start(void)
165 for (list = timeservers_list; list; list = list->next) {
166 char *timeserver = list->data;
168 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
170 ts_list = g_slist_reverse(ts_list);
175 static gboolean timeserver_sync_restart(gpointer user_data)
177 timeserver_sync_start();
184 * Select the next time server from the working list (ts_list) because
185 * for some reason the first time server in the list didn't work. If
186 * none of the server did work we start over with the first server
189 static void sync_next(void)
196 __connman_ntp_stop();
199 ts_current = ts_list->data;
200 ts_list = g_slist_delete_link(ts_list, ts_list);
202 /* if it's an IP, directly query it. */
203 if (connman_inet_check_ipaddress(ts_current) > 0) {
204 DBG("Using timeserver %s", ts_current);
205 __connman_ntp_start(ts_current, ntp_callback, NULL);
208 #if defined TIZEN_EXT
209 if (ts_current[0] == '\0') {
210 DBG("current time server is empty. ignore next time server..");
216 DBG("Resolving timeserver %s", ts_current);
217 #if defined TIZEN_EXT
218 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
219 resolv_result, ts_current);
221 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
222 resolv_result, NULL);
227 DBG("No timeserver could be used, restart probing in 5 seconds");
228 ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
231 GSList *__connman_timeserver_add_list(GSList *server_list,
232 const char *timeserver)
234 GSList *list = server_list;
240 char *existing_server = list->data;
241 if (strcmp(timeserver, existing_server) == 0)
243 list = g_slist_next(list);
245 return g_slist_prepend(server_list, g_strdup(timeserver));
249 * __connman_timeserver_get_all function creates the timeserver
250 * list which will be used to determine NTP server for time corrections.
251 * The service settings take priority over the global timeservers.
253 GSList *__connman_timeserver_get_all(struct connman_service *service)
256 struct connman_network *network;
259 char **service_ts_config;
260 const char *service_gw;
264 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
267 service_ts_config = connman_service_get_timeservers_config(service);
269 /* First add Service Timeservers.Configuration to the list */
270 for (i = 0; service_ts_config && service_ts_config[i];
272 list = __connman_timeserver_add_list(list,
273 service_ts_config[i]);
275 service_ts = connman_service_get_timeservers(service);
277 /* Then add Service Timeservers via DHCP to the list */
278 for (i = 0; service_ts && service_ts[i]; i++)
279 list = __connman_timeserver_add_list(list, service_ts[i]);
282 * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
283 * configuration option is set to true.
285 if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
286 network = __connman_service_get_network(service);
288 index = connman_network_get_index(network);
289 service_gw = __connman_ipconfig_get_gateway_from_index(index,
290 CONNMAN_IPCONFIG_TYPE_ALL);
293 list = __connman_timeserver_add_list(list, service_gw);
297 /* Then add Global Timeservers to the list */
298 timeservers = load_timeservers();
300 for (i = 0; timeservers && timeservers[i]; i++)
301 list = __connman_timeserver_add_list(list, timeservers[i]);
303 g_strfreev(timeservers);
305 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
307 /* Lastly add the fallback servers */
308 for (i = 0; fallback_ts && fallback_ts[i]; i++)
309 list = __connman_timeserver_add_list(list, fallback_ts[i]);
311 return g_slist_reverse(list);
314 static gboolean ts_recheck(gpointer user_data)
316 struct connman_service *service;
319 ts = __connman_timeserver_get_all(connman_service_get_default());
322 DBG("timeservers disabled");
327 if (g_strcmp0(ts_current, ts->data) != 0) {
328 DBG("current %s preferred %s", ts_current, (char *)ts->data);
330 g_slist_free_full(ts, g_free);
332 service = connman_service_get_default();
333 __connman_timeserver_sync(service);
340 g_slist_free_full(ts, g_free);
345 static void ts_recheck_disable(void)
347 if (ts_recheck_id == 0)
350 g_source_remove(ts_recheck_id);
354 g_source_remove(ts_backoff_id);
364 static void ts_recheck_enable(void)
366 if (ts_recheck_id > 0)
369 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
373 static void ts_reset(struct connman_service *service)
381 __connman_timeserver_set_synced(false);
384 * Before we start creating the new timeserver list we must stop
385 * any ongoing ntp query and server resolution.
388 __connman_ntp_stop();
390 ts_recheck_disable();
393 g_resolv_cancel_lookup(resolv, resolv_id);
395 g_resolv_flush_nameservers(resolv);
397 nameservers = connman_service_get_nameservers(service);
399 for (i = 0; nameservers[i]; i++)
400 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
402 g_strfreev(nameservers);
405 g_slist_free_full(timeservers_list, g_free);
407 timeservers_list = __connman_timeserver_get_all(service);
409 __connman_service_timeserver_changed(service, timeservers_list);
411 if (!timeservers_list) {
412 DBG("No timeservers set.");
418 ts_service = service;
419 timeserver_sync_start();
422 void __connman_timeserver_sync(struct connman_service *service)
424 if (!service || ts_service == service)
430 void __connman_timeserver_conf_update(struct connman_service *service)
432 if (!service || (ts_service && ts_service != service))
439 bool __connman_timeserver_is_synced(void)
444 void __connman_timeserver_set_synced(bool status)
446 dbus_bool_t is_synced;
448 if (ts_is_synced == status)
451 ts_is_synced = status;
453 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
454 CONNMAN_CLOCK_INTERFACE, "TimeserverSynced",
455 DBUS_TYPE_BOOLEAN, &is_synced);
458 static int timeserver_start(struct connman_service *service)
463 DBG("service %p", service);
465 i = __connman_service_get_index(service);
469 nameservers = connman_service_get_nameservers(service);
471 /* Stop an already ongoing resolution, if there is one */
472 if (resolv && resolv_id > 0)
473 g_resolv_cancel_lookup(resolv, resolv_id);
475 /* get rid of the old resolver */
477 g_resolv_unref(resolv);
481 resolv = g_resolv_new(i);
483 g_strfreev(nameservers);
487 if (getenv("CONNMAN_RESOLV_DEBUG"))
488 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
491 for (i = 0; nameservers[i]; i++)
492 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
494 g_strfreev(nameservers);
497 __connman_timeserver_sync(service);
502 static void timeserver_stop(void)
509 g_resolv_unref(resolv);
513 g_slist_free_full(timeservers_list, g_free);
514 timeservers_list = NULL;
516 g_slist_free_full(ts_list, g_free);
519 __connman_ntp_stop();
521 ts_recheck_disable();
524 int __connman_timeserver_system_set(char **servers)
526 struct connman_service *service;
528 save_timeservers(servers);
530 service = connman_service_get_default();
537 char **__connman_timeserver_system_get()
541 servers = load_timeservers();
545 static void default_changed(struct connman_service *default_service)
548 timeserver_start(default_service);
553 static const struct connman_notifier timeserver_notifier = {
554 .name = "timeserver",
555 .default_changed = default_changed,
558 int __connman_timeserver_init(void)
562 connman_notifier_register(×erver_notifier);
567 void __connman_timeserver_cleanup(void)
571 connman_notifier_unregister(×erver_notifier);