| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | /* GIO - GLib Input, Output and Streaming Library
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-05-18 09:12:45 +01:00
										 |  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2017-05-27 18:21:30 +02:00
										 |  |  |  * version 2.1 of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2014-01-23 12:58:29 +01:00
										 |  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Authors: Christian Kellner <gicmo@gnome.org> | 
					
						
							|  |  |  |  *          Samuel Cormier-Iijima <sciyoshi@gmail.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <config.h>
 | 
					
						
							|  |  |  | #include <glib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "ginetsocketaddress.h"
 | 
					
						
							|  |  |  | #include "ginetaddress.h"
 | 
					
						
							|  |  |  | #include "gnetworkingprivate.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-04 15:24:24 +01:00
										 |  |  | #include "gsocketconnectable.h"
 | 
					
						
							| 
									
										
										
										
											2009-05-18 14:10:07 +02:00
										 |  |  | #include "gioerror.h"
 | 
					
						
							|  |  |  | #include "glibintl.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * GInetSocketAddress: | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-10-23 14:39:16 +01:00
										 |  |  |  * An IPv4 or IPv6 socket address. That is, the combination of a | 
					
						
							|  |  |  |  * [class@Gio.InetAddress] and a port number. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * In UNIX terms, `GInetSocketAddress` corresponds to a | 
					
						
							|  |  |  |  * [`struct sockaddr_in` or `struct sockaddr_in6`](man:sockaddr(3type)). | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct _GInetSocketAddressPrivate | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetAddress *address; | 
					
						
							|  |  |  |   guint16       port; | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |   guint32       flowinfo; | 
					
						
							|  |  |  |   guint32       scope_id; | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-04 15:24:24 +01:00
										 |  |  | static void   g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface); | 
					
						
							|  |  |  | static gchar *g_inet_socket_address_connectable_to_string  (GSocketConnectable      *connectable); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | G_DEFINE_TYPE_WITH_CODE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS, | 
					
						
							|  |  |  |                          G_ADD_PRIVATE (GInetSocketAddress) | 
					
						
							|  |  |  |                          G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE, | 
					
						
							|  |  |  |                                                 g_inet_socket_address_connectable_iface_init)) | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-11 00:29:58 +01:00
										 |  |  | enum { | 
					
						
							|  |  |  |   PROP_0, | 
					
						
							|  |  |  |   PROP_ADDRESS, | 
					
						
							|  |  |  |   PROP_PORT, | 
					
						
							|  |  |  |   PROP_FLOWINFO, | 
					
						
							|  |  |  |   PROP_SCOPE_ID | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_dispose (GObject *object) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-11 00:29:58 +01:00
										 |  |  |   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-11 00:29:58 +01:00
										 |  |  |   g_clear_object (&(address->priv->address)); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-11 00:29:58 +01:00
										 |  |  |   G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_get_property (GObject    *object, | 
					
						
							|  |  |  |                                     guint       prop_id, | 
					
						
							|  |  |  |                                     GValue     *value, | 
					
						
							|  |  |  |                                     GParamSpec *pspec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (prop_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       case PROP_ADDRESS: | 
					
						
							|  |  |  |         g_value_set_object (value, address->priv->address); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case PROP_PORT: | 
					
						
							|  |  |  |         g_value_set_uint (value, address->priv->port); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |       case PROP_FLOWINFO: | 
					
						
							|  |  |  | 	g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6); | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |         g_value_set_uint (value, g_inet_socket_address_get_flowinfo (address)); | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case PROP_SCOPE_ID: | 
					
						
							|  |  |  | 	g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6); | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |         g_value_set_uint (value, g_inet_socket_address_get_scope_id (address)); | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |       default: | 
					
						
							|  |  |  |         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_set_property (GObject      *object, | 
					
						
							|  |  |  |                                     guint         prop_id, | 
					
						
							|  |  |  |                                     const GValue *value, | 
					
						
							|  |  |  |                                     GParamSpec   *pspec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (prop_id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       case PROP_ADDRESS: | 
					
						
							|  |  |  |         address->priv->address = g_object_ref (g_value_get_object (value)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case PROP_PORT: | 
					
						
							|  |  |  |         address->priv->port = (guint16) g_value_get_uint (value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |       case PROP_FLOWINFO: | 
					
						
							|  |  |  | 	/* We can't test that address->priv->address is IPv6 here,
 | 
					
						
							|  |  |  | 	 * since this property might get set before PROP_ADDRESS. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  |         address->priv->flowinfo = g_value_get_uint (value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case PROP_SCOPE_ID: | 
					
						
							|  |  |  |         address->priv->scope_id = g_value_get_uint (value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |       default: | 
					
						
							|  |  |  |         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GSocketFamily | 
					
						
							|  |  |  | g_inet_socket_address_get_family (GSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *addr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addr = G_INET_SOCKET_ADDRESS (address); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return g_inet_address_get_family (addr->priv->address); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gssize | 
					
						
							|  |  |  | g_inet_socket_address_get_native_size (GSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *addr; | 
					
						
							|  |  |  |   GSocketFamily family; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addr = G_INET_SOCKET_ADDRESS (address); | 
					
						
							|  |  |  |   family = g_inet_address_get_family (addr->priv->address); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (family == AF_INET) | 
					
						
							|  |  |  |     return sizeof (struct sockaddr_in); | 
					
						
							|  |  |  |   else if (family == AF_INET6) | 
					
						
							|  |  |  |     return sizeof (struct sockaddr_in6); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gboolean | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  | g_inet_socket_address_to_native (GSocketAddress  *address, | 
					
						
							|  |  |  |                                  gpointer         dest, | 
					
						
							|  |  |  | 				 gsize            destlen, | 
					
						
							|  |  |  | 				 GError         **error) | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *addr; | 
					
						
							|  |  |  |   GSocketFamily family; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-10 17:21:32 -04:00
										 |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   addr = G_INET_SOCKET_ADDRESS (address); | 
					
						
							|  |  |  |   family = g_inet_address_get_family (addr->priv->address); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (family == AF_INET) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       struct sockaddr_in *sock = (struct sockaddr_in *) dest; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (destlen < sizeof (*sock)) | 
					
						
							| 
									
										
										
										
											2009-05-18 14:10:07 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE, | 
					
						
							|  |  |  | 			       _("Not enough space for socket address")); | 
					
						
							|  |  |  | 	  return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       sock->sin_family = AF_INET; | 
					
						
							|  |  |  |       sock->sin_port = g_htons (addr->priv->port); | 
					
						
							|  |  |  |       memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr)); | 
					
						
							|  |  |  |       memset (sock->sin_zero, 0, sizeof (sock->sin_zero)); | 
					
						
							|  |  |  |       return TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (family == AF_INET6) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (destlen < sizeof (*sock)) | 
					
						
							| 
									
										
										
										
											2009-05-18 14:10:07 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE, | 
					
						
							|  |  |  | 			       _("Not enough space for socket address")); | 
					
						
							|  |  |  | 	  return FALSE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-17 13:20:49 -04:00
										 |  |  |       memset (sock, 0, sizeof (*sock)); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |       sock->sin6_family = AF_INET6; | 
					
						
							|  |  |  |       sock->sin6_port = g_htons (addr->priv->port); | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |       sock->sin6_flowinfo = g_inet_socket_address_get_flowinfo (addr); | 
					
						
							|  |  |  |       sock->sin6_scope_id = g_inet_socket_address_get_scope_id (addr); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |       memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr)); | 
					
						
							|  |  |  |       return TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2009-05-18 14:10:07 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, | 
					
						
							|  |  |  | 			   _("Unsupported socket address")); | 
					
						
							|  |  |  |       return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_class_init (GInetSocketAddressClass *klass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GObjectClass *gobject_class = G_OBJECT_CLASS (klass); | 
					
						
							|  |  |  |   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gobject_class->dispose = g_inet_socket_address_dispose; | 
					
						
							|  |  |  |   gobject_class->set_property = g_inet_socket_address_set_property; | 
					
						
							|  |  |  |   gobject_class->get_property = g_inet_socket_address_get_property; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gsocketaddress_class->get_family = g_inet_socket_address_get_family; | 
					
						
							|  |  |  |   gsocketaddress_class->to_native = g_inet_socket_address_to_native; | 
					
						
							|  |  |  |   gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * GInetSocketAddress:address: | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The address. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.22 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |   g_object_class_install_property (gobject_class, PROP_ADDRESS, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  |                                    g_param_spec_object ("address", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |                                                         G_TYPE_INET_ADDRESS, | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |                                                         G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  |                                                         G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                         G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * GInetSocketAddress:port: | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * The port. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Since: 2.22 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |   g_object_class_install_property (gobject_class, PROP_PORT, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  |                                    g_param_spec_uint ("port", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |                                                       0, | 
					
						
							|  |  |  |                                                       65535, | 
					
						
							|  |  |  |                                                       0, | 
					
						
							| 
									
										
										
										
											2009-05-27 18:20:08 -04:00
										 |  |  |                                                       G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  |                                                       G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                       G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * GInetSocketAddress:flowinfo: | 
					
						
							|  |  |  |    * | 
					
						
							| 
									
										
										
										
											2014-02-06 08:04:52 -05:00
										 |  |  |    * The `sin6_flowinfo` field, for IPv6 addresses. | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |    * If unset this property is inherited from [property@Gio.InetSocketAddress:address]. | 
					
						
							|  |  |  |    * | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |    * Since: 2.32 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   g_object_class_install_property (gobject_class, PROP_FLOWINFO, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  |                                    g_param_spec_uint ("flowinfo", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |                                                       0, | 
					
						
							|  |  |  |                                                       G_MAXUINT32, | 
					
						
							|  |  |  |                                                       0, | 
					
						
							|  |  |  |                                                       G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  |                                                       G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                       G_PARAM_STATIC_STRINGS)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2023-11-29 13:23:09 +00:00
										 |  |  |    * GInetSocketAddress:scope-id: | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2014-02-06 16:59:49 -05:00
										 |  |  |    * The `sin6_scope_id` field, for IPv6 addresses. | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |    * If unset this property is inherited from [property@Gio.InetSocketAddress:address]. | 
					
						
							|  |  |  |    * | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |    * Since: 2.32 | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   g_object_class_install_property (gobject_class, PROP_SCOPE_ID, | 
					
						
							| 
									
										
										
										
											2023-04-28 01:59:26 +02:00
										 |  |  |                                    g_param_spec_uint ("scope-id", NULL, NULL, | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |                                                       0, | 
					
						
							|  |  |  |                                                       G_MAXUINT32, | 
					
						
							|  |  |  |                                                       0, | 
					
						
							|  |  |  |                                                       G_PARAM_CONSTRUCT_ONLY | | 
					
						
							|  |  |  |                                                       G_PARAM_READWRITE | | 
					
						
							|  |  |  |                                                       G_PARAM_STATIC_STRINGS)); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-04 15:24:24 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   iface->enumerate = parent_iface->enumerate; | 
					
						
							|  |  |  |   iface->proxy_enumerate = parent_iface->proxy_enumerate; | 
					
						
							|  |  |  |   iface->to_string = g_inet_socket_address_connectable_to_string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gchar * | 
					
						
							|  |  |  | g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GInetSocketAddress *sa; | 
					
						
							|  |  |  |   GInetAddress *a; | 
					
						
							|  |  |  |   gchar *a_string; | 
					
						
							|  |  |  |   GString *out; | 
					
						
							|  |  |  |   guint16 port; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sa = G_INET_SOCKET_ADDRESS (connectable); | 
					
						
							|  |  |  |   a = g_inet_socket_address_get_address (sa); | 
					
						
							|  |  |  |   out = g_string_new (""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Address. */ | 
					
						
							|  |  |  |   a_string = g_inet_address_to_string (a); | 
					
						
							|  |  |  |   g_string_append (out, a_string); | 
					
						
							|  |  |  |   g_free (a_string); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Scope ID (IPv6 only). */ | 
					
						
							|  |  |  |   if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6 && | 
					
						
							|  |  |  |       g_inet_socket_address_get_scope_id (sa) != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_string_append_printf (out, "%%%u", | 
					
						
							|  |  |  |                               g_inet_socket_address_get_scope_id (sa)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Port. */ | 
					
						
							|  |  |  |   port = g_inet_socket_address_get_port (sa); | 
					
						
							|  |  |  |   if (port != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       /* Disambiguate ports from IPv6 addresses using square brackets. */ | 
					
						
							|  |  |  |       if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           g_string_prepend (out, "["); | 
					
						
							|  |  |  |           g_string_append (out, "]"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       g_string_append_printf (out, ":%u", port); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return g_string_free (out, FALSE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | static void | 
					
						
							|  |  |  | g_inet_socket_address_init (GInetSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-24 15:43:04 +01:00
										 |  |  |   address->priv = g_inet_socket_address_get_instance_private (address); | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_inet_socket_address_new: | 
					
						
							|  |  |  |  * @address: a #GInetAddress | 
					
						
							|  |  |  |  * @port: a port number | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Creates a new #GInetSocketAddress for @address and @port. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns: a new #GInetSocketAddress | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.22 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | GSocketAddress * | 
					
						
							|  |  |  | g_inet_socket_address_new (GInetAddress *address, | 
					
						
							|  |  |  |                            guint16       port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return g_object_new (G_TYPE_INET_SOCKET_ADDRESS, | 
					
						
							|  |  |  | 		       "address", address, | 
					
						
							|  |  |  | 		       "port", port, | 
					
						
							|  |  |  | 		       NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * g_inet_socket_address_new_from_string: | 
					
						
							|  |  |  |  * @address: the string form of an IP address | 
					
						
							|  |  |  |  * @port: a port number | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Creates a new #GInetSocketAddress for @address and @port. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If @address is an IPv6 address, it can also contain a scope ID | 
					
						
							| 
									
										
										
										
											2025-07-07 11:24:11 -05:00
										 |  |  |  * (separated from the address by a `%`). Note that currently this | 
					
						
							|  |  |  |  * behavior is platform specific. This may change in a future release. | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-03-17 00:22:36 +00:00
										 |  |  |  * Returns: (nullable) (transfer full): a new #GInetSocketAddress, | 
					
						
							|  |  |  |  * or %NULL if @address cannot be parsed. | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.40 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | GSocketAddress * | 
					
						
							|  |  |  | g_inet_socket_address_new_from_string (const char *address, | 
					
						
							|  |  |  |                                        guint       port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GSocketAddress *saddr; | 
					
						
							|  |  |  |   GInetAddress *iaddr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 16:51:55 -05:00
										 |  |  |   iaddr = g_inet_address_new_from_string (address); | 
					
						
							|  |  |  |   if (!iaddr) | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 16:51:55 -05:00
										 |  |  |   saddr = g_inet_socket_address_new (iaddr, port); | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 16:51:55 -05:00
										 |  |  |   g_object_unref (iaddr); | 
					
						
							| 
									
										
										
										
											2013-08-20 21:36:25 -04:00
										 |  |  |   return saddr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * g_inet_socket_address_get_address: | 
					
						
							|  |  |  |  * @address: a #GInetSocketAddress | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Gets @address's #GInetAddress. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-12-29 13:51:44 +01:00
										 |  |  |  * Returns: (transfer none): the #GInetAddress for @address, which must be | 
					
						
							| 
									
										
										
										
											2008-12-12 13:13:55 -05:00
										 |  |  |  * g_object_ref()'d if it will be stored | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.22 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | GInetAddress * | 
					
						
							|  |  |  | g_inet_socket_address_get_address (GInetSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return address->priv->address; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_inet_socket_address_get_port: | 
					
						
							|  |  |  |  * @address: a #GInetSocketAddress | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Gets @address's port. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Returns: the port for @address | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.22 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | guint16 | 
					
						
							|  |  |  | g_inet_socket_address_get_port (GInetSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return address->priv->port; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * g_inet_socket_address_get_flowinfo: | 
					
						
							|  |  |  |  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-02-06 16:59:49 -05:00
										 |  |  |  * Gets the `sin6_flowinfo` field from @address, | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |  * which must be an IPv6 address. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |  * If not overridden this value will be inherited from [property@Gio.InetSocketAddress:address]. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-02-19 19:35:23 -05:00
										 |  |  |  * Returns: the flowinfo field | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.32 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | guint32 | 
					
						
							|  |  |  | g_inet_socket_address_get_flowinfo (GInetSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0); | 
					
						
							|  |  |  |   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |   return address->priv->flowinfo ? address->priv->flowinfo : g_inet_address_get_flowinfo (address->priv->address); | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2012-01-30 15:59:38 -05:00
										 |  |  |  * g_inet_socket_address_get_scope_id: | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-02-06 16:59:49 -05:00
										 |  |  |  * Gets the `sin6_scope_id` field from @address, | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |  * which must be an IPv6 address. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |  * If not overridden this value will be inherited from [property@Gio.InetSocketAddress:address]. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-02-19 19:35:23 -05:00
										 |  |  |  * Returns: the scope id field | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Since: 2.32 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | guint32 | 
					
						
							|  |  |  | g_inet_socket_address_get_scope_id (GInetSocketAddress *address) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0); | 
					
						
							|  |  |  |   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-02 09:30:05 -05:00
										 |  |  |   return address->priv->scope_id ? address->priv->scope_id : g_inet_address_get_scope_id (address->priv->address); | 
					
						
							| 
									
										
										
										
											2011-12-29 09:42:25 -05:00
										 |  |  | } |