Implemented proxy_enumerate() for all Connectables
[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 "gproxyaddressenumerator.h"
32 #include "gsocketaddressenumerator.h"
33 #include "gsocketconnectable.h"
34 #include "glibintl.h"
35 #include "gioenumtypes.h"
36
37 #ifdef G_OS_UNIX
38 #include "gunixsocketaddress.h"
39 #endif
40
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 static GSocketAddressEnumerator *g_socket_address_connectable_proxy_enumerate  (GSocketConnectable      *connectable);
69
70 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
71                                   G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
72                                                          g_socket_address_connectable_iface_init))
73
74 /**
75  * g_socket_address_get_family:
76  * @address: a #GSocketAddress
77  *
78  * Gets the socket family type of @address.
79  *
80  * Returns: the socket family type of @address.
81  *
82  * Since: 2.22
83  */
84 GSocketFamily
85 g_socket_address_get_family (GSocketAddress *address)
86 {
87   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
88
89   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
90 }
91
92 static void
93 g_socket_address_get_property (GObject *object, guint prop_id,
94                                GValue *value, GParamSpec *pspec)
95 {
96   GSocketAddress *address = G_SOCKET_ADDRESS (object);
97
98   switch (prop_id)
99     {
100      case PROP_FAMILY:
101       g_value_set_enum (value, g_socket_address_get_family (address));
102       break;
103
104      default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106     }
107 }
108
109 static void
110 g_socket_address_class_init (GSocketAddressClass *klass)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113
114   gobject_class->get_property = g_socket_address_get_property;
115
116   g_object_class_install_property (gobject_class, PROP_FAMILY,
117                                    g_param_spec_enum ("family",
118                                                       P_("Address family"),
119                                                       P_("The family of the socket address"),
120                                                       G_TYPE_SOCKET_FAMILY,
121                                                       G_SOCKET_FAMILY_INVALID,
122                                                       G_PARAM_READABLE |
123                                                       G_PARAM_STATIC_STRINGS));
124 }
125
126 static void
127 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
128 {
129   connectable_iface->enumerate  = g_socket_address_connectable_enumerate;
130   connectable_iface->proxy_enumerate  = g_socket_address_connectable_proxy_enumerate;
131 }
132
133 static void
134 g_socket_address_init (GSocketAddress *address)
135 {
136
137 }
138
139 /**
140  * g_socket_address_get_native_size:
141  * @address: a #GSocketAddress
142  *
143  * Gets the size of @address's native <type>struct sockaddr</type>.
144  * You can use this to allocate memory to pass to
145  * g_socket_address_to_native().
146  *
147  * Returns: the size of the native <type>struct sockaddr</type> that
148  *     @address represents
149  *
150  * Since: 2.22
151  */
152 gssize
153 g_socket_address_get_native_size (GSocketAddress *address)
154 {
155   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
156
157   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
158 }
159
160 /**
161  * g_socket_address_to_native:
162  * @address: a #GSocketAddress
163  * @dest: a pointer to a memory location that will contain the native
164  * <type>struct sockaddr</type>.
165  * @destlen: the size of @dest. Must be at least as large as
166  * g_socket_address_get_native_size().
167  * @error: #GError for error reporting, or %NULL to ignore.
168  *
169  * Converts a #GSocketAddress to a native <type>struct
170  * sockaddr</type>, which can be passed to low-level functions like
171  * connect() or bind().
172  *
173  * If not enough space is availible, a %G_IO_ERROR_NO_SPACE error is
174  * returned. If the address type is not known on the system
175  * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
176  *
177  * Returns: %TRUE if @dest was filled in, %FALSE on error
178  *
179  * Since: 2.22
180  */
181 gboolean
182 g_socket_address_to_native (GSocketAddress  *address,
183                             gpointer         dest,
184                             gsize            destlen,
185                             GError         **error)
186 {
187   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
188
189   return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
190 }
191
192 /**
193  * g_socket_address_new_from_native:
194  * @native: a pointer to a <type>struct sockaddr</type>
195  * @len: the size of the memory location pointed to by @native
196  *
197  * Creates a #GSocketAddress subclass corresponding to the native
198  * <type>struct sockaddr</type> @native.
199  *
200  * Returns: a new #GSocketAddress if @native could successfully be converted,
201  * otherwise %NULL.
202  *
203  * Since: 2.22
204  */
205 GSocketAddress *
206 g_socket_address_new_from_native (gpointer native,
207                                   gsize    len)
208 {
209   gshort family;
210
211   if (len < sizeof (gshort))
212     return NULL;
213
214   family = ((struct sockaddr *) native)->sa_family;
215
216   if (family == AF_UNSPEC)
217     return NULL;
218
219   if (family == AF_INET)
220     {
221       struct sockaddr_in *addr = (struct sockaddr_in *) native;
222       GInetAddress *iaddr;
223       GSocketAddress *sockaddr;
224
225       if (len < sizeof (*addr))
226         return NULL;
227
228       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
229       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
230       g_object_unref (iaddr);
231       return sockaddr;
232     }
233
234   if (family == AF_INET6)
235     {
236       struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
237       GInetAddress *iaddr;
238       GSocketAddress *sockaddr;
239
240       if (len < sizeof (*addr))
241         return NULL;
242
243       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
244       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
245       g_object_unref (iaddr);
246       return sockaddr;
247     }
248
249 #ifdef G_OS_UNIX
250   if (family == AF_UNIX)
251     {
252       struct sockaddr_un *addr = (struct sockaddr_un *) native;
253       gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
254
255       if (path_len == 0)
256         {
257           return g_unix_socket_address_new_with_type ("", 0,
258                                                       G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
259         }
260       else if (addr->sun_path[0] == 0)
261         {
262           if (len < sizeof (*addr))
263             {
264               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
265                                                           path_len - 1,
266                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT);
267             }
268           else
269             {
270               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
271                                                           path_len - 1,
272                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
273             }
274         }
275       else
276         return g_unix_socket_address_new (addr->sun_path);
277     }
278 #endif
279
280   return NULL;
281 }
282
283
284 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
285 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
286
287 typedef struct {
288   GSocketAddressEnumerator parent_instance;
289
290   GSocketAddress *sockaddr;
291 } GSocketAddressAddressEnumerator;
292
293 typedef struct {
294   GSocketAddressEnumeratorClass parent_class;
295
296 } GSocketAddressAddressEnumeratorClass;
297
298 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
299
300 static void
301 g_socket_address_address_enumerator_finalize (GObject *object)
302 {
303   GSocketAddressAddressEnumerator *sockaddr_enum =
304     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
305
306   if (sockaddr_enum->sockaddr)
307     g_object_unref (sockaddr_enum->sockaddr);
308
309   G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
310 }
311
312 static GSocketAddress *
313 g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
314                                           GCancellable              *cancellable,
315                                           GError                   **error)
316 {
317   GSocketAddressAddressEnumerator *sockaddr_enum =
318     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
319
320   if (sockaddr_enum->sockaddr)
321     {
322       GSocketAddress *ret = sockaddr_enum->sockaddr;
323
324       sockaddr_enum->sockaddr = NULL;
325       return ret;
326     }
327   else
328     return NULL;
329 }
330
331 static void
332 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
333 {
334 }
335
336 static void
337 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
338 {
339   GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
340   GSocketAddressEnumeratorClass *enumerator_class =
341     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
342
343   enumerator_class->next = g_socket_address_address_enumerator_next;
344   object_class->finalize = g_socket_address_address_enumerator_finalize;
345 }
346
347 static GSocketAddressEnumerator *
348 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
349 {
350   GSocketAddressAddressEnumerator *sockaddr_enum;
351
352   sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
353   sockaddr_enum->sockaddr = g_object_ref (connectable);
354
355   return (GSocketAddressEnumerator *)sockaddr_enum;
356 }
357
358 static GSocketAddressEnumerator *
359 g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
360 {
361   GSocketAddressEnumerator *addr_enum = NULL;
362
363   if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
364       !G_IS_PROXY_ADDRESS (connectable))
365     {
366       GInetAddress *addr;
367       guint port;
368       gchar *uri;
369       gchar *ip;
370
371       g_object_get (connectable, "address", &addr, "port", &port, NULL);
372
373       ip = g_inet_address_to_string (addr);
374       uri = g_strdup_printf ("none://%s:%u", ip, port);
375
376       addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
377                                 "connectable", connectable,
378                                 "uri", uri,
379                                 NULL);
380
381       g_object_unref (addr);
382       g_free (ip);
383       g_free (uri);
384     }
385   else
386     {
387       addr_enum = g_socket_address_connectable_enumerate (connectable);
388     }
389
390   return addr_enum;
391 }