Coverage Report

Created: 2025-06-13 06:20

/src/glib/gio/gsocketaddress.c
Line
Count
Source (jump to first uncovered line)
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
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))
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
g_socket_address_get_family (GSocketAddress *address)
82
0
{
83
0
  g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
84
85
0
  return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
86
0
}
87
88
static void
89
g_socket_address_get_property (GObject *object, guint prop_id,
90
             GValue *value, GParamSpec *pspec)
91
0
{
92
0
  GSocketAddress *address = G_SOCKET_ADDRESS (object);
93
94
0
  switch (prop_id)
95
0
    {
96
0
     case PROP_FAMILY:
97
0
      g_value_set_enum (value, g_socket_address_get_family (address));
98
0
      break;
99
100
0
     default:
101
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102
0
    }
103
0
}
104
105
static void
106
g_socket_address_class_init (GSocketAddressClass *klass)
107
0
{
108
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
109
110
0
  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
0
  g_object_class_install_property (gobject_class, PROP_FAMILY,
120
0
                                   g_param_spec_enum ("family", NULL, NULL,
121
0
                  G_TYPE_SOCKET_FAMILY,
122
0
                  G_SOCKET_FAMILY_INVALID,
123
0
                  G_PARAM_READABLE |
124
0
                                                      G_PARAM_STATIC_STRINGS));
125
0
}
126
127
static void
128
g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
129
0
{
130
0
  connectable_iface->enumerate  = g_socket_address_connectable_enumerate;
131
0
  connectable_iface->proxy_enumerate  = g_socket_address_connectable_proxy_enumerate;
132
  /* to_string() is implemented by subclasses */
133
0
}
134
135
static void
136
g_socket_address_init (GSocketAddress *address)
137
0
{
138
139
0
}
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
g_socket_address_get_native_size (GSocketAddress *address)
157
0
{
158
0
  g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
159
160
0
  return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
161
0
}
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
g_socket_address_to_native (GSocketAddress  *address,
185
          gpointer         dest,
186
          gsize            destlen,
187
          GError         **error)
188
0
{
189
0
  g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
190
191
0
  return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
192
0
}
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
g_socket_address_new_from_native (gpointer native,
209
          gsize    len)
210
0
{
211
0
  gshort family;
212
213
0
  if (len < sizeof (gshort))
214
0
    return NULL;
215
216
0
  family = ((struct sockaddr *) native)->sa_family;
217
218
0
  if (family == AF_UNSPEC)
219
0
    return NULL;
220
221
0
  if (family == AF_INET)
222
0
    {
223
0
      struct sockaddr_in *addr = (struct sockaddr_in *) native;
224
0
      GInetAddress *iaddr;
225
0
      GSocketAddress *sockaddr;
226
227
0
      if (len < sizeof (*addr))
228
0
  return NULL;
229
230
0
      iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), G_SOCKET_FAMILY_IPV4);
231
0
      sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
232
0
      g_object_unref (iaddr);
233
0
      return sockaddr;
234
0
    }
235
236
0
  if (family == AF_INET6)
237
0
    {
238
0
      struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
239
0
      GInetAddress *iaddr;
240
0
      GSocketAddress *sockaddr;
241
242
0
      if (len < sizeof (*addr))
243
0
  return NULL;
244
245
0
      if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
246
0
  {
247
0
    struct sockaddr_in sin_addr;
248
249
0
    sin_addr.sin_family = AF_INET;
250
0
    sin_addr.sin_port = addr->sin6_port;
251
0
    memcpy (&(sin_addr.sin_addr.s_addr), addr->sin6_addr.s6_addr + 12, 4);
252
0
    iaddr = g_inet_address_new_from_bytes ((guint8 *) &(sin_addr.sin_addr), G_SOCKET_FAMILY_IPV4);
253
0
  }
254
0
      else
255
0
  {
256
0
    iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), G_SOCKET_FAMILY_IPV6);
257
0
  }
258
259
0
      sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
260
0
             "address", iaddr,
261
0
             "port", g_ntohs (addr->sin6_port),
262
0
             "flowinfo", addr->sin6_flowinfo,
263
0
             "scope_id", addr->sin6_scope_id,
264
0
             NULL);
265
0
      g_object_unref (iaddr);
266
0
      return sockaddr;
267
0
    }
268
269
0
  if (family == AF_UNIX)
