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