It is 'registered', not 'registred'
[glib.git] / gio / gsocketaddress.c
blob83414fc89ab92dbb2a1a81b4b07ffd1092b8f099
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
24 #include <config.h>
25 #include <glib.h>
26 #include <string.h>
28 #include "gsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "ginetsocketaddress.h"
31 #include "gnetworkingprivate.h"
32 #include "gproxyaddress.h"
33 #include "gproxyaddressenumerator.h"
34 #include "gsocketaddressenumerator.h"
35 #include "gsocketconnectable.h"
36 #include "glibintl.h"
37 #include "gioenumtypes.h"
39 #ifdef G_OS_UNIX
40 #include "gunixsocketaddress.h"
41 #endif
44 /**
45 * SECTION:gsocketaddress
46 * @short_description: Abstract base class representing endpoints for
47 * socket communication
49 * #GSocketAddress is the equivalent of <type>struct sockaddr</type>
50 * in the BSD sockets API. This is an abstract class; use
51 * #GInetSocketAddress for internet sockets, or #GUnixSocketAddress
52 * for UNIX domain sockets.
55 /**
56 * GSocketAddress:
58 * A socket endpoint address, corresponding to <type>struct sockaddr</type>
59 * or one of its subtypes.
62 enum
64 PROP_NONE,
65 PROP_FAMILY
68 static void g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
69 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate (GSocketConnectable *connectable);
70 static GSocketAddressEnumerator *g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable);
72 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
73 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
74 g_socket_address_connectable_iface_init))
76 /**
77 * g_socket_address_get_family:
78 * @address: a #GSocketAddress
80 * Gets the socket family type of @address.
82 * Returns: the socket family type of @address.
84 * Since: 2.22
86 GSocketFamily
87 g_socket_address_get_family (GSocketAddress *address)
89 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
91 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
94 static void
95 g_socket_address_get_property (GObject *object, guint prop_id,
96 GValue *value, GParamSpec *pspec)
98 GSocketAddress *address = G_SOCKET_ADDRESS (object);
100 switch (prop_id)
102 case PROP_FAMILY:
103 g_value_set_enum (value, g_socket_address_get_family (address));
104 break;
106 default:
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 static void
112 g_socket_address_class_init (GSocketAddressClass *klass)
114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 gobject_class->get_property = g_socket_address_get_property;
118 g_object_class_install_property (gobject_class, PROP_FAMILY,
119 g_param_spec_enum ("family",
120 P_("Address family"),
121 P_("The family of the socket address"),
122 G_TYPE_SOCKET_FAMILY,
123 G_SOCKET_FAMILY_INVALID,
124 G_PARAM_READABLE |
125 G_PARAM_STATIC_STRINGS));
128 static void
129 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
131 connectable_iface->enumerate = g_socket_address_connectable_enumerate;
132 connectable_iface->proxy_enumerate = g_socket_address_connectable_proxy_enumerate;
135 static void
136 g_socket_address_init (GSocketAddress *address)
142 * g_socket_address_get_native_size:
143 * @address: a #GSocketAddress
145 * Gets the size of @address's native <type>struct sockaddr</type>.
146 * You can use this to allocate memory to pass to
147 * g_socket_address_to_native().
149 * Returns: the size of the native <type>struct sockaddr</type> that
150 * @address represents
152 * Since: 2.22
154 gssize
155 g_socket_address_get_native_size (GSocketAddress *address)
157 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
159 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
163 * g_socket_address_to_native:
164 * @address: a #GSocketAddress
165 * @dest: a pointer to a memory location that will contain the native
166 * <type>struct sockaddr</type>.
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.
171 * Converts a #GSocketAddress to a native <type>struct
172 * sockaddr</type>, which can be passed to low-level functions like
173 * connect() or bind().
175 * If not enough space is available, a %G_IO_ERROR_NO_SPACE error is
176 * returned. If the address type is not known on the system
177 * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
179 * Returns: %TRUE if @dest was filled in, %FALSE on error
181 * Since: 2.22
183 gboolean
184 g_socket_address_to_native (GSocketAddress *address,
185 gpointer dest,
186 gsize destlen,
187 GError **error)
189 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
191 return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
195 * g_socket_address_new_from_native:
196 * @native: a pointer to a <type>struct sockaddr</type>
197 * @len: the size of the memory location pointed to by @native
199 * Creates a #GSocketAddress subclass corresponding to the native
200 * <type>struct sockaddr</type> @native.
202 * Returns: a new #GSocketAddress if @native could successfully be converted,
203 * otherwise %NULL.
205 * Since: 2.22
207 GSocketAddress *
208 g_socket_address_new_from_native (gpointer native,
209 gsize len)
211 gshort family;
213 if (len < sizeof (gshort))
214 return NULL;
216 family = ((struct sockaddr *) native)->sa_family;
218 if (family == AF_UNSPEC)
219 return NULL;
221 if (family == AF_INET)
223 struct sockaddr_in *addr = (struct sockaddr_in *) native;
224 GInetAddress *iaddr;
225 GSocketAddress *sockaddr;
227 if (len < sizeof (*addr))
228 return NULL;
230 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
231 sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
232 g_object_unref (iaddr);
233 return sockaddr;
236 if (family == AF_INET6)
238 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
239 GInetAddress *iaddr;
240 GSocketAddress *sockaddr;
242 if (len < sizeof (*addr))
243 return NULL;
245 if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
247 struct sockaddr_in sin_addr;
249 sin_addr.sin_family = AF_INET;
250 sin_addr.sin_port = addr->sin6_port;
251 memcpy (&(sin_addr.sin_addr.s_addr), addr->sin6_addr.s6_addr + 12, 4);
252 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(sin_addr.sin_addr), AF_INET);
254 else
256 iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
259 sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
260 "address", iaddr,
261 "port", g_ntohs (addr->sin6_port),
262 "flowinfo", g_ntohl (addr->sin6_flowinfo),
263 "scope_id", g_ntohl (addr->sin6_scope_id),
264 NULL);
265 g_object_unref (iaddr);
266 return sockaddr;
269 #ifdef G_OS_UNIX
270 if (family == AF_UNIX)
272 struct sockaddr_un *addr = (struct sockaddr_un *) native;
273 gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
275 if (path_len == 0)
277 return g_unix_socket_address_new_with_type ("", 0,
278 G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
280 else if (addr->sun_path[0] == 0)
282 if (!g_unix_socket_address_abstract_names_supported ())
284 return g_unix_socket_address_new_with_type ("", 0,
285 G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
287 else if (len < sizeof (*addr))
289 return g_unix_socket_address_new_with_type (addr->sun_path + 1,
290 path_len - 1,
291 G_UNIX_SOCKET_ADDRESS_ABSTRACT);
293 else
295 return g_unix_socket_address_new_with_type (addr->sun_path + 1,
296 path_len - 1,
297 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
300 else
301 return g_unix_socket_address_new (addr->sun_path);
303 #endif
305 return NULL;
309 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
310 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
312 typedef struct {
313 GSocketAddressEnumerator parent_instance;
315 GSocketAddress *sockaddr;
316 } GSocketAddressAddressEnumerator;
318 typedef struct {
319 GSocketAddressEnumeratorClass parent_class;
321 } GSocketAddressAddressEnumeratorClass;
323 static GType _g_socket_address_address_enumerator_get_type (void);
324 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
326 static void
327 g_socket_address_address_enumerator_finalize (GObject *object)
329 GSocketAddressAddressEnumerator *sockaddr_enum =
330 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
332 if (sockaddr_enum->sockaddr)
333 g_object_unref (sockaddr_enum->sockaddr);
335 G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
338 static GSocketAddress *
339 g_socket_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
340 GCancellable *cancellable,
341 GError **error)
343 GSocketAddressAddressEnumerator *sockaddr_enum =
344 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
346 if (sockaddr_enum->sockaddr)
348 GSocketAddress *ret = sockaddr_enum->sockaddr;
350 sockaddr_enum->sockaddr = NULL;
351 return ret;
353 else
354 return NULL;
357 static void
358 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
362 static void
363 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
365 GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
366 GSocketAddressEnumeratorClass *enumerator_class =
367 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
369 enumerator_class->next = g_socket_address_address_enumerator_next;
370 object_class->finalize = g_socket_address_address_enumerator_finalize;
373 static GSocketAddressEnumerator *
374 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
376 GSocketAddressAddressEnumerator *sockaddr_enum;
378 sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
379 sockaddr_enum->sockaddr = g_object_ref (connectable);
381 return (GSocketAddressEnumerator *)sockaddr_enum;
384 static GSocketAddressEnumerator *
385 g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
387 GSocketAddressEnumerator *addr_enum = NULL;
389 if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
390 !G_IS_PROXY_ADDRESS (connectable))
392 GInetAddress *addr;
393 guint port;
394 gchar *uri;
395 gchar *ip;
397 g_object_get (connectable, "address", &addr, "port", &port, NULL);
399 ip = g_inet_address_to_string (addr);
400 uri = _g_uri_from_authority ("none", ip, port, NULL);
402 addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
403 "connectable", connectable,
404 "uri", uri,
405 NULL);
407 g_object_unref (addr);
408 g_free (ip);
409 g_free (uri);
411 else
413 addr_enum = g_socket_address_connectable_enumerate (connectable);
416 return addr_enum;