1/* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
20 */
21
22#include <config.h>
23#include <glib.h>
24#include <string.h>
25
26#include "gsocketaddress.h"
27#include "ginetaddress.h"
28#include "ginetsocketaddress.h"
29#include "gnativesocketaddress.h"
30#include "gnetworkingprivate.h"
31#include "gproxyaddress.h"
32#include "gproxyaddressenumerator.h"
33#include "gsocketaddressenumerator.h"
34#include "gsocketconnectable.h"
35#include "glibintl.h"
36#include "gioenumtypes.h"
37
38#ifdef G_OS_UNIX
39#include "gunixsocketaddress.h"
40#endif
41
42
43/**
44 * SECTION:gsocketaddress
45 * @short_description: Abstract base class representing endpoints
46 * for socket communication
47 * @include: gio/gio.h
48 *
49 * #GSocketAddress is the equivalent of struct sockaddr in the BSD
50 * sockets API. This is an abstract class; use #GInetSocketAddress
51 * for internet sockets, or #GUnixSocketAddress for UNIX domain sockets.
52 */
53
54/**
55 * GSocketAddress:
56 *
57 * A socket endpoint address, corresponding to struct sockaddr
58 * or one of its subtypes.
59 */
60
61enum
62{
63 PROP_NONE,
64 PROP_FAMILY
65};
66
67static void g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
68static GSocketAddressEnumerator *g_socket_address_connectable_enumerate (GSocketConnectable *connectable);
69static GSocketAddressEnumerator *g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable);
70
71G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
73 g_socket_address_connectable_iface_init))
74
75/**
76 * g_socket_address_get_family:
77 * @address: a #GSocketAddress
78 *
79 * Gets the socket family type of @address.
80 *
81 * Returns: the socket family type of @address
82 *
83 * Since: 2.22
84 */
85GSocketFamily
86g_socket_address_get_family (GSocketAddress *address)
87{
88 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
89
90 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
91}
92
93static void
94g_socket_address_get_property (GObject *object, guint prop_id,
95 GValue *value, GParamSpec *pspec)
96{
97 GSocketAddress *address = G_SOCKET_ADDRESS (object);
98
99 switch (prop_id)
100 {
101 case PROP_FAMILY:
102 g_value_set_enum (value, v_enum: g_socket_address_get_family (address));
103 break;
104
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 }
108}
109
110static void
111g_socket_address_class_init (GSocketAddressClass *klass)
112{
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114
115 gobject_class->get_property = g_socket_address_get_property;
116
117 g_object_class_install_property (oclass: gobject_class, property_id: PROP_FAMILY,
118 pspec: g_param_spec_enum (name: "family",
119 P_("Address family"),
120 P_("The family of the socket address"),
121 enum_type: G_TYPE_SOCKET_FAMILY,
122 default_value: G_SOCKET_FAMILY_INVALID,
123 flags: G_PARAM_READABLE |
124 G_PARAM_STATIC_STRINGS));
125}
126
127static void
128g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
129{
130 connectable_iface->enumerate = g_socket_address_connectable_enumerate;
131 connectable_iface->proxy_enumerate = g_socket_address_connectable_proxy_enumerate;
132 /* to_string() is implemented by subclasses */
133}
134
135static void
136g_socket_address_init (GSocketAddress *address)
137{
138
139}
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
151 *
152 * Since: 2.22
153 */
154gssize
155g_socket_address_get_native_size (GSocketAddress *address)
156{
157 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
158
159 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
160}
161
162/**
163 * g_socket_address_to_native:
164 * @address: a #GSocketAddress
165 * @dest: a pointer to a memory location that will contain the native
166 * struct sockaddr
167 * @destlen: the size of @dest. Must be at least as large as
168 * g_socket_address_get_native_size()
169 * @error: #GError for error reporting, or %NULL to ignore
170 *
171 * Converts a #GSocketAddress to a native struct sockaddr, which can
172 * be passed to low-level functions like connect() or bind().
173 *
174 * If not enough space is available, a %G_IO_ERROR_NO_SPACE error
175 * is returned. If the address type is not known on the system
176 * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
177 *
178 * Returns: %TRUE if @dest was filled in, %FALSE on error
179 *
180 * Since: 2.22
181 */
182gboolean
183g_socket_address_to_native (GSocketAddress *address,
184 gpointer dest,
185 gsize destlen,
186 GError **error)
187{
188 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
189
190 return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
191}
192
193/**
194 * g_socket_address_new_from_native:
195 * @native: (not nullable): a pointer to a struct sockaddr
196 * @len: the size of the memory location pointed to by @native
197 *
198 * Creates a #GSocketAddress subclass corresponding to the native
199 * struct sockaddr @native.
200 *
201 * Returns: a new #GSocketAddress if @native could successfully
202 * be converted, otherwise %NULL
203 *
204 * Since: 2.22
205 */
206GSocketAddress *
207g_socket_address_new_from_native (gpointer native,
208 gsize len)
209{
210 gshort family;
211
212 if (len < sizeof (gshort))
213 return NULL;
214
215 family = ((struct sockaddr *) native)->sa_family;
216
217 if (family == AF_UNSPEC)
218 return NULL;
219
220 if (family == AF_INET)
221 {
222 struct sockaddr_in *addr = (struct sockaddr_in *) native;
223 GInetAddress *iaddr;
224 GSocketAddress *sockaddr;
225
226 if (len < sizeof (*addr))
227 return NULL;
228
229 iaddr = g_inet_address_new_from_bytes (bytes: (guint8 *) &(addr->sin_addr), AF_INET);
230 sockaddr = g_inet_socket_address_new (address: iaddr, g_ntohs (addr->sin_port));
231 g_object_unref (object: iaddr);
232 return sockaddr;
233 }
234
235 if (family == AF_INET6)
236 {
237 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
238 GInetAddress *iaddr;
239 GSocketAddress *sockaddr;
240
241 if (len < sizeof (*addr))
242 return NULL;
243
244 if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
245 {
246 struct sockaddr_in sin_addr;
247
248 sin_addr.sin_family = AF_INET;
249 sin_addr.sin_port = addr->sin6_port;
250 memcpy (dest: &(sin_addr.sin_addr.s_addr), src: addr->sin6_addr.s6_addr + 12, n: 4);
251 iaddr = g_inet_address_new_from_bytes (bytes: (guint8 *) &(sin_addr.sin_addr), AF_INET);
252 }
253 else
254 {
255 iaddr = g_inet_address_new_from_bytes (bytes: (guint8 *) &(addr->sin6_addr), AF_INET6);
256 }
257
258 sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
259 first_property_name: "address", iaddr,
260 "port", g_ntohs (addr->sin6_port),
261 "flowinfo", addr->sin6_flowinfo,
262 "scope_id", addr->sin6_scope_id,
263 NULL);
264 g_object_unref (object: iaddr);
265 return sockaddr;
266 }
267
268#ifdef G_OS_UNIX
269 if (family == AF_UNIX)
270 {
271 struct sockaddr_un *addr = (struct sockaddr_un *) native;
272 gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
273
274 if (path_len == 0)
275 {
276 return g_unix_socket_address_new_with_type (path: "", path_len: 0,
277 type: G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
278 }
279 else if (addr->sun_path[0] == 0)
280 {
281 if (!g_unix_socket_address_abstract_names_supported ())
282 {
283 return g_unix_socket_address_new_with_type (path: "", path_len: 0,
284 type: G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
285 }
286 else if (len < sizeof (*addr))
287 {
288 return g_unix_socket_address_new_with_type (path: addr->sun_path + 1,
289 path_len: path_len - 1,
290 type: G_UNIX_SOCKET_ADDRESS_ABSTRACT);
291 }
292 else
293 {
294 return g_unix_socket_address_new_with_type (path: addr->sun_path + 1,
295 path_len: path_len - 1,
296 type: G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
297 }
298 }
299 else
300 return g_unix_socket_address_new (path: addr->sun_path);
301 }
302#endif
303
304 return g_native_socket_address_new (native, len);
305}
306
307
308#define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
309#define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
310
311typedef struct {
312 GSocketAddressEnumerator parent_instance;
313
314 GSocketAddress *sockaddr;
315} GSocketAddressAddressEnumerator;
316
317typedef struct {
318 GSocketAddressEnumeratorClass parent_class;
319
320} GSocketAddressAddressEnumeratorClass;
321
322static GType _g_socket_address_address_enumerator_get_type (void);
323G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
324
325static void
326g_socket_address_address_enumerator_finalize (GObject *object)
327{
328 GSocketAddressAddressEnumerator *sockaddr_enum =
329 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
330
331 if (sockaddr_enum->sockaddr)
332 g_object_unref (object: sockaddr_enum->sockaddr);
333
334 G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
335}
336
337static GSocketAddress *
338g_socket_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
339 GCancellable *cancellable,
340 GError **error)
341{
342 GSocketAddressAddressEnumerator *sockaddr_enum =
343 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
344
345 if (sockaddr_enum->sockaddr)
346 {
347 GSocketAddress *ret = sockaddr_enum->sockaddr;
348
349 sockaddr_enum->sockaddr = NULL;
350 return ret;
351 }
352 else
353 return NULL;
354}
355
356static void
357_g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
358{
359}
360
361static void
362_g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
363{
364 GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
365 GSocketAddressEnumeratorClass *enumerator_class =
366 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
367
368 enumerator_class->next = g_socket_address_address_enumerator_next;
369 object_class->finalize = g_socket_address_address_enumerator_finalize;
370}
371
372static GSocketAddressEnumerator *
373g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
374{
375 GSocketAddressAddressEnumerator *sockaddr_enum;
376
377 sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
378 sockaddr_enum->sockaddr = g_object_ref (G_SOCKET_ADDRESS (connectable));
379
380 return (GSocketAddressEnumerator *)sockaddr_enum;
381}
382
383static GSocketAddressEnumerator *
384g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
385{
386 GSocketAddressEnumerator *addr_enum = NULL;
387
388 g_assert (connectable != NULL);
389
390 if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
391 !G_IS_PROXY_ADDRESS (connectable))
392 {
393 GInetAddress *addr;
394 guint port;
395 gchar *uri;
396 gchar *ip;
397
398 g_object_get (object: connectable, first_property_name: "address", &addr, "port", &port, NULL);
399
400 ip = g_inet_address_to_string (address: addr);
401 uri = g_uri_join (flags: G_URI_FLAGS_NONE, scheme: "none", NULL, host: ip, port, path: "", NULL, NULL);
402
403 addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
404 first_property_name: "connectable", connectable,
405 "uri", uri,
406 NULL);
407
408 g_object_unref (object: addr);
409 g_free (mem: ip);
410 g_free (mem: uri);
411 }
412 else
413 {
414 addr_enum = g_socket_address_connectable_enumerate (connectable);
415 }
416
417 return addr_enum;
418}
419

source code of gtk/subprojects/glib/gio/gsocketaddress.c