Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/ginetsocketaddress.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 "ginetsocketaddress.h"
29
#include "ginetaddress.h"
30
#include "gnetworkingprivate.h"
31
#include "gsocketconnectable.h"
32
#include "gioerror.h"
33
#include "glibintl.h"
34
35
36
/**
37
 * SECTION:ginetsocketaddress
38
 * @short_description: Internet GSocketAddress
39
 * @include: gio/gio.h
40
 *
41
 * An IPv4 or IPv6 socket address; that is, the combination of a
42
 * #GInetAddress and a port number.
43
 */
44
45
/**
46
 * GInetSocketAddress:
47
 *
48
 * An IPv4 or IPv6 socket address, corresponding to a struct
49
 * sockaddr_in or struct sockaddr_in6.
50
 */
51
52
struct _GInetSocketAddressPrivate
53
{
54
  GInetAddress *address;
55
  guint16       port;
56
  guint32       flowinfo;
57
  guint32       scope_id;
58
};
59
60
static void   g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
61
static gchar *g_inet_socket_address_connectable_to_string  (GSocketConnectable      *connectable);
62
63
G_DEFINE_TYPE_WITH_CODE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS,
64
                         G_ADD_PRIVATE (GInetSocketAddress)
65
                         G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
66
                                                g_inet_socket_address_connectable_iface_init))
67
68
enum {
69
  PROP_0,
70
  PROP_ADDRESS,
71
  PROP_PORT,
72
  PROP_FLOWINFO,
73
  PROP_SCOPE_ID
74
};
75
76
static void
77
g_inet_socket_address_dispose (GObject *object)
78
0
{
79
0
  GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
80
81
0
  g_clear_object (&(address->priv->address));
82
83
0
  G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
84
0
}
85
86
static void
87
g_inet_socket_address_get_property (GObject    *object,
88
                                    guint       prop_id,
89
                                    GValue     *value,
90
                                    GParamSpec *pspec)
91
0
{
92
0
  GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
93
94
0
  switch (prop_id)
95
0
    {
96
0
      case PROP_ADDRESS:
97
0
        g_value_set_object (value, address->priv->address);
98
0
        break;
99
100
0
      case PROP_PORT:
101
0
        g_value_set_uint (value, address->priv->port);
102
0
        break;
103
104
0
      case PROP_FLOWINFO:
105
0
  g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
106
0
        g_value_set_uint (value, address->priv->flowinfo);
107
0
        break;
108
109
0
      case PROP_SCOPE_ID:
110
0
  g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
111
0
        g_value_set_uint (value, address->priv->scope_id);
112
0
        break;
113
114
0
      default:
115
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116
0
    }
117
0
}
118
119
static void
120
g_inet_socket_address_set_property (GObject      *object,
121
                                    guint         prop_id,
122
                                    const GValue *value,
123
                                    GParamSpec   *pspec)
124
0
{
125
0
  GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
126
127
0
  switch (prop_id)
128
0
    {
129
0
      case PROP_ADDRESS:
130
0
        address->priv->address = g_object_ref (g_value_get_object (value));
131
0
        break;
132
133
0
      case PROP_PORT:
134
0
        address->priv->port = (guint16) g_value_get_uint (value);
135
0
        break;
136
137
0
      case PROP_FLOWINFO:
138
  /* We can't test that address->priv->address is IPv6 here,
139
   * since this property might get set before PROP_ADDRESS.
140
   */
141
0
        address->priv->flowinfo = g_value_get_uint (value);
142
0
        break;
143
144
0
      case PROP_SCOPE_ID:
145
0
        address->priv->scope_id = g_value_get_uint (value);
146
0
        break;
147
148
0
      default:
149
0
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150
0
    }
151
0
}
152
153
static GSocketFamily
154
g_inet_socket_address_get_family (GSocketAddress *address)
155
0
{
156
0
  GInetSocketAddress *addr;
157
158
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
159
160
0
  addr = G_INET_SOCKET_ADDRESS (address);
161
162
0
  return g_inet_address_get_family (addr->priv->address);
163
0
}
164
165
static gssize
166
g_inet_socket_address_get_native_size (GSocketAddress *address)
167
0
{
168
0
  GInetSocketAddress *addr;
169
0
  GSocketFamily family;
170
171
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
172
173
0
  addr = G_INET_SOCKET_ADDRESS (address);
174
0
  family = g_inet_address_get_family (addr->priv->address);
175
176
0
  if (family == AF_INET)
177
0
    return sizeof (struct sockaddr_in);
178
0
  else if (family == AF_INET6)
179
0
    return sizeof (struct sockaddr_in6);
180
0
  else
181
0
    return -1;
182
0
}
183
184
static gboolean
185
g_inet_socket_address_to_native (GSocketAddress  *address,
186
                                 gpointer         dest,
187
         gsize            destlen,
188
         GError         **error)
189
0
{
190
0
  GInetSocketAddress *addr;
191
0
  GSocketFamily family;
192
193
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
194
195
0
  addr = G_INET_SOCKET_ADDRESS (address);
196
0
  family = g_inet_address_get_family (addr->priv->address);
197
198
0
  if (family == AF_INET)
199
0
    {
200
0
      struct sockaddr_in *sock = (struct sockaddr_in *) dest;
201
202
0
      if (destlen < sizeof (*sock))
203
0
  {
204
0
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
205
0
             _("Not enough space for socket address"));
206
0
    return FALSE;
207
0
  }
208
209
0
      sock->sin_family = AF_INET;
210
0
      sock->sin_port = g_htons (addr->priv->port);
211
0
      memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
212
0
      memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
213
0
      return TRUE;
214
0
    }
215
0
  else if (family == AF_INET6)
216
0
    {
217
0
      struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
218
219
0
      if (destlen < sizeof (*sock))
220
0
  {
221
0
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
222
0
             _("Not enough space for socket address"));
223
0
    return FALSE;
224
0
  }
225
226
0
      memset (sock, 0, sizeof (*sock));
227
0
      sock->sin6_family = AF_INET6;
228
0
      sock->sin6_port = g_htons (addr->priv->port);
229
0
      sock->sin6_flowinfo = addr->priv->flowinfo;
230
0
      sock->sin6_scope_id = addr->priv->scope_id;
231
0
      memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
232
0
      return TRUE;
233
0
    }
234
0
  else
235
0
    {
236
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
237
0
         _("Unsupported socket address"));
238
0
      return FALSE;
239
0
    }
240
0
}
241
242
static void
243
g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
244
0
{
245
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
246
0
  GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
247
248
0
  gobject_class->dispose = g_inet_socket_address_dispose;
249
0
  gobject_class->set_property = g_inet_socket_address_set_property;
250
0
  gobject_class->get_property = g_inet_socket_address_get_property;
251
252
0
  gsocketaddress_class->get_family = g_inet_socket_address_get_family;
253
0
  gsocketaddress_class->to_native = g_inet_socket_address_to_native;
254
0
  gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
255
256
0
  g_object_class_install_property (gobject_class, PROP_ADDRESS,
257
0
                                   g_param_spec_object ("address",
258
0
                                                        P_("Address"),
259
0
                                                        P_("The address"),
260
0
                                                        G_TYPE_INET_ADDRESS,
261
0
                                                        G_PARAM_CONSTRUCT_ONLY |
262
0
                                                        G_PARAM_READWRITE |
263
0
                                                        G_PARAM_STATIC_STRINGS));
264
265
0
  g_object_class_install_property (gobject_class, PROP_PORT,
266
0
                                   g_param_spec_uint ("port",
267
0
                                                      P_("Port"),
268
0
                                                      P_("The port"),
269
0
                                                      0,
270
0
                                                      65535,
271
0
                                                      0,
272
0
                                                      G_PARAM_CONSTRUCT_ONLY |
273
0
                                                      G_PARAM_READWRITE |
274
0
                                                      G_PARAM_STATIC_STRINGS));
275
276
  /**
277
   * GInetSocketAddress:flowinfo:
278
   *
279
   * The `sin6_flowinfo` field, for IPv6 addresses.
280
   *
281
   * Since: 2.32
282
   */
283
0
  g_object_class_install_property (gobject_class, PROP_FLOWINFO,
284
0
                                   g_param_spec_uint ("flowinfo",
285
0
                                                      P_("Flow info"),
286
0
                                                      P_("IPv6 flow info"),
287
0
                                                      0,
288
0
                                                      G_MAXUINT32,
289
0
                                                      0,
290
0
                                                      G_PARAM_CONSTRUCT_ONLY |
291
0
                                                      G_PARAM_READWRITE |
292
0
                                                      G_PARAM_STATIC_STRINGS));
293
294
  /**
295
   * GInetSocketAddress:scope_id:
296
   *
297
   * The `sin6_scope_id` field, for IPv6 addresses.
298
   *
299
   * Since: 2.32
300
   */
301
0
  g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
302
0
                                   g_param_spec_uint ("scope-id",
303
0
                                                      P_("Scope ID"),
304
0
                                                      P_("IPv6 scope ID"),
305
0
                                                      0,
306
0
                                                      G_MAXUINT32,
307
0
                                                      0,
308
0
                                                      G_PARAM_CONSTRUCT_ONLY |
309
0
                                                      G_PARAM_READWRITE |
310
0
                                                      G_PARAM_STATIC_STRINGS));
311
0
}
312
313
static void
314
g_inet_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
315
0
{
316
0
  GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
317
318
0
  iface->enumerate = parent_iface->enumerate;
319
0
  iface->proxy_enumerate = parent_iface->proxy_enumerate;
320
0
  iface->to_string = g_inet_socket_address_connectable_to_string;
321
0
}
322
323
static gchar *
324
g_inet_socket_address_connectable_to_string (GSocketConnectable *connectable)
325
0
{
326
0
  GInetSocketAddress *sa;
327
0
  GInetAddress *a;
328
0
  gchar *a_string;
329
0
  GString *out;
330
0
  guint16 port;
331
332
0
  sa = G_INET_SOCKET_ADDRESS (connectable);
333
0
  a = g_inet_socket_address_get_address (sa);
334
0
  out = g_string_new ("");
335
336
  /* Address. */
337
0
  a_string = g_inet_address_to_string (a);
338
0
  g_string_append (out, a_string);
339
0
  g_free (a_string);
340
341
  /* Scope ID (IPv6 only). */
342
0
  if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6 &&
343
0
      g_inet_socket_address_get_scope_id (sa) != 0)
344
0
    {
345
0
      g_string_append_printf (out, "%%%u",
346
0
                              g_inet_socket_address_get_scope_id (sa));
347
0
    }
348
349
  /* Port. */
350
0
  port = g_inet_socket_address_get_port (sa);
351
0
  if (port != 0)
352
0
    {
353
      /* Disambiguate ports from IPv6 addresses using square brackets. */
354
0
      if (g_inet_address_get_family (a) == G_SOCKET_FAMILY_IPV6)
355
0
        {
356
0
          g_string_prepend (out, "[");
357
0
          g_string_append (out, "]");
358
0
        }
359
360
0
      g_string_append_printf (out, ":%u", port);
361
0
    }
362
363
0
  return g_string_free (out, FALSE);
364
0
}
365
366
static void
367
g_inet_socket_address_init (GInetSocketAddress *address)
368
0
{
369
0
  address->priv = g_inet_socket_address_get_instance_private (address);
370
0
}
371
372
/**
373
 * g_inet_socket_address_new:
374
 * @address: a #GInetAddress
375
 * @port: a port number
376
 *
377
 * Creates a new #GInetSocketAddress for @address and @port.
378
 *
379
 * Returns: a new #GInetSocketAddress
380
 *
381
 * Since: 2.22
382
 */
383
GSocketAddress *
384
g_inet_socket_address_new (GInetAddress *address,
385
                           guint16       port)
386
0
{
387
0
  return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
388
0
           "address", address,
389
0
           "port", port,
390
0
           NULL);
391
0
}
392
393
/**
394
 * g_inet_socket_address_new_from_string:
395
 * @address: the string form of an IP address
396
 * @port: a port number
397
 *
398
 * Creates a new #GInetSocketAddress for @address and @port.
399
 *
400
 * If @address is an IPv6 address, it can also contain a scope ID
401
 * (separated from the address by a `%`).
402
 *
403
 * Returns: (nullable) (transfer full): a new #GInetSocketAddress,
404
 * or %NULL if @address cannot be parsed.
405
 *
406
 * Since: 2.40
407
 */
408
GSocketAddress *
409
g_inet_socket_address_new_from_string (const char *address,
410
                                       guint       port)
411
0
{
412
0
  static struct addrinfo *hints, hints_struct;
413
0
  GSocketAddress *saddr;
414
0
  GInetAddress *iaddr;
415
0
  struct addrinfo *res;
416
0
  gint status;
417
418
0
  if (strchr (address, ':'))
419
0
    {
420
      /* IPv6 address (or it's invalid). We use getaddrinfo() because
421
       * it will handle parsing a scope_id as well.
422
       */
423
424
0
      if (G_UNLIKELY (g_once_init_enter (&hints)))
425
0
        {
426
0
          hints_struct.ai_family = AF_UNSPEC;
427
0
          hints_struct.ai_socktype = SOCK_STREAM;
428
0
          hints_struct.ai_protocol = 0;
429
0
          hints_struct.ai_flags = AI_NUMERICHOST;
430
0
          g_once_init_leave (&hints, &hints_struct);
431
0
        }
432
433
0
      status = getaddrinfo (address, NULL, hints, &res);
434
0
      if (status != 0)
435
0
        return NULL;
436
437
0
      if (res->ai_family == AF_INET6 &&
438
0
          res->ai_addrlen == sizeof (struct sockaddr_in6))
439
0
        {
440
0
          ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
441
0
          saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
442
0
        }
443
0
      else
444
0
        saddr = NULL;
445
446
0
      freeaddrinfo (res);
447
0
    }
448
0
  else
449
0
    {
450
      /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
451
       * because it accepts the stupid "IPv4 numbers-and-dots
452
       * notation" addresses that are never used for anything except
453
       * phishing. Since we don't have to worry about scope IDs for
454
       * IPv4, we can just use g_inet_address_new_from_string().
455
       */
456
0
      iaddr = g_inet_address_new_from_string (address);
457
0
      if (!iaddr)
458
0
        return NULL;
459
460
0
      g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
461
462
0
      saddr = g_inet_socket_address_new (iaddr, port);
463
0
      g_object_unref (iaddr);
464
0
    }
465
466
0
  return saddr;
467
0
}
468
469
/**
470
 * g_inet_socket_address_get_address:
471
 * @address: a #GInetSocketAddress
472
 *
473
 * Gets @address's #GInetAddress.
474
 *
475
 * Returns: (transfer none): the #GInetAddress for @address, which must be
476
 * g_object_ref()'d if it will be stored
477
 *
478
 * Since: 2.22
479
 */
480
GInetAddress *
481
g_inet_socket_address_get_address (GInetSocketAddress *address)
482
0
{
483
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
484
485
0
  return address->priv->address;
486
0
}
487
488
/**
489
 * g_inet_socket_address_get_port:
490
 * @address: a #GInetSocketAddress
491
 *
492
 * Gets @address's port.
493
 *
494
 * Returns: the port for @address
495
 *
496
 * Since: 2.22
497
 */
498
guint16
499
g_inet_socket_address_get_port (GInetSocketAddress *address)
500
0
{
501
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
502
503
0
  return address->priv->port;
504
0
}
505
506
507
/**
508
 * g_inet_socket_address_get_flowinfo:
509
 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
510
 *
511
 * Gets the `sin6_flowinfo` field from @address,
512
 * which must be an IPv6 address.
513
 *
514
 * Returns: the flowinfo field
515
 *
516
 * Since: 2.32
517
 */
518
guint32
519
g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
520
0
{
521
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
522
0
  g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
523
524
0
  return address->priv->flowinfo;
525
0
}
526
527
/**
528
 * g_inet_socket_address_get_scope_id:
529
 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
530
 *
531
 * Gets the `sin6_scope_id` field from @address,
532
 * which must be an IPv6 address.
533
 *
534
 * Returns: the scope id field
535
 *
536
 * Since: 2.32
537
 */
538
guint32
539
g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
540
0
{
541
0
  g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
542
0
  g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
543
544
0
  return address->priv->scope_id;
545
0
}