Coverage Report

Created: 2025-06-13 06:55

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