/src/pango/subprojects/glib/gio/gthreadedresolver.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ |
2 | | |
3 | | /* GIO - GLib Input, Output and Streaming Library |
4 | | * |
5 | | * Copyright (C) 2008 Red Hat, Inc. |
6 | | * Copyright (C) 2018 Igalia S.L. |
7 | | * |
8 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
9 | | * |
10 | | * This library is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public |
12 | | * License as published by the Free Software Foundation; either |
13 | | * version 2.1 of the License, or (at your option) any later version. |
14 | | * |
15 | | * This library is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General |
21 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | #include <glib.h> |
26 | | #include "glibintl.h" |
27 | | |
28 | | #include <stdio.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include "glib/glib-private.h" |
32 | | #include "gthreadedresolver.h" |
33 | | #include "gthreadedresolver-private.h" |
34 | | #include "gnetworkingprivate.h" |
35 | | |
36 | | #include "gcancellable.h" |
37 | | #include "ginetaddress.h" |
38 | | #include "ginetsocketaddress.h" |
39 | | #include "gnetworkmonitorbase.h" |
40 | | #include "gtask.h" |
41 | | #include "gsocketaddress.h" |
42 | | #include "gsrvtarget.h" |
43 | | |
44 | | #if HAVE_GETIFADDRS |
45 | | #include <ifaddrs.h> |
46 | | #endif |
47 | | |
48 | | /* |
49 | | * GThreadedResolver is a threaded wrapper around the system libc’s |
50 | | * `getaddrinfo()`. |
51 | | * |
52 | | * It has to be threaded, as `getaddrinfo()` is synchronous. libc does provide |
53 | | * `getaddrinfo_a()` as an asynchronous version of `getaddrinfo()`, but it does |
54 | | * not integrate with a poll loop. It requires use of sigevent to notify of |
55 | | * completion of an asynchronous operation. That either emits a signal, or calls |
56 | | * a callback function in a newly spawned thread. |
57 | | * |
58 | | * A signal (`SIGEV_SIGNAL`) can’t be used for completion as (aside from being |
59 | | * another expensive round trip into the kernel) GLib cannot pick a `SIG*` |
60 | | * number which is guaranteed to not be in use elsewhere in the process. Various |
61 | | * other things could be interfering with signal dispositions, such as gdb or |
62 | | * other libraries in the process. Using a `signalfd()` |
63 | | * [cannot improve this situation](https://ldpreload.com/blog/signalfd-is-useless). |
64 | | * |
65 | | * A callback function in a newly spawned thread (`SIGEV_THREAD`) could be used, |
66 | | * but that is very expensive. Internally, glibc currently also just implements |
67 | | * `getaddrinfo_a()` |
68 | | * [using its own thread pool](https://github.com/bminor/glibc/blob/master/resolv/gai_misc.c), |
69 | | * and then |
70 | | * [spawns an additional thread for each completion callback](https://github.com/bminor/glibc/blob/master/resolv/gai_notify.c). |
71 | | * That is very expensive. |
72 | | * |
73 | | * No other appropriate sigevent callback types |
74 | | * [currently exist](https://sourceware.org/bugzilla/show_bug.cgi?id=30287), and |
75 | | * [others agree that sigevent is not great](http://davmac.org/davpage/linux/async-io.html#posixaio). |
76 | | * |
77 | | * Hence, #GThreadedResolver calls the normal synchronous `getaddrinfo()` in its |
78 | | * own thread pool. Previously, #GThreadedResolver used the thread pool which is |
79 | | * internal to #GTask by calling g_task_run_in_thread(). That lead to exhaustion |
80 | | * of the #GTask thread pool in some situations, though, as DNS lookups are |
81 | | * quite frequent leaf operations in some use cases. Now, #GThreadedResolver |
82 | | * uses its own private thread pool. |
83 | | * |
84 | | * This is similar to what |
85 | | * [libasyncns](http://git.0pointer.net/libasyncns.git/tree/libasyncns/asyncns.h) |
86 | | * and other multi-threaded users of `getaddrinfo()` do. |
87 | | */ |
88 | | |
89 | | struct _GThreadedResolver |
90 | | { |
91 | | GResolver parent_instance; |
92 | | |
93 | | GThreadPool *thread_pool; /* (owned) */ |
94 | | |
95 | | GMutex interface_mutex; |
96 | | GNetworkMonitor *network_monitor; /* (owned) */ |
97 | | gboolean monitor_supports_caching; |
98 | | int network_is_loopback_only; |
99 | | }; |
100 | | |
101 | | G_DEFINE_TYPE (GThreadedResolver, g_threaded_resolver, G_TYPE_RESOLVER) |
102 | | |
103 | | static void run_task_in_thread_pool_async (GThreadedResolver *self, |
104 | | GTask *task); |
105 | | static void run_task_in_thread_pool_sync (GThreadedResolver *self, |
106 | | GTask *task); |
107 | | static void threaded_resolver_worker_cb (gpointer task_data, |
108 | | gpointer user_data); |
109 | | |
110 | | static void |
111 | | g_threaded_resolver_init (GThreadedResolver *self) |
112 | 0 | { |
113 | 0 | self->thread_pool = g_thread_pool_new_full (threaded_resolver_worker_cb, |
114 | 0 | self, |
115 | 0 | (GDestroyNotify) g_object_unref, |
116 | 0 | 20, |
117 | 0 | FALSE, |
118 | 0 | NULL); |
119 | |
|
120 | 0 | self->network_is_loopback_only = -1; |
121 | |
|
122 | 0 | g_mutex_init (&self->interface_mutex); |
123 | 0 | } |
124 | | |
125 | | static void |
126 | | g_threaded_resolver_finalize (GObject *object) |
127 | 0 | { |
128 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (object); |
129 | |
|
130 | 0 | g_thread_pool_free (self->thread_pool, TRUE, FALSE); |
131 | 0 | self->thread_pool = NULL; |
132 | |
|
133 | 0 | if (self->network_monitor) |
134 | 0 | g_signal_handlers_disconnect_by_data (self->network_monitor, object); |
135 | |
|
136 | 0 | g_clear_object (&self->network_monitor); |
137 | 0 | g_mutex_clear (&self->interface_mutex); |
138 | |
|
139 | 0 | G_OBJECT_CLASS (g_threaded_resolver_parent_class)->finalize (object); |
140 | 0 | } |
141 | | |
142 | | static GResolverError |
143 | | g_resolver_error_from_addrinfo_error (gint err) |
144 | 0 | { |
145 | 0 | switch (err) |
146 | 0 | { |
147 | 0 | case EAI_FAIL: |
148 | 0 | #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME) |
149 | 0 | case EAI_NODATA: |
150 | 0 | #endif |
151 | 0 | case EAI_NONAME: |
152 | 0 | return G_RESOLVER_ERROR_NOT_FOUND; |
153 | | |
154 | 0 | case EAI_AGAIN: |
155 | 0 | return G_RESOLVER_ERROR_TEMPORARY_FAILURE; |
156 | | |
157 | 0 | default: |
158 | 0 | return G_RESOLVER_ERROR_INTERNAL; |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | typedef struct { |
163 | | enum { |
164 | | LOOKUP_BY_NAME, |
165 | | LOOKUP_BY_ADDRESS, |
166 | | LOOKUP_RECORDS, |
167 | | } lookup_type; |
168 | | |
169 | | union { |
170 | | struct { |
171 | | char *hostname; |
172 | | int address_family; |
173 | | } lookup_by_name; |
174 | | struct { |
175 | | GInetAddress *address; /* (owned) */ |
176 | | } lookup_by_address; |
177 | | struct { |
178 | | char *rrname; |
179 | | GResolverRecordType record_type; |
180 | | } lookup_records; |
181 | | }; |
182 | | |
183 | | GCond cond; /* used for signalling completion of the task when running it sync */ |
184 | | GMutex lock; |
185 | | |
186 | | GSource *timeout_source; /* (nullable) (owned) */ |
187 | | GSource *cancellable_source; /* (nullable) (owned) */ |
188 | | |
189 | | /* This enum indicates that a particular code path has claimed the |
190 | | * task and is shortly about to call g_task_return_*() on it. |
191 | | * This must be accessed with GThreadedResolver.lock held. */ |
192 | | enum |
193 | | { |
194 | | NOT_YET, |
195 | | COMPLETED, /* libc lookup call has completed successfully or errored */ |
196 | | TIMED_OUT, |
197 | | CANCELLED, |
198 | | } will_return; |
199 | | |
200 | | /* Whether the thread pool thread executing this lookup has finished executing |
201 | | * it and g_task_return_*() has been called on it already. |
202 | | * This must be accessed with GThreadedResolver.lock held. */ |
203 | | gboolean has_returned; |
204 | | } LookupData; |
205 | | |
206 | | static LookupData * |
207 | | lookup_data_new_by_name (const char *hostname, |
208 | | int address_family) |
209 | 0 | { |
210 | 0 | LookupData *data = g_new0 (LookupData, 1); |
211 | 0 | data->lookup_type = LOOKUP_BY_NAME; |
212 | 0 | g_cond_init (&data->cond); |
213 | 0 | g_mutex_init (&data->lock); |
214 | 0 | data->lookup_by_name.hostname = g_strdup (hostname); |
215 | 0 | data->lookup_by_name.address_family = address_family; |
216 | 0 | return g_steal_pointer (&data); |
217 | 0 | } |
218 | | |
219 | | static LookupData * |
220 | | lookup_data_new_by_address (GInetAddress *address) |
221 | 0 | { |
222 | 0 | LookupData *data = g_new0 (LookupData, 1); |
223 | 0 | data->lookup_type = LOOKUP_BY_ADDRESS; |
224 | 0 | g_cond_init (&data->cond); |
225 | 0 | g_mutex_init (&data->lock); |
226 | 0 | data->lookup_by_address.address = g_object_ref (address); |
227 | 0 | return g_steal_pointer (&data); |
228 | 0 | } |
229 | | |
230 | | static LookupData * |
231 | | lookup_data_new_records (const gchar *rrname, |
232 | | GResolverRecordType record_type) |
233 | 0 | { |
234 | 0 | LookupData *data = g_new0 (LookupData, 1); |
235 | 0 | data->lookup_type = LOOKUP_RECORDS; |
236 | 0 | g_cond_init (&data->cond); |
237 | 0 | g_mutex_init (&data->lock); |
238 | 0 | data->lookup_records.rrname = g_strdup (rrname); |
239 | 0 | data->lookup_records.record_type = record_type; |
240 | 0 | return g_steal_pointer (&data); |
241 | 0 | } |
242 | | |
243 | | static void |
244 | | lookup_data_free (LookupData *data) |
245 | 0 | { |
246 | 0 | switch (data->lookup_type) { |
247 | 0 | case LOOKUP_BY_NAME: |
248 | 0 | g_free (data->lookup_by_name.hostname); |
249 | 0 | break; |
250 | 0 | case LOOKUP_BY_ADDRESS: |
251 | 0 | g_clear_object (&data->lookup_by_address.address); |
252 | 0 | break; |
253 | 0 | case LOOKUP_RECORDS: |
254 | 0 | g_free (data->lookup_records.rrname); |
255 | 0 | break; |
256 | 0 | default: |
257 | 0 | g_assert_not_reached (); |
258 | 0 | } |
259 | | |
260 | 0 | if (data->timeout_source != NULL) |
261 | 0 | { |
262 | 0 | g_source_destroy (data->timeout_source); |
263 | 0 | g_clear_pointer (&data->timeout_source, g_source_unref); |
264 | 0 | } |
265 | |
|
266 | 0 | if (data->cancellable_source != NULL) |
267 | 0 | { |
268 | 0 | g_source_destroy (data->cancellable_source); |
269 | 0 | g_clear_pointer (&data->cancellable_source, g_source_unref); |
270 | 0 | } |
271 | |
|
272 | 0 | g_mutex_clear (&data->lock); |
273 | 0 | g_cond_clear (&data->cond); |
274 | |
|
275 | 0 | g_free (data); |
276 | 0 | } |
277 | | |
278 | | static gboolean |
279 | | check_only_has_loopback_interfaces (void) |
280 | 0 | { |
281 | 0 | #if HAVE_GETIFADDRS |
282 | 0 | struct ifaddrs *addrs; |
283 | 0 | gboolean only_loopback = TRUE; |
284 | |
|
285 | 0 | if (getifaddrs (&addrs) != 0) |
286 | 0 | { |
287 | 0 | int saved_errno = errno; |
288 | 0 | g_debug ("getifaddrs() failed: %s", g_strerror (saved_errno)); |
289 | 0 | return FALSE; |
290 | 0 | } |
291 | | |
292 | 0 | for (struct ifaddrs *addr = addrs; addr; addr = addr->ifa_next) |
293 | 0 | { |
294 | 0 | struct sockaddr *sa = addr->ifa_addr; |
295 | 0 | GSocketAddress *saddr; |
296 | 0 | if (!sa) |
297 | 0 | continue; |
298 | | |
299 | 0 | saddr = g_socket_address_new_from_native (sa, sizeof (struct sockaddr)); |
300 | 0 | if (!saddr) |
301 | 0 | continue; |
302 | | |
303 | 0 | if (!G_IS_INET_SOCKET_ADDRESS (saddr)) |
304 | 0 | { |
305 | 0 | g_object_unref (saddr); |
306 | 0 | continue; |
307 | 0 | } |
308 | | |
309 | 0 | GInetAddress *inetaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (saddr)); |
310 | 0 | if (!g_inet_address_get_is_loopback (inetaddr)) |
311 | 0 | { |
312 | 0 | only_loopback = FALSE; |
313 | 0 | g_object_unref (saddr); |
314 | 0 | break; |
315 | 0 | } |
316 | | |
317 | 0 | g_object_unref (saddr); |
318 | 0 | } |
319 | |
|
320 | 0 | freeifaddrs (addrs); |
321 | 0 | return only_loopback; |
322 | | #else /* FIXME: Check GetAdaptersAddresses() on win32. */ |
323 | | return FALSE; |
324 | | #endif |
325 | 0 | } |
326 | | |
327 | | static void |
328 | | network_changed_cb (GNetworkMonitor *monitor, |
329 | | gboolean network_available, |
330 | | GThreadedResolver *resolver) |
331 | 0 | { |
332 | 0 | g_mutex_lock (&resolver->interface_mutex); |
333 | 0 | resolver->network_is_loopback_only = -1; |
334 | 0 | g_mutex_unlock (&resolver->interface_mutex); |
335 | 0 | } |
336 | | |
337 | | static gboolean |
338 | | only_has_loopback_interfaces_cached (GThreadedResolver *resolver) |
339 | 0 | { |
340 | 0 | g_mutex_lock (&resolver->interface_mutex); |
341 | |
|
342 | 0 | if (!resolver->network_monitor) |
343 | 0 | { |
344 | 0 | resolver->network_monitor = g_object_ref (g_network_monitor_get_default ()); |
345 | 0 | resolver->monitor_supports_caching = G_TYPE_FROM_INSTANCE (resolver->network_monitor) != G_TYPE_NETWORK_MONITOR_BASE; |
346 | 0 | g_signal_connect_object (resolver->network_monitor, "network-changed", G_CALLBACK (network_changed_cb), resolver, G_CONNECT_DEFAULT); |
347 | 0 | } |
348 | |
|
349 | 0 | if (!resolver->monitor_supports_caching || resolver->network_is_loopback_only == -1) |
350 | 0 | resolver->network_is_loopback_only = check_only_has_loopback_interfaces (); |
351 | |
|
352 | 0 | g_mutex_unlock (&resolver->interface_mutex); |
353 | |
|
354 | 0 | return resolver->network_is_loopback_only; |
355 | 0 | } |
356 | | |
357 | | static GList * |
358 | | do_lookup_by_name (GThreadedResolver *resolver, |
359 | | const gchar *hostname, |
360 | | int address_family, |
361 | | GCancellable *cancellable, |
362 | | GError **error) |
363 | 0 | { |
364 | 0 | struct addrinfo *res = NULL; |
365 | 0 | GList *addresses; |
366 | 0 | gint retval; |
367 | 0 | struct addrinfo addrinfo_hints = { 0 }; |
368 | | |
369 | | /* In general we only want IPs for valid interfaces. |
370 | | * However this will return nothing if you only have loopback interfaces. |
371 | | * Instead in this case we will manually filter out invalid IPs. */ |
372 | 0 | gboolean only_loopback = only_has_loopback_interfaces_cached (resolver); |
373 | |
|
374 | 0 | #ifdef AI_ADDRCONFIG |
375 | 0 | if (!only_loopback) |
376 | 0 | addrinfo_hints.ai_flags = AI_ADDRCONFIG; |
377 | 0 | #endif |
378 | | /* socktype and protocol don't actually matter, they just get copied into the |
379 | | * returned addrinfo structures (and then we ignore them). But if |
380 | | * we leave them unset, we'll get back duplicate answers. |
381 | | */ |
382 | 0 | addrinfo_hints.ai_socktype = SOCK_STREAM; |
383 | 0 | addrinfo_hints.ai_protocol = IPPROTO_TCP; |
384 | |
|
385 | 0 | addrinfo_hints.ai_family = address_family; |
386 | 0 | retval = getaddrinfo (hostname, NULL, &addrinfo_hints, &res); |
387 | |
|
388 | 0 | if (retval == 0) |
389 | 0 | { |
390 | 0 | addresses = NULL; |
391 | 0 | for (struct addrinfo *ai = res; ai; ai = ai->ai_next) |
392 | 0 | { |
393 | 0 | GInetAddress *addr; |
394 | 0 | GSocketAddress *sockaddr = g_socket_address_new_from_native (ai->ai_addr, ai->ai_addrlen); |
395 | |
|
396 | 0 | if (!sockaddr) |
397 | 0 | continue; |
398 | 0 | if (!G_IS_INET_SOCKET_ADDRESS (sockaddr)) |
399 | 0 | { |
400 | 0 | g_clear_object (&sockaddr); |
401 | 0 | continue; |
402 | 0 | } |
403 | | |
404 | 0 | addr = g_inet_socket_address_get_address ((GInetSocketAddress *) sockaddr); |
405 | 0 | if (only_loopback && !g_inet_address_get_is_loopback (addr)) |
406 | 0 | { |
407 | 0 | g_object_unref (sockaddr); |
408 | 0 | continue; |
409 | 0 | } |
410 | | |
411 | 0 | addresses = g_list_prepend (addresses, g_object_ref (addr)); |
412 | 0 | g_object_unref (sockaddr); |
413 | 0 | } |
414 | |
|
415 | 0 | g_clear_pointer (&res, freeaddrinfo); |
416 | |
|
417 | 0 | if (addresses != NULL) |
418 | 0 | { |
419 | 0 | addresses = g_list_reverse (addresses); |
420 | 0 | return g_steal_pointer (&addresses); |
421 | 0 | } |
422 | 0 | else |
423 | 0 | { |
424 | | /* All addresses failed to be converted to GSocketAddresses. */ |
425 | 0 | g_set_error (error, |
426 | 0 | G_RESOLVER_ERROR, |
427 | 0 | G_RESOLVER_ERROR_NOT_FOUND, |
428 | 0 | _("Error resolving “%s”: %s"), |
429 | 0 | hostname, |
430 | 0 | _("No valid addresses were found")); |
431 | 0 | return NULL; |
432 | 0 | } |
433 | 0 | } |
434 | 0 | else |
435 | 0 | { |
436 | | #ifdef G_OS_WIN32 |
437 | | gchar *error_message = g_win32_error_message (WSAGetLastError ()); |
438 | | #else |
439 | 0 | gchar *error_message = g_locale_to_utf8 (gai_strerror (retval), -1, NULL, NULL, NULL); |
440 | 0 | if (error_message == NULL) |
441 | 0 | error_message = g_strdup ("[Invalid UTF-8]"); |
442 | 0 | #endif |
443 | |
|
444 | 0 | g_clear_pointer (&res, freeaddrinfo); |
445 | |
|
446 | 0 | g_set_error (error, |
447 | 0 | G_RESOLVER_ERROR, |
448 | 0 | g_resolver_error_from_addrinfo_error (retval), |
449 | 0 | _("Error resolving “%s”: %s"), |
450 | 0 | hostname, error_message); |
451 | 0 | g_free (error_message); |
452 | |
|
453 | 0 | return NULL; |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | | static GList * |
458 | | lookup_by_name (GResolver *resolver, |
459 | | const gchar *hostname, |
460 | | GCancellable *cancellable, |
461 | | GError **error) |
462 | 0 | { |
463 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
464 | 0 | GTask *task; |
465 | 0 | GList *addresses; |
466 | 0 | LookupData *data; |
467 | |
|
468 | 0 | data = lookup_data_new_by_name (hostname, AF_UNSPEC); |
469 | 0 | task = g_task_new (resolver, cancellable, NULL, NULL); |
470 | 0 | g_task_set_source_tag (task, lookup_by_name); |
471 | 0 | g_task_set_name (task, "[gio] resolver lookup"); |
472 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
473 | |
|
474 | 0 | run_task_in_thread_pool_sync (self, task); |
475 | |
|
476 | 0 | addresses = g_task_propagate_pointer (task, error); |
477 | 0 | g_object_unref (task); |
478 | |
|
479 | 0 | return addresses; |
480 | 0 | } |
481 | | |
482 | | static int |
483 | | flags_to_family (GResolverNameLookupFlags flags) |
484 | 0 | { |
485 | 0 | int address_family = AF_UNSPEC; |
486 | |
|
487 | 0 | if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY) |
488 | 0 | address_family = AF_INET; |
489 | |
|
490 | 0 | if (flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY) |
491 | 0 | { |
492 | 0 | address_family = AF_INET6; |
493 | | /* You can only filter by one family at a time */ |
494 | 0 | g_return_val_if_fail (!(flags & G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY), address_family); |
495 | 0 | } |
496 | | |
497 | 0 | return address_family; |
498 | 0 | } |
499 | | |
500 | | static GList * |
501 | | lookup_by_name_with_flags (GResolver *resolver, |
502 | | const gchar *hostname, |
503 | | GResolverNameLookupFlags flags, |
504 | | GCancellable *cancellable, |
505 | | GError **error) |
506 | 0 | { |
507 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
508 | 0 | GTask *task; |
509 | 0 | GList *addresses; |
510 | 0 | LookupData *data; |
511 | |
|
512 | 0 | data = lookup_data_new_by_name (hostname, flags_to_family (flags)); |
513 | 0 | task = g_task_new (resolver, cancellable, NULL, NULL); |
514 | 0 | g_task_set_source_tag (task, lookup_by_name_with_flags); |
515 | 0 | g_task_set_name (task, "[gio] resolver lookup"); |
516 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
517 | |
|
518 | 0 | run_task_in_thread_pool_sync (self, task); |
519 | |
|
520 | 0 | addresses = g_task_propagate_pointer (task, error); |
521 | 0 | g_object_unref (task); |
522 | |
|
523 | 0 | return addresses; |
524 | 0 | } |
525 | | |
526 | | static void |
527 | | lookup_by_name_with_flags_async (GResolver *resolver, |
528 | | const gchar *hostname, |
529 | | GResolverNameLookupFlags flags, |
530 | | GCancellable *cancellable, |
531 | | GAsyncReadyCallback callback, |
532 | | gpointer user_data) |
533 | 0 | { |
534 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
535 | 0 | GTask *task; |
536 | 0 | LookupData *data; |
537 | |
|
538 | 0 | data = lookup_data_new_by_name (hostname, flags_to_family (flags)); |
539 | 0 | task = g_task_new (resolver, cancellable, callback, user_data); |
540 | |
|
541 | 0 | g_debug ("%s: starting new lookup for %s with GTask %p, LookupData %p", |
542 | 0 | G_STRFUNC, hostname, task, data); |
543 | |
|
544 | 0 | g_task_set_source_tag (task, lookup_by_name_with_flags_async); |
545 | 0 | g_task_set_name (task, "[gio] resolver lookup"); |
546 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
547 | |
|
548 | 0 | run_task_in_thread_pool_async (self, task); |
549 | |
|
550 | 0 | g_object_unref (task); |
551 | 0 | } |
552 | | |
553 | | static void |
554 | | lookup_by_name_async (GResolver *resolver, |
555 | | const gchar *hostname, |
556 | | GCancellable *cancellable, |
557 | | GAsyncReadyCallback callback, |
558 | | gpointer user_data) |
559 | 0 | { |
560 | 0 | lookup_by_name_with_flags_async (resolver, |
561 | 0 | hostname, |
562 | 0 | G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT, |
563 | 0 | cancellable, |
564 | 0 | callback, |
565 | 0 | user_data); |
566 | 0 | } |
567 | | |
568 | | static GList * |
569 | | lookup_by_name_finish (GResolver *resolver, |
570 | | GAsyncResult *result, |
571 | | GError **error) |
572 | 0 | { |
573 | 0 | g_return_val_if_fail (g_task_is_valid (result, resolver), NULL); |
574 | | |
575 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
576 | 0 | } |
577 | | |
578 | | static GList * |
579 | | lookup_by_name_with_flags_finish (GResolver *resolver, |
580 | | GAsyncResult *result, |
581 | | GError **error) |
582 | 0 | { |
583 | 0 | g_return_val_if_fail (g_task_is_valid (result, resolver), NULL); |
584 | | |
585 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
586 | 0 | } |
587 | | |
588 | | static gchar * |
589 | | do_lookup_by_address (GInetAddress *address, |
590 | | GCancellable *cancellable, |
591 | | GError **error) |
592 | 0 | { |
593 | 0 | struct sockaddr_storage sockaddr_address; |
594 | 0 | gsize sockaddr_address_size; |
595 | 0 | GSocketAddress *gsockaddr; |
596 | 0 | gchar name[NI_MAXHOST]; |
597 | 0 | gint retval; |
598 | |
|
599 | 0 | gsockaddr = g_inet_socket_address_new (address, 0); |
600 | 0 | g_socket_address_to_native (gsockaddr, (struct sockaddr *)&sockaddr_address, |
601 | 0 | sizeof (sockaddr_address), NULL); |
602 | 0 | sockaddr_address_size = g_socket_address_get_native_size (gsockaddr); |
603 | 0 | g_object_unref (gsockaddr); |
604 | |
|
605 | 0 | retval = getnameinfo ((struct sockaddr *) &sockaddr_address, sockaddr_address_size, |
606 | 0 | name, sizeof (name), NULL, 0, NI_NAMEREQD); |
607 | 0 | if (retval == 0) |
608 | 0 | return g_strdup (name); |
609 | 0 | else |
610 | 0 | { |
611 | 0 | gchar *phys; |
612 | |
|
613 | | #ifdef G_OS_WIN32 |
614 | | gchar *error_message = g_win32_error_message (WSAGetLastError ()); |
615 | | #else |
616 | 0 | gchar *error_message = g_locale_to_utf8 (gai_strerror (retval), -1, NULL, NULL, NULL); |
617 | 0 | if (error_message == NULL) |
618 | 0 | error_message = g_strdup ("[Invalid UTF-8]"); |
619 | 0 | #endif |
620 | |
|
621 | 0 | phys = g_inet_address_to_string (address); |
622 | 0 | g_set_error (error, |
623 | 0 | G_RESOLVER_ERROR, |
624 | 0 | g_resolver_error_from_addrinfo_error (retval), |
625 | 0 | _("Error reverse-resolving “%s”: %s"), |
626 | 0 | phys ? phys : "(unknown)", |
627 | 0 | error_message); |
628 | 0 | g_free (phys); |
629 | 0 | g_free (error_message); |
630 | |
|
631 | 0 | return NULL; |
632 | 0 | } |
633 | 0 | } |
634 | | |
635 | | static gchar * |
636 | | lookup_by_address (GResolver *resolver, |
637 | | GInetAddress *address, |
638 | | GCancellable *cancellable, |
639 | | GError **error) |
640 | 0 | { |
641 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
642 | 0 | LookupData *data = NULL; |
643 | 0 | GTask *task; |
644 | 0 | gchar *name; |
645 | |
|
646 | 0 | data = lookup_data_new_by_address (address); |
647 | 0 | task = g_task_new (resolver, cancellable, NULL, NULL); |
648 | 0 | g_task_set_source_tag (task, lookup_by_address); |
649 | 0 | g_task_set_name (task, "[gio] resolver lookup"); |
650 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
651 | |
|
652 | 0 | run_task_in_thread_pool_sync (self, task); |
653 | |
|
654 | 0 | name = g_task_propagate_pointer (task, error); |
655 | 0 | g_object_unref (task); |
656 | |
|
657 | 0 | return name; |
658 | 0 | } |
659 | | |
660 | | static void |
661 | | lookup_by_address_async (GResolver *resolver, |
662 | | GInetAddress *address, |
663 | | GCancellable *cancellable, |
664 | | GAsyncReadyCallback callback, |
665 | | gpointer user_data) |
666 | 0 | { |
667 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
668 | 0 | LookupData *data = NULL; |
669 | 0 | GTask *task; |
670 | |
|
671 | 0 | data = lookup_data_new_by_address (address); |
672 | 0 | task = g_task_new (resolver, cancellable, callback, user_data); |
673 | 0 | g_task_set_source_tag (task, lookup_by_address_async); |
674 | 0 | g_task_set_name (task, "[gio] resolver lookup"); |
675 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
676 | |
|
677 | 0 | run_task_in_thread_pool_async (self, task); |
678 | |
|
679 | 0 | g_object_unref (task); |
680 | 0 | } |
681 | | |
682 | | static gchar * |
683 | | lookup_by_address_finish (GResolver *resolver, |
684 | | GAsyncResult *result, |
685 | | GError **error) |
686 | 0 | { |
687 | 0 | g_return_val_if_fail (g_task_is_valid (result, resolver), NULL); |
688 | | |
689 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
690 | 0 | } |
691 | | |
692 | | |
693 | | #if defined(G_OS_UNIX) |
694 | | |
695 | | #if defined __BIONIC__ && !defined BIND_4_COMPAT |
696 | | /* Copy from bionic/libc/private/arpa_nameser_compat.h |
697 | | * and bionic/libc/private/arpa_nameser.h */ |
698 | | typedef struct { |
699 | | unsigned id :16; /* query identification number */ |
700 | | #if BYTE_ORDER == BIG_ENDIAN |
701 | | /* fields in third byte */ |
702 | | unsigned qr: 1; /* response flag */ |
703 | | unsigned opcode: 4; /* purpose of message */ |
704 | | unsigned aa: 1; /* authoritative answer */ |
705 | | unsigned tc: 1; /* truncated message */ |
706 | | unsigned rd: 1; /* recursion desired */ |
707 | | /* fields in fourth byte */ |
708 | | unsigned ra: 1; /* recursion available */ |
709 | | unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */ |
710 | | unsigned ad: 1; /* authentic data from named */ |
711 | | unsigned cd: 1; /* checking disabled by resolver */ |
712 | | unsigned rcode :4; /* response code */ |
713 | | #endif |
714 | | #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN |
715 | | /* fields in third byte */ |
716 | | unsigned rd :1; /* recursion desired */ |
717 | | unsigned tc :1; /* truncated message */ |
718 | | unsigned aa :1; /* authoritative answer */ |
719 | | unsigned opcode :4; /* purpose of message */ |
720 | | unsigned qr :1; /* response flag */ |
721 | | /* fields in fourth byte */ |
722 | | unsigned rcode :4; /* response code */ |
723 | | unsigned cd: 1; /* checking disabled by resolver */ |
724 | | unsigned ad: 1; /* authentic data from named */ |
725 | | unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */ |
726 | | unsigned ra :1; /* recursion available */ |
727 | | #endif |
728 | | /* remaining bytes */ |
729 | | unsigned qdcount :16; /* number of question entries */ |
730 | | unsigned ancount :16; /* number of answer entries */ |
731 | | unsigned nscount :16; /* number of authority entries */ |
732 | | unsigned arcount :16; /* number of resource entries */ |
733 | | } HEADER; |
734 | | |
735 | | #define NS_INT32SZ 4 /* #/bytes of data in a uint32_t */ |
736 | | #define NS_INT16SZ 2 /* #/bytes of data in a uint16_t */ |
737 | | |
738 | | #define NS_GET16(s, cp) do { \ |
739 | | const u_char *t_cp = (const u_char *)(cp); \ |
740 | | (s) = ((uint16_t)t_cp[0] << 8) \ |
741 | | | ((uint16_t)t_cp[1]) \ |
742 | | ; \ |
743 | | (cp) += NS_INT16SZ; \ |
744 | | } while (/*CONSTCOND*/0) |
745 | | |
746 | | #define NS_GET32(l, cp) do { \ |
747 | | const u_char *t_cp = (const u_char *)(cp); \ |
748 | | (l) = ((uint32_t)t_cp[0] << 24) \ |
749 | | | ((uint32_t)t_cp[1] << 16) \ |
750 | | | ((uint32_t)t_cp[2] << 8) \ |
751 | | | ((uint32_t)t_cp[3]) \ |
752 | | ; \ |
753 | | (cp) += NS_INT32SZ; \ |
754 | | } while (/*CONSTCOND*/0) |
755 | | |
756 | | #define GETSHORT NS_GET16 |
757 | | #define GETLONG NS_GET32 |
758 | | |
759 | | #define C_IN 1 |
760 | | |
761 | | /* From bionic/libc/private/resolv_private.h */ |
762 | | int dn_expand(const u_char *, const u_char *, const u_char *, char *, int); |
763 | | #define dn_skipname __dn_skipname |
764 | | int dn_skipname(const u_char *, const u_char *); |
765 | | |
766 | | /* From bionic/libc/private/arpa_nameser_compat.h */ |
767 | | #define T_MX ns_t_mx |
768 | | #define T_TXT ns_t_txt |
769 | | #define T_SOA ns_t_soa |
770 | | #define T_NS ns_t_ns |
771 | | |
772 | | /* From bionic/libc/private/arpa_nameser.h */ |
773 | | typedef enum __ns_type { |
774 | | ns_t_invalid = 0, /* Cookie. */ |
775 | | ns_t_a = 1, /* Host address. */ |
776 | | ns_t_ns = 2, /* Authoritative server. */ |
777 | | ns_t_md = 3, /* Mail destination. */ |
778 | | ns_t_mf = 4, /* Mail forwarder. */ |
779 | | ns_t_cname = 5, /* Canonical name. */ |
780 | | ns_t_soa = 6, /* Start of authority zone. */ |
781 | | ns_t_mb = 7, /* Mailbox domain name. */ |
782 | | ns_t_mg = 8, /* Mail group member. */ |
783 | | ns_t_mr = 9, /* Mail rename name. */ |
784 | | ns_t_null = 10, /* Null resource record. */ |
785 | | ns_t_wks = 11, /* Well known service. */ |
786 | | ns_t_ptr = 12, /* Domain name pointer. */ |
787 | | ns_t_hinfo = 13, /* Host information. */ |
788 | | ns_t_minfo = 14, /* Mailbox information. */ |
789 | | ns_t_mx = 15, /* Mail routing information. */ |
790 | | ns_t_txt = 16, /* Text strings. */ |
791 | | ns_t_rp = 17, /* Responsible person. */ |
792 | | ns_t_afsdb = 18, /* AFS cell database. */ |
793 | | ns_t_x25 = 19, /* X_25 calling address. */ |
794 | | ns_t_isdn = 20, /* ISDN calling address. */ |
795 | | ns_t_rt = 21, /* Router. */ |
796 | | ns_t_nsap = 22, /* NSAP address. */ |
797 | | ns_t_nsap_ptr = 23, /* Reverse NSAP lookup (deprecated). */ |
798 | | ns_t_sig = 24, /* Security signature. */ |
799 | | ns_t_key = 25, /* Security key. */ |
800 | | ns_t_px = 26, /* X.400 mail mapping. */ |
801 | | ns_t_gpos = 27, /* Geographical position (withdrawn). */ |
802 | | ns_t_aaaa = 28, /* Ip6 Address. */ |
803 | | ns_t_loc = 29, /* Location Information. */ |
804 | | ns_t_nxt = 30, /* Next domain (security). */ |
805 | | ns_t_eid = 31, /* Endpoint identifier. */ |
806 | | ns_t_nimloc = 32, /* Nimrod Locator. */ |
807 | | ns_t_srv = 33, /* Server Selection. */ |
808 | | ns_t_atma = 34, /* ATM Address */ |
809 | | ns_t_naptr = 35, /* Naming Authority PoinTeR */ |
810 | | ns_t_kx = 36, /* Key Exchange */ |
811 | | ns_t_cert = 37, /* Certification record */ |
812 | | ns_t_a6 = 38, /* IPv6 address (deprecates AAAA) */ |
813 | | ns_t_dname = 39, /* Non-terminal DNAME (for IPv6) */ |
814 | | ns_t_sink = 40, /* Kitchen sink (experimental) */ |
815 | | ns_t_opt = 41, /* EDNS0 option (meta-RR) */ |
816 | | ns_t_apl = 42, /* Address prefix list (RFC 3123) */ |
817 | | ns_t_tkey = 249, /* Transaction key */ |
818 | | ns_t_tsig = 250, /* Transaction signature. */ |
819 | | ns_t_ixfr = 251, /* Incremental zone transfer. */ |
820 | | ns_t_axfr = 252, /* Transfer zone of authority. */ |
821 | | ns_t_mailb = 253, /* Transfer mailbox records. */ |
822 | | ns_t_maila = 254, /* Transfer mail agent records. */ |
823 | | ns_t_any = 255, /* Wildcard match. */ |
824 | | ns_t_zxfr = 256, /* BIND-specific, nonstandard. */ |
825 | | ns_t_max = 65536 |
826 | | } ns_type; |
827 | | |
828 | | #endif /* __BIONIC__ */ |
829 | | |
830 | | /* Wrapper around dn_expand() which does associated length checks and returns |
831 | | * errors as #GError. */ |
832 | | static gboolean |
833 | | expand_name (const gchar *rrname, |
834 | | const guint8 *answer, |
835 | | const guint8 *end, |
836 | | const guint8 **p, |
837 | | gchar *namebuf, |
838 | | gsize namebuf_len, |
839 | | GError **error) |
840 | 0 | { |
841 | 0 | int expand_result; |
842 | |
|
843 | 0 | expand_result = dn_expand (answer, end, *p, namebuf, namebuf_len); |
844 | 0 | if (expand_result < 0 || end - *p < expand_result) |
845 | 0 | { |
846 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
847 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
848 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), rrname); |
849 | 0 | return FALSE; |
850 | 0 | } |
851 | | |
852 | 0 | *p += expand_result; |
853 | |
|
854 | 0 | return TRUE; |
855 | 0 | } |
856 | | |
857 | | static GVariant * |
858 | | parse_res_srv (const guint8 *answer, |
859 | | const guint8 *end, |
860 | | const guint8 **p, |
861 | | GError **error) |
862 | 0 | { |
863 | 0 | gchar namebuf[1024]; |
864 | 0 | guint16 priority, weight, port; |
865 | |
|
866 | 0 | if (end - *p < 6) |
867 | 0 | { |
868 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
869 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
870 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), "SRV"); |
871 | 0 | return NULL; |
872 | 0 | } |
873 | | |
874 | 0 | GETSHORT (priority, *p); |
875 | 0 | GETSHORT (weight, *p); |
876 | 0 | GETSHORT (port, *p); |
877 | | |
878 | | /* RFC 2782 says (on page 4) that “Unless and until permitted by future |
879 | | * standards action, name compression is not to be used for this field.”, so |
880 | | * technically we shouldn’t be expanding names here for SRV records. |
881 | | * |
882 | | * However, other DNS resolvers (such as systemd[1]) do, and it seems in |
883 | | * keeping with the principle of being liberal in what you accept and strict |
884 | | * in what you emit. It also seems harmless. |
885 | | * |
886 | | * An earlier version of the RFC, RFC 2052 (now obsolete) specified that name |
887 | | * compression *was* to be used for SRV targets[2]. |
888 | | * |
889 | | * See discussion on https://gitlab.gnome.org/GNOME/glib/-/issues/2622. |
890 | | * |
891 | | * [1]: https://github.com/yuwata/systemd/blob/2d23cc3c07c49722ce93170737b3efd2692a2d08/src/resolve/resolved-dns-packet.c#L1674 |
892 | | * [2]: https://datatracker.ietf.org/doc/html/rfc2052#page-3 |
893 | | */ |
894 | 0 | if (!expand_name ("SRV", answer, end, p, namebuf, sizeof (namebuf), error)) |
895 | 0 | return NULL; |
896 | | |
897 | 0 | return g_variant_new ("(qqqs)", |
898 | 0 | priority, |
899 | 0 | weight, |
900 | 0 | port, |
901 | 0 | namebuf); |
902 | 0 | } |
903 | | |
904 | | static GVariant * |
905 | | parse_res_soa (const guint8 *answer, |
906 | | const guint8 *end, |
907 | | const guint8 **p, |
908 | | GError **error) |
909 | 0 | { |
910 | 0 | gchar mnamebuf[1024]; |
911 | 0 | gchar rnamebuf[1024]; |
912 | 0 | guint32 serial, refresh, retry, expire, ttl; |
913 | |
|
914 | 0 | if (!expand_name ("SOA", answer, end, p, mnamebuf, sizeof (mnamebuf), error)) |
915 | 0 | return NULL; |
916 | | |
917 | 0 | if (!expand_name ("SOA", answer, end, p, rnamebuf, sizeof (rnamebuf), error)) |
918 | 0 | return NULL; |
919 | | |
920 | 0 | if (end - *p < 20) |
921 | 0 | { |
922 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
923 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
924 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), "SOA"); |
925 | 0 | return NULL; |
926 | 0 | } |
927 | | |
928 | 0 | GETLONG (serial, *p); |
929 | 0 | GETLONG (refresh, *p); |
930 | 0 | GETLONG (retry, *p); |
931 | 0 | GETLONG (expire, *p); |
932 | 0 | GETLONG (ttl, *p); |
933 | |
|
934 | 0 | return g_variant_new ("(ssuuuuu)", |
935 | 0 | mnamebuf, |
936 | 0 | rnamebuf, |
937 | 0 | serial, |
938 | 0 | refresh, |
939 | 0 | retry, |
940 | 0 | expire, |
941 | 0 | ttl); |
942 | 0 | } |
943 | | |
944 | | static GVariant * |
945 | | parse_res_ns (const guint8 *answer, |
946 | | const guint8 *end, |
947 | | const guint8 **p, |
948 | | GError **error) |
949 | 0 | { |
950 | 0 | gchar namebuf[1024]; |
951 | |
|
952 | 0 | if (!expand_name ("NS", answer, end, p, namebuf, sizeof (namebuf), error)) |
953 | 0 | return NULL; |
954 | | |
955 | 0 | return g_variant_new ("(s)", namebuf); |
956 | 0 | } |
957 | | |
958 | | static GVariant * |
959 | | parse_res_mx (const guint8 *answer, |
960 | | const guint8 *end, |
961 | | const guint8 **p, |
962 | | GError **error) |
963 | 0 | { |
964 | 0 | gchar namebuf[1024]; |
965 | 0 | guint16 preference; |
966 | |
|
967 | 0 | if (end - *p < 2) |
968 | 0 | { |
969 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
970 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
971 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), "MX"); |
972 | 0 | return NULL; |
973 | 0 | } |
974 | | |
975 | 0 | GETSHORT (preference, *p); |
976 | |
|
977 | 0 | if (!expand_name ("MX", answer, end, p, namebuf, sizeof (namebuf), error)) |
978 | 0 | return NULL; |
979 | | |
980 | 0 | return g_variant_new ("(qs)", |
981 | 0 | preference, |
982 | 0 | namebuf); |
983 | 0 | } |
984 | | |
985 | | static GVariant * |
986 | | parse_res_txt (const guint8 *answer, |
987 | | const guint8 *end, |
988 | | const guint8 **p, |
989 | | GError **error) |
990 | 0 | { |
991 | 0 | GVariant *record; |
992 | 0 | GPtrArray *array; |
993 | 0 | const guint8 *at = *p; |
994 | 0 | gsize len; |
995 | |
|
996 | 0 | if (end - *p == 0) |
997 | 0 | { |
998 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
999 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
1000 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), "TXT"); |
1001 | 0 | return NULL; |
1002 | 0 | } |
1003 | | |
1004 | 0 | array = g_ptr_array_new_with_free_func (g_free); |
1005 | 0 | while (at < end) |
1006 | 0 | { |
1007 | 0 | len = *(at++); |
1008 | 0 | if (len > (gsize) (end - at)) |
1009 | 0 | { |
1010 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1011 | | /* Translators: the placeholder is a DNS record type, such as ‘MX’ or ‘SRV’ */ |
1012 | 0 | _("Error parsing DNS %s record: malformed DNS packet"), "TXT"); |
1013 | 0 | g_ptr_array_free (array, TRUE); |
1014 | 0 | return NULL; |
1015 | 0 | } |
1016 | | |
1017 | 0 | g_ptr_array_add (array, g_strndup ((gchar *)at, len)); |
1018 | 0 | at += len; |
1019 | 0 | } |
1020 | | |
1021 | 0 | *p = at; |
1022 | 0 | record = g_variant_new ("(@as)", |
1023 | 0 | g_variant_new_strv ((const gchar **)array->pdata, array->len)); |
1024 | 0 | g_ptr_array_free (array, TRUE); |
1025 | 0 | return record; |
1026 | 0 | } |
1027 | | |
1028 | | gint |
1029 | | g_resolver_record_type_to_rrtype (GResolverRecordType type) |
1030 | 0 | { |
1031 | 0 | switch (type) |
1032 | 0 | { |
1033 | 0 | case G_RESOLVER_RECORD_SRV: |
1034 | 0 | return T_SRV; |
1035 | 0 | case G_RESOLVER_RECORD_TXT: |
1036 | 0 | return T_TXT; |
1037 | 0 | case G_RESOLVER_RECORD_SOA: |
1038 | 0 | return T_SOA; |
1039 | 0 | case G_RESOLVER_RECORD_NS: |
1040 | 0 | return T_NS; |
1041 | 0 | case G_RESOLVER_RECORD_MX: |
1042 | 0 | return T_MX; |
1043 | 0 | } |
1044 | 0 | g_return_val_if_reached (-1); |
1045 | 0 | } |
1046 | | |
1047 | | GList * |
1048 | | g_resolver_records_from_res_query (const gchar *rrname, |
1049 | | gint rrtype, |
1050 | | const guint8 *answer, |
1051 | | gssize len, |
1052 | | gint herr, |
1053 | | GError **error) |
1054 | 0 | { |
1055 | 0 | uint16_t count; |
1056 | 0 | gchar namebuf[1024]; |
1057 | 0 | const guint8 *end, *p; |
1058 | 0 | guint16 type, qclass, rdlength; |
1059 | 0 | const HEADER *header; |
1060 | 0 | GList *records; |
1061 | 0 | GVariant *record; |
1062 | 0 | gsize len_unsigned; |
1063 | 0 | GError *parsing_error = NULL; |
1064 | |
|
1065 | 0 | if (len <= 0) |
1066 | 0 | { |
1067 | 0 | if (len == 0 || herr == HOST_NOT_FOUND || herr == NO_DATA) |
1068 | 0 | { |
1069 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND, |
1070 | 0 | _("No DNS record of the requested type for “%s”"), rrname); |
1071 | 0 | } |
1072 | 0 | else if (herr == TRY_AGAIN) |
1073 | 0 | { |
1074 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE, |
1075 | 0 | _("Temporarily unable to resolve “%s”"), rrname); |
1076 | 0 | } |
1077 | 0 | else |
1078 | 0 | { |
1079 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1080 | 0 | _("Error resolving “%s”"), rrname); |
1081 | 0 | } |
1082 | |
|
1083 | 0 | return NULL; |
1084 | 0 | } |
1085 | | |
1086 | | /* We know len ≥ 0 now. */ |
1087 | 0 | len_unsigned = (gsize) len; |
1088 | |
|
1089 | 0 | if (len_unsigned < sizeof (HEADER)) |
1090 | 0 | { |
1091 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1092 | | /* Translators: the first placeholder is a domain name, the |
1093 | | * second is an error message */ |
1094 | 0 | _("Error resolving “%s”: %s"), rrname, _("Malformed DNS packet")); |
1095 | 0 | return NULL; |
1096 | 0 | } |
1097 | | |
1098 | 0 | records = NULL; |
1099 | |
|
1100 | 0 | header = (HEADER *)answer; |
1101 | 0 | p = answer + sizeof (HEADER); |
1102 | 0 | end = answer + len_unsigned; |
1103 | | |
1104 | | /* Skip query */ |
1105 | 0 | count = ntohs (header->qdcount); |
1106 | 0 | while (count-- && p < end) |
1107 | 0 | { |
1108 | 0 | int expand_result; |
1109 | |
|
1110 | 0 | expand_result = dn_expand (answer, end, p, namebuf, sizeof (namebuf)); |
1111 | 0 | if (expand_result < 0 || end - p < expand_result + 4) |
1112 | 0 | { |
1113 | | /* Not possible to recover parsing as the length of the rest of the |
1114 | | * record is unknown or is too short. */ |
1115 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1116 | | /* Translators: the first placeholder is a domain name, the |
1117 | | * second is an error message */ |
1118 | 0 | _("Error resolving “%s”: %s"), rrname, _("Malformed DNS packet")); |
1119 | 0 | return NULL; |
1120 | 0 | } |
1121 | | |
1122 | 0 | p += expand_result; |
1123 | 0 | p += 4; /* skip TYPE and CLASS */ |
1124 | | |
1125 | | /* To silence gcc warnings */ |
1126 | 0 | namebuf[0] = namebuf[1]; |
1127 | 0 | } |
1128 | | |
1129 | | /* Read answers */ |
1130 | 0 | count = ntohs (header->ancount); |
1131 | 0 | while (count-- && p < end) |
1132 | 0 | { |
1133 | 0 | int expand_result; |
1134 | |
|
1135 | 0 | expand_result = dn_expand (answer, end, p, namebuf, sizeof (namebuf)); |
1136 | 0 | if (expand_result < 0 || end - p < expand_result + 10) |
1137 | 0 | { |
1138 | | /* Not possible to recover parsing as the length of the rest of the |
1139 | | * record is unknown or is too short. */ |
1140 | 0 | g_set_error (&parsing_error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1141 | | /* Translators: the first placeholder is a domain name, the |
1142 | | * second is an error message */ |
1143 | 0 | _("Error resolving “%s”: %s"), rrname, _("Malformed DNS packet")); |
1144 | 0 | break; |
1145 | 0 | } |
1146 | | |
1147 | 0 | p += expand_result; |
1148 | 0 | GETSHORT (type, p); |
1149 | 0 | GETSHORT (qclass, p); |
1150 | 0 | p += 4; /* ignore the ttl (type=long) value */ |
1151 | 0 | GETSHORT (rdlength, p); |
1152 | |
|
1153 | 0 | if (end - p < rdlength) |
1154 | 0 | { |
1155 | 0 | g_set_error (&parsing_error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1156 | | /* Translators: the first placeholder is a domain name, the |
1157 | | * second is an error message */ |
1158 | 0 | _("Error resolving “%s”: %s"), rrname, _("Malformed DNS packet")); |
1159 | 0 | break; |
1160 | 0 | } |
1161 | | |
1162 | 0 | if (type != rrtype || qclass != C_IN) |
1163 | 0 | { |
1164 | 0 | p += rdlength; |
1165 | 0 | continue; |
1166 | 0 | } |
1167 | | |
1168 | 0 | switch (rrtype) |
1169 | 0 | { |
1170 | 0 | case T_SRV: |
1171 | 0 | record = parse_res_srv (answer, p + rdlength, &p, &parsing_error); |
1172 | 0 | break; |
1173 | 0 | case T_MX: |
1174 | 0 | record = parse_res_mx (answer, p + rdlength, &p, &parsing_error); |
1175 | 0 | break; |
1176 | 0 | case T_SOA: |
1177 | 0 | record = parse_res_soa (answer, p + rdlength, &p, &parsing_error); |
1178 | 0 | break; |
1179 | 0 | case T_NS: |
1180 | 0 | record = parse_res_ns (answer, p + rdlength, &p, &parsing_error); |
1181 | 0 | break; |
1182 | 0 | case T_TXT: |
1183 | 0 | record = parse_res_txt (answer, p + rdlength, &p, &parsing_error); |
1184 | 0 | break; |
1185 | 0 | default: |
1186 | 0 | g_debug ("Unrecognized DNS record type %u", rrtype); |
1187 | 0 | record = NULL; |
1188 | 0 | break; |
1189 | 0 | } |
1190 | | |
1191 | 0 | if (record != NULL) |
1192 | 0 | records = g_list_prepend (records, g_variant_ref_sink (record)); |
1193 | |
|
1194 | 0 | if (parsing_error != NULL) |
1195 | 0 | break; |
1196 | 0 | } |
1197 | | |
1198 | 0 | if (parsing_error != NULL) |
1199 | 0 | { |
1200 | 0 | g_propagate_prefixed_error (error, parsing_error, _("Failed to parse DNS response for “%s”: "), rrname); |
1201 | 0 | g_list_free_full (records, (GDestroyNotify)g_variant_unref); |
1202 | 0 | return NULL; |
1203 | 0 | } |
1204 | 0 | else if (records == NULL) |
1205 | 0 | { |
1206 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND, |
1207 | 0 | _("No DNS record of the requested type for “%s”"), rrname); |
1208 | |
|
1209 | 0 | return NULL; |
1210 | 0 | } |
1211 | 0 | else |
1212 | 0 | return records; |
1213 | 0 | } |
1214 | | |
1215 | | #elif defined(G_OS_WIN32) |
1216 | | |
1217 | | static GVariant * |
1218 | | parse_dns_srv (DNS_RECORDA *rec) |
1219 | | { |
1220 | | return g_variant_new ("(qqqs)", |
1221 | | (guint16)rec->Data.SRV.wPriority, |
1222 | | (guint16)rec->Data.SRV.wWeight, |
1223 | | (guint16)rec->Data.SRV.wPort, |
1224 | | rec->Data.SRV.pNameTarget); |
1225 | | } |
1226 | | |
1227 | | static GVariant * |
1228 | | parse_dns_soa (DNS_RECORDA *rec) |
1229 | | { |
1230 | | return g_variant_new ("(ssuuuuu)", |
1231 | | rec->Data.SOA.pNamePrimaryServer, |
1232 | | rec->Data.SOA.pNameAdministrator, |
1233 | | (guint32)rec->Data.SOA.dwSerialNo, |
1234 | | (guint32)rec->Data.SOA.dwRefresh, |
1235 | | (guint32)rec->Data.SOA.dwRetry, |
1236 | | (guint32)rec->Data.SOA.dwExpire, |
1237 | | (guint32)rec->Data.SOA.dwDefaultTtl); |
1238 | | } |
1239 | | |
1240 | | static GVariant * |
1241 | | parse_dns_ns (DNS_RECORDA *rec) |
1242 | | { |
1243 | | return g_variant_new ("(s)", rec->Data.NS.pNameHost); |
1244 | | } |
1245 | | |
1246 | | static GVariant * |
1247 | | parse_dns_mx (DNS_RECORDA *rec) |
1248 | | { |
1249 | | return g_variant_new ("(qs)", |
1250 | | (guint16)rec->Data.MX.wPreference, |
1251 | | rec->Data.MX.pNameExchange); |
1252 | | } |
1253 | | |
1254 | | static GVariant * |
1255 | | parse_dns_txt (DNS_RECORDA *rec) |
1256 | | { |
1257 | | GVariant *record; |
1258 | | GPtrArray *array; |
1259 | | DWORD i; |
1260 | | |
1261 | | array = g_ptr_array_new (); |
1262 | | for (i = 0; i < rec->Data.TXT.dwStringCount; i++) |
1263 | | g_ptr_array_add (array, rec->Data.TXT.pStringArray[i]); |
1264 | | record = g_variant_new ("(@as)", |
1265 | | g_variant_new_strv ((const gchar **)array->pdata, array->len)); |
1266 | | g_ptr_array_free (array, TRUE); |
1267 | | return record; |
1268 | | } |
1269 | | |
1270 | | static WORD |
1271 | | g_resolver_record_type_to_dnstype (GResolverRecordType type) |
1272 | | { |
1273 | | switch (type) |
1274 | | { |
1275 | | case G_RESOLVER_RECORD_SRV: |
1276 | | return DNS_TYPE_SRV; |
1277 | | case G_RESOLVER_RECORD_TXT: |
1278 | | return DNS_TYPE_TEXT; |
1279 | | case G_RESOLVER_RECORD_SOA: |
1280 | | return DNS_TYPE_SOA; |
1281 | | case G_RESOLVER_RECORD_NS: |
1282 | | return DNS_TYPE_NS; |
1283 | | case G_RESOLVER_RECORD_MX: |
1284 | | return DNS_TYPE_MX; |
1285 | | } |
1286 | | g_return_val_if_reached (-1); |
1287 | | } |
1288 | | |
1289 | | static GList * |
1290 | | g_resolver_records_from_DnsQuery (const gchar *rrname, |
1291 | | WORD dnstype, |
1292 | | DNS_STATUS status, |
1293 | | DNS_RECORDA *results, |
1294 | | GError **error) |
1295 | | { |
1296 | | DNS_RECORDA *rec; |
1297 | | gpointer record; |
1298 | | GList *records; |
1299 | | |
1300 | | if (status != ERROR_SUCCESS) |
1301 | | { |
1302 | | if (status == DNS_ERROR_RCODE_NAME_ERROR) |
1303 | | { |
1304 | | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND, |
1305 | | _("No DNS record of the requested type for “%s”"), rrname); |
1306 | | } |
1307 | | else if (status == DNS_ERROR_RCODE_SERVER_FAILURE) |
1308 | | { |
1309 | | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_TEMPORARY_FAILURE, |
1310 | | _("Temporarily unable to resolve “%s”"), rrname); |
1311 | | } |
1312 | | else |
1313 | | { |
1314 | | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1315 | | _("Error resolving “%s”"), rrname); |
1316 | | } |
1317 | | |
1318 | | return NULL; |
1319 | | } |
1320 | | |
1321 | | records = NULL; |
1322 | | for (rec = results; rec; rec = rec->pNext) |
1323 | | { |
1324 | | if (rec->wType != dnstype) |
1325 | | continue; |
1326 | | switch (dnstype) |
1327 | | { |
1328 | | case DNS_TYPE_SRV: |
1329 | | record = parse_dns_srv (rec); |
1330 | | break; |
1331 | | case DNS_TYPE_SOA: |
1332 | | record = parse_dns_soa (rec); |
1333 | | break; |
1334 | | case DNS_TYPE_NS: |
1335 | | record = parse_dns_ns (rec); |
1336 | | break; |
1337 | | case DNS_TYPE_MX: |
1338 | | record = parse_dns_mx (rec); |
1339 | | break; |
1340 | | case DNS_TYPE_TEXT: |
1341 | | record = parse_dns_txt (rec); |
1342 | | break; |
1343 | | default: |
1344 | | g_warn_if_reached (); |
1345 | | record = NULL; |
1346 | | break; |
1347 | | } |
1348 | | if (record != NULL) |
1349 | | records = g_list_prepend (records, g_variant_ref_sink (record)); |
1350 | | } |
1351 | | |
1352 | | if (records == NULL) |
1353 | | { |
1354 | | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_NOT_FOUND, |
1355 | | _("No DNS record of the requested type for “%s”"), rrname); |
1356 | | |
1357 | | return NULL; |
1358 | | } |
1359 | | else |
1360 | | return records; |
1361 | | } |
1362 | | |
1363 | | #endif |
1364 | | |
1365 | | static void |
1366 | | free_records (GList *records) |
1367 | 0 | { |
1368 | 0 | g_list_free_full (records, (GDestroyNotify) g_variant_unref); |
1369 | 0 | } |
1370 | | |
1371 | | #if defined(G_OS_UNIX) |
1372 | | #ifdef __BIONIC__ |
1373 | | #ifndef C_IN |
1374 | | #define C_IN 1 |
1375 | | #endif |
1376 | | int res_query(const char *, int, int, u_char *, int); |
1377 | | #endif |
1378 | | #endif |
1379 | | |
1380 | | static GList * |
1381 | | do_lookup_records (const gchar *rrname, |
1382 | | GResolverRecordType record_type, |
1383 | | GCancellable *cancellable, |
1384 | | GError **error) |
1385 | 0 | { |
1386 | 0 | GList *records; |
1387 | |
|
1388 | 0 | #if defined(G_OS_UNIX) |
1389 | 0 | gint len = 512; |
1390 | 0 | gint herr; |
1391 | 0 | GByteArray *answer; |
1392 | 0 | gint rrtype; |
1393 | |
|
1394 | 0 | #ifdef HAVE_RES_NQUERY |
1395 | | /* Load the resolver state. This is done once per worker thread, and the |
1396 | | * #GResolver::reload signal is ignored (since we always reload). This could |
1397 | | * be improved by having an explicit worker thread pool, with each thread |
1398 | | * containing some state which is initialised at thread creation time and |
1399 | | * updated in response to #GResolver::reload. |
1400 | | * |
1401 | | * What we have currently is not particularly worse than using res_query() in |
1402 | | * worker threads, since it would transparently call res_init() for each new |
1403 | | * worker thread. (Although the workers would get reused by the |
1404 | | * #GThreadPool.) |
1405 | | * |
1406 | | * FreeBSD requires the state to be zero-filled before calling res_ninit(). */ |
1407 | 0 | struct __res_state res = { 0, }; |
1408 | 0 | if (res_ninit (&res) != 0) |
1409 | 0 | { |
1410 | 0 | g_set_error (error, G_RESOLVER_ERROR, G_RESOLVER_ERROR_INTERNAL, |
1411 | 0 | _("Error resolving “%s”"), rrname); |
1412 | 0 | return NULL; |
1413 | 0 | } |
1414 | 0 | #endif |
1415 | | |
1416 | 0 | rrtype = g_resolver_record_type_to_rrtype (record_type); |
1417 | 0 | answer = g_byte_array_new (); |
1418 | 0 | for (;;) |
1419 | 0 | { |
1420 | 0 | g_byte_array_set_size (answer, len * 2); |
1421 | 0 | #if defined(HAVE_RES_NQUERY) |
1422 | 0 | len = res_nquery (&res, rrname, C_IN, rrtype, answer->data, answer->len); |
1423 | | #else |
1424 | | len = res_query (rrname, C_IN, rrtype, answer->data, answer->len); |
1425 | | #endif |
1426 | | |
1427 | | /* If answer fit in the buffer then we're done */ |
1428 | 0 | if (len < 0 || len < (gint)answer->len) |
1429 | 0 | break; |
1430 | | |
1431 | | /* |
1432 | | * On overflow some res_query's return the length needed, others |
1433 | | * return the full length entered. This code works in either case. |
1434 | | */ |
1435 | 0 | } |
1436 | |
|
1437 | 0 | herr = h_errno; |
1438 | 0 | records = g_resolver_records_from_res_query (rrname, rrtype, answer->data, len, herr, error); |
1439 | 0 | g_byte_array_free (answer, TRUE); |
1440 | |
|
1441 | 0 | #ifdef HAVE_RES_NQUERY |
1442 | |
|
1443 | | #if defined(HAVE_RES_NDESTROY) |
1444 | | res_ndestroy (&res); |
1445 | | #elif defined(HAVE_RES_NCLOSE) |
1446 | | res_nclose (&res); |
1447 | | #elif defined(HAVE_RES_NINIT) |
1448 | | #error "Your platform has res_ninit() but not res_nclose() or res_ndestroy(). Please file a bug at https://gitlab.gnome.org/GNOME/glib/issues/new" |
1449 | | #endif |
1450 | |
|
1451 | 0 | #endif /* HAVE_RES_NQUERY */ |
1452 | |
|
1453 | | #else |
1454 | | |
1455 | | DNS_STATUS status; |
1456 | | DNS_RECORDA *results = NULL; |
1457 | | WORD dnstype; |
1458 | | |
1459 | | /* Work around differences in Windows SDK and mingw-w64 headers */ |
1460 | | #ifdef _MSC_VER |
1461 | | typedef DNS_RECORDW * PDNS_RECORD_UTF8_; |
1462 | | #else |
1463 | | typedef DNS_RECORDA * PDNS_RECORD_UTF8_; |
1464 | | #endif |
1465 | | |
1466 | | dnstype = g_resolver_record_type_to_dnstype (record_type); |
1467 | | status = DnsQuery_UTF8 (rrname, dnstype, DNS_QUERY_STANDARD, NULL, (PDNS_RECORD_UTF8_*)&results, NULL); |
1468 | | records = g_resolver_records_from_DnsQuery (rrname, dnstype, status, results, error); |
1469 | | if (results != NULL) |
1470 | | DnsRecordListFree (results, DnsFreeRecordList); |
1471 | | |
1472 | | #endif |
1473 | |
|
1474 | 0 | return g_steal_pointer (&records); |
1475 | 0 | } |
1476 | | |
1477 | | static GList * |
1478 | | lookup_records (GResolver *resolver, |
1479 | | const gchar *rrname, |
1480 | | GResolverRecordType record_type, |
1481 | | GCancellable *cancellable, |
1482 | | GError **error) |
1483 | 0 | { |
1484 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
1485 | 0 | GTask *task; |
1486 | 0 | GList *records; |
1487 | 0 | LookupData *data = NULL; |
1488 | |
|
1489 | 0 | task = g_task_new (resolver, cancellable, NULL, NULL); |
1490 | 0 | g_task_set_source_tag (task, lookup_records); |
1491 | 0 | g_task_set_name (task, "[gio] resolver lookup records"); |
1492 | |
|
1493 | 0 | data = lookup_data_new_records (rrname, record_type); |
1494 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
1495 | |
|
1496 | 0 | run_task_in_thread_pool_sync (self, task); |
1497 | |
|
1498 | 0 | records = g_task_propagate_pointer (task, error); |
1499 | 0 | g_object_unref (task); |
1500 | |
|
1501 | 0 | return records; |
1502 | 0 | } |
1503 | | |
1504 | | static void |
1505 | | lookup_records_async (GResolver *resolver, |
1506 | | const char *rrname, |
1507 | | GResolverRecordType record_type, |
1508 | | GCancellable *cancellable, |
1509 | | GAsyncReadyCallback callback, |
1510 | | gpointer user_data) |
1511 | 0 | { |
1512 | 0 | GThreadedResolver *self = G_THREADED_RESOLVER (resolver); |
1513 | 0 | GTask *task; |
1514 | 0 | LookupData *data = NULL; |
1515 | |
|
1516 | 0 | task = g_task_new (resolver, cancellable, callback, user_data); |
1517 | 0 | g_task_set_source_tag (task, lookup_records_async); |
1518 | 0 | g_task_set_name (task, "[gio] resolver lookup records"); |
1519 | |
|
1520 | 0 | data = lookup_data_new_records (rrname, record_type); |
1521 | 0 | g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) lookup_data_free); |
1522 | |
|
1523 | 0 | run_task_in_thread_pool_async (self, task); |
1524 | |
|
1525 | 0 | g_object_unref (task); |
1526 | 0 | } |
1527 | | |
1528 | | static GList * |
1529 | | lookup_records_finish (GResolver *resolver, |
1530 | | GAsyncResult *result, |
1531 | | GError **error) |
1532 | 0 | { |
1533 | 0 | g_return_val_if_fail (g_task_is_valid (result, resolver), NULL); |
1534 | | |
1535 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
1536 | 0 | } |
1537 | | |
1538 | | /* Will be called in the GLib worker thread, so must lock all accesses to shared |
1539 | | * data. */ |
1540 | | static gboolean |
1541 | | timeout_cb (gpointer user_data) |
1542 | 0 | { |
1543 | 0 | GWeakRef *weak_task = user_data; |
1544 | 0 | GTask *task = NULL; /* (owned) */ |
1545 | 0 | LookupData *data; |
1546 | 0 | gboolean should_return; |
1547 | |
|
1548 | 0 | task = g_weak_ref_get (weak_task); |
1549 | 0 | if (task == NULL) |
1550 | 0 | return G_SOURCE_REMOVE; |
1551 | | |
1552 | 0 | data = g_task_get_task_data (task); |
1553 | |
|
1554 | 0 | g_mutex_lock (&data->lock); |
1555 | |
|
1556 | 0 | should_return = g_atomic_int_compare_and_exchange (&data->will_return, NOT_YET, TIMED_OUT); |
1557 | 0 | g_clear_pointer (&data->timeout_source, g_source_unref); |
1558 | |
|
1559 | 0 | g_mutex_unlock (&data->lock); |
1560 | |
|
1561 | 0 | if (should_return) |
1562 | 0 | { |
1563 | 0 | g_task_return_new_error_literal (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT, |
1564 | 0 | _("Socket I/O timed out")); |
1565 | 0 | } |
1566 | | |
1567 | | /* Signal completion of the task. */ |
1568 | 0 | g_mutex_lock (&data->lock); |
1569 | 0 | data->has_returned = TRUE; |
1570 | 0 | g_cond_broadcast (&data->cond); |
1571 | 0 | g_mutex_unlock (&data->lock); |
1572 | |
|
1573 | 0 | g_object_unref (task); |
1574 | |
|
1575 | 0 | return G_SOURCE_REMOVE; |
1576 | 0 | } |
1577 | | |
1578 | | /* Will be called in the GLib worker thread, so must lock all accesses to shared |
1579 | | * data. */ |
1580 | | static gboolean |
1581 | | cancelled_cb (GCancellable *cancellable, |
1582 | | gpointer user_data) |
1583 | 0 | { |
1584 | 0 | GWeakRef *weak_task = user_data; |
1585 | 0 | GTask *task = NULL; /* (owned) */ |
1586 | 0 | LookupData *data; |
1587 | 0 | gboolean should_return; |
1588 | |
|
1589 | 0 | task = g_weak_ref_get (weak_task); |
1590 | 0 | if (task == NULL) |
1591 | 0 | return G_SOURCE_REMOVE; |
1592 | | |
1593 | 0 | data = g_task_get_task_data (task); |
1594 | |
|
1595 | 0 | g_mutex_lock (&data->lock); |
1596 | |
|
1597 | 0 | g_assert (g_cancellable_is_cancelled (cancellable)); |
1598 | 0 | should_return = g_atomic_int_compare_and_exchange (&data->will_return, NOT_YET, CANCELLED); |
1599 | 0 | g_clear_pointer (&data->cancellable_source, g_source_unref); |
1600 | |
|
1601 | 0 | g_mutex_unlock (&data->lock); |
1602 | |
|
1603 | 0 | if (should_return) |
1604 | 0 | g_task_return_error_if_cancelled (task); |
1605 | | |
1606 | | /* Signal completion of the task. */ |
1607 | 0 | g_mutex_lock (&data->lock); |
1608 | 0 | data->has_returned = TRUE; |
1609 | 0 | g_cond_broadcast (&data->cond); |
1610 | 0 | g_mutex_unlock (&data->lock); |
1611 | |
|
1612 | 0 | g_object_unref (task); |
1613 | |
|
1614 | 0 | return G_SOURCE_REMOVE; |
1615 | 0 | } |
1616 | | |
1617 | | static void |
1618 | | weak_ref_clear_and_free (GWeakRef *weak_ref) |
1619 | 0 | { |
1620 | 0 | g_weak_ref_clear (weak_ref); |
1621 | 0 | g_free (weak_ref); |
1622 | 0 | } |
1623 | | |
1624 | | static void |
1625 | | run_task_in_thread_pool_async (GThreadedResolver *self, |
1626 | | GTask *task) |
1627 | 0 | { |
1628 | 0 | LookupData *data = g_task_get_task_data (task); |
1629 | 0 | guint timeout_ms = g_resolver_get_timeout (G_RESOLVER (self)); |
1630 | 0 | GCancellable *cancellable = g_task_get_cancellable (task); |
1631 | |
|
1632 | 0 | g_mutex_lock (&data->lock); |
1633 | |
|
1634 | 0 | g_thread_pool_push (self->thread_pool, g_object_ref (task), NULL); |
1635 | |
|
1636 | 0 | if (timeout_ms != 0) |
1637 | 0 | { |
1638 | 0 | GWeakRef *weak_task = g_new0 (GWeakRef, 1); |
1639 | 0 | g_weak_ref_set (weak_task, task); |
1640 | |
|
1641 | 0 | data->timeout_source = g_timeout_source_new (timeout_ms); |
1642 | 0 | g_source_set_static_name (data->timeout_source, "[gio] threaded resolver timeout"); |
1643 | 0 | g_source_set_callback (data->timeout_source, G_SOURCE_FUNC (timeout_cb), g_steal_pointer (&weak_task), (GDestroyNotify) weak_ref_clear_and_free); |
1644 | 0 | g_source_attach (data->timeout_source, GLIB_PRIVATE_CALL (g_get_worker_context) ()); |
1645 | 0 | } |
1646 | |
|
1647 | 0 | if (cancellable != NULL) |
1648 | 0 | { |
1649 | 0 | GWeakRef *weak_task = g_new0 (GWeakRef, 1); |
1650 | 0 | g_weak_ref_set (weak_task, task); |
1651 | |
|
1652 | 0 | data->cancellable_source = g_cancellable_source_new (cancellable); |
1653 | 0 | g_source_set_static_name (data->cancellable_source, "[gio] threaded resolver cancellable"); |
1654 | 0 | g_source_set_callback (data->cancellable_source, G_SOURCE_FUNC (cancelled_cb), g_steal_pointer (&weak_task), (GDestroyNotify) weak_ref_clear_and_free); |
1655 | 0 | g_source_attach (data->cancellable_source, GLIB_PRIVATE_CALL (g_get_worker_context) ()); |
1656 | 0 | } |
1657 | |
|
1658 | 0 | g_mutex_unlock (&data->lock); |
1659 | 0 | } |
1660 | | |
1661 | | static void |
1662 | | run_task_in_thread_pool_sync (GThreadedResolver *self, |
1663 | | GTask *task) |
1664 | 0 | { |
1665 | 0 | LookupData *data = g_task_get_task_data (task); |
1666 | |
|
1667 | 0 | run_task_in_thread_pool_async (self, task); |
1668 | |
|
1669 | 0 | g_mutex_lock (&data->lock); |
1670 | 0 | while (!data->has_returned) |
1671 | 0 | g_cond_wait (&data->cond, &data->lock); |
1672 | 0 | g_mutex_unlock (&data->lock); |
1673 | 0 | } |
1674 | | |
1675 | | static void |
1676 | | threaded_resolver_worker_cb (gpointer task_data, |
1677 | | gpointer user_data) |
1678 | 0 | { |
1679 | 0 | GTask *task = G_TASK (g_steal_pointer (&task_data)); |
1680 | 0 | LookupData *data = g_task_get_task_data (task); |
1681 | 0 | GCancellable *cancellable = g_task_get_cancellable (task); |
1682 | 0 | GThreadedResolver *resolver = G_THREADED_RESOLVER (user_data); |
1683 | 0 | GError *local_error = NULL; |
1684 | 0 | gboolean should_return; |
1685 | |
|
1686 | 0 | switch (data->lookup_type) { |
1687 | 0 | case LOOKUP_BY_NAME: |
1688 | 0 | { |
1689 | 0 | GList *addresses = do_lookup_by_name (resolver, |
1690 | 0 | data->lookup_by_name.hostname, |
1691 | 0 | data->lookup_by_name.address_family, |
1692 | 0 | cancellable, |
1693 | 0 | &local_error); |
1694 | |
|
1695 | 0 | g_mutex_lock (&data->lock); |
1696 | 0 | should_return = g_atomic_int_compare_and_exchange (&data->will_return, NOT_YET, COMPLETED); |
1697 | 0 | g_mutex_unlock (&data->lock); |
1698 | |
|
1699 | 0 | if (should_return) |
1700 | 0 | { |
1701 | 0 | if (addresses != NULL) |
1702 | 0 | g_task_return_pointer (task, g_steal_pointer (&addresses), (GDestroyNotify) g_resolver_free_addresses); |
1703 | 0 | else |
1704 | 0 | g_task_return_error (task, g_steal_pointer (&local_error)); |
1705 | 0 | } |
1706 | |
|
1707 | 0 | g_clear_pointer (&addresses, g_resolver_free_addresses); |
1708 | 0 | g_clear_error (&local_error); |
1709 | 0 | } |
1710 | 0 | break; |
1711 | 0 | case LOOKUP_BY_ADDRESS: |
1712 | 0 | { |
1713 | 0 | gchar *name = do_lookup_by_address (data->lookup_by_address.address, |
1714 | 0 | cancellable, |
1715 | 0 | &local_error); |
1716 | |
|
1717 | 0 | g_mutex_lock (&data->lock); |
1718 | 0 | should_return = g_atomic_int_compare_and_exchange (&data->will_return, NOT_YET, COMPLETED); |
1719 | 0 | g_mutex_unlock (&data->lock); |
1720 | |
|
1721 | 0 | if (should_return) |
1722 | 0 | { |
1723 | 0 | if (name != NULL) |
1724 | 0 | g_task_return_pointer (task, g_steal_pointer (&name), g_free); |
1725 | 0 | else |
1726 | 0 | g_task_return_error (task, g_steal_pointer (&local_error)); |
1727 | 0 | } |
1728 | |
|
1729 | 0 | g_clear_pointer (&name, g_free); |
1730 | 0 | g_clear_error (&local_error); |
1731 | 0 | } |
1732 | 0 | break; |
1733 | 0 | case LOOKUP_RECORDS: |
1734 | 0 | { |
1735 | 0 | GList *records = do_lookup_records (data->lookup_records.rrname, |
1736 | 0 | data->lookup_records.record_type, |
1737 | 0 | cancellable, |
1738 | 0 | &local_error); |
1739 | |
|
1740 | 0 | g_mutex_lock (&data->lock); |
1741 | 0 | should_return = g_atomic_int_compare_and_exchange (&data->will_return, NOT_YET, COMPLETED); |
1742 | 0 | g_mutex_unlock (&data->lock); |
1743 | |
|
1744 | 0 | if (should_return) |
1745 | 0 | { |
1746 | 0 | if (records != NULL) |
1747 | 0 | g_task_return_pointer (task, g_steal_pointer (&records), (GDestroyNotify) free_records); |
1748 | 0 | else |
1749 | 0 | g_task_return_error (task, g_steal_pointer (&local_error)); |
1750 | 0 | } |
1751 | |
|
1752 | 0 | g_clear_pointer (&records, free_records); |
1753 | 0 | g_clear_error (&local_error); |
1754 | 0 | } |
1755 | 0 | break; |
1756 | 0 | default: |
1757 | 0 | g_assert_not_reached (); |
1758 | 0 | } |
1759 | | |
1760 | | /* Signal completion of a task. */ |
1761 | 0 | g_mutex_lock (&data->lock); |
1762 | 0 | data->has_returned = TRUE; |
1763 | 0 | g_cond_broadcast (&data->cond); |
1764 | 0 | g_mutex_unlock (&data->lock); |
1765 | |
|
1766 | 0 | g_object_unref (task); |
1767 | 0 | } |
1768 | | |
1769 | | static void |
1770 | | g_threaded_resolver_class_init (GThreadedResolverClass *threaded_class) |
1771 | 0 | { |
1772 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (threaded_class); |
1773 | 0 | GResolverClass *resolver_class = G_RESOLVER_CLASS (threaded_class); |
1774 | |
|
1775 | 0 | object_class->finalize = g_threaded_resolver_finalize; |
1776 | |
|
1777 | 0 | resolver_class->lookup_by_name = lookup_by_name; |
1778 | 0 | resolver_class->lookup_by_name_async = lookup_by_name_async; |
1779 | 0 | resolver_class->lookup_by_name_finish = lookup_by_name_finish; |
1780 | 0 | resolver_class->lookup_by_name_with_flags = lookup_by_name_with_flags; |
1781 | 0 | resolver_class->lookup_by_name_with_flags_async = lookup_by_name_with_flags_async; |
1782 | 0 | resolver_class->lookup_by_name_with_flags_finish = lookup_by_name_with_flags_finish; |
1783 | 0 | resolver_class->lookup_by_address = lookup_by_address; |
1784 | 0 | resolver_class->lookup_by_address_async = lookup_by_address_async; |
1785 | 0 | resolver_class->lookup_by_address_finish = lookup_by_address_finish; |
1786 | 0 | resolver_class->lookup_records = lookup_records; |
1787 | 0 | resolver_class->lookup_records_async = lookup_records_async; |
1788 | 0 | resolver_class->lookup_records_finish = lookup_records_finish; |
1789 | 0 | } |