*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2013 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 <config.h>
#endif
-#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <resolv.h>
+#include <netdb.h>
#include "connman.h"
+/*
+ * Just to avoid build failure due to missing STATEDIR
+ */
+#if defined TIZEN_EXT
+#ifdef STATEDIR
+#undef STATEDIR
+#endif
+#define STATEDIR "/etc"
+#endif
+
+#define RESOLV_CONF_STATEDIR STATEDIR"/resolv.conf"
+#define RESOLV_CONF_ETC "/etc/resolv.conf"
+
#define RESOLVER_FLAG_PUBLIC (1 << 0)
+/*
+ * Threshold for RDNSS lifetime. Will be used to trigger RS
+ * before RDNSS entries actually expire
+ */
+#define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
+
struct entry_data {
- char *interface;
+ int index;
char *domain;
char *server;
+ int family;
unsigned int flags;
+ unsigned int lifetime;
guint timeout;
};
static GSList *entry_list = NULL;
-static connman_bool_t dnsproxy_enabled = FALSE;
+static bool dnsproxy_enabled = false;
struct resolvfile_entry {
- char *interface;
+ int index;
char *domain;
char *server;
};
g_free(entry->server);
g_free(entry->domain);
- g_free(entry->interface);
g_free(entry);
}
g_list_free(entries);
}
-static int resolvfile_export(void)
+static bool already_exported(GList *export_list, const char *str)
{
GList *list;
+
+ for (list = export_list; list; list = g_list_next(list)) {
+ const char *str0 = list->data;
+ if (g_strcmp0(str0, str) == 0)
+ return true;
+ }
+
+ return false;
+}
+
+static int resolvfile_export(void)
+{
+ GList *list, *export_list;
GString *content;
int fd, err;
unsigned int count;
* MAXDNSRCH/MAXNS entries are used.
*/
- for (count = 0, list = g_list_last(resolvfile_list);
+ export_list = NULL;
+ for (count = 0, list = g_list_first(resolvfile_list);
list && (count < MAXDNSRCH);
- list = g_list_previous(list)) {
+ list = g_list_next(list)) {
struct resolvfile_entry *entry = list->data;
if (!entry->domain)
continue;
+ if (already_exported(export_list, entry->domain))
+ continue;
+
if (count == 0)
g_string_append_printf(content, "search ");
g_string_append_printf(content, "%s ", entry->domain);
+
+ export_list = g_list_append(export_list, entry->domain);
+
count++;
}
+ g_list_free(export_list);
+
if (count)
g_string_append_printf(content, "\n");
- for (count = 0, list = g_list_last(resolvfile_list);
+ export_list = NULL;
+ for (count = 0, list = g_list_first(resolvfile_list);
list && (count < MAXNS);
- list = g_list_previous(list)) {
+ list = g_list_next(list)) {
struct resolvfile_entry *entry = list->data;
if (!entry->server)
continue;
- g_string_append_printf(content, "nameserver %s\n",
- entry->server);
+ if (already_exported(export_list, entry->server))
+ continue;
+
+ g_string_append_printf(content, "nameserver %s\n", entry->server);
+
+ export_list = g_list_append(export_list, entry->server);
+
count++;
}
+ g_list_free(export_list);
old_umask = umask(022);
- fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
+ fd = open(RESOLV_CONF_STATEDIR, O_RDWR | O_CREAT | O_CLOEXEC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
- err = -errno;
- goto done;
+ connman_warn_once("Cannot create "RESOLV_CONF_STATEDIR" "
+ "falling back to "RESOLV_CONF_ETC);
+
+ fd = open(RESOLV_CONF_ETC, O_RDWR | O_CREAT | O_CLOEXEC,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+ if (fd < 0) {
+ err = -errno;
+ goto done;
+ }
}
if (ftruncate(fd, 0) < 0) {
return err;
}
-int __connman_resolvfile_append(const char *interface, const char *domain,
+int __connman_resolvfile_append(int index, const char *domain,
const char *server)
{
struct resolvfile_entry *entry;
- DBG("interface %s server %s", interface, server);
+ DBG("index %d domain %s server %s", index, domain, server);
- if (interface == NULL)
+ if (index < 0)
return -ENOENT;
entry = g_try_new0(struct resolvfile_entry, 1);
- if (entry == NULL)
+ if (!entry)
return -ENOMEM;
- entry->interface = g_strdup(interface);
+ entry->index = index;
entry->domain = g_strdup(domain);
entry->server = g_strdup(server);
return resolvfile_export();
}
-int __connman_resolvfile_remove(const char *interface, const char *domain,
+int __connman_resolvfile_remove(int index, const char *domain,
const char *server)
{
GList *list, *matches = NULL;
- DBG("interface %s server %s", interface, server);
+ DBG("index %d domain %s server %s", index, domain, server);
for (list = resolvfile_list; list; list = g_list_next(list)) {
struct resolvfile_entry *entry = list->data;
- if (interface != NULL &&
- g_strcmp0(entry->interface, interface) != 0)
+ if (index >= 0 && entry->index != index)
continue;
- if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
+ if (domain && g_strcmp0(entry->domain, domain) != 0)
continue;
if (g_strcmp0(entry->server, server) != 0)
return resolvfile_export();
}
+void __connman_resolver_append_fallback_nameservers(void)
+{
+ GSList *list;
+
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ if (entry->index >= 0 && entry->server)
+ return;
+ }
+
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ if (entry->index != -1 || !entry->server)
+ continue;
+
+ DBG("index %d server %s", entry->index, entry->server);
+
+ if (dnsproxy_enabled) {
+ __connman_dnsproxy_append(entry->index, entry->domain,
+ entry->server);
+ } else {
+ __connman_resolvfile_append(entry->index,
+ entry->domain, entry->server);
+ }
+ }
+}
+
+static void remove_fallback_nameservers(void)
+{
+ GSList *list;
+
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ if (entry->index >= 0 || !entry->server)
+ continue;
+
+ DBG("index %d server %s", entry->index, entry->server);
+
+ if (dnsproxy_enabled) {
+ __connman_dnsproxy_remove(entry->index, entry->domain,
+ entry->server);
+ } else {
+ __connman_resolvfile_remove(entry->index,
+ entry->domain, entry->server);
+ }
+ }
+}
+
static void remove_entries(GSList *entries)
{
GSList *list;
entry_list = g_slist_remove(entry_list, entry);
- if (dnsproxy_enabled == TRUE) {
- __connman_dnsproxy_remove(entry->interface, entry->domain,
+ if (dnsproxy_enabled) {
+ __connman_dnsproxy_remove(entry->index, entry->domain,
entry->server);
} else {
- __connman_resolvfile_remove(entry->interface, entry->domain,
+ __connman_resolvfile_remove(entry->index, entry->domain,
entry->server);
}
g_source_remove(entry->timeout);
g_free(entry->server);
g_free(entry->domain);
- g_free(entry->interface);
g_free(entry);
}
g_slist_free(entries);
+
+ __connman_resolver_append_fallback_nameservers();
}
static gboolean resolver_expire_cb(gpointer user_data)
{
struct entry_data *entry = user_data;
GSList *list;
- int index;
- DBG("interface %s domain %s server %s",
- entry->interface, entry->domain, entry->server);
+ DBG("index %d domain %s server %s",
+ entry->index, entry->domain, entry->server);
- list = g_slist_append(NULL, entry);
+ list = g_slist_prepend(NULL, entry);
- index = connman_inet_ifindex(entry->interface);
- if (index >= 0) {
+ if (entry->index >= 0) {
struct connman_service *service;
- service = __connman_service_lookup_from_index(index);
- if (service != NULL)
+ service = __connman_service_lookup_from_index(entry->index);
+ if (service)
+#if defined TIZEN_EXT
__connman_service_nameserver_remove(service,
- entry->server, TRUE);
+ entry->server, true,
+ CONNMAN_IPCONFIG_TYPE_ALL);
+#else
+ __connman_service_nameserver_remove(service,
+ entry->server, true);
+#endif
}
remove_entries(list);
return FALSE;
}
-static int append_resolver(const char *interface, const char *domain,
+static gboolean resolver_refresh_cb(gpointer user_data)
+{
+ struct entry_data *entry = user_data;
+ unsigned int interval;
+ struct connman_service *service = NULL;
+
+ /* Round up what we have left from lifetime */
+ interval = entry->lifetime *
+ (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
+
+ DBG("RDNSS start index %d domain %s "
+ "server %s remaining lifetime %d",
+ entry->index, entry->domain,
+ entry->server, interval);
+
+ entry->timeout = g_timeout_add_seconds(interval,
+ resolver_expire_cb, entry);
+
+ if (entry->index >= 0) {
+ service = __connman_service_lookup_from_index(entry->index);
+ if (service) {
+ /*
+ * Send Router Solicitation to refresh RDNSS entries
+ * before their lifetime expires
+ */
+ __connman_network_refresh_rs_ipv6(
+ __connman_service_get_network(service),
+ entry->index);
+ }
+ }
+ return FALSE;
+}
+
+static int append_resolver(int index, const char *domain,
const char *server, unsigned int lifetime,
unsigned int flags)
{
struct entry_data *entry;
+ unsigned int interval;
- DBG("interface %s domain %s server %s lifetime %d flags %d",
- interface, domain, server, lifetime, flags);
+ DBG("index %d domain %s server %s lifetime %d flags %d",
+ index, domain, server, lifetime, flags);
- if (server == NULL && domain == NULL)
+ if (!server && !domain)
return -EINVAL;
+#ifdef TIZEN_EXT
+ if (g_strcmp0(server, "0.0.0.0") == 0)
+ return -EINVAL;
+#endif
+
entry = g_try_new0(struct entry_data, 1);
- if (entry == NULL)
+ if (!entry)
return -ENOMEM;
- entry->interface = g_strdup(interface);
+ entry->index = index;
entry->domain = g_strdup(domain);
entry->server = g_strdup(server);
entry->flags = flags;
+ entry->lifetime = lifetime;
+
+ if (server)
+ entry->family = connman_inet_check_ipaddress(server);
+
if (lifetime) {
- int index;
- entry->timeout = g_timeout_add_seconds(lifetime,
- resolver_expire_cb, entry);
+ interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
- /*
- * We update the service only for those nameservers
- * that are automagically added via netlink (lifetime > 0)
- */
- index = connman_inet_ifindex(interface);
- if (index >= 0) {
- struct connman_service *service;
- service = __connman_service_lookup_from_index(index);
- if (service != NULL)
- __connman_service_nameserver_append(service,
- server, TRUE);
- }
+ DBG("RDNSS start index %d domain %s "
+ "server %s lifetime threshold %d",
+ index, domain, server, interval);
+
+ entry->timeout = g_timeout_add_seconds(interval,
+ resolver_refresh_cb, entry);
}
+
+ if (entry->index >= 0 && entry->server)
+ remove_fallback_nameservers();
+
entry_list = g_slist_append(entry_list, entry);
- if (dnsproxy_enabled == TRUE)
- __connman_dnsproxy_append(interface, domain, server);
+ if (dnsproxy_enabled)
+ __connman_dnsproxy_append(entry->index, domain, server);
else
- __connman_resolvfile_append(interface, domain, server);
+ __connman_resolvfile_append(entry->index, domain, server);
+
+ /*
+ * We update the service only for those nameservers
+ * that are automagically added via netlink (lifetime > 0)
+ */
+ if (server && entry->index >= 0 && lifetime) {
+ struct connman_service *service;
+ service = __connman_service_lookup_from_index(entry->index);
+ if (service)
+#if defined TIZEN_EXT
+ __connman_service_nameserver_append(service,
+ server, true,
+ CONNMAN_IPCONFIG_TYPE_ALL);
+#else
+ __connman_service_nameserver_append(service,
+ server, true);
+#endif
+ }
return 0;
}
/**
* connman_resolver_append:
- * @interface: network interface
+ * @index: network interface index
* @domain: domain limitation
* @server: server address
*
* Append resolver server address to current list
*/
-int connman_resolver_append(const char *interface, const char *domain,
+int connman_resolver_append(int index, const char *domain,
const char *server)
{
- GSList *list, *matches = NULL;
+ GSList *list;
- DBG("interface %s domain %s server %s", interface, domain, server);
+ DBG("index %d domain %s server %s", index, domain, server);
- if (server == NULL && domain == NULL)
+ if (!server && !domain)
return -EINVAL;
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (entry->timeout > 0 ||
- g_strcmp0(entry->interface, interface) != 0 ||
- g_strcmp0(entry->domain, domain) != 0 ||
- g_strcmp0(entry->server, server) != 0)
+ if (entry->timeout > 0)
continue;
- matches = g_slist_append(matches, entry);
- }
+ if (entry->index == index &&
+ g_strcmp0(entry->domain, domain) == 0 &&
+ g_strcmp0(entry->server, server) == 0) {
+ if (dnsproxy_enabled)
+ __connman_dnsproxy_append(entry->index, domain,
+ server);
- if (matches != NULL)
- remove_entries(matches);
+ return -EEXIST;
+ }
+ }
- return append_resolver(interface, domain, server, 0, 0);
+ return append_resolver(index, domain, server, 0, 0);
}
/**
* connman_resolver_append_lifetime:
- * @interface: network interface
+ * @index: network interface index
* @domain: domain limitation
* @server: server address
* @timeout: server lifetime in seconds
*
* Append resolver server address to current list
*/
-int connman_resolver_append_lifetime(const char *interface, const char *domain,
+int connman_resolver_append_lifetime(int index, const char *domain,
const char *server, unsigned int lifetime)
{
GSList *list;
+ unsigned int interval;
- DBG("interface %s domain %s server %s lifetime %d",
- interface, domain, server, lifetime);
+ DBG("index %d domain %s server %s lifetime %d",
+ index, domain, server, lifetime);
- if (server == NULL)
+ if (!server && !domain)
return -EINVAL;
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (!entry->timeout ||
- g_strcmp0(entry->interface, interface) ||
- g_strcmp0(entry->domain, domain) ||
- g_strcmp0(entry->server, server))
+ if (entry->timeout == 0 ||
+ entry->index != index ||
+ g_strcmp0(entry->domain, domain) != 0 ||
+ g_strcmp0(entry->server, server) != 0)
continue;
g_source_remove(entry->timeout);
return 0;
}
- entry->timeout = g_timeout_add_seconds(lifetime,
- resolver_expire_cb, entry);
+ interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
+
+ DBG("RDNSS start index %d domain %s "
+ "server %s lifetime threshold %d",
+ index, domain, server, interval);
+
+ entry->timeout = g_timeout_add_seconds(interval,
+ resolver_refresh_cb, entry);
return 0;
}
- return append_resolver(interface, domain, server, lifetime, 0);
+ return append_resolver(index, domain, server, lifetime, 0);
}
/**
* connman_resolver_remove:
- * @interface: network interface
+ * @index: network interface index
* @domain: domain limitation
* @server: server address
*
* Remover resolver server address from current list
*/
-int connman_resolver_remove(const char *interface, const char *domain,
- const char *server)
+int connman_resolver_remove(int index, const char *domain, const char *server)
{
GSList *list, *matches = NULL;
- DBG("interface %s domain %s server %s", interface, domain, server);
-
- if (server == NULL)
- return -EINVAL;
+ DBG("index %d domain %s server %s", index, domain, server);
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (interface != NULL &&
- g_strcmp0(entry->interface, interface) != 0)
+ if (entry->index != index)
continue;
- if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
+ if (g_strcmp0(entry->domain, domain) != 0)
continue;
if (g_strcmp0(entry->server, server) != 0)
continue;
- matches = g_slist_append(matches, entry);
+ matches = g_slist_prepend(matches, entry);
+ break;
}
- if (matches == NULL)
+ if (!matches)
return -ENOENT;
remove_entries(matches);
/**
* connman_resolver_remove_all:
- * @interface: network interface
+ * @index: network interface index
*
- * Remove all resolver server address for the specified interface
+ * Remove all resolver server address for the specified interface index
*/
-int connman_resolver_remove_all(const char *interface)
+int connman_resolver_remove_all(int index)
{
GSList *list, *matches = NULL;
- DBG("interface %s", interface);
+ DBG("index %d", index);
- if (interface == NULL)
+ if (index < 0)
return -EINVAL;
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (g_strcmp0(entry->interface, interface) != 0)
+ if (entry->index != index)
continue;
- matches = g_slist_append(matches, entry);
+ matches = g_slist_prepend(matches, entry);
}
- if (matches == NULL)
+ if (!matches)
return -ENOENT;
remove_entries(matches);
return 0;
}
-/**
- * connman_resolver_flush:
- *
- * Flush pending resolver requests
- */
-void connman_resolver_flush(void)
-{
- if (dnsproxy_enabled == TRUE)
- __connman_dnsproxy_flush();
-
- return;
-}
-
-int __connman_resolver_redo_servers(const char *interface)
+int __connman_resolver_redo_servers(int index)
{
GSList *list;
- if (dnsproxy_enabled == FALSE)
+ if (!dnsproxy_enabled)
return 0;
- DBG("interface %s", interface);
+ DBG("index %d", index);
- if (interface == NULL)
+ if (index < 0)
return -EINVAL;
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (entry->timeout == 0 ||
- g_strcmp0(entry->interface, interface) != 0)
+ if (entry->timeout == 0 || entry->index != index)
+ continue;
+
+ /*
+ * This function must only check IPv6 server addresses so
+ * do not remove IPv4 name servers unnecessarily.
+ */
+ if (entry->family != AF_INET6)
continue;
/*
* We remove the server, and then re-create so that it will
* use proper source addresses when sending DNS queries.
*/
- __connman_dnsproxy_remove(entry->interface, entry->domain,
+ __connman_dnsproxy_remove(entry->index, entry->domain,
entry->server);
- __connman_dnsproxy_append(entry->interface, entry->domain,
+ __connman_dnsproxy_append(entry->index, entry->domain,
entry->server);
}
+ /*
+ * We want to re-add all search domains back to search
+ * domain lists as they just got removed for RDNSS IPv6-servers
+ * (above).
+ * Removal of search domains is not necessary
+ * as there can be only one instance of each search domain
+ * in the each dns-servers search domain list.
+ */
+
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ if (entry->index != index)
+ continue;
+
+ if (entry->server)
+ continue;
+
+ __connman_dnsproxy_append(entry->index, entry->domain,
+ NULL);
+ }
+
return 0;
}
static void free_entry(gpointer data)
{
struct entry_data *entry = data;
- g_free(entry->interface);
g_free(entry->domain);
g_free(entry->server);
g_free(entry);
static void free_resolvfile(gpointer data)
{
struct resolvfile_entry *entry = data;
- g_free(entry->interface);
g_free(entry->domain);
g_free(entry->server);
g_free(entry);
}
-int __connman_resolver_init(connman_bool_t dnsproxy)
+int __connman_resolver_set_mdns(int index, bool enabled)
+{
+ if (!dnsproxy_enabled)
+ return -ENOTSUP;
+
+ return __connman_dnsproxy_set_mdns(index, enabled);
+}
+
+int __connman_resolver_init(gboolean dnsproxy)
{
int i;
char **ns;
DBG("dnsproxy %d", dnsproxy);
- if (dnsproxy == FALSE)
+ /* get autoip nameservers */
+ ns = __connman_inet_get_pnp_nameservers(NULL);
+ for (i = 0; ns && ns[i]; i += 1) {
+ DBG("pnp server %s", ns[i]);
+ append_resolver(i, NULL, ns[i], 86400, 0);
+ }
+ g_strfreev(ns);
+
+ if (!dnsproxy)
return 0;
if (__connman_dnsproxy_init() < 0) {
return 0;
}
- dnsproxy_enabled = TRUE;
+ dnsproxy_enabled = true;
ns = connman_setting_get_string_list("FallbackNameservers");
- for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
+ for (i = 0; ns && ns[i]; i += 1) {
DBG("server %s", ns[i]);
- append_resolver(NULL, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
+ append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
}
return 0;
{
DBG("");
- if (dnsproxy_enabled == TRUE)
+ if (dnsproxy_enabled)
__connman_dnsproxy_cleanup();
else {
GList *list;