Add some more cases to the app-id unit tests
[glib.git] / gio / gthreadedresolver.c
blob6b3b1f08852dd68f53b1d0b34211607c4c40a5e4
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
25 #include <stdio.h>
26 #include <string.h>
28 #include "gthreadedresolver.h"
29 #include "gnetworkingprivate.h"
31 #include "gcancellable.h"
32 #include "ginetaddress.h"
33 #include "ginetsocketaddress.h"
34 #include "gtask.h"
35 #include "gsocketaddress.h"
36 #include "gsrvtarget.h"
39 G_DEFINE_TYPE (GThreadedResolver, g_threaded_resolver, G_TYPE_RESOLVER)
41 static void
42 g_threaded_resolver_init (GThreadedResolver *gtr)
46 static GResolverError
47 g_resolver_error_from_addrinfo_error (gint err)
49 switch (err)
51 case EAI_FAIL:
52 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
53 case EAI_NODATA:
54 #endif
55 case EAI_NONAME:
56 return G_RESOLVER_ERROR_NOT_FOUND;
58 case EAI_AGAIN:
59 return G_RESOLVER_ERROR_TEMPORARY_FAILURE;
61 default:
62 return G_RESOLVER_ERROR_INTERNAL;
66 static struct addrinfo addrinfo_hints;
68 static void
69 do_lookup_by_name (GTask *task,
70 gpointer source_object,
71 gpointer task_data,
72 GCancellable *cancellable)
74 const char *hostname = task_data;
75 struct addrinfo *res = NULL;
76 GList *addresses;
77 gint retval;
79 retval = getaddrinfo (hostname, NULL, &addrinfo_hints, &res);
81 if (retval == 0)
83 struct addrinfo *ai;
84 GSocketAddress *sockaddr;
85 GInetAddress *addr;
87 addresses = NULL;
88 for (ai = res; ai; ai = ai->ai_next)
90 sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen);
91 if (!sockaddr)
92 continue;
93 if (!G_IS_INET_SOCKET_ADDRESS (sockaddr))
95 g_clear_object (&sockaddr);
96 continue;
99 addr = g_object_ref (g_inet_socket_address_get_address ((GInetSocketAddress *)sockaddr));
100 addresses = g_list_prepend (addresses, addr);
101 g_object_unref (sockaddr);
104 if (addresses != NULL)
106 addresses = g_list_reverse (addresses);
107 g_task_return_pointer (task, addresses,
108 (GDestroyNotify)g_resolver_free_addresses);
110 else
112 /* All addresses failed to be converted to GSocketAddresses. */
113 g_task_return_new_error (task,
114 G_RESOLVER_ERROR,
115 G_RESOLVER_ERROR_NOT_FOUND,
116 _("Error resolving “%s”: %s"),
117 hostname,
118 _("No valid addresses were found"));
121 else
123 g_task_return_new_error (task,
124 G_RESOLVER_ERROR,
125 g_resolver_error_from_addrinfo_error (retval),
126 _("Error resolving “%s”: %s"),
127 hostname, gai_strerror (retval));
130 if (res)
131 freeaddrinfo (res);
134 static GList *
135 lookup_by_name (GResolver *resolver,
136 const gchar *hostname,
137 GCancellable *cancellable,
138 GError **error)
140 GTask *task;
141 GList *addresses;
143 task = g_task_new (resolver, cancellable, NULL, NULL);
144 g_task_set_source_tag (task, lookup_by_name);
145 g_task_set_task_data (task, g_strdup (hostname), g_free);
146 g_task_set_return_on_cancel (task, TRUE);
147 g_task_run_in_thread_sync (task, do_lookup_by_name);
148 addresses = g_task_propagate_pointer (task, error);
149 g_object_unref (task);
151 return addresses;
154 static void
155 lookup_by_name_async (GResolver *resolver,
156 const gchar *hostname,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
159 gpointer user_data)
161 GTask *task;
163 task = g_task_new (resolver, cancellable, callback, user_data);
164 g_task_set_source_tag (task, lookup_by_name_async);
165 g_task_set_task_data (task, g_strdup (hostname), g_free);
166 g_task_set_return_on_cancel (task, TRUE);
167 g_task_run_in_thread (task, do_lookup_by_name);
168 g_object_unref (task);
171 static GList *
172 lookup_by_name_finish (GResolver *resolver,
173 GAsyncResult *result,
174 GError **error)
176 g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
178 return g_task_propagate_pointer (G_TASK (result), error);
182 static void
183 do_lookup_by_address (GTask *task,
184 gpointer source_object,
185 gpointer task_data,
186 GCancellable *cancellable)
188 GInetAddress *address = task_data;
189 struct sockaddr_storage sockaddr;
190 gsize sockaddr_size;
191 GSocketAddress *gsockaddr;
192 gchar name[NI_MAXHOST];
193 gint retval;
195 gsockaddr = g_inet_socket_address_new (address, 0);
196 g_socket_address_to_native (gsockaddr, (struct sockaddr *)&sockaddr,
197 sizeof (sockaddr), NULL);
198 sockaddr_size = g_socket_address_get_native_size (gsockaddr);
199 g_object_unref (gsockaddr);
201 retval = getnameinfo ((struct sockaddr *)&sockaddr, sockaddr_size,
202 name, sizeof (name), NULL, 0, NI_NAMEREQD);
203 if (retval == 0)
204 g_task_return_pointer (task, g_strdup (name), g_free);
205 else
207 gchar *phys;
209 phys = g_inet_address_to_string (address);
210 g_task_return_new_error (task,
211 G_RESOLVER_ERROR,
212 g_resolver_error_from_addrinfo_error (retval),
213 _("Error reverse-resolving “%s”: %s"),
214 phys ? phys : "(unknown)",
215 gai_strerror (retval));
216 g_free (phys);
220 static gchar *
221 lookup_by_address (GResolver *resolver,
222 GInetAddress *address,
223 GCancellable *cancellable,
224 GError **error)
226 GTask *task;
227 gchar *name;
229 task = g_task_new (resolver, cancellable, NULL, NULL);
230 g_task_set_source_tag (task, lookup_by_address);
231 g_task_set_task_data (task, g_object_ref (address), g_object_unref);
232 g_task_set_return_on_cancel (task, TRUE);
233 g_task_run_in_thread_sync (task, do_lookup_by_address);
234 name = g_task_propagate_pointer (task, error);
235 g_object_unref (task);
237 return name;
240 static void
241 lookup_by_address_async (GResolver *resolver,
242 GInetAddress *address,
243 GCancellable *cancellable,
244 GAsyncReadyCallback callback,
245 gpointer user_data)
247 GTask *task;
249 task = g_task_new (resolver, cancellable, callback, user_data);
250 g_task_set_source_tag (task, lookup_by_address_async);
251 g_task_set_task_data (task, g_object_ref (address), g_object_unref);
252 g_task_set_return_on_cancel (task, TRUE);
253 g_task_run_in_thread (task, do_lookup_by_address);
254 g_object_unref (task);
257 static gchar *
258 lookup_by_address_finish (GResolver *resolver,
259 GAsyncResult *result,
260 GError **error)
262 g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
264 return g_task_propagate_pointer (G_TASK (result), error);
268 #if defined(G_OS_UNIX)
270 #if defined __BIONIC__ && !defined BIND_4_COMPAT
271 /* Copy from bionic/libc/private/arpa_nameser_compat.h
272 * and bionic/libc/private/arpa_nameser.h */
273 typedef struct {
274 unsigned id :16; /* query identification number */
275 #if BYTE_ORDER == BIG_ENDIAN
276 /* fields in third byte */
277 unsigned qr: 1; /* response flag */
278 unsigned opcode: 4; /* purpose of message */
279 unsigned aa: 1; /* authoritive answer */
280 unsigned tc: 1; /* truncated message */
281 unsigned rd: 1; /* recursion desired */
282 /* fields in fourth byte */
283 unsigned ra: 1; /* recursion available */
284 unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
285 unsigned ad: 1; /* authentic data from named */
286 unsigned cd: 1; /* checking disabled by resolver */
287 unsigned rcode :4; /* response code */
288 #endif
289 #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
290 /* fields in third byte */
291 unsigned rd :1; /* recursion desired */
292 unsigned tc :1; /* truncated message */
293 unsigned aa :1; /* authoritive answer */
294 unsigned opcode :4; /* purpose of message */
295 unsigned qr :1; /* response flag */
296 /* fields in fourth byte */
297 unsigned rcode :4; /* response code */
298 unsigned cd: 1; /* checking disabled by resolver */
299 unsigned ad: 1; /* authentic data from named */
300 unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
301 unsigned ra :1; /* recursion available */
302 #endif
303 /* remaining bytes */
304 unsigned qdcount :16; /* number of question entries */
305 unsigned ancount :16; /* number of answer entries */
306 unsigned nscount :16; /* number of authority entries */
307 unsigned arcount :16; /* number of resource entries */
308 } HEADER;
310 #define NS_INT32SZ 4 /* #/bytes of data in a uint32_t */
311 #define NS_INT16SZ 2 /* #/bytes of data in a uint16_t */
313 #define NS_GET16(s, cp) do { \
314 const u_char *t_cp = (const u_char *)(cp); \
315 (s) = ((uint16_t)t_cp[0] << 8) \
316 | ((uint16_t)t_cp[1]) \
318 (cp) += NS_INT16SZ; \
319 } while (/*CONSTCOND*/0)
321 #define NS_GET32(l, cp) do { \
322 const u_char *t_cp = (const u_char *)(cp); \
323 (l) = ((uint32_t)t_cp[0] << 24) \
324 | ((uint32_t)t_cp[1] << 16) \
325 | ((uint32_t)t_cp[2] << 8) \
326 | ((uint32_t)t_cp[3]) \
328 (cp) += NS_INT32SZ; \
329 } while (/*CONSTCOND*/0)
331 #define GETSHORT NS_GET16
332 #define GETLONG NS_GET32
334 #define C_IN 1
336 /* From bionic/libc/private/resolv_private.h */
337 int dn_expand(const u_char *, const u_char *, const u_char *, char *, int);
338 #define dn_skipname __dn_skipname
339 int dn_skipname(const u_char *, const u_char *);
341 /* From bionic/libc/private/arpa_nameser_compat.h */
342 #define T_MX ns_t_mx
343 #define T_TXT ns_t_txt
344 #define T_SOA ns_t_soa
345 #define T_NS ns_t_ns
347 /* From bionic/libc/private/arpa_nameser.h */
348 typedef enum __ns_type {
349 ns_t_invalid = 0, /* Cookie. */
350 ns_t_a = 1, /* Host address. */
351 ns_t_ns = 2, /* Authoritative server. */
352 ns_t_md = 3, /* Mail destination. */
353 ns_t_mf = 4, /* Mail forwarder. */
354 ns_t_cname = 5, /* Canonical name. */
355 ns_t_soa = 6, /* Start of authority zone. */
356 ns_t_mb = 7, /* Mailbox domain name. */
357 ns_t_mg = 8, /* Mail group member. */
358 ns_t_mr = 9, /* Mail rename name. */
359 ns_t_null = 10, /* Null resource record. */
360 ns_t_wks = 11, /* Well known service. */
361 ns_t_ptr = 12, /* Domain name pointer. */
362 ns_t_hinfo = 13, /* Host information. */
363 ns_t_minfo = 14, /* Mailbox information. */
364 ns_t_mx = 15, /* Mail routing information. */
365 ns_t_txt = 16, /* Text strings. */
366 ns_t_rp = 17, /* Responsible person. */
367 ns_t_afsdb = 18, /* AFS cell database. */
368 ns_t_x25 = 19, /* X_25 calling address. */
369 ns_t_isdn = 20, /* ISDN calling address. */
370 ns_t_rt = 21, /* Router. */
371 ns_t_nsap = 22, /* NSAP address. */
372 ns_t_nsap_ptr = 23, /* Reverse NSAP lookup (deprecated). */
373 ns_t_sig = 24, /* Security signature. */
374 ns_t_key = 25, /* Security key. */
375 ns_t_px = 26, /* X.400 mail mapping. */
376 ns_t_gpos = 27, /* Geographical position (withdrawn). */
377 ns_t_aaaa = 28, /* Ip6 Address. */
378 ns_t_loc = 29, /* Location Information. */
379 ns_t_nxt = 30, /* Next domain (security). */
380 ns_t_eid = 31, /* Endpoint identifier. */
381 ns_t_nimloc = 32, /* Nimrod Locator. */
382 ns_t_srv = 33, /* Server Selection. */
383 ns_t_atma = 34, /* ATM Address */
384 ns_t_naptr = 35, /* Naming Authority PoinTeR */
385 ns_t_kx = 36, /* Key Exchange */
386 ns_t_cert = 37, /* Certification record */
387 ns_t_a6 = 38, /* IPv6 address (deprecates AAAA) */
388 ns_t_dname = 39, /* Non-terminal DNAME (for IPv6) */
389 ns_t_sink = 40, /* Kitchen sink (experimentatl) */
390 ns_t_opt = 41, /* EDNS0 option (meta-RR) */
391 ns_t_apl = 42, /* Address prefix list (RFC 3123) */
392 ns_t_tkey = 249, /* Transaction key */
393 ns_t_tsig = 250, /* Transaction signature. */
394 ns_t_ixfr = 251, /* Incremental zone transfer. */
395 ns_t_axfr = 252, /* Transfer zone of authority. */
396 ns_t_mailb = 253, /* Transfer mailbox records. */
397 ns_t_maila = 254, /* Transfer mail agent records. */
398 ns_t_any = 255, /* Wildcard match. */
399 ns_t_zxfr = 256, /* BIND-specific, nonstandard. */
400 ns_t_max = 65536
401 } ns_type;
403 #endif /* __BIONIC__ */
405 static GVariant *
406 parse_res_srv (guchar *answer,
407 guchar *end,
408 guchar **p)
410 gchar namebuf[1024];
411 guint16 priority, weight, port;
413 GETSHORT (priority, *p);
414 GETSHORT (weight, *p);
415 GETSHORT (port, *p);
416 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
418 return g_variant_new ("(qqqs)",
419 priority,
420 weight,
421 port,
422 namebuf);
425 static GVariant *
426 parse_res_soa (guchar *answer,
427 guchar *end,
428 guchar **p)
430 gchar mnamebuf[1024];
431 gchar rnamebuf[1024];
432 guint32 serial, refresh, retry, expire, ttl;
434 *p += dn_expand (answer, end, *p, mnamebuf, sizeof (mnamebuf));
435 *p += dn_expand (answer, end, *p, rnamebuf, sizeof (rnamebuf));
437 GETLONG (serial, *p);
438 GETLONG (refresh, *p);
439 GETLONG (retry, *p);
440 GETLONG (expire, *p);
441 GETLONG (ttl, *p);
443 return g_variant_new ("(ssuuuuu)",
444 mnamebuf,
445 rnamebuf,
446 serial,
447 refresh,
448 retry,
449 expire,
450 ttl);
453 static GVariant *
454 parse_res_ns (guchar *answer,
455 guchar *end,
456 guchar **p)
458 gchar namebuf[1024];
460 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
462 return g_variant_new ("(s)", namebuf);
465 static GVariant *
466 parse_res_mx (guchar *answer,
467 guchar *end,
468 guchar **p)
470 gchar namebuf[1024];
471 guint16 preference;
473 GETSHORT (preference, *p);
475 *p += dn_expand (answer, end, *p, namebuf, sizeof (namebuf));
477 return g_variant_new ("(qs)",
478 preference,
479 namebuf);
482 static GVariant *
483 parse_res_txt (guchar *answer,
484 guchar *end,
485 guchar **p)
487 GVariant *record;
488 GPtrArray *array;
489 guchar *at = *p;
490 gsize len;
492 array = g_ptr_array_new_with_free_func (g_free);
493 while (at < end)
495 len = *(at++);
496 if (len > at - end)
497 break;
498 g_ptr_array_add (array, g_strndup ((gchar *)at, len));
499 at += len;
502 *p = at;
503 record = g_variant_new ("(@as)",
504 g_variant_new_strv ((const gchar **)array->pdata, array->len));
505 g_ptr_array_free (array, TRUE);
506 return record;
509 static gint
510 g_resolver_record_type_to_rrtype (GResolverRecordType type)
512 switch (type)
514 case G_RESOLVER_RECORD_SRV:
515 return T_SRV;
516 case G_RESOLVER_RECORD_TXT:
517 return T_TXT;
518 case G_RESOLVER_RECORD_SOA:
519 return T_SOA;
520 case G_RESOLVER_RECORD_NS:
521 return T_NS;
522 case G_RESOLVER_RECORD_MX:
523 return T_MX;
525 g_return_val_if_reached (-1);
528 static GList *
529 g_resolver_records_from_res_query (const gchar *rrname,
530 gint rrtype,
531 guchar *answer,
532 gint len,
533 gint herr,
534 GError **error)
536 gint count;
537 gchar namebuf[1024];
538 guchar *end, *p;
539 guint16 type, qclass, rdlength;
540 guint32 ttl;
541 HEADER *header;
542 GList *records;
543 GVariant *record;
545 if (len <= 0)
547 if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA)
549 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
550 _("No DNS record of the requested type for “%s”"), rrname);
552 else if (herr == TRY_AGAIN)
554 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
555 _("Temporarily unable to resolve “%s”"), rrname);
557 else
559 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL,
560 _("Error resolving “%s”"), rrname);
563 return NULL;
566 records = NULL;
568 header = (HEADER *)answer;
569 p = answer + sizeof (HEADER);
570 end = answer + len;
572 /* Skip query */
573 count = ntohs (header->qdcount);
574 while (count-- && p < end)
576 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
577 p += 4;
579 /* To silence gcc warnings */
580 namebuf[0] = namebuf[1];
583 /* Read answers */
584 count = ntohs (header->ancount);
585 while (count-- && p < end)
587 p += dn_expand (answer, end, p, namebuf, sizeof (namebuf));
588 GETSHORT (type, p);
589 GETSHORT (qclass, p);
590 GETLONG (ttl, p);
591 ttl = ttl; /* To avoid -Wunused-but-set-variable */
592 GETSHORT (rdlength, p);
594 if (type != rrtype || qclass != C_IN)
596 p += rdlength;
597 continue;
600 switch (rrtype)
602 case T_SRV:
603 record = parse_res_srv (answer, end, &p);
604 break;
605 case T_MX:
606 record = parse_res_mx (answer, end, &p);
607 break;
608 case T_SOA:
609 record = parse_res_soa (answer, end, &p);
610 break;
611 case T_NS:
612 record = parse_res_ns (answer, end, &p);
613 break;
614 case T_TXT:
615 record = parse_res_txt (answer, p + rdlength, &p);
616 break;
617 default:
618 g_warn_if_reached ();
619 record = NULL;
620 break;
623 if (record != NULL)
624 records = g_list_prepend (records, record);
627 if (records == NULL)
629 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
630 _("No DNS record of the requested type for “%s”"), rrname);
632 return NULL;
634 else
635 return records;
638 #elif defined(G_OS_WIN32)
640 static GVariant *
641 parse_dns_srv (DNS_RECORD *rec)
643 return g_variant_new ("(qqqs)",
644 (guint16)rec->Data.SRV.wPriority,
645 (guint16)rec->Data.SRV.wWeight,
646 (guint16)rec->Data.SRV.wPort,
647 rec->Data.SRV.pNameTarget);
650 static GVariant *
651 parse_dns_soa (DNS_RECORD *rec)
653 return g_variant_new ("(ssuuuuu)",
654 rec->Data.SOA.pNamePrimaryServer,
655 rec->Data.SOA.pNameAdministrator,
656 (guint32)rec->Data.SOA.dwSerialNo,
657 (guint32)rec->Data.SOA.dwRefresh,
658 (guint32)rec->Data.SOA.dwRetry,
659 (guint32)rec->Data.SOA.dwExpire,
660 (guint32)rec->Data.SOA.dwDefaultTtl);
663 static GVariant *
664 parse_dns_ns (DNS_RECORD *rec)
666 return g_variant_new ("(s)", rec->Data.NS.pNameHost);
669 static GVariant *
670 parse_dns_mx (DNS_RECORD *rec)
672 return g_variant_new ("(qs)",
673 (guint16)rec->Data.MX.wPreference,
674 rec->Data.MX.pNameExchange);
677 static GVariant *
678 parse_dns_txt (DNS_RECORD *rec)
680 GVariant *record;
681 GPtrArray *array;
682 DWORD i;
684 array = g_ptr_array_new ();
685 for (i = 0; i < rec->Data.TXT.dwStringCount; i++)
686 g_ptr_array_add (array, rec->Data.TXT.pStringArray[i]);
687 record = g_variant_new ("(@as)",
688 g_variant_new_strv ((const gchar **)array->pdata, array->len));
689 g_ptr_array_free (array, TRUE);
690 return record;
693 static WORD
694 g_resolver_record_type_to_dnstype (GResolverRecordType type)
696 switch (type)
698 case G_RESOLVER_RECORD_SRV:
699 return DNS_TYPE_SRV;
700 case G_RESOLVER_RECORD_TXT:
701 return DNS_TYPE_TEXT;
702 case G_RESOLVER_RECORD_SOA:
703 return DNS_TYPE_SOA;
704 case G_RESOLVER_RECORD_NS:
705 return DNS_TYPE_NS;
706 case G_RESOLVER_RECORD_MX:
707 return DNS_TYPE_MX;
709 g_return_val_if_reached (-1);
712 static GList *
713 g_resolver_records_from_DnsQuery (const gchar *rrname,
714 WORD dnstype,
715 DNS_STATUS status,
716 DNS_RECORD *results,
717 GError **error)
719 DNS_RECORD *rec;
720 gpointer record;
721 GList *records;
723 if (status != ERROR_SUCCESS)
725 if (status == DNS_ERROR_RCODE_NAME_ERROR)
727 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
728 _("No DNS record of the requested type for “%s”"), rrname);
730 else if (status == DNS_ERROR_RCODE_SERVER_FAILURE)
732 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE,
733 _("Temporarily unable to resolve “%s”"), rrname);
735 else
737 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL,
738 _("Error resolving “%s”"), rrname);
741 return NULL;
744 records = NULL;
745 for (rec = results; rec; rec = rec->pNext)
747 if (rec->wType != dnstype)
748 continue;
749 switch (dnstype)
751 case DNS_TYPE_SRV:
752 record = parse_dns_srv (rec);
753 break;
754 case DNS_TYPE_SOA:
755 record = parse_dns_soa (rec);
756 break;
757 case DNS_TYPE_NS:
758 record = parse_dns_ns (rec);
759 break;
760 case DNS_TYPE_MX:
761 record = parse_dns_mx (rec);
762 break;
763 case DNS_TYPE_TEXT:
764 record = parse_dns_txt (rec);
765 break;
766 default:
767 g_warn_if_reached ();
768 record = NULL;
769 break;
771 if (record != NULL)
772 records = g_list_prepend (records, g_variant_ref_sink (record));
775 if (records == NULL)
777 g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND,
778 _("No DNS record of the requested type for “%s”"), rrname);
780 return NULL;
782 else
783 return records;
786 #endif
788 typedef struct {
789 char *rrname;
790 GResolverRecordType record_type;
791 } LookupRecordsData;
793 static void
794 free_lookup_records_data (LookupRecordsData *lrd)
796 g_free (lrd->rrname);
797 g_slice_free (LookupRecordsData, lrd);
800 static void
801 free_records (GList *records)
803 g_list_free_full (records, (GDestroyNotify) g_variant_unref);
806 #if defined(G_OS_UNIX)
807 #ifdef __BIONIC__
808 #define C_IN 1
809 int res_query(const char *, int, int, u_char *, int);
810 #endif
811 #endif
813 static void
814 do_lookup_records (GTask *task,
815 gpointer source_object,
816 gpointer task_data,
817 GCancellable *cancellable)
819 LookupRecordsData *lrd = task_data;
820 GList *records;
821 GError *error = NULL;
823 #if defined(G_OS_UNIX)
824 gint len = 512;
825 gint herr;
826 GByteArray *answer;
827 gint rrtype;
829 rrtype = g_resolver_record_type_to_rrtype (lrd->record_type);
830 answer = g_byte_array_new ();
831 for (;;)
833 g_byte_array_set_size (answer, len * 2);
834 len = res_query (lrd->rrname, C_IN, rrtype, answer->data, answer->len);
836 /* If answer fit in the buffer then we're done */
837 if (len < 0 || len < (gint)answer->len)
838 break;
841 * On overflow some res_query's return the length needed, others
842 * return the full length entered. This code works in either case.
846 herr = h_errno;
847 records = g_resolver_records_from_res_query (lrd->rrname, rrtype, answer->data, len, herr, &error);
848 g_byte_array_free (answer, TRUE);
850 #else
852 DNS_STATUS status;
853 DNS_RECORD *results = NULL;
854 WORD dnstype;
856 dnstype = g_resolver_record_type_to_dnstype (lrd->record_type);
857 status = DnsQuery_A (lrd->rrname, dnstype, DNS_QUERY_STANDARD, NULL, &results, NULL);
858 records = g_resolver_records_from_DnsQuery (lrd->rrname, dnstype, status, results, &error);
859 if (results != NULL)
860 DnsRecordListFree (results, DnsFreeRecordList);
862 #endif
864 if (records)
865 g_task_return_pointer (task, records, (GDestroyNotify) free_records);
866 else
867 g_task_return_error (task, error);
870 static GList *
871 lookup_records (GResolver *resolver,
872 const gchar *rrname,
873 GResolverRecordType record_type,
874 GCancellable *cancellable,
875 GError **error)
877 GTask *task;
878 GList *records;
879 LookupRecordsData *lrd;
881 task = g_task_new (resolver, cancellable, NULL, NULL);
882 g_task_set_source_tag (task, lookup_records);
884 lrd = g_slice_new (LookupRecordsData);
885 lrd->rrname = g_strdup (rrname);
886 lrd->record_type = record_type;
887 g_task_set_task_data (task, lrd, (GDestroyNotify) free_lookup_records_data);
889 g_task_set_return_on_cancel (task, TRUE);
890 g_task_run_in_thread_sync (task, do_lookup_records);
891 records = g_task_propagate_pointer (task, error);
892 g_object_unref (task);
894 return records;
897 static void
898 lookup_records_async (GResolver *resolver,
899 const char *rrname,
900 GResolverRecordType record_type,
901 GCancellable *cancellable,
902 GAsyncReadyCallback callback,
903 gpointer user_data)
905 GTask *task;
906 LookupRecordsData *lrd;
908 task = g_task_new (resolver, cancellable, callback, user_data);
909 g_task_set_source_tag (task, lookup_records_async);
911 lrd = g_slice_new (LookupRecordsData);
912 lrd->rrname = g_strdup (rrname);
913 lrd->record_type = record_type;
914 g_task_set_task_data (task, lrd, (GDestroyNotify) free_lookup_records_data);
916 g_task_set_return_on_cancel (task, TRUE);
917 g_task_run_in_thread (task, do_lookup_records);
918 g_object_unref (task);
921 static GList *
922 lookup_records_finish (GResolver *resolver,
923 GAsyncResult *result,
924 GError **error)
926 g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
928 return g_task_propagate_pointer (G_TASK (result), error);
932 static void
933 g_threaded_resolver_class_init (GThreadedResolverClass *threaded_class)
935 GResolverClass *resolver_class = G_RESOLVER_CLASS (threaded_class);
937 resolver_class->lookup_by_name = lookup_by_name;
938 resolver_class->lookup_by_name_async = lookup_by_name_async;
939 resolver_class->lookup_by_name_finish = lookup_by_name_finish;
940 resolver_class->lookup_by_address = lookup_by_address;
941 resolver_class->lookup_by_address_async = lookup_by_address_async;
942 resolver_class->lookup_by_address_finish = lookup_by_address_finish;
943 resolver_class->lookup_records = lookup_records;
944 resolver_class->lookup_records_async = lookup_records_async;
945 resolver_class->lookup_records_finish = lookup_records_finish;
947 /* Initialize addrinfo_hints */
948 #ifdef AI_ADDRCONFIG
949 addrinfo_hints.ai_flags |= AI_ADDRCONFIG;
950 #endif
951 /* These two don't actually matter, they just get copied into the
952 * returned addrinfo structures (and then we ignore them). But if
953 * we leave them unset, we'll get back duplicate answers.
955 addrinfo_hints.ai_socktype = SOCK_STREAM;
956 addrinfo_hints.ai_protocol = IPPROTO_TCP;