Don't add useless domain and search entries to resolv.conf
[connman] / src / resolver.c
index d77ebc2..24caf73 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2009  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 "connman.h"
 
 struct entry_data {
@@ -142,6 +149,9 @@ int connman_resolver_append(const char *interface, const char *domain,
 
        DBG("interface %s domain %s server %s", interface, domain, server);
 
+       if (server == NULL)
+               return -EINVAL;
+
        entry = g_try_new0(struct entry_data, 1);
        if (entry == NULL)
                return -ENOMEM;
@@ -181,7 +191,6 @@ int connman_resolver_remove_all(const char *interface)
 
        for (list = entry_list; list; list = list->next) {
                struct entry_data *entry = list->data;
-               struct connman_resolver *resolver;
 
                if (g_str_equal(entry->interface, interface) == FALSE)
                        continue;
@@ -210,7 +219,7 @@ static int selftest_remove(const char *interface, const char *domain,
        return 0;
 }
 
-static struct connman_resolver selftest = {
+static struct connman_resolver selftest_resolver = {
        .name     = "selftest",
        .priority = CONNMAN_RESOLVER_PRIORITY_HIGH + 42,
        .append   = selftest_append,
@@ -221,14 +230,70 @@ int __connman_resolver_selftest(void)
 {
        connman_resolver_append("wlan0", "lwn.net", "192.168.0.1");
 
-       connman_resolver_register(&selftest);
+       connman_resolver_register(&selftest_resolver);
 
        connman_resolver_append("eth0", "moblin.org", "192.168.42.1");
        connman_resolver_append("wlan0", "lwn.net", "192.168.0.2");
 
        connman_resolver_remove_all("wlan0");
 
-       connman_resolver_unregister(&selftest);
+       connman_resolver_unregister(&selftest_resolver);
+
+       return 0;
+}
+
+static int resolvfile_append(const char *interface, const char *domain,
+                                                       const char *server)
+{
+       char *cmd;
+       int fd, len, err;
+
+       DBG("interface %s server %s", interface, server);
+
+       fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
+                                       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+       if (fd < 0)
+               return -errno;
+
+       err = ftruncate(fd, 0);
+
+       cmd = g_strdup_printf("# Generated by Connection Manager\n"
+                                               "nameserver %s\n", server);
+
+       len = write(fd, cmd, strlen(cmd));
+
+       g_free(cmd);
+
+       close(fd);
 
        return 0;
 }
+
+static int resolvfile_remove(const char *interface, const char *domain,
+                                                       const char *server)
+{
+       DBG("interface %s server %s", interface, server);
+
+       return 0;
+}
+
+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);
+}