timeserver.c 9.16 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>

#include <glib.h>
#include <stdlib.h>
#include <gweb/gresolv.h>
#include <netdb.h>

#include "connman.h"

35 36
#define TS_RECHECK_INTERVAL     7200

37
static GSList *ts_list = NULL;
38 39
static char *ts_current = NULL;
static int ts_recheck_id = 0;
40 41 42 43 44 45

static GResolv *resolv = NULL;
static int resolv_id = 0;

static void resolv_debug(const char *str, void *data)
{
46
	DBG("%s: %s\n", (const char *) data, str);
47 48 49 50 51 52 53
}
static void save_timeservers(char **servers)
{
	GKeyFile *keyfile;
	int cnt;

	keyfile = __connman_storage_load_global();
54
	if (!keyfile)
55 56
		keyfile = g_key_file_new();

57
	for (cnt = 0; servers && servers[cnt]; cnt++);
58 59 60 61 62 63

	g_key_file_set_string_list(keyfile, "global", "Timeservers",
			   (const gchar **)servers, cnt);

	__connman_storage_save_global(keyfile);

64
	g_key_file_unref(keyfile);
65 66 67 68

	return;
}

69
static char **load_timeservers(void)
70 71 72 73 74
{
	GKeyFile *keyfile;
	char **servers = NULL;

	keyfile = __connman_storage_load_global();
75
	if (!keyfile)
76 77 78 79 80
		return NULL;

	servers = g_key_file_get_string_list(keyfile, "global",
						"Timeservers", NULL, NULL);

81
	g_key_file_unref(keyfile);
82 83 84 85

	return servers;
}

86 87
static void resolv_result(GResolvResultStatus status, char **results,
				gpointer user_data)
88 89 90 91 92 93
{
	int i;

	DBG("status %d", status);

	if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
94
		if (results) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			for (i = 0; results[i]; i++) {
				DBG("result[%d]: %s", i, results[i]);
				if (i == 0)
					continue;

				ts_list = __connman_timeserver_add_list(
							ts_list, results[i]);
			}

			DBG("Using timeserver %s", results[0]);

			__connman_ntp_start(results[0]);

			return;
		}
	}

	/* If resolving fails, move to the next server */
	__connman_timeserver_sync_next();
}

/*
 * Once the timeserver list (ts_list) is created, we start querying the
 * servers one by one. If resolving fails on one of them, we move to the
 * next one. The user can enter either an IP address or a URL for the
120
 * timeserver. We only resolve the URLs. Once we have an IP for the NTP
121 122 123 124
 * server, we start querying it for time corrections.
 */
void __connman_timeserver_sync_next()
{
125 126 127 128
	if (ts_current) {
		g_free(ts_current);
		ts_current = NULL;
	}
129 130 131 132

	__connman_ntp_stop();

	/* Get the 1st server in the list */
133
	if (!ts_list)
134 135
		return;

136
	ts_current = ts_list->data;
137 138 139

	ts_list = g_slist_delete_link(ts_list, ts_list);

140
	/* if it's an IP, directly query it. */
141 142
	if (connman_inet_check_ipaddress(ts_current) > 0) {
		DBG("Using timeserver %s", ts_current);
143

144
		__connman_ntp_start(ts_current);
145 146 147 148

		return;
	}

149
	DBG("Resolving timeserver %s", ts_current);
150

151
	resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
152 153 154 155 156 157 158 159 160 161
						resolv_result, NULL);

	return;
}

GSList *__connman_timeserver_add_list(GSList *server_list,
		const char *timeserver)
{
	GSList *list = server_list;

162
	if (!timeserver)
163 164
		return server_list;

165
	while (list) {
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		char *existing_server = list->data;
		if (strcmp(timeserver, existing_server) == 0)
			return server_list;
		list = g_slist_next(list);
	}
	return g_slist_prepend(server_list, g_strdup(timeserver));
}

/*
 * __connman_timeserver_get_all function creates the timeserver
 * list which will be used to determine NTP server for time corrections.
 * The service settings take priority over the global timeservers.
 */
GSList *__connman_timeserver_get_all(struct connman_service *service)
{
	GSList *list = NULL;
	char **timeservers;
	char **service_ts;
	char **service_ts_config;
	char **fallback_ts;
186 187 188 189 190 191 192
	int i;

#ifdef DEFAULT_GW_SUPPORTS_NTP
	struct connman_network *network;
	const char *service_gw;
	int index;
#endif
193 194 195 196 197 198 199

	if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
		return NULL;

	service_ts_config = connman_service_get_timeservers_config(service);

	/* First add Service Timeservers.Configuration to the list */
200
	for (i = 0; service_ts_config && service_ts_config[i];
201 202 203 204 205 206
			i++)
		list = __connman_timeserver_add_list(list,
				service_ts_config[i]);

	service_ts = connman_service_get_timeservers(service);

207
	/* Then add Service Timeservers via DHCP to the list */
208
	for (i = 0; service_ts && service_ts[i]; i++)
209 210
		list = __connman_timeserver_add_list(list, service_ts[i]);

211
#ifdef DEFAULT_GW_SUPPORTS_NTP
212
	network = __connman_service_get_network(service);
213
	if (network) {
214 215 216 217 218
		index = connman_network_get_index(network);
		service_gw = __connman_ipconfig_get_gateway_from_index(index,
			CONNMAN_IPCONFIG_TYPE_ALL);

		/* Then add Service Gateway to the list */
219
		if (service_gw)
220 221
			list = __connman_timeserver_add_list(list, service_gw);
	}
222
#endif
223 224 225 226

	/* Then add Global Timeservers to the list */
	timeservers = load_timeservers();

227
	for (i = 0; timeservers && timeservers[i]; i++)
228 229 230 231 232 233 234
		list = __connman_timeserver_add_list(list, timeservers[i]);

	g_strfreev(timeservers);

	fallback_ts = connman_setting_get_string_list("FallbackTimeservers");

	/* Lastly add the fallback servers */
235
	for (i = 0; fallback_ts && fallback_ts[i]; i++)
236 237 238 239 240
		list = __connman_timeserver_add_list(list, fallback_ts[i]);

	return g_slist_reverse(list);
}

