gdbus: Add support for org.freedesktop.DBus.ObjectManager interface
[platform/upstream/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         char **servers = NULL;
69
70         keyfile = __connman_storage_load_global();
71         if (keyfile == NULL)
72                 return NULL;
73
74         servers = g_key_file_get_string_list(keyfile, "global",
75                                                 "Timeservers", NULL, NULL);
76
77         g_key_file_free(keyfile);
78
79         return servers;
80 }
81
82 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
83 {
84         int i;
85
86         DBG("status %d", status);
87
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]);
92                                 if (i == 0)
93                                         continue;
94
95                                 ts_list = __connman_timeserver_add_list(
96                                                         ts_list, results[i]);
97                         }
98
99                         DBG("Using timeserver %s", results[0]);
100
101                         __connman_ntp_start(results[0]);
102
103                         return;
104                 }
105         }
106
107         /* If resolving fails, move to the next server */
108         __connman_timeserver_sync_next();
109 }
110
111 /*
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.
117  */
118 void __connman_timeserver_sync_next()
119 {
120         char *server;
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         /* if its a IP , directly query it. */
133         if (connman_inet_check_ipaddress(server) > 0) {
134                 DBG("Using timeserver %s", server);
135
136                 __connman_ntp_start(server);
137
138                 g_free(server);
139                 return;
140         }
141
142         DBG("Resolving server %s", server);
143
144         resolv_id = g_resolv_lookup_hostname(resolv, server,
145                                                 resolv_result, NULL);
146
147         g_free(server);
148
149         return;
150 }
151
152 GSList *__connman_timeserver_add_list(GSList *server_list,
153                 const char *timeserver)
154 {
155         GSList *list = server_list;
156
157         if (timeserver == NULL)
158                 return server_list;
159
160         while (list != NULL) {
161                 char *existing_server = list->data;
162                 if (strcmp(timeserver, existing_server) == 0)
163                         return server_list;
164                 list = g_slist_next(list);
165         }
166         return g_slist_prepend(server_list, g_strdup(timeserver));
167 }
168
169 /*
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.
173  */
174 GSList *__connman_timeserver_get_all(struct connman_service *service)
175 {
176         GSList *list = NULL;
177         struct connman_network *network;
178         char **timeservers;
179         char **service_ts;
180         char **service_ts_config;
181         const char *service_gw;
182         char **fallback_ts;
183         int index, i;
184
185         service_ts_config = connman_service_get_timeservers_config(service);
186
187         /* First add Service Timeservers.Configuration to the list */
188         for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
189                         i++)
190                 list = __connman_timeserver_add_list(list,
191                                 service_ts_config[i]);
192
193         service_ts = connman_service_get_timeservers(service);
194
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]);
198
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);
204
205                 /* Then add Service Gateway to the list */
206                 if (service_gw != NULL)
207                         list = __connman_timeserver_add_list(list, service_gw);
208         }
209
210         /* Then add Global Timeservers to the list */
211         timeservers = load_timeservers();
212
213         for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
214                 list = __connman_timeserver_add_list(list, timeservers[i]);
215
216         g_strfreev(timeservers);
217
218         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
219
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]);
223
224         return g_slist_reverse(list);
225 }
226
227 /*
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.
230  */
231 int __connman_timeserver_sync(struct connman_service *default_service)
232 {
233         struct connman_service *service;
234
235         if (default_service != NULL)
236                 service = default_service;
237         else
238                 service = __connman_service_get_default();
239
240         if (service == NULL)
241                 return -EINVAL;
242
243         if (resolv == NULL)
244                 return 0;
245         /*
246          * Before we start creating the new timeserver list we must stop
247          * any ongoing ntp query and server resolution.
248          */
249
250         __connman_ntp_stop();
251
252         if (resolv_id > 0)
253                 g_resolv_cancel_lookup(resolv, resolv_id);
254
255         g_slist_free_full(ts_list, g_free);
256
257         ts_list = __connman_timeserver_get_all(service);
258
259         __connman_service_timeserver_changed(service, ts_list);
260
261         if (ts_list == NULL) {
262                 DBG("No timeservers set.");
263                 return 0;
264         }
265
266         __connman_timeserver_sync_next();
267
268         return 0;
269 }
270
271 static int timeserver_start(struct connman_service *service)
272 {
273         char **nameservers;
274         int i;
275
276         DBG("service %p", service);
277
278         i = __connman_service_get_index(service);
279         if (i < 0)
280                 return -EINVAL;
281
282         nameservers = connman_service_get_nameservers(service);
283         if (nameservers == NULL)
284                 return -EINVAL;
285
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);
289
290         /* get rid of the old resolver */
291         if (resolv != NULL) {
292                 g_resolv_unref(resolv);
293                 resolv = NULL;
294         }
295
296         resolv = g_resolv_new(i);
297         if (resolv == NULL) {
298                 g_strfreev(nameservers);
299                 return -ENOMEM;
300         }
301
302         if (getenv("CONNMAN_RESOLV_DEBUG"))
303                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
304
305         for (i = 0; nameservers[i] != NULL; i++)
306                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
307
308         g_strfreev(nameservers);
309
310         return __connman_timeserver_sync(service);
311 }
312
313 static void timeserver_stop()
314 {
315         DBG(" ");
316
317         if (resolv != NULL) {
318                 g_resolv_unref(resolv);
319                 resolv = NULL;
320         }
321
322         g_slist_free_full(ts_list, g_free);
323
324         ts_list = NULL;
325
326         __connman_ntp_stop();
327 }
328
329 int __connman_timeserver_system_set(char **servers)
330 {
331         save_timeservers(servers);
332
333         __connman_timeserver_sync(NULL);
334
335         return 0;
336 }
337
338 char **__connman_timeserver_system_get()
339 {
340         char **servers;
341
342         servers = load_timeservers();
343         return servers;
344 }
345
346 static void default_changed(struct connman_service *default_service)
347 {
348         if (default_service != NULL)
349                 timeserver_start(default_service);
350         else
351                 timeserver_stop();
352 }
353
354 static struct connman_notifier timeserver_notifier = {
355         .name                   = "timeserver",
356         .default_changed        = default_changed,
357 };
358
359 int __connman_timeserver_init(void)
360 {
361         DBG("");
362
363         connman_notifier_register(&timeserver_notifier);
364
365         return 0;
366 }
367
368 void __connman_timeserver_cleanup(void)
369 {
370         DBG("");
371
372         connman_notifier_unregister(&timeserver_notifier);
373 }