#include <config.h>
#endif
+#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#define RESOLVER_FLAG_PUBLIC (1 << 0)
struct entry_data {
- struct connman_resolver *resolver;
char *interface;
char *domain;
char *server;
};
static GSList *entry_list = NULL;
-static GSList *resolver_list = NULL;
+static connman_bool_t dnsproxy_enabled = FALSE;
-static void remove_entries(GSList *entries)
+struct resolvfile_entry {
+ char *interface;
+ char *domain;
+ char *server;
+};
+
+static GList *resolvfile_list = NULL;
+
+static void resolvfile_remove_entries(GList *entries)
{
- GSList *list;
+ GList *list;
for (list = entries; list; list = list->next) {
- struct entry_data *entry = list->data;
- struct connman_resolver *resolver = entry->resolver;
-
- entry_list = g_slist_remove(entry_list, entry);
+ struct resolvfile_entry *entry = list->data;
- if (resolver && resolver->remove)
- resolver->remove(entry->interface, entry->domain,
- entry->server);
+ resolvfile_list = g_list_remove(resolvfile_list, entry);
- if (entry->timeout)
- g_source_remove(entry->timeout);
g_free(entry->server);
g_free(entry->domain);
g_free(entry->interface);
g_free(entry);
}
- g_slist_free(entries);
+ g_list_free(entries);
}
-static gint compare_priority(gconstpointer a, gconstpointer b)
+static int resolvfile_export(void)
{
- const struct connman_resolver *resolver1 = a;
- const struct connman_resolver *resolver2 = b;
+ GList *list;
+ GString *content;
+ int fd, err;
+ unsigned int count;
+ mode_t old_umask;
- return resolver2->priority - resolver1->priority;
-}
+ content = g_string_new("# Generated by Connection Manager\n");
-/**
- * connman_resolver_register:
- * @resolver: resolver module
- *
- * Register a new resolver module
- *
- * Returns: %0 on success
- */
-int connman_resolver_register(struct connman_resolver *resolver)
-{
- GSList *list;
+ /*
+ * Domains and nameservers are added in reverse so that the most
+ * recently appended entry is the primary one. No more than
+ * MAXDNSRCH/MAXNS entries are used.
+ */
- DBG("resolver %p name %s", resolver, resolver->name);
+ for (count = 0, list = g_list_last(resolvfile_list);
+ list && (count < MAXDNSRCH);
+ list = g_list_previous(list)) {
+ struct resolvfile_entry *entry = list->data;
- resolver_list = g_slist_insert_sorted(resolver_list, resolver,
- compare_priority);
+ if (!entry->domain)
+ continue;
- if (resolver->append == NULL)
- return 0;
+ if (count == 0)
+ g_string_append_printf(content, "search ");
- for (list = entry_list; list; list = list->next) {
- struct entry_data *entry = list->data;
+ g_string_append_printf(content, "%s ", entry->domain);
+ count++;
+ }
+
+ if (count)
+ g_string_append_printf(content, "\n");
- if (entry->resolver)
+ for (count = 0, list = g_list_last(resolvfile_list);
+ list && (count < MAXNS);
+ list = g_list_previous(list)) {
+ struct resolvfile_entry *entry = list->data;
+
+ if (!entry->server)
continue;
- if (resolver->append(entry->interface, entry->domain,
- entry->server) == 0)
- entry->resolver = resolver;
+ g_string_append_printf(content, "nameserver %s\n",
+ entry->server);
+ count++;
}
- return 0;
+ old_umask = umask(022);
+
+ fd = open("/etc/resolv.conf", 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) {
+ err = -errno;
+ goto failed;
+ }
+
+ err = 0;
+
+ if (write(fd, content->str, content->len) < 0)
+ err = -errno;
+
+failed:
+ close(fd);
+
+done:
+ g_string_free(content, TRUE);
+ umask(old_umask);
+
+ return err;
}
-/**
- * connman_resolver_unregister:
- * @resolver: resolver module
- *
- * Remove a previously registered resolver module
- */
-void connman_resolver_unregister(struct connman_resolver *resolver)
+int __connman_resolvfile_append(const char *interface, const char *domain,
+ const char *server)
{
- GSList *list, *matches = NULL;
+ struct resolvfile_entry *entry;
- DBG("resolver %p name %s", resolver, resolver->name);
+ DBG("interface %s server %s", interface, server);
- resolver_list = g_slist_remove(resolver_list, resolver);
+ if (interface == NULL)
+ return -ENOENT;
- for (list = entry_list; list; list = list->next) {
- struct entry_data *entry = list->data;
+ entry = g_try_new0(struct resolvfile_entry, 1);
+ if (entry == NULL)
+ return -ENOMEM;
+
+ entry->interface = g_strdup(interface);
+ entry->domain = g_strdup(domain);
+ entry->server = g_strdup(server);
+
+ resolvfile_list = g_list_append(resolvfile_list, entry);
- if (entry->resolver != resolver)
+ return resolvfile_export();
+}
+
+int __connman_resolvfile_remove(const char *interface, const char *domain,
+ const char *server)
+{
+ GList *list, *matches = NULL;
+
+ DBG("interface %s server %s", interface, 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)
continue;
- matches = g_slist_append(matches, entry);
+ if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
+ continue;
+
+ if (g_strcmp0(entry->server, server) != 0)
+ continue;
+
+ matches = g_list_append(matches, entry);
}
- remove_entries(matches);
+ resolvfile_remove_entries(matches);
+
+ return resolvfile_export();
+}
+
+static void remove_entries(GSList *entries)
+{
+ GSList *list;
+
+ for (list = entries; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ entry_list = g_slist_remove(entry_list, entry);
+
+ if (dnsproxy_enabled == TRUE) {
+ __connman_dnsproxy_remove(entry->interface, entry->domain,
+ entry->server);
+ } else {
+ __connman_resolvfile_remove(entry->interface, entry->domain,
+ entry->server);
+ }
+
+ if (entry->timeout)
+ g_source_remove(entry->timeout);
+ g_free(entry->server);
+ g_free(entry->domain);
+ g_free(entry->interface);
+ g_free(entry);
+ }
+
+ g_slist_free(entries);
}
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);
+ entry->interface, entry->domain, entry->server);
list = g_slist_append(NULL, entry);
+
+ index = connman_inet_ifindex(entry->interface);
+ if (index >= 0) {
+ struct connman_service *service;
+ service = __connman_service_lookup_from_index(index);
+ if (service != NULL)
+ __connman_service_nameserver_remove(service,
+ entry->server, TRUE);
+ }
+
remove_entries(list);
return FALSE;
}
static int append_resolver(const char *interface, const char *domain,
- const char *server, unsigned int lifetime,
- unsigned int flags)
+ const char *server, unsigned int lifetime,
+ unsigned int flags)
{
struct entry_data *entry;
- GSList *list;
DBG("interface %s domain %s server %s lifetime %d flags %d",
- interface, domain, server, lifetime, flags);
+ interface, domain, server, lifetime, flags);
if (server == NULL && domain == NULL)
return -EINVAL;
entry->domain = g_strdup(domain);
entry->server = g_strdup(server);
entry->flags = flags;
- if (lifetime)
+ if (lifetime) {
+ int index;
entry->timeout = g_timeout_add_seconds(lifetime,
- resolver_expire_cb,
- entry);
-
- entry_list = g_slist_append(entry_list, entry);
-
- for (list = resolver_list; list; list = list->next) {
- struct connman_resolver *resolver = list->data;
-
- if (resolver->append == NULL)
- continue;
-
- if (resolver->append(interface, domain, server) == 0) {
- entry->resolver = resolver;
- break;
+ resolver_expire_cb, entry);
+
+ /*
+ * 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);
}
}
+ entry_list = g_slist_append(entry_list, entry);
+
+ if (dnsproxy_enabled == TRUE)
+ __connman_dnsproxy_append(interface, domain, server);
+ else
+ __connman_resolvfile_append(interface, domain, server);
return 0;
}
* Append resolver server address to current list
*/
int connman_resolver_append(const char *interface, const char *domain,
- const char *server)
+ const char *server)
{
+ GSList *list, *matches = NULL;
+
DBG("interface %s domain %s server %s", interface, domain, server);
+ if (server == NULL && domain == NULL)
+ 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)
+ continue;
+
+ matches = g_slist_append(matches, entry);
+ }
+
+ if (matches != NULL)
+ remove_entries(matches);
+
return append_resolver(interface, domain, server, 0, 0);
}
* Append resolver server address to current list
*/
int connman_resolver_append_lifetime(const char *interface, const char *domain,
- const char *server, unsigned int lifetime)
+ const char *server, unsigned int lifetime)
{
GSList *list;
DBG("interface %s domain %s server %s lifetime %d",
- interface, domain, server, lifetime);
+ interface, domain, server, lifetime);
if (server == NULL)
return -EINVAL;
struct entry_data *entry = list->data;
if (!entry->timeout ||
- g_strcmp0(entry->interface, interface) ||
- g_strcmp0(entry->domain, domain) ||
- g_strcmp0(entry->server, server))
+ g_strcmp0(entry->interface, interface) ||
+ g_strcmp0(entry->domain, domain) ||
+ g_strcmp0(entry->server, server))
continue;
g_source_remove(entry->timeout);
+
+ if (lifetime == 0) {
+ resolver_expire_cb(entry);
+ return 0;
+ }
+
entry->timeout = g_timeout_add_seconds(lifetime,
- resolver_expire_cb,
- entry);
+ resolver_expire_cb, entry);
return 0;
}
+
return append_resolver(interface, domain, server, lifetime, 0);
}
}
/**
- * connman_resolver_append_public_server:
- * @server: server address
- *
- * Append public resolver server address to current list
- */
-int connman_resolver_append_public_server(const char *server)
-{
- DBG("server %s", server);
-
- return append_resolver(NULL, NULL, server, 0, RESOLVER_FLAG_PUBLIC);
-}
-
-/**
- * connman_resolver_remove_public_server:
- * @server: server address
- *
- * Remove public resolver server address to current list
- */
-int connman_resolver_remove_public_server(const char *server)
-{
- DBG("server %s", server);
-
- return connman_resolver_remove(NULL, NULL, server);
-}
-
-/**
* connman_resolver_flush:
*
* Flush pending resolver requests
*/
void connman_resolver_flush(void)
{
- GSList *list;
-
- for (list = resolver_list; list; list = list->next) {
- struct connman_resolver *resolver = list->data;
-
- if (resolver->flush == NULL)
- continue;
-
- resolver->flush();
- }
+ if (dnsproxy_enabled == TRUE)
+ __connman_dnsproxy_flush();
return;
}
-struct resolvfile_entry {
- char *interface;
- char *domain;
- char *server;
-};
-
-static GList *resolvfile_list = NULL;
-
-static void resolvfile_remove_entries(GList *entries)
-{
- GList *list;
-
- for (list = entries; list; list = list->next) {
- struct resolvfile_entry *entry = list->data;
-
- resolvfile_list = g_list_remove(resolvfile_list, entry);
-
- g_free(entry->server);
- g_free(entry->domain);
- g_free(entry->interface);
- g_free(entry);
- }
-
- g_list_free(entries);
-}
-
-static int resolvfile_export(void)
+int __connman_resolver_redo_servers(const char *interface)
{
- GList *list;
- GString *content;
- int fd, err;
- unsigned int count;
- mode_t old_umask;
-
- content = g_string_new("# Generated by Connection Manager\n");
-
- /*
- * Domains and nameservers are added in reverse so that the most
- * recently appended entry is the primary one. No more than
- * MAXDNSRCH/MAXNS entries are used.
- */
-
- for (count = 0, list = g_list_last(resolvfile_list);
- list && (count < MAXDNSRCH);
- list = g_list_previous(list)) {
- struct resolvfile_entry *entry = list->data;
-
- if (!entry->domain)
- continue;
+ GSList *list;
- if (count == 0)
- g_string_append_printf(content, "search ");
+ if (dnsproxy_enabled == FALSE)
+ return 0;
- g_string_append_printf(content, "%s ", entry->domain);
- count++;
- }
+ DBG("interface %s", interface);
- if (count)
- g_string_append_printf(content, "\n");
+ if (interface == NULL)
+ return -EINVAL;
- for (count = 0, list = g_list_last(resolvfile_list);
- list && (count < MAXNS);
- list = g_list_previous(list)) {
- struct resolvfile_entry *entry = list->data;
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
- if (!entry->server)
+ if (entry->timeout == 0 ||
+ g_strcmp0(entry->interface, interface) != 0)
continue;
- g_string_append_printf(content, "nameserver %s\n",
- entry->server);
- count++;
- }
-
- old_umask = umask(022);
+ /*
+ * 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,
+ entry->server);
- fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
- S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (fd < 0) {
- err = -errno;
- goto done;
+ __connman_dnsproxy_append(entry->interface, entry->domain,
+ entry->server);
}
- if (ftruncate(fd, 0) < 0) {
- err = -errno;
- goto failed;
- }
-
- err = 0;
-
- if (write(fd, content->str, content->len) < 0)
- err = -errno;
-
-failed:
- close(fd);
-
-done:
- g_string_free(content, TRUE);
- umask(old_umask);
-
- return err;
+ return 0;
}
-static int resolvfile_append(const char *interface, const char *domain,
- const char *server)
+static void free_entry(gpointer data)
{
- struct resolvfile_entry *entry;
-
- DBG("interface %s server %s", interface, server);
-
- if (interface == NULL)
- return -ENOENT;
-
- entry = g_try_new0(struct resolvfile_entry, 1);
- if (entry == NULL)
- return -ENOMEM;
-
- entry->interface = g_strdup(interface);
- entry->domain = g_strdup(domain);
- entry->server = g_strdup(server);
-
- resolvfile_list = g_list_append(resolvfile_list, entry);
-
- return resolvfile_export();
+ struct entry_data *entry = data;
+ g_free(entry->interface);
+ g_free(entry->domain);
+ g_free(entry->server);
+ g_free(entry);
}
-static int resolvfile_remove(const char *interface, const char *domain,
- const char *server)
+static void free_resolvfile(gpointer data)
{
- GList *list, *matches = NULL;
-
- DBG("interface %s server %s", interface, server);
-
- for (list = resolvfile_list; list; list = g_list_next(list)) {
- struct resolvfile_entry *entry = list->data;
+ struct resolvfile_entry *entry = data;
+ g_free(entry->interface);
+ g_free(entry->domain);
+ g_free(entry->server);
+ g_free(entry);
+}
- if (interface != NULL &&
- g_strcmp0(entry->interface, interface) != 0)
- continue;
+int __connman_resolver_init(connman_bool_t dnsproxy)
+{
+ int i;
+ char **ns;
- if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
- continue;
+ DBG("dnsproxy %d", dnsproxy);
- if (g_strcmp0(entry->server, server) != 0)
- continue;
+ if (dnsproxy == FALSE)
+ return 0;
- matches = g_list_append(matches, entry);
+ if (__connman_dnsproxy_init() < 0) {
+ /* Fall back to resolv.conf */
+ return 0;
}
- resolvfile_remove_entries(matches);
-
- return resolvfile_export();
-}
-
-static struct connman_resolver resolvfile_resolver = {
- .name = "resolvfile",
- .priority = CONNMAN_RESOLVER_PRIORITY_LOW,
- .append = resolvfile_append,
- .remove = resolvfile_remove,
-};
+ dnsproxy_enabled = TRUE;
-int __connman_resolver_init(void)
-{
- DBG("");
+ ns = connman_setting_get_string_list("FallbackNameservers");
+ for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
+ DBG("server %s", ns[i]);
+ append_resolver(NULL, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
+ }
- return connman_resolver_register(&resolvfile_resolver);
+ return 0;
}
void __connman_resolver_cleanup(void)
{
DBG("");
- connman_resolver_unregister(&resolvfile_resolver);
+ if (dnsproxy_enabled == TRUE)
+ __connman_dnsproxy_cleanup();
+ else {
+ GList *list;
+ GSList *slist;
+
+ for (list = resolvfile_list; list; list = g_list_next(list))
+ free_resolvfile(list->data);
+ g_list_free(resolvfile_list);
+ resolvfile_list = NULL;
+
+ for (slist = entry_list; slist; slist = g_slist_next(slist))
+ free_entry(slist->data);
+ g_slist_free(entry_list);
+ entry_list = NULL;
+ }
}