270
0
    {
271
0
      struct sockaddr_un *addr = (struct sockaddr_un *) native;
272
0
      gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
273
274
0
      if (path_len == 0)
275
0
  {
276
0
    return g_unix_socket_address_new_with_type ("", 0,
277
0
                  G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
278
0
  }
279
0
      else if (addr->sun_path[0] == 0)
280
0
  {
281
0
    if (!g_unix_socket_address_abstract_names_supported ())
282
0
      {
283
0
        return g_unix_socket_address_new_with_type ("", 0,
284
0
                G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
285
0
      }
286
0
    else if (len < sizeof (*addr))
287
0
      {
288
0
        return g_unix_socket_address_new_with_type (addr->sun_path + 1,
289
0
                path_len - 1,
290
0
                G_UNIX_SOCKET_ADDRESS_ABSTRACT);
291
0
      }
292
0
    else
293
0
      {
294
0
        return g_unix_socket_address_new_with_type (addr->sun_path + 1,
295
0
                path_len - 1,
296
0
                G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
297
0
      }
298
0
  }
299
0
      else
300
0
  return g_unix_socket_address_new (addr->sun_path);
301
0
    }
302
303
0
  return g_native_socket_address_new (native, len);
304
0
}
305
306
307
0
#define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
308
0
#define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
309
310
typedef struct {
311
  GSocketAddressEnumerator parent_instance;
312
313
  GSocketAddress *sockaddr;
314
} GSocketAddressAddressEnumerator;
315
316
typedef struct {
317
  GSocketAddressEnumeratorClass parent_class;
318
319
} GSocketAddressAddressEnumeratorClass;
320
321
static GType _g_socket_address_address_enumerator_get_type (void);
322
G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
323
324
static void
325
g_socket_address_address_enumerator_finalize (GObject *object)
326
0
{
327
0
  GSocketAddressAddressEnumerator *sockaddr_enum =
328
0
    G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
329
330
0
  if (sockaddr_enum->sockaddr)
331
0
    g_object_unref (sockaddr_enum->sockaddr);
332
333
0
  G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
334
0
}
335
336
static GSocketAddress *
337
g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
338
            GCancellable              *cancellable,
339
            GError                   **error)
340
0
{
341
0
  GSocketAddressAddressEnumerator *sockaddr_enum =
342
0
    G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
343
344
0
  if (sockaddr_enum->sockaddr)
345
0
    {
346
0
      GSocketAddress *ret = sockaddr_enum->sockaddr;
347
348
0
      sockaddr_enum->sockaddr = NULL;
349
0
      return ret;
350
0
    }
351
0
  else
352
0
    return NULL;
353
0
}
354
355
static void
356
_g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
357
0
{
358
0
}
359
360
static void
361
_g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
362
0
{
363
0
  GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
364
0
  GSocketAddressEnumeratorClass *enumerator_class =
365
0
    G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
366
367
0
  enumerator_class->next = g_socket_address_address_enumerator_next;
368
0
  object_class->finalize = g_socket_address_address_enumerator_finalize;
369
0
}
370
371
static GSocketAddressEnumerator *
372
g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
373
0
{
374
0
  GSocketAddressAddressEnumerator *sockaddr_enum;
375
376
0
  sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
377
0
  sockaddr_enum->sockaddr = g_object_ref (G_SOCKET_ADDRESS (connectable));
378
379
0
  return (GSocketAddressEnumerator *)sockaddr_enum;
380
0
}
381
382
static GSocketAddressEnumerator *
383
g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
384
0
{
385
0
  GSocketAddressEnumerator *addr_enum = NULL;
386
387
0
  g_assert (connectable != NULL);
388
389
0
  if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
390
0
      !G_IS_PROXY_ADDRESS (connectable))
391
0
    {
392
0
      GInetAddress *addr;
393
0
      guint port;
394
0
      gchar *uri;
395
0
      gchar *ip;
396
397
0
      g_object_get (connectable, "address", &addr, "port", &port, NULL);
398
399
0
      ip = g_inet_address_to_string (addr);
400
0
      uri = g_uri_join (G_URI_FLAGS_NONE, "none", NULL, ip, port, "", NULL, NULL);
401
402
0
      addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
403
0
                                "connectable", connectable,
404
0
                                "uri", uri,
405
0
                                NULL);
406
407
0
      g_object_unref (addr);
408
0
      g_free (ip);
409
0
      g_free (uri);
410
0
    }
411
0
  else
412
0
    {
413
0
      addr_enum = g_socket_address_connectable_enumerate (connectable);
414
0
    }
415
416
0
  return addr_enum;
417
0
}