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()
69 char **servers = NULL;
71 keyfile = __connman_storage_load_global();
75 servers = g_key_file_get_string_list(keyfile, "global",
76 "Timeservers", NULL, &error);
78 DBG("Error loading timeservers: %s", error->message);
82 g_key_file_free(keyfile);
87 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
91 DBG("status %d", status);
93 if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
94 if (results != NULL) {
95 for (i = 0; results[i]; i++)
96 DBG("result: %s", results[i]);
98 __connman_ntp_start(results[0]);
104 /* If resolving fails, move to the next server */
105 __connman_timeserver_sync_next();
109 * Once the timeserver list (ts_list) is created, we start querying the
110 * servers one by one. If resolving fails on one of them, we move to the
111 * next one. The user can enter either an IP address or a URL for the
112 * timeserver. We only resolve the urls. Once we have a IP for the NTP
113 * server, we start querying it for time corrections.
115 void __connman_timeserver_sync_next()
119 struct addrinfo hints;
120 struct addrinfo *addr;
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 memset(&hints, 0, sizeof(struct addrinfo));
133 hints.ai_flags = AI_NUMERICHOST;
136 ret = getaddrinfo(server, NULL, &hints, &addr);
139 /* if its a IP , directly query it. */
141 DBG("Using timeservers %s", server);
143 __connman_ntp_start(server);
149 DBG("Resolving server %s", server);
151 resolv_id = g_resolv_lookup_hostname(resolv, server,
152 resolv_result, NULL);
159 GSList *__connman_timeserver_add_list(GSList *server_list,
160 const char *timeserver)
162 GSList *list = server_list;
164 if (timeserver == NULL)
167 while (list != NULL) {
168 char *existing_server = list->data;
169 if (strcmp(timeserver, existing_server) == 0)
171 list = g_slist_next(list);
173 return g_slist_prepend(server_list, g_strdup(timeserver));
177 * __connman_timeserver_get_all function creates the timeserver
178 * list which will be used to determine NTP server for time corrections.
179 * The service settings take priority over the global timeservers.
181 GSList *__connman_timeserver_get_all(struct connman_service *service)
184 struct connman_network *network;
187 char **service_ts_config;
188 const char *service_gw;
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);
235 * This function must be called everytime the default service changes, the
236 * service timeserver(s) or gatway changes or the global timeserver(s) changes.
238 int __connman_timeserver_sync(struct connman_service *default_service)
240 struct connman_service *service;
242 if (default_service != NULL)
243 service = default_service;
245 service = __connman_service_get_default();
253 * Before we start creating the new timeserver list we must stop
254 * any ongoing ntp query and server resolution.
257 __connman_ntp_stop();
260 g_resolv_cancel_lookup(resolv, resolv_id);
262 g_slist_free_full(ts_list, g_free);
264 ts_list = __connman_timeserver_get_all(service);
266 __connman_service_timeserver_changed(service, ts_list);
268 if (ts_list == NULL) {
269 DBG("No timeservers set.");
273 __connman_timeserver_sync_next();
278 static int timeserver_start(struct connman_service *service)
283 DBG("service %p", service);
285 i = __connman_service_get_index(service);
289 nameservers = connman_service_get_nameservers(service);
290 if (nameservers == NULL)
293 /* Stop an already ongoing resolution, if there is one */
294 if (resolv != NULL && resolv_id > 0)
295 g_resolv_cancel_lookup(resolv, resolv_id);
297 /* get rid of the old resolver */
298 if (resolv != NULL) {
299 g_resolv_unref(resolv);
303 resolv = g_resolv_new(i);
304 if (resolv == NULL) {
305 g_strfreev(nameservers);
309 if (getenv("CONNMAN_RESOLV_DEBUG"))
310 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
312 for (i = 0; nameservers[i] != NULL; i++)
313 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
315 g_strfreev(nameservers);
317 return __connman_timeserver_sync(service);
320 static void timeserver_stop()
324 if (resolv != NULL) {
325 g_resolv_unref(resolv);
329 g_slist_free_full(ts_list, g_free);
333 __connman_ntp_stop();
336 int __connman_timeserver_system_set(char **servers)
338 save_timeservers(servers);
340 __connman_timeserver_sync(NULL);
345 char **__connman_timeserver_system_get()
349 servers = load_timeservers();
353 static void default_changed(struct connman_service *default_service)
355 if (default_service != NULL)
356 timeserver_start(default_service);
361 static struct connman_notifier timeserver_notifier = {
362 .name = "timeserver",
363 .default_changed = default_changed,
366 int __connman_timeserver_init(void)
370 connman_notifier_register(×erver_notifier);
375 void __connman_timeserver_cleanup(void)
379 connman_notifier_unregister(×erver_notifier);