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
211 DBG("Resolving timeserver %s", ts_current);
212 #if defined TIZEN_EXT
213 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
214 resolv_result, ts_current);
216 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
217 resolv_result, NULL);
222 DBG("No timeserver could be used, restart probing in 5 seconds");
223 ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
226 GSList *__connman_timeserver_add_list(GSList *server_list,
227 const char *timeserver)
229 GSList *list = server_list;
235 char *existing_server = list->data;
236 if (strcmp(timeserver, existing_server) == 0)
238 list = g_slist_next(list);
240 return g_slist_prepend(server_list, g_strdup(timeserver));
244 * __connman_timeserver_get_all function creates the timeserver
245 * list which will be used to determine NTP server for time corrections.
246 * The service settings take priority over the global timeservers.
248 GSList *__connman_timeserver_get_all(struct connman_service *service)
251 struct connman_network *network;
254 char **service_ts_config;
255 const char *service_gw;
259 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
262 service_ts_config = connman_service_get_timeservers_config(service);
264 /* First add Service Timeservers.Configuration to the list */
265 for (i = 0; service_ts_config && service_ts_config[i];
267 list = __connman_timeserver_add_list(list,
268 service_ts_config[i]);
270 service_ts = connman_service_get_timeservers(service);
272 /* Then add Service Timeservers via DHCP to the list */
273 for (i = 0; service_ts && service_ts[i]; i++)
274 list = __connman_timeserver_add_list(list, service_ts[i]);
277 * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
278 * configuration option is set to true.
280 if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
281 network = __connman_service_get_network(service);
283 index = connman_network_get_index(network);
284 service_gw = __connman_ipconfig_get_gateway_from_index(index,
285 CONNMAN_IPCONFIG_TYPE_ALL);
288 list = __connman_timeserver_add_list(list, service_gw);
292 /* Then add Global Timeservers to the list */
293 timeservers = load_timeservers();
295 for (i = 0; timeservers && timeservers[i]; i++)
296 list = __connman_timeserver_add_list(list, timeservers[i]);
298 g_strfreev(timeservers);
300 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
302 /* Lastly add the fallback servers */
303 for (i = 0; fallback_ts && fallback_ts[i]; i++)
304 list = __connman_timeserver_add_list(list, fallback_ts[i]);
306 return g_slist_reverse(list);
309 static gboolean ts_recheck(gpointer user_data)
311 struct connman_service *service;
314 ts = __connman_timeserver_get_all(connman_service_get_default());
317 DBG("timeservers disabled");
322 if (g_strcmp0(ts_current, ts->data) != 0) {
323 DBG("current %s preferred %s", ts_current, (char *)ts->data);
325 g_slist_free_full(ts, g_free);
327 service = connman_service_get_default();
328 __connman_timeserver_sync(service);
335 g_slist_free_full(ts, g_free);
340 static void ts_recheck_disable(void)
342 if (ts_recheck_id == 0)
345 g_source_remove(ts_recheck_id);
349 g_source_remove(ts_backoff_id);
359 static void ts_recheck_enable(void)
361 if (ts_recheck_id > 0)
364 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
368 static void ts_reset(struct connman_service *service)
376 __connman_timeserver_set_synced(false);
379 * Before we start creating the new timeserver list we must stop
380 * any ongoing ntp query and server resolution.
383 __connman_ntp_stop();
385 ts_recheck_disable();
388 g_resolv_cancel_lookup(resolv, resolv_id);
390 g_resolv_flush_nameservers(resolv);
392 nameservers = connman_service_get_nameservers(service);
394 for (i = 0; nameservers[i]; i++)
395 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
397 g_strfreev(nameservers);
400 g_slist_free_full(timeservers_list, g_free);
402 timeservers_list = __connman_timeserver_get_all(service);
404 __connman_service_timeserver_changed(service, timeservers_list);
406 if (!timeservers_list) {
407 DBG("No timeservers set.");
413 ts_service = service;
414 timeserver_sync_start();
417 void __connman_timeserver_sync(struct connman_service *service)
419 if (!service || ts_service == service)
425 void __connman_timeserver_conf_update(struct connman_service *service)
427 if (!service || (ts_service && ts_service != service))
434 bool __connman_timeserver_is_synced(void)
439 void __connman_timeserver_set_synced(bool status)
441 dbus_bool_t is_synced;
443 if (ts_is_synced == status)
446 ts_is_synced = status;
448 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
449 CONNMAN_CLOCK_INTERFACE, "TimeserverSynced",
450 DBUS_TYPE_BOOLEAN, &is_synced);
453 static int timeserver_start(struct connman_service *service)
458 DBG("service %p", service);
460 i = __connman_service_get_index(service);
464 nameservers = connman_service_get_nameservers(service);
466 /* Stop an already ongoing resolution, if there is one */
467 if (resolv && resolv_id > 0)
468 g_resolv_cancel_lookup(resolv, resolv_id);
470 /* get rid of the old resolver */
472 g_resolv_unref(resolv);
476 resolv = g_resolv_new(i);
478 g_strfreev(nameservers);
482 if (getenv("CONNMAN_RESOLV_DEBUG"))
483 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
486 for (i = 0; nameservers[i]; i++)
487 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
489 g_strfreev(nameservers);
492 __connman_timeserver_sync(service);
497 static void timeserver_stop(void)
504 g_resolv_unref(resolv);
508 g_slist_free_full(timeservers_list, g_free);
509 timeservers_list = NULL;
511 g_slist_free_full(ts_list, g_free);
514 __connman_ntp_stop();
516 ts_recheck_disable();
519 int __connman_timeserver_system_set(char **servers)
521 struct connman_service *service;
523 save_timeservers(servers);
525 service = connman_service_get_default();
532 char **__connman_timeserver_system_get()
536 servers = load_timeservers();
540 static void default_changed(struct connman_service *default_service)
543 timeserver_start(default_service);
548 static const struct connman_notifier timeserver_notifier = {
549 .name = "timeserver",
550 .default_changed = default_changed,
553 int __connman_timeserver_init(void)
557 connman_notifier_register(×erver_notifier);
562 void __connman_timeserver_cleanup(void)
566 connman_notifier_unregister(×erver_notifier);