gio/: fully remove gioalias hacks
[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
41 /**
42  * SECTION:gsocketaddress
43  * @short_description: Abstract base class representing endpoints for
44  * socket communication
45  *
46  * #GSocketAddress is the equivalent of <type>struct sockaddr</type>
47  * in the BSD sockets API. This is an abstract class; use
48  * #GInetSocketAddress for internet sockets, or #GUnixSocketAddress
49  * for UNIX domain sockets.
50  */
51
52 /**
53  * GSocketAddress:
54  *
55  * A socket endpoint address, corresponding to <type>struct sockaddr</type>
56  * or one of its subtypes.
57  */
58
59 enum
60 {
61   PROP_NONE,
62   PROP_FAMILY
63 };
64
65 static void                      g_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
66 static GSocketAddressEnumerator *g_socket_address_connectable_enumerate  (GSocketConnectable      *connectable);
67
68 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GSocketAddress, g_socket_address, G_TYPE_OBJECT,
69                                   G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
70                                                          g_socket_address_connectable_iface_init))
71
72 /**
73  * g_socket_address_get_family:
74  * @address: a #GSocketAddress
75  *
76  * Gets the socket family type of @address.
77  *
78  * Returns: the socket family type of @address.
79  *
80  * Since: 2.22
81  */
82 GSocketFamily
83 g_socket_address_get_family (GSocketAddress *address)
84 {
85   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), 0);
86
87   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_family (address);
88 }
89
90 static void
91 g_socket_address_get_property (GObject *object, guint prop_id,
92                                GValue *value, GParamSpec *pspec)
93 {
94   GSocketAddress *address = G_SOCKET_ADDRESS (object);
95
96   switch (prop_id)
97     {
98      case PROP_FAMILY:
99       g_value_set_enum (value, g_socket_address_get_family (address));
100       break;
101
102      default:
103       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104     }
105 }
106
107 static void
108 g_socket_address_class_init (GSocketAddressClass *klass)
109 {
110   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
111
112   gobject_class->get_property = g_socket_address_get_property;
113
114   g_object_class_install_property (gobject_class, PROP_FAMILY,
115                                    g_param_spec_enum ("family",
116                                                       P_("Address family"),
117                                                       P_("The family of the socket address"),
118                                                       G_TYPE_SOCKET_FAMILY,
119                                                       G_SOCKET_FAMILY_INVALID,
120                                                       G_PARAM_READABLE |
121                                                       G_PARAM_STATIC_STRINGS));
122 }
123
124 static void
125 g_socket_address_connectable_iface_init (GSocketConnectableIface *connectable_iface)
126 {
127   connectable_iface->enumerate  = g_socket_address_connectable_enumerate;
128 }
129
130 static void
131 g_socket_address_init (GSocketAddress *address)
132 {
133
134 }
135
136 /**
137  * g_socket_address_get_native_size:
138  * @address: a #GSocketAddress
139  *
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().
143  *
144  * Returns: the size of the native <type>struct sockaddr</type> that
145  *     @address represents
146  *
147  * Since: 2.22
148  */
149 gssize
150 g_socket_address_get_native_size (GSocketAddress *address)
151 {
152   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
153
154   return G_SOCKET_ADDRESS_GET_CLASS (address)->get_native_size (address);
155 }
156
157 /**
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.
165  *
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().
169  *
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.
173  *
174  * Returns: %TRUE if @dest was filled in, %FALSE on error
175  *
176  * Since: 2.22
177  */
178 gboolean
179 g_socket_address_to_native (GSocketAddress  *address,
180                             gpointer         dest,
181                             gsize            destlen,
182                             GError         **error)
183 {
184   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
185
186   return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
187 }
188
189 /**
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
193  *
194  * Creates a #GSocketAddress subclass corresponding to the native
195  * <type>struct sockaddr</type> @native.
196  *
197  * Returns: a new #GSocketAddress if @native could successfully be converted,
198  * otherwise %NULL.
199  *
200  * Since: 2.22
201  */
202 GSocketAddress *
203 g_socket_address_new_from_native (gpointer native,
204                                   gsize    len)
205 {
206   gshort family;
207
208   if (len < sizeof (gshort))
209     return NULL;
210
211   family = ((struct sockaddr *) native)->sa_family;
212
213   if (family == AF_UNSPEC)
214     return NULL;
215
216   if (family == AF_INET)
217     {
218       struct sockaddr_in *addr = (struct sockaddr_in *) native;
219       GInetAddress *iaddr;
220       GSocketAddress *sockaddr;
221
222       if (len < sizeof (*addr))
223         return NULL;
224
225       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
226       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
227       g_object_unref (iaddr);
228       return sockaddr;
229     }
230
231   if (family == AF_INET6)
232     {
233       struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
234       GInetAddress *iaddr;
235       GSocketAddress *sockaddr;
236
237       if (len < sizeof (*addr))
238         return NULL;
239
240       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
241       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
242       g_object_unref (iaddr);
243       return sockaddr;
244     }
245
246 #ifdef G_OS_UNIX
247   if (family == AF_UNIX)
248     {
249       struct sockaddr_un *addr = (struct sockaddr_un *) native;
250       gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
251
252       if (path_len == 0)
253         {
254           return g_unix_socket_address_new_with_type ("", 0,
255                                                       G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
256         }
257       else if (addr->sun_path[0] == 0)
258         {
259           if (len < sizeof (*addr))
260             {
261               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
262                                                           path_len - 1,
263                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT);
264             }
265           else
266             {
267               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
268                                                           path_len - 1,
269                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
270             }
271         }
272       else
273         return g_unix_socket_address_new (addr->sun_path);
274     }
275 #endif
276
277   return NULL;
278 }
279
280
281 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
282 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
283
284 typedef struct {
285   GSocketAddressEnumerator parent_instance;
286
287   GSocketAddress *sockaddr;
288 } GSocketAddressAddressEnumerator;
289
290 typedef struct {
291   GSocketAddressEnumeratorClass parent_class;
292
293 } GSocketAddressAddressEnumeratorClass;
294
295 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
296
297 static void
298 g_socket_address_address_enumerator_finalize (GObject *object)
299 {
300   GSocketAddressAddressEnumerator *sockaddr_enum =
301     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
302
303   if (sockaddr_enum->sockaddr)
304     g_object_unref (sockaddr_enum->sockaddr);
305
306   G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
307 }
308
309 static GSocketAddress *
310 g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
311                                           GCancellable              *cancellable,
312                                           GError                   **error)
313 {
314   GSocketAddressAddressEnumerator *sockaddr_enum =
315     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
316
317   if (sockaddr_enum->sockaddr)
318     {
319       GSocketAddress *ret = sockaddr_enum->sockaddr;
320
321       sockaddr_enum->sockaddr = NULL;
322       return ret;
323     }
324   else
325     return NULL;
326 }
327
328 static void
329 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
330 {
331 }
332
333 static void
334 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
335 {
336   GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
337   GSocketAddressEnumeratorClass *enumerator_class =
338     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
339
340   enumerator_class->next = g_socket_address_address_enumerator_next;
341   object_class->finalize = g_socket_address_address_enumerator_finalize;
342 }
343
344 static GSocketAddressEnumerator *
345 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
346 {
347   GSocketAddressAddressEnumerator *sockaddr_enum;
348
349   sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
350   sockaddr_enum->sockaddr = g_object_ref (connectable);
351
352   return (GSocketAddressEnumerator *)sockaddr_enum;
353 }