241 242 243 244
static gboolean ts_recheck(gpointer user_data)
{
	GSList *ts;

245
	ts = __connman_timeserver_get_all(connman_service_get_default());
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	if (!ts) {
		DBG("timeservers disabled");

		return TRUE;
	}

	if (g_strcmp0(ts_current, ts->data) != 0) {
		DBG("current %s preferred %s", ts_current, (char *)ts->data);

		g_slist_free_full(ts, g_free);

		__connman_timeserver_sync(NULL);

		return FALSE;
	}

	DBG("");

	g_slist_free_full(ts, g_free);

	return TRUE;
}

static void ts_recheck_disable(void)
{
	if (ts_recheck_id == 0)
		return;

	g_source_remove(ts_recheck_id);
	ts_recheck_id = 0;

	if (ts_current) {
		g_free(ts_current);
		ts_current = NULL;
	}
}

static void ts_recheck_enable(void)
{
	if (ts_recheck_id > 0)
		return;

	ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
			NULL);
}

293 294
/*
 * This function must be called everytime the default service changes, the
295
 * service timeserver(s) or gateway changes or the global timeserver(s) changes.
296 297 298 299 300
 */
int __connman_timeserver_sync(struct connman_service *default_service)
{
	struct connman_service *service;

301
	if (default_service)
302 303
		service = default_service;
	else
304
		service = connman_service_get_default();
305

306
	if (!service)
307 308
		return -EINVAL;

309
	if (!resolv)
310 311 312 313 314 315 316 317
		return 0;
	/*
	 * Before we start creating the new timeserver list we must stop
	 * any ongoing ntp query and server resolution.
	 */

	__connman_ntp_stop();

318 319
	ts_recheck_disable();

320 321 322 323 324 325 326 327 328
	if (resolv_id > 0)
		g_resolv_cancel_lookup(resolv, resolv_id);

	g_slist_free_full(ts_list, g_free);

	ts_list = __connman_timeserver_get_all(service);

	__connman_service_timeserver_changed(service, ts_list);

329
	if (!ts_list) {
330 331 332 333
		DBG("No timeservers set.");
		return 0;
	}

334 335 336
	ts_recheck_enable();

	__connman_timeserver_sync_next();
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

	return 0;
}

static int timeserver_start(struct connman_service *service)
{
	char **nameservers;
	int i;

	DBG("service %p", service);

	i = __connman_service_get_index(service);
	if (i < 0)
		return -EINVAL;

	nameservers = connman_service_get_nameservers(service);
353
	if (!nameservers)
354 355 356
		return -EINVAL;

	/* Stop an already ongoing resolution, if there is one */
357
	if (resolv && resolv_id > 0)
358 359 360
		g_resolv_cancel_lookup(resolv, resolv_id);

	/* get rid of the old resolver */
361
	if (resolv) {
362 363 364 365 366
		g_resolv_unref(resolv);
		resolv = NULL;
	}

	resolv = g_resolv_new(i);
367
	if (!resolv) {
368 369 370 371 372 373 374
		g_strfreev(nameservers);
		return -ENOMEM;
	}

	if (getenv("CONNMAN_RESOLV_DEBUG"))
		g_resolv_set_debug(resolv, resolv_debug, "RESOLV");

375
	for (i = 0; nameservers[i]; i++)
376 377 378 379 380 381 382
		g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);

	g_strfreev(nameservers);

	return __connman_timeserver_sync(service);
}

383
static void timeserver_stop(void)
384 385 386
{
	DBG(" ");

387
	if (resolv) {
388 389 390 391 392 393 394 395 396
		g_resolv_unref(resolv);
		resolv = NULL;
	}

	g_slist_free_full(ts_list, g_free);

	ts_list = NULL;

	__connman_ntp_stop();
397 398

	ts_recheck_disable();
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
}

int __connman_timeserver_system_set(char **servers)
{
	save_timeservers(servers);

	__connman_timeserver_sync(NULL);

	return 0;
}

char **__connman_timeserver_system_get()
{
	char **servers;

	servers = load_timeservers();
	return servers;
}

static void default_changed(struct connman_service *default_service)
{
420
	if (default_service)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		timeserver_start(default_service);
	else
		timeserver_stop();
}

static struct connman_notifier timeserver_notifier = {
	.name			= "timeserver",
	.default_changed	= default_changed,
};

int __connman_timeserver_init(void)
{
	DBG("");

	connman_notifier_register(&timeserver_notifier);

	return 0;
}

void __connman_timeserver_cleanup(void)
{
	DBG("");

	connman_notifier_unregister(&timeserver_notifier);
}