technology: return already enabled when tethering is enabled
[framework/connectivity/connman.git] / gweb / gresolv.c
index 326b5f9..e93bf4f 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Resolver library with GLib integration
  *
- *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2009-2012  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
@@ -34,6 +34,7 @@
 #include <netdb.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include <net/if.h>
 
 #include "gresolv.h"
 
@@ -97,7 +98,7 @@ struct resolv_nameserver {
 };
 
 struct _GResolv {
-       gint ref_count;
+       int ref_count;
 
        int result_family;
 
@@ -606,8 +607,10 @@ static void add_result(struct resolv_lookup *lookup, int family,
                                                        const void *data)
 {
        int n = lookup->nr_results++;
-       lookup->results = g_realloc(lookup->results,
+       lookup->results = g_try_realloc(lookup->results,
                                        sizeof(struct sort_result) * (n + 1));
+       if (lookup->results == NULL)
+               return;
 
        memset(&lookup->results[n], 0, sizeof(struct sort_result));
 
@@ -760,6 +763,26 @@ static int connect_udp_channel(struct resolv_nameserver *nameserver)
                return -EIO;
        }
 
+       /*
+        * If nameserver points to localhost ip, their is no need to
+        * bind the socket on any interface.
+        */
+       if (nameserver->resolv->index > 0 &&
+                       strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
+               char interface[IF_NAMESIZE];
+
+               memset(interface, 0, IF_NAMESIZE);
+               if (if_indextoname(nameserver->resolv->index,
+                                               interface) != NULL) {
+                       if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
+                                               interface, IF_NAMESIZE) < 0) {
+                               close(sk);
+                               freeaddrinfo(rp);
+                               return -EIO;
+                       }
+               }
+       }
+
        if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
                close(sk);
                freeaddrinfo(rp);
@@ -826,7 +849,7 @@ GResolv *g_resolv_ref(GResolv *resolv)
        if (resolv == NULL)
                return NULL;
 
-       g_atomic_int_inc(&resolv->ref_count);
+       __sync_fetch_and_add(&resolv->ref_count, 1);
 
        return resolv;
 }
@@ -838,7 +861,7 @@ void g_resolv_unref(GResolv *resolv)
        if (resolv == NULL)
                return;
 
-       if (g_atomic_int_dec_and_test(&resolv->ref_count) == FALSE)
+       if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
                return;
 
        while ((query = g_queue_pop_head(resolv->query_queue)))
@@ -879,14 +902,13 @@ gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
        nameserver->address = g_strdup(address);
        nameserver->port = port;
        nameserver->flags = flags;
+       nameserver->resolv = resolv;
 
        if (connect_udp_channel(nameserver) < 0) {
                free_nameserver(nameserver);
                return FALSE;
        }
 
-       nameserver->resolv = resolv;
-
        resolv->nameserver_list = g_list_append(resolv->nameserver_list,
                                                                nameserver);