1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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/>.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
28 #include "ginetsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gsocketconnectable.h"
37 * SECTION:ginetsocketaddress
38 * @short_description: Internet GSocketAddress
41 * An IPv4 or IPv6 socket address; that is, the combination of a
42 * #GInetAddress and a port number.
48 * An IPv4 or IPv6 socket address, corresponding to a struct
49 * sockaddr_in or struct sockaddr_in6.
52 struct _GInetSocketAddressPrivate
54 GInetAddress *address;
60 static void g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
61 static gchar *g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable);
63 G_DEFINE_TYPE_WITH_CODE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS,
64 G_ADD_PRIVATE (GInetSocketAddress)
65 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
66 g_inet_socket_address_connectable_iface_init))
77 g_inet_socket_address_dispose (GObject *object)
79 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
81 g_clear_object (&(address->priv->address));
83 G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
87 g_inet_socket_address_get_property (GObject *object,
92 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
97 g_value_set_object (value, address->priv->address);
101 g_value_set_uint (value, address->priv->port);
105 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
106 g_value_set_uint (value, address->priv->flowinfo);
110 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
111 g_value_set_uint (value, address->priv->scope_id);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 g_inet_socket_address_set_property (GObject *object,
125 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
130 address->priv->address = g_object_ref (g_value_get_object (value));
134 address->priv->port = (guint16) g_value_get_uint (value);
138 /* We can't test that address->priv->address is IPv6 here,
139 * since this property might get set before PROP_ADDRESS.
141 address->priv->flowinfo = g_value_get_uint (value);
145 address->priv->scope_id = g_value_get_uint (value);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 g_inet_socket_address_get_family (GSocketAddress *address)
156 GInetSocketAddress *addr;
158 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
160 addr = G_INET_SOCKET_ADDRESS (address);
162 return g_inet_address_get_family (addr->priv->address);
166 g_inet_socket_address_get_native_size (GSocketAddress *address)
168 GInetSocketAddress *addr;
169 GSocketFamily family;
171 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
173 addr = G_INET_SOCKET_ADDRESS (address);
174 family = g_inet_address_get_family (addr->priv->address);
176 if (family == AF_INET)
177 return sizeof (struct sockaddr_in);
178 else if (family == AF_INET6)
179 return sizeof (struct sockaddr_in6);
185 g_inet_socket_address_to_native (GSocketAddress *address,
190 GInetSocketAddress *addr;
191 GSocketFamily family;
193 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
195 addr = G_INET_SOCKET_ADDRESS (address);
196 family = g_inet_address_get_family (addr->priv->address);
198 if (family == AF_INET)
200 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
202 if (destlen < sizeof (*sock))
204 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
205 _("Not enough space for socket address"));
209 sock->sin_family = AF_INET;
210 sock->sin_port = g_htons (addr->priv->port);
211 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
212 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
215 else if (family == AF_INET6)
217 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
219 if (destlen < sizeof (*sock))
221 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
222 _("Not enough space for socket address"));
226 memset (sock, 0, sizeof (*sock));
227 sock->sin6_family = AF_INET6;
228 sock->sin6_port = g_htons (addr->priv->port);
229 sock->sin6_flowinfo = addr->priv->flowinfo;
230 sock->sin6_scope_id = addr->priv->scope_id;
231 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
236 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
237 _("Unsupported socket address"));
243 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
246 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
248 gobject_class->dispose = g_inet_socket_address_dispose;
249 gobject_class->set_property = g_inet_socket_address_set_property;
250 gobject_class->get_property = g_inet_socket_address_get_property;
252 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
253 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
254 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
256 g_object_class_install_property (gobject_class, PROP_ADDRESS,
257 g_param_spec_object ("address",
261 G_PARAM_CONSTRUCT_ONLY |
263 G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_PORT,
266 g_param_spec_uint ("port",
272 G_PARAM_CONSTRUCT_ONLY |
274 G_PARAM_STATIC_STRINGS));
277 * GInetSocketAddress:flowinfo:
279 * The `sin6_flowinfo` field, for IPv6 addresses.
283 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
284 g_param_spec_uint ("flowinfo",
286 P_("IPv6 flow info"),
290 G_PARAM_CONSTRUCT_ONLY |
292 G_PARAM_STATIC_STRINGS));
295 * GInetSocketAddress:scope_id:
297 * The `sin6_scope_id` field, for IPv6 addresses.
301 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
302 g_param_spec_uint ("scope-id",
308 G_PARAM_CONSTRUCT_ONLY |
310 G_PARAM_STATIC_STRINGS));
314 g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
316 GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
318 iface->enumerate = parent_iface->enumerate;
319 iface->proxy_enumerate = parent_iface->proxy_enumerate;
320 iface->to_string = g_inet_socket_address_connectable_to_string;
324 g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable)
326 GInetSocketAddress *sa;
332 sa = G_INET_SOCKET_ADDRESS (connectable);
333 a = g_inet_socket_address_get_address (sa);
334 out = g_string_new ("");
337 a_string = g_inet_address_to_string (a);
338 g_string_append (out, a_string);
341 /* Scope ID (IPv6 only). */
342 if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6 &&
343 g_inet_socket_address_get_scope_id (sa) != 0)
345 g_string_append_printf (out, "%%%u",
346 g_inet_socket_address_get_scope_id (sa));
350 port = g_inet_socket_address_get_port (sa);
353 /* Disambiguate ports from IPv6 addresses using square brackets. */
354 if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6)
356 g_string_prepend (out, "[");
357 g_string_append (out, "]");
360 g_string_append_printf (out, ":%u", port);
363 return g_string_free (out, FALSE);
367 g_inet_socket_address_init (GInetSocketAddress *address)
369 address->priv = g_inet_socket_address_get_instance_private (address);
373 * g_inet_socket_address_new:
374 * @address: a #GInetAddress
375 * @port: a port number
377 * Creates a new #GInetSocketAddress for @address and @port.
379 * Returns: a new #GInetSocketAddress
384 g_inet_socket_address_new (GInetAddress *address,
387 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
394 * g_inet_socket_address_new_from_string:
395 * @address: the string form of an IP address
396 * @port: a port number
398 * Creates a new #GInetSocketAddress for @address and @port.
400 * If @address is an IPv6 address, it can also contain a scope ID
401 * (separated from the address by a `%`).
403 * Returns: (nullable) (transfer full): a new #GInetSocketAddress,
404 * or %NULL if @address cannot be parsed.
409 g_inet_socket_address_new_from_string (const char *address,
412 static struct addrinfo *hints, hints_struct;
413 GSocketAddress *saddr;
415 struct addrinfo *res;
418 if (strchr (address, ':'))
420 /* IPv6 address (or it's invalid). We use getaddrinfo() because
421 * it will handle parsing a scope_id as well.
424 if (G_UNLIKELY (g_once_init_enter (&hints)))
426 hints_struct.ai_family = AF_UNSPEC;
427 hints_struct.ai_socktype = SOCK_STREAM;
428 hints_struct.ai_protocol = 0;
429 hints_struct.ai_flags = AI_NUMERICHOST;
430 g_once_init_leave (&hints, &hints_struct);
433 status = getaddrinfo (address, NULL, hints, &res);
437 if (res->ai_family == AF_INET6 &&
438 res->ai_addrlen == sizeof (struct sockaddr_in6))
440 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
441 saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
450 /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
451 * because it accepts the stupid "IPv4 numbers-and-dots
452 * notation" addresses that are never used for anything except
453 * phishing. Since we don't have to worry about scope IDs for
454 * IPv4, we can just use g_inet_address_new_from_string().
456 iaddr = g_inet_address_new_from_string (address);
460 g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
462 saddr = g_inet_socket_address_new (iaddr, port);
463 g_object_unref (iaddr);
470 * g_inet_socket_address_get_address:
471 * @address: a #GInetSocketAddress
473 * Gets @address's #GInetAddress.
475 * Returns: (transfer none): the #GInetAddress for @address, which must be
476 * g_object_ref()'d if it will be stored
481 g_inet_socket_address_get_address (GInetSocketAddress *address)
483 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
485 return address->priv->address;
489 * g_inet_socket_address_get_port:
490 * @address: a #GInetSocketAddress
492 * Gets @address's port.
494 * Returns: the port for @address
499 g_inet_socket_address_get_port (GInetSocketAddress *address)
501 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
503 return address->priv->port;
508 * g_inet_socket_address_get_flowinfo:
509 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
511 * Gets the `sin6_flowinfo` field from @address,
512 * which must be an IPv6 address.
514 * Returns: the flowinfo field
519 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
521 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
522 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
524 return address->priv->flowinfo;
528 * g_inet_socket_address_get_scope_id:
529 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
531 * Gets the `sin6_scope_id` field from @address,
532 * which must be an IPv6 address.
534 * Returns: the scope id field
539 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
541 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
542 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
544 return address->priv->scope_id;