#include <config.h>
#endif
+#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
old_umask = umask(022);
- fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
+ fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
err = -errno;
{
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;
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);
-
+ 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)
int connman_resolver_append(const char *interface, const char *domain,
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
+ * connman_resolver_flush:
*
- * Append public resolver server address to current list
+ * Flush pending resolver requests
*/
-int connman_resolver_append_public_server(const char *server)
+void connman_resolver_flush(void)
{
- DBG("server %s", server);
+ if (dnsproxy_enabled == TRUE)
+ __connman_dnsproxy_flush();
- return append_resolver(NULL, NULL, server, 0, RESOLVER_FLAG_PUBLIC);
+ return;
}
-/**
- * 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)
+int __connman_resolver_redo_servers(const char *interface)
{
- DBG("server %s", server);
+ GSList *list;
+
+ if (dnsproxy_enabled == FALSE)
+ return 0;
+
+ DBG("interface %s", interface);
+
+ if (interface == 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)
+ 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,
+ entry->server);
+
+ __connman_dnsproxy_append(entry->interface, entry->domain,
+ entry->server);
+ }
- return connman_resolver_remove(NULL, NULL, server);
+ return 0;
}
-/**
- * connman_resolver_flush:
- *
- * Flush pending resolver requests
- */
-void connman_resolver_flush(void)
+static void free_entry(gpointer data)
{
- if (dnsproxy_enabled == TRUE)
- __connman_dnsproxy_flush();
+ struct entry_data *entry = data;
+ g_free(entry->interface);
+ g_free(entry->domain);
+ g_free(entry->server);
+ g_free(entry);
+}
- return;
+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 i;
+ char **ns;
+
DBG("dnsproxy %d", dnsproxy);
if (dnsproxy == FALSE)
dnsproxy_enabled = TRUE;
+ 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 0;
}
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;
+ }
}