5 * Copyright (C) 2007-2012 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 != NULL && servers[cnt] != NULL; 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()
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, gpointer user_data)
90 DBG("status %d", status);
92 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
93 if (results != NULL) {
94 for (i = 0; results[i]; i++) {
95 DBG("result[%d]: %s", i, results[i]);
99 ts_list = __connman_timeserver_add_list(
100 ts_list, results[i]);
103 DBG("Using timeserver %s", results[0]);
105 __connman_ntp_start(results[0]);
111 /* If resolving fails, move to the next server */
112 __connman_timeserver_sync_next();
116 * Once the timeserver list (ts_list) is created, we start querying the
117 * servers one by one. If resolving fails on one of them, we move to the
118 * next one. The user can enter either an IP address or a URL for the
119 * timeserver. We only resolve the urls. Once we have a IP for the NTP
120 * server, we start querying it for time corrections.
122 void __connman_timeserver_sync_next()
124 if (ts_current != NULL) {
129 __connman_ntp_stop();
131 /* Get the 1st server in the list */
135 ts_current = ts_list->data;
137 ts_list = g_slist_delete_link(ts_list, ts_list);
139 /* if its a IP , directly query it. */
140 if (connman_inet_check_ipaddress(ts_current) > 0) {
141 DBG("Using timeserver %s", ts_current);
143 __connman_ntp_start(ts_current);
148 DBG("Resolving server %s", ts_current);
150 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
151 resolv_result, NULL);
156 GSList *__connman_timeserver_add_list(GSList *server_list,
157 const char *timeserver)
159 GSList *list = server_list;
161 if (timeserver == NULL)
164 while (list != NULL) {
165 char *existing_server = list->data;
166 if (strcmp(timeserver, existing_server) == 0)
168 list = g_slist_next(list);
170 return g_slist_prepend(server_list, g_strdup(timeserver));
174 * __connman_timeserver_get_all function creates the timeserver
175 * list which will be used to determine NTP server for time corrections.
176 * The service settings take priority over the global timeservers.
178 GSList *__connman_timeserver_get_all(struct connman_service *service)
181 struct connman_network *network;
184 char **service_ts_config;
185 const char *service_gw;
189 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
192 service_ts_config = connman_service_get_timeservers_config(service);
194 /* First add Service Timeservers.Configuration to the list */
195 for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
197 list = __connman_timeserver_add_list(list,
198 service_ts_config[i]);
200 service_ts = connman_service_get_timeservers(service);
202 /* First add Service Timeservers via DHCP to the list */
203 for (i = 0; service_ts != NULL && service_ts[i] != NULL; i++)
204 list = __connman_timeserver_add_list(list, service_ts[i]);
206 network = __connman_service_get_network(service);
207 if (network != NULL) {
208 index = connman_network_get_index(network);
209 service_gw = __connman_ipconfig_get_gateway_from_index(index,
210 CONNMAN_IPCONFIG_TYPE_ALL);
212 /* Then add Service Gateway to the list */
213 if (service_gw != NULL)
214 list = __connman_timeserver_add_list(list, service_gw);
217 /* Then add Global Timeservers to the list */
218 timeservers = load_timeservers();
220 for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
221 list = __connman_timeserver_add_list(list, timeservers[i]);
223 g_strfreev(timeservers);
225 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
227 /* Lastly add the fallback servers */
228 for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
229 list = __connman_timeserver_add_list(list, fallback_ts[i]);
231 return g_slist_reverse(list);
234 static gboolean ts_recheck(void *user_data)
238 ts = __connman_timeserver_get_all(__connman_service_get_default());
241 DBG("timeservers disabled");
246 if (g_strcmp0(ts_current, ts->data) != 0) {
247 DBG("current %s preferred %s", ts_current, (char *)ts->data);
249 g_slist_free_full(ts, g_free);
251 __connman_timeserver_sync(NULL);
258 g_slist_free_full(ts, g_free);
263 static void ts_recheck_disable(void)
265 if (ts_recheck_id == 0)
268 g_source_remove(ts_recheck_id);
271 if (ts_current != NULL) {
277 static void ts_recheck_enable(void)
279 if (ts_recheck_id > 0)
282 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
287 * This function must be called everytime the default service changes, the
288 * service timeserver(s) or gatway changes or the global timeserver(s) changes.
290 int __connman_timeserver_sync(struct connman_service *default_service)
292 struct connman_service *service;
294 if (default_service != NULL)
295 service = default_service;
297 service = __connman_service_get_default();
305 * Before we start creating the new timeserver list we must stop
306 * any ongoing ntp query and server resolution.
309 __connman_ntp_stop();
311 ts_recheck_disable();
314 g_resolv_cancel_lookup(resolv, resolv_id);
316 g_slist_free_full(ts_list, g_free);
318 ts_list = __connman_timeserver_get_all(service);
320 __connman_service_timeserver_changed(service, ts_list);
322 if (ts_list == NULL) {
323 DBG("No timeservers set.");
329 __connman_timeserver_sync_next();
334 static int timeserver_start(struct connman_service *service)
339 DBG("service %p", service);
341 i = __connman_service_get_index(service);
345 nameservers = connman_service_get_nameservers(service);
346 if (nameservers == NULL)
349 /* Stop an already ongoing resolution, if there is one */
350 if (resolv != NULL && resolv_id > 0)
351 g_resolv_cancel_lookup(resolv, resolv_id);
353 /* get rid of the old resolver */
354 if (resolv != NULL) {
355 g_resolv_unref(resolv);
359 resolv = g_resolv_new(i);
360 if (resolv == NULL) {
361 g_strfreev(nameservers);
365 if (getenv("CONNMAN_RESOLV_DEBUG"))
366 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
368 for (i = 0; nameservers[i] != NULL; i++)
369 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
371 g_strfreev(nameservers);
373 return __connman_timeserver_sync(service);
376 static void timeserver_stop()
380 if (resolv != NULL) {
381 g_resolv_unref(resolv);
385 g_slist_free_full(ts_list, g_free);
389 __connman_ntp_stop();
391 ts_recheck_disable();
394 int __connman_timeserver_system_set(char **servers)
396 save_timeservers(servers);
398 __connman_timeserver_sync(NULL);
403 char **__connman_timeserver_system_get()
407 servers = load_timeservers();
411 static void default_changed(struct connman_service *default_service)
413 if (default_service != NULL)
414 timeserver_start(default_service);
419 static struct connman_notifier timeserver_notifier = {
420 .name = "timeserver",
421 .default_changed = default_changed,
424 int __connman_timeserver_init(void)
428 connman_notifier_register(×erver_notifier);
433 void __connman_timeserver_cleanup(void)
437 connman_notifier_unregister(×erver_notifier);