*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
*
* 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
return;
}
+GSList *__connman_timeserver_add_list(GSList *server_list,
+ const char *timeserver)
+{
+ GSList *list = server_list;
+
+ if (timeserver == NULL)
+ return server_list;
+
+ while (list != NULL) {
+ 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_sync function recreates the timeserver
+ * __connman_timeserver_get_all function creates the timeserver
* list which will be used to determine NTP server for time corrections.
- * It must be called everytime the default service changes, the service
- * timeserver(s) or gatway changes or the global timeserver(s) changes.
* The service settings take priority over the global timeservers.
*/
-int __connman_timeserver_sync(struct connman_service *default_service)
+GSList *__connman_timeserver_get_all(struct connman_service *service)
{
- struct connman_service *service;
+ GSList *list = NULL;
struct connman_network *network;
char **timeservers;
char **service_ts;
char **service_ts_config;
const char *service_gw;
+ char **fallback_ts;
int index, i;
+ service_ts_config = connman_service_get_timeservers_config(service);
+
+ /* First add Service Timeservers.Configuration to the list */
+ for (i = 0; service_ts_config != NULL && service_ts_config[i] != NULL;
+ i++)
+ list = __connman_timeserver_add_list(list,
+ service_ts_config[i]);
+
+ service_ts = connman_service_get_timeservers(service);
+
+ /* First add Service Timeservers via DHCP to the list */
+ for (i = 0; service_ts != NULL && service_ts[i] != NULL; i++)
+ list = __connman_timeserver_add_list(list, service_ts[i]);
+
+ network = __connman_service_get_network(service);
+ if (network != NULL) {
+ 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 */
+ if (service_gw != NULL)
+ list = __connman_timeserver_add_list(list, service_gw);
+ }
+
+ /* Then add Global Timeservers to the list */
+ timeservers = load_timeservers();
+
+ for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
+ list = __connman_timeserver_add_list(list, timeservers[i]);
+
+ g_strfreev(timeservers);
+
+ fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
+
+ /* Lastly add the fallback servers */
+ for (i = 0; fallback_ts != NULL && fallback_ts[i] != NULL; i++)
+ list = __connman_timeserver_add_list(list, fallback_ts[i]);
+
+ return g_slist_reverse(list);
+}
+
+/*
+ * This function must be called everytime the default service changes, the
+ * service timeserver(s) or gatway changes or the global timeserver(s) changes.
+ */
+int __connman_timeserver_sync(struct connman_service *default_service)
+{
+ struct connman_service *service;
+
if (default_service != NULL)
service = default_service;
else
if (resolv == NULL)
return 0;
/*
- * Before be start creating the new timeserver list we must stop
+ * Before we start creating the new timeserver list we must stop
* any ongoing ntp query and server resolution.
*/
if (resolv_id > 0)
g_resolv_cancel_lookup(resolv, resolv_id);
- if (ts_list != NULL) {
- g_slist_free_full(ts_list, g_free);
- ts_list = NULL;
- }
-
- service_ts_config = connman_service_get_timeservers_config(service);
+ g_slist_free_full(ts_list, g_free);
- /* First add Service Timeservers.Configuration to the list */
- for (i=0; service_ts_config != NULL && service_ts_config[i] != NULL; i++)
- ts_list = g_slist_prepend(ts_list, g_strdup(service_ts_config[i]));
+ ts_list = __connman_timeserver_get_all(service);
- service_ts = connman_service_get_timeservers(service);
-
- /* First add Service Timeservers via DHCP to the list */
- for (i=0; service_ts != NULL && service_ts[i] != NULL; i++)
- ts_list = g_slist_prepend(ts_list, g_strdup(service_ts[i]));
-
- network = __connman_service_get_network(service);
-
- index = connman_network_get_index(network);
-
- service_gw = __connman_ipconfig_get_gateway_from_index(index);
-
- /* Then add Service Gateway to the list */
- if (service_gw != NULL)
- ts_list = g_slist_prepend(ts_list, g_strdup(service_gw));
-
- /* Then add Global Timeservers to the list */
- timeservers = load_timeservers();
-
- for (i=0; timeservers != NULL && timeservers[i] != NULL; i++)
- ts_list = g_slist_prepend(ts_list, g_strdup(timeservers[i]));
+ __connman_service_timeserver_changed(service, ts_list);
if (ts_list == NULL) {
DBG("No timeservers set.");
return 0;
}
- ts_list = g_slist_reverse(ts_list);
-
__connman_timeserver_sync_next();
return 0;
return -EINVAL;
/* Stop an already ongoing resolution, if there is one */
- if (resolv_id > 0)
+ if (resolv != NULL && resolv_id > 0)
g_resolv_cancel_lookup(resolv, resolv_id);
/* get rid of the old resolver */
}
resolv = g_resolv_new(i);
- if (resolv == NULL)
+ if (resolv == NULL) {
+ g_strfreev(nameservers);
return -ENOMEM;
+ }
if (getenv("CONNMAN_RESOLV_DEBUG"))
g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
for (i = 0; nameservers[i] != NULL; i++)
g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
+ g_strfreev(nameservers);
+
return __connman_timeserver_sync(service);
}
resolv = NULL;
}
- if (ts_list != NULL)
- g_slist_free_full(ts_list, g_free);
+ g_slist_free_full(ts_list, g_free);
ts_list = NULL;