*
* 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
#include <glib.h>
#include <stdlib.h>
#include <gweb/gresolv.h>
+#include <netdb.h>
#include "connman.h"
-static GSList *driver_list = NULL;
-static GHashTable *server_hash = NULL;
-
-static char **system_timeservers = NULL;
+static GSList *ts_list = NULL;
static GResolv *resolv = NULL;
static int resolv_id = 0;
-static volatile int count;
static void resolv_debug(const char *str, void *data)
{
return servers;
}
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
{
- const struct connman_timeserver_driver *driver1 = a;
- const struct connman_timeserver_driver *driver2 = b;
+ int i;
- return driver2->priority - driver1->priority;
-}
+ DBG("status %d", status);
-/**
- * connman_timeserver_driver_register:
- * @driver: timeserver driver definition
- *
- * Register a new timeserver driver
- *
- * Returns: %0 on success
- */
-int connman_timeserver_driver_register(struct connman_timeserver_driver *driver)
-{
- DBG("driver %p name %s", driver, driver->name);
+ if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
+ if (results != NULL) {
+ for (i = 0; results[i]; i++)
+ DBG("result: %s", results[i]);
- driver_list = g_slist_insert_sorted(driver_list, driver,
- compare_priority);
+ __connman_ntp_start(results[0]);
- return 0;
+ return;
+ }
+ }
+
+ /* If resolving fails, move to the next server */
+ __connman_timeserver_sync_next();
}
-/**
- * connman_timeserver_driver_unregister:
- * @driver: timeserver driver definition
- *
- * Remove a previously registered timeserver driver
+/*
+ * 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
+ * timeserver. We only resolve the urls. Once we have a IP for the NTP
+ * server, we start querying it for time corrections.
*/
-void connman_timeserver_driver_unregister(struct connman_timeserver_driver *driver)
+void __connman_timeserver_sync_next()
{
- DBG("driver %p name %s", driver, driver->name);
+ char *server;
+ int ret;
+ struct addrinfo hints;
+ struct addrinfo *addr;
- driver_list = g_slist_remove(driver_list, driver);
-}
+ __connman_ntp_stop();
-/**
- * connman_timeserver_append:
- * @server: server address
- *
- * Append time server server address to current list
- */
-int connman_timeserver_append(const char *server)
-{
- GSList *list;
+ /* Get the 1st server in the list */
+ if (ts_list == NULL)
+ return;
- DBG("server %s", server);
+ server = ts_list->data;
- if (server == NULL)
- return -EINVAL;
+ ts_list = g_slist_delete_link(ts_list, ts_list);
- /* This server is already handled by a driver */
- if (g_hash_table_lookup(server_hash, server))
- return 0;
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_flags = AI_NUMERICHOST;
+ addr = NULL;
- for (list = driver_list; list; list = list->next) {
- struct connman_timeserver_driver *driver = list->data;
- char *new_server;
+ ret = getaddrinfo(server, NULL, &hints, &addr);
+ freeaddrinfo(addr);
- if (driver->append == NULL)
- continue;
+ /* if its a IP , directly query it. */
+ if (ret == 0) {
+ DBG("Using timeservers %s", server);
- new_server = g_strdup(server);
- if (new_server == NULL)
- return -ENOMEM;
+ __connman_ntp_start(server);
- if (driver->append(server) == 0) {
- g_hash_table_insert(server_hash, new_server, driver);
- return 0;
- } else {
- g_free(new_server);
- }
+ g_free(server);
+ return;
}
- return -ENOENT;
-}
+ DBG("Resolving server %s", server);
-/**
- * connman_timeserver_remove:
- * @server: server address
- *
- * Remover time server server address from current list
- */
-int connman_timeserver_remove(const char *server)
-{
- struct connman_timeserver_driver *driver;
-
- DBG("server %s", server);
+ resolv_id = g_resolv_lookup_hostname(resolv, server,
+ resolv_result, NULL);
- if (server == NULL)
- return -EINVAL;
+ g_free(server);
- driver = g_hash_table_lookup(server_hash, server);
- if (driver == NULL)
- return -EINVAL;
+ return;
+}
- g_hash_table_remove(server_hash, server);
+GSList *__connman_timeserver_add_list(GSList *server_list,
+ const char *timeserver)
+{
+ GSList *list = server_list;
- if (driver->remove == NULL)
- return -ENOENT;
+ if (timeserver == NULL)
+ return server_list;
- return driver->remove(server);
+ 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));
}
-/* Restart NTP procedure */
-static void connman_timeserver_restart()
+/*
+ * __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)
{
- if (resolv == NULL) {
- DBG("No online service.");
- return;
+ 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);
}
- /* Cancel current lookup */
- if(resolv_id > 0)
- g_resolv_cancel_lookup(resolv, resolv_id);
+ /* Then add Global Timeservers to the list */
+ timeservers = load_timeservers();
- /* Reload system timeserver list */
- if (system_timeservers != NULL) {
- g_strfreev(system_timeservers);
- system_timeservers = NULL;
- }
+ for (i = 0; timeservers != NULL && timeservers[i] != NULL; i++)
+ list = __connman_timeserver_add_list(list, timeservers[i]);
- system_timeservers = load_timeservers();
+ g_strfreev(timeservers);
- if (system_timeservers == NULL)
- return;
-
- __connman_ntp_stop();
+ fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
- count = 0;
+ /* 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]);
- __connman_timeserver_sync_next();
+ return g_slist_reverse(list);
}
-static void resolv_result(GResolvResultStatus status, char **results, gpointer user_data)
+/*
+ * 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)
{
- int i;
+ struct connman_service *service;
- DBG("status %d", status);
+ if (default_service != NULL)
+ service = default_service;
+ else
+ service = __connman_service_get_default();
- __sync_fetch_and_add(&count, 1);
+ if (service == NULL)
+ return -EINVAL;
- if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
- if (results != NULL) {
- for (i = 0; results[i]; i++)
- DBG("result: %s", results[i]);
+ if (resolv == NULL)
+ return 0;
+ /*
+ * Before we start creating the new timeserver list we must stop
+ * any ongoing ntp query and server resolution.
+ */
- __connman_ntp_start(results[0]);
+ __connman_ntp_stop();
- return;
- }
- }
+ if (resolv_id > 0)
+ g_resolv_cancel_lookup(resolv, resolv_id);
- __connman_timeserver_sync_next();
-}
+ g_slist_free_full(ts_list, g_free);
-void __connman_timeserver_sync_next()
-{
- if (system_timeservers == NULL ||
- system_timeservers[count] == NULL)
- return;
+ ts_list = __connman_timeserver_get_all(service);
+
+ __connman_service_timeserver_changed(service, ts_list);
+
+ if (ts_list == NULL) {
+ DBG("No timeservers set.");
+ return 0;
+ }
- DBG("Trying timeserver %s", system_timeservers[count]);
+ __connman_timeserver_sync_next();
- if (resolv)
- resolv_id = g_resolv_lookup_hostname(resolv,
- system_timeservers[count], resolv_result,
- NULL);
+ return 0;
}
-int __connman_timeserver_sync(struct connman_service *service)
+static int timeserver_start(struct connman_service *service)
{
- char **nameservers = NULL;
+ char **nameservers;
int i;
DBG("service %p", service);
if (nameservers == NULL)
return -EINVAL;
+ /* Stop an already ongoing resolution, if there is one */
+ if (resolv != NULL && resolv_id > 0)
+ g_resolv_cancel_lookup(resolv, resolv_id);
+
+ /* get rid of the old resolver */
+ if (resolv != NULL) {
+ g_resolv_unref(resolv);
+ resolv = NULL;
+ }
+
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);
- count = 0;
-
- system_timeservers = load_timeservers();
+ g_strfreev(nameservers);
- if (system_timeservers == NULL || system_timeservers[count] == NULL) {
- DBG("No timeservers set.");
- return 0;
- }
-
- DBG("Trying server %s", system_timeservers[count]);
-
- resolv_id = g_resolv_lookup_hostname(resolv, system_timeservers[count],
- resolv_result, NULL);
- return 0;
+ return __connman_timeserver_sync(service);
}
-void __connman_timeserver_stop()
+static void timeserver_stop()
{
DBG(" ");
resolv = NULL;
}
- if (system_timeservers != NULL) {
- g_strfreev(system_timeservers);
- system_timeservers = NULL;
- }
+ g_slist_free_full(ts_list, g_free);
- count = 0;
+ ts_list = NULL;
__connman_ntp_stop();
}
-int __connman_timeserver_system_append(const char *server)
+int __connman_timeserver_system_set(char **servers)
{
- int len;
- char **servers = NULL;
-
- if (server == NULL) {
- save_timeservers(servers);
- goto restart;
- }
-
- DBG("server %s", server);
-
- servers = load_timeservers();
-
- if (servers != NULL) {
- int i;
-
- for (i = 0; servers[i] != NULL; i++)
- if (g_strcmp0(servers[i], server) == 0) {
- g_strfreev(servers);
- return -EEXIST;
- }
-
- len = g_strv_length(servers);
- servers = g_try_renew(char *, servers, len + 2);
- } else {
- len = 0;
- servers = g_try_new0(char *, len + 2);
- }
-
- if (servers == NULL)
- return -ENOMEM;
-
- servers[len] = g_strdup(server);
- servers[len + 1] = NULL;
-
save_timeservers(servers);
- g_strfreev(servers);
-restart:
- connman_timeserver_restart();
+ __connman_timeserver_sync(NULL);
return 0;
}
-int __connman_timeserver_system_remove(const char *server)
+char **__connman_timeserver_system_get()
{
char **servers;
- char **temp;
- int len, i, j;
-
- if (server == NULL)
- return -EINVAL;
-
- DBG("server %s", server);
servers = load_timeservers();
-
- if (servers == NULL)
- return 0;
-
- len = g_strv_length(servers);
- if (len == 1) {
- if (g_strcmp0(servers[0], server) != 0) {
- g_strfreev(servers);
- return 0;
- }
-
- g_strfreev(servers);
- servers = NULL;
- save_timeservers(servers);
- return 0;
- }
-
- temp = g_try_new0(char *, len - 1);
- if (temp == NULL) {
- g_strfreev(servers);
- return -ENOMEM;
- }
-
- for (i = 0, j = 0; i < len; i++) {
- if (g_strcmp0(servers[i], server) != 0) {
- temp[j] = g_strdup(servers[i]);
- j++;
- }
- }
- temp[len - 1] = NULL;
-
- g_strfreev(servers);
- servers = g_strdupv(temp);
- g_strfreev(temp);
-
- save_timeservers(servers);
- g_strfreev(servers);
-
- connman_timeserver_restart();
-
- return 0;
+ return servers;
}
-char **__connman_timeserver_system_get()
+static void default_changed(struct connman_service *default_service)
{
- char **servers;
-
- servers = load_timeservers();
- return servers;
+ if (default_service != NULL)
+ 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("");
- server_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
- g_free, NULL);
+ connman_notifier_register(×erver_notifier);
return 0;
}
{
DBG("");
- g_hash_table_destroy(server_hash);
+ connman_notifier_unregister(×erver_notifier);
}