d41fa40426cdde8133ea42005999f301843c7e6d
[platform/upstream/connman.git] / src / timeserver.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 #define TS_RECHECK_INTERVAL     7200
36
37 static GSList *ts_list = NULL;
38 static char *ts_current = NULL;
39 static int ts_recheck_id = 0;
40
41 static GResolv *resolv = NULL;
42 static int resolv_id = 0;
43
44 static void resolv_debug(const char *str, void *data)
45 {
46         connman_info("%s: %s\n", (const char *) data, str);
47 }
48 static void save_timeservers(char **servers)
49 {
50         GKeyFile *keyfile;
51         int cnt;
52
53         keyfile = __connman_storage_load_global();
54         if (!keyfile)
55                 keyfile = g_key_file_new();
56
57         for (cnt = 0; servers && servers[cnt]; cnt++);
58
59         g_key_file_set_string_list(keyfile, "global", "Timeservers",
60                            (const gchar **)servers, cnt);
61
62         __connman_storage_save_global(keyfile);
63
64         g_key_file_free(keyfile);
65
66         return;
67 }
68
69 static char **load_timeservers(void)
70 {
71         GKeyFile *keyfile;
72         char **servers = NULL;
73
74         keyfile = __connman_storage_load_global();
75         if (!keyfile)
76                 return NULL;
77
78         servers = g_key_file_get_string_list(keyfile, "global",
79                                                 "Timeservers", NULL, NULL);
80
81         g_key_file_free(keyfile);
82
83         return servers;
84 }
85
86 static void resolv_result(GResolvResultStatus status, char **results,
87                                 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) {
95                         for (i = 0; results[i]; i++) {
96                                 DBG("result[%d]: %s", i, results[i]);
97                                 if (i == 0)
98                                         continue;
99
100                                 ts_list = __connman_timeserver_add_list(
101                                                         ts_list, results[i]);
102                         }
103
104                         DBG("Using timeserver %s", results[0]);
105
106                         __connman_ntp_start(results[0]);
107
108                         return;
109                 }
110         }
111
112         /* If resolving fails, move to the next server */
113         __connman_timeserver_sync_next();
114 }
115
116 /*
117  * Once the timeserver list (ts_list) is created, we start querying the
118  * servers one by one. If resolving fails on one of them, we move to the
119  * next one. The user can enter either an IP address or a URL for the
120  * timeserver. We only resolve the urls. Once we have a IP for the NTP
121  * server, we start querying it for time corrections.
122  */
123 void __connman_timeserver_sync_next()
124 {
125         if (ts_current) {
126                 g_free(ts_current);
127                 ts_current = NULL;
128         }
129
130         __connman_ntp_stop();
131
132         /* Get the 1st server in the list */
133         if (!ts_list)
134                 return;
135
136         ts_current = ts_list->data;
137
138         ts_list = g_slist_delete_link(ts_list, ts_list);
139
140         /* if its a IP , directly query it. */
141         if (connman_inet_check_ipaddress(ts_current) > 0) {
142                 DBG("Using timeserver %s", ts_current);
143
144                 __connman_ntp_start(ts_current);
145
146                 return;
147         }
148
149         DBG("Resolving server %s", ts_current);
150
151         resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
152                                                 resolv_result, NULL);
153
154         return;
155 }
156
157 GSList *__connman_timeserver_add_list(GSList *server_list,
158                 const char *timeserver)
159 {
160         GSList *list = server_list;
161
162         if (!timeserver)
163                 return server_list;
164
165         while (list) {
166                 char *existing_server = list->data;
167                 if (strcmp(timeserver, existing_server) == 0)
168                         return server_list;
169                 list = g_slist_next(list);
170         }
171         return g_slist_prepend(server_list, g_strdup(timeserver));
172 }
173
174 /*
175  * __connman_timeserver_get_all function creates the timeserver
176  * list which will be used to determine NTP server for time corrections.
177  * The service settings take priority over the global timeservers.
178  */
179 GSList *__connman_timeserver_get_all(struct connman_service *service)
180 {
181         GSList *list = NULL;
182         struct connman_network *network;
183         char **timeservers;
184         char **service_ts;
185         char **service_ts_config;
186         const char *service_gw;
187         char **fallback_ts;
188         int index, i;
189
190         if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
191                 return NULL;
192
193         service_ts_config = connman_service_get_timeservers_config(service);
194
195         /* First add Service Timeservers.Configuration to the list */
196         for (i = 0; service_ts_config && service_ts_config[i];
197                         i++)
198                 list = __connman_timeserver_add_list(list,
199                                 service_ts_config[i]);
200
201         service_ts = connman_service_get_timeservers(service);
202
203         /* First add Service Timeservers via DHCP to the list */
204         for (i = 0; service_ts && service_ts[i]; i++)
205                 list = __connman_timeserver_add_list(list, service_ts[i]);
206
207         network = __connman_service_get_network(service);
208         if (network) {
209                 index = connman_network_get_index(network);
210                 service_gw = __connman_ipconfig_get_gateway_from_index(index,
211                         CONNMAN_IPCONFIG_TYPE_ALL);
212
213                 /* Then add Service Gateway to the list */
214                 if (service_gw)
215                         list = __connman_timeserver_add_list(list, service_gw);
216         }
217
218         /* Then add Global Timeservers to the list */
219         timeservers = load_timeservers();
220
221         for (i = 0; timeservers && timeservers[i]; i++)
222                 list = __connman_timeserver_add_list(list, timeservers[i]);
223
224         g_strfreev(timeservers);
225
226         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
227
228         /* Lastly add the fallback servers */
229         for (i = 0; fallback_ts && fallback_ts[i]; i++)
230                 list = __connman_timeserver_add_list(list, fallback_ts[i]);
231
232         return g_slist_reverse(list);
233 }
234
235 static gboolean ts_recheck(gpointer user_data)
236 {
237         GSList *ts;
238
239         ts = __connman_timeserver_get_all(__connman_service_get_default());
240
241         if (!ts) {
242                 DBG("timeservers disabled");
243
244                 return TRUE;
245         }
246
247         if (g_strcmp0(ts_current, ts->data) != 0) {
248                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
249
250                 g_slist_free_full(ts, g_free);
251
252                 __connman_timeserver_sync(NULL);
253
254                 return FALSE;
255         }
256
257         DBG("");
258
259         g_slist_free_full(ts, g_free);
260
261         return TRUE;
262 }
263
264 static void ts_recheck_disable(void)
265 {
266         if (ts_recheck_id == 0)
267                 return;
268
269         g_source_remove(ts_recheck_id);
270         ts_recheck_id = 0;
271
272         if (ts_current) {
273                 g_free(ts_current);
274                 ts_current = NULL;
275         }
276 }
277
278 static void ts_recheck_enable(void)
279 {
280         if (ts_recheck_id > 0)
281                 return;
282
283         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
284                         NULL);
285 }
286
287 /*
288  * This function must be called everytime the default service changes, the
289  * service timeserver(s) or gatway changes or the global timeserver(s) changes.
290  */
291 int __connman_timeserver_sync(struct connman_service *default_service)
292 {
293         struct connman_service *service;
294
295         if (default_service)
296                 service = default_service;
297         else
298                 service = __connman_service_get_default();
299
300         if (!service)
301                 return -EINVAL;
302
303         if (!resolv)
304                 return 0;
305         /*
306          * Before we start creating the new timeserver list we must stop
307          * any ongoing ntp query and server resolution.
308          */
309
310         __connman_ntp_stop();
311
312         ts_recheck_disable();
313
314         if (resolv_id > 0)
315                 g_resolv_cancel_lookup(resolv, resolv_id);
316
317         g_slist_free_full(ts_list, g_free);
318
319         ts_list = __connman_timeserver_get_all(service);
320
321         __connman_service_timeserver_changed(service, ts_list);
322
323         if (!ts_list) {
324                 DBG("No timeservers set.");
325                 return 0;
326         }
327
328         ts_recheck_enable();
329
330         __connman_timeserver_sync_next();
331
332         return 0;
333 }
334
335 static int timeserver_start(struct connman_service *service)
336 {
337         char **nameservers;
338         int i;
339
340         DBG("service %p", service);
341
342         i = __connman_service_get_index(service);
343         if (i < 0)
344                 return -EINVAL;
345
346         nameservers = connman_service_get_nameservers(service);
347         if (!nameservers)
348                 return -EINVAL;
349
350         /* Stop an already ongoing resolution, if there is one */
351         if (resolv && resolv_id > 0)
352                 g_resolv_cancel_lookup(resolv, resolv_id);
353
354         /* get rid of the old resolver */
355         if (resolv) {
356                 g_resolv_unref(resolv);
357                 resolv = NULL;
358         }
359
360         resolv = g_resolv_new(i);
361         if (!resolv) {
362                 g_strfreev(nameservers);
363                 return -ENOMEM;
364         }
365
366         if (getenv("CONNMAN_RESOLV_DEBUG"))
367                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
368
369         for (i = 0; nameservers[i]; i++)
370                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
371
372         g_strfreev(nameservers);
373
374         return __connman_timeserver_sync(service);
375 }
376
377 static void timeserver_stop(void)
378 {
379         DBG(" ");
380
381         if (resolv) {
382                 g_resolv_unref(resolv);
383                 resolv = NULL;
384         }
385
386         g_slist_free_full(ts_list, g_free);
387
388         ts_list = NULL;
389
390         __connman_ntp_stop();
391
392         ts_recheck_disable();
393 }
394
395 int __connman_timeserver_system_set(char **servers)
396 {
397         save_timeservers(servers);
398
399         __connman_timeserver_sync(NULL);
400
401         return 0;
402 }
403
404 char **__connman_timeserver_system_get()
405 {
406         char **servers;
407
408         servers = load_timeservers();
409         return servers;
410 }
411
412 static void default_changed(struct connman_service *default_service)
413 {
414         if (default_service)
415                 timeserver_start(default_service);
416         else
417                 timeserver_stop();
418 }
419
420 static struct connman_notifier timeserver_notifier = {
421         .name                   = "timeserver",
422         .default_changed        = default_changed,
423 };
424
425 int __connman_timeserver_init(void)
426 {
427         DBG("");
428
429         connman_notifier_register(&timeserver_notifier);
430
431         return 0;
432 }
433
434 void __connman_timeserver_cleanup(void)
435 {
436         DBG("");
437
438         connman_notifier_unregister(&timeserver_notifier);
439 }