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 *default_service)
160 struct connman_service *service;
164 if (default_service != NULL)
165 service = default_service;
167 service = __connman_service_get_default();
172 DBG("service %p", service);
174 i = __connman_service_get_index(service);
178 nameservers = connman_service_get_nameservers(service);
179 if (nameservers == NULL)
182 resolv = g_resolv_new(i);
186 if (getenv("CONNMAN_RESOLV_DEBUG"))
187 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
189 for (i = 0; nameservers[i] != NULL; i++)
190 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
194 system_timeservers = load_timeservers();
196 timeservers = connman_service_get_timeservers(service);
198 if (timeservers != NULL && timeservers[0] != NULL) {
199 DBG("Using service tiemservers");
200 __connman_ntp_start(timeservers[0]);
204 if (system_timeservers == NULL || system_timeservers[count] == NULL) {
205 DBG("No timeservers set.");
209 DBG("Trying server %s", system_timeservers[count]);
211 resolv_id = g_resolv_lookup_hostname(resolv, system_timeservers[count],
212 resolv_result, NULL);
216 void __connman_timeserver_stop()
220 if (resolv != NULL) {
221 g_resolv_unref(resolv);
225 if (system_timeservers != NULL) {
226 g_strfreev(system_timeservers);
227 system_timeservers = NULL;
234 __connman_ntp_stop();
237 int __connman_timeserver_system_set(char **servers)
239 save_timeservers(servers);
241 connman_timeserver_restart();
246 char **__connman_timeserver_system_get()
250 servers = load_timeservers();
254 static void default_changed(struct connman_service *default_service)
258 if (default_service != NULL)
259 __connman_timeserver_sync(default_service);
261 __connman_timeserver_stop();
264 static struct connman_notifier timeserver_notifier = {
265 .name = "timeserver",
266 .default_changed = default_changed,
269 int __connman_timeserver_init(void)
273 connman_notifier_register(×erver_notifier);
278 void __connman_timeserver_cleanup(void)
282 connman_notifier_unregister(×erver_notifier);