*
* 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
#include <netdb.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
+#include <net/if.h>
#include "gresolv.h"
};
struct _GResolv {
- gint ref_count;
+ int ref_count;
int result_family;
socklen_t sl = sizeof(res->src);
int fd;
- fd = socket(res->dst.sa.sa_family, SOCK_DGRAM, IPPROTO_IP);
+ fd = socket(res->dst.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC,
+ IPPROTO_IP);
if (fd < 0)
return;
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));
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);
if (resolv == NULL)
return NULL;
- g_atomic_int_inc(&resolv->ref_count);
+ __sync_fetch_and_add(&resolv->ref_count, 1);
return 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)))
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);