5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <gweb/gresolv.h>
34 static char **system_timeservers = NULL;
35 static char **timeservers = NULL;
37 static GResolv *resolv = NULL;
38 static int resolv_id = 0;
39 static volatile int count;
41 static void resolv_debug(const char *str, void *data)
43 connman_info("%s: %s\n", (const char *) data, str);
45 static void save_timeservers(char **servers)
50 keyfile = __connman_storage_load_global();
52 keyfile = g_key_file_new();
54 for (cnt = 0; servers != NULL && servers[cnt] != NULL; cnt++);
56 g_key_file_set_string_list(keyfile, "global", "Timeservers",
57 (const gchar **)servers, cnt);
59 __connman_storage_save_global(keyfile);
61 g_key_file_free(keyfile);
66 static char **load_timeservers()
70 char **servers = NULL;
72 keyfile = __connman_storage_load_global();
76 servers = g_key_file_get_string_list(keyfile, "global",
77 "Timeservers", NULL, &error);
79 DBG("Error loading timeservers: %s", error->message);
83 g_key_file_free(keyfile);
88 /* Restart NTP procedure */
89 static void connman_timeserver_restart()
91 /* If service timeservers are in use, dont restart ntp */
92 if (timeservers != NULL)
96 DBG("No online service.");
100 /* Cancel current lookup */
102 g_resolv_cancel_lookup(resolv, resolv_id);
104 /* Reload system timeserver list */
105 if (system_timeservers != NULL) {
106 g_strfreev(system_timeservers);
107 system_timeservers = NULL;
110 system_timeservers = load_timeservers();
112 if (system_timeservers == NULL)
115 __connman_ntp_stop();
119 __connman_timeserver_sync_next();
122 static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
126 DBG("status %d", status);
128 __sync_fetch_and_add(&count, 1);
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]);
135 __connman_ntp_start(results[0]);
141 __connman_timeserver_sync_next();
144 void __connman_timeserver_sync_next()
146 if (system_timeservers == NULL ||
147 system_timeservers[count] == NULL)
150 DBG("Trying timeserver %s", system_timeservers[count]);
153 resolv_id = g_resolv_lookup_hostname(resolv,
154 system_timeservers[count], resolv_result,
158 int __connman_timeserver_sync(struct connman_service *service)
160 char **nameservers = NULL;
163 DBG("service %p", service);
165 i = __connman_service_get_index(service);
169 nameservers = connman_service_get_nameservers(service);
170 if (nameservers == NULL)
173 resolv = g_resolv_new(i);
177 if (getenv("CONNMAN_RESOLV_DEBUG"))
178 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
180 for (i = 0; nameservers[i] != NULL; i++)
181 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
185 system_timeservers = load_timeservers();
187 timeservers = connman_service_get_timeservers(service);
189 if (timeservers != NULL && timeservers[0] != NULL) {
190 DBG("Using service tiemservers");
191 __connman_ntp_start(timeservers[0]);
195 if (system_timeservers == NULL || system_timeservers[count] == NULL) {
196 DBG("No timeservers set.");
200 DBG("Trying server %s", system_timeservers[count]);
202 resolv_id = g_resolv_lookup_hostname(resolv, system_timeservers[count],
203 resolv_result, NULL);
207 void __connman_timeserver_stop()
211 if (resolv != NULL) {
212 g_resolv_unref(resolv);
216 if (system_timeservers != NULL) {
217 g_strfreev(system_timeservers);
218 system_timeservers = NULL;
225 __connman_ntp_stop();
228 int __connman_timeserver_system_append(const char *server)
231 char **servers = NULL;
233 if (server == NULL) {
234 save_timeservers(servers);
238 DBG("server %s", server);
240 servers = load_timeservers();
242 if (servers != NULL) {
245 for (i = 0; servers[i] != NULL; i++)
246 if (g_strcmp0(servers[i], server) == 0) {
251 len = g_strv_length(servers);
252 servers = g_try_renew(char *, servers, len + 2);
255 servers = g_try_new0(char *, len + 2);
261 servers[len] = g_strdup(server);
262 servers[len + 1] = NULL;
264 save_timeservers(servers);
268 connman_timeserver_restart();
273 int __connman_timeserver_system_remove(const char *server)
282 DBG("server %s", server);
284 servers = load_timeservers();
289 len = g_strv_length(servers);
291 if (g_strcmp0(servers[0], server) != 0) {
298 save_timeservers(servers);
302 temp = g_try_new0(char *, len - 1);
308 for (i = 0, j = 0; i < len; i++) {
309 if (g_strcmp0(servers[i], server) != 0) {
310 temp[j] = g_strdup(servers[i]);
314 temp[len - 1] = NULL;
317 servers = g_strdupv(temp);
320 save_timeservers(servers);
323 connman_timeserver_restart();
328 char **__connman_timeserver_system_get()
332 servers = load_timeservers();
336 int __connman_timeserver_init(void)
343 void __connman_timeserver_cleanup(void)