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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
28 #include "gnetworkaddress.h"
29 #include "gasyncresult.h"
30 #include "ginetaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "gnetworkingprivate.h"
33 #include "gresolver.h"
34 #include "gsimpleasyncresult.h"
35 #include "gsocketaddressenumerator.h"
37 #include "gsocketconnectable.h"
44 * SECTION:gnetworkaddress
45 * @short_description: A GSocketConnectable for resolving hostnames
48 * #GNetworkAddress provides an easy way to resolve a hostname and
49 * then attempt to connect to that host, handling the possibility of
50 * multiple IP addresses and multiple address families.
52 * See #GSocketConnectable for and example of using the connectable
59 * A #GSocketConnectable for resolving a hostname and connecting to
63 struct _GNetworkAddressPrivate {
75 static void g_network_address_set_property (GObject *object,
79 static void g_network_address_get_property (GObject *object,
84 static void g_network_address_connectable_iface_init (GSocketConnectableIface *iface);
85 static GSocketAddressEnumerator *g_network_address_connectable_enumerate (GSocketConnectable *connectable);
87 G_DEFINE_TYPE_WITH_CODE (GNetworkAddress, g_network_address, G_TYPE_OBJECT,
88 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
89 g_network_address_connectable_iface_init))
92 g_network_address_finalize (GObject *object)
94 GNetworkAddress *addr = G_NETWORK_ADDRESS (object);
96 g_free (addr->priv->hostname);
98 if (addr->priv->sockaddrs)
102 for (a = addr->priv->sockaddrs; a; a = a->next)
103 g_object_unref (a->data);
104 g_list_free (addr->priv->sockaddrs);
107 G_OBJECT_CLASS (g_network_address_parent_class)->finalize (object);
111 g_network_address_class_init (GNetworkAddressClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GNetworkAddressPrivate));
117 gobject_class->set_property = g_network_address_set_property;
118 gobject_class->get_property = g_network_address_get_property;
119 gobject_class->finalize = g_network_address_finalize;
121 g_object_class_install_property (gobject_class, PROP_HOSTNAME,
122 g_param_spec_string ("hostname",
124 P_("Hostname to resolve"),
127 G_PARAM_CONSTRUCT_ONLY |
128 G_PARAM_STATIC_STRINGS));
129 g_object_class_install_property (gobject_class, PROP_PORT,
130 g_param_spec_uint ("port",
135 G_PARAM_CONSTRUCT_ONLY |
136 G_PARAM_STATIC_STRINGS));
140 g_network_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
142 connectable_iface->enumerate = g_network_address_connectable_enumerate;
146 g_network_address_init (GNetworkAddress *addr)
148 addr->priv = G_TYPE_INSTANCE_GET_PRIVATE (addr, G_TYPE_NETWORK_ADDRESS,
149 GNetworkAddressPrivate);
153 g_network_address_set_property (GObject *object,
158 GNetworkAddress *addr = G_NETWORK_ADDRESS (object);
163 if (addr->priv->hostname)
164 g_free (addr->priv->hostname);
165 addr->priv->hostname = g_value_dup_string (value);
169 addr->priv->port = g_value_get_uint (value);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 g_network_address_get_property (GObject *object,
185 GNetworkAddress *addr = G_NETWORK_ADDRESS (object);
190 g_value_set_string (value, addr->priv->hostname);
194 g_value_set_uint (value, addr->priv->port);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 g_network_address_set_addresses (GNetworkAddress *addr,
209 GSocketAddress *sockaddr;
211 g_return_if_fail (addresses != NULL && addr->priv->sockaddrs == NULL);
213 for (a = addresses; a; a = a->next)
215 sockaddr = g_inet_socket_address_new (a->data, addr->priv->port);
216 addr->priv->sockaddrs = g_list_prepend (addr->priv->sockaddrs, sockaddr);
217 g_object_unref (a->data);
219 g_list_free (addresses);
220 addr->priv->sockaddrs = g_list_reverse (addr->priv->sockaddrs);
224 * g_network_address_new:
225 * @hostname: the hostname
228 * Creates a new #GSocketConnectable for connecting to the given
229 * @hostname and @port.
231 * Return value: the new #GNetworkAddress
236 g_network_address_new (const gchar *hostname,
239 return g_object_new (G_TYPE_NETWORK_ADDRESS,
240 "hostname", hostname,
246 * g_network_address_parse:
247 * @host_and_port: the hostname and optionally a port
248 * @default_port: the default port if not in @host_and_port
249 * @error: a pointer to a #GError, or %NULL
251 * Creates a new #GSocketConnectable for connecting to the given
252 * @hostname and @port. May fail and return %NULL in case
253 * parsing @host_and_port fails.
255 * @host_and_port may be in any of a number of recognised formats: an IPv6
256 * address, an IPv4 address, or a domain name (in which case a DNS
257 * lookup is performed). Quoting with [] is supported for all address
258 * types. A port override may be specified in the usual way with a
259 * colon. Ports may be given as decimal numbers or symbolic names (in
260 * which case an /etc/services lookup is performed).
262 * If no port is specified in @host_and_port then @default_port will be
263 * used as the port number to connect to.
265 * In general, @host_and_port is expected to be provided by the user
266 * (allowing them to give the hostname, and a port overide if necessary)
267 * and @default_port is expected to be provided by the application.
269 * Return value: the new #GNetworkAddress, or %NULL on error
274 g_network_address_parse (const gchar *host_and_port,
275 guint16 default_port,
278 GSocketConnectable *connectable;
283 g_return_val_if_fail (host_and_port != NULL, NULL);
286 if (host_and_port[0] == '[')
287 /* escaped host part (to allow, eg. "[2001:db8::1]:888") */
291 end = strchr (host_and_port, ']');
294 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
295 _("Hostname '%s' contains '[' but not ']'"), host_and_port);
301 else if (end[1] == ':')
305 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
306 "The ']' character (in hostname '%s') must come at the"
307 " end or be immediately followed by ':' and a port",
312 name = g_strndup (host_and_port + 1, end - host_and_port - 1);
315 else if ((port = strchr (host_and_port, ':')))
316 /* string has a ':' in it */
321 if (strchr (port, ':'))
322 /* more than one ':' in string */
324 /* this is actually an unescaped IPv6 address */
325 name = g_strdup (host_and_port);
329 name = g_strndup (host_and_port, port - host_and_port - 1);
333 /* plain hostname, no port */
334 name = g_strdup (host_and_port);
340 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
341 "If a ':' character is given, it must be followed by a "
342 "port (in hostname '%s').", host_and_port);
347 else if ('0' <= port[0] && port[0] <= '9')
352 value = strtol (port, &end, 10);
353 if (*end != '\0' || value < 0 || value > G_MAXUINT16)
355 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
356 "Invalid numeric port '%s' specified in hostname '%s'",
357 port, host_and_port);
367 struct servent *entry;
369 entry = getservbyname (port, "tcp");
372 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
373 "Unknown service '%s' specified in hostname '%s'",
374 port, host_and_port);
375 #ifdef HAVE_ENDSERVENT
382 portnum = g_ntohs (entry->s_port);
384 #ifdef HAVE_ENDSERVENT
391 /* No port in host_and_port */
392 portnum = default_port;
395 connectable = g_network_address_new (name, portnum);
402 * g_network_address_get_hostname:
403 * @addr: a #GNetworkAddress
405 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded,
406 * depending on what @addr was created with.
408 * Return value: @addr's hostname
413 g_network_address_get_hostname (GNetworkAddress *addr)
415 g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), NULL);
417 return addr->priv->hostname;
421 * g_network_address_get_port:
422 * @addr: a #GNetworkAddress
424 * Gets @addr's port number
426 * Return value: @addr's port (which may be 0)
431 g_network_address_get_port (GNetworkAddress *addr)
433 g_return_val_if_fail (G_IS_NETWORK_ADDRESS (addr), 0);
435 return addr->priv->port;
438 #define G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (_g_network_address_address_enumerator_get_type ())
439 #define G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, GNetworkAddressAddressEnumerator))
442 GSocketAddressEnumerator parent_instance;
444 GNetworkAddress *addr;
446 } GNetworkAddressAddressEnumerator;
449 GSocketAddressEnumeratorClass parent_class;
451 } GNetworkAddressAddressEnumeratorClass;
453 G_DEFINE_TYPE (GNetworkAddressAddressEnumerator, _g_network_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
456 g_network_address_address_enumerator_finalize (GObject *object)
458 GNetworkAddressAddressEnumerator *addr_enum =
459 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (object);
461 g_object_unref (addr_enum->addr);
463 G_OBJECT_CLASS (_g_network_address_address_enumerator_parent_class)->finalize (object);
466 static GSocketAddress *
467 g_network_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
468 GCancellable *cancellable,
471 GNetworkAddressAddressEnumerator *addr_enum =
472 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
473 GSocketAddress *sockaddr;
475 if (!addr_enum->addr->priv->sockaddrs)
477 GResolver *resolver = g_resolver_get_default ();
480 addresses = g_resolver_lookup_by_name (resolver,
481 addr_enum->addr->priv->hostname,
483 g_object_unref (resolver);
488 g_network_address_set_addresses (addr_enum->addr, addresses);
489 addr_enum->a = addr_enum->addr->priv->sockaddrs;
496 sockaddr = addr_enum->a->data;
497 addr_enum->a = addr_enum->a->next;
498 return g_object_ref (sockaddr);
503 got_addresses (GObject *source_object,
504 GAsyncResult *result,
507 GSimpleAsyncResult *simple = user_data;
508 GNetworkAddressAddressEnumerator *addr_enum =
509 g_simple_async_result_get_op_res_gpointer (simple);
510 GResolver *resolver = G_RESOLVER (source_object);
512 GError *error = NULL;
514 addresses = g_resolver_lookup_by_name_finish (resolver, result, &error);
515 if (!addr_enum->addr->priv->sockaddrs)
519 g_simple_async_result_set_from_error (simple, error);
520 g_error_free (error);
524 g_network_address_set_addresses (addr_enum->addr, addresses);
525 addr_enum->a = addr_enum->addr->priv->sockaddrs;
529 g_error_free (error);
531 g_object_unref (resolver);
533 g_simple_async_result_complete (simple);
534 g_object_unref (simple);
538 g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
539 GCancellable *cancellable,
540 GAsyncReadyCallback callback,
543 GNetworkAddressAddressEnumerator *addr_enum =
544 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
545 GSimpleAsyncResult *simple;
547 simple = g_simple_async_result_new (G_OBJECT (enumerator),
549 g_network_address_address_enumerator_next_async);
551 if (!addr_enum->addr->priv->sockaddrs)
553 GResolver *resolver = g_resolver_get_default ();
555 g_simple_async_result_set_op_res_gpointer (simple, g_object_ref (addr_enum), g_object_unref);
556 g_resolver_lookup_by_name_async (resolver,
557 addr_enum->addr->priv->hostname,
559 got_addresses, simple);
563 g_simple_async_result_complete_in_idle (simple);
564 g_object_unref (simple);
568 static GSocketAddress *
569 g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
570 GAsyncResult *result,
573 GNetworkAddressAddressEnumerator *addr_enum =
574 G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
575 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
576 GSocketAddress *sockaddr;
578 if (g_simple_async_result_propagate_error (simple, error))
580 else if (!addr_enum->a)
584 sockaddr = addr_enum->a->data;
585 addr_enum->a = addr_enum->a->next;
586 return g_object_ref (sockaddr);
591 _g_network_address_address_enumerator_init (GNetworkAddressAddressEnumerator *enumerator)
596 _g_network_address_address_enumerator_class_init (GNetworkAddressAddressEnumeratorClass *addrenum_class)
598 GObjectClass *object_class = G_OBJECT_CLASS (addrenum_class);
599 GSocketAddressEnumeratorClass *enumerator_class =
600 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (addrenum_class);
602 enumerator_class->next = g_network_address_address_enumerator_next;
603 enumerator_class->next_async = g_network_address_address_enumerator_next_async;
604 enumerator_class->next_finish = g_network_address_address_enumerator_next_finish;
605 object_class->finalize = g_network_address_address_enumerator_finalize;
608 static GSocketAddressEnumerator *
609 g_network_address_connectable_enumerate (GSocketConnectable *connectable)
611 GNetworkAddressAddressEnumerator *addr_enum;
613 addr_enum = g_object_new (G_TYPE_NETWORK_ADDRESS_ADDRESS_ENUMERATOR, NULL);
614 addr_enum->addr = g_object_ref (connectable);
616 return (GSocketAddressEnumerator *)addr_enum;
619 #define __G_NETWORK_ADDRESS_C__
620 #include "gioaliasdef.c"