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 | #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 | { |
108 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
109 | |
110 | g_free (mem: addr->priv->hostname); |
111 | g_free (mem: addr->priv->scheme); |
112 | g_list_free_full (list: addr->priv->cached_sockaddrs, free_func: g_object_unref); |
113 | |
114 | G_OBJECT_CLASS (g_network_address_parent_class)->finalize (object); |
115 | } |
116 | |
117 | static void |
118 | g_network_address_class_init (GNetworkAddressClass *klass) |
119 | { |
120 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
121 | |
122 | gobject_class->set_property = g_network_address_set_property; |
123 | gobject_class->get_property = g_network_address_get_property; |
124 | gobject_class->finalize = g_network_address_finalize; |
125 | |
126 | g_object_class_install_property (oclass: gobject_class, property_id: PROP_HOSTNAME, |
127 | pspec: g_param_spec_string (name: "hostname" , |
128 | P_("Hostname" ), |
129 | P_("Hostname to resolve" ), |
130 | NULL, |
131 | flags: G_PARAM_READWRITE | |
132 | G_PARAM_CONSTRUCT_ONLY | |
133 | G_PARAM_STATIC_STRINGS)); |
134 | g_object_class_install_property (oclass: gobject_class, property_id: PROP_PORT, |
135 | pspec: g_param_spec_uint (name: "port" , |
136 | P_("Port" ), |
137 | P_("Network port" ), |
138 | minimum: 0, maximum: 65535, default_value: 0, |
139 | flags: G_PARAM_READWRITE | |
140 | G_PARAM_CONSTRUCT_ONLY | |
141 | G_PARAM_STATIC_STRINGS)); |
142 | |
143 | g_object_class_install_property (oclass: gobject_class, property_id: PROP_SCHEME, |
144 | pspec: g_param_spec_string (name: "scheme" , |
145 | P_("Scheme" ), |
146 | P_("URI Scheme" ), |
147 | NULL, |
148 | flags: G_PARAM_READWRITE | |
149 | G_PARAM_CONSTRUCT_ONLY | |
150 | G_PARAM_STATIC_STRINGS)); |
151 | } |
152 | |
153 | static void |
154 | g_network_address_connectable_iface_init (GSocketConnectableIface *connectable_iface) |
155 | { |
156 | connectable_iface->enumerate = g_network_address_connectable_enumerate; |
157 | connectable_iface->proxy_enumerate = g_network_address_connectable_proxy_enumerate; |
158 | connectable_iface->to_string = g_network_address_connectable_to_string; |
159 | } |
160 | |
161 | static void |
162 | g_network_address_init (GNetworkAddress *addr) |
163 | { |
164 | addr->priv = g_network_address_get_instance_private (self: addr); |
165 | } |
166 | |
167 | static void |
168 | g_network_address_set_property (GObject *object, |
169 | guint prop_id, |
170 | const GValue *value, |
171 | GParamSpec *pspec) |
172 | { |
173 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
174 | |
175 | switch (prop_id) |
176 | { |
177 | case PROP_HOSTNAME: |
178 | g_free (mem: addr->priv->hostname); |
179 | addr->priv->hostname = g_value_dup_string (value); |
180 | break; |
181 | |
182 | case PROP_PORT: |
183 | addr->priv->port = g_value_get_uint (value); |
184 | break; |
185 | |
186 | case PROP_SCHEME: |
187 | g_free (mem: addr->priv->scheme); |
188 | addr->priv->scheme = g_value_dup_string (value); |
189 | break; |
190 | |
191 | default: |
192 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
193 | break; |
194 | } |
195 | |
196 | } |
197 | |
198 | static void |
199 | g_network_address_get_property (GObject *object, |
200 | guint prop_id, |
201 | GValue *value, |
202 | GParamSpec *pspec) |
203 | { |
204 | GNetworkAddress *addr = G_NETWORK_ADDRESS (object); |
205 | |
206 | switch (prop_id) |
207 | { |
208 | case PROP_HOSTNAME: |
209 | g_value_set_string (value, v_string: addr->priv->hostname); |
210 | break; |
211 | |
212 | case PROP_PORT: |
213 | g_value_set_uint (value, v_uint: addr->priv->port); |
214 | break; |
215 | |
216 | case PROP_SCHEME: |
217 | g_value_set_string (value, v_string: addr->priv->scheme); |
218 | break; |
219 | |
220 | default: |
221 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
222 | break; |
223 | } |
224 | |
225 | } |
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 | { |
237 | GList *a, *socket_addresses = NULL; |
238 | |
239 | for (a = addresses; a; a = a->next) |
240 | { |
241 | GSocketAddress *sockaddr = g_inet_socket_address_new (address: a->data, port: addr->priv->port); |
242 | socket_addresses = g_list_append (list: socket_addresses, g_steal_pointer (&sockaddr)); |
243 | g_object_unref (object: a->data); |
244 | } |
245 | |
246 | g_list_free (list: addresses); |
247 | return socket_addresses; |
248 | } |
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 | { |
263 | g_assert (addresses != NULL); |
264 | |
265 | if (addr->priv->cached_sockaddrs) |
266 | g_list_free_full (list: addr->priv->cached_sockaddrs, free_func: g_object_unref); |
267 | |
268 | if (G_IS_INET_SOCKET_ADDRESS (addresses->data)) |
269 | addr->priv->cached_sockaddrs = g_steal_pointer (&addresses); |
270 | else |
271 | addr->priv->cached_sockaddrs = inet_addresses_to_inet_socket_addresses (addr, g_steal_pointer (&addresses)); |
272 | addr->priv->resolver_serial = resolver_serial; |
273 | } |
274 | |
275 | static gboolean |
276 | g_network_address_parse_sockaddr (GNetworkAddress *addr) |
277 | { |
278 | GSocketAddress *sockaddr; |
279 | |
280 | g_assert (addr->priv->cached_sockaddrs == NULL); |
281 | |
282 | sockaddr = g_inet_socket_address_new_from_string (address: addr->priv->hostname, |
283 | port: addr->priv->port); |
284 | if (sockaddr) |
285 | { |
286 | addr->priv->cached_sockaddrs = g_list_append (list: addr->priv->cached_sockaddrs, data: sockaddr); |
287 | return TRUE; |
288 | } |
289 | else |
290 | return FALSE; |
291 | } |
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 | { |
315 | return g_object_new (G_TYPE_NETWORK_ADDRESS, |
316 | first_property_name: "hostname" , hostname, |
317 | "port" , port, |
318 | NULL); |
319 | } |
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 | { |
345 | GNetworkAddress *addr; |
346 | GList *addrs = NULL; |
347 | |
348 | addr = g_object_new (G_TYPE_NETWORK_ADDRESS, |
349 | first_property_name: "hostname" , "localhost" , |
350 | "port" , port, |
351 | NULL); |
352 | |
353 | addrs = g_list_append (list: addrs, data: g_inet_address_new_loopback (AF_INET6)); |
354 | addrs = g_list_append (list: addrs, data: g_inet_address_new_loopback (AF_INET)); |
355 | g_network_address_set_cached_addresses (addr, g_steal_pointer (&addrs), resolver_serial: 0); |
356 | |
357 | return G_SOCKET_CONNECTABLE (addr); |
358 | } |
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 | { |
398 | GSocketConnectable *connectable; |
399 | const gchar *port; |
400 | guint16 portnum; |
401 | gchar *name; |
402 | |
403 | g_return_val_if_fail (host_and_port != NULL, NULL); |
404 | |
405 | port = NULL; |
406 | if (host_and_port[0] == '[') |
407 | /* escaped host part (to allow, eg. "[2001:db8::1]:888") */ |
408 | { |
409 | const gchar *end; |
410 | |
411 | end = strchr (s: host_and_port, c: ']'); |
412 | if (end == NULL) |
413 | { |
414 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
415 | _("Hostname “%s” contains “[” but not “]”" ), host_and_port); |
416 | return NULL; |
417 | } |
418 | |
419 | if (end[1] == '\0') |
420 | port = NULL; |
421 | else if (end[1] == ':') |
422 | port = &end[2]; |
423 | else |
424 | { |
425 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
426 | format: "The ']' character (in hostname '%s') must come at the" |
427 | " end or be immediately followed by ':' and a port" , |
428 | host_and_port); |
429 | return NULL; |
430 | } |
431 | |
432 | name = g_strndup (str: host_and_port + 1, n: end - host_and_port - 1); |
433 | } |
434 | |
435 | else if ((port = strchr (s: host_and_port, c: ':'))) |
436 | /* string has a ':' in it */ |
437 | { |
438 | /* skip ':' */ |
439 | port++; |
440 | |
441 | if (strchr (s: port, c: ':')) |
442 | /* more than one ':' in string */ |
443 | { |
444 | /* this is actually an unescaped IPv6 address */ |
445 | name = g_strdup (str: host_and_port); |
446 | port = NULL; |
447 | } |
448 | else |
449 | name = g_strndup (str: host_and_port, n: port - host_and_port - 1); |
450 | } |
451 | |
452 | else |
453 | /* plain hostname, no port */ |
454 | name = g_strdup (str: host_and_port); |
455 | |
456 | if (port != NULL) |
457 | { |
458 | if (port[0] == '\0') |
459 | { |
460 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
461 | format: "If a ':' character is given, it must be followed by a " |
462 | "port (in hostname '%s')." , host_and_port); |
463 | g_free (mem: name); |
464 | return NULL; |
465 | } |
466 | |
467 | else if ('0' <= port[0] && port[0] <= '9') |
468 | { |
469 | char *end; |
470 | long value; |
471 | |
472 | value = strtol (nptr: port, endptr: &end, base: 10); |
473 | if (*end != '\0' || value < 0 || value > G_MAXUINT16) |
474 | { |
475 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
476 | format: "Invalid numeric port '%s' specified in hostname '%s'" , |
477 | port, host_and_port); |
478 | g_free (mem: name); |
479 | return NULL; |
480 | } |
481 | |
482 | portnum = value; |
483 | } |
484 | |
485 | else |
486 | { |
487 | struct servent *entry; |
488 | |
489 | entry = getservbyname (name: port, proto: "tcp" ); |
490 | if (entry == NULL) |
491 | { |
492 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
493 | format: "Unknown service '%s' specified in hostname '%s'" , |
494 | port, host_and_port); |
495 | #ifdef HAVE_ENDSERVENT |
496 | endservent (); |
497 | #endif |
498 | g_free (mem: name); |
499 | return NULL; |
500 | } |
501 | |
502 | portnum = g_ntohs (entry->s_port); |
503 | |
504 | #ifdef HAVE_ENDSERVENT |
505 | endservent (); |
506 | #endif |
507 | } |
508 | } |
509 | else |
510 | { |
511 | /* No port in host_and_port */ |
512 | portnum = default_port; |
513 | } |
514 | |
515 | connectable = g_network_address_new (hostname: name, port: portnum); |
516 | g_free (mem: name); |
517 | |
518 | return connectable; |
519 | } |
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 | { |
544 | GSocketConnectable *conn = NULL; |
545 | gchar *scheme = NULL; |
546 | gchar *hostname = NULL; |
547 | gint port; |
548 | |
549 | if (!g_uri_split_network (uri_string: uri, flags: G_URI_FLAGS_NONE, |
550 | scheme: &scheme, host: &hostname, port: &port, NULL)) |
551 | { |
552 | g_set_error (err: error, G_IO_ERROR, code: G_IO_ERROR_INVALID_ARGUMENT, |
553 | format: "Invalid URI ‘%s’" , uri); |
554 | return NULL; |
555 | } |
556 | |
557 | if (port <= 0) |
558 | port = default_port; |
559 | |
560 | conn = g_object_new (G_TYPE_NETWORK_ADDRESS, |
561 | first_property_name: "hostname" , hostname, |
562 | "port" , (guint) port, |
563 | "scheme" , scheme, |
564 | NULL); |
565 | g_free (mem: scheme); |
566 | g_free (mem: hostname); |
567 | |
568 | return conn; |
569 | } |
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 | { |
585 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL); |
586 | |
587 | return addr->priv->hostname; |
588 | } |
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 | { |
603 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), 0); |
604 | |
605 | return addr->priv->port; |
606 | } |
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 | { |
621 | g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL); |
622 | |
623 | return addr->priv->scheme; |
624 | } |
625 | |
626 | #define G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (_g_network_address_address_enumerator_get_type ()) |
627 | #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 | { |
660 | GNetworkAddressAddressEnumerator *addr_enum = |
661 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (object); |
662 | |
663 | if (addr_enum->wait_source) |
664 | { |
665 | g_source_destroy (source: addr_enum->wait_source); |
666 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
667 | } |
668 | g_clear_object (&addr_enum->queued_task); |
669 | g_clear_object (&addr_enum->waiting_task); |
670 | g_clear_error (err: &addr_enum->last_error); |
671 | g_object_unref (object: addr_enum->addr); |
672 | g_clear_pointer (&addr_enum->context, g_main_context_unref); |
673 | g_list_free_full (list: addr_enum->addresses, free_func: g_object_unref); |
674 | |
675 | G_OBJECT_CLASS (_g_network_address_address_enumerator_parent_class)->finalize (object); |
676 | } |
677 | |
678 | static inline GSocketFamily |
679 | get_address_family (GInetSocketAddress *address) |
680 | { |
681 | return g_inet_address_get_family (address: g_inet_socket_address_get_address (address)); |
682 | } |
683 | |
684 | static void |
685 | list_split_families (GList *list, |
686 | GList **out_ipv4, |
687 | GList **out_ipv6) |
688 | { |
689 | g_assert (out_ipv4); |
690 | g_assert (out_ipv6); |
691 | |
692 | while (list) |
693 | { |
694 | GSocketFamily family = get_address_family (address: list->data); |
695 | switch (family) |
696 | { |
697 | case G_SOCKET_FAMILY_IPV4: |
698 | *out_ipv4 = g_list_prepend (list: *out_ipv4, data: list->data); |
699 | break; |
700 | case G_SOCKET_FAMILY_IPV6: |
701 | *out_ipv6 = g_list_prepend (list: *out_ipv6, data: list->data); |
702 | break; |
703 | case G_SOCKET_FAMILY_INVALID: |
704 | case G_SOCKET_FAMILY_UNIX: |
705 | g_assert_not_reached (); |
706 | } |
707 | |
708 | list = g_list_next (list); |
709 | } |
710 | |
711 | *out_ipv4 = g_list_reverse (list: *out_ipv4); |
712 | *out_ipv6 = g_list_reverse (list: *out_ipv6); |
713 | } |
714 | |
715 | static GList * |
716 | list_interleave_families (GList *list1, |
717 | GList *list2) |
718 | { |
719 | GList *interleaved = NULL; |
720 | |
721 | while (list1 || list2) |
722 | { |
723 | if (list1) |
724 | { |
725 | interleaved = g_list_append (list: interleaved, data: list1->data); |
726 | list1 = g_list_delete_link (list: list1, link_: list1); |
727 | } |
728 | if (list2) |
729 | { |
730 | interleaved = g_list_append (list: interleaved, data: list2->data); |
731 | list2 = g_list_delete_link (list: list2, link_: list2); |
732 | } |
733 | } |
734 | |
735 | return interleaved; |
736 | } |
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 | { |
752 | GList *ipv4 = NULL, *ipv6 = NULL; |
753 | |
754 | list_split_families (list, out_ipv4: &ipv4, out_ipv6: &ipv6); |
755 | return list_interleave_families (list1: ipv6, list2: ipv4); |
756 | } |
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 | { |
774 | GList *ipv4 = NULL, *ipv6 = NULL, *interleaved, *trailing = NULL; |
775 | GSocketFamily last_family = G_SOCKET_FAMILY_IPV4; /* Default to starting with ipv6 */ |
776 | |
777 | if (current_item) |
778 | { |
779 | last_family = get_address_family (address: current_item->data); |
780 | |
781 | /* Unused addresses will get removed, resorted, then readded */ |
782 | trailing = g_list_next (current_item); |
783 | current_item->next = NULL; |
784 | } |
785 | |
786 | list_split_families (list: trailing, out_ipv4: &ipv4, out_ipv6: &ipv6); |
787 | list_split_families (list: new_list, out_ipv4: &ipv4, out_ipv6: &ipv6); |
788 | g_list_free (list: new_list); |
789 | |
790 | if (trailing) |
791 | g_list_free (list: trailing); |
792 | |
793 | if (last_family == G_SOCKET_FAMILY_IPV4) |
794 | interleaved = list_interleave_families (list1: ipv6, list2: ipv4); |
795 | else |
796 | interleaved = list_interleave_families (list1: ipv4, list2: ipv6); |
797 | |
798 | return g_list_concat (list1: parent_list, list2: interleaved); |
799 | } |
800 | |
801 | static void |
802 | maybe_update_address_cache (GNetworkAddressAddressEnumerator *addr_enum, |
803 | GResolver *resolver) |
804 | { |
805 | GList *addresses, *p; |
806 | |
807 | /* Only cache complete results */ |
808 | if (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4 || addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV6) |
809 | return; |
810 | |
811 | /* The enumerators list will not necessarily be fully sorted */ |
812 | addresses = list_copy_interleaved (list: addr_enum->addresses); |
813 | for (p = addresses; p; p = p->next) |
814 | g_object_ref (p->data); |
815 | |
816 | g_network_address_set_cached_addresses (addr: addr_enum->addr, g_steal_pointer (&addresses), resolver_serial: g_resolver_get_serial (resolver)); |
817 | } |
818 | |
819 | static void |
820 | g_network_address_address_enumerator_add_addresses (GNetworkAddressAddressEnumerator *addr_enum, |
821 | GList *addresses, |
822 | GResolver *resolver) |
823 | { |
824 | GList *new_addresses = inet_addresses_to_inet_socket_addresses (addr: addr_enum->addr, addresses); |
825 | |
826 | if (addr_enum->addresses == NULL) |
827 | addr_enum->addresses = g_steal_pointer (&new_addresses); |
828 | else |
829 | addr_enum->addresses = list_concat_interleaved (parent_list: addr_enum->addresses, current_item: addr_enum->current_item, g_steal_pointer (&new_addresses)); |
830 | |
831 | maybe_update_address_cache (addr_enum, resolver); |
832 | } |
833 | |
834 | static gpointer |
835 | copy_object (gconstpointer src, |
836 | gpointer user_data) |
837 | { |
838 | return g_object_ref (G_OBJECT (src)); |
839 | } |
840 | |
841 | static GSocketAddress * |
842 | init_and_query_next_address (GNetworkAddressAddressEnumerator *addr_enum) |
843 | { |
844 | GList *next_item; |
845 | |
846 | if (addr_enum->addresses == NULL) |
847 | addr_enum->addresses = g_list_copy_deep (list: addr_enum->addr->priv->cached_sockaddrs, |
848 | func: 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 | if (addr_enum->current_item == NULL) |
854 | next_item = addr_enum->current_item = addr_enum->addresses; |
855 | else |
856 | next_item = g_list_next (addr_enum->current_item); |
857 | |
858 | if (next_item) |
859 | { |
860 | addr_enum->current_item = next_item; |
861 | return g_object_ref (addr_enum->current_item->data); |
862 | } |
863 | else |
864 | return NULL; |
865 | } |
866 | |
867 | static GSocketAddress * |
868 | g_network_address_address_enumerator_next (GSocketAddressEnumerator *enumerator, |
869 | GCancellable *cancellable, |
870 | GError **error) |
871 | { |
872 | GNetworkAddressAddressEnumerator *addr_enum = |
873 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator); |
874 | |
875 | if (addr_enum->addresses == NULL) |
876 | { |
877 | GNetworkAddress *addr = addr_enum->addr; |
878 | GResolver *resolver = g_resolver_get_default (); |
879 | gint64 serial = g_resolver_get_serial (resolver); |
880 | |
881 | if (addr->priv->resolver_serial != 0 && |
882 | addr->priv->resolver_serial != serial) |
883 | { |
884 | /* Resolver has reloaded, discard cached addresses */ |
885 | g_list_free_full (list: addr->priv->cached_sockaddrs, free_func: g_object_unref); |
886 | addr->priv->cached_sockaddrs = NULL; |
887 | } |
888 | |
889 | if (!addr->priv->cached_sockaddrs) |
890 | g_network_address_parse_sockaddr (addr); |
891 | if (!addr->priv->cached_sockaddrs) |
892 | { |
893 | GList *addresses; |
894 | |
895 | addresses = g_resolver_lookup_by_name (resolver, |
896 | hostname: addr->priv->hostname, |
897 | cancellable, error); |
898 | if (!addresses) |
899 | { |
900 | g_object_unref (object: resolver); |
901 | return NULL; |
902 | } |
903 | |
904 | g_network_address_set_cached_addresses (addr, g_steal_pointer (&addresses), resolver_serial: serial); |
905 | } |
906 | |
907 | g_object_unref (object: resolver); |
908 | } |
909 | |
910 | return init_and_query_next_address (addr_enum); |
911 | } |
912 | |
913 | static void |
914 | complete_queued_task (GNetworkAddressAddressEnumerator *addr_enum, |
915 | GTask *task, |
916 | GError *error) |
917 | { |
918 | if (error) |
919 | g_task_return_error (task, error); |
920 | else |
921 | { |
922 | GSocketAddress *sockaddr = init_and_query_next_address (addr_enum); |
923 | g_task_return_pointer (task, g_steal_pointer (&sockaddr), result_destroy: g_object_unref); |
924 | } |
925 | g_object_unref (object: task); |
926 | } |
927 | |
928 | static int |
929 | on_address_timeout (gpointer user_data) |
930 | { |
931 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
932 | |
933 | /* Upon completion it may get unref'd by the owner */ |
934 | g_object_ref (addr_enum); |
935 | |
936 | if (addr_enum->queued_task != NULL) |
937 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
938 | g_steal_pointer (&addr_enum->last_error)); |
939 | else if (addr_enum->waiting_task != NULL) |
940 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), |
941 | NULL); |
942 | |
943 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
944 | g_object_unref (object: addr_enum); |
945 | |
946 | return G_SOURCE_REMOVE; |
947 | } |
948 | |
949 | static void |
950 | got_ipv6_addresses (GObject *source_object, |
951 | GAsyncResult *result, |
952 | gpointer user_data) |
953 | { |
954 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
955 | GResolver *resolver = G_RESOLVER (source_object); |
956 | GList *addresses; |
957 | GError *error = NULL; |
958 | |
959 | addr_enum->state ^= RESOLVE_STATE_WAITING_ON_IPV6; |
960 | |
961 | addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, error: &error); |
962 | if (!error) |
963 | g_network_address_address_enumerator_add_addresses (addr_enum, g_steal_pointer (&addresses), resolver); |
964 | else |
965 | g_debug ("IPv6 DNS error: %s" , error->message); |
966 | |
967 | /* If ipv4 was first and waiting on us it can stop waiting */ |
968 | if (addr_enum->wait_source) |
969 | { |
970 | g_source_destroy (source: addr_enum->wait_source); |
971 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
972 | } |
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 | if (error != NULL && !addr_enum->last_error && (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4)) |
979 | { |
980 | /* ipv6 lookup failed, but ipv4 is still outstanding. wait. */ |
981 | addr_enum->last_error = g_steal_pointer (&error); |
982 | } |
983 | else if (addr_enum->waiting_task != NULL) |
984 | { |
985 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), NULL); |
986 | } |
987 | else if (addr_enum->queued_task != NULL) |
988 | { |
989 | 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 | if (error != NULL && addr_enum->last_error) |
994 | task_error = g_steal_pointer (&error); |
995 | |
996 | g_clear_error (err: &addr_enum->last_error); |
997 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
998 | g_steal_pointer (&task_error)); |
999 | } |
1000 | |
1001 | g_clear_error (err: &error); |
1002 | g_object_unref (object: addr_enum); |
1003 | } |
1004 | |
1005 | static void |
1006 | got_ipv4_addresses (GObject *source_object, |
1007 | GAsyncResult *result, |
1008 | gpointer user_data) |
1009 | { |
1010 | GNetworkAddressAddressEnumerator *addr_enum = user_data; |
1011 | GResolver *resolver = G_RESOLVER (source_object); |
1012 | GList *addresses; |
1013 | GError *error = NULL; |
1014 | |
1015 | addr_enum->state ^= RESOLVE_STATE_WAITING_ON_IPV4; |
1016 | |
1017 | addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, error: &error); |
1018 | if (!error) |
1019 | g_network_address_address_enumerator_add_addresses (addr_enum, g_steal_pointer (&addresses), resolver); |
1020 | else |
1021 | g_debug ("IPv4 DNS error: %s" , error->message); |
1022 | |
1023 | if (addr_enum->wait_source) |
1024 | { |
1025 | g_source_destroy (source: addr_enum->wait_source); |
1026 | g_clear_pointer (&addr_enum->wait_source, g_source_unref); |
1027 | } |
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 | if (addr_enum->last_error) |
1035 | { |
1036 | g_assert (addr_enum->queued_task); |
1037 | g_clear_error (err: &addr_enum->last_error); |
1038 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task), |
1039 | g_steal_pointer (&error)); |
1040 | } |
1041 | else if (addr_enum->waiting_task != NULL) |
1042 | { |
1043 | complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->waiting_task), NULL); |
1044 | } |
1045 | else if (addr_enum->queued_task != NULL) |
1046 | { |
1047 | addr_enum->last_error = g_steal_pointer (&error); |
1048 | addr_enum->wait_source = g_timeout_source_new (HAPPY_EYEBALLS_RESOLUTION_DELAY_MS); |
1049 | g_source_set_callback (source: addr_enum->wait_source, |
1050 | func: on_address_timeout, |
1051 | data: addr_enum, NULL); |
1052 | g_source_attach (source: addr_enum->wait_source, context: addr_enum->context); |
1053 | } |
1054 | |
1055 | g_clear_error (err: &error); |
1056 | g_object_unref (object: addr_enum); |
1057 | } |
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 | { |
1065 | GNetworkAddressAddressEnumerator *addr_enum = |
1066 | G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator); |
1067 | GSocketAddress *sockaddr; |
1068 | GTask *task; |
1069 | |
1070 | task = g_task_new (source_object: addr_enum, cancellable, callback, callback_data: user_data); |
1071 | g_task_set_source_tag (task, g_network_address_address_enumerator_next_async); |
1072 | |
1073 | if (addr_enum->addresses == NULL && addr_enum->state == RESOLVE_STATE_NONE) |
1074 | { |
1075 | GNetworkAddress *addr = addr_enum->addr; |
1076 | GResolver *resolver = g_resolver_get_default (); |
1077 | gint64 serial = g_resolver_get_serial (resolver); |
1078 | |
1079 | if (addr->priv->resolver_serial != 0 && |
1080 | addr->priv->resolver_serial != serial) |
1081 | { |
1082 | /* Resolver has reloaded, discard cached addresses */ |
1083 | g_list_free_full (list: addr->priv->cached_sockaddrs, free_func: g_object_unref); |
1084 | addr->priv->cached_sockaddrs = NULL; |
1085 | } |
1086 | |
1087 | if (addr->priv->cached_sockaddrs == NULL) |
1088 | { |
1089 | if (g_network_address_parse_sockaddr (addr)) |
1090 | complete_queued_task (addr_enum, task, NULL); |
1091 | else |
1092 | { |
1093 | /* It does not make sense for this to be called multiple |
1094 | * times before the initial callback has been called */ |
1095 | g_assert (addr_enum->queued_task == NULL); |
1096 | |
1097 | addr_enum->state = RESOLVE_STATE_WAITING_ON_IPV4 | RESOLVE_STATE_WAITING_ON_IPV6; |
1098 | addr_enum->queued_task = g_steal_pointer (&task); |
1099 | /* Look up in parallel as per RFC 8305 */ |
1100 | g_resolver_lookup_by_name_with_flags_async (resolver, |
1101 | hostname: addr->priv->hostname, |
1102 | flags: G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY, |
1103 | cancellable, |
1104 | callback: got_ipv6_addresses, g_object_ref (addr_enum)); |
1105 | g_resolver_lookup_by_name_with_flags_async (resolver, |
1106 | hostname: addr->priv->hostname, |
1107 | flags: G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY, |
1108 | cancellable, |
1109 | callback: got_ipv4_addresses, g_object_ref (addr_enum)); |
1110 | } |
1111 | g_object_unref (object: resolver); |
1112 | return; |
1113 | } |
1114 | |
1115 | g_object_unref (object: resolver); |
1116 | } |
1117 | |
1118 | sockaddr = init_and_query_next_address (addr_enum); |
1119 | if (sockaddr == NULL && (addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV4 || |
1120 | addr_enum->state & RESOLVE_STATE_WAITING_ON_IPV6)) |
1121 | { |
1122 | addr_enum->waiting_task = task; |
1123 | } |
1124 | else |
1125 | { |
1126 | g_task_return_pointer (task, result: sockaddr, result_destroy: g_object_unref); |
1127 | g_object_unref (object: task); |
1128 | } |
1129 | } |
1130 | |
1131 | static GSocketAddress * |
1132 | g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator, |
1133 | GAsyncResult *result, |
1134 | GError **error) |
1135 | { |
1136 | g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL); |
1137 | |
1138 | return g_task_propagate_pointer (G_TASK (result), error); |
1139 | } |
1140 | |
1141 | static void |
1142 | _g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator *enumerator) |
1143 | { |
1144 | enumerator->context = g_main_context_ref_thread_default (); |
1145 | } |
1146 | |
1147 | static void |
1148 | _g_network_address_address_enumerator_class_init (GNetworkAddressAddressEnumeratorClass *addrenum_class) |
1149 | { |
1150 | GObjectClass *object_class = G_OBJECT_CLASS (addrenum_class); |
1151 | GSocketAddressEnumeratorClass *enumerator_class = |
1152 | G_SOCKET_ADDRESS_ENUMERATOR_CLASS (addrenum_class); |
1153 | |
1154 | enumerator_class->next = g_network_address_address_enumerator_next; |
1155 | enumerator_class->next_async = g_network_address_address_enumerator_next_async; |
1156 | enumerator_class->next_finish = g_network_address_address_enumerator_next_finish; |
1157 | object_class->finalize = g_network_address_address_enumerator_finalize; |
1158 | } |
1159 | |
1160 | static GSocketAddressEnumerator * |
1161 | g_network_address_connectable_enumerate (GSocketConnectable *connectable) |
1162 | { |
1163 | GNetworkAddressAddressEnumerator *addr_enum; |
1164 | |
1165 | addr_enum = g_object_new (G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, NULL); |
1166 | addr_enum->addr = g_object_ref (G_NETWORK_ADDRESS (connectable)); |
1167 | |
1168 | return (GSocketAddressEnumerator *)addr_enum; |
1169 | } |
1170 | |
1171 | static GSocketAddressEnumerator * |
1172 | g_network_address_connectable_proxy_enumerate (GSocketConnectable *connectable) |
1173 | { |
1174 | GNetworkAddress *self = G_NETWORK_ADDRESS (connectable); |
1175 | GSocketAddressEnumerator *proxy_enum; |
1176 | gchar *uri; |
1177 | |
1178 | uri = g_uri_join (flags: G_URI_FLAGS_NONE, |
1179 | scheme: self->priv->scheme ? self->priv->scheme : "none" , |
1180 | NULL, |
1181 | host: self->priv->hostname, |
1182 | port: self->priv->port, |
1183 | path: "" , |
1184 | NULL, |
1185 | NULL); |
1186 | |
1187 | proxy_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR, |
1188 | first_property_name: "connectable" , connectable, |
1189 | "uri" , uri, |
1190 | NULL); |
1191 | |
1192 | g_free (mem: uri); |
1193 | |
1194 | return proxy_enum; |
1195 | } |
1196 | |
1197 | static gchar * |
1198 | g_network_address_connectable_to_string (GSocketConnectable *connectable) |
1199 | { |
1200 | GNetworkAddress *addr; |
1201 | const gchar *scheme; |
1202 | guint16 port; |
1203 | GString *out; /* owned */ |
1204 | |
1205 | addr = G_NETWORK_ADDRESS (connectable); |
1206 | out = g_string_new (init: "" ); |
1207 | |
1208 | scheme = g_network_address_get_scheme (addr); |
1209 | if (scheme != NULL) |
1210 | g_string_append_printf (string: out, format: "%s:" , scheme); |
1211 | |
1212 | g_string_append (string: out, val: g_network_address_get_hostname (addr)); |
1213 | |
1214 | port = g_network_address_get_port (addr); |
1215 | if (port != 0) |
1216 | g_string_append_printf (string: out, format: ":%u" , port); |
1217 | |
1218 | return g_string_free (string: out, FALSE); |
1219 | } |
1220 | |