gnetworkaddress.c 40.7 KB
Newer Older
1 2 3 4 5
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2008 Red Hat, Inc.
6
 * Copyright (C) 2018 Igalia S.L.
7 8 9 10
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17 18
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 23 24 25
 */

#include "config.h"
#include <glib.h>
#include "glibintl.h"

26
#include <stdlib.h>
27 28 29 30
#include "gnetworkaddress.h"
#include "gasyncresult.h"
#include "ginetaddress.h"
#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 38 39 40 41 42 43
#include "gsocketconnectable.h"

#include <string.h>


/**
 * SECTION:gnetworkaddress
Matthias Clasen's avatar
Matthias Clasen committed
44
 * @short_description: A GSocketConnectable for resolving hostnames
45 46 47 48 49 50
 * @include: gio/gio.h
 *
 * #GNetworkAddress provides an easy way to resolve a hostname and
 * then attempt to connect to that host, handling the possibility of
 * multiple IP addresses and multiple address families.
 *
51
 * See #GSocketConnectable for an example of using the connectable
52
 * interface.
53
 */
54 55 56 57 58 59

/**
 * GNetworkAddress:
 *
 * A #GSocketConnectable for resolving a hostname and connecting to
 * that host.
60
 */
61 62 63 64 65

struct _GNetworkAddressPrivate {
  gchar *hostname;
  guint16 port;
  GList *sockaddrs;
66
  gchar *scheme;
67 68

  gint64 resolver_serial;
69 70 71 72 73 74
};

enum {
  PROP_0,
  PROP_HOSTNAME,
  PROP_PORT,
75
  PROP_SCHEME,
76 77 78 79 80 81 82 83 84 85 86
};

static void g_network_address_set_property (GObject      *object,
                                            guint         prop_id,
                                            const GValue *value,
                                            GParamSpec   *pspec);
static void g_network_address_get_property (GObject      *object,
                                            guint         prop_id,
                                            GValue       *value,
                                            GParamSpec   *pspec);

87 88 89
static void                      g_network_address_connectable_iface_init       (GSocketConnectableIface *iface);
static GSocketAddressEnumerator *g_network_address_connectable_enumerate        (GSocketConnectable      *connectable);
static GSocketAddressEnumerator	*g_network_address_connectable_proxy_enumerate  (GSocketConnectable      *connectable);
90
static gchar                    *g_network_address_connectable_to_string        (GSocketConnectable      *connectable);
91 92

G_DEFINE_TYPE_WITH_CODE (GNetworkAddress, g_network_address, G_TYPE_OBJECT,
93
                         G_ADD_PRIVATE (GNetworkAddress)
94 95 96 97 98 99 100 101 102
                         G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
                                                g_network_address_connectable_iface_init))

static void
g_network_address_finalize (GObject *object)
{
  GNetworkAddress *addr = G_NETWORK_ADDRESS (object);

  g_free (addr->priv->hostname);
103
  g_free (addr->priv->scheme);
104
  g_list_free_full (addr->priv->sockaddrs, g_object_unref);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

  G_OBJECT_CLASS (g_network_address_parent_class)->finalize (object);
}

static void
g_network_address_class_init (GNetworkAddressClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = g_network_address_set_property;
  gobject_class->get_property = g_network_address_get_property;
  gobject_class->finalize = g_network_address_finalize;

  g_object_class_install_property (gobject_class, PROP_HOSTNAME,
                                   g_param_spec_string ("hostname",
                                                        P_("Hostname"),
                                                        P_("Hostname to resolve"),
                                                        NULL,
123 124 125
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
126 127 128 129 130
  g_object_class_install_property (gobject_class, PROP_PORT,
                                   g_param_spec_uint ("port",
                                                      P_("Port"),
                                                      P_("Network port"),
                                                      0, 65535, 0,
131 132 133
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_STATIC_STRINGS));
134 135 136 137 138 139 140 141 142

  g_object_class_install_property (gobject_class, PROP_SCHEME,
                                   g_param_spec_string ("scheme",
                                                        P_("Scheme"),
                                                        P_("URI Scheme"),
                                                        NULL,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
143 144 145 146 147 148
}

static void
g_network_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
{
  connectable_iface->enumerate  = g_network_address_connectable_enumerate;
149
  connectable_iface->proxy_enumerate = g_network_address_connectable_proxy_enumerate;
150
  connectable_iface->to_string = g_network_address_connectable_to_string;
151 152 153 154 155
}

static void
g_network_address_init (GNetworkAddress *addr)
{
156
  addr->priv = g_network_address_get_instance_private (addr);
157 158 159 160 161 162 163 164 165 166
}

static void
g_network_address_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  GNetworkAddress *addr = G_NETWORK_ADDRESS (object);

167
  switch (prop_id)
168 169
    {
    case PROP_HOSTNAME:
Matthias Clasen's avatar
Matthias Clasen committed
170
      g_free (addr->priv->hostname);
171 172 173 174 175 176 177
      addr->priv->hostname = g_value_dup_string (value);
      break;

    case PROP_PORT:
      addr->priv->port = g_value_get_uint (value);
      break;

178
    case PROP_SCHEME:
179
      g_free (addr->priv->scheme);
180 181 182
      addr->priv->scheme = g_value_dup_string (value);
      break;

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

}

static void
g_network_address_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  GNetworkAddress *addr = G_NETWORK_ADDRESS (object);

  switch (prop_id)
199
    {
200 201 202 203 204 205 206 207
    case PROP_HOSTNAME:
      g_value_set_string (value, addr->priv->hostname);
      break;

    case PROP_PORT:
      g_value_set_uint (value, addr->priv->port);
      break;

208 209 210 211
    case PROP_SCHEME:
      g_value_set_string (value, addr->priv->scheme);
      break;

212 213 214 215 216 217 218
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

}

219 220 221 222 223 224 225 226
/*
 * g_network_address_add_addresses:
 * @addr: A #GNetworkAddress
 * @addresses: (transfer full): List of #GSocketAddress
 * @resolver_serial: Serial of #GResolver used
 *
 * Consumes address list and adds them to internal list.
 */
227
static void
228
g_network_address_add_addresses (GNetworkAddress *addr,
229 230
                                 GList           *addresses,
                                 guint64          resolver_serial)
231 232 233 234 235 236 237
{
  GList *a;
  GSocketAddress *sockaddr;

  for (a = addresses; a; a = a->next)
    {
      sockaddr = g_inet_socket_address_new (a->data, addr->priv->port);
238
      addr->priv->sockaddrs = g_list_append (addr->priv->sockaddrs, sockaddr);
239 240 241
      g_object_unref (a->data);
    }
  g_list_free (addresses);
242 243

  addr->priv->resolver_serial = resolver_serial;
244 245
}

246 247 248 249 250
static gboolean
g_network_address_parse_sockaddr (GNetworkAddress *addr)
{
  GSocketAddress *sockaddr;

251 252 253 254
  sockaddr = g_inet_socket_address_new_from_string (addr->priv->hostname,
                                                    addr->priv->port);
  if (sockaddr)
    {
255
      addr->priv->sockaddrs = g_list_append (addr->priv->sockaddrs, sockaddr);
256 257 258
      return TRUE;
    }
  else
259 260 261
    return FALSE;
}

262 263 264 265 266 267 268 269
/**
 * g_network_address_new:
 * @hostname: the hostname
 * @port: the port
 *
 * Creates a new #GSocketConnectable for connecting to the given
 * @hostname and @port.
 *
270 271 272 273 274 275
 * Note that depending on the configuration of the machine, a
 * @hostname of `localhost` may refer to the IPv4 loopback address
 * only, or to both IPv4 and IPv6; use
 * g_network_address_new_loopback() to create a #GNetworkAddress that
 * is guaranteed to resolve to both addresses.
 *
276
 * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress
277 278
 *
 * Since: 2.22
279
 */
280 281 282 283 284 285 286 287 288 289
GSocketConnectable *
g_network_address_new (const gchar *hostname,
                       guint16      port)
{
  return g_object_new (G_TYPE_NETWORK_ADDRESS,
                       "hostname", hostname,
                       "port", port,
                       NULL);
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/**
 * g_network_address_new_loopback:
 * @port: the port
 *
 * Creates a new #GSocketConnectable for connecting to the local host
 * over a loopback connection to the given @port. This is intended for
 * use in connecting to local services which may be running on IPv4 or
 * IPv6.
 *
 * The connectable will return IPv4 and IPv6 loopback addresses,
 * regardless of how the host resolves `localhost`. By contrast,
 * g_network_address_new() will often only return an IPv4 address when
 * resolving `localhost`, and an IPv6 address for `localhost6`.
 *
 * g_network_address_get_hostname() will always return `localhost` for
 * #GNetworkAddresses created with this constructor.
 *
 * Returns: (transfer full) (type GNetworkAddress): the new #GNetworkAddress
 *
 * Since: 2.44
 */
GSocketConnectable *
g_network_address_new_loopback (guint16 port)
{
  GNetworkAddress *addr;
  GList *addrs = NULL;

  addr = g_object_new (G_TYPE_NETWORK_ADDRESS,
                       "hostname", "localhost",
                       "port", port,
                       NULL);

  addrs = g_list_append (addrs, g_inet_address_new_loopback (AF_INET6));
  addrs = g_list_append (addrs, g_inet_address_new_loopback (AF_INET));
324
  g_network_address_add_addresses (addr, g_steal_pointer (&addrs), 0);
325 326 327 328

  return G_SOCKET_CONNECTABLE (addr);
}

329 330 331 332 333 334 335 336 337 338
/**
 * g_network_address_parse:
 * @host_and_port: the hostname and optionally a port
 * @default_port: the default port if not in @host_and_port
 * @error: a pointer to a #GError, or %NULL
 *
 * Creates a new #GSocketConnectable for connecting to the given
 * @hostname and @port. May fail and return %NULL in case
 * parsing @host_and_port fails.
 *
339
 * @host_and_port may be in any of a number of recognised formats; an IPv6
340
 * address, an IPv4 address, or a domain name (in which case a DNS
341 342
 * lookup is performed). Quoting with [] is supported for all address
 * types. A port override may be specified in the usual way with a
343
 * colon.
344 345 346 347
 *
 * If no port is specified in @host_and_port then @default_port will be
 * used as the port number to connect to.
 *
348
 * In general, @host_and_port is expected to be provided by the user
349
 * (allowing them to give the hostname, and a port override if necessary)
350
 * and @default_port is expected to be provided by the application.
351
 *
352 353 354 355 356
 * (The port component of @host_and_port can also be specified as a
 * service name rather than as a numeric port, but this functionality
 * is deprecated, because it depends on the contents of /etc/services,
 * which is generally quite sparse on platforms other than Linux.)
 *
357 358
 * Returns: (transfer full) (type GNetworkAddress): the new
 *   #GNetworkAddress, or %NULL on error
359 360
 *
 * Since: 2.22
361
 */
362
GSocketConnectable *
363 364 365
g_network_address_parse (const gchar  *host_and_port,
                         guint16       default_port,
                         GError      **error)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
  GSocketConnectable *connectable;
  const gchar *port;
  guint16 portnum;
  gchar *name;

  g_return_val_if_fail (host_and_port != NULL, NULL);

  port = NULL;
  if (host_and_port[0] == '[')
    /* escaped host part (to allow, eg. "[2001:db8::1]:888") */
    {
      const gchar *end;

      end = strchr (host_and_port, ']');
      if (end == NULL)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
384
                       _("Hostname “%s” contains “[” but not “]”"), host_and_port);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
          return NULL;
        }

      if (end[1] == '\0')
        port = NULL;
      else if (end[1] == ':')
        port = &end[2];
      else
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                       "The ']' character (in hostname '%s') must come at the"
                       " end or be immediately followed by ':' and a port",
                       host_and_port);
          return NULL;
        }

      name = g_strndup (host_and_port + 1, end - host_and_port - 1);
    }

  else if ((port = strchr (host_and_port, ':')))
    /* string has a ':' in it */
    {
      /* skip ':' */
      port++;

      if (strchr (port, ':'))
        /* more than one ':' in string */
        {
          /* this is actually an unescaped IPv6 address */
          name = g_strdup (host_and_port);
          port = NULL;
        }
      else
        name = g_strndup (host_and_port, port - host_and_port - 1);
    }

  else
    /* plain hostname, no port */
    name = g_strdup (host_and_port);

  if (port != NULL)
    {
      if (port[0] == '\0')
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                       "If a ':' character is given, it must be followed by a "
                       "port (in hostname '%s').", host_and_port);
          g_free (name);
          return NULL;
        }

      else if ('0' <= port[0] && port[0] <= '9')
        {
          char *end;
          long value;

          value = strtol (port, &end, 10);
          if (*end != '\0' || value < 0 || value > G_MAXUINT16)
            {
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                           "Invalid numeric port '%s' specified in hostname '%s'",
                           port, host_and_port);
              g_free (name);
              return NULL;
            }

          portnum = value;
        }

      else
        {
          struct servent *entry;

          entry = getservbyname (port, "tcp");
          if (entry == NULL)
            {
              g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                           "Unknown service '%s' specified in hostname '%s'",
                           port, host_and_port);
#ifdef HAVE_ENDSERVENT
              endservent ();
#endif
              g_free (name);
              return NULL;
            }

          portnum = g_ntohs (entry->s_port);

#ifdef HAVE_ENDSERVENT
          endservent ();
#endif
        }
    }
  else
    {
      /* No port in host_and_port */
      portnum = default_port;
    }

  connectable = g_network_address_new (name, portnum);
  g_free (name);

  return connectable;
}

490 491 492 493
/* Allowed characters outside alphanumeric for unreserved. */
#define G_URI_OTHER_UNRESERVED "-._~"

/* This or something equivalent will eventually go into glib/guri.h */
494
gboolean
495 496 497
_g_uri_parse_authority (const char  *uri,
		        char       **host,
		        guint16     *port,
498 499
		        char       **userinfo,
		        GError     **error)
500
{
501
  char *ascii_uri, *tmp_str;
502
  const char *start, *p, *at, *delim;
503 504 505 506 507 508 509 510 511 512 513 514 515
  char c;

  g_return_val_if_fail (uri != NULL, FALSE);

  if (host)
    *host = NULL;

  if (port)
    *port = 0;

  if (userinfo)
    *userinfo = NULL;

516 517 518 519 520
  /* Catch broken URIs early by trying to convert to ASCII. */
  ascii_uri = g_hostname_to_ascii (uri);
  if (!ascii_uri)
    goto error;

521 522 523 524 525 526 527 528
  /* From RFC 3986 Decodes:
   * URI          = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
   * hier-part    = "//" authority path-abempty
   * path-abempty = *( "/" segment )
   * authority    = [ userinfo "@" ] host [ ":" port ]
   */

  /* Check we have a valid scheme */
529
  tmp_str = g_uri_parse_scheme (ascii_uri);
530 531

  if (tmp_str == NULL)
532
    goto error;
533 534 535 536 537 538

  g_free (tmp_str);

  /* Decode hier-part:
   *  hier-part   = "//" authority path-abempty
   */
539
  p = ascii_uri;
540 541 542
  start = strstr (p, "//");

  if (start == NULL)
543
    goto error;
544 545 546

  start += 2;

547 548 549 550 551 552 553 554
  /* check if the @ sign is part of the authority before attempting to
   * decode the userinfo */
  delim = strpbrk (start, "/?#[]");
  at = strchr (start, '@');
  if (at && delim && at > delim)
    at = NULL;

  if (at != NULL)
555 556 557 558 559 560
    {
      /* Decode userinfo:
       * userinfo      = *( unreserved / pct-encoded / sub-delims / ":" )
       * unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
       * pct-encoded   = "%" HEXDIG HEXDIG
       */
561
      p = start;
562 563 564 565 566 567 568 569 570 571 572 573
      while (1)
	{
	  c = *p++;

	  if (c == '@')
	    break;

	  /* pct-encoded */
	  if (c == '%')
	    {
	      if (!(g_ascii_isxdigit (p[0]) ||
		    g_ascii_isxdigit (p[1])))
574
          goto error;
575 576 577 578 579 580 581

	      p++;

	      continue;
	    }

	  /* unreserved /  sub-delims / : */
582
	  if (!(g_ascii_isalnum (c) ||
583 584 585
		strchr (G_URI_OTHER_UNRESERVED, c) ||
		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
		c == ':'))
586
      goto error;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	}

      if (userinfo)
	*userinfo = g_strndup (start, p - start - 1);

      start = p;
    }
  else
    {
      p = start;
    }


  /* decode host:
   * host          = IP-literal / IPv4address / reg-name
   * reg-name      = *( unreserved / pct-encoded / sub-delims )
   */

  /* If IPv6 or IPvFuture */
  if (*p == '[')
    {
608 609
      gboolean has_scope_id = FALSE, has_bad_scope_id = FALSE;

610 611
      start++;
      p++;
612 613 614 615 616 617 618
      while (1)
	{
	  c = *p++;

	  if (c == ']')
	    break;

619 620 621 622 623 624 625 626
          if (c == '%' && !has_scope_id)
            {
              has_scope_id = TRUE;
              if (p[0] != '2' || p[1] != '5')
                has_bad_scope_id = TRUE;
              continue;
            }

627
	  /* unreserved /  sub-delims */
628
	  if (!(g_ascii_isalnum (c) ||
629 630 631 632
		strchr (G_URI_OTHER_UNRESERVED, c) ||
		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c) ||
		c == ':' ||
		c == '.'))
633
      goto error;
634
	}
635 636 637 638 639 640 641 642 643 644

      if (host)
        {
          if (has_bad_scope_id)
            *host = g_strndup (start, p - start - 1);
          else
            *host = g_uri_unescape_segment (start, p - 1, NULL);
        }

      c = *p++;
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    }
  else
    {
      while (1)
	{
	  c = *p++;

	  if (c == ':' ||
	      c == '/' ||
	      c == '?' ||
	      c == '#' ||
	      c == '\0')
	    break;

	  /* pct-encoded */
	  if (c == '%')
	    {
	      if (!(g_ascii_isxdigit (p[0]) ||
		    g_ascii_isxdigit (p[1])))
664
          goto error;
665 666 667 668 669 670 671

	      p++;

	      continue;
	    }

	  /* unreserved /  sub-delims */
672
	  if (!(g_ascii_isalnum (c) ||
673 674
		strchr (G_URI_OTHER_UNRESERVED, c) ||
		strchr (G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS, c)))
675
      goto error;
676 677
	}

678 679 680
      if (host)
        *host = g_uri_unescape_segment (start, p - 1, NULL);
    }
681

682 683
  if (c == ':')
    {
684
      /* Decode port:
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
       *  port          = *DIGIT
       */
      guint tmp = 0;

      while (1)
	{
	  c = *p++;

	  if (c == '/' ||
	      c == '?' ||
	      c == '#' ||
	      c == '\0')
	    break;

	  if (!g_ascii_isdigit (c))
700
      goto error;
701 702 703 704 705 706 707 708 709 710

	  tmp = (tmp * 10) + (c - '0');

	  if (tmp > 65535)
	    goto error;
	}
      if (port)
	*port = (guint16) tmp;
    }

711 712
  g_free (ascii_uri);

713 714 715
  return TRUE;

error:
716 717 718
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
               "Invalid URI ‘%s’", uri);

719 720 721 722 723 724 725 726 727 728 729 730
  if (host && *host)
    {
      g_free (*host);
      *host = NULL;
    }

  if (userinfo && *userinfo)
    {
      g_free (*userinfo);
      *userinfo = NULL;
    }

731 732
  g_free (ascii_uri);

733 734 735
  return FALSE;
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
gchar *
_g_uri_from_authority (const gchar *protocol,
                       const gchar *host,
                       guint        port,
                       const gchar *userinfo)
{
  GString *uri;

  uri = g_string_new (protocol);
  g_string_append (uri, "://");

  if (userinfo)
    {
      g_string_append_uri_escaped (uri, userinfo, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, FALSE);
      g_string_append_c (uri, '@');
    }

  if (g_hostname_is_non_ascii (host))
    {
      gchar *ace_encoded = g_hostname_to_ascii (host);

      if (!ace_encoded)
        {
          g_string_free (uri, TRUE);
          return NULL;
        }
      g_string_append (uri, ace_encoded);
      g_free (ace_encoded);
    }
  else if (strchr (host, ':'))
    g_string_append_printf (uri, "[%s]", host);
  else
    g_string_append (uri, host);

  if (port != 0)
    g_string_append_printf (uri, ":%u", port);

  return g_string_free (uri, FALSE);
}

776 777 778 779 780 781 782 783 784 785
/**
 * g_network_address_parse_uri:
 * @uri: the hostname and optionally a port
 * @default_port: The default port if none is found in the URI
 * @error: a pointer to a #GError, or %NULL
 *
 * Creates a new #GSocketConnectable for connecting to the given
 * @uri. May fail and return %NULL in case parsing @uri fails.
 *
 * Using this rather than g_network_address_new() or
786
 * g_network_address_parse() allows #GSocketClient to determine
787 788
 * when to use application-specific proxy protocols.
 *
789 790
 * Returns: (transfer full) (type GNetworkAddress): the new
 *   #GNetworkAddress, or %NULL on error
791 792 793 794 795 796 797 798 799 800 801 802 803
 *
 * Since: 2.26
 */
GSocketConnectable *
g_network_address_parse_uri (const gchar  *uri,
    			     guint16       default_port,
			     GError      **error)
{
  GSocketConnectable *conn;
  gchar *scheme;
  gchar *hostname;
  guint16 port;

804 805
  if (!_g_uri_parse_authority (uri, &hostname, &port, NULL, error))
    return NULL;
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

  if (port == 0)
    port = default_port;

  scheme = g_uri_parse_scheme (uri);

  conn = g_object_new (G_TYPE_NETWORK_ADDRESS,
                       "hostname", hostname,
                       "port", port,
                       "scheme", scheme,
                       NULL);

  g_free (scheme);
  g_free (hostname);

  return conn;
}

824 825 826 827 828 829 830
/**
 * g_network_address_get_hostname:
 * @addr: a #GNetworkAddress
 *
 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded,
 * depending on what @addr was created with.
 *
831
 * Returns: @addr's hostname
832 833
 *
 * Since: 2.22
834
 */
835 836 837 838 839 840 841 842 843 844 845 846 847 848
const gchar *
g_network_address_get_hostname (GNetworkAddress *addr)
{
  g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL);

  return addr->priv->hostname;
}

/**
 * g_network_address_get_port:
 * @addr: a #GNetworkAddress
 *
 * Gets @addr's port number
 *
849
 * Returns: @addr's port (which may be 0)
850 851
 *
 * Since: 2.22
852
 */
853 854 855 856 857 858 859 860
guint16
g_network_address_get_port (GNetworkAddress *addr)
{
  g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), 0);

  return addr->priv->port;
}

861 862 863 864 865 866
/**
 * g_network_address_get_scheme:
 * @addr: a #GNetworkAddress
 *
 * Gets @addr's scheme
 *
867
 * Returns: @addr's scheme (%NULL if not built from URI)
868 869 870 871 872 873 874 875 876 877 878
 *
 * Since: 2.26
 */
const gchar *
g_network_address_get_scheme (GNetworkAddress *addr)
{
  g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL);

  return addr->priv->scheme;
}

879 880 881 882 883 884
#define G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (_g_network_address_address_enumerator_get_type ())
#define G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, GNetworkAddressAddressEnumerator))

typedef struct {
  GSocketAddressEnumerator parent_instance;

885 886
  GNetworkAddress *addr; /* (owned) */
  GList *addresses; /* (owned) (nullable) */
887
  GList *last_tail; /* (unowned) (nullable) */
888 889 890 891 892
  GList *current_item; /* (unowned) (nullable) */
  GTask *queued_task; /* (owned) (nullable) */
  GError *last_error; /* (owned) (nullable) */
  GSource *wait_source; /* (owned) (nullable) */
  GMainContext *context; /* (owned) (nullable) */
893 894 895 896 897 898 899
} GNetworkAddressAddressEnumerator;

typedef struct {
  GSocketAddressEnumeratorClass parent_class;

} GNetworkAddressAddressEnumeratorClass;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
900
static GType _g_network_address_address_enumerator_get_type (void);
901 902 903 904 905 906 907 908
G_DEFINE_TYPE (GNetworkAddressAddressEnumerator, _g_network_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)

static void
g_network_address_address_enumerator_finalize (GObject *object)
{
  GNetworkAddressAddressEnumerator *addr_enum =
    G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (object);

909 910 911 912 913 914 915
  if (addr_enum->wait_source)
    {
      g_source_destroy (addr_enum->wait_source);
      g_clear_pointer (&addr_enum->wait_source, g_source_unref);
    }
  g_clear_object (&addr_enum->queued_task);
  g_clear_error (&addr_enum->last_error);
916
  g_object_unref (addr_enum->addr);
917 918
  g_clear_pointer (&addr_enum->addresses, g_list_free);
  g_clear_pointer (&addr_enum->context, g_main_context_unref);
919 920 921 922

  G_OBJECT_CLASS (_g_network_address_address_enumerator_parent_class)->finalize (object);
}

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
static inline GSocketFamily
get_address_family (GInetSocketAddress *address)
{
  return g_inet_address_get_family (g_inet_socket_address_get_address (address));
}

static void
list_split_families (GList  *list,
                     GList **out_ipv4,
                     GList **out_ipv6)
{
  g_assert (out_ipv4);
  g_assert (out_ipv6);

  while (list)
    {
      GSocketFamily family = get_address_family (list->data);
      switch (family)
        {
          case G_SOCKET_FAMILY_IPV4:
            *out_ipv4 = g_list_prepend (*out_ipv4, list->data);
            break;
          case G_SOCKET_FAMILY_IPV6:
            *out_ipv6 = g_list_prepend (*out_ipv6, list->data);
            break;
          case G_SOCKET_FAMILY_INVALID:
          case G_SOCKET_FAMILY_UNIX:
            g_assert_not_reached ();
        }

      list = g_list_next (list);
    }

  *out_ipv4 = g_list_reverse (*out_ipv4);
  *out_ipv6 = g_list_reverse (*out_ipv6);
}

static GList *
list_interleave_families (GList *list1,
                          GList *list2)
{
  GList *interleaved = NULL;

  while (list1 || list2)
    {
      if (list1)
        {
          interleaved = g_list_append (interleaved, list1->data);
          list1 = g_list_delete_link (list1, list1);
        }
      if (list2)
        {
          interleaved = g_list_append (interleaved, list2->data);
          list2 = g_list_delete_link (list2, list2);
        }
    }

  return interleaved;
}

/* list_copy_interleaved:
 * @list: (transfer container): List to copy
 *
 * Does a shallow copy of a list with address families interleaved.
 *
 * For example:
 *   Input: [ipv6, ipv6, ipv4, ipv4]
 *   Output: [ipv6, ipv4, ipv6, ipv4]
 *
 * Returns: (transfer container): A new list
 */
static GList *
list_copy_interleaved (GList *list)
{
  GList *ipv4 = NULL, *ipv6 = NULL;

  list_split_families (list, &ipv4, &ipv6);
  return list_interleave_families (ipv6, ipv4);
}

/* list_concat_interleaved:
 * @current_item: (transfer container): Already existing list
 * @new_list: (transfer none): New list to be interleaved and concatenated
 *
 * This differs from g_list_concat() + list_copy_interleaved() in that it sorts
 * items in the previous list starting from @current_item.
 *
 * Returns: (transfer container): New start of list
 */
static GList *
list_concat_interleaved (GList *current_item,
                         GList *new_list)
{
  GList *ipv4 = NULL, *ipv6 = NULL, *interleaved, *trailing = NULL;
  GSocketFamily last_family = G_SOCKET_FAMILY_IPV4; /* Default to starting with ipv6 */

  if (current_item)
    {
      last_family = get_address_family (current_item->data);

      /* Unused addresses will get removed, resorted, then readded */
      trailing = g_list_next (current_item);
      current_item->next = NULL;
    }

  list_split_families (trailing, &ipv4, &ipv6);
  list_split_families (new_list, &ipv4, &ipv6);

  if (trailing)
    g_list_free (trailing);

  if (last_family == G_SOCKET_FAMILY_IPV4)
    interleaved = list_interleave_families (ipv6, ipv4);
  else
    interleaved = list_interleave_families (ipv4, ipv6);

  return g_list_concat (current_item, interleaved);
}

1042 1043 1044 1045 1046 1047 1048 1049 1050
static GSocketAddress *
g_network_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
                                           GCancellable              *cancellable,
                                           GError                   **error)
{
  GNetworkAddressAddressEnumerator *addr_enum =
    G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
  GSocketAddress *sockaddr;

1051
  if (addr_enum->addresses == NULL)
1052
    {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
      GNetworkAddress *addr = addr_enum->addr;
      GResolver *resolver = g_resolver_get_default ();
      gint64 serial = g_resolver_get_serial (resolver);

      if (addr->priv->resolver_serial != 0 &&
          addr->priv->resolver_serial != serial)
        {
          /* Resolver has reloaded, discard cached addresses */
          g_list_free_full (addr->priv->sockaddrs, g_object_unref);
          addr->priv->sockaddrs = NULL;
        }

      if (!addr->priv->sockaddrs)
        g_network_address_parse_sockaddr (addr);
      if (!addr->priv->sockaddrs)
1068 1069
        {
          GList *addresses;
1070

1071
          addresses = g_resolver_lookup_by_name (resolver,
1072
                                                 addr->priv->hostname,
1073 1074
                                                 cancellable, error);
          if (!addresses)
1075 1076 1077 1078
            {
              g_object_unref (resolver);
              return NULL;
            }
1079

1080
          g_network_address_add_addresses (addr, g_steal_pointer (&addresses), serial);
1081
        }
1082

1083 1084
      addr_enum->current_item = addr_enum->addresses = list_copy_interleaved (addr->priv->sockaddrs);
      addr_enum->last_tail = g_list_last (addr->priv->sockaddrs);
1085
      g_object_unref (resolver);
1086 1087
    }

1088
  if (addr_enum->current_item == NULL)
1089
    return NULL;
1090

1091 1092
  sockaddr = addr_enum->current_item->data;
  addr_enum->current_item = g_list_next (addr_enum->current_item);
1093
  return g_object_ref (sockaddr);
1094 1095 1096
}

static void
1097 1098 1099
complete_queued_task (GNetworkAddressAddressEnumerator *addr_enum,
                      GTask                            *task,
                      GError                           *error)
1100
{
1101
  GSocketAddress *sockaddr;
1102

1103 1104
  addr_enum->current_item = addr_enum->addresses = list_copy_interleaved (addr_enum->addr->priv->sockaddrs);
  addr_enum->last_tail = g_list_last (addr_enum->addr->priv->sockaddrs);
1105

1106
  if (addr_enum->current_item)
1107
    sockaddr = g_object_ref (addr_enum->current_item->data);
1108 1109 1110
  else
    sockaddr = NULL;

1111 1112 1113
  if (error)
    g_task_return_error (task, error);
  else
1114 1115
    g_task_return_pointer (task, sockaddr, g_object_unref);
  g_object_unref (task);
1116 1117
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
static int
on_address_timeout (gpointer user_data)
{
  GNetworkAddressAddressEnumerator *addr_enum = user_data;

  /* If ipv6 didn't come in yet, just complete the task */
  if (addr_enum->queued_task != NULL)
    complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task),
                          g_steal_pointer (&addr_enum->last_error));

  g_clear_pointer (&addr_enum->wait_source, g_source_unref);
  return G_SOURCE_REMOVE;
}

1132
static void
1133 1134 1135
got_ipv6_addresses (GObject      *source_object,
                    GAsyncResult *result,
                    gpointer      user_data)
1136
{
1137
  GNetworkAddressAddressEnumerator *addr_enum = user_data;
1138 1139 1140 1141
  GResolver *resolver = G_RESOLVER (source_object);
  GList *addresses;
  GError *error = NULL;

1142 1143
  addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, &error);
  if (!error)
1144
    {
1145 1146 1147 1148 1149 1150 1151 1152
      /* Regardless of which responds first we add them to the enumerator
       * which does mean the timing of next_async() will potentially change
       * the results */
      g_network_address_add_addresses (addr_enum->addr, g_steal_pointer (&addresses),
                                       g_resolver_get_serial (resolver));
    }
  else
    g_debug ("IPv6 DNS error: %s", error->message);
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
  /* If ipv4 was first and waiting on us it can stop waiting */
  if (addr_enum->wait_source)
    {
      g_source_destroy (addr_enum->wait_source);
      g_clear_pointer (&addr_enum->wait_source, g_source_unref);
    }

  /* If we got an error before ipv4 then let it handle it.
   * If we get ipv6 response first or error second then
   * immediately complete the task.
   */
  if (error != NULL && !addr_enum->last_error)
    {
      addr_enum->last_error = g_steal_pointer (&error);

      /* This shouldn't happen often but avoid never responding. */
      addr_enum->wait_source = g_timeout_source_new_seconds (1);
      g_source_set_callback (addr_enum->wait_source,
                             on_address_timeout,
                             addr_enum, NULL);
      g_source_attach (addr_enum->wait_source, addr_enum->context);
    }
  else if (addr_enum->queued_task != NULL)
    {
      g_clear_error (&addr_enum->last_error);
      complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task),
                            g_steal_pointer (&error));
    }
  else if (error != NULL)
    g_clear_error (&error);

  g_object_unref (addr_enum);
}

static void
got_ipv4_addresses (GObject      *source_object,
                    GAsyncResult *result,
                    gpointer      user_data)
{
  GNetworkAddressAddressEnumerator *addr_enum = user_data;
  GResolver *resolver = G_RESOLVER (source_object);
  GList *addresses;
  GError *error = NULL;

  addresses = g_resolver_lookup_by_name_with_flags_finish (resolver, result, &error);
  if (!error)
    {
      g_network_address_add_addresses (addr_enum->addr, g_steal_pointer (&addresses),
                                       g_resolver_get_serial (resolver));
    }
  else
    g_debug ("IPv4 DNS error: %s", error->message);

  if (addr_enum->wait_source)
    {
      g_source_destroy (addr_enum->wait_source);
      g_clear_pointer (&addr_enum->wait_source, g_source_unref);
1211
    }
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

  /* If ipv6 already came in and errored then we return.
   * If ipv6 returned successfully then we don't need to do anything.
   * Otherwise we should wait a short while for ipv6 as RFC 8305 suggests.
   */
  if (addr_enum->last_error)
    {
      g_assert (addr_enum->queued_task);
      g_clear_error (&addr_enum->last_error);
      complete_queued_task (addr_enum, g_steal_pointer (&addr_enum->queued_task),
                            g_steal_pointer (&error));
    }
  else if (addr_enum->queued_task != NULL)
    {
      addr_enum->last_error = g_steal_pointer (&error);
      addr_enum->wait_source = g_timeout_source_new (50);
      g_source_set_callback (addr_enum->wait_source,
                             on_address_timeout,
                             addr_enum, NULL);
      g_source_attach (addr_enum->wait_source, addr_enum->context);
    }
  else if (error != NULL)
    g_clear_error (&error);

  g_object_unref (addr_enum);
1237 1238
}

1239 1240 1241 1242 1243 1244 1245 1246
static void
g_network_address_address_enumerator_next_async (GSocketAddressEnumerator  *enumerator,
                                                 GCancellable              *cancellable,
                                                 GAsyncReadyCallback        callback,
                                                 gpointer                   user_data)
{
  GNetworkAddressAddressEnumerator *addr_enum =
    G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
1247 1248
  GSocketAddress *sockaddr;
  GTask *task;
1249
  GNetworkAddress *addr = addr_enum->addr;
1250

1251
  task = g_task_new (addr_enum, cancellable, callback, user_data);
1252
  g_task_set_source_tag (task, g_network_address_address_enumerator_next_async);
1253

1254
  if (addr_enum->addresses == NULL)
1255
    {
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
      GNetworkAddress *addr = addr_enum->addr;
      GResolver *resolver = g_resolver_get_default ();
      gint64 serial = g_resolver_get_serial (resolver);

      if (addr->priv->resolver_serial != 0 &&
          addr->priv->resolver_serial != serial)
        {
          /* Resolver has reloaded, discard cached addresses */
          g_list_free_full (addr->priv->sockaddrs, g_object_unref);
          addr->priv->sockaddrs = NULL;
        }

1268
      if (addr->priv->sockaddrs == NULL)
1269
        {
1270
          if (g_network_address_parse_sockaddr (addr))
1271
            complete_queued_task (addr_enum, task, NULL);
1272 1273
          else
            {
1274 1275 1276 1277
              /* It does not make sense for this to be called multiple
               * times before the initial callback has been called */
              g_assert (addr_enum->queued_task == NULL);

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
              addr_enum->queued_task = g_steal_pointer (&task);
              /* Lookup in parallel as per RFC 8305 */
              g_resolver_lookup_by_name_with_flags_async (resolver,
                                                          addr->priv->hostname,
                                                          G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY,
                                                          cancellable,
                                                          got_ipv6_addresses, g_object_ref (addr_enum));
              g_resolver_lookup_by_name_with_flags_async (resolver,
                                                          addr->priv->hostname,
                                                          G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY,
                                                          cancellable,
                                                          got_ipv4_addresses, g_object_ref (addr_enum));
1290
            }
1291
          g_object_unref (resolver);
1292 1293
          return;
        }
1294

1295
      g_object_unref (resolver);
1296
    }
1297

1298 1299 1300
  if (addr_enum->addresses == NULL)
    {
      g_assert (addr->priv->sockaddrs);
1301 1302

      addr_enum->current_item = addr_enum->addresses = list_copy_interleaved (addr->priv->sockaddrs);
1303 1304
      sockaddr = g_object_ref (addr_enum->current_item->data);
    }
1305
  else
1306
    {
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
      GList *parent_tail = g_list_last (addr_enum->addr->priv->sockaddrs);

      if (addr_enum->last_tail != parent_tail)
        {
          addr_enum->current_item = list_concat_interleaved (addr_enum->current_item, g_list_next (addr_enum->last_tail));
          addr_enum->last_tail = parent_tail;
        }

      if (addr_enum->current_item->next)
        {
          addr_enum->current_item = g_list_next (addr_enum->current_item);
          sockaddr = g_object_ref (addr_enum->current_item->data);
        }
      else
        sockaddr = NULL;
1322 1323 1324 1325
    }

  g_task_return_pointer (task, sockaddr, g_object_unref);
  g_object_unref (task);
1326 1327 1328 1329 1330 1331 1332
}

static GSocketAddress *
g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator  *enumerator,
                                                  GAsyncResult              *result,
                                                  GError                   **error)
{
1333
  g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
1334

1335
  return g_task_propagate_pointer (G_TASK (result), error);
1336 1337 1338 1339 1340
}

static void
_g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator *enumerator)
{
1341
  enumerator->context = g_main_context_ref_thread_default ();
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
}

static void
_g_network_address_address_enumerator_class_init (GNetworkAddressAddressEnumeratorClass *addrenum_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (addrenum_class);
  GSocketAddressEnumeratorClass *enumerator_class =
    G_SOCKET_ADDRESS_ENUMERATOR_CLASS (addrenum_class);

  enumerator_class->next = g_network_address_address_enumerator_next;
  enumerator_class->next_async = g_network_address_address_enumerator_next_async;
  enumerator_class->next_finish = g_network_address_address_enumerator_next_finish;
  object_class->finalize = g_network_address_address_enumerator_finalize;
}

static GSocketAddressEnumerator *
g_network_address_connectable_enumerate (GSocketConnectable *connectable)
{
  GNetworkAddressAddressEnumerator *addr_enum;

  addr_enum = g_object_new (G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, NULL);
1363
  addr_enum->addr = g_object_ref (G_NETWORK_ADDRESS (connectable));
1364 1365 1366

  return (GSocketAddressEnumerator *)addr_enum;
}
1367 1368 1369 1370 1371 1372 1373 1374

static GSocketAddressEnumerator *
g_network_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
{
  GNetworkAddress *self = G_NETWORK_ADDRESS (connectable);
  GSocketAddressEnumerator *proxy_enum;
  gchar *uri;

1375 1376 1377 1378
  uri = _g_uri_from_authority (self->priv->scheme ? self->priv->scheme : "none",
                               self->priv->hostname,
                               self->priv->port,
                               NULL);
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

  proxy_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
                             "connectable", connectable,
      	       	       	     "uri", uri,
      	       	       	     NULL);

  g_free (uri);

  return proxy_enum;
}
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412

static gchar *
g_network_address_connectable_to_string (GSocketConnectable *connectable)
{
  GNetworkAddress *addr;
  const gchar *scheme;
  guint16 port;
  GString *out;  /* owned */

  addr = G_NETWORK_ADDRESS (connectable);
  out = g_string_new ("");

  scheme = g_network_address_get_scheme (addr);
  if (scheme != NULL)
    g_string_append_printf (out, "%s:", scheme);

  g_string_append (out, g_network_address_get_hostname (addr));

  port = g_network_address_get_port (addr);
  if (port != 0)
    g_string_append_printf (out, ":%u", port);

  return g_string_free (out, FALSE);
}