*
* Connection Manager
*
- * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2010 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
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <resolv.h>
+
#include "connman.h"
+#define RESOLVER_FLAG_PUBLIC (1 << 0)
+
struct entry_data {
struct connman_resolver *resolver;
char *interface;
char *domain;
char *server;
+ unsigned int flags;
};
static GSList *entry_list = NULL;
entry_list = g_slist_remove(entry_list, entry);
- if (resolver->remove)
+ if (resolver && resolver->remove)
resolver->remove(entry->interface, entry->domain,
entry->server);
remove_entries(matches);
}
-/**
- * connman_resolver_append:
- * @interface: network interface
- * @domain: domain limitation
- * @server: server address
- *
- * Append resolver server address to current list
- */
-int connman_resolver_append(const char *interface, const char *domain,
- const char *server)
+static int append_resolver(const char *interface, const char *domain,
+ const char *server, unsigned int flags)
{
struct entry_data *entry;
GSList *list;
- DBG("interface %s domain %s server %s", interface, domain, server);
+ DBG("interface %s domain %s server %s flags %d",
+ interface, domain, server, flags);
+
+ if (server == NULL)
+ return -EINVAL;
entry = g_try_new0(struct entry_data, 1);
if (entry == NULL)
entry->interface = g_strdup(interface);
entry->domain = g_strdup(domain);
entry->server = g_strdup(server);
+ entry->flags = flags;
entry_list = g_slist_append(entry_list, entry);
}
/**
+ * connman_resolver_append:
+ * @interface: network interface
+ * @domain: domain limitation
+ * @server: server address
+ *
+ * Append resolver server address to current list
+ */
+int connman_resolver_append(const char *interface, const char *domain,
+ const char *server)
+{
+ DBG("interface %s domain %s server %s", interface, domain, server);
+
+ return append_resolver(interface, domain, server, 0);
+}
+
+/**
+ * connman_resolver_remove:
+ * @interface: network interface
+ * @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)
+{
+ GSList *list, *matches = NULL;
+
+ DBG("interface %s domain %s server %s", interface, domain, server);
+
+ if (server == NULL)
+ return -EINVAL;
+
+ for (list = entry_list; list; list = list->next) {
+ struct entry_data *entry = list->data;
+
+ if (interface != NULL &&
+ g_strcmp0(entry->interface, interface) != 0)
+ continue;
+
+ if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
+ continue;
+
+ if (g_strcmp0(entry->server, server) != 0)
+ continue;
+
+ matches = g_slist_append(matches, entry);
+ }
+
+ if (matches == NULL)
+ return -ENOENT;
+
+ remove_entries(matches);
+
+ return 0;
+}
+
+/**
* connman_resolver_remove_all:
* @interface: network interface
*
DBG("interface %s", interface);
+ if (interface == NULL)
+ return -EINVAL;
+
for (list = entry_list; list; list = list->next) {
struct entry_data *entry = list->data;
- if (g_str_equal(entry->interface, interface) == FALSE)
+ if (g_strcmp0(entry->interface, interface) != 0)
continue;
matches = g_slist_append(matches, entry);
}
+ if (matches == NULL)
+ return -ENOENT;
+
remove_entries(matches);
return 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, 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);
+}
+
static int selftest_append(const char *interface, const char *domain,
const char *server)
{
connman_resolver_append("eth0", "moblin.org", "192.168.42.1");
connman_resolver_append("wlan0", "lwn.net", "192.168.0.2");
+ connman_resolver_append_public_server("8.8.8.8");
+
+ connman_resolver_remove_public_server("8.8.8.8");
+
connman_resolver_remove_all("wlan0");
connman_resolver_unregister(&selftest_resolver);
return 0;
}
+
+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)
+{
+ GList *list;
+ GString *content;
+ int fd, err;
+ unsigned int count;
+ mode_t old_umask;
+
+ content = g_string_new("# Generated by Connection Manager\n"
+ "options edns0\n");
+
+ /* Nameservers are added in reverse so that the most recently appended
+ * entry is the primary nameserver. No more than MAXNS nameservers are
+ * used.
+ */
+ for (count = 0, list = g_list_last(resolvfile_list);
+ list && (count < MAXNS);
+ list = g_list_previous(list)) {
+ struct resolvfile_entry *entry = list->data;
+ g_string_append_printf(content, "nameserver %s\n",
+ entry->server);
+ count++;
+ }
+
+ old_umask = umask(022);
+
+ fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
+ 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;
+}
+
+static int resolvfile_append(const char *interface, const char *domain,
+ const char *server)
+{
+ 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();
+}
+
+static int 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;
+
+ if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
+ continue;
+
+ if (g_strcmp0(entry->server, server) != 0)
+ continue;
+
+ matches = g_list_append(matches, entry);
+ }
+
+ 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,
+};
+
+int __connman_resolver_init(void)
+{
+ DBG("");
+
+ return connman_resolver_register(&resolvfile_resolver);
+}
+
+void __connman_resolver_cleanup(void)
+{
+ DBG("");
+
+ connman_resolver_unregister(&resolvfile_resolver);
+}