Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
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.
11 : : *
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.
16 : : *
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/>.
19 : : *
20 : : * Authors: Christian Kellner <gicmo@gnome.org>
21 : : * Samuel Cormier-Iijima <sciyoshi@gmail.com>
22 : : */
23 : :
24 : : #include <config.h>
25 : : #include <glib.h>
26 : : #include <string.h>
27 : :
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"
37 : : #include "glibintl.h"
38 : : #include "gioenumtypes.h"
39 : :
40 : : #include "gunixsocketaddress.h"
41 : :
42 : : #ifdef G_OS_WIN32
43 : : #include "giowin32-afunix.h"
44 : : #endif
45 : :
46 : :
47 : : /**
48 : : * GSocketAddress:
49 : : *
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.
54 : : */
55 : :
56 : : enum
57 : : {
58 : : PROP_NONE,
59 : : PROP_FAMILY
60 : : };
61 : :
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);
65 : :
66 : 478933 : G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
67 : 21 : G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
68 : : g_socket_address_connectable_iface_init))
69 : :
70 : : /**
71 : : * g_socket_address_get_family:
72 : : * @address: a #GSocketAddress
73 : : *
74 : : * Gets the socket family type of @address.
75 : : *
76 : : * Returns: the socket family type of @address
77 : : *
78 : : * Since: 2.22
79 : : */
80 : : GSocketFamily
81 : 1895 : g_socket_address_get_family (GSocketAddress *address)
82 : : {
83 : 1969 : g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
84 : :
85 : 1895 : return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
86 : 74 : }
87 : :
88 : : static void
89 : 2 : g_socket_address_get_property (GObject *object, guint prop_id,
90 : : GValue *value, GParamSpec *pspec)
91 : : {
92 : 2 : GSocketAddress *address = G_SOCKET_ADDRESS (object);
93 : :
94 : 2 : switch (prop_id)
95 : : {
96 : 1 : case PROP_FAMILY:
97 : 2 : g_value_set_enum (value, g_socket_address_get_family (address));
98 : 2 : break;
99 : :
100 : 0 : default:
101 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 : 0 : }
103 : 2 : }
104 : :
105 : : static void
106 : 170 : g_socket_address_class_init (GSocketAddressClass *klass)
107 : : {
108 : 170 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
109 : :
110 : 170 : gobject_class->get_property = g_socket_address_get_property;
111 : :
112 : : /**
113 : : * GSocketAddress:family:
114 : : *
115 : : * The family of the socket address.
116 : : *
117 : : * Since: 2.22
118 : : */
119 : 190 : g_object_class_install_property (gobject_class, PROP_FAMILY,
120 : 20 : g_param_spec_enum ("family", NULL, NULL,
121 : 20 : G_TYPE_SOCKET_FAMILY,
122 : : G_SOCKET_FAMILY_INVALID,
123 : : G_PARAM_READABLE |
124 : : G_PARAM_STATIC_STRINGS));
125 : 170 : }
126 : :
127 : : static void
128 : 170 : g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
129 : : {
130 : 170 : connectable_iface->enumerate = g_socket_address_connectable_enumerate;
131 : 170 : connectable_iface->proxy_enumerate = g_socket_address_connectable_proxy_enumerate;
132 : : /* to_string() is implemented by subclasses */
133 : 170 : }
134 : :
135 : : static void
136 : 4957 : g_socket_address_init (GSocketAddress *address)
137 : : {
138 : :
139 : 4957 : }
140 : :
141 : : /**
142 : : * g_socket_address_get_native_size:
143 : : * @address: a #GSocketAddress
144 : : *
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().
148 : : *
149 : : * Returns: the size of the native struct sockaddr that
150 : : * @address represents, or `-1` if @address
151 : : * is not valid
152 : : *
153 : : * Since: 2.22
154 : : */
155 : : gssize
156 : 2122 : g_socket_address_get_native_size (GSocketAddress *address)
157 : : {
158 : 2294 : g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
159 : :
160 : 2122 : return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
161 : 172 : }
162 : :
163 : : /**
164 : : * g_socket_address_to_native:
165 : : * @address: a #GSocketAddress
166 : : * @dest: a pointer to a memory location that will contain the native
167 : : * struct sockaddr
168 : : * @destlen: the size of @dest. Must be at least as large as
169 : : * g_socket_address_get_native_size()
170 : : * @error: #GError for error reporting, or %NULL to ignore
171 : : *
172 : : * Converts a #GSocketAddress to a native struct sockaddr, which can
173 : : * be passed to low-level functions like connect() or bind().
174 : : *
175 : : * If not enough space is available, a %G_IO_ERROR_NO_SPACE error
176 : : * is returned. If the address type is not known on the system
177 : : * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
178 : : *
179 : : * Returns: %TRUE if @dest was filled in, %FALSE on error
180 : : *
181 : : * Since: 2.22
182 : : */
183 : : gboolean
184 : 2174 : g_socket_address_to_native (GSocketAddress *address,
185 : : gpointer dest,
186 : : gsize destlen,
187 : : GError **error)
188 : : {
189 : 2349 : g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
190 : :
191 : 2174 : return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
192 : 175 : }
193 : :
194 : : /**
195 : : * g_socket_address_new_from_native:
196 : : * @native: (not nullable): a pointer to a struct sockaddr
197 : : * @len: the size of the memory location pointed to by @native
198 : : *
199 : : * Creates a #GSocketAddress subclass corresponding to the native
200 : : * struct sockaddr @native.
201 : : *
202 : : * Returns: a new #GSocketAddress if @native could successfully
203 : : * be converted, otherwise %NULL
204 : : *
205 : : * Since: 2.22
206 : : */
207 : : GSocketAddress *
208 : 165 : g_socket_address_new_from_native (gpointer native,
209 : : gsize len)
210 : : {
211 : : #ifdef G_OS_WIN32
212 : : ADDRESS_FAMILY family;
213 : : #else
214 : : sa_family_t family;
215 : : #endif
216 : :
217 : 165 : if (len < sizeof (family))
218 : 2 : return NULL;
219 : :
220 : 163 : family = ((struct sockaddr *) native)->sa_family;
221 : :
222 : 163 : if (family == AF_UNSPEC)
223 : 0 : return NULL;
224 : :
225 : 163 : if (family == AF_INET)
226 : : {
227 : 89 : struct sockaddr_in *addr = (struct sockaddr_in *) native;
228 : : GInetAddress *iaddr;
229 : : GSocketAddress *sockaddr;
230 : :
231 : 89 : if (len < sizeof (*addr))
232 : 0 : return NULL;
233 : :
234 : 89 : iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), G_SOCKET_FAMILY_IPV4);
235 : 89 : sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
236 : 89 : g_object_unref (iaddr);
237 : 89 : return sockaddr;
238 : : }
239 : :
240 : 74 : if (family == AF_INET6)
241 : : {
242 : 48 : struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
243 : : GInetAddress *iaddr;
244 : : GSocketAddress *sockaddr;
245 : :
246 : 48 : if (len < sizeof (*addr))
247 : 0 : return NULL;
248 : :
249 : 48 : if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
250 : : {
251 : : struct sockaddr_in sin_addr;
252 : :
253 : 4 : sin_addr.sin_family = AF_INET;
254 : 4 : sin_addr.sin_port = addr->sin6_port;
255 : 4 : memcpy (&(sin_addr.sin_addr.s_addr), addr->sin6_addr.s6_addr + 12, 4);
256 : 4 : iaddr = g_inet_address_new_from_bytes ((guint8 *) &(sin_addr.sin_addr), G_SOCKET_FAMILY_IPV4);
257 : 2 : }
258 : : else
259 : : {
260 : 44 : iaddr = g_inet_address_new_from_bytes_with_ipv6_info ((guint8 *) &(addr->sin6_addr), G_SOCKET_FAMILY_IPV6, addr->sin6_flowinfo, addr->sin6_scope_id);
261 : : }
262 : :
263 : 66 : sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
264 : 18 : "address", iaddr,
265 : 48 : "port", g_ntohs (addr->sin6_port),
266 : : NULL);
267 : 48 : g_object_unref (iaddr);
268 : 48 : return sockaddr;
269 : : }
270 : :
271 : 26 : if (family == AF_UNIX)
272 : : {
273 : 4 : struct sockaddr_un *addr = (struct sockaddr_un *) native;
274 : 4 : gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
275 : :
276 : 4 : if (len < G_STRUCT_OFFSET (struct sockaddr_un, sun_path))
277 : 0 : return NULL;
278 : :
279 : 4 : if (path_len == 0)
280 : : {
281 : 1 : return g_unix_socket_address_new_with_type ("", 0,
282 : : G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
283 : : }
284 : 3 : else if (addr->sun_path[0] == 0)
285 : : {
286 : 0 : if (!g_unix_socket_address_abstract_names_supported ())
287 : : {
288 : 0 : return g_unix_socket_address_new_with_type ("", 0,
289 : : G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
290 : : }
291 : 0 : else if (len < sizeof (*addr))
292 : : {
293 : 0 : return g_unix_socket_address_new_with_type (addr->sun_path + 1,
294 : 0 : path_len - 1,
295 : : G_UNIX_SOCKET_ADDRESS_ABSTRACT);
296 : : }
297 : : else
298 : : {
299 : 0 : return g_unix_socket_address_new_with_type (addr->sun_path + 1,
300 : 0 : path_len - 1,
301 : : G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
302 : : }
303 : : }
304 : : else
305 : : {
306 : 3 : return g_unix_socket_address_new_with_type (addr->sun_path,
307 : 0 : path_len,
308 : : G_UNIX_SOCKET_ADDRESS_PATH);
309 : : }
310 : : }
311 : :
312 : 22 : return g_native_socket_address_new (native, len);
313 : 61 : }
314 : :
315 : :
316 : : #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
317 : : #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
318 : :
319 : : typedef struct {
320 : : GSocketAddressEnumerator parent_instance;
321 : :
322 : : GSocketAddress *sockaddr;
323 : : } GSocketAddressAddressEnumerator;
324 : :
325 : : typedef struct {
326 : : GSocketAddressEnumeratorClass parent_class;
327 : :
328 : : } GSocketAddressAddressEnumeratorClass;
329 : :
330 : : static GType _g_socket_address_address_enumerator_get_type (void);
331 : 8414 : G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
332 : :
333 : : static void
334 : 2601 : g_socket_address_address_enumerator_finalize (GObject *object)
335 : : {
336 : 434 : GSocketAddressAddressEnumerator *sockaddr_enum =
337 : 2601 : G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
338 : :
339 : 2601 : if (sockaddr_enum->sockaddr)
340 : 1 : g_object_unref (sockaddr_enum->sockaddr);
341 : :
342 : 2601 : G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
343 : 2601 : }
344 : :
345 : : static GSocketAddress *
346 : 2952 : g_socket_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
347 : : GCancellable *cancellable,
348 : : GError **error)
349 : : {
350 : 586 : GSocketAddressAddressEnumerator *sockaddr_enum =
351 : 2952 : G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
352 : :
353 : 2952 : if (sockaddr_enum->sockaddr)
354 : : {
355 : 2600 : GSocketAddress *ret = sockaddr_enum->sockaddr;
356 : :
357 : 2600 : sockaddr_enum->sockaddr = NULL;
358 : 2600 : return ret;
359 : : }
360 : : else
361 : 352 : return NULL;
362 : 586 : }
363 : :
364 : : static void
365 : 2601 : _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
366 : : {
367 : 2601 : }
368 : :
369 : : static void
370 : 130 : _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
371 : : {
372 : 130 : GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
373 : 5 : GSocketAddressEnumeratorClass *enumerator_class =
374 : 130 : G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
375 : :
376 : 130 : enumerator_class->next = g_socket_address_address_enumerator_next;
377 : 130 : object_class->finalize = g_socket_address_address_enumerator_finalize;
378 : 130 : }
379 : :
380 : : static GSocketAddressEnumerator *
381 : 2601 : g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
382 : : {
383 : : GSocketAddressAddressEnumerator *sockaddr_enum;
384 : :
385 : 2601 : sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
386 : 2601 : sockaddr_enum->sockaddr = g_object_ref (G_SOCKET_ADDRESS (connectable));
387 : :
388 : 2601 : return (GSocketAddressEnumerator *)sockaddr_enum;
389 : : }
390 : :
391 : : static GSocketAddressEnumerator *
392 : 858 : g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
393 : : {
394 : 858 : GSocketAddressEnumerator *addr_enum = NULL;
395 : :
396 : 858 : g_assert (connectable != NULL);
397 : :
398 : 1706 : if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
399 : 1276 : !G_IS_PROXY_ADDRESS (connectable))
400 : 428 : {
401 : : GInetAddress *addr;
402 : : guint port;
403 : : gchar *uri;
404 : : gchar *ip;
405 : :
406 : 852 : g_object_get (connectable, "address", &addr, "port", &port, NULL);
407 : :
408 : 852 : ip = g_inet_address_to_string (addr);
409 : 852 : uri = g_uri_join (G_URI_FLAGS_NONE, "none", NULL, ip, port, "", NULL, NULL);
410 : :
411 : 1276 : addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
412 : 424 : "connectable", connectable,
413 : 424 : "uri", uri,
414 : : NULL);
415 : :
416 : 852 : g_object_unref (addr);
417 : 852 : g_free (ip);
418 : 852 : g_free (uri);
419 : 424 : }
420 : : else
421 : : {
422 : 6 : addr_enum = g_socket_address_connectable_enumerate (connectable);
423 : : }
424 : :
425 : 858 : return addr_enum;
426 : : }
|