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,
94 server = g_strdup((gchar *)user_data);
95 ts_list = g_slist_append(ts_list, server);
97 DBG("added server %s", server);
100 DBG("status %d", status);
102 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
104 for (i = 0; results[i]; i++) {
105 DBG("result[%d]: %s", i, results[i]);
109 ts_list = __connman_timeserver_add_list(
110 ts_list, results[i]);
113 DBG("Using timeserver %s", results[0]);
115 __connman_ntp_start(results[0]);
121 /* If resolving fails, move to the next server */
122 __connman_timeserver_sync_next();
126 * Once the timeserver list (ts_list) is created, we start querying the
127 * servers one by one. If resolving fails on one of them, we move to the
128 * next one. The user can enter either an IP address or a URL for the
129 * timeserver. We only resolve the URLs. Once we have an IP for the NTP
130 * server, we start querying it for time corrections.
132 void __connman_timeserver_sync_next()
139 __connman_ntp_stop();
141 /* Get the 1st server in the list */
145 ts_current = ts_list->data;
147 ts_list = g_slist_delete_link(ts_list, ts_list);
149 /* if it's an IP, directly query it. */
150 if (connman_inet_check_ipaddress(ts_current) > 0) {
151 DBG("Using timeserver %s", ts_current);
153 __connman_ntp_start(ts_current);
158 DBG("Resolving timeserver %s", ts_current);
160 #if defined TIZEN_EXT
161 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
162 resolv_result, ts_current);
164 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
165 resolv_result, NULL);
171 GSList *__connman_timeserver_add_list(GSList *server_list,
172 const char *timeserver)
174 GSList *list = server_list;
180 char *existing_server = list->data;
181 if (strcmp(timeserver, existing_server) == 0)
183 list = g_slist_next(list);
185 return g_slist_prepend(server_list, g_strdup(timeserver));
189 * __connman_timeserver_get_all function creates the timeserver
190 * list which will be used to determine NTP server for time corrections.
191 * The service settings take priority over the global timeservers.
193 GSList *__connman_timeserver_get_all(struct connman_service *service)
196 struct connman_network *network;
199 char **service_ts_config;
200 const char *service_gw;
204 if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
207 service_ts_config = connman_service_get_timeservers_config(service);
209 /* First add Service Timeservers.Configuration to the list */
210 for (i = 0; service_ts_config && service_ts_config[i];
212 list = __connman_timeserver_add_list(list,
213 service_ts_config[i]);
215 service_ts = connman_service_get_timeservers(service);
217 /* Then add Service Timeservers via DHCP to the list */
218 for (i = 0; service_ts && service_ts[i]; i++)
219 list = __connman_timeserver_add_list(list, service_ts[i]);
221 #if defined TIZEN_EXT
222 if (connman_setting_get_bool("UseGatewayTimeserver")) {
224 network = __connman_service_get_network(service);
226 index = connman_network_get_index(network);
227 service_gw = __connman_ipconfig_get_gateway_from_index(index,
228 CONNMAN_IPCONFIG_TYPE_ALL);
230 /* Then add Service Gateway to the list */
232 list = __connman_timeserver_add_list(list, service_gw);
234 #if defined TIZEN_EXT
237 /* Then add Global Timeservers to the list */
238 timeservers = load_timeservers();
240 for (i = 0; timeservers && timeservers[i]; i++)
241 list = __connman_timeserver_add_list(list, timeservers[i]);
243 g_strfreev(timeservers);
245 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
247 /* Lastly add the fallback servers */
248 for (i = 0; fallback_ts && fallback_ts[i]; i++)
249 list = __connman_timeserver_add_list(list, fallback_ts[i]);
251 return g_slist_reverse(list);
254 static gboolean ts_recheck(gpointer user_data)
258 ts = __connman_timeserver_get_all(__connman_service_get_default());
261 DBG("timeservers disabled");
266 if (g_strcmp0(ts_current, ts->data) != 0) {
267 DBG("current %s preferred %s", ts_current, (char *)ts->data);
269 g_slist_free_full(ts, g_free);
271 __connman_timeserver_sync(NULL);
278 g_slist_free_full(ts, g_free);
283 static void ts_recheck_disable(void)
285 if (ts_recheck_id == 0)
288 g_source_remove(ts_recheck_id);
297 static void ts_recheck_enable(void)
299 if (ts_recheck_id > 0)
302 ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
307 * This function must be called everytime the default service changes, the
308 * service timeserver(s) or gateway changes or the global timeserver(s) changes.
310 int __connman_timeserver_sync(struct connman_service *default_service)
312 struct connman_service *service;
317 service = default_service;
319 service = __connman_service_get_default();
327 * Before we start creating the new timeserver list we must stop
328 * any ongoing ntp query and server resolution.
331 __connman_ntp_stop();
333 ts_recheck_disable();
336 g_resolv_cancel_lookup(resolv, resolv_id);
338 g_resolv_flush_nameservers(resolv);
340 nameservers = connman_service_get_nameservers(service);
344 for (i = 0; nameservers[i]; i++)
345 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
347 g_strfreev(nameservers);
349 g_slist_free_full(ts_list, g_free);
351 ts_list = __connman_timeserver_get_all(service);
353 __connman_service_timeserver_changed(service, ts_list);
356 DBG("No timeservers set.");
362 __connman_timeserver_sync_next();
367 static int timeserver_start(struct connman_service *service)
372 DBG("service %p", service);
374 i = __connman_service_get_index(service);
378 nameservers = connman_service_get_nameservers(service);
382 /* Stop an already ongoing resolution, if there is one */
383 if (resolv && resolv_id > 0)
384 g_resolv_cancel_lookup(resolv, resolv_id);
386 /* get rid of the old resolver */
388 g_resolv_unref(resolv);
392 resolv = g_resolv_new(i);
394 g_strfreev(nameservers);
398 if (getenv("CONNMAN_RESOLV_DEBUG"))
399 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
401 for (i = 0; nameservers[i]; i++)
402 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
404 g_strfreev(nameservers);
406 return __connman_timeserver_sync(service);
409 static void timeserver_stop(void)
414 g_resolv_unref(resolv);
418 g_slist_free_full(ts_list, g_free);
422 __connman_ntp_stop();
424 ts_recheck_disable();
427 int __connman_timeserver_system_set(char **servers)
429 save_timeservers(servers);
431 __connman_timeserver_sync(NULL);
436 char **__connman_timeserver_system_get()
440 servers = load_timeservers();
444 static void default_changed(struct connman_service *default_service)
447 timeserver_start(default_service);
452 static struct connman_notifier timeserver_notifier = {
453 .name = "timeserver",
454 .default_changed = default_changed,
457 int __connman_timeserver_init(void)
461 connman_notifier_register(×erver_notifier);
466 void __connman_timeserver_cleanup(void)
470 connman_notifier_unregister(×erver_notifier);