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 static GSList *ts_list = NULL;
37 static GResolv *resolv = NULL;
38 static int resolv_id = 0;
40 static void resolv_debug(const char *str, void *data)
42 connman_info("%s: %s\n", (const char *) data, str);
44 static void save_timeservers(char **servers)
49 keyfile = __connman_storage_load_global();
51 keyfile = g_key_file_new();
53 for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++);
55 g_key_file_set_string_list(keyfile, "global", "Timeservers",
56 (const gchar **)servers, cnt);
58 __connman_storage_save_global(keyfile);
60 g_key_file_free(keyfile);
65 static char **load_timeservers()
68 char **servers = NULL;
70 keyfile = __connman_storage_load_global();
74 servers = g_key_file_get_string_list(keyfile, "global",
75 "Timeservers", NULL, NULL);
77 g_key_file_free(keyfile);
82 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
86 DBG("status %d", status);
88 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
89 if (results != NULL) {
90 for (i = 0; results[i]; i++) {
91 DBG("result[%d]: %s", i, results[i]);
95 ts_list = __connman_timeserver_add_list(
99 DBG("Using timeserver %s", results[0]);
101 __connman_ntp_start(results[0]);
107 /* If resolving fails, move to the next server */
108 __connman_timeserver_sync_next();
112 * Once the timeserver list (ts_list) is created, we start querying the
113 * servers one by one. If resolving fails on one of them, we move to the
114 * next one. The user can enter either an IP address or a URL for the
115 * timeserver. We only resolve the urls. Once we have a IP for the NTP
116 * server, we start querying it for time corrections.
118 void __connman_timeserver_sync_next()
122 __connman_ntp_stop();
124 /* Get the 1st server in the list */
128 server = ts_list->data;
130 ts_list = g_slist_delete_link(ts_list, ts_list);
132 /* if its a IP , directly query it. */
133 if (connman_inet_check_ipaddress(server) > 0) {
134 DBG("Using timeserver %s", server);
136 __connman_ntp_start(server);
142 DBG("Resolving server %s", server);
144 resolv_id = g_resolv_lookup_hostname(resolv, server,
145 resolv_result, NULL);
152 GSList *__connman_timeserver_add_list(GSList *server_list,
153 const char *timeserver)
155 GSList *list = server_list;
157 if (timeserver == NULL)
160 while (list != NULL) {
161 char *existing_server = list->data;
162 if (strcmp(timeserver, existing_server) == 0)
164 list = g_slist_next(list);
166 return g_slist_prepend(server_list, g_strdup(timeserver));
170 * __connman_timeserver_get_all function creates the timeserver
171 * list which will be used to determine NTP server for time corrections.
172 * The service settings take priority over the global timeservers.
174 GSList *__connman_timeserver_get_all(struct connman_service *service)
177 struct connman_network *network;
180 char **service_ts_config;
181 const char *service_gw;
185 service_ts_config = connman_service_get_timeservers_config(service);
187 /* First add Service Timeservers.Configuration to the list */
188 for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
190 list = __connman_timeserver_add_list(list,
191 service_ts_config[i]);
193 service_ts = connman_service_get_timeservers(service);
195 /* First add Service Timeservers via DHCP to the list */
196 for (i = 0; service_ts != NULL && service_ts[i] != NULL; i++)
197 list = __connman_timeserver_add_list(list, service_ts[i]);
199 network = __connman_service_get_network(service);
200 if (network != NULL) {
201 index = connman_network_get_index(network);
202 service_gw = __connman_ipconfig_get_gateway_from_index(index,
203 CONNMAN_IPCONFIG_TYPE_ALL);
205 /* Then add Service Gateway to the list */
206 if (service_gw != NULL)
207 list = __connman_timeserver_add_list(list, service_gw);
210 /* Then add Global Timeservers to the list */
211 timeservers = load_timeservers();
213 for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
214 list = __connman_timeserver_add_list(list, timeservers[i]);
216 g_strfreev(timeservers);
218 fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
220 /* Lastly add the fallback servers */
221 for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
222 list = __connman_timeserver_add_list(list, fallback_ts[i]);
224 return g_slist_reverse(list);
228 * This function must be called everytime the default service changes, the
229 * service timeserver(s) or gatway changes or the global timeserver(s) changes.
231 int __connman_timeserver_sync(struct connman_service *default_service)
233 struct connman_service *service;
235 if (default_service != NULL)
236 service = default_service;
238 service = __connman_service_get_default();
246 * Before we start creating the new timeserver list we must stop
247 * any ongoing ntp query and server resolution.
250 __connman_ntp_stop();
253 g_resolv_cancel_lookup(resolv, resolv_id);
255 g_slist_free_full(ts_list, g_free);
257 ts_list = __connman_timeserver_get_all(service);
259 __connman_service_timeserver_changed(service, ts_list);
261 if (ts_list == NULL) {
262 DBG("No timeservers set.");
266 __connman_timeserver_sync_next();
271 static int timeserver_start(struct connman_service *service)
276 DBG("service %p", service);
278 i = __connman_service_get_index(service);
282 nameservers = connman_service_get_nameservers(service);
283 if (nameservers == NULL)
286 /* Stop an already ongoing resolution, if there is one */
287 if (resolv != NULL && resolv_id > 0)
288 g_resolv_cancel_lookup(resolv, resolv_id);
290 /* get rid of the old resolver */
291 if (resolv != NULL) {
292 g_resolv_unref(resolv);
296 resolv = g_resolv_new(i);
297 if (resolv == NULL) {
298 g_strfreev(nameservers);
302 if (getenv("CONNMAN_RESOLV_DEBUG"))
303 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
305 for (i = 0; nameservers[i] != NULL; i++)
306 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
308 g_strfreev(nameservers);
310 return __connman_timeserver_sync(service);
313 static void timeserver_stop()
317 if (resolv != NULL) {
318 g_resolv_unref(resolv);
322 g_slist_free_full(ts_list, g_free);
326 __connman_ntp_stop();
329 int __connman_timeserver_system_set(char **servers)
331 save_timeservers(servers);
333 __connman_timeserver_sync(NULL);
338 char **__connman_timeserver_system_get()
342 servers = load_timeservers();
346 static void default_changed(struct connman_service *default_service)
348 if (default_service != NULL)
349 timeserver_start(default_service);
354 static struct connman_notifier timeserver_notifier = {
355 .name = "timeserver",
356 .default_changed = default_changed,
359 int __connman_timeserver_init(void)
363 connman_notifier_register(×erver_notifier);
368 void __connman_timeserver_cleanup(void)
372 connman_notifier_unregister(×erver_notifier);