Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gsocketaddress.c
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 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
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
27 #include "gsocketaddress.h"
28 #include "ginetaddress.h"
29 #include "ginetsocketaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gsocketaddressenumerator.h"
32 #include "gsocketconnectable.h"
33 #include "glibintl.h"
34 #include "gioenumtypes.h"
35
36 #ifdef G_OS_UNIX
37 #include "gunixsocketaddress.h"
38 #endif
39
40 #include "gioalias.h"
41
42 /**
43  * SECTION:gsocketaddress
44  * @short_description: Abstract base class representing endpoints for
45  * socket communication
46  *
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.
51  */
52
53 /**
54  * GSocketAddress:
55  *
56  * A socket endpoint address, corresponding to <type>struct sockaddr</type>
57  * or one of its subtypes.
58  */
59
60 enum
61 {
62   PROP_NONE,
63   PROP_FAMILY
64 };
65
66 static void                      g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
67 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate  (GSocketConnectable      *connectable);
68
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))
72
73 /**
74  * g_socket_address_get_family:
75  * @address: a #GSocketAddress
76  *
77  * Gets the socket family type of @address.
78  *
79  * Returns: the socket family type of @address.
80  *
81  * Since: 2.22
82  */
83 GSocketFamily
84 g_socket_address_get_family (GSocketAddress *address)
85 {
86   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
87
88   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
89 }
90
91 static void
92 g_socket_address_get_property (GObject *object, guint prop_id,
93                                GValue *value, GParamSpec *pspec)
94 {
95   GSocketAddress *address = G_SOCKET_ADDRESS (object);
96
97   switch (prop_id)
98     {
99      case PROP_FAMILY:
100       g_value_set_enum (value, g_socket_address_get_family (address));
101       break;
102
103      default:
104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105     }
106 }
107
108 static void
109 g_socket_address_class_init (GSocketAddressClass *klass)
110 {
111   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112
113   gobject_class->get_property = g_socket_address_get_property;
114
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 |
122                                                       G_PARAM_STATIC_STRINGS));
123 }
124
125 static void
126 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
127 {
128   connectable_iface->enumerate  = g_socket_address_connectable_enumerate;
129 }
130
131 static void
132 g_socket_address_init (GSocketAddress *address)
133 {
134
135 }
136
137 /**
138  * g_socket_address_get_native_size:
139  * @address: a #GSocketAddress
140  *
141  * Gets the size of @address's native <type>struct sockaddr</type>.
142  * You can use this to allocate memory to pass to
143  * g_socket_address_to_native().
144  *
145  * Returns: the size of the native <type>struct sockaddr</type> that
146  *     @address represents
147  *
148  * Since: 2.22
149  */
150 gssize
151 g_socket_address_get_native_size (GSocketAddress *address)
152 {
153   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
154
155   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
156 }
157
158 /**
159  * g_socket_address_to_native:
160  * @address: a #GSocketAddress
161  * @dest: a pointer to a memory location that will contain the native
162  * <type>struct sockaddr</type>.
163  * @destlen: the size of @dest. Must be at least as large as
164  * g_socket_address_get_native_size().
165  * @error: #GError for error reporting, or %NULL to ignore.
166  *
167  * Converts a #GSocketAddress to a native <type>struct
168  * sockaddr</type>, which can be passed to low-level functions like
169  * connect() or bind().
170  *
171  * If not enough space is availible, a %G_IO_ERROR_NO_SPACE error is
172  * returned. If the address type is not known on the system
173  * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
174  *
175  * Returns: %TRUE if @dest was filled in, %FALSE on error
176  *
177  * Since: 2.22
178  */
179 gboolean
180 g_socket_address_to_native (GSocketAddress  *address,
181                             gpointer         dest,
182                             gsize            destlen,
183                             GError         **error)
184 {
185   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
186
187   return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
188 }
189
190 /**
191  * g_socket_address_new_from_native:
192  * @native: a pointer to a <type>struct sockaddr</type>
193  * @len: the size of the memory location pointed to by @native
194  *
195  * Creates a #GSocketAddress subclass corresponding to the native
196  * <type>struct sockaddr</type> @native.
197  *
198  * Returns: a new #GSocketAddress if @native could successfully be converted,
199  * otherwise %NULL.
200  *
201  * Since: 2.22
202  */
203 GSocketAddress *
204 g_socket_address_new_from_native (gpointer native,
205                                   gsize    len)
206 {
207   gshort family;
208
209   if (len < sizeof (gshort))
210     return NULL;
211
212   family = ((struct sockaddr *) native)->sa_family;
213
214   if (family == AF_UNSPEC)
215     return NULL;
216
217   if (family == AF_INET)
218     {
219       struct sockaddr_in *addr = (struct sockaddr_in *) native;
220       GInetAddress *iaddr;
221       GSocketAddress *sockaddr;
222
223       if (len < sizeof (*addr))
224         return NULL;
225
226       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
227       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
228       g_object_unref (iaddr);
229       return sockaddr;
230     }
231
232   if (family == AF_INET6)
233     {
234       struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
235       GInetAddress *iaddr;
236       GSocketAddress *sockaddr;
237
238       if (len < sizeof (*addr))
239         return NULL;
240
241       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
242       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
243       g_object_unref (iaddr);
244       return sockaddr;
245     }
246
247 #ifdef G_OS_UNIX
248   if (family == AF_UNIX)
249     {
250       struct sockaddr_un *addr = (struct sockaddr_un *) native;
251       gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
252
253       if (path_len == 0)
254         {
255           return g_unix_socket_address_new_with_type ("", 0,
256                                                       G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
257         }
258       else if (addr->sun_path[0] == 0)
259         {
260           if (len < sizeof (*addr))
261             {
262               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
263                                                           path_len - 1,
264                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT);
265             }
266           else
267             {
268               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
269                                                           path_len - 1,
270                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
271             }
272         }
273       else
274         return g_unix_socket_address_new (addr->sun_path);
275     }
276 #endif
277
278   return NULL;
279 }
280
281
282 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
283 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
284
285 typedef struct {
286   GSocketAddressEnumerator parent_instance;
287
288   GSocketAddress *sockaddr;
289 } GSocketAddressAddressEnumerator;
290
291 typedef struct {
292   GSocketAddressEnumeratorClass parent_class;
293
294 } GSocketAddressAddressEnumeratorClass;
295
296 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
297
298 static void
299 g_socket_address_address_enumerator_finalize (GObject *object)
300 {
301   GSocketAddressAddressEnumerator *sockaddr_enum =
302     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
303
304   if (sockaddr_enum->sockaddr)
305     g_object_unref (sockaddr_enum->sockaddr);
306
307   G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
308 }
309
310 static GSocketAddress *
311 g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
312                                           GCancellable              *cancellable,
313                                           GError                   **error)
314 {
315   GSocketAddressAddressEnumerator *sockaddr_enum =
316     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
317
318   if (sockaddr_enum->sockaddr)
319     {
320       GSocketAddress *ret = sockaddr_enum->sockaddr;
321
322       sockaddr_enum->sockaddr = NULL;
323       return ret;
324     }
325   else
326     return NULL;
327 }
328
329 static void
330 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
331 {
332 }
333
334 static void
335 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
336 {
337   GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
338   GSocketAddressEnumeratorClass *enumerator_class =
339     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
340
341   enumerator_class->next = g_socket_address_address_enumerator_next;
342   object_class->finalize = g_socket_address_address_enumerator_finalize;
343 }
344
345 static GSocketAddressEnumerator *
346 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
347 {
348   GSocketAddressAddressEnumerator *sockaddr_enum;
349
350   sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
351   sockaddr_enum->sockaddr = g_object_ref (connectable);
352
353   return (GSocketAddressEnumerator *)sockaddr_enum;
354 }
355
356 #define __G_SOCKET_ADDRESS_C__
357 #include "gioaliasdef.c"