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