cfa4a82f5884487be67208d3081d8344a6e4cb0c
[platform/upstream/connman.git] / src / timeserver.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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
160 /*
161  * __connman_timeserver_sync function recreates the timeserver
162  * list which will be used to determine NTP server for time corrections.
163  * It must be called everytime the default service changes, the service
164  * timeserver(s) changes or the global timeserver(s) changes.The service
165  * settings take priority over the global timeservers.
166  */
167 int __connman_timeserver_sync(struct connman_service *default_service)
168 {
169         struct connman_service *service;
170         char **timeservers;
171         char **service_ts;
172         int i;
173
174         if (default_service != NULL)
175                 service = default_service;
176         else
177                 service = __connman_service_get_default();
178
179         if (service == NULL)
180                 return -EINVAL;
181
182         if (resolv == NULL)
183                 return 0;
184         /*
185          * Before be start creating the new timeserver list we must stop
186          * any ongoing ntp query and server resolution.
187          */
188
189         __connman_ntp_stop();
190
191         if (resolv_id > 0)
192                 g_resolv_cancel_lookup(resolv, resolv_id);
193
194         if (ts_list != NULL) {
195                 g_slist_free_full(ts_list, g_free);
196                 ts_list = NULL;
197         }
198
199         service_ts = connman_service_get_timeservers(service);
200
201         for (i=0; service_ts != NULL && service_ts[i] != NULL; i++)
202                 ts_list = g_slist_prepend(ts_list, g_strdup(service_ts[i]));
203
204         timeservers = load_timeservers();
205
206         for (i=0; timeservers != NULL && timeservers[i] != NULL; i++)
207                 ts_list = g_slist_prepend(ts_list, g_strdup(timeservers[i]));
208
209         if (ts_list == NULL) {
210                 DBG("No timeservers set.");
211                 return 0;
212         }
213
214         ts_list = g_slist_reverse(ts_list);
215
216         __connman_timeserver_sync_next();
217
218         return 0;
219 }
220
221 static int timeserver_start(struct connman_service *service)
222 {
223         char **nameservers;
224         int i;
225
226         DBG("service %p", service);
227
228         i = __connman_service_get_index(service);
229         if (i < 0)
230                 return -EINVAL;
231
232         nameservers = connman_service_get_nameservers(service);
233         if (nameservers == NULL)
234                 return -EINVAL;
235
236         /* Stop an already ongoing resolution, if there is one */
237         if (resolv_id > 0)
238                 g_resolv_cancel_lookup(resolv, resolv_id);
239
240         /* get rid of the old resolver */
241         if (resolv != NULL) {
242                 g_resolv_unref(resolv);
243                 resolv = NULL;
244         }
245
246         resolv = g_resolv_new(i);
247         if (resolv == NULL)
248                 return -ENOMEM;
249
250         if (getenv("CONNMAN_RESOLV_DEBUG"))
251                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
252
253         for (i = 0; nameservers[i] != NULL; i++)
254                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
255
256         return __connman_timeserver_sync(service);
257 }
258
259 static void timeserver_stop()
260 {
261         DBG(" ");
262
263         if (resolv != NULL) {
264                 g_resolv_unref(resolv);
265                 resolv = NULL;
266         }
267
268         if (ts_list != NULL)
269                 g_slist_free_full(ts_list, g_free);
270
271         ts_list = NULL;
272
273         __connman_ntp_stop();
274 }
275
276 int __connman_timeserver_system_set(char **servers)
277 {
278         save_timeservers(servers);
279
280         __connman_timeserver_sync(NULL);
281
282         return 0;
283 }
284
285 char **__connman_timeserver_system_get()
286 {
287         char **servers;
288
289         servers = load_timeservers();
290         return servers;
291 }
292
293 static void default_changed(struct connman_service *default_service)
294 {
295         if (default_service != NULL)
296                 timeserver_start(default_service);
297         else
298                 timeserver_stop();
299 }
300
301 static struct connman_notifier timeserver_notifier = {
302         .name                   = "timeserver",
303         .default_changed        = default_changed,
304 };
305
306 int __connman_timeserver_init(void)
307 {
308         DBG("");
309
310         connman_notifier_register(&timeserver_notifier);
311
312         return 0;
313 }
314
315 void __connman_timeserver_cleanup(void)
316 {
317         DBG("");
318
319         connman_notifier_unregister(&timeserver_notifier);
320 }