1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
26 #include "ginetsocketaddress.h"
27 #include "ginetaddress.h"
28 #include "gnetworkingprivate.h"
29 #include "gsocketconnectable.h"
35 * SECTION:ginetsocketaddress
36 * @short_description: Internet GSocketAddress
39 * An IPv4 or IPv6 socket address; that is, the combination of a
40 * #GInetAddress and a port number.
46 * An IPv4 or IPv6 socket address, corresponding to a struct
47 * sockaddr_in or struct sockaddr_in6.
50 struct _GInetSocketAddressPrivate
52 GInetAddress *address;
58 static void g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
59 static gchar *g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable);
61 G_DEFINE_TYPE_WITH_CODE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS,
62 G_ADD_PRIVATE (GInetSocketAddress)
63 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
64 g_inet_socket_address_connectable_iface_init))
75 g_inet_socket_address_dispose (GObject *object)
77 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
79 g_clear_object (&(address->priv->address));
81 G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
85 g_inet_socket_address_get_property (GObject *object,
90 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
95 g_value_set_object (value, address->priv->address);
99 g_value_set_uint (value, address->priv->port);
103 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
104 g_value_set_uint (value, address->priv->flowinfo);
108 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
109 g_value_set_uint (value, address->priv->scope_id);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_inet_socket_address_set_property (GObject *object,
123 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
128 address->priv->address = g_object_ref (g_value_get_object (value));
132 address->priv->port = (guint16) g_value_get_uint (value);
136 /* We can't test that address->priv->address is IPv6 here,
137 * since this property might get set before PROP_ADDRESS.
139 address->priv->flowinfo = g_value_get_uint (value);
143 address->priv->scope_id = g_value_get_uint (value);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 g_inet_socket_address_get_family (GSocketAddress *address)
154 GInetSocketAddress *addr;
156 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
158 addr = G_INET_SOCKET_ADDRESS (address);
160 return g_inet_address_get_family (addr->priv->address);
164 g_inet_socket_address_get_native_size (GSocketAddress *address)
166 GInetSocketAddress *addr;
167 GSocketFamily family;
169 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
171 addr = G_INET_SOCKET_ADDRESS (address);
172 family = g_inet_address_get_family (addr->priv->address);
174 if (family == AF_INET)
175 return sizeof (struct sockaddr_in);
176 else if (family == AF_INET6)
177 return sizeof (struct sockaddr_in6);
183 g_inet_socket_address_to_native (GSocketAddress *address,
188 GInetSocketAddress *addr;
189 GSocketFamily family;
191 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
193 addr = G_INET_SOCKET_ADDRESS (address);
194 family = g_inet_address_get_family (addr->priv->address);
196 if (family == AF_INET)
198 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
200 if (destlen < sizeof (*sock))
202 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
203 _("Not enough space for socket address"));
207 sock->sin_family = AF_INET;
208 sock->sin_port = g_htons (addr->priv->port);
209 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
210 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
213 else if (family == AF_INET6)
215 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
217 if (destlen < sizeof (*sock))
219 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
220 _("Not enough space for socket address"));
224 memset (sock, 0, sizeof (*sock));
225 sock->sin6_family = AF_INET6;
226 sock->sin6_port = g_htons (addr->priv->port);
227 sock->sin6_flowinfo = addr->priv->flowinfo;
228 sock->sin6_scope_id = addr->priv->scope_id;
229 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
234 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
235 _("Unsupported socket address"));
241 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
243 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
244 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
246 gobject_class->dispose = g_inet_socket_address_dispose;
247 gobject_class->set_property = g_inet_socket_address_set_property;
248 gobject_class->get_property = g_inet_socket_address_get_property;
250 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
251 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
252 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
254 g_object_class_install_property (gobject_class, PROP_ADDRESS,
255 g_param_spec_object ("address",
259 G_PARAM_CONSTRUCT_ONLY |
261 G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_PORT,
264 g_param_spec_uint ("port",
270 G_PARAM_CONSTRUCT_ONLY |
272 G_PARAM_STATIC_STRINGS));
275 * GInetSocketAddress:flowinfo:
277 * The `sin6_flowinfo` field, for IPv6 addresses.
281 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
282 g_param_spec_uint ("flowinfo",
284 P_("IPv6 flow info"),
288 G_PARAM_CONSTRUCT_ONLY |
290 G_PARAM_STATIC_STRINGS));
293 * GInetSocketAddress:scope_id:
295 * The `sin6_scope_id` field, for IPv6 addresses.
299 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
300 g_param_spec_uint ("scope-id",
306 G_PARAM_CONSTRUCT_ONLY |
308 G_PARAM_STATIC_STRINGS));
312 g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
314 GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
316 iface->enumerate = parent_iface->enumerate;
317 iface->proxy_enumerate = parent_iface->proxy_enumerate;
318 iface->to_string = g_inet_socket_address_connectable_to_string;
322 g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable)
324 GInetSocketAddress *sa;
330 sa = G_INET_SOCKET_ADDRESS (connectable);
331 a = g_inet_socket_address_get_address (sa);
332 out = g_string_new ("");
335 a_string = g_inet_address_to_string (a);
336 g_string_append (out, a_string);
339 /* Scope ID (IPv6 only). */
340 if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6 &&
341 g_inet_socket_address_get_scope_id (sa) != 0)
343 g_string_append_printf (out, "%%%u",
344 g_inet_socket_address_get_scope_id (sa));
348 port = g_inet_socket_address_get_port (sa);
351 /* Disambiguate ports from IPv6 addresses using square brackets. */
352 if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6)
354 g_string_prepend (out, "[");
355 g_string_append (out, "]");
358 g_string_append_printf (out, ":%u", port);
361 return g_string_free (out, FALSE);
365 g_inet_socket_address_init (GInetSocketAddress *address)
367 address->priv = g_inet_socket_address_get_instance_private (address);
371 * g_inet_socket_address_new:
372 * @address: a #GInetAddress
373 * @port: a port number
375 * Creates a new #GInetSocketAddress for @address and @port.
377 * Returns: a new #GInetSocketAddress
382 g_inet_socket_address_new (GInetAddress *address,
385 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
392 * g_inet_socket_address_new_from_string:
393 * @address: the string form of an IP address
394 * @port: a port number
396 * Creates a new #GInetSocketAddress for @address and @port.
398 * If @address is an IPv6 address, it can also contain a scope ID
399 * (separated from the address by a `%`).
401 * Returns: (nullable) (transfer full): a new #GInetSocketAddress,
402 * or %NULL if @address cannot be parsed.
407 g_inet_socket_address_new_from_string (const char *address,
410 static struct addrinfo *hints, hints_struct;
411 GSocketAddress *saddr;
413 struct addrinfo *res;
416 if (strchr (address, ':'))
418 /* IPv6 address (or it's invalid). We use getaddrinfo() because
419 * it will handle parsing a scope_id as well.
422 if (G_UNLIKELY (g_once_init_enter (&hints)))
424 hints_struct.ai_family = AF_UNSPEC;
425 hints_struct.ai_socktype = SOCK_STREAM;
426 hints_struct.ai_protocol = 0;
427 hints_struct.ai_flags = AI_NUMERICHOST;
428 g_once_init_leave (&hints, &hints_struct);
431 status = getaddrinfo (address, NULL, hints, &res);
435 if (res->ai_family == AF_INET6 &&
436 res->ai_addrlen == sizeof (struct sockaddr_in6))
438 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
439 saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
448 /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
449 * because it accepts the stupid "IPv4 numbers-and-dots
450 * notation" addresses that are never used for anything except
451 * phishing. Since we don't have to worry about scope IDs for
452 * IPv4, we can just use g_inet_address_new_from_string().
454 iaddr = g_inet_address_new_from_string (address);
458 g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
460 saddr = g_inet_socket_address_new (iaddr, port);
461 g_object_unref (iaddr);
468 * g_inet_socket_address_get_address:
469 * @address: a #GInetSocketAddress
471 * Gets @address's #GInetAddress.
473 * Returns: (transfer none): the #GInetAddress for @address, which must be
474 * g_object_ref()'d if it will be stored
479 g_inet_socket_address_get_address (GInetSocketAddress *address)
481 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
483 return address->priv->address;
487 * g_inet_socket_address_get_port:
488 * @address: a #GInetSocketAddress
490 * Gets @address's port.
492 * Returns: the port for @address
497 g_inet_socket_address_get_port (GInetSocketAddress *address)
499 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
501 return address->priv->port;
506 * g_inet_socket_address_get_flowinfo:
507 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
509 * Gets the `sin6_flowinfo` field from @address,
510 * which must be an IPv6 address.
512 * Returns: the flowinfo field
517 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
519 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
520 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
522 return address->priv->flowinfo;
526 * g_inet_socket_address_get_scope_id:
527 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
529 * Gets the `sin6_scope_id` field from @address,
530 * which must be an IPv6 address.
532 * Returns: the scope id field
537 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
539 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
540 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
542 return address->priv->scope_id;