Emit StateChangedProperties post emitting service state's PropertyChanged
[platform/upstream/connman.git] / src / resolver.c
old mode 100644 (file)
new mode 100755 (executable)
index b965778..7ec2150
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2012  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
@@ -23,7 +23,6 @@
 #include <config.h>
 #endif
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <errno.h>
 #include <fcntl.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)
 
 /*
@@ -54,7 +66,7 @@ struct entry_data {
 };
 
 static GSList *entry_list = NULL;
-static connman_bool_t dnsproxy_enabled = FALSE;
+static bool dnsproxy_enabled = false;
 
 struct resolvfile_entry {
        int index;
@@ -97,9 +109,9 @@ static int resolvfile_export(void)
         * MAXDNSRCH/MAXNS entries are used.
         */
 
-       for (count = 0, list = g_list_last(resolvfile_list);
+       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)
@@ -115,9 +127,9 @@ static int resolvfile_export(void)
        if (count)
                g_string_append_printf(content, "\n");
 
-       for (count = 0, list = g_list_last(resolvfile_list);
+       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)
@@ -130,11 +142,19 @@ static int resolvfile_export(void)
 
        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) {
@@ -168,7 +188,7 @@ int __connman_resolvfile_append(int index, const char *domain,
                return -ENOENT;
 
        entry = g_try_new0(struct resolvfile_entry, 1);
-       if (entry == NULL)
+       if (!entry)
                return -ENOMEM;
 
        entry->index = index;
@@ -193,7 +213,7 @@ int __connman_resolvfile_remove(int index, const char *domain,
                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)
@@ -207,6 +227,57 @@ int __connman_resolvfile_remove(int index, const char *domain,
        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;
@@ -216,7 +287,7 @@ static void remove_entries(GSList *entries)
 
                entry_list = g_slist_remove(entry_list, entry);
 
-               if (dnsproxy_enabled == TRUE) {
+               if (dnsproxy_enabled) {
                        __connman_dnsproxy_remove(entry->index, entry->domain,
                                                        entry->server);
                } else {
@@ -232,6 +303,8 @@ static void remove_entries(GSList *entries)
        }
 
        g_slist_free(entries);
+
+       __connman_resolver_append_fallback_nameservers();
 }
 
 static gboolean resolver_expire_cb(gpointer user_data)
@@ -247,9 +320,15 @@ static gboolean resolver_expire_cb(gpointer user_data)
        if (entry->index >= 0) {
                struct connman_service *service;
                service = __connman_service_lookup_from_index(entry->index);
-               if (service != NULL)
+               if (service)
+#if defined TIZEN_EXT
+                       __connman_service_nameserver_remove(service,
+                                       entry->server, true,
+                                       CONNMAN_IPCONFIG_TYPE_ALL);
+#else
                        __connman_service_nameserver_remove(service,
-                                                       entry->server, TRUE);
+                                                       entry->server, true);
+#endif
        }
 
        remove_entries(list);
@@ -277,12 +356,12 @@ static gboolean resolver_refresh_cb(gpointer user_data)
 
        if (entry->index >= 0) {
                service = __connman_service_lookup_from_index(entry->index);
-               if (service != NULL) {
+               if (service) {
                        /*
                         * Send Router Solicitation to refresh RDNSS entries
                         * before their lifetime expires
                         */
-                       __connman_refresh_rs_ipv6(
+                       __connman_network_refresh_rs_ipv6(
                                        __connman_service_get_network(service),
                                        entry->index);
                }
@@ -300,11 +379,16 @@ static int append_resolver(int index, const char *domain,
        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->index = index;
@@ -313,7 +397,7 @@ static int append_resolver(int index, const char *domain,
        entry->flags = flags;
        entry->lifetime = lifetime;
 
-       if (server != NULL)
+       if (server)
                entry->family = connman_inet_check_ipaddress(server);
 
        if (lifetime) {
@@ -325,26 +409,36 @@ static int append_resolver(int index, const char *domain,
 
                entry->timeout = g_timeout_add_seconds(interval,
                                resolver_refresh_cb, entry);
-
-               /*
-                * We update the service only for those nameservers
-                * that are automagically added via netlink (lifetime > 0)
-                */
-               if (server != NULL && entry->index >= 0) {
-                       struct connman_service *service;
-                       service = __connman_service_lookup_from_index(entry->index);
-                       if (service != NULL)
-                               __connman_service_nameserver_append(service,
-                                                               server, TRUE);
-               }
        }
+
+       if (entry->index >= 0 && entry->server)
+               remove_fallback_nameservers();
+
        entry_list = g_slist_append(entry_list, entry);
 
-       if (dnsproxy_enabled == TRUE)
+       if (dnsproxy_enabled)
                __connman_dnsproxy_append(entry->index, domain, server);
        else
                __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;
 }
 
@@ -363,7 +457,7 @@ int connman_resolver_append(int index, const char *domain,
 
        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) {
@@ -374,8 +468,13 @@ int connman_resolver_append(int index, const char *domain,
 
                if (entry->index == index &&
                                g_strcmp0(entry->domain, domain) == 0 &&
-                               g_strcmp0(entry->server, server) == 0)
+                               g_strcmp0(entry->server, server) == 0) {
+                       if (dnsproxy_enabled)
+                               __connman_dnsproxy_append(entry->index, domain,
+                                               server);
+
                        return -EEXIST;
+               }
        }
 
        return append_resolver(index, domain, server, 0, 0);
@@ -399,7 +498,7 @@ int connman_resolver_append_lifetime(int index, const char *domain,
        DBG("index %d domain %s server %s lifetime %d",
                                index, domain, server, lifetime);
 
-       if (server == NULL && domain == NULL)
+       if (!server && !domain)
                return -EINVAL;
 
        for (list = entry_list; list; list = list->next) {
@@ -462,7 +561,7 @@ int connman_resolver_remove(int index, const char *domain, const char *server)
                break;
        }
 
-       if (matches == NULL)
+       if (!matches)
                return -ENOENT;
 
        remove_entries(matches);
@@ -494,7 +593,7 @@ int connman_resolver_remove_all(int index)
                matches = g_slist_prepend(matches, entry);
        }
 
-       if (matches == NULL)
+       if (!matches)
                return -ENOENT;
 
        remove_entries(matches);
@@ -502,24 +601,11 @@ int connman_resolver_remove_all(int index)
        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(int index)
 {
        GSList *list;
 
-       if (dnsproxy_enabled == FALSE)
+       if (!dnsproxy_enabled)
                return 0;
 
        DBG("index %d", index);
@@ -546,19 +632,33 @@ int __connman_resolver_redo_servers(int index)
                 */
                __connman_dnsproxy_remove(entry->index, entry->domain,
                                        entry->server);
-               /*
-                * Remove also the resolver timer for the old server entry.
-                * A new timer will be set for the new server entry
-                * when the next Router Advertisement message arrives
-                * with RDNSS/DNSSL settings.
-                */
-               g_source_remove(entry->timeout);
-               entry->timeout = 0;
 
                __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;
 }
 
@@ -578,14 +678,30 @@ static void free_resolvfile(gpointer data)
        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) {
@@ -593,10 +709,10 @@ int __connman_resolver_init(connman_bool_t dnsproxy)
                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(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
        }
@@ -608,7 +724,7 @@ void __connman_resolver_cleanup(void)
 {
        DBG("");
 
-       if (dnsproxy_enabled == TRUE)
+       if (dnsproxy_enabled)
                __connman_dnsproxy_cleanup();
        else {
                GList *list;