Coverage Report

Created: 2025-12-31 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gnativesocketaddress.c
Line
Count
Source
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 "gnativesocketaddress.h"
29
#include "gnetworkingprivate.h"
30
#include "gioerror.h"
31
#include "glibintl.h"
32
33
34
/**
35
 * GNativeSocketAddress:
36
 *
37
 * A socket address of some unknown native type.
38
 *
39
 * This corresponds to a general `struct sockaddr` of a type not otherwise
40
 * handled by GLib.
41
 *
42
 * Since: 2.46
43
 */
44
45
struct _GNativeSocketAddressPrivate
46
{
47
  struct sockaddr *sockaddr;
48
  union {
49
    struct sockaddr_storage storage;
50
    struct sockaddr sa;
51
  } storage;
52
  gsize sockaddr_len;
53
};
54
55
0
G_DEFINE_TYPE_WITH_PRIVATE (GNativeSocketAddress, g_native_socket_address, G_TYPE_SOCKET_ADDRESS)
56
0
57
0
static void
58
0
g_native_socket_address_dispose (GObject *object)
59
0
{
60
0
  GNativeSocketAddress *address = G_NATIVE_SOCKET_ADDRESS (object);
61
62
0
  if (address->priv->sockaddr != &address->priv->storage.sa)
63
0
    g_free (address->priv->sockaddr);
64
65
0
  G_OBJECT_CLASS (g_native_socket_address_parent_class)->dispose (object);
66
0
}
67
68
static GSocketFamily
69
g_native_socket_address_get_family (GSocketAddress *address)
70
0
{
71
0
  GNativeSocketAddress *addr;
72
73
0
  g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0);
74
75
0
  addr = G_NATIVE_SOCKET_ADDRESS (address);
76
77
0
  return addr->priv->sockaddr->sa_family;
78
0
}
79
80
static gssize
81
g_native_socket_address_get_native_size (GSocketAddress *address)
82
0
{
83
0
  GNativeSocketAddress *addr;
84
85
0
  g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0);
86
87
0
  addr = G_NATIVE_SOCKET_ADDRESS (address);
88
89
0
  g_assert (addr->priv->sockaddr_len <= G_MAXSSIZE);
90
0
  return (gssize) addr->priv->sockaddr_len;
91
0
}
92
93
static gboolean
94
g_native_socket_address_to_native (GSocketAddress  *address,
95
                                   gpointer         dest,
96
                                   gsize            destlen,
97
                                   GError         **error)
98
0
{
99
0
  GNativeSocketAddress *addr;
100
101
0
  g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), FALSE);
102
103
0
  addr = G_NATIVE_SOCKET_ADDRESS (address);
104
105
0
  if (destlen < addr->priv->sockaddr_len)
106
0
    {
107
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
108
0
                           _("Not enough space for socket address"));
109
0
      return FALSE;
110
0
    }
111
112
0
  memcpy (dest, addr->priv->sockaddr, addr->priv->sockaddr_len);
113
0
  return TRUE;
114
0
}
115
116
static void
117
g_native_socket_address_class_init (GNativeSocketAddressClass *klass)
118
0
{
119
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120
0
  GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
121
122
0
  gobject_class->dispose = g_native_socket_address_dispose;
123
124
0
  gsocketaddress_class->get_family = g_native_socket_address_get_family;
125
0
  gsocketaddress_class->to_native = g_native_socket_address_to_native;
126
0
  gsocketaddress_class->get_native_size = g_native_socket_address_get_native_size;
127
0
}
128
129
static void
130
g_native_socket_address_init (GNativeSocketAddress *address)
131
0
{
132
0
  address->priv = g_native_socket_address_get_instance_private (address);
133
0
}
134
135
/**
136
 * g_native_socket_address_new:
137
 * @native: a native address object
138
 * @len: the length of @native, in bytes
139
 *
140
 * Creates a new #GNativeSocketAddress for @native and @len.
141
 *
142
 * Returns: a new #GNativeSocketAddress
143
 *
144
 * Since: 2.46
145
 */
146
GSocketAddress *
147
g_native_socket_address_new (gpointer        native,
148
                             gsize           len)
149
0
{
150
0
  GNativeSocketAddress *addr;
151
152
0
  addr = g_object_new (G_TYPE_NATIVE_SOCKET_ADDRESS, NULL);
153
154
0
  if (len <= sizeof(addr->priv->storage))
155
0
    addr->priv->sockaddr = &addr->priv->storage.sa;
156
0
  else
157
0
    addr->priv->sockaddr = g_malloc (len);
158
159
0
  memcpy (addr->priv->sockaddr, native, len);
160
0
  addr->priv->sockaddr_len = len;
161
0
  return G_SOCKET_ADDRESS (addr);
162
0
}