1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "gnetworkaddress.h"
27 #include "gasyncresult.h"
28 #include "ginetaddress.h"
29 #include "ginetsocketaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gproxyaddressenumerator.h"
32 #include "gresolver.h"
34 #include "gsocketaddressenumerator.h"
36 #include "gsocketconnectable.h"
42 * SECTION:gnetworkaddress
43 * @short_description: A GSocketConnectable for resolving hostnames
46 * #GNetworkAddress provides an easy way to resolve a hostname and
47 * then attempt to connect to that host, handling the possibility of
48 * multiple IP addresses and multiple address families.
50 * See #GSocketConnectable for and example of using the connectable
57 * A #GSocketConnectable for resolving a hostname and connecting to
61 struct _GNetworkAddressPrivate
{
67 gint64 resolver_serial
;
77 static void g_network_address_set_property (GObject
*object
,
81 static void g_network_address_get_property (GObject
*object
,
86 static void g_network_address_connectable_iface_init (GSocketConnectableIface
*iface
);
87 static GSocketAddressEnumerator
*g_network_address_connectable_enumerate (GSocketConnectable
*connectable
);
88 static GSocketAddressEnumerator
*g_network_address_connectable_proxy_enumerate (GSocketConnectable
*connectable
);
89 static gchar
*g_network_address_connectable_to_string (GSocketConnectable
*connectable
);
91 G_DEFINE_TYPE_WITH_CODE (GNetworkAddress
, g_network_address
, G_TYPE_OBJECT
,
92 G_ADD_PRIVATE (GNetworkAddress
)
93 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE
,
94 g_network_address_connectable_iface_init
))
97 g_network_address_finalize (GObject
*object
)
99 GNetworkAddress
*addr
= G_NETWORK_ADDRESS (object
);
101 g_free (addr
->priv
->hostname
);
102 g_free (addr
->priv
->scheme
);
103 g_list_free_full (addr
->priv
->sockaddrs
, g_object_unref
);
105 G_OBJECT_CLASS (g_network_address_parent_class
)->finalize (object
);
109 g_network_address_class_init (GNetworkAddressClass
*klass
)
111 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
113 gobject_class
->set_property
= g_network_address_set_property
;
114 gobject_class
->get_property
= g_network_address_get_property
;
115 gobject_class
->finalize
= g_network_address_finalize
;
117 g_object_class_install_property (gobject_class
, PROP_HOSTNAME
,
118 g_param_spec_string ("hostname",
120 P_("Hostname to resolve"),
123 G_PARAM_CONSTRUCT_ONLY
|
124 G_PARAM_STATIC_STRINGS
));
125 g_object_class_install_property (gobject_class
, PROP_PORT
,
126 g_param_spec_uint ("port",
131 G_PARAM_CONSTRUCT_ONLY
|
132 G_PARAM_STATIC_STRINGS
));
134 g_object_class_install_property (gobject_class
, PROP_SCHEME
,
135 g_param_spec_string ("scheme",
140 G_PARAM_CONSTRUCT_ONLY
|
141 G_PARAM_STATIC_STRINGS
));
145 g_network_address_connectable_iface_init (GSocketConnectableIface
*connectable_iface
)
147 connectable_iface
->enumerate
= g_network_address_connectable_enumerate
;
148 connectable_iface
->proxy_enumerate
= g_network_address_connectable_proxy_enumerate
;
149 connectable_iface
->to_string
= g_network_address_connectable_to_string
;
153 g_network_address_init (GNetworkAddress
*addr
)
155 addr
->priv
= g_network_address_get_instance_private (addr
);
159 g_network_address_set_property (GObject
*object
,
164 GNetworkAddress
*addr
= G_NETWORK_ADDRESS (object
);
169 g_free (addr
->priv
->hostname
);
170 addr
->priv
->hostname
= g_value_dup_string (value
);
174 addr
->priv
->port
= g_value_get_uint (value
);
178 g_free (addr
->priv
->scheme
);
179 addr
->priv
->scheme
= g_value_dup_string (value
);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
190 g_network_address_get_property (GObject
*object
,
195 GNetworkAddress
*addr
= G_NETWORK_ADDRESS (object
);
200 g_value_set_string (value
, addr
->priv
->hostname
);
204 g_value_set_uint (value
, addr
->priv
->port
);
208 g_value_set_string (value
, addr
->priv
->scheme
);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
219 g_network_address_set_addresses (GNetworkAddress
*addr
,
221 guint64 resolver_serial
)
224 GSocketAddress
*sockaddr
;
226 g_return_if_fail (addresses
!= NULL
&& addr
->priv
->sockaddrs
== NULL
);
228 for (a
= addresses
; a
; a
= a
->next
)
230 sockaddr
= g_inet_socket_address_new (a
->data
, addr
->priv
->port
);
231 addr
->priv
->sockaddrs
= g_list_prepend (addr
->priv
->sockaddrs
, sockaddr
);
232 g_object_unref (a
->data
);
234 g_list_free (addresses
);
235 addr
->priv
->sockaddrs
= g_list_reverse (addr
->priv
->sockaddrs
);
237 addr
->priv
->resolver_serial
= resolver_serial
;
241 g_network_address_parse_sockaddr (GNetworkAddress
*addr
)
243 GSocketAddress
*sockaddr
;
245 sockaddr
= g_inet_socket_address_new_from_string (addr
->priv
->hostname
,
249 addr
->priv
->sockaddrs
= g_list_prepend (addr
->priv
->sockaddrs
, sockaddr
);
257 * g_network_address_new:
258 * @hostname: the hostname
261 * Creates a new #GSocketConnectable for connecting to the given
262 * @hostname and @port.
264 * Note that depending on the configuration of the machine, a
265 * @hostname of `localhost` may refer to the IPv4 loopback address
266 * only, or to both IPv4 and IPv6; use
267 * g_network_address_new_loopback() to create a #GNetworkAddress that
268 * is guaranteed to resolve to both addresses.
270 * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress
275 g_network_address_new (const gchar
*hostname
,
278 return g_object_new (G_TYPE_NETWORK_ADDRESS
,
279 "hostname", hostname
,
285 * g_network_address_new_loopback:
288 * Creates a new #GSocketConnectable for connecting to the local host
289 * over a loopback connection to the given @port. This is intended for
290 * use in connecting to local services which may be running on IPv4 or
293 * The connectable will return IPv4 and IPv6 loopback addresses,
294 * regardless of how the host resolves `localhost`. By contrast,
295 * g_network_address_new() will often only return an IPv4 address when
296 * resolving `localhost`, and an IPv6 address for `localhost6`.
298 * g_network_address_get_hostname() will always return `localhost` for
299 * #GNetworkAddresses created with this constructor.
301 * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress
306 g_network_address_new_loopback (guint16 port
)
308 GNetworkAddress
*addr
;
311 addr
= g_object_new (G_TYPE_NETWORK_ADDRESS
,
312 "hostname", "localhost",
316 addrs
= g_list_append (addrs
, g_inet_address_new_loopback (AF_INET6
));
317 addrs
= g_list_append (addrs
, g_inet_address_new_loopback (AF_INET
));
318 g_network_address_set_addresses (addr
, addrs
, 0);
320 return G_SOCKET_CONNECTABLE (addr
);
324 * g_network_address_parse:
325 * @host_and_port: the hostname and optionally a port
326 * @default_port: the default port if not in @host_and_port
327 * @error: a pointer to a #GError, or %NULL
329 * Creates a new #GSocketConnectable for connecting to the given
330 * @hostname and @port. May fail and return %NULL in case
331 * parsing @host_and_port fails.
333 * @host_and_port may be in any of a number of recognised formats; an IPv6
334 * address, an IPv4 address, or a domain name (in which case a DNS
335 * lookup is performed). Quoting with [] is supported for all address
336 * types. A port override may be specified in the usual way with a
339 * If no port is specified in @host_and_port then @default_port will be
340 * used as the port number to connect to.
342 * In general, @host_and_port is expected to be provided by the user
343 * (allowing them to give the hostname, and a port overide if necessary)
344 * and @default_port is expected to be provided by the application.
346 * (The port component of @host_and_port can also be specified as a
347 * service name rather than as a numeric port, but this functionality
348 * is deprecated, because it depends on the contents of /etc/services,
349 * which is generally quite sparse on platforms other than Linux.)
351 * Returns: (transfer full) (type GNetworkAddress): the new
352 * #GNetworkAddress, or %NULL on error
357 g_network_address_parse (const gchar
*host_and_port
,
358 guint16 default_port
,
361 GSocketConnectable
*connectable
;
366 g_return_val_if_fail (host_and_port
!= NULL
, NULL
);
369 if (host_and_port
[0] == '[')
370 /* escaped host part (to allow, eg. "[2001:db8::1]:888") */
374 end
= strchr (host_and_port
, ']');
377 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
378 _("Hostname “%s” contains “[” but not “]”"), host_and_port
);
384 else if (end
[1] == ':')
388 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
389 "The ']' character (in hostname '%s') must come at the"
390 " end or be immediately followed by ':' and a port",
395 name
= g_strndup (host_and_port
+ 1, end
- host_and_port
- 1);
398 else if ((port
= strchr (host_and_port
, ':')))
399 /* string has a ':' in it */
404 if (strchr (port
, ':'))
405 /* more than one ':' in string */
407 /* this is actually an unescaped IPv6 address */
408 name
= g_strdup (host_and_port
);
412 name
= g_strndup (host_and_port
, port
- host_and_port
- 1);
416 /* plain hostname, no port */
417 name
= g_strdup (host_and_port
);
423 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
424 "If a ':' character is given, it must be followed by a "
425 "port (in hostname '%s').", host_and_port
);
430 else if ('0' <= port
[0] && port
[0] <= '9')
435 value
= strtol (port
, &end
, 10);
436 if (*end
!= '\0' || value
< 0 || value
> G_MAXUINT16
)
438 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
439 "Invalid numeric port '%s' specified in hostname '%s'",
440 port
, host_and_port
);
450 struct servent
*entry
;
452 entry
= getservbyname (port
, "tcp");
455 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
456 "Unknown service '%s' specified in hostname '%s'",
457 port
, host_and_port
);
458 #ifdef HAVE_ENDSERVENT
465 portnum
= g_ntohs (entry
->s_port
);
467 #ifdef HAVE_ENDSERVENT
474 /* No port in host_and_port */
475 portnum
= default_port
;
478 connectable
= g_network_address_new (name
, portnum
);
484 /* Allowed characters outside alphanumeric for unreserved. */
485 #define G_URI_OTHER_UNRESERVED "-._~"
487 /* This or something equivalent will eventually go into glib/guri.h */
489 _g_uri_parse_authority (const char *uri
,
495 const char *start
, *p
, *at
, *delim
;
498 g_return_val_if_fail (uri
!= NULL
, FALSE
);
509 /* From RFC 3986 Decodes:
510 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
511 * hier-part = "//" authority path-abempty
512 * path-abempty = *( "/" segment )
513 * authority = [ userinfo "@" ] host [ ":" port ]
516 /* Check we have a valid scheme */
517 tmp_str
= g_uri_parse_scheme (uri
);
525 * hier-part = "//" authority path-abempty
528 start
= strstr (p
, "//");
535 /* check if the @ sign is part of the authority before attempting to
536 * decode the userinfo */
537 delim
= strpbrk (start
, "/?#[]");
538 at
= strchr (start
, '@');
539 if (at
&& delim
&& at
> delim
)
545 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
546 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
547 * pct-encoded = "%" HEXDIG HEXDIG
560 if (!(g_ascii_isxdigit (p
[0]) ||
561 g_ascii_isxdigit (p
[1])))
569 /* unreserved / sub-delims / : */
570 if (!(g_ascii_isalnum (c
) ||
571 strchr (G_URI_OTHER_UNRESERVED
, c
) ||
572 strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
, c
) ||
578 *userinfo
= g_strndup (start
, p
- start
- 1);
589 * host = IP-literal / IPv4address / reg-name
590 * reg-name = *( unreserved / pct-encoded / sub-delims )
593 /* If IPv6 or IPvFuture */
596 gboolean has_scope_id
= FALSE
, has_bad_scope_id
= FALSE
;
607 if (c
== '%' && !has_scope_id
)
610 if (p
[0] != '2' || p
[1] != '5')
611 has_bad_scope_id
= TRUE
;
615 /* unreserved / sub-delims */
616 if (!(g_ascii_isalnum (c
) ||
617 strchr (G_URI_OTHER_UNRESERVED
, c
) ||
618 strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
, c
) ||
626 if (has_bad_scope_id
)
627 *host
= g_strndup (start
, p
- start
- 1);
629 *host
= g_uri_unescape_segment (start
, p
- 1, NULL
);
650 if (!(g_ascii_isxdigit (p
[0]) ||
651 g_ascii_isxdigit (p
[1])))
659 /* unreserved / sub-delims */
660 if (!(g_ascii_isalnum (c
) ||
661 strchr (G_URI_OTHER_UNRESERVED
, c
) ||
662 strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
, c
)))
667 *host
= g_uri_unescape_segment (start
, p
- 1, NULL
);
687 if (!g_ascii_isdigit (c
))
690 tmp
= (tmp
* 10) + (c
- '0');
696 *port
= (guint16
) tmp
;
708 if (userinfo
&& *userinfo
)
718 _g_uri_from_authority (const gchar
*protocol
,
721 const gchar
*userinfo
)
725 uri
= g_string_new (protocol
);
726 g_string_append (uri
, "://");
730 g_string_append_uri_escaped (uri
, userinfo
, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO
, FALSE
);
731 g_string_append_c (uri
, '@');
734 if (g_hostname_is_non_ascii (host
))
736 gchar
*ace_encoded
= g_hostname_to_ascii (host
);
740 g_string_free (uri
, TRUE
);
743 g_string_append (uri
, ace_encoded
);
744 g_free (ace_encoded
);
746 else if (strchr (host
, ':'))
747 g_string_append_printf (uri
, "[%s]", host
);
749 g_string_append (uri
, host
);
752 g_string_append_printf (uri
, ":%u", port
);
754 return g_string_free (uri
, FALSE
);
758 * g_network_address_parse_uri:
759 * @uri: the hostname and optionally a port
760 * @default_port: The default port if none is found in the URI
761 * @error: a pointer to a #GError, or %NULL
763 * Creates a new #GSocketConnectable for connecting to the given
764 * @uri. May fail and return %NULL in case parsing @uri fails.
766 * Using this rather than g_network_address_new() or
767 * g_network_address_parse() allows #GSocketClient to determine
768 * when to use application-specific proxy protocols.
770 * Returns: (transfer full) (type GNetworkAddress): the new
771 * #GNetworkAddress, or %NULL on error
776 g_network_address_parse_uri (const gchar
*uri
,
777 guint16 default_port
,
780 GSocketConnectable
*conn
;
785 if (!_g_uri_parse_authority (uri
, &hostname
, &port
, NULL
))
787 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
796 scheme
= g_uri_parse_scheme (uri
);
798 conn
= g_object_new (G_TYPE_NETWORK_ADDRESS
,
799 "hostname", hostname
,
811 * g_network_address_get_hostname:
812 * @addr: a #GNetworkAddress
814 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded,
815 * depending on what @addr was created with.
817 * Returns: @addr's hostname
822 g_network_address_get_hostname (GNetworkAddress
*addr
)
824 g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr
), NULL
);
826 return addr
->priv
->hostname
;
830 * g_network_address_get_port:
831 * @addr: a #GNetworkAddress
833 * Gets @addr's port number
835 * Returns: @addr's port (which may be 0)
840 g_network_address_get_port (GNetworkAddress
*addr
)
842 g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr
), 0);
844 return addr
->priv
->port
;
848 * g_network_address_get_scheme:
849 * @addr: a #GNetworkAddress
851 * Gets @addr's scheme
853 * Returns: @addr's scheme (%NULL if not built from URI)
858 g_network_address_get_scheme (GNetworkAddress
*addr
)
860 g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr
), NULL
);
862 return addr
->priv
->scheme
;
865 #define G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (_g_network_address_address_enumerator_get_type ())
866 #define G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, GNetworkAddressAddressEnumerator))
869 GSocketAddressEnumerator parent_instance
;
871 GNetworkAddress
*addr
;
874 } GNetworkAddressAddressEnumerator
;
877 GSocketAddressEnumeratorClass parent_class
;
879 } GNetworkAddressAddressEnumeratorClass
;
881 static GType
_g_network_address_address_enumerator_get_type (void);
882 G_DEFINE_TYPE (GNetworkAddressAddressEnumerator
, _g_network_address_address_enumerator
, G_TYPE_SOCKET_ADDRESS_ENUMERATOR
)
885 g_network_address_address_enumerator_finalize (GObject
*object
)
887 GNetworkAddressAddressEnumerator
*addr_enum
=
888 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (object
);
890 g_object_unref (addr_enum
->addr
);
892 G_OBJECT_CLASS (_g_network_address_address_enumerator_parent_class
)->finalize (object
);
895 static GSocketAddress
*
896 g_network_address_address_enumerator_next (GSocketAddressEnumerator
*enumerator
,
897 GCancellable
*cancellable
,
900 GNetworkAddressAddressEnumerator
*addr_enum
=
901 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator
);
902 GSocketAddress
*sockaddr
;
904 if (addr_enum
->addresses
== NULL
)
906 GNetworkAddress
*addr
= addr_enum
->addr
;
907 GResolver
*resolver
= g_resolver_get_default ();
908 gint64 serial
= g_resolver_get_serial (resolver
);
910 if (addr
->priv
->resolver_serial
!= 0 &&
911 addr
->priv
->resolver_serial
!= serial
)
913 /* Resolver has reloaded, discard cached addresses */
914 g_list_free_full (addr
->priv
->sockaddrs
, g_object_unref
);
915 addr
->priv
->sockaddrs
= NULL
;
918 if (!addr
->priv
->sockaddrs
)
919 g_network_address_parse_sockaddr (addr
);
920 if (!addr
->priv
->sockaddrs
)
924 addresses
= g_resolver_lookup_by_name (resolver
,
925 addr
->priv
->hostname
,
929 g_object_unref (resolver
);
933 g_network_address_set_addresses (addr
, addresses
, serial
);
936 addr_enum
->addresses
= addr
->priv
->sockaddrs
;
937 addr_enum
->next
= addr_enum
->addresses
;
938 g_object_unref (resolver
);
941 if (addr_enum
->next
== NULL
)
944 sockaddr
= addr_enum
->next
->data
;
945 addr_enum
->next
= addr_enum
->next
->next
;
946 return g_object_ref (sockaddr
);
950 have_addresses (GNetworkAddressAddressEnumerator
*addr_enum
,
951 GTask
*task
, GError
*error
)
953 GSocketAddress
*sockaddr
;
955 addr_enum
->addresses
= addr_enum
->addr
->priv
->sockaddrs
;
956 addr_enum
->next
= addr_enum
->addresses
;
960 sockaddr
= g_object_ref (addr_enum
->next
->data
);
961 addr_enum
->next
= addr_enum
->next
->next
;
967 g_task_return_error (task
, error
);
969 g_task_return_pointer (task
, sockaddr
, g_object_unref
);
970 g_object_unref (task
);
974 got_addresses (GObject
*source_object
,
975 GAsyncResult
*result
,
978 GTask
*task
= user_data
;
979 GNetworkAddressAddressEnumerator
*addr_enum
= g_task_get_source_object (task
);
980 GResolver
*resolver
= G_RESOLVER (source_object
);
982 GError
*error
= NULL
;
984 if (!addr_enum
->addr
->priv
->sockaddrs
)
986 addresses
= g_resolver_lookup_by_name_finish (resolver
, result
, &error
);
990 g_network_address_set_addresses (addr_enum
->addr
, addresses
,
991 g_resolver_get_serial (resolver
));
994 have_addresses (addr_enum
, task
, error
);
998 g_network_address_address_enumerator_next_async (GSocketAddressEnumerator
*enumerator
,
999 GCancellable
*cancellable
,
1000 GAsyncReadyCallback callback
,
1003 GNetworkAddressAddressEnumerator
*addr_enum
=
1004 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator
);
1005 GSocketAddress
*sockaddr
;
1008 task
= g_task_new (addr_enum
, cancellable
, callback
, user_data
);
1009 g_task_set_source_tag (task
, g_network_address_address_enumerator_next_async
);
1011 if (addr_enum
->addresses
== NULL
)
1013 GNetworkAddress
*addr
= addr_enum
->addr
;
1014 GResolver
*resolver
= g_resolver_get_default ();
1015 gint64 serial
= g_resolver_get_serial (resolver
);
1017 if (addr
->priv
->resolver_serial
!= 0 &&
1018 addr
->priv
->resolver_serial
!= serial
)
1020 /* Resolver has reloaded, discard cached addresses */
1021 g_list_free_full (addr
->priv
->sockaddrs
, g_object_unref
);
1022 addr
->priv
->sockaddrs
= NULL
;
1025 if (!addr
->priv
->sockaddrs
)
1027 if (g_network_address_parse_sockaddr (addr
))
1028 have_addresses (addr_enum
, task
, NULL
);
1031 g_resolver_lookup_by_name_async (resolver
,
1032 addr
->priv
->hostname
,
1034 got_addresses
, task
);
1036 g_object_unref (resolver
);
1040 addr_enum
->addresses
= addr
->priv
->sockaddrs
;
1041 addr_enum
->next
= addr_enum
->addresses
;
1042 g_object_unref (resolver
);
1045 if (addr_enum
->next
)
1047 sockaddr
= g_object_ref (addr_enum
->next
->data
);
1048 addr_enum
->next
= addr_enum
->next
->next
;
1053 g_task_return_pointer (task
, sockaddr
, g_object_unref
);
1054 g_object_unref (task
);
1057 static GSocketAddress
*
1058 g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator
*enumerator
,
1059 GAsyncResult
*result
,
1062 g_return_val_if_fail (g_task_is_valid (result
, enumerator
), NULL
);
1064 return g_task_propagate_pointer (G_TASK (result
), error
);
1068 _g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator
*enumerator
)
1073 _g_network_address_address_enumerator_class_init (GNetworkAddressAddressEnumeratorClass
*addrenum_class
)
1075 GObjectClass
*object_class
= G_OBJECT_CLASS (addrenum_class
);
1076 GSocketAddressEnumeratorClass
*enumerator_class
=
1077 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (addrenum_class
);
1079 enumerator_class
->next
= g_network_address_address_enumerator_next
;
1080 enumerator_class
->next_async
= g_network_address_address_enumerator_next_async
;
1081 enumerator_class
->next_finish
= g_network_address_address_enumerator_next_finish
;
1082 object_class
->finalize
= g_network_address_address_enumerator_finalize
;
1085 static GSocketAddressEnumerator
*
1086 g_network_address_connectable_enumerate (GSocketConnectable
*connectable
)
1088 GNetworkAddressAddressEnumerator
*addr_enum
;
1090 addr_enum
= g_object_new (G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR
, NULL
);
1091 addr_enum
->addr
= g_object_ref (connectable
);
1093 return (GSocketAddressEnumerator
*)addr_enum
;
1096 static GSocketAddressEnumerator
*
1097 g_network_address_connectable_proxy_enumerate (GSocketConnectable
*connectable
)
1099 GNetworkAddress
*self
= G_NETWORK_ADDRESS (connectable
);
1100 GSocketAddressEnumerator
*proxy_enum
;
1103 uri
= _g_uri_from_authority (self
->priv
->scheme
? self
->priv
->scheme
: "none",
1104 self
->priv
->hostname
,
1108 proxy_enum
= g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR
,
1109 "connectable", connectable
,
1119 g_network_address_connectable_to_string (GSocketConnectable
*connectable
)
1121 GNetworkAddress
*addr
;
1122 const gchar
*scheme
;
1124 GString
*out
; /* owned */
1126 addr
= G_NETWORK_ADDRESS (connectable
);
1127 out
= g_string_new ("");
1129 scheme
= g_network_address_get_scheme (addr
);
1131 g_string_append_printf (out
, "%s:", scheme
);
1133 g_string_append (out
, g_network_address_get_hostname (addr
));
1135 port
= g_network_address_get_port (addr
);
1137 g_string_append_printf (out
, ":%u", port
);
1139 return g_string_free (out
, FALSE
);