Do not expand autoptr macros when running introspection
[glib.git] / gio / gnetworkaddress.c
blob07f923a9057220c4eae431ffc3013470f302bba0
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.1 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/>.
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
25 #include <stdlib.h>
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"
33 #include "gtask.h"
34 #include "gsocketaddressenumerator.h"
35 #include "gioerror.h"
36 #include "gsocketconnectable.h"
38 #include <string.h>
41 /**
42 * SECTION:gnetworkaddress
43 * @short_description: A GSocketConnectable for resolving hostnames
44 * @include: gio/gio.h
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
51 * interface.
54 /**
55 * GNetworkAddress:
57 * A #GSocketConnectable for resolving a hostname and connecting to
58 * that host.
61 struct _GNetworkAddressPrivate {
62 gchar *hostname;
63 guint16 port;
64 GList *sockaddrs;
65 gchar *scheme;
67 gint64 resolver_serial;
70 enum {
71 PROP_0,
72 PROP_HOSTNAME,
73 PROP_PORT,
74 PROP_SCHEME,
77 static void g_network_address_set_property (GObject *object,
78 guint prop_id,
79 const GValue *value,
80 GParamSpec *pspec);
81 static void g_network_address_get_property (GObject *object,
82 guint prop_id,
83 GValue *value,
84 GParamSpec *pspec);
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))
96 static void
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);
108 static void
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",
119 P_("Hostname"),
120 P_("Hostname to resolve"),
121 NULL,
122 G_PARAM_READWRITE |
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",
127 P_("Port"),
128 P_("Network port"),
129 0, 65535, 0,
130 G_PARAM_READWRITE |
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",
136 P_("Scheme"),
137 P_("URI Scheme"),
138 NULL,
139 G_PARAM_READWRITE |
140 G_PARAM_CONSTRUCT_ONLY |
141 G_PARAM_STATIC_STRINGS));
144 static void
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;
152 static void
153 g_network_address_init (GNetworkAddress *addr)
155 addr->priv = g_network_address_get_instance_private (addr);
158 static void
159 g_network_address_set_property (GObject *object,
160 guint prop_id,
161 const GValue *value,
162 GParamSpec *pspec)
164 GNetworkAddress *addr = G_NETWORK_ADDRESS (object);
166 switch (prop_id)
168 case PROP_HOSTNAME:
169 g_free (addr->priv->hostname);
170 addr->priv->hostname = g_value_dup_string (value);
171 break;
173 case PROP_PORT:
174 addr->priv->port = g_value_get_uint (value);
175 break;
177 case PROP_SCHEME:
178 g_free (addr->priv->scheme);
179 addr->priv->scheme = g_value_dup_string (value);
180 break;
182 default:
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 break;
189 static void
190 g_network_address_get_property (GObject *object,
191 guint prop_id,
192 GValue *value,
193 GParamSpec *pspec)
195 GNetworkAddress *addr = G_NETWORK_ADDRESS (object);
197 switch (prop_id)
199 case PROP_HOSTNAME:
200 g_value_set_string (value, addr->priv->hostname);
201 break;
203 case PROP_PORT:
204 g_value_set_uint (value, addr->priv->port);
205 break;
207 case PROP_SCHEME:
208 g_value_set_string (value, addr->priv->scheme);
209 break;
211 default:
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 break;
218 static void
219 g_network_address_set_addresses (GNetworkAddress *addr,
220 GList *addresses,
221 guint64 resolver_serial)
223 GList *a;
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;
240 static gboolean
241 g_network_address_parse_sockaddr (GNetworkAddress *addr)
243 GSocketAddress *sockaddr;
245 sockaddr = g_inet_socket_address_new_from_string (addr->priv->hostname,
246 addr->priv->port);
247 if (sockaddr)
249 addr->priv->sockaddrs = g_list_prepend (addr->priv->sockaddrs, sockaddr);
250 return TRUE;
252 else
253 return FALSE;
257 * g_network_address_new:
258 * @hostname: the hostname
259 * @port: the port
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
272 * Since: 2.22
274 GSocketConnectable *
275 g_network_address_new (const gchar *hostname,
276 guint16 port)
278 return g_object_new (G_TYPE_NETWORK_ADDRESS,
279 "hostname", hostname,
280 "port", port,
281 NULL);
285 * g_network_address_new_loopback:
286 * @port: the port
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
291 * IPv6.
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
303 * Since: 2.44
305 GSocketConnectable *
306 g_network_address_new_loopback (guint16 port)
308 GNetworkAddress *addr;
309 GList *addrs = NULL;
311 addr = g_object_new (G_TYPE_NETWORK_ADDRESS,
312 "hostname", "localhost",
313 "port", port,
314 NULL);
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
337 * colon.
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
354 * Since: 2.22
356 GSocketConnectable *
357 g_network_address_parse (const gchar *host_and_port,
358 guint16 default_port,
359 GError **error)
361 GSocketConnectable *connectable;
362 const gchar *port;
363 guint16 portnum;
364 gchar *name;
366 g_return_val_if_fail (host_and_port != NULL, NULL);
368 port = NULL;
369 if (host_and_port[0] == '[')
370 /* escaped host part (to allow, eg. "[2001:db8::1]:888") */
372 const gchar *end;
374 end = strchr (host_and_port, ']');
375 if (end == NULL)
377 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
378 _("Hostname “%s” contains “[” but not “]”"), host_and_port);
379 return NULL;
382 if (end[1] == '\0')
383 port = NULL;
384 else if (end[1] == ':')
385 port = &end[2];
386 else
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",
391 host_and_port);
392 return NULL;
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 */
401 /* skip ':' */
402 port++;
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);
409 port = NULL;
411 else
412 name = g_strndup (host_and_port, port - host_and_port - 1);
415 else
416 /* plain hostname, no port */
417 name = g_strdup (host_and_port);
419 if (port != NULL)
421 if (port[0] == '\0')
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);
426 g_free (name);
427 return NULL;
430 else if ('0' <= port[0] && port[0] <= '9')
432 char *end;
433 long value;
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);
441 g_free (name);
442 return NULL;
445 portnum = value;
448 else
450 struct servent *entry;
452 entry = getservbyname (port, "tcp");
453 if (entry == NULL)
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
459 endservent ();
460 #endif
461 g_free (name);
462 return NULL;
465 portnum = g_ntohs (entry->s_port);
467 #ifdef HAVE_ENDSERVENT
468 endservent ();
469 #endif
472 else
474 /* No port in host_and_port */
475 portnum = default_port;
478 connectable = g_network_address_new (name, portnum);
479 g_free (name);
481 return connectable;
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 */
488 gboolean
489 _g_uri_parse_authority (const char *uri,
490 char **host,
491 guint16 *port,
492 char **userinfo)
494 char *tmp_str;
495 const char *start, *p, *at, *delim;
496 char c;
498 g_return_val_if_fail (uri != NULL, FALSE);
500 if (host)
501 *host = NULL;
503 if (port)
504 *port = 0;
506 if (userinfo)
507 *userinfo = NULL;
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);
519 if (tmp_str == NULL)
520 return FALSE;
522 g_free (tmp_str);
524 /* Decode hier-part:
525 * hier-part = "//" authority path-abempty
527 p = uri;
528 start = strstr (p, "//");
530 if (start == NULL)
531 return FALSE;
533 start += 2;
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)
540 at = NULL;
542 if (at != NULL)
544 /* Decode userinfo:
545 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
546 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
547 * pct-encoded = "%" HEXDIG HEXDIG
549 p = start;
550 while (1)
552 c = *p++;
554 if (c == '@')
555 break;
557 /* pct-encoded */
558 if (c == '%')
560 if (!(g_ascii_isxdigit (p[0]) ||
561 g_ascii_isxdigit (p[1])))
562 return FALSE;
564 p++;
566 continue;
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) ||
573 c == ':'))
574 return FALSE;
577 if (userinfo)
578 *userinfo = g_strndup (start, p - start - 1);
580 start = p;
582 else
584 p = start;
588 /* decode host:
589 * host = IP-literal / IPv4address / reg-name
590 * reg-name = *( unreserved / pct-encoded / sub-delims )
593 /* If IPv6 or IPvFuture */
594 if (*p == '[')
596 gboolean has_scope_id = FALSE, has_bad_scope_id = FALSE;
598 start++;
599 p++;
600 while (1)
602 c = *p++;
604 if (c == ']')
605 break;
607 if (c == '%' && !has_scope_id)
609 has_scope_id = TRUE;
610 if (p[0] != '2' || p[1] != '5')
611 has_bad_scope_id = TRUE;
612 continue;
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) ||
619 c == ':' ||
620 c == '.'))
621 goto error;
624 if (host)
626 if (has_bad_scope_id)
627 *host = g_strndup (start, p - start - 1);
628 else
629 *host = g_uri_unescape_segment (start, p - 1, NULL);
632 c = *p++;
634 else
636 while (1)
638 c = *p++;
640 if (c == ':' ||
641 c == '/' ||
642 c == '?' ||
643 c == '#' ||
644 c == '\0')
645 break;
647 /* pct-encoded */
648 if (c == '%')
650 if (!(g_ascii_isxdigit (p[0]) ||
651 g_ascii_isxdigit (p[1])))
652 goto error;
654 p++;
656 continue;
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)))
663 goto error;
666 if (host)
667 *host = g_uri_unescape_segment (start, p - 1, NULL);
670 if (c == ':')
672 /* Decode port:
673 * port = *DIGIT
675 guint tmp = 0;
677 while (1)
679 c = *p++;
681 if (c == '/' ||
682 c == '?' ||
683 c == '#' ||
684 c == '\0')
685 break;
687 if (!g_ascii_isdigit (c))
688 goto error;
690 tmp = (tmp * 10) + (c - '0');
692 if (tmp > 65535)
693 goto error;
695 if (port)
696 *port = (guint16) tmp;
699 return TRUE;
701 error:
702 if (host && *host)
704 g_free (*host);
705 *host = NULL;
708 if (userinfo && *userinfo)
710 g_free (*userinfo);
711 *userinfo = NULL;
714 return FALSE;
717 gchar *
718 _g_uri_from_authority (const gchar *protocol,
719 const gchar *host,
720 guint port,
721 const gchar *userinfo)
723 GString *uri;
725 uri = g_string_new (protocol);
726 g_string_append (uri, "://");
728 if (userinfo)
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);
738 if (!ace_encoded)
740 g_string_free (uri, TRUE);
741 return NULL;
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);
748 else
749 g_string_append (uri, host);
751 if (port != 0)
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
773 * Since: 2.26
775 GSocketConnectable *
776 g_network_address_parse_uri (const gchar *uri,
777 guint16 default_port,
778 GError **error)
780 GSocketConnectable *conn;
781 gchar *scheme;
782 gchar *hostname;
783 guint16 port;
785 if (!_g_uri_parse_authority (uri, &hostname, &port, NULL))
787 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
788 "Invalid URI '%s'",
789 uri);
790 return NULL;
793 if (port == 0)
794 port = default_port;
796 scheme = g_uri_parse_scheme (uri);
798 conn = g_object_new (G_TYPE_NETWORK_ADDRESS,
799 "hostname", hostname,
800 "port", port,
801 "scheme", scheme,
802 NULL);
804 g_free (scheme);
805 g_free (hostname);
807 return conn;
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
819 * Since: 2.22
821 const gchar *
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)
837 * Since: 2.22
839 guint16
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)
855 * Since: 2.26
857 const gchar *
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))
868 typedef struct {
869 GSocketAddressEnumerator parent_instance;
871 GNetworkAddress *addr;
872 GList *addresses;
873 GList *next;
874 } GNetworkAddressAddressEnumerator;
876 typedef struct {
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)
884 static void
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,
898 GError **error)
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)
922 GList *addresses;
924 addresses = g_resolver_lookup_by_name (resolver,
925 addr->priv->hostname,
926 cancellable, error);
927 if (!addresses)
929 g_object_unref (resolver);
930 return NULL;
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)
942 return NULL;
944 sockaddr = addr_enum->next->data;
945 addr_enum->next = addr_enum->next->next;
946 return g_object_ref (sockaddr);
949 static void
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;
958 if (addr_enum->next)
960 sockaddr = g_object_ref (addr_enum->next->data);
961 addr_enum->next = addr_enum->next->next;
963 else
964 sockaddr = NULL;
966 if (error)
967 g_task_return_error (task, error);
968 else
969 g_task_return_pointer (task, sockaddr, g_object_unref);
970 g_object_unref (task);
973 static void
974 got_addresses (GObject *source_object,
975 GAsyncResult *result,
976 gpointer user_data)
978 GTask *task = user_data;
979 GNetworkAddressAddressEnumerator *addr_enum = g_task_get_source_object (task);
980 GResolver *resolver = G_RESOLVER (source_object);
981 GList *addresses;
982 GError *error = NULL;
984 if (!addr_enum->addr->priv->sockaddrs)
986 addresses = g_resolver_lookup_by_name_finish (resolver, result, &error);
988 if (!error)
990 g_network_address_set_addresses (addr_enum->addr, addresses,
991 g_resolver_get_serial (resolver));
994 have_addresses (addr_enum, task, error);
997 static void
998 g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
999 GCancellable *cancellable,
1000 GAsyncReadyCallback callback,
1001 gpointer user_data)
1003 GNetworkAddressAddressEnumerator *addr_enum =
1004 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
1005 GSocketAddress *sockaddr;
1006 GTask *task;
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);
1029 else
1031 g_resolver_lookup_by_name_async (resolver,
1032 addr->priv->hostname,
1033 cancellable,
1034 got_addresses, task);
1036 g_object_unref (resolver);
1037 return;
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;
1050 else
1051 sockaddr = NULL;
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,
1060 GError **error)
1062 g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
1064 return g_task_propagate_pointer (G_TASK (result), error);
1067 static void
1068 _g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator *enumerator)
1072 static void
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 (G_NETWORK_ADDRESS (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;
1101 gchar *uri;
1103 uri = _g_uri_from_authority (self->priv->scheme ? self->priv->scheme : "none",
1104 self->priv->hostname,
1105 self->priv->port,
1106 NULL);
1108 proxy_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
1109 "connectable", connectable,
1110 "uri", uri,
1111 NULL);
1113 g_free (uri);
1115 return proxy_enum;
1118 static gchar *
1119 g_network_address_connectable_to_string (GSocketConnectable *connectable)
1121 GNetworkAddress *addr;
1122 const gchar *scheme;
1123 guint16 port;
1124 GString *out; /* owned */
1126 addr = G_NETWORK_ADDRESS (connectable);
1127 out = g_string_new ("");
1129 scheme = g_network_address_get_scheme (addr);
1130 if (scheme != NULL)
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);
1136 if (port != 0)
1137 g_string_append_printf (out, ":%u", port);
1139 return g_string_free (out, FALSE);