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