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 GSList *ts_list = NULL;
38 static char *ts_current = NULL;
39 static int ts_recheck_id = 0;
41 static GResolv *resolv = NULL;
42 static int resolv_id = 0;
44 static void resolv_debug(const char *str, void *data)
46 connman_info("%s: %s\n", (const char *) data, str);
48 static void save_timeservers(char **servers)
53 keyfile = __connman_storage_load_global();
55 keyfile = g_key_file_new();
57 for (cnt = 0; servers && servers[cnt]; cnt++);
59 g_key_file_set_string_list(keyfile, "global", "Timeservers",
60 (const gchar **)servers, cnt);
62 __connman_storage_save_global(keyfile);
64 g_key_file_free(keyfile);
69 static char **load_timeservers(void)
72 char **servers = NULL;
74 keyfile = __connman_storage_load_global();
78 servers = g_key_file_get_string_list(keyfile, "global",
79 "Timeservers", NULL, NULL);
81 g_key_file_free(keyfile);
86 static void resolv_result(GResolvResultStatus status, char **results,
91 DBG("status %d", status);
93 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
95 for (i = 0; results[i]; i++) {
96 DBG("result[%d]: %s", i, results[i]);
100 ts_list = __connman_timeserver_add_list(
101 ts_list, results[i]);
104 DBG("Using timeserver %s", results[0]);
106 __connman_ntp_start(results[0]);
112 /* If resolving fails, move to the next server */
113 __connman_timeserver_sync_next();
117 * Once the timeserver list (ts_list) is created, we start querying the
118 * servers one by one. If resolving fails on one of them, we move to the
119 * next one. The user can enter either an IP address or a URL for the
120 * timeserver. We only resolve the URLs. Once we have an IP for the NTP
121 * server, we start querying it for time corrections.
123 void __connman_timeserver_sync_next()
130 __connman_ntp_stop();
132 /* Get the 1st server in the list */
136 ts_current = ts_list->data;
138 ts_list = g_slist_delete_link(ts_list, ts_list);
140 /* if it's an IP, directly query it. */
141 if (connman_inet_check_ipaddress(ts_current) > 0) {
142 DBG("Using timeserver %s", ts_current);
144 __connman_ntp_start(ts_current);
149 DBG("Resolving timeserver %s", ts_current);
151 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
152 resolv_result, NULL);
157 GSList *__connman_timeserver_add_list(GSList *server_list,
158 const char *timeserver)
160 GSList *list = server_list;
166 char *existing_server = list->data;
167 if (strcmp(timeserver, existing_server) == 0)
169 list = g_slist_next(list);
171 return g_slist_prepend(server_list, g_strdup(timeserver));
175 * __connman_timeserver_get_all function creates the timeserver
176 * list which will be used to determine NTP server for time corrections.
177 * The service settings take priority over the global timeservers.
179 GSList *__connman_timeserver_get_all(struct connman_service *service)
182 struct connman_network *network;
185 char **service_ts_config;
186 const char *service_gw;
190 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
193 service_ts_config = connman_service_get_timeservers_config(service);
195 /* First add Service Timeservers.Configuration to the list */
196 for (i = 0; service_ts_config && service_ts_config[i];
198 list = __connman_timeserver_add_list(list,
199 service_ts_config[i]);
201 service_ts = connman_service_get_timeservers(service);
203 /* Then add Service Timeservers via DHCP to the list */
204 for (i = 0; service_ts && service_ts[i]; i++)
205 list = __connman_timeserver_add_list(list, service_ts[i]);
207 network = __connman_service_get_network(service);
209 index = connman_network_get_index(network);
210 service_gw = __connman_ipconfig_get_gateway_from_index(index,
211 CONNMAN_IPCONFIG_TYPE_ALL);
213 /* Then add Service Gateway to the list */
215 list = __connman_timeserver_add_list(list, service_gw);
218 /* Then add Global Timeservers to the list */
219 timeservers = load_timeservers();
221 for (i = 0; timeservers && timeservers[i]; i++)
222 list = __connman_timeserver_add_list(list, timeservers[i]);
224 g_strfreev(timeservers);
226 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
228 /* Lastly add the fallback servers */
229 for (i = 0; fallback_ts && fallback_ts[i]; i++)
230 list = __connman_timeserver_add_list(list, fallback_ts[i]);
232 return g_slist_reverse(list);
235 static gboolean ts_recheck(gpointer user_data)
239 ts = __connman_timeserver_get_all(__connman_service_get_default());
242 DBG("timeservers disabled");
247 if (g_strcmp0(ts_current, ts->data) != 0) {
248 DBG("current %s preferred %s", ts_current, (char *)ts->data);
250 g_slist_free_full(ts, g_free);
252 __connman_timeserver_sync(NULL);
259 g_slist_free_full(ts, g_free);
264 static void ts_recheck_disable(void)
266 if (ts_recheck_id == 0)
269 g_source_remove(ts_recheck_id);
278 static void ts_recheck_enable(void)
280 if (ts_recheck_id > 0)
283 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
288 * This function must be called everytime the default service changes, the
289 * service timeserver(s) or gateway changes or the global timeserver(s) changes.
291 int __connman_timeserver_sync(struct connman_service *default_service)
293 #if defined TIZEN_EXT && !defined TIZEN_CONNMAN_NTP
294 /* Tizen updates time (ntp) by system service */
298 struct connman_service *service;
301 service = default_service;
303 service = __connman_service_get_default();
311 * Before we start creating the new timeserver list we must stop
312 * any ongoing ntp query and server resolution.
315 __connman_ntp_stop();
317 ts_recheck_disable();
320 g_resolv_cancel_lookup(resolv, resolv_id);
322 g_slist_free_full(ts_list, g_free);
324 ts_list = __connman_timeserver_get_all(service);
326 __connman_service_timeserver_changed(service, ts_list);
329 DBG("No timeservers set.");
335 __connman_timeserver_sync_next();
340 static int timeserver_start(struct connman_service *service)
345 DBG("service %p", service);
347 i = __connman_service_get_index(service);
351 nameservers = connman_service_get_nameservers(service);
355 /* Stop an already ongoing resolution, if there is one */
356 if (resolv && resolv_id > 0)
357 g_resolv_cancel_lookup(resolv, resolv_id);
359 /* get rid of the old resolver */
361 g_resolv_unref(resolv);
365 resolv = g_resolv_new(i);
367 g_strfreev(nameservers);
371 if (getenv("CONNMAN_RESOLV_DEBUG"))
372 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
374 for (i = 0; nameservers[i]; i++)
375 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
377 g_strfreev(nameservers);
379 return __connman_timeserver_sync(service);
382 static void timeserver_stop(void)
387 g_resolv_unref(resolv);
391 g_slist_free_full(ts_list, g_free);
395 __connman_ntp_stop();
397 ts_recheck_disable();
400 int __connman_timeserver_system_set(char **servers)
402 save_timeservers(servers);
404 __connman_timeserver_sync(NULL);
409 char **__connman_timeserver_system_get()
413 servers = load_timeservers();
417 static void default_changed(struct connman_service *default_service)
420 timeserver_start(default_service);
425 static struct connman_notifier timeserver_notifier = {
426 .name = "timeserver",
427 .default_changed = default_changed,
430 int __connman_timeserver_init(void)
434 connman_notifier_register(×erver_notifier);
439 void __connman_timeserver_cleanup(void)
443 connman_notifier_unregister(×erver_notifier);