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 "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 for
46  * socket communication
47  *
48  * #GSocketAddress is the equivalent of <type>struct sockaddr</type>
49  * in the BSD sockets API. This is an abstract class; use
50  * #GInetSocketAddress for internet sockets, or #GUnixSocketAddress
51  * for UNIX domain sockets.
52  */
53
54 /**
55  * GSocketAddress:
56  *
57  * A socket endpoint address, corresponding to <type>struct sockaddr</type>
58  * or one of its subtypes.
59  */
60
61 enum
62 {
63   PROP_NONE,
64   PROP_FAMILY
65 };
66
67 static void                      g_socket_address_connectable_iface_init       (GSocketConnectableIface *iface);
68 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate        (GSocketConnectable      *connectable);
69 static GSocketAddressEnumerator *g_socket_address_connectable_proxy_enumerate  (GSocketConnectable      *connectable);
70
71 G_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  */
85 GSocketFamily
86 g_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
93 static void
94 g_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, 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
110 static void
111 g_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 (gobject_class, PROP_FAMILY,
118                                    g_param_spec_enum ("family",
119                                                       P_("Address family"),
120                                                       P_("The family of the socket address"),
121                                                       G_TYPE_SOCKET_FAMILY,
122                                                       G_SOCKET_FAMILY_INVALID,
123                                                       G_PARAM_READABLE |
124                                                       G_PARAM_STATIC_STRINGS));
125 }
126
127 static void
128 g_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 }
133
134 static void
135 g_socket_address_init (GSocketAddress *address)
136 {
137
138 }
139
140 /**
141  * g_socket_address_get_native_size:
142  * @address: a #GSocketAddress
143  *
144  * Gets the size of @address's native <type>struct sockaddr</type>.
145  * You can use this to allocate memory to pass to
146  * g_socket_address_to_native().
147  *
148  * Returns: the size of the native <type>struct sockaddr</type> that
149  *     @address represents
150  *
151  * Since: 2.22
152  */
153 gssize
154 g_socket_address_get_native_size (GSocketAddress *address)
155 {
156   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
157
158   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
159 }
160
161 /**
162  * g_socket_address_to_native:
163  * @address: a #GSocketAddress
164  * @dest: a pointer to a memory location that will contain the native
165  * <type>struct sockaddr</type>.
166  * @destlen: the size of @dest. Must be at least as large as
167  * g_socket_address_get_native_size().
168  * @error: #GError for error reporting, or %NULL to ignore.
169  *
170  * Converts a #GSocketAddress to a native <type>struct
171  * sockaddr</type>, which can be passed to low-level functions like
172  * connect() or bind().
173  *
174  * If not enough space is availible, a %G_IO_ERROR_NO_SPACE error is
175  * 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  */
182 gboolean
183 g_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: a pointer to a <type>struct sockaddr</type>
196  * @len: the size of the memory location pointed to by @native
197  *
198  * Creates a #GSocketAddress subclass corresponding to the native
199  * <type>struct sockaddr</type> @native.
200  *
201  * Returns: a new #GSocketAddress if @native could successfully be converted,
202  * otherwise %NULL.
203  *
204  * Since: 2.22
205  */
206 GSocketAddress *
207 g_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 ((guint8 *) &(addr->sin_addr), AF_INET);
230       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
231       g_object_unref (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       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
245       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
246       g_object_unref (iaddr);
247       return sockaddr;
248     }
249
250 #ifdef G_OS_UNIX
251   if (family == AF_UNIX)
252     {
253       struct sockaddr_un *addr = (struct sockaddr_un *) native;
254       gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
255
256       if (path_len == 0)
257         {
258           return g_unix_socket_address_new_with_type ("", 0,
259                                                       G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
260         }
261       else if (addr->sun_path[0] == 0)
262         {
263           if (len < sizeof (*addr))
264             {
265               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
266                                                           path_len - 1,
267                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT);
268             }
269           else
270             {
271               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
272                                                           path_len - 1,
273                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
274             }
275         }
276       else
277         return g_unix_socket_address_new (addr->sun_path);
278     }
279 #endif
280
281   return NULL;
282 }
283
284
285 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
286 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
287
288 typedef struct {
289   GSocketAddressEnumerator parent_instance;
290
291   GSocketAddress *sockaddr;
292 } GSocketAddressAddressEnumerator;
293
294 typedef struct {
295   GSocketAddressEnumeratorClass parent_class;
296
297 } GSocketAddressAddressEnumeratorClass;
298
299 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
300
301 static void
302 g_socket_address_address_enumerator_finalize (GObject *object)
303 {
304   GSocketAddressAddressEnumerator *sockaddr_enum =
305     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
306
307   if (sockaddr_enum->sockaddr)
308     g_object_unref (sockaddr_enum->sockaddr);
309
310   G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
311 }
312
313 static GSocketAddress *
314 g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
315                                           GCancellable              *cancellable,
316                                           GError                   **error)
317 {
318   GSocketAddressAddressEnumerator *sockaddr_enum =
319     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
320
321   if (sockaddr_enum->sockaddr)
322     {
323       GSocketAddress *ret = sockaddr_enum->sockaddr;
324
325       sockaddr_enum->sockaddr = NULL;
326       return ret;
327     }
328   else
329     return NULL;
330 }
331
332 static void
333 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
334 {
335 }
336
337 static void
338 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
339 {
340   GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
341   GSocketAddressEnumeratorClass *enumerator_class =
342     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
343
344   enumerator_class->next = g_socket_address_address_enumerator_next;
345   object_class->finalize = g_socket_address_address_enumerator_finalize;
346 }
347
348 static GSocketAddressEnumerator *
349 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
350 {
351   GSocketAddressAddressEnumerator *sockaddr_enum;
352
353   sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
354   sockaddr_enum->sockaddr = g_object_ref (connectable);
355
356   return (GSocketAddressEnumerator *)sockaddr_enum;
357 }
358
359 static GSocketAddressEnumerator *
360 g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
361 {
362   GSocketAddressEnumerator *addr_enum = NULL;
363
364   if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
365       !G_IS_PROXY_ADDRESS (connectable))
366     {
367       GInetAddress *addr;
368       guint port;
369       gchar *uri;
370       gchar *ip;
371
372       g_object_get (connectable, "address", &addr, "port", &port, NULL);
373
374       ip = g_inet_address_to_string (addr);
375       uri = _g_uri_from_authority ("none", ip, port, NULL);
376
377       addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
378                                 "connectable", connectable,
379                                 "uri", uri,
380                                 NULL);
381
382       g_object_unref (addr);
383       g_free (ip);
384       g_free (uri);
385     }
386   else
387     {
388       addr_enum = g_socket_address_connectable_enumerate (connectable);
389     }
390
391   return addr_enum;
392 }