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>
27 #include "gsocketaddress.h"
28 #include "ginetaddress.h"
29 #include "ginetsocketaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gsocketaddressenumerator.h"
32 #include "gsocketconnectable.h"
34 #include "gioenumtypes.h"
37 #include "gunixsocketaddress.h"
43 * SECTION:gsocketaddress
44 * @short_description: Abstract base class representing endpoints for
45 * socket communication
47 * #GSocketAddress is the equivalent of <type>struct sockaddr</type>
48 * in the BSD sockets API. This is an abstract class; use
49 * #GInetSocketAddress for internet sockets, or #GUnixSocketAddress
50 * for UNIX domain sockets.
56 * A socket endpoint address, corresponding to <type>struct sockaddr</type>
57 * or one of its subtypes.
66 static void g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
67 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate (GSocketConnectable *connectable);
69 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
71 g_socket_address_connectable_iface_init))
74 * g_socket_address_get_family:
75 * @address: a #GSocketAddress
77 * Gets the socket family type of @address.
79 * Returns: the socket family type of @address.
84 g_socket_address_get_family (GSocketAddress *address)
86 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
88 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
92 g_socket_address_get_property (GObject *object, guint prop_id,
93 GValue *value, GParamSpec *pspec)
95 GSocketAddress *address = G_SOCKET_ADDRESS (object);
100 g_value_set_enum (value, g_socket_address_get_family (address));
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 g_socket_address_class_init (GSocketAddressClass *klass)
111 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 gobject_class->get_property = g_socket_address_get_property;
115 g_object_class_install_property (gobject_class, PROP_FAMILY,
116 g_param_spec_enum ("family",
117 P_("Address family"),
118 P_("The family of the socket address"),
119 G_TYPE_SOCKET_FAMILY,
120 G_SOCKET_FAMILY_INVALID,
121 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NAME));
125 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
127 connectable_iface->enumerate = g_socket_address_connectable_enumerate;
131 g_socket_address_init (GSocketAddress *address)
137 * g_socket_address_get_native_size:
138 * @address: a #GSocketAddress
140 * Gets the size of @address's native <type>struct sockaddr</type>.
141 * You can use this to allocate memory to pass to
142 * g_socket_address_to_native().
144 * Returns: the size of the native <type>struct sockaddr</type> that
145 * @address represents
150 g_socket_address_get_native_size (GSocketAddress *address)
152 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
154 return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
158 * g_socket_address_to_native:
159 * @address: a #GSocketAddress
160 * @dest: a pointer to a memory location that will contain the native
161 * <type>struct sockaddr</type>.
162 * @destlen: the size of @dest. Must be at least as large as
163 * g_socket_address_get_native_size().
164 * @error: #GError for error reporting, or %NULL to ignore.
166 * Converts a #GSocketAddress to a native <type>struct
167 * sockaddr</type>, which can be passed to low-level functions like
168 * connect() or bind().
170 * If not enough space is availible, a %G_IO_ERROR_NO_SPACE error is
171 * returned. If the address type is not known on the system
172 * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
174 * Returns: %TRUE if @dest was filled in, %FALSE on error
179 g_socket_address_to_native (GSocketAddress *address,
184 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
186 return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
190 * g_socket_address_new_from_native:
191 * @native: a pointer to a <type>struct sockaddr</type>
192 * @len: the size of the memory location pointed to by @native
194 * Creates a #GSocketAddress subclass corresponding to the native
195 * <type>struct sockaddr</type> @native.
197 * Returns: a new #GSocketAddress if @native could successfully be converted,
203 g_socket_address_new_from_native (gpointer native,
208 if (len < sizeof (gshort))
211 family = ((struct sockaddr *) native)->sa_family;
213 if (family == AF_UNSPEC)
216 if (family == AF_INET)
218 struct sockaddr_in *addr = (struct sockaddr_in *) native;
219 GInetAddress *iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
220 GSocketAddress *sockaddr;
222 sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
223 g_object_unref (iaddr);
227 if (family == AF_INET6)
229 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
230 GInetAddress *iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
231 GSocketAddress *sockaddr;
233 sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
234 g_object_unref (iaddr);
239 if (family == AF_UNIX)
241 struct sockaddr_un *addr = (struct sockaddr_un *) native;
243 if (addr->sun_path[0] == 0)
244 return g_unix_socket_address_new_abstract (addr->sun_path+1,
245 sizeof (addr->sun_path) - 1);
246 return g_unix_socket_address_new (addr->sun_path);
254 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
255 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
258 GSocketAddressEnumerator parent_instance;
260 GSocketAddress *sockaddr;
261 } GSocketAddressAddressEnumerator;
264 GSocketAddressEnumeratorClass parent_class;
266 } GSocketAddressAddressEnumeratorClass;
268 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
271 g_socket_address_address_enumerator_finalize (GObject *object)
273 GSocketAddressAddressEnumerator *sockaddr_enum =
274 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
276 if (sockaddr_enum->sockaddr)
277 g_object_unref (sockaddr_enum->sockaddr);
279 G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
282 static GSocketAddress *
283 g_socket_address_address_enumerator_next (GSocketAddressEnumerator *enumerator,
284 GCancellable *cancellable,
287 GSocketAddressAddressEnumerator *sockaddr_enum =
288 G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
290 if (sockaddr_enum->sockaddr)
292 GSocketAddress *ret = sockaddr_enum->sockaddr;
294 sockaddr_enum->sockaddr = NULL;
302 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
307 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
309 GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
310 GSocketAddressEnumeratorClass *enumerator_class =
311 G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
313 enumerator_class->next = g_socket_address_address_enumerator_next;
314 object_class->finalize = g_socket_address_address_enumerator_finalize;
317 static GSocketAddressEnumerator *
318 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
320 GSocketAddressAddressEnumerator *sockaddr_enum;
322 sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
323 sockaddr_enum->sockaddr = g_object_ref (connectable);
325 return (GSocketAddressEnumerator *)sockaddr_enum;
328 #define __G_SOCKET_ADDRESS_C__
329 #include "gioaliasdef.c"