/src/glib/gio/gnetworkaddress.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 <stdlib.h> |
29 | | #include "gnetworkaddress.h" |
30 | | #include "gasyncresult.h" |
31 | | #include "ginetaddress.h" |
32 | | #include "ginetsocketaddress.h" |
33 | | #include "gnetworkingprivate.h" |
34 | | #include "gproxyaddressenumerator.h" |
35 | | #include "gresolver.h" |
36 | | #include "gtask.h" |
37 | | #include "gsocketaddressenumerator.h" |
38 | | #include "gioerror.h" |
39 | | #include "gsocketconnectable.h" |
40 | | |
41 | | #include <string.h> |
42 | | |
43 | | /* As recommended by RFC 8305 this is the time it waits for a following |
44 | | DNS response to come in (ipv4 waiting on ipv6 generally) |
45 | | */ |
46 | 0 | #define HAPPY_EYEBALLS_RESOLUTION_DELAY_MS 50 |
47 | | |
48 | | /** |
49 | | * SECTION:gnetworkaddress |
50 | | * @short_description: A GSocketConnectable for resolving hostnames |
51 | | * @include: gio/gio.h |
52 | | * |
53 | | * #GNetworkAddress provides an easy way to resolve a hostname and |
54 | | * then attempt to connect to that host, handling the possibility of |
55 | | * multiple IP addresses and multiple address families. |
56 | | * |
57 | | * The enumeration results of resolved addresses *may* be cached as long |
58 | | * as this object is kept alive which may have unexpected results if |
59 | | * alive for too long. |
60 | | * |
61 | | * See #GSocketConnectable for an example of using the connectable |
62 | | * interface. |
63 | | */ |
64 | | |
65 | | /** |
66 | | * GNetworkAddress: |
67 | | * |
68 | | * A #GSocketConnectable for resolving a hostname and connecting to |
69 | | * that host. |
70 | | */ |
71 | | |
72 | | struct _GNetworkAddressPrivate { |
73 | | gchar *hostname; |
74 | | guint16 port; |
75 | | GList *cached_sockaddrs; |
76 | | gchar *scheme; |
77 | | |
78 | | gint64 resolver_serial; |
79 | | }; |
80 | | |
81 | | enum { |
82 | | PROP_0, |
83 | | PROP_HOSTNAME, |
84 | | PROP_PORT, |
85 | | PROP_SCHEME, |
86 | | }; |
87 | | |
88 | | static void g_network_address_set_property (GObject *object, |
89 | | guint prop_id, |
90 | | const GValue *value, |
91 | | GParamSpec *pspec); |
92 | | static void g_network_address_get_property (GObject *object, |
93 | | guint prop_id, |
94 | | GValue *value, |
95 | | GParamSpec *pspec); |
96 | | |
97 | | static void g_network_address_connectable_iface_init (GSocketConnectableIface *iface); |
98 | | static GSocketAddressEnumerator *g_network_address_connectable_enumerate (GSocketConnectable *connectable); |
99 | | static GSocketAddressEnumerator *g_network_address_connectable_proxy_enumerate (GSocketConnectable *connectable); |
100 | | static gchar *g_network_address_connectable_to_string (GSocketConnectable *connectable); |
101 | | |
102 | | G_DEFINE_TYPE_WITH_CODE (GNetworkAddress, g_network_address, G_TYPE_OBJECT, |
103 | | G_ADD_PRIVATE (GNetworkAddress) |
104 | | G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE, |
105 | | g_network_address_connectable_iface_init)) |
106 | | |
107 | | static void |
108 | | g_network_address_finalize (GObject *object) |
109 | 0 | { |
110 | 0 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
111 | |
|
112 | 0 | g_free (addr->priv->hostname); |
113 | 0 | g_free (addr->priv->scheme); |
114 | 0 | g_list_free_full (addr->priv->cached_sockaddrs, g_object_unref); |
115 | |
|
116 | 0 | G_OBJECT_CLASS (g_network_address_parent_class)->finalize (object); |
117 | 0 | } |
118 | | |
119 | | static void |
120 | | g_network_address_class_init (GNetworkAddressClass *klass) |
121 | 0 | { |
122 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
123 | |
|
124 | 0 | gobject_class->set_property = g_network_address_set_property; |
125 | 0 | gobject_class->get_property = g_network_address_get_property; |
126 | 0 | gobject_class->finalize = g_network_address_finalize; |
127 | |
|
128 | 0 | g_object_class_install_property (gobject_class, PROP_HOSTNAME, |
129 | 0 | g_param_spec_string ("hostname", |
130 | 0 | P_("Hostname"), |
131 | 0 | P_("Hostname to resolve"), |
132 | 0 | NULL, |
133 | 0 | G_PARAM_READWRITE | |
134 | 0 | G_PARAM_CONSTRUCT_ONLY | |
135 | 0 | G_PARAM_STATIC_STRINGS)); |
136 | 0 | g_object_class_install_property (gobject_class, PROP_PORT, |
137 | 0 | g_param_spec_uint ("port", |
138 | 0 | P_("Port"), |
139 | 0 | P_("Network port"), |
140 | 0 | 0, 65535, 0, |
141 | 0 | G_PARAM_READWRITE | |
142 | 0 | G_PARAM_CONSTRUCT_ONLY | |
143 | 0 | G_PARAM_STATIC_STRINGS)); |
144 | |
|
145 | 0 | g_object_class_install_property (gobject_class, PROP_SCHEME, |
146 | 0 | g_param_spec_string ("scheme", |
147 | 0 | P_("Scheme"), |
148 | 0 | P_("URI Scheme"), |
149 | 0 | NULL, |
150 | 0 | G_PARAM_READWRITE | |
151 | 0 | G_PARAM_CONSTRUCT_ONLY | |
152 | 0 | G_PARAM_STATIC_STRINGS)); |
153 | 0 | } |
154 | | |
155 | | static void |
156 | | g_network_address_connectable_iface_init (GSocketConnectableIface *connectable_iface) |
157 | 0 | { |
158 | 0 | connectable_iface->enumerate = g_network_address_connectable_enumerate; |
159 | 0 | connectable_iface->proxy_enumerate = g_network_address_connectable_proxy_enumerate; |
160 | 0 | connectable_iface->to_string = g_network_address_connectable_to_string; |
161 | 0 | } |
162 | | |
163 | | static void |
164 | | g_network_address_init (GNetworkAddress *addr) |
165 | 0 | { |
166 | 0 | addr->priv = g_network_address_get_instance_private (addr); |
167 | 0 | } |
168 | | |
169 | | static void |
170 | | g_network_address_set_property (GObject *object, |
171 | | guint prop_id, |
172 | | const GValue *value, |
173 | | GParamSpec *pspec) |
174 | 0 | { |
175 | 0 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
176 | |
|
177 | 0 | switch (prop_id) |
178 | 0 | { |
179 | 0 | case PROP_HOSTNAME: |
180 | 0 | g_free (addr->priv->hostname); |
181 | 0 | addr->priv->hostname = g_value_dup_string (value); |
182 | 0 | break; |
183 | | |
184 | 0 | case PROP_PORT: |
185 | 0 | addr->priv->port = g_value_get_uint (value); |
186 | 0 | break; |
187 | | |
188 | 0 | case PROP_SCHEME: |
189 | 0 | g_free (addr->priv->scheme); |
190 | 0 | addr->priv->scheme = g_value_dup_string (value); |
191 | 0 | break; |
192 | | |
193 | 0 | default: |
194 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
195 | 0 | break; |
196 | 0 | } |
197 | |
|
198 | 0 | } |
199 | | |
200 | | static void |
201 | | g_network_address_get_property (GObject *object, |
202 | | guint prop_id, |
203 | | GValue *value, |
204 | | GParamSpec *pspec) |
205 | 0 | { |
206 | 0 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
207 | |
|
208 | 0 | switch (prop_id) |
209 | 0 | { |
210 | 0 | case PROP_HOSTNAME: |
211 | 0 | g_value_set_string (value, addr->priv->hostname); |
212 | 0 | break; |
213 | | |
214 | 0 | case PROP_PORT: |
215 | 0 | g_value_set_uint (value, addr->priv->port); |
216 | 0 | break; |
217 | | |
218 | 0 | case PROP_SCHEME: |
219 | 0 | g_value_set_string (value, addr->priv->scheme); |
220 | 0 | break; |
221 | | |
222 | 0 | default: |
223 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
224 | 0 | break; |
225 | 0 | } |
226 | |
|
227 | 0 | } |
228 | | |
229 | | /* |
230 | | * inet_addresses_to_inet_socket_addresses: |
231 | | * @addresses: (transfer full): #GList of #GInetAddress |
232 | | * |
233 | | * Returns: (transfer full): #GList of #GInetSocketAddress |
234 | | */ |
235 | | static GList * |
236 | | inet_addresses_to_inet_socket_addresses (GNetworkAddress *addr, |
237 | | GList *addresses) |
238 | 0 | { |
239 | 0 | GList *a, *socket_addresses = NULL; |
240 | |
|
241 | 0 | for (a = addresses; a; a = a->next) |
242 | 0 | { |
243 | 0 | GSocketAddress *sockaddr = g_inet_socket_address_new (a->data, addr->priv->port); |
244 | 0 | socket_addresses = g_list_append (socket_addresses, g_steal_pointer (&sockaddr)); |
245 | 0 | g_object_unref (a->data); |
246 | 0 | } |
247 | |
|
248 | 0 | g_list_free (addresses); |
249 | 0 | return socket_addresses; |
250 | 0 | } |
251 | | |
252 | | /* |
253 | | * g_network_address_set_cached_addresses: |
254 | | * @addr: A #GNetworkAddress |
255 | | * @addresses: (transfer full): List of #GInetAddress or #GInetSocketAddress |
256 | | * @resolver_serial: Serial of #GResolver used |
257 | | * |
258 | | * Consumes @addresses and uses them to replace the current internal list. |
259 | | */ |
260 | | static void |
261 | | g_network_address_set_cached_addresses (GNetworkAddress *addr, |
262 | | GList *addresses, |
263 | | guint64 resolver_serial) |
264 | 0 | { |
265 | 0 | g_assert (addresses != NULL); |
266 | | |
267 | 0 | if (addr->priv->cached_sockaddrs) |
268 | 0 | g_list_free_full (addr->priv->cached_sockaddrs, g_object_unref); |
269 | |
|
270 | 0 | if (G_IS_INET_SOCKET_ADDRESS (addresses->data)) |
271 | 0 | addr->priv->cached_sockaddrs = g_steal_pointer (&addresses); |
272 | 0 | else |
273 | 0 | addr->priv->cached_sockaddrs = inet_addresses_to_inet_socket_addresses (addr, g_steal_pointer (&addresses)); |
274 | 0 | addr->priv->resolver_serial = resolver_serial; |
275 | 0 | } |
276 | | |
277 | | static gboolean |
278 | | g_network_address_parse_sockaddr (GNetworkAddress *addr) |
279 | 0 | { |
280 | 0 | GSocketAddress *sockaddr; |
281 | |
|
282 | 0 | g_assert (addr->priv->cached_sockaddrs == NULL); |
283 | | |
284 | 0 | sockaddr = g_inet_socket_address_new_from_string (addr->priv->hostname, |
285 | 0 | addr->priv->port); |
286 | 0 | if (sockaddr) |
287 | 0 | { |
288 | 0 | addr->priv->cached_sockaddrs = g_list_append (addr->priv->cached_sockaddrs, sockaddr); |
289 | 0 | return TRUE; |
290 | 0 | } |
291 | 0 | else |
292 | 0 | return FALSE; |
293 | 0 | } |
294 | | |
295 | | /** |
296 | | * g_network_address_new: |
297 | | * @hostname: the hostname |
298 | | * @port: the port |
299 | | * |
300 | | * Creates a new #GSocketConnectable for connecting to the given |
301 | | * @hostname and @port. |
302 | | * |
303 | | * Note that depending on the configuration of the machine, a |
304 | | * @hostname of `localhost` may refer to the IPv4 loopback address |
305 | | * only, or to both IPv4 and IPv6; use |
306 | | * g_network_address_new_loopback() to create a #GNetworkAddress that |
307 | | * is guaranteed to resolve to both addresses. |
308 | | * |
309 | | * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress |
310 | | * |
311 | | * Since: 2.22 |
312 | | */ |
313 | | GSocketConnectable * |
314 | | g_network_address_new (const gchar *hostname, |
315 | | guint16 port) |
316 | 0 | { |
317 | 0 | return g_object_new (G_TYPE_NETWORK_ADDRESS, |
318 | 0 | "hostname", hostname, |
319 | 0 | "port", port, |
320 | 0 | NULL); |
321 | 0 | } |
322 | | |
323 | | /** |
324 | | * g_network_address_new_loopback: |
325 | | * @port: the port |
326 | | * |
327 | | * Creates a new #GSocketConnectable for connecting to the local host |
328 | | * over a loopback connection to the given @port. This is intended for |
329 | | * use in connecting to local services which may be running on IPv4 or |
330 | | * IPv6. |
331 | | * |
332 | | * The connectable will return IPv4 and IPv6 loopback addresses, |
333 | | * regardless of how the host resolves `localhost`. By contrast, |
334 | | * g_network_address_new() will often only return an IPv4 address when |
335 | | * resolving `localhost`, and an IPv6 address for `localhost6`. |
336 | | * |
337 | | * g_network_address_get_hostname() will always return `localhost` for |
338 | | * a #GNetworkAddress created with this constructor. |
339 | | * |
340 | | * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress |
341 | | * |
342 | | * Since: 2.44 |
343 | | */ |
344 | | GSocketConnectable * |
345 | | g_network_address_new_loopback (guint16 port) |
346 | 0 | { |
347 | 0 | GNetworkAddress *addr; |
348 | 0 | GList *addrs = NULL; |
349 | |
|
350 | 0 | addr = g_object_new (G_TYPE_NETWORK_ADDRESS, |
351 | 0 | "hostname", "localhost", |
352 | 0 | "port", port, |
353 | 0 | NULL); |
354 | |
|
355 | 0 | addrs = g_list_append (addrs, g_inet_address_new_loopback (AF_INET6)); |
356 | 0 | addrs = g_list_append (addrs, g_inet_address_new_loopback (AF_INET)); |
357 | 0 | g_network_address_set_cached_addresses (addr, g_steal_pointer (&addrs), 0); |
358 | |
|
359 | 0 | return G_SOCKET_CONNECTABLE (addr); |
360 | 0 | } |
361 | | |
362 | | /** |
363 | | * g_network_address_parse: |
364 | | * @host_and_port: the hostname and optionally a port |
365 | | * @default_port: the default port if not in @host_and_port |
366 | | * @error: a pointer to a #GError, or %NULL |
367 | | * |
368 | | * Creates a new #GSocketConnectable for connecting to the given |
369 | | * @hostname and @port. May fail and return %NULL in case |
370 | | * parsing @host_and_port fails. |
371 | | * |
372 | | * @host_and_port may be in any of a number of recognised formats; an IPv6 |
373 | | * address, an IPv4 address, or a domain name (in which case a DNS |
374 | | * lookup is performed). Quoting with [] is supported for all address |
375 | | * types. A port override may be specified in the usual way with a |
376 | | * colon. |
377 | | * |
378 | | * If no port is specified in @host_and_port then @default_port will be |
379 | | * used as the port number to connect to. |
380 | | * |
381 | | * In general, @host_and_port is expected to be provided by the user |
382 | | * (allowing them to give the hostname, and a port override if necessary) |
383 | | * and @default_port is expected to be provided by the application. |
384 | | * |
385 | | * (The port component of @host_and_port can also be specified as a |
386 | | * service name rather than as a numeric port, but this functionality |
387 | | * is deprecated, because it depends on the contents of /etc/services, |
388 | | * which is generally quite sparse on platforms other than Linux.) |
389 | | * |
390 | | * Returns: (transfer full) (type GNetworkAddress): the new |
391 | | * #GNetworkAddress, or %NULL on error |
392 | | * |
393 | | * Since: 2.22 |
394 | | */ |
395 | | GSocketConnectable * |
396 | | g_network_address_parse (const gchar *host_and_port, |
397 | | guint16 default_port, |
398 | | GError **error) |
399 | 0 | { |
400 | 0 | GSocketConnectable *connectable; |
401 | 0 | const gchar *port; |
402 | 0 | guint16 portnum; |
403 | 0 | gchar *name; |
404 | |
|
405 | 0 | g_return_val_if_fail (host_and_port != NULL, NULL); |
406 | | |
407 | 0 | port = NULL; |
408 | 0 | if (host_and_port[0] == '[') |
409 | | /* escaped host part (to allow, eg. "[2001:db8::1]:888") */ |
410 | 0 | { |
411 | 0 | const gchar *end; |
412 | |
|
413 | 0 | end = strchr (host_and_port, ']'); |
414 | 0 | if (end == NULL) |
415 | 0 | { |
416 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
417 | 0 | _("Hostname “%s” contains “[” but not “]”"), host_and_port); |
418 | 0 | return NULL; |
419 | 0 | } |
420 | | |
421 | 0 | if (end[1] == '\0') |
422 | 0 | port = NULL; |
423 | 0 | else if (end[1] == ':') |
424 | 0 | port = &end[2]; |
425 | 0 | else |
426 | 0 | { |
427 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
428 | 0 | "The ']' character (in hostname '%s') must come at the" |
429 | 0 | " end or be immediately followed by ':' and a port", |
430 | 0 | host_and_port); |
431 | 0 | return NULL; |
432 | 0 | } |
433 | | |
434 | 0 | name = g_strndup (host_and_port + 1, end - host_and_port - 1); |
435 | 0 | } |
436 | | |
437 | 0 | else if ((port = strchr (host_and_port, ':'))) |
438 | | /* string has a ':' in it */ |
439 | 0 | { |
440 | | /* skip ':' */ |
441 | 0 | port++; |
442 | |
|
443 | 0 | if (strchr (port, ':')) |
444 | | /* more than one ':' in string */ |
445 | 0 | { |
446 | | /* this is actually an unescaped IPv6 address */ |
447 | 0 | name = g_strdup (host_and_port); |
448 | 0 | port = NULL; |
449 | 0 | } |
450 | 0 | else |
451 | 0 | name = g_strndup (host_and_port, port - host_and_port - 1); |
452 | 0 | } |
453 | | |
454 | 0 | else |
455 | | /* plain hostname, no port */ |
456 | 0 | name = g_strdup (host_and_port); |
457 | | |
458 | 0 | if (port != NULL) |
459 | 0 | { |
460 | 0 | if (port[0] == '\0') |
461 | 0 | { |
462 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
463 | 0 | "If a ':' character is given, it must be followed by a " |
464 | 0 | "port (in hostname '%s').", host_and_port); |
465 | 0 | g_free (name); |
466 | 0 | return NULL; |
467 | 0 | } |
468 | | |
469 | 0 | else if ('0' <= port[0] && port[0] <= '9') |
470 | 0 | { |
471 | 0 | char *end; |
472 | 0 | long value; |
473 | |
|
474 | 0 | value = strtol (port, &end, 10); |
475 | 0 | if (*end != '\0' || value < 0 || value > G_MAXUINT16) |
476 | 0 | { |
477 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
478 | 0 | "Invalid numeric port '%s' specified in hostname '%s'", |
479 | 0 | port, host_and_port); |
480 | 0 | g_free (name); |
481 | 0 | return NULL; |
482 | 0 | } |
483 | | |
484 | 0 | portnum = value; |
485 | 0 | } |
486 | | |
487 | 0 | else |
488 | 0 | { |
489 | 0 | if (!g_getservbyname_ntohs (port, "tcp", &portnum)) |
490 | 0 | { |
491 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
492 | 0 | "Unknown service '%s' specified in hostname '%s'", |
493 | 0 | port, host_and_port); |
494 | 0 | #ifdef HAVE_ENDSERVENT |
495 | 0 | endservent (); |
496 | 0 | #endif |
497 | 0 | g_free (name); |
498 | 0 | return NULL; |
499 | 0 | } |
500 | | |
501 | 0 | #ifdef HAVE_ENDSERVENT |
502 | 0 | endservent (); |
503 | 0 | #endif |
504 | 0 | } |
505 | 0 | } |
506 | 0 | else |
507 | 0 | { |
508 | | /* No port in host_and_port */ |
509 | 0 | portnum = default_port; |
510 | 0 | } |
511 | | |
512 | 0 | connectable = g_network_address_new (name, portnum); |
513 | 0 | g_free (name); |
514 | |
|
515 | 0 | return connectable; |
516 | 0 | } |
517 | | |
518 | | /** |
519 | | * g_network_address_parse_uri: |
520 | | * @uri: the hostname and optionally a port |
521 | | * @default_port: The default port if none is found in the URI |
522 | | * @error: a pointer to a #GError, or %NULL |
523 | | * |
524 | | * Creates a new #GSocketConnectable for connecting to the given |
525 | | * @uri. May fail and return %NULL in case parsing @uri fails. |
526 | | * |
527 | | * Using this rather than g_network_address_new() or |
528 | | * g_network_address_parse() allows #GSocketClient to determine |
529 | | * when to use application-specific proxy protocols. |
530 | | * |
531 | | * Returns: (transfer full) (type GNetworkAddress): the new |
532 | | * #GNetworkAddress, or %NULL on error |
533 | | * |
534 | | * Since: 2.26 |
535 | | */ |
536 | | GSocketConnectable * |
537 | | g_network_address_parse_uri (const gchar *uri, |
538 | | guint16 default_port, |
539 | | GError **error) |
540 | 0 | { |
541 | 0 | GSocketConnectable *conn = NULL; |
542 | 0 | gchar *scheme = NULL; |
543 | 0 | gchar *hostname = NULL; |
544 | 0 | gint port; |
545 | |
|
546 | 0 | if (!g_uri_split_network (uri, G_URI_FLAGS_NONE, |
547 | 0 | &scheme, &hostname, &port, NULL)) |
548 | 0 | { |
549 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
550 | 0 | "Invalid URI ‘%s’", uri); |
551 | 0 | return NULL; |
552 | 0 | } |
553 | | |
554 | 0 | if (port <= 0) |
555 | 0 | port = default_port; |
556 | |
|
557 | 0 | conn = g_object_new (G_TYPE_NETWORK_ADDRESS, |
558 | 0 | "hostname", hostname, |
559 | 0 | "port", (guint) port, |
560 | 0 | "scheme", scheme, |
561 | 0 | NULL); |
562 | 0 | g_free (scheme); |
563 | 0 | g_free (hostname); |
564 | |
|
565 | 0 | return conn; |
566 | 0 | } |
567 | | |
568 | | /** |
569 | | * g_network_address_get_hostname: |
570 | | * @addr: a #GNetworkAddress |
571 | | * |
572 | | * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded, |
573 | | * depending on what @addr was created with. |
574 | | * |
575 | | * Returns: @addr's hostname |
576 | | * |
577 | | * Since: 2.22 |
578 | | */ |
579 | | const gchar * |
580 | | g_network_address_get_hostname (GNetworkAddress *addr) |
581 | 0 | { |
582 | 0 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL); |
583 | | |
584 | 0 | return addr->priv->hostname; |
585 | 0 | } |
586 | | |
587 | | /** |
588 | | * g_network_address_get_port: |
589 | | * @addr: a #GNetworkAddress |
590 | | * |
591 | | * Gets @addr's port number |
592 | | * |
593 | | * Returns: @addr's port (which may be 0) |
594 | | * |
595 | | * Since: 2.22 |
596 | | */ |
597 | | guint16 |
598 | | g_network_address_get_port (GNetworkAddress *addr) |
599 | 0 | { |
600 | 0 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), 0); |
601 | | |
602 | 0 | return addr->priv->port; |
603 | 0 | } |
604 | | |
605 | | /** |
606 | | * g_network_address_get_scheme: |
607 | | * @addr: a #GNetworkAddress |
608 | | * |
609 | | * Gets @addr's scheme |
610 | | * |
611 | | * Returns: (nullable): @addr's scheme (%NULL if not built from URI) |
612 | | * |
613 | | * Since: 2.26 |
614 | | */ |
615 | | const gchar * |
616 | | g_network_address_get_scheme (GNetworkAddress *addr) |
617 | 0 | { |
618 | 0 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL); |
619 | | |
620 | 0 | return addr->priv->scheme; |
621 | 0 | } |
622 | | |
623 | 0 | #define G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (_g_network_address_address_enumerator_get_type ()) |
624 | 0 | #define G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, GNetworkAddressAddressEnumerator)) |
625 | | |
626 | | typedef enum { |
627 | | RESOLVE_STATE_NONE = 0, |
628 | | RESOLVE_STATE_WAITING_ON_IPV4 = 1 << 0, |
629 | | RESOLVE_STATE_WAITING_ON_IPV6 = 1 << 1, |
630 | | } ResolveState; |
631 | | |
632 | | typedef struct { |
633 | | GSocketAddressEnumerator parent_instance; |
634 | | |
635 | | GNetworkAddress *addr; /* (owned) */ |
636 | | GList *addresses; /* (owned) (nullable) */ |
637 | | GList *current_item; /* (unowned) (nullable) */ |
638 | | GTask *queued_task; /* (owned) (nullable) */ |
639 | | GTask *waiting_task; /* (owned) (nullable) */ |
640 | | GError *last_error; /* (owned) (nullable) */ |
641 | | GSource *wait_source; /* (owned) (nullable) */ |
642 | | GMainContext *context; /* (owned) (nullable) */ |
643 | | ResolveState state; |
644 | | } GNetworkAddressAddressEnumerator; |
645 | | |
646 | | typedef struct { |
647 | | GSocketAddressEnumeratorClass parent_class; |
648 | | |
649 | | } GNetworkAddressAddressEnumeratorClass; |
650 | | |
651 | | static GType _g_network_address_address_enumerator_get_type (void); |
652 | | G_DEFINE_TYPE (GNetworkAddressAddressEnumerator, _g_network_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR) |
653 | | |
654 | | static void |
655 | | g_network_address_address_enumerator_finalize (GObject *object) |
656 | 0 | { |
657 | 0 | GNetworkAddressAddressEnumerator *addr_enum = |
658 | 0 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (object); |
659 | |
|
660 | 0 | if (addr_enum->wait_source) |
661 | 0 | { |
662 | 0 | g_source_destroy (addr_enum->wait_source); |
663 | 0 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
664 | 0 | } |
665 | 0 | g_clear_object (&addr_enum->queued_task); |
666 | 0 | g_clear_object (&addr_enum->waiting_task); |
667 | 0 | g_clear_error (&addr_enum->last_error); |
668 | 0 | g_object_unref (addr_enum->addr); |
669 | 0 | g_clear_pointer (&addr_enum->context, g_main_context_unref); |
670 | 0 | g_list_free_full (addr_enum->addresses, g_object_unref); |
671 | |
|
672 | 0 | G_OBJECT_CLASS (_g_network_address_address_enumerator_parent_class)->finalize (object); |
673 | 0 | } |
674 | | |
675 | | static inline GSocketFamily |
676 | | get_address_family (GInetSocketAddress *address) |
677 | 0 | { |
678 | 0 | return g_inet_address_get_family (g_inet_socket_address_get_address (address)); |
679 | 0 | } |
680 | | |
681 | | static void |
682 | | list_split_families (GList *list, |
683 | | GList **out_ipv4, |
684 | | GList **out_ipv6) |
685 | 0 | { |
686 | 0 | g_assert (out_ipv4); |
687 | 0 | g_assert (out_ipv6); |
688 | | |
689 | 0 | while (list) |
690 | 0 | { |
691 | 0 | GSocketFamily family = get_address_family (list->data); |
692 | 0 | switch (family) |
693 | 0 | { |
694 | 0 | case G_SOCKET_FAMILY_IPV4: |
695 | 0 | *out_ipv4 = g_list_prepend (*out_ipv4, list->data); |
696 | 0 | break; |
697 | 0 | case G_SOCKET_FAMILY_IPV6: |
698 | 0 | *out_ipv6 = g_list_prepend (*out_ipv6, list->data); |
699 | 0 | break; |
700 | 0 | case G_SOCKET_FAMILY_INVALID: |
701 | 0 | case G_SOCKET_FAMILY_UNIX: |
702 | 0 | g_assert_not_reached (); |
703 | 0 | } |
704 | | |
705 | 0 | list = g_list_next (list); |
706 | 0 | } |
707 | | |
708 | 0 | *out_ipv4 = g_list_reverse (*out_ipv4); |
709 | 0 | *out_ipv6 = g_list_reverse (*out_ipv6); |
710 | 0 | } |
711 | | |
712 | | static GList * |
713 | | list_interleave_families (GList *list1, |
714 | | GList *list2) |
715 | 0 | { |
716 | 0 | GList *interleaved = NULL; |
717 | |
|
718 | 0 | while (list1 || list2) |
719 | 0 | { |
720 | 0 | if (list1) |
721 | 0 | { |
722 | 0 | interleaved = g_list_append (interleaved, list1->data); |
723 | 0 | list1 = g_list_delete_link (list1, list1); |
724 | 0 | } |
725 | 0 | if (list2) |
726 | 0 | { |
727 | 0 | interleaved = g_list_append (interleaved, list2->data); |
728 | 0 | list2 = g_list_delete_link (list2, list2); |
729 | 0 | } |
730 | 0 | } |
731 | |
|
732 | 0 | return interleaved; |
733 | 0 | } |
734 | | |
735 | | /* list_copy_interleaved: |
736 | | * @list: (transfer container): List to copy |
737 | | * |
738 | | * Does a shallow copy of a list with address families interleaved. |
739 | | * |
740 | | * For example: |
741 | | * Input: [ipv6, ipv6, ipv4, ipv4] |
742 | | * Output: [ipv6, ipv4, ipv6, ipv4] |
743 | | * |
744 | | * Returns: (transfer container): A new list |
745 | | */ |
746 | | static GList * |
747 | | list_copy_interleaved (GList *list) |
748 | 0 | { |
749 | 0 | GList *ipv4 = NULL, *ipv6 = NULL; |
750 | |
|
751 | 0 | list_split_families (list, &ipv4, &ipv6); |
752 | 0 | return list_interleave_families (ipv6, ipv4); |
753 | 0 | } |
754 | | |
755 | | /* list_concat_interleaved: |
756 | | * @parent_list: (transfer container): Already existing list |
757 | | * @current_item: (transfer container): Item after which to resort |
758 | | * @new_list: (transfer container): New list to be interleaved and concatenated |
759 | | * |
760 | | * This differs from g_list_concat() + list_copy_interleaved() in that it sorts |
761 | | * items in the previous list starting from @current_item and concats the results |
762 | | * to @parent_list. |
763 | | * |
764 | | * Returns: (transfer container): New start of list |
765 | | */ |
766 | | static GList * |
767 | | list_concat_interleaved (GList *parent_list, |
768 | | GList *current_item, |
769 | | GList *new_list) |
770 | 0 | { |
771 | 0 | GList *ipv4 = NULL, *ipv6 = NULL, *interleaved, *trailing = NULL; |
772 | 0 | GSocketFamily last_family = G_SOCKET_FAMILY_IPV4; /* Default to starting with ipv6 */ |
773 | |
|
774 | 0 | if (current_item) |
775 | 0 | { |
776 | 0 | last_family = get_address_family (current_item->data); |
777 | | |
778 | | /* Unused addresses will get removed, resorted, then readded */ |
779 | 0 | trailing = g_list_next (current_item); |
780 | 0 | current_item->next = NULL; |
781 | 0 | } |
782 | |
|
783 | 0 | list_split_families (trailing, &ipv4, &ipv6); |
784 | 0 | list_split_families (new_list, &ipv4, &ipv6); |
785 | 0 | g_list_free (new_list); |
786 | |
|
787 | 0 | if (trailing) |
788 | 0 | g_list_free (trailing); |
789 | |
|
790 | 0 | if (last_family == G_SOCKET_FAMILY_IPV4) |
791 | 0 | interleaved = list_interleave_families (ipv6, ipv4); |
792 | 0 | else |
793 | 0 | interleaved = list_interleave_families (ipv4, ipv6); |
794 | |
|
795 | 0 | return g_list_concat (parent_list, interleaved); |
796 | 0 | } |
797 | | |
798 | | static void |
799 | | maybe_update_address_cache (GNetworkAddressAddressEnumerator *addr_enum, |
800 | | GResolver *resolver) |
801 | 0 | { |
802 | 0 | GList *addresses, *p; |
803 | | |
804 | | /* Only cache complete results */ |
805 | 0 | if (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4 || addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV6) |
806 | 0 | return; |
807 | | |
808 | | /* The enumerators list will not necessarily be fully sorted */ |
809 | 0 | addresses = list_copy_interleaved (addr_enum->addresses); |
810 | 0 | for (p = addresses; p; p = p->next) |
811 | 0 | g_object_ref (p->data); |
812 | |
|
813 | 0 | g_network_address_set_cached_addresses (addr_enum->addr, g_steal_pointer (&addresses), g_resolver_get_serial (resolver)); |
814 | 0 | } |
815 | | |
816 | | static void |
817 | | g_network_address_address_enumerator_add_addresses (GNetworkAddressAddressEnumerator *addr_enum, |
818 | | GList *addresses, |
819 | | GResolver *resolver) |
820 | 0 | { |
821 | 0 | GList *new_addresses = inet_addresses_to_inet_socket_addresses (addr_enum->addr, addresses); |
822 | |
|
823 | 0 | if (addr_enum->addresses == NULL) |
824 | 0 | addr_enum->addresses = g_steal_pointer (&new_addresses); |
825 | 0 | else |
826 | 0 | addr_enum->addresses = list_concat_interleaved (addr_enum->addresses, addr_enum->current_item, g_steal_pointer (&new_addresses)); |
827 | |
|
828 | 0 | maybe_update_address_cache (addr_enum, resolver); |
829 | 0 | } |
830 | | |
831 | | static gpointer |
832 | | copy_object (gconstpointer src, |
833 | | gpointer user_data) |
834 | 0 | { |
835 | 0 | return g_object_ref (G_OBJECT (src)); |
836 | 0 | } |
837 | | |
838 | | static GSocketAddress * |
839 | | init_and_query_next_address (GNetworkAddressAddressEnumerator *addr_enum) |
840 | 0 | { |
841 | 0 | GList *next_item; |
842 | |
|
843 | 0 | if (addr_enum->addresses == NULL) |
844 | 0 | addr_enum->addresses = g_list_copy_deep (addr_enum->addr->priv->cached_sockaddrs, |
845 | 0 | copy_object, NULL); |
846 | | |
847 | | /* We always want to look at the next item at call time to get the latest results. |
848 | | That means that sometimes ->next is NULL this call but is valid next call. |
849 | | */ |
850 | 0 | if (addr_enum->current_item == NULL) |
851 | 0 | next_item = addr_enum->current_item = addr_enum->addresses; |
852 | 0 | else |
853 | 0 | next_item = g_list_next (addr_enum->current_item); |
854 | |
|
855 | 0 | if (next_item) |
856 | 0 | { |
857 | 0 | addr_enum->current_item = next_item; |
858 | 0 | return g_object_ref (addr_enum->current_item->data); |
859 | 0 | } |
860 | 0 | else |
861 | 0 | return NULL; |
862 | 0 | } |
863 | | |
864 | | static GSocketAddress * |
865 | | g_network_address_address_enumerator_next (GSocketAddressEnumerator *enumerator, |
866 | | GCancellable *cancellable, |
867 | | GError **error) |
868 | 0 | { |
869 | 0 | GNetworkAddressAddressEnumerator *addr_enum = |
870 | 0 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator); |
871 | |
|
872 | 0 | if (addr_enum->addresses == NULL) |
873 | 0 | { |
874 | 0 | GNetworkAddress *addr = addr_enum->addr; |
875 | 0 | GResolver *resolver = g_resolver_get_default (); |
876 | 0 | gint64 serial = g_resolver_get_serial (resolver); |
877 | |
|
878 | 0 | if (addr->priv->resolver_serial != 0 && |
879 | 0 | addr->priv->resolver_serial != serial) |
880 | 0 | { |
881 | | /* Resolver has reloaded, discard cached addresses */ |
882 | 0 | g_list_free_full (addr->priv->cached_sockaddrs, g_object_unref); |
883 | 0 | addr->priv->cached_sockaddrs = NULL; |
884 | 0 | } |
885 | |
|
886 | 0 | if (!addr->priv->cached_sockaddrs) |
887 | 0 | g_network_address_parse_sockaddr (addr); |
888 | 0 | if (!addr->priv->cached_sockaddrs) |
889 | 0 | { |
890 | 0 | GList *addresses; |
891 | |
|
892 | 0 | addresses = g_resolver_lookup_by_name (resolver, |
893 | 0 | addr->priv->hostname, |
894 | 0 | cancellable, error); |
895 | 0 | if (!addresses) |
896 | 0 | { |
897 | 0 | g_object_unref (resolver); |
898 | 0 | return NULL; |
899 | 0 | } |
900 | | |
901 | 0 | g_network_address_set_cached_addresses (addr, g_steal_pointer (&addresses), serial); |
902 | 0 | } |
903 | | |
904 | 0 | g_object_unref (resolver); |
905 | 0 | } |
906 | | |
907 | 0 | return init_and_query_next_address (addr_enum); |
908 | 0 | } |
909 | | |
910 | | static void |
911 | | complete_queued_task (GNetworkAddressAddressEnumerator *addr_enum, |
912 | | GTask *task, |
913 | | GError *error) |
914 | 0 | { |
915 | 0 | if (error) |
916 | 0 | g_task_return_error (task, error); |
917 | 0 | else |
918 | 0 | { |
919 | 0 | GSocketAddress *sockaddr = init_and_query_next_address (addr_enum); |
920 | 0 | g_task_return_pointer (task, g_steal_pointer (&sockaddr), g_object_unref); |
921 | 0 | } |
922 | 0 | g_object_unref (task); |
923 | 0 | } |
924 | | |
925 | | static int |
926 | | on_address_timeout (gpointer user_data) |
927 | 0 | { |
928 | 0 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
929 | | |
930 | | /* Upon completion it may get unref'd by the owner */ |
931 | 0 | g_object_ref (addr_enum); |
932 | |
|
933 | 0 | if (addr_enum->queued_task != NULL) |
934 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
935 | 0 | g_steal_pointer (&addr_enum->last_error)); |
936 | 0 | else if (addr_enum->waiting_task != NULL) |
937 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), |
938 | 0 | NULL); |
939 | |
|
940 | 0 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
941 | 0 | g_object_unref (addr_enum); |
942 | |
|
943 | 0 | return G_SOURCE_REMOVE; |
944 | 0 | } |
945 | | |
946 | | static void |
947 | | got_ipv6_addresses (GObject *source_object, |
948 | | GAsyncResult *result, |
949 | | gpointer user_data) |
950 | 0 | { |
951 | 0 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
952 | 0 | GResolver *resolver = G_RESOLVER (source_object); |
953 | 0 | GList *addresses; |
954 | 0 | GError *error = NULL; |
955 | |
|
956 | 0 | addr_enum->state ^= RESOLVE_STATE_WAITING_ON_IPV6; |
957 | |
|
958 | 0 | addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, &error); |
959 | 0 | if (!error) |
960 | 0 | g_network_address_address_enumerator_add_addresses (addr_enum, g_steal_pointer (&addresses), resolver); |
961 | 0 | else |
962 | 0 | g_debug ("IPv6 DNS error: %s", error->message); |
963 | | |
964 | | /* If ipv4 was first and waiting on us it can stop waiting */ |
965 | 0 | if (addr_enum->wait_source) |
966 | 0 | { |
967 | 0 | g_source_destroy (addr_enum->wait_source); |
968 | 0 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
969 | 0 | } |
970 | | |
971 | | /* If we got an error before ipv4 then let its response handle it. |
972 | | * If we get ipv6 response first or error second then |
973 | | * immediately complete the task. |
974 | | */ |
975 | 0 | if (error != NULL && !addr_enum->last_error && (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4)) |
976 | 0 | { |
977 | | /* ipv6 lookup failed, but ipv4 is still outstanding. wait. */ |
978 | 0 | addr_enum->last_error = g_steal_pointer (&error); |
979 | 0 | } |
980 | 0 | else if (addr_enum->waiting_task != NULL) |
981 | 0 | { |
982 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), NULL); |
983 | 0 | } |
984 | 0 | else if (addr_enum->queued_task != NULL) |
985 | 0 | { |
986 | 0 | GError *task_error = NULL; |
987 | | |
988 | | /* If both errored just use the ipv6 one, |
989 | | but if ipv6 errored and ipv4 didn't we don't error */ |
990 | 0 | if (error != NULL && addr_enum->last_error) |
991 | 0 | task_error = g_steal_pointer (&error); |
992 | |
|
993 | 0 | g_clear_error (&addr_enum->last_error); |
994 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
995 | 0 | g_steal_pointer (&task_error)); |
996 | 0 | } |
997 | |
|
998 | 0 | g_clear_error (&error); |
999 | 0 | g_object_unref (addr_enum); |
1000 | 0 | } |
1001 | | |
1002 | | static void |
1003 | | got_ipv4_addresses (GObject *source_object, |
1004 | | GAsyncResult *result, |
1005 | | gpointer user_data) |
1006 | 0 | { |
1007 | 0 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
1008 | 0 | GResolver *resolver = G_RESOLVER (source_object); |
1009 | 0 | GList *addresses; |
1010 | 0 | GError *error = NULL; |
1011 | |
|
1012 | 0 | addr_enum->state ^= RESOLVE_STATE_WAITING_ON_IPV4; |
1013 | |
|
1014 | 0 | addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, &error); |
1015 | 0 | if (!error) |
1016 | 0 | g_network_address_address_enumerator_add_addresses (addr_enum, g_steal_pointer (&addresses), resolver); |
1017 | 0 | else |
1018 | 0 | g_debug ("IPv4 DNS error: %s", error->message); |
1019 | |
|
1020 | 0 | if (addr_enum->wait_source) |
1021 | 0 | { |
1022 | 0 | g_source_destroy (addr_enum->wait_source); |
1023 | 0 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
1024 | 0 | } |
1025 | | |
1026 | | /* If ipv6 already came in and errored then we return. |
1027 | | * If ipv6 returned successfully then we don't need to do anything unless |
1028 | | * another enumeration was waiting on us. |
1029 | | * If ipv6 hasn't come we should wait a short while for it as RFC 8305 suggests. |
1030 | | */ |
1031 | 0 | if (addr_enum->last_error) |
1032 | 0 | { |
1033 | 0 | g_assert (addr_enum->queued_task); |
1034 | 0 | g_clear_error (&addr_enum->last_error); |
1035 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
1036 | 0 | g_steal_pointer (&error)); |
1037 | 0 | } |
1038 | 0 | else if (addr_enum->waiting_task != NULL) |
1039 | 0 | { |
1040 | 0 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), NULL); |
1041 | 0 | } |
1042 | 0 | else if (addr_enum->queued_task != NULL) |
1043 | 0 | { |
1044 | 0 | addr_enum->last_error = g_steal_pointer (&error); |
1045 | 0 | addr_enum->wait_source = g_timeout_source_new (HAPPY_EYEBALLS_RESOLUTION_DELAY_MS); |
1046 | 0 | g_source_set_callback (addr_enum->wait_source, |
1047 | 0 | on_address_timeout, |
1048 | 0 | addr_enum, NULL); |
1049 | 0 | g_source_attach (addr_enum->wait_source, addr_enum->context); |
1050 | 0 | } |
1051 | | |
1052 | 0 | g_clear_error (&error); |
1053 | 0 | g_object_unref (addr_enum); |
1054 | 0 | } |
1055 | | |
1056 | | static void |
1057 | | g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enumerator, |
1058 | | GCancellable *cancellable, |
1059 | | GAsyncReadyCallback callback, |
1060 | | gpointer user_data) |
1061 | 0 | { |
1062 | 0 | GNetworkAddressAddressEnumerator *addr_enum = |
1063 | 0 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator); |
1064 | 0 | GSocketAddress *sockaddr; |
1065 | 0 | GTask *task; |
1066 | |
|
1067 | 0 | task = g_task_new (addr_enum, cancellable, callback, user_data); |
1068 | 0 | g_task_set_source_tag (task, g_network_address_address_enumerator_next_async); |
1069 | |
|
1070 | 0 | if (addr_enum->addresses == NULL && addr_enum->state == RESOLVE_STATE_NONE) |
1071 | 0 | { |
1072 | 0 | GNetworkAddress *addr = addr_enum->addr; |
1073 | 0 | GResolver *resolver = g_resolver_get_default (); |
1074 | 0 | gint64 serial = g_resolver_get_serial (resolver); |
1075 | |
|
1076 | 0 | if (addr->priv->resolver_serial != 0 && |
1077 | 0 | addr->priv->resolver_serial != serial) |
1078 | 0 | { |
1079 | | /* Resolver has reloaded, discard cached addresses */ |
1080 | 0 | g_list_free_full (addr->priv->cached_sockaddrs, g_object_unref); |
1081 | 0 | addr->priv->cached_sockaddrs = NULL; |
1082 | 0 | } |
1083 | |
|
1084 | 0 | if (addr->priv->cached_sockaddrs == NULL) |
1085 | 0 | { |
1086 | 0 | if (g_network_address_parse_sockaddr (addr)) |
1087 | 0 | complete_queued_task (addr_enum, task, NULL); |
1088 | 0 | else |
1089 | 0 | { |
1090 | | /* It does not make sense for this to be called multiple |
1091 | | * times before the initial callback has been called */ |
1092 | 0 | g_assert (addr_enum->queued_task == NULL); |
1093 | | |
1094 | 0 | addr_enum->state = RESOLVE_STATE_WAITING_ON_IPV4 | RESOLVE_STATE_WAITING_ON_IPV6; |
1095 | 0 | addr_enum->queued_task = g_steal_pointer (&task); |
1096 | | /* Look up in parallel as per RFC 8305 */ |
1097 | 0 | g_resolver_lookup_by_name_with_flags_async (resolver, |
1098 | 0 | addr->priv->hostname, |
1099 | 0 | G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY, |
1100 | 0 | cancellable, |
1101 | 0 | got_ipv6_addresses, g_object_ref (addr_enum)); |
1102 | 0 | g_resolver_lookup_by_name_with_flags_async (resolver, |
1103 | 0 | addr->priv->hostname, |
1104 | 0 | G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY, |
1105 | 0 | cancellable, |
1106 | 0 | got_ipv4_addresses, g_object_ref (addr_enum)); |
1107 | 0 | } |
1108 | 0 | g_object_unref (resolver); |
1109 | 0 | return; |
1110 | 0 | } |
1111 | | |
1112 | 0 | g_object_unref (resolver); |
1113 | 0 | } |
1114 | | |
1115 | 0 | sockaddr = init_and_query_next_address (addr_enum); |
1116 | 0 | if (sockaddr == NULL && (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4 || |
1117 | 0 | addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV6)) |
1118 | 0 | { |
1119 | 0 | addr_enum->waiting_task = task; |
1120 | 0 | } |
1121 | 0 | else |
1122 | 0 | { |
1123 | 0 | g_task_return_pointer (task, sockaddr, g_object_unref); |
1124 | 0 | g_object_unref (task); |
1125 | 0 | } |
1126 | 0 | } |
1127 | | |
1128 | | static GSocketAddress * |
1129 | | g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator, |
1130 | | GAsyncResult *result, |
1131 | | GError **error) |
1132 | 0 | { |
1133 | 0 | g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL); |
1134 | | |
1135 | 0 | return g_task_propagate_pointer (G_TASK (result), error); |
1136 | 0 | } |
1137 | | |
1138 | | static void |
1139 | | _g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator *enumerator) |
1140 | 0 | { |
1141 | 0 | enumerator->context = g_main_context_ref_thread_default (); |
1142 | 0 | } |
1143 | | |
1144 | | static void |
1145 | | _g_network_address_address_enumerator_class_init (GNetworkAddressAddressEnumeratorClass *addrenum_class) |
1146 | 0 | { |
1147 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (addrenum_class); |
1148 | 0 | GSocketAddressEnumeratorClass *enumerator_class = |
1149 | 0 | G_SOCKET_ADDRESS_ENUMERATOR_CLASS (addrenum_class); |
1150 | |
|
1151 | 0 | enumerator_class->next = g_network_address_address_enumerator_next; |
1152 | 0 | enumerator_class->next_async = g_network_address_address_enumerator_next_async; |
1153 | 0 | enumerator_class->next_finish = g_network_address_address_enumerator_next_finish; |
1154 | 0 | object_class->finalize = g_network_address_address_enumerator_finalize; |
1155 | 0 | } |
1156 | | |
1157 | | static GSocketAddressEnumerator * |
1158 | | g_network_address_connectable_enumerate (GSocketConnectable *connectable) |
1159 | 0 | { |
1160 | 0 | GNetworkAddressAddressEnumerator *addr_enum; |
1161 | |
|
1162 | 0 | addr_enum = g_object_new (G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, NULL); |
1163 | 0 | addr_enum->addr = g_object_ref (G_NETWORK_ADDRESS (connectable)); |
1164 | |
|
1165 | 0 | return (GSocketAddressEnumerator *)addr_enum; |
1166 | 0 | } |
1167 | | |
1168 | | static GSocketAddressEnumerator * |
1169 | | g_network_address_connectable_proxy_enumerate (GSocketConnectable *connectable) |
1170 | 0 | { |
1171 | 0 | GNetworkAddress *self = G_NETWORK_ADDRESS (connectable); |
1172 | 0 | GSocketAddressEnumerator *proxy_enum; |
1173 | 0 | gchar *uri; |
1174 | |
|
1175 | 0 | uri = g_uri_join (G_URI_FLAGS_NONE, |
1176 | 0 | self->priv->scheme ? self->priv->scheme : "none", |
1177 | 0 | NULL, |
1178 | 0 | self->priv->hostname, |
1179 | 0 | self->priv->port, |
1180 | 0 | "", |
1181 | 0 | NULL, |
1182 | 0 | NULL); |
1183 | |
|
1184 | 0 | proxy_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR, |
1185 | 0 | "connectable", connectable, |
1186 | 0 | "uri", uri, |
1187 | 0 | NULL); |
1188 | |
|
1189 | 0 | g_free (uri); |
1190 | |
|
1191 | 0 | return proxy_enum; |
1192 | 0 | } |
1193 | | |
1194 | | static gchar * |
1195 | | g_network_address_connectable_to_string (GSocketConnectable *connectable) |
1196 | 0 | { |
1197 | 0 | GNetworkAddress *addr; |
1198 | 0 | const gchar *scheme; |
1199 | 0 | guint16 port; |
1200 | 0 | GString *out; /* owned */ |
1201 | |
|
1202 | 0 | addr = G_NETWORK_ADDRESS (connectable); |
1203 | 0 | out = g_string_new (""); |
1204 | |
|
1205 | 0 | scheme = g_network_address_get_scheme (addr); |
1206 | 0 | if (scheme != NULL) |
1207 | 0 | g_string_append_printf (out, "%s:", scheme); |
1208 | |
|
1209 | 0 | g_string_append (out, g_network_address_get_hostname (addr)); |
1210 | |
|
1211 | 0 | port = g_network_address_get_port (addr); |
1212 | 0 | if (port != 0) |
1213 | 0 | g_string_append_printf (out, ":%u", port); |
1214 | |
|
1215 | 0 | return g_string_free (out, FALSE); |
1216 | 0 | } |