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 "gsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "ginetsocketaddress.h"
31 #include "gnativesocketaddress.h"
32 #include "gnetworkingprivate.h"
33 #include "gproxyaddress.h"
34 #include "gproxyaddressenumerator.h"
35 #include "gsocketaddressenumerator.h"
36 #include "gsocketconnectable.h"
38 #include "gioenumtypes.h"
40 #include "gunixsocketaddress.h"
43 #include "giowin32-afunix.h"
50 * `GSocketAddress` is the equivalent of
51 * [`struct sockaddr`](man:sockaddr(3type)) and its subtypes in the BSD sockets
52 * API. This is an abstract class; use [class@Gio.InetSocketAddress] for
53 * internet sockets, or [class@Gio.UnixSocketAddress] for UNIX domain sockets.
62 static void g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
63 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate (GSocketConnectable *connectable);
64 static GSocketAddressEnumerator *g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable);
66 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
67 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
68 g_socket_address_connectable_iface_init))
71 * g_socket_address_get_family:
72 * @address: a #GSocketAddress
74 * Gets the socket family type of @address.
76 * Returns: the socket family type of @address
81 g_socket_address_get_family (GSocketAddress *address)
83 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
85 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
89 g_socket_address_get_property (GObject *object, guint prop_id,
90 GValue *value, GParamSpec *pspec)
92 GSocketAddress *address = G_SOCKET_ADDRESS (object);
97 g_value_set_enum (value, g_socket_address_get_family (address));
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 g_socket_address_class_init (GSocketAddressClass *klass)
108 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
110 gobject_class->get_property = g_socket_address_get_property;
113 * GSocketAddress:family:
115 * The family of the socket address.
119 g_object_class_install_property (gobject_class, PROP_FAMILY,
120 g_param_spec_enum ("family", NULL, NULL,
121 G_TYPE_SOCKET_FAMILY,
122 G_SOCKET_FAMILY_INVALID,
124 G_PARAM_STATIC_STRINGS));
128 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
130 connectable_iface->enumerate = g_socket_address_connectable_enumerate;
131 connectable_iface->proxy_enumerate = g_socket_address_connectable_proxy_enumerate;
132 /* to_string() is implemented by subclasses */
136 g_socket_address_init (GSocketAddress *address)
142 * g_socket_address_get_native_size:
143 * @address: a #GSocketAddress
145 * Gets the size of @address's native struct sockaddr.
146 * You can use this to allocate memory to pass to
147 * g_socket_address_to_native().
149 * Returns: the size of the native struct sockaddr that
150 * @address represents
155 g_socket_address_get_native_size (GSocketAddress *address)
157 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
159 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
163 * g_socket_address_to_native:
164 * @address: a #GSocketAddress
165 * @dest: a pointer to a memory location that will contain the native
167 * @destlen: the size of @dest. Must be at least as large as
168 * g_socket_address_get_native_size()
169 * @error: #GError for error reporting, or %NULL to ignore
171 * Converts a #GSocketAddress to a native struct sockaddr, which can
172 * be passed to low-level functions like connect() or bind().
174 * If not enough space is available, a %G_IO_ERROR_NO_SPACE error
175 * is returned. If the address type is not known on the system
176 * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
178 * Returns: %TRUE if @dest was filled in, %FALSE on error
183 g_socket_address_to_native (GSocketAddress *address,
188 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
190 return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
194 * g_socket_address_new_from_native:
195 * @native: (not nullable): a pointer to a struct sockaddr
196 * @len: the size of the memory location pointed to by @native
198 * Creates a #GSocketAddress subclass corresponding to the native
199 * struct sockaddr @native.
201 * Returns: a new #GSocketAddress if @native could successfully
202 * be converted, otherwise %NULL
207 g_socket_address_new_from_native (gpointer native,
212 if (len < sizeof (gshort))
215 family = ((struct sockaddr *) native)->sa_family;
217 if (family == AF_UNSPEC)
220 if (family == AF_INET)
222 struct sockaddr_in *addr = (struct sockaddr_in *) native;
224 GSocketAddress *sockaddr;
226 if (len < sizeof (*addr))
229 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
230 sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
231 g_object_unref (iaddr);
235 if (family == AF_INET6)
237 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
239 GSocketAddress *sockaddr;
241 if (len < sizeof (*addr))
244 if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
246 struct sockaddr_in sin_addr;
248 sin_addr.sin_family = AF_INET;
249 sin_addr.sin_port = addr->sin6_port;
250 memcpy (&(sin_addr.sin_addr.s_addr), addr->sin6_addr.s6_addr + 12, 4);
251 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(sin_addr.sin_addr), AF_INET);
255 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
258 sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
260 "port", g_ntohs (addr->sin6_port),
261 "flowinfo", addr->sin6_flowinfo,
262 "scope_id", addr->sin6_scope_id,
264 g_object_unref (iaddr);
268 if (family == AF_UNIX)
270 struct sockaddr_un *addr = (struct sockaddr_un *) native;
271 gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
275 return g_unix_socket_address_new_with_type ("", 0,
276 G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
278 else if (addr->sun_path[0] == 0)
280 if (!g_unix_socket_address_abstract_names_supported ())
282 return g_unix_socket_address_new_with_type ("", 0,
283 G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
285 else if (len < sizeof (*addr))
287 return g_unix_socket_address_new_with_type (addr->sun_path + 1,
289 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
293 return g_unix_socket_address_new_with_type (addr->sun_path + 1,
295 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
299 return g_unix_socket_address_new (addr->sun_path);
302 return g_native_socket_address_new (native, len);
306 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
307 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
310 GSocketAddressEnumerator parent_instance;
312 GSocketAddress *sockaddr;
313 } GSocketAddressAddressEnumerator;
316 GSocketAddressEnumeratorClass parent_class;
318 } GSocketAddressAddressEnumeratorClass;
320 static GType _g_socket_address_address_enumerator_get_type (void);
321 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
324 g_socket_address_address_enumerator_finalize (GObject *object)
326 GSocketAddressAddressEnumerator *sockaddr_enum =
327 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
329 if (sockaddr_enum->sockaddr)
330 g_object_unref (sockaddr_enum->sockaddr);
332 G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
335 static GSocketAddress *
336 g_socket_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
337 GCancellable *cancellable,
340 GSocketAddressAddressEnumerator *sockaddr_enum =
341 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
343 if (sockaddr_enum->sockaddr)
345 GSocketAddress *ret = sockaddr_enum->sockaddr;
347 sockaddr_enum->sockaddr = NULL;
355 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
360 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
362 GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
363 GSocketAddressEnumeratorClass *enumerator_class =
364 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
366 enumerator_class->next = g_socket_address_address_enumerator_next;
367 object_class->finalize = g_socket_address_address_enumerator_finalize;
370 static GSocketAddressEnumerator *
371 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
373 GSocketAddressAddressEnumerator *sockaddr_enum;
375 sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
376 sockaddr_enum->sockaddr = g_object_ref (G_SOCKET_ADDRESS (connectable));
378 return (GSocketAddressEnumerator *)sockaddr_enum;
381 static GSocketAddressEnumerator *
382 g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
384 GSocketAddressEnumerator *addr_enum = NULL;
386 g_assert (connectable != NULL);
388 if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
389 !G_IS_PROXY_ADDRESS (connectable))
396 g_object_get (connectable, "address", &addr, "port", &port, NULL);
398 ip = g_inet_address_to_string (addr);
399 uri = g_uri_join (G_URI_FLAGS_NONE, "none", NULL, ip, port, "", NULL, NULL);
401 addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
402 "connectable", connectable,
406 g_object_unref (addr);
412 addr_enum = g_socket_address_connectable_enumerate (connectable);