3 * Resolver library with GLib integration
5 * Copyright (C) 2009-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <arpa/nameser.h>
50 struct sockaddr_in sin;
51 struct sockaddr_in6 sin6;
55 struct sockaddr_in sin;
56 struct sockaddr_in6 sin6;
62 struct resolv_lookup {
67 struct sort_result *results;
69 struct resolv_query *ipv4_query;
70 struct resolv_query *ipv6_query;
75 GResolvResultFunc result_func;
86 struct resolv_lookup *lookup;
89 struct resolv_nameserver {
96 GIOChannel *udp_channel;
105 guint next_lookup_id;
106 GQueue *lookup_queue;
110 GList *nameserver_list;
112 struct __res_state res;
114 GResolvDebugFunc debug_func;
118 #define debug(resolv, format, arg...) \
119 _debug(resolv, __FILE__, __func__, format, ## arg)
121 static void _debug(GResolv *resolv, const char *file, const char *caller,
122 const char *format, ...)
128 if (resolv->debug_func == NULL)
131 va_start(ap, format);
133 if ((len = snprintf(str, sizeof(str), "%s:%s() resolv %p ",
134 file, caller, resolv)) > 0) {
135 if (vsnprintf(str + len, sizeof(str) - len, format, ap) > 0)
136 resolv->debug_func(str, resolv->debug_data);
142 static void destroy_query(struct resolv_query *query)
144 if (query->timeout > 0)
145 g_source_remove(query->timeout);
150 static void destroy_lookup(struct resolv_lookup *lookup)
152 if (lookup->ipv4_query != NULL) {
153 g_queue_remove(lookup->resolv->query_queue,
155 destroy_query(lookup->ipv4_query);
158 if (lookup->ipv6_query != NULL) {
159 g_queue_remove(lookup->resolv->query_queue,
161 destroy_query(lookup->ipv6_query);
164 g_free(lookup->results);
168 static void find_srcaddr(struct sort_result *res)
170 socklen_t sl = sizeof(res->src);
173 fd = socket(res->dst.sa.sa_family, SOCK_DGRAM | SOCK_CLOEXEC,
178 if (connect(fd, &res->dst.sa, sizeof(res->dst)) < 0) {
183 if (getsockname(fd, &res->src.sa, &sl) < 0) {
188 res->reachable = TRUE;
194 unsigned char addr[NS_IN6ADDRSZ];
199 static const struct gai_table gai_labels[] = {
201 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
206 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
211 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
216 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
221 /* Variations from RFC 3484, matching glibc behaviour */
222 .addr = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
227 .addr = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
232 .addr = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
238 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
245 static const struct gai_table gai_precedences[] = {
247 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
252 .addr = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
257 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
262 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 },
267 .addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
274 static unsigned char v4mapped[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
275 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
277 static gboolean mask_compare(const unsigned char *one,
278 const unsigned char *two, int mask)
281 if (memcmp(one, two, mask / 8))
288 if (mask && ((*one ^ *two) >> (8 - mask)))
294 static int match_gai_table(struct sockaddr *sa, const struct gai_table *tbl)
296 struct sockaddr_in *sin = (void *)sa;
297 struct sockaddr_in6 *sin6 = (void *)sa;
300 if (sa->sa_family == AF_INET) {
302 memcpy(v4mapped+12, &sin->sin_addr, NS_INADDRSZ);
304 addr = &sin6->sin6_addr;
307 if (mask_compare(addr, tbl->addr, tbl->mask))
313 #define DQUAD(_a,_b,_c,_d) ( ((_a)<<24) | ((_b)<<16) | ((_c)<<8) | (_d) )
314 #define V4MATCH(addr, a,b,c,d, m) ( ((addr) ^ DQUAD(a,b,c,d)) >> (32 - (m)) )
316 #define RFC3484_SCOPE_LINK 2
317 #define RFC3484_SCOPE_SITE 5
318 #define RFC3484_SCOPE_GLOBAL 14
320 static int addr_scope(struct sockaddr *sa)
322 if (sa->sa_family == AF_INET) {
323 struct sockaddr_in *sin = (void *)sa;
324 guint32 addr = ntohl(sin->sin_addr.s_addr);
326 if (V4MATCH(addr, 169,254,0,0, 16) ||
327 V4MATCH(addr, 127,0,0,0, 8))
328 return RFC3484_SCOPE_LINK;
331 if (V4MATCH(addr, 10,0,0,0, 8) ||
332 V4MATCH(addr, 172,16,0,0, 12) ||
333 V4MATCH(addr, 192,168,0,0, 16))
334 return RFC3484_SCOPE_SITE;
337 return RFC3484_SCOPE_GLOBAL;
339 struct sockaddr_in6 *sin6 = (void *)sa;
341 /* Multicast addresses have a 4-bit scope field */
342 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
343 return sin6->sin6_addr.s6_addr[1] & 0xf;
345 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
346 IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
347 return RFC3484_SCOPE_LINK;
349 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
350 return RFC3484_SCOPE_SITE;
352 return RFC3484_SCOPE_GLOBAL;
356 static int rfc3484_compare(const void *__one, const void *__two)
358 const struct sort_result *one = __one;
359 const struct sort_result *two = __two;
361 /* Rule 1: Avoid unusable destinations */
362 if (one->reachable && !two->reachable)
364 else if (two->reachable && !one->reachable)
367 /* Rule 2: Prefer matching scope */
368 if (one->dst_scope == one->src_scope &&
369 two->dst_scope != two->src_scope)
371 else if (two->dst_scope == two->src_scope &&
372 one->dst_scope != one->src_scope)
375 /* Rule 3: Avoid deprecated addresses */
377 /* Rule 4: Prefer home addresses */
379 /* Rule 5: Prefer matching label */
380 if (one->dst_label == one->src_label &&
381 two->dst_label != two->src_label)
383 else if (two->dst_label == two->src_label &&
384 one->dst_label != one->src_label)
387 /* Rule 6: Prefer higher precedence */
388 if (one->precedence > two->precedence)
390 else if (two->precedence > one->precedence)
393 /* Rule 7: Prefer native transport */
395 /* Rule 8: Prefer smaller scope */
396 if (one->dst_scope != two->dst_scope)
397 return one->dst_scope - two->dst_scope;
399 /* Rule 9: Use longest matching prefix */
400 if (one->dst.sa.sa_family == AF_INET) {
402 * Rule 9 is meaningless and counterproductive for Legacy IP
403 * unless perhaps we can tell that it's actually on the local
404 * subnet. But we don't (yet) have local interface config
405 * information, so do nothing here for Legacy IP for now.
410 for (i = 0; i < 4; i++) {
411 guint32 cmp_one, cmp_two;
413 cmp_one = one->src.sin6.sin6_addr.s6_addr32[i] ^
414 one->dst.sin6.sin6_addr.s6_addr32[i];
415 cmp_two = two->src.sin6.sin6_addr.s6_addr32[i] ^
416 two->dst.sin6.sin6_addr.s6_addr32[i];
418 if (!cmp_two && !cmp_one)
421 if (cmp_one && !cmp_two)
423 if (cmp_two && !cmp_one)
426 /* g_bit_storage() is effectively fls() */
427 cmp_one = g_bit_storage(ntohl(cmp_one));
428 cmp_two = g_bit_storage(ntohl(cmp_two));
430 if (cmp_one == cmp_two)
433 return cmp_one - cmp_two;
438 /* Rule 10: Otherwise, leave the order unchanged */
445 static void rfc3484_sort_results(struct resolv_lookup *lookup)
449 for (i = 0; i < lookup->nr_results; i++) {
450 struct sort_result *res = &lookup->results[i];
452 res->precedence = match_gai_table(&res->dst.sa,
454 res->dst_label = match_gai_table(&res->dst.sa, gai_labels);
455 res->src_label = match_gai_table(&res->src.sa, gai_labels);
456 res->dst_scope = addr_scope(&res->dst.sa);
457 res->src_scope = addr_scope(&res->src.sa);
460 qsort(lookup->results, lookup->nr_results,
461 sizeof(struct sort_result), rfc3484_compare);
464 static void sort_and_return_results(struct resolv_lookup *lookup)
466 char buf[INET6_ADDRSTRLEN + 1];
467 GResolvResultStatus status;
468 char **results = g_try_new0(char *, lookup->nr_results + 1);
474 memset(buf, 0, INET6_ADDRSTRLEN + 1);
476 rfc3484_sort_results(lookup);
478 for (i = 0; i < lookup->nr_results; i++) {
479 if (lookup->results[i].dst.sa.sa_family == AF_INET) {
480 if (inet_ntop(AF_INET,
481 &lookup->results[i].dst.sin.sin_addr,
482 buf, sizeof(buf) - 1) == NULL)
484 } else if (lookup->results[i].dst.sa.sa_family == AF_INET6) {
485 if (inet_ntop(AF_INET6,
486 &lookup->results[i].dst.sin6.sin6_addr,
487 buf, sizeof(buf) - 1) == NULL)
492 results[n++] = strdup(buf);
497 if (lookup->resolv->result_family == AF_INET)
498 status = lookup->ipv4_status;
499 else if (lookup->resolv->result_family == AF_INET6)
500 status = lookup->ipv6_status;
502 if (lookup->ipv6_status == G_RESOLV_RESULT_STATUS_SUCCESS)
503 status = lookup->ipv6_status;
505 status = lookup->ipv4_status;
508 g_queue_remove(lookup->resolv->lookup_queue, lookup);
510 lookup->result_func(status, results, lookup->result_data);
513 destroy_lookup(lookup);
516 static gboolean query_timeout(gpointer user_data)
518 struct resolv_query *query = user_data;
519 struct resolv_lookup *lookup = query->lookup;
520 GResolv *resolv = query->resolv;
524 if (query == lookup->ipv4_query) {
525 lookup->ipv4_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
526 lookup->ipv4_query = NULL;
527 } else if (query == lookup->ipv6_query) {
528 lookup->ipv6_status = G_RESOLV_RESULT_STATUS_NO_RESPONSE;
529 lookup->ipv6_query = NULL;
532 g_queue_remove(resolv->query_queue, query);
534 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
535 sort_and_return_results(lookup);
537 destroy_query(query);
542 static void free_nameserver(struct resolv_nameserver *nameserver)
544 if (nameserver == NULL)
547 if (nameserver->udp_watch > 0)
548 g_source_remove(nameserver->udp_watch);
550 if (nameserver->udp_channel != NULL)
551 g_io_channel_unref(nameserver->udp_channel);
553 g_free(nameserver->address);
557 static void flush_nameservers(GResolv *resolv)
561 for (list = g_list_first(resolv->nameserver_list);
562 list; list = g_list_next(list))
563 free_nameserver(list->data);
565 g_list_free(resolv->nameserver_list);
566 resolv->nameserver_list = NULL;
569 static int send_query(GResolv *resolv, const unsigned char *buf, int len)
573 if (resolv->nameserver_list == NULL)
576 for (list = g_list_first(resolv->nameserver_list);
577 list; list = g_list_next(list)) {
578 struct resolv_nameserver *nameserver = list->data;
581 if (nameserver->udp_channel == NULL)
584 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
586 sent = send(sk, buf, len, 0);
594 static gint compare_lookup_id(gconstpointer a, gconstpointer b)
596 const struct resolv_lookup *lookup = a;
597 guint id = GPOINTER_TO_UINT(b);
608 static gint compare_query_msgid(gconstpointer a, gconstpointer b)
610 const struct resolv_query *query = a;
611 uint16_t msgid = GPOINTER_TO_UINT(b);
613 if (query->msgid < msgid)
616 if (query->msgid > msgid)
622 static void add_result(struct resolv_lookup *lookup, int family,
625 int n = lookup->nr_results++;
626 lookup->results = g_try_realloc(lookup->results,
627 sizeof(struct sort_result) * (n + 1));
628 if (lookup->results == NULL)
631 memset(&lookup->results[n], 0, sizeof(struct sort_result));
633 lookup->results[n].dst.sa.sa_family = family;
634 if (family == AF_INET)
635 memcpy(&lookup->results[n].dst.sin.sin_addr,
638 memcpy(&lookup->results[n].dst.sin6.sin6_addr,
642 static void parse_response(struct resolv_nameserver *nameserver,
643 const unsigned char *buf, int len)
645 GResolv *resolv = nameserver->resolv;
646 GResolvResultStatus status;
647 struct resolv_query *query;
648 struct resolv_lookup *lookup;
654 debug(resolv, "response from %s", nameserver->address);
656 ns_initparse(buf, len, &msg);
658 rcode = ns_msg_getflag(msg, ns_f_rcode);
659 count = ns_msg_count(msg, ns_s_an);
661 debug(resolv, "msg id: 0x%04x rcode: %d count: %d",
662 ns_msg_id(msg), rcode, count);
666 status = G_RESOLV_RESULT_STATUS_SUCCESS;
669 status = G_RESOLV_RESULT_STATUS_FORMAT_ERROR;
672 status = G_RESOLV_RESULT_STATUS_SERVER_FAILURE;
675 status = G_RESOLV_RESULT_STATUS_NAME_ERROR;
678 status = G_RESOLV_RESULT_STATUS_NOT_IMPLEMENTED;
681 status = G_RESOLV_RESULT_STATUS_REFUSED;
684 status = G_RESOLV_RESULT_STATUS_ERROR;
688 list = g_queue_find_custom(resolv->query_queue,
689 GUINT_TO_POINTER(ns_msg_id(msg)), compare_query_msgid);
694 lookup = query->lookup;
696 if (query == lookup->ipv6_query) {
697 lookup->ipv6_status = status;
698 lookup->ipv6_query = NULL;
699 } else if (query == lookup->ipv4_query) {
700 lookup->ipv4_status = status;
701 lookup->ipv4_query = NULL;
704 for (i = 0; i < count; i++) {
705 ns_parserr(&msg, ns_s_an, i, &rr);
707 if (ns_rr_class(rr) != ns_c_in)
710 g_assert(offsetof(struct sockaddr_in, sin_addr) ==
711 offsetof(struct sockaddr_in6, sin6_flowinfo));
713 if (ns_rr_type(rr) == ns_t_a &&
714 ns_rr_rdlen(rr) == NS_INADDRSZ) {
715 add_result(lookup, AF_INET, ns_rr_rdata(rr));
716 } else if (ns_rr_type(rr) == ns_t_aaaa &&
717 ns_rr_rdlen(rr) == NS_IN6ADDRSZ) {
718 add_result(lookup, AF_INET6, ns_rr_rdata(rr));
722 g_queue_remove(resolv->query_queue, query);
724 if (lookup->ipv4_query == NULL && lookup->ipv6_query == NULL)
725 sort_and_return_results(lookup);
727 destroy_query(query);
730 static gboolean received_udp_data(GIOChannel *channel, GIOCondition cond,
733 struct resolv_nameserver *nameserver = user_data;
734 unsigned char buf[4096];
737 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
738 nameserver->udp_watch = 0;
742 sk = g_io_channel_unix_get_fd(nameserver->udp_channel);
744 len = recv(sk, buf, sizeof(buf), 0);
748 parse_response(nameserver, buf, len);
753 static int connect_udp_channel(struct resolv_nameserver *nameserver)
755 struct addrinfo hints, *rp;
759 memset(&hints, 0, sizeof(hints));
760 hints.ai_family = AF_UNSPEC;
761 hints.ai_socktype = SOCK_DGRAM;
762 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV | AI_NUMERICHOST;
764 sprintf(portnr, "%d", nameserver->port);
765 err = getaddrinfo(nameserver->address, portnr, &hints, &rp);
770 * Do not blindly copy this code elsewhere; it doesn't loop over the
771 * results using ->ai_next as it should. That's OK in *this* case
772 * because it was a numeric lookup; we *know* there's only one.
777 sk = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
784 * If nameserver points to localhost ip, their is no need to
785 * bind the socket on any interface.
787 if (nameserver->resolv->index > 0 &&
788 strncmp(nameserver->address, "127.0.0.1", 9) != 0) {
789 char interface[IF_NAMESIZE];
791 memset(interface, 0, IF_NAMESIZE);
792 if (if_indextoname(nameserver->resolv->index,
793 interface) != NULL) {
794 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
795 interface, IF_NAMESIZE) < 0) {
803 if (connect(sk, rp->ai_addr, rp->ai_addrlen) < 0) {
811 nameserver->udp_channel = g_io_channel_unix_new(sk);
812 if (nameserver->udp_channel == NULL) {
817 g_io_channel_set_close_on_unref(nameserver->udp_channel, TRUE);
819 nameserver->udp_watch = g_io_add_watch(nameserver->udp_channel,
820 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
821 received_udp_data, nameserver);
826 GResolv *g_resolv_new(int index)
833 resolv = g_try_new0(GResolv, 1);
837 resolv->ref_count = 1;
839 resolv->result_family = AF_UNSPEC;
841 resolv->next_lookup_id = 1;
843 resolv->query_queue = g_queue_new();
844 if (resolv->query_queue == NULL) {
849 resolv->lookup_queue = g_queue_new();
850 if (resolv->lookup_queue == NULL) {
851 g_queue_free(resolv->query_queue);
856 resolv->index = index;
857 resolv->nameserver_list = NULL;
859 res_ninit(&resolv->res);
864 GResolv *g_resolv_ref(GResolv *resolv)
869 __sync_fetch_and_add(&resolv->ref_count, 1);
874 void g_resolv_unref(GResolv *resolv)
876 struct resolv_query *query;
877 struct resolv_lookup *lookup;
882 if (__sync_fetch_and_sub(&resolv->ref_count, 1) != 1)
885 while ((lookup = g_queue_pop_head(resolv->lookup_queue)))
886 destroy_lookup(lookup);
888 while ((query = g_queue_pop_head(resolv->query_queue)))
889 destroy_query(query);
891 g_queue_free(resolv->query_queue);
892 g_queue_free(resolv->lookup_queue);
894 flush_nameservers(resolv);
896 res_nclose(&resolv->res);
901 void g_resolv_set_debug(GResolv *resolv, GResolvDebugFunc func,
907 resolv->debug_func = func;
908 resolv->debug_data = user_data;
911 gboolean g_resolv_add_nameserver(GResolv *resolv, const char *address,
912 uint16_t port, unsigned long flags)
914 struct resolv_nameserver *nameserver;
919 nameserver = g_try_new0(struct resolv_nameserver, 1);
920 if (nameserver == NULL)
923 nameserver->address = g_strdup(address);
924 nameserver->port = port;
925 nameserver->flags = flags;
926 nameserver->resolv = resolv;
928 if (connect_udp_channel(nameserver) < 0) {
929 free_nameserver(nameserver);
933 resolv->nameserver_list = g_list_append(resolv->nameserver_list,
936 debug(resolv, "setting nameserver %s", address);
941 void g_resolv_flush_nameservers(GResolv *resolv)
946 flush_nameservers(resolv);
949 static gint add_query(struct resolv_lookup *lookup, const char *hostname, int type)
951 struct resolv_query *query = g_try_new0(struct resolv_query, 1);
952 unsigned char buf[4096];
958 len = res_mkquery(ns_o_query, hostname, ns_c_in, type,
959 NULL, 0, NULL, buf, sizeof(buf));
961 query->msgid = buf[0] << 8 | buf[1];
963 if (send_query(lookup->resolv, buf, len) < 0) {
968 query->resolv = lookup->resolv;
969 query->lookup = lookup;
971 g_queue_push_tail(lookup->resolv->query_queue, query);
973 query->timeout = g_timeout_add_seconds(5, query_timeout, query);
975 if (type == ns_t_aaaa)
976 lookup->ipv6_query = query;
978 lookup->ipv4_query = query;
983 guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
984 GResolvResultFunc func, gpointer user_data)
986 struct resolv_lookup *lookup;
988 debug(resolv, "lookup hostname %s", hostname);
993 if (resolv->nameserver_list == NULL) {
996 for (i = 0; i < resolv->res.nscount; i++) {
998 int family = resolv->res.nsaddr_list[i].sin_family;
999 void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
1001 if (family != AF_INET &&
1002 resolv->res._u._ext.nsaddrs[i]) {
1004 sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
1007 if (family != AF_INET && family != AF_INET6)
1010 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
1011 g_resolv_add_nameserver(resolv, buf, 53, 0);
1014 if (resolv->nameserver_list == NULL)
1015 g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
1018 lookup = g_try_new0(struct resolv_lookup, 1);
1022 lookup->resolv = resolv;
1023 lookup->result_func = func;
1024 lookup->result_data = user_data;
1025 lookup->id = resolv->next_lookup_id++;
1027 if (resolv->result_family != AF_INET6) {
1028 if (add_query(lookup, hostname, ns_t_a)) {
1034 if (resolv->result_family != AF_INET) {
1035 if (add_query(lookup, hostname, ns_t_aaaa)) {
1036 if (resolv->result_family != AF_INET6) {
1037 destroy_query(lookup->ipv4_query);
1038 g_queue_remove(resolv->query_queue,
1039 lookup->ipv4_query);
1047 g_queue_push_tail(resolv->lookup_queue, lookup);
1051 gboolean g_resolv_cancel_lookup(GResolv *resolv, guint id)
1055 list = g_queue_find_custom(resolv->lookup_queue,
1056 GUINT_TO_POINTER(id), compare_lookup_id);
1061 g_queue_remove(resolv->lookup_queue, list->data);
1062 destroy_lookup(list->data);
1067 gboolean g_resolv_set_address_family(GResolv *resolv, int family)
1072 if (family != AF_UNSPEC && family != AF_INET && family != AF_INET6)
1075 resolv->result_family = family;