timeserver: Helper function for adding timeservers once to a list
[framework/connectivity/connman.git] / src / timeserver.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27
28 #include <glib.h>
29 #include <stdlib.h>
30 #include <gweb/gresolv.h>
31 #include <netdb.h>
32
33 #include "connman.h"
34
35 static GSList *ts_list = NULL;
36
37 static GResolv *resolv = NULL;
38 static int resolv_id = 0;
39
40 static void resolv_debug(const char *str, void *data)
41 {
42         connman_info("%s: %s\n", (const char *) data, str);
43 }
44 static void save_timeservers(char **servers)
45 {
46         GKeyFile *keyfile;
47         int cnt;
48
49         keyfile = __connman_storage_load_global();
50         if (keyfile == NULL)
51                 keyfile = g_key_file_new();
52
53         for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++);
54
55         g_key_file_set_string_list(keyfile, "global", "Timeservers",
56                            (const gchar **)servers, cnt);
57
58         __connman_storage_save_global(keyfile);
59
60         g_key_file_free(keyfile);
61
62         return;
63 }
64
65 static char **load_timeservers()
66 {
67         GKeyFile *keyfile;
68         GError *error = NULL;
69         char **servers = NULL;
70
71         keyfile = __connman_storage_load_global();
72         if (keyfile == NULL)
73                 return NULL;
74
75         servers = g_key_file_get_string_list(keyfile, "global",
76                                                 "Timeservers", NULL, &error);
77         if (error) {
78                 DBG("Error loading timeservers: %s", error->message);
79                 g_error_free(error);
80         }
81
82         g_key_file_free(keyfile);
83
84         return servers;
85 }
86
87 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
88 {
89         int i;
90
91         DBG("status %d", status);
92
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]);
97
98                         __connman_ntp_start(results[0]);
99
100                         return;
101                 }
102         }
103
104         /* If resolving fails, move to the next server */
105         __connman_timeserver_sync_next();
106 }
107
108 /*
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.
114  */
115 void __connman_timeserver_sync_next()
116 {
117         char *server;
118         int ret;
119         struct addrinfo hints;
120         struct addrinfo *addr;
121
122         __connman_ntp_stop();
123
124         /* Get the 1st server in the list */
125         if (ts_list == NULL)
126                 return;
127
128         server = ts_list->data;
129
130         ts_list = g_slist_delete_link(ts_list, ts_list);
131
132         memset(&hints, 0, sizeof(struct addrinfo));
133         hints.ai_flags = AI_NUMERICHOST;
134         addr = NULL;
135
136         ret = getaddrinfo(server, NULL, &hints, &addr);
137         freeaddrinfo(addr);
138
139         /* if its a IP , directly query it. */
140         if (ret == 0) {
141                 DBG("Using timeservers %s", server);
142
143                 __connman_ntp_start(server);
144
145                 g_free(server);
146                 return;
147         }
148
149         DBG("Resolving server %s", server);
150
151         resolv_id = g_resolv_lookup_hostname(resolv, server,
152                                                 resolv_result, NULL);
153
154         g_free(server);
155
156         return;
157 }
158
159 GSList *__connman_timeserver_add_list(GSList *server_list,
160                 const char *timeserver)
161 {
162         GSList *list = server_list;
163
164         if (timeserver == NULL)
165                 return server_list;
166
167         while (list != NULL) {
168                 char *existing_server = list->data;
169                 if (strcmp(timeserver, existing_server) == 0)
170                         return server_list;
171                 list = g_slist_next(list);
172         }
173         return g_slist_prepend(server_list, g_strdup(timeserver));
174 }
175
176 /*
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.
180  */
181 GSList *__connman_timeserver_get_all(struct connman_service *service)
182 {
183         GSList *list = NULL;
184         struct connman_network *network;
185         char **timeservers;
186         char **service_ts;
187         char **service_ts_config;
188         const char *service_gw;
189         char **fallback_ts;
190         int index, i;
191
192         service_ts_config = connman_service_get_timeservers_config(service);
193
194         /* First add Service Timeservers.Configuration to the list */
195         for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
196                         i++)
197                 list = g_slist_prepend(list, g_strdup(service_ts_config[i]));
198
199         service_ts = connman_service_get_timeservers(service);
200
201         /* First add Service Timeservers via DHCP to the list */
202         for (i = 0; service_ts != NULL && service_ts[i] != NULL; i++)
203                 list = g_slist_prepend(list, g_strdup(service_ts[i]));
204
205         network = __connman_service_get_network(service);
206         if (network != NULL) {
207                 index = connman_network_get_index(network);
208                 service_gw = __connman_ipconfig_get_gateway_from_index(index,
209                         CONNMAN_IPCONFIG_TYPE_ALL);
210
211                 /* Then add Service Gateway to the list */
212                 if (service_gw != NULL)
213                         list = g_slist_prepend(list, g_strdup(service_gw));
214         }
215
216         /* Then add Global Timeservers to the list */
217         timeservers = load_timeservers();
218
219         for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
220                 list = g_slist_prepend(list, g_strdup(timeservers[i]));
221
222         g_strfreev(timeservers);
223
224         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
225
226         /* Lastly add the fallback servers */
227         for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
228                 list = g_slist_prepend(list, g_strdup(fallback_ts[i]));
229
230         return g_slist_reverse(list);
231 }
232
233 /*
234  * This function must be called everytime the default service changes, the
235  * service timeserver(s) or gatway changes or the global timeserver(s) changes.
236  */
237 int __connman_timeserver_sync(struct connman_service *default_service)
238 {
239         struct connman_service *service;
240
241         if (default_service != NULL)
242                 service = default_service;
243         else
244                 service = __connman_service_get_default();
245
246         if (service == NULL)
247                 return -EINVAL;
248
249         if (resolv == NULL)
250                 return 0;
251         /*
252          * Before we start creating the new timeserver list we must stop
253          * any ongoing ntp query and server resolution.
254          */
255
256         __connman_ntp_stop();
257
258         if (resolv_id > 0)
259                 g_resolv_cancel_lookup(resolv, resolv_id);
260
261         g_slist_free_full(ts_list, g_free);
262
263         ts_list = __connman_timeserver_get_all(service);
264
265         __connman_service_timeserver_changed(service, ts_list);
266
267         if (ts_list == NULL) {
268                 DBG("No timeservers set.");
269                 return 0;
270         }
271
272         __connman_timeserver_sync_next();
273
274         return 0;
275 }
276
277 static int timeserver_start(struct connman_service *service)
278 {
279         char **nameservers;
280         int i;
281
282         DBG("service %p", service);
283
284         i = __connman_service_get_index(service);
285         if (i < 0)
286                 return -EINVAL;
287
288         nameservers = connman_service_get_nameservers(service);
289         if (nameservers == NULL)
290                 return -EINVAL;
291
292         /* Stop an already ongoing resolution, if there is one */
293         if (resolv != NULL && resolv_id > 0)
294                 g_resolv_cancel_lookup(resolv, resolv_id);
295
296         /* get rid of the old resolver */
297         if (resolv != NULL) {
298                 g_resolv_unref(resolv);
299                 resolv = NULL;
300         }
301
302         resolv = g_resolv_new(i);
303         if (resolv == NULL) {
304                 g_strfreev(nameservers);
305                 return -ENOMEM;
306         }
307
308         if (getenv("CONNMAN_RESOLV_DEBUG"))
309                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
310
311         for (i = 0; nameservers[i] != NULL; i++)
312                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
313
314         g_strfreev(nameservers);
315
316         return __connman_timeserver_sync(service);
317 }
318
319 static void timeserver_stop()
320 {
321         DBG(" ");
322
323         if (resolv != NULL) {
324                 g_resolv_unref(resolv);
325                 resolv = NULL;
326         }
327
328         g_slist_free_full(ts_list, g_free);
329
330         ts_list = NULL;
331
332         __connman_ntp_stop();
333 }
334
335 int __connman_timeserver_system_set(char **servers)
336 {
337         save_timeservers(servers);
338
339         __connman_timeserver_sync(NULL);
340
341         return 0;
342 }
343
344 char **__connman_timeserver_system_get()
345 {
346         char **servers;
347
348         servers = load_timeservers();
349         return servers;
350 }
351
352 static void default_changed(struct connman_service *default_service)
353 {
354         if (default_service != NULL)
355                 timeserver_start(default_service);
356         else
357                 timeserver_stop();
358 }
359
360 static struct connman_notifier timeserver_notifier = {
361         .name                   = "timeserver",
362         .default_changed        = default_changed,
363 };
364
365 int __connman_timeserver_init(void)
366 {
367         DBG("");
368
369         connman_notifier_register(&timeserver_notifier);
370
371         return 0;
372 }
373
374 void __connman_timeserver_cleanup(void)
375 {
376         DBG("");
377
378         connman_notifier_unregister(&timeserver_notifier);
379 }