32f1b01c9f9a397ecedc5cc22d99541814af1c1e
[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 struct connman_service *ts_service;
38 static GSList *timeservers_list = NULL;
39 static GSList *ts_list = NULL;
40 static char *ts_current = NULL;
41 static int ts_recheck_id = 0;
42 static int ts_backoff_id = 0;
43
44 static GResolv *resolv = NULL;
45 static int resolv_id = 0;
46
47 static void sync_next(void);
48
49 static void resolv_debug(const char *str, void *data)
50 {
51         connman_info("%s: %s\n", (const char *) data, str);
52 }
53
54 static void ntp_callback(bool success, void *user_data)
55 {
56         DBG("success %d", success);
57
58         if (!success)
59                 sync_next();
60 }
61
62 static void save_timeservers(char **servers)
63 {
64         GKeyFile *keyfile;
65         int cnt;
66
67         keyfile = __connman_storage_load_global();
68         if (!keyfile)
69                 keyfile = g_key_file_new();
70
71         for (cnt = 0; servers && servers[cnt]; cnt++);
72
73         g_key_file_set_string_list(keyfile, "global", "Timeservers",
74                            (const gchar **)servers, cnt);
75
76         __connman_storage_save_global(keyfile);
77
78         g_key_file_free(keyfile);
79 }
80
81 static char **load_timeservers(void)
82 {
83         GKeyFile *keyfile;
84         char **servers = NULL;
85
86         keyfile = __connman_storage_load_global();
87         if (!keyfile)
88                 return NULL;
89
90         servers = g_key_file_get_string_list(keyfile, "global",
91                                                 "Timeservers", NULL, NULL);
92
93         g_key_file_free(keyfile);
94
95         return servers;
96 }
97
98 static void resolv_result(GResolvResultStatus status, char **results,
99                                 gpointer user_data)
100 {
101         int i;
102
103 #if defined TIZEN_EXT
104         gchar *server = NULL;
105
106         server = g_strdup((gchar *)user_data);
107         ts_list = g_slist_append(ts_list, server);
108
109         DBG("added server %s", server);
110 #endif
111
112         DBG("status %d", status);
113
114         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
115                 if (results) {
116                         /* prepend the results in reverse order */
117
118                         for (i = 0; results[i]; i++)
119                                 /* count */;
120                         i--;
121
122                         for (; i >= 0; i--) {
123                                 DBG("result[%d]: %s", i, results[i]);
124
125                                 ts_list = __connman_timeserver_add_list(
126                                         ts_list, results[i]);
127                         }
128                 }
129         }
130
131         sync_next();
132 }
133
134 /*
135  * Once the timeserver list (timeserver_list) is created, we start
136  * querying the servers one by one. If resolving fails on one of them,
137  * we move to the next one. The user can enter either an IP address or
138  * a URL for the timeserver. We only resolve the URLs. Once we have an
139  * IP for the NTP server, we start querying it for time corrections.
140  */
141 static void timeserver_sync_start(void)
142 {
143         GSList *list;
144
145         for (list = timeservers_list; list; list = list->next) {
146                 char *timeserver = list->data;
147
148                 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
149         }
150         ts_list = g_slist_reverse(ts_list);
151
152         sync_next();
153 }
154
155 static gboolean timeserver_sync_restart(gpointer user_data)
156 {
157         timeserver_sync_start();
158         ts_backoff_id = 0;
159
160         return FALSE;
161 }
162
163 /*
164  * Select the next time server from the working list (ts_list) because
165  * for some reason the first time server in the list didn't work. If
166  * none of the server did work we start over with the first server
167  * with a backoff.
168  */
169 static void sync_next(void)
170 {
171         if (ts_current) {
172                 g_free(ts_current);
173                 ts_current = NULL;
174         }
175
176         __connman_ntp_stop();
177
178         while (ts_list) {
179                 ts_current = ts_list->data;
180                 ts_list = g_slist_delete_link(ts_list, ts_list);
181
182                 /* if it's an IP, directly query it. */
183                 if (connman_inet_check_ipaddress(ts_current) > 0) {
184                         DBG("Using timeserver %s", ts_current);
185                         __connman_ntp_start(ts_current, ntp_callback, NULL);
186                         return;
187                 }
188
189                 DBG("Resolving timeserver %s", ts_current);
190 #if defined TIZEN_EXT
191                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
192                                                 resolv_result, ts_current);
193 #else
194                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
195                                                 resolv_result, NULL);
196 #endif
197                 return;
198         }
199
200         DBG("No timeserver could be used, restart probing in 5 seconds");
201         ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
202 }
203
204 GSList *__connman_timeserver_add_list(GSList *server_list,
205                 const char *timeserver)
206 {
207         GSList *list = server_list;
208
209         if (!timeserver)
210                 return server_list;
211
212         while (list) {
213                 char *existing_server = list->data;
214                 if (strcmp(timeserver, existing_server) == 0)
215                         return server_list;
216                 list = g_slist_next(list);
217         }
218         return g_slist_prepend(server_list, g_strdup(timeserver));
219 }
220
221 /*
222  * __connman_timeserver_get_all function creates the timeserver
223  * list which will be used to determine NTP server for time corrections.
224  * The service settings take priority over the global timeservers.
225  */
226 GSList *__connman_timeserver_get_all(struct connman_service *service)
227 {
228         GSList *list = NULL;
229         struct connman_network *network;
230         char **timeservers;
231         char **service_ts;
232         char **service_ts_config;
233         const char *service_gw;
234         char **fallback_ts;
235         int index, i;
236
237         if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
238                 return NULL;
239
240         service_ts_config = connman_service_get_timeservers_config(service);
241
242         /* First add Service Timeservers.Configuration to the list */
243         for (i = 0; service_ts_config && service_ts_config[i];
244                         i++)
245                 list = __connman_timeserver_add_list(list,
246                                 service_ts_config[i]);
247
248         service_ts = connman_service_get_timeservers(service);
249
250         /* Then add Service Timeservers via DHCP to the list */
251         for (i = 0; service_ts && service_ts[i]; i++)
252                 list = __connman_timeserver_add_list(list, service_ts[i]);
253
254         /*
255          * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
256          * configuration option is set to true.
257          */
258         if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
259                 network = __connman_service_get_network(service);
260                 if (network) {
261                         index = connman_network_get_index(network);
262                         service_gw = __connman_ipconfig_get_gateway_from_index(index,
263                                 CONNMAN_IPCONFIG_TYPE_ALL);
264
265                         if (service_gw)
266                                 list = __connman_timeserver_add_list(list, service_gw);
267                 }
268         }
269
270         /* Then add Global Timeservers to the list */
271         timeservers = load_timeservers();
272
273         for (i = 0; timeservers && timeservers[i]; i++)
274                 list = __connman_timeserver_add_list(list, timeservers[i]);
275
276         g_strfreev(timeservers);
277
278         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
279
280         /* Lastly add the fallback servers */
281         for (i = 0; fallback_ts && fallback_ts[i]; i++)
282                 list = __connman_timeserver_add_list(list, fallback_ts[i]);
283
284         return g_slist_reverse(list);
285 }
286
287 static gboolean ts_recheck(gpointer user_data)
288 {
289         GSList *ts;
290
291         ts = __connman_timeserver_get_all(connman_service_get_default());
292
293         if (!ts) {
294                 DBG("timeservers disabled");
295
296                 return TRUE;
297         }
298
299         if (g_strcmp0(ts_current, ts->data) != 0) {
300                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
301
302                 g_slist_free_full(ts, g_free);
303
304                 __connman_timeserver_sync(NULL);
305
306                 return FALSE;
307         }
308
309         DBG("");
310
311         g_slist_free_full(ts, g_free);
312
313         return TRUE;
314 }
315
316 static void ts_recheck_disable(void)
317 {
318         if (ts_recheck_id == 0)
319                 return;
320
321         g_source_remove(ts_recheck_id);
322         ts_recheck_id = 0;
323
324         if (ts_backoff_id) {
325                 g_source_remove(ts_backoff_id);
326                 ts_backoff_id = 0;
327         }
328
329         if (ts_current) {
330                 g_free(ts_current);
331                 ts_current = NULL;
332         }
333 }
334
335 static void ts_recheck_enable(void)
336 {
337         if (ts_recheck_id > 0)
338                 return;
339
340         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
341                         NULL);
342 }
343
344 /*
345  * This function must be called everytime the default service changes, the
346  * service timeserver(s) or gateway changes or the global timeserver(s) changes.
347  */
348 int __connman_timeserver_sync(struct connman_service *default_service)
349 {
350         struct connman_service *service;
351         char **nameservers;
352         int i;
353
354         if (default_service)
355                 service = default_service;
356         else
357                 service = connman_service_get_default();
358
359         if (!service)
360                 return -EINVAL;
361
362 #if !defined TIZEN_EXT
363         if (service == ts_service)
364                 return -EALREADY;
365 #endif
366
367         if (!resolv)
368                 return 0;
369         /*
370          * Before we start creating the new timeserver list we must stop
371          * any ongoing ntp query and server resolution.
372          */
373
374         __connman_ntp_stop();
375
376         ts_recheck_disable();
377
378         if (resolv_id > 0)
379                 g_resolv_cancel_lookup(resolv, resolv_id);
380
381         g_resolv_flush_nameservers(resolv);
382
383         nameservers = connman_service_get_nameservers(service);
384         if (!nameservers)
385                 return -EINVAL;
386
387         for (i = 0; nameservers[i]; i++)
388                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
389
390         g_strfreev(nameservers);
391
392         g_slist_free_full(timeservers_list, g_free);
393
394         timeservers_list = __connman_timeserver_get_all(service);
395
396         __connman_service_timeserver_changed(service, timeservers_list);
397
398         if (!timeservers_list) {
399                 DBG("No timeservers set.");
400                 return 0;
401         }
402
403         ts_recheck_enable();
404
405         ts_service = service;
406         timeserver_sync_start();
407
408         return 0;
409 }
410
411 static int timeserver_start(struct connman_service *service)
412 {
413         char **nameservers;
414         int i;
415
416         DBG("service %p", service);
417
418         i = __connman_service_get_index(service);
419         if (i < 0)
420                 return -EINVAL;
421
422         nameservers = connman_service_get_nameservers(service);
423
424         /* Stop an already ongoing resolution, if there is one */
425         if (resolv && resolv_id > 0)
426                 g_resolv_cancel_lookup(resolv, resolv_id);
427
428         /* get rid of the old resolver */
429         if (resolv) {
430                 g_resolv_unref(resolv);
431                 resolv = NULL;
432         }
433
434         resolv = g_resolv_new(i);
435         if (!resolv) {
436                 g_strfreev(nameservers);
437                 return -ENOMEM;
438         }
439
440         if (getenv("CONNMAN_RESOLV_DEBUG"))
441                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
442
443         if (nameservers) {
444                 for (i = 0; nameservers[i]; i++)
445                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
446
447                 g_strfreev(nameservers);
448         }
449
450         return __connman_timeserver_sync(service);
451 }
452
453 static void timeserver_stop(void)
454 {
455         DBG(" ");
456
457         ts_service = NULL;
458
459         if (resolv) {
460                 g_resolv_unref(resolv);
461                 resolv = NULL;
462         }
463
464         g_slist_free_full(timeservers_list, g_free);
465         timeservers_list = NULL;
466
467         g_slist_free_full(ts_list, g_free);
468         ts_list = NULL;
469
470         __connman_ntp_stop();
471
472         ts_recheck_disable();
473 }
474
475 int __connman_timeserver_system_set(char **servers)
476 {
477         save_timeservers(servers);
478
479         __connman_timeserver_sync(NULL);
480
481         return 0;
482 }
483
484 char **__connman_timeserver_system_get()
485 {
486         char **servers;
487
488         servers = load_timeservers();
489         return servers;
490 }
491
492 static void default_changed(struct connman_service *default_service)
493 {
494         if (default_service)
495                 timeserver_start(default_service);
496         else
497                 timeserver_stop();
498 }
499
500 static const struct connman_notifier timeserver_notifier = {
501         .name                   = "timeserver",
502         .default_changed        = default_changed,
503 };
504
505 int __connman_timeserver_init(void)
506 {
507         DBG("");
508
509         connman_notifier_register(&timeserver_notifier);
510
511         return 0;
512 }
513
514 void __connman_timeserver_cleanup(void)
515 {
516         DBG("");
517
518         connman_notifier_unregister(&timeserver_notifier);
519 }