provider: Allow user to set routes when creating provider
[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
32 #include "connman.h"
33
34 static char **system_timeservers = NULL;
35 static char **timeservers = NULL;
36
37 static GResolv *resolv = NULL;
38 static int resolv_id = 0;
39 static volatile int count;
40
41 static void resolv_debug(const char *str, void *data)
42 {
43         connman_info("%s: %s\n", (const char *) data, str);
44 }
45 static void save_timeservers(char **servers)
46 {
47         GKeyFile *keyfile;
48         int cnt;
49
50         keyfile = __connman_storage_load_global();
51         if (keyfile == NULL)
52                 keyfile = g_key_file_new();
53
54         for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++);
55
56         g_key_file_set_string_list(keyfile, "global", "Timeservers",
57                            (const gchar **)servers, cnt);
58
59         __connman_storage_save_global(keyfile);
60
61         g_key_file_free(keyfile);
62
63         return;
64 }
65
66 static char **load_timeservers()
67 {
68         GKeyFile *keyfile;
69         GError *error = NULL;
70         char **servers = NULL;
71
72         keyfile = __connman_storage_load_global();
73         if (keyfile == NULL)
74                 return NULL;
75
76         servers = g_key_file_get_string_list(keyfile, "global",
77                                                 "Timeservers", NULL, &error);
78         if (error) {
79                 DBG("Error loading timeservers: %s", error->message);
80                 g_error_free(error);
81         }
82
83         g_key_file_free(keyfile);
84
85         return servers;
86 }
87
88 /* Restart NTP procedure */
89 static void connman_timeserver_restart()
90 {
91         /* If service timeservers are in use, dont restart ntp */
92         if (timeservers != NULL)
93                 return;
94
95         if (resolv == NULL) {
96                 DBG("No online service.");
97                 return;
98         }
99
100         /* Cancel current lookup */
101         if(resolv_id > 0)
102                 g_resolv_cancel_lookup(resolv, resolv_id);
103
104         /* Reload system timeserver list */
105         if (system_timeservers != NULL) {
106                 g_strfreev(system_timeservers);
107                 system_timeservers = NULL;
108         }
109
110         system_timeservers = load_timeservers();
111
112         if (system_timeservers == NULL)
113                 return;
114
115         __connman_ntp_stop();
116
117         count = 0;
118
119         __connman_timeserver_sync_next();
120 }
121
122 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
123 {
124         int i;
125
126         DBG("status %d", status);
127
128         __sync_fetch_and_add(&count, 1);
129
130         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
131                 if (results != NULL) {
132                         for (i = 0; results[i]; i++)
133                                 DBG("result: %s", results[i]);
134
135                         __connman_ntp_start(results[0]);
136
137                         return;
138                 }
139         }
140
141         __connman_timeserver_sync_next();
142 }
143
144 void __connman_timeserver_sync_next()
145 {
146         if (system_timeservers == NULL ||
147         system_timeservers[count] == NULL)
148                 return;
149
150         DBG("Trying timeserver %s", system_timeservers[count]);
151
152         if (resolv)
153                 resolv_id = g_resolv_lookup_hostname(resolv,
154                         system_timeservers[count], resolv_result,
155                                                 NULL);
156 }
157
158 int __connman_timeserver_sync(struct connman_service *service)
159 {
160         char **nameservers = NULL;
161         int i;
162
163         DBG("service %p", service);
164
165         i = __connman_service_get_index(service);
166         if (i < 0)
167                 return -EINVAL;
168
169         nameservers = connman_service_get_nameservers(service);
170         if (nameservers == NULL)
171                 return -EINVAL;
172
173         resolv = g_resolv_new(i);
174         if (resolv == NULL)
175                 return -ENOMEM;
176
177         if (getenv("CONNMAN_RESOLV_DEBUG"))
178                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
179
180         for (i = 0; nameservers[i] != NULL; i++)
181                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
182
183         count = 0;
184
185         system_timeservers = load_timeservers();
186
187         timeservers = connman_service_get_timeservers(service);
188
189         if (timeservers != NULL && timeservers[0] != NULL) {
190                 DBG("Using service tiemservers");
191                 __connman_ntp_start(timeservers[0]);
192                 return 0;
193         }
194
195         if (system_timeservers == NULL || system_timeservers[count] == NULL) {
196                 DBG("No timeservers set.");
197                 return 0;
198         }
199
200         DBG("Trying server %s", system_timeservers[count]);
201
202         resolv_id = g_resolv_lookup_hostname(resolv, system_timeservers[count],
203                                                 resolv_result, NULL);
204         return 0;
205 }
206
207 void __connman_timeserver_stop()
208 {
209         DBG(" ");
210
211         if (resolv != NULL) {
212                 g_resolv_unref(resolv);
213                 resolv = NULL;
214         }
215
216         if (system_timeservers != NULL) {
217                 g_strfreev(system_timeservers);
218                 system_timeservers = NULL;
219         }
220
221         timeservers = NULL;
222
223         count = 0;
224
225         __connman_ntp_stop();
226 }
227
228 int __connman_timeserver_system_append(const char *server)
229 {
230         int len;
231         char **servers = NULL;
232
233         if (server == NULL) {
234                 save_timeservers(servers);
235                 goto restart;
236         }
237
238         DBG("server %s", server);
239
240         servers = load_timeservers();
241
242         if (servers != NULL) {
243                 int i;
244
245                 for (i = 0; servers[i] != NULL; i++)
246                         if (g_strcmp0(servers[i], server) == 0) {
247                                 g_strfreev(servers);
248                                 return -EEXIST;
249                         }
250
251                 len = g_strv_length(servers);
252                 servers = g_try_renew(char *, servers, len + 2);
253         } else {
254                 len = 0;
255                 servers = g_try_new0(char *, len + 2);
256         }
257
258         if (servers == NULL)
259                 return -ENOMEM;
260
261         servers[len] = g_strdup(server);
262         servers[len + 1] = NULL;
263
264         save_timeservers(servers);
265
266         g_strfreev(servers);
267 restart:
268         connman_timeserver_restart();
269
270         return 0;
271 }
272
273 int __connman_timeserver_system_remove(const char *server)
274 {
275         char **servers;
276         char **temp;
277         int len, i, j;
278
279         if (server == NULL)
280                 return -EINVAL;
281
282         DBG("server %s", server);
283
284         servers = load_timeservers();
285
286         if (servers == NULL)
287                 return 0;
288
289         len = g_strv_length(servers);
290         if (len == 1) {
291                 if (g_strcmp0(servers[0], server) != 0) {
292                         g_strfreev(servers);
293                         return 0;
294                 }
295
296                 g_strfreev(servers);
297                 servers = NULL;
298                 save_timeservers(servers);
299                 return 0;
300         }
301
302         temp = g_try_new0(char *, len - 1);
303         if (temp == NULL) {
304                         g_strfreev(servers);
305                         return -ENOMEM;
306         }
307
308         for (i = 0, j = 0; i < len; i++) {
309                 if (g_strcmp0(servers[i], server) != 0) {
310                         temp[j] = g_strdup(servers[i]);
311                         j++;
312                 }
313         }
314         temp[len - 1] = NULL;
315
316         g_strfreev(servers);
317         servers = g_strdupv(temp);
318         g_strfreev(temp);
319
320         save_timeservers(servers);
321         g_strfreev(servers);
322
323         connman_timeserver_restart();
324
325         return 0;
326 }
327
328 char **__connman_timeserver_system_get()
329 {
330         char **servers;
331
332         servers = load_timeservers();
333         return servers;
334 }
335
336 int __connman_timeserver_init(void)
337 {
338         DBG("");
339
340         return 0;
341 }
342
343 void __connman_timeserver_cleanup(void)
344 {
345         DBG("");
346 }