[kdbus] Fix freeing allocated memory
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors: Christian Kellner <gicmo@gnome.org>
19  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
20  */
21
22 #include <config.h>
23 #include <glib.h>
24 #include <string.h>
25
26 #include "gsocketaddress.h"
27 #include "ginetaddress.h"
28 #include "ginetsocketaddress.h"
29 #include "gnetworkingprivate.h"
30 #include "gproxyaddress.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
45  *     for socket communication
46  * @include: gio/gio.h
47  *
48  * #GSocketAddress is the equivalent of struct sockaddr in the BSD
49  * sockets API. This is an abstract class; use #GInetSocketAddress
50  * for internet sockets, or #GUnixSocketAddress for UNIX domain sockets.
51  */
52
53 /**
54  * GSocketAddress:
55  *
56  * A socket endpoint address, corresponding to struct sockaddr
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 struct sockaddr.
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 struct sockaddr 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  * struct sockaddr
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 struct sockaddr, which can
170  * be passed to low-level functions like connect() or bind().
171  *
172  * If not enough space is available, a %G_IO_ERROR_NO_SPACE error
173  * is returned. If the address type is not known on the system
174  * then a %G_IO_ERROR_NOT_SUPPORTED error is returned.
175  *
176  * Returns: %TRUE if @dest was filled in, %FALSE on error
177  *
178  * Since: 2.22
179  */
180 gboolean
181 g_socket_address_to_native (GSocketAddress  *address,
182                             gpointer         dest,
183                             gsize            destlen,
184                             GError         **error)
185 {
186   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
187
188   return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest, destlen, error);
189 }
190
191 /**
192  * g_socket_address_new_from_native:
193  * @native: a pointer to a struct sockaddr
194  * @len: the size of the memory location pointed to by @native
195  *
196  * Creates a #GSocketAddress subclass corresponding to the native
197  * struct sockaddr @native.
198  *
199  * Returns: a new #GSocketAddress if @native could successfully
200  *     be converted, otherwise %NULL
201  *
202  * Since: 2.22
203  */
204 GSocketAddress *
205 g_socket_address_new_from_native (gpointer native,
206                                   gsize    len)
207 {
208   gshort family;
209
210   if (len < sizeof (gshort))
211     return NULL;
212
213   family = ((struct sockaddr *) native)->sa_family;
214
215   if (family == AF_UNSPEC)
216     return NULL;
217
218   if (family == AF_INET)
219     {
220       struct sockaddr_in *addr = (struct sockaddr_in *) native;
221       GInetAddress *iaddr;
222       GSocketAddress *sockaddr;
223
224       if (len < sizeof (*addr))
225         return NULL;
226
227       iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
228       sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
229       g_object_unref (iaddr);
230       return sockaddr;
231     }
232
233   if (family == AF_INET6)
234     {
235       struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
236       GInetAddress *iaddr;
237       GSocketAddress *sockaddr;
238
239       if (len < sizeof (*addr))
240         return NULL;
241
242       if (IN6_IS_ADDR_V4MAPPED (&(addr->sin6_addr)))
243         {
244           struct sockaddr_in sin_addr;
245
246           sin_addr.sin_family = AF_INET;
247           sin_addr.sin_port = addr->sin6_port;
248           memcpy (&(sin_addr.sin_addr.s_addr), addr->sin6_addr.s6_addr + 12, 4);
249           iaddr = g_inet_address_new_from_bytes ((guint8 *) &(sin_addr.sin_addr), AF_INET);
250         }
251       else
252         {
253           iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
254         }
255
256       sockaddr = g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
257                                "address", iaddr,
258                                "port", g_ntohs (addr->sin6_port),
259                                "flowinfo", addr->sin6_flowinfo,
260                                "scope_id", addr->sin6_scope_id,
261                                NULL);
262       g_object_unref (iaddr);
263       return sockaddr;
264     }
265
266 #ifdef G_OS_UNIX
267   if (family == AF_UNIX)
268     {
269       struct sockaddr_un *addr = (struct sockaddr_un *) native;
270       gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
271
272       if (path_len == 0)
273         {
274           return g_unix_socket_address_new_with_type ("", 0,
275                                                       G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
276         }
277       else if (addr->sun_path[0] == 0)
278         {
279           if (!g_unix_socket_address_abstract_names_supported ())
280             {
281               return g_unix_socket_address_new_with_type ("", 0,
282                                                           G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
283             }
284           else if (len < sizeof (*addr))
285             {
286               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
287                                                           path_len - 1,
288                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT);
289             }
290           else
291             {
292               return g_unix_socket_address_new_with_type (addr->sun_path + 1,
293                                                           path_len - 1,
294                                                           G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
295             }
296         }
297       else
298         return g_unix_socket_address_new (addr->sun_path);
299     }
300 #endif
301
302   return NULL;
303 }
304
305
306 #define G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (_g_socket_address_address_enumerator_get_type ())
307 #define G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, GSocketAddressAddressEnumerator))
308
309 typedef struct {
310   GSocketAddressEnumerator parent_instance;
311
312   GSocketAddress *sockaddr;
313 } GSocketAddressAddressEnumerator;
314
315 typedef struct {
316   GSocketAddressEnumeratorClass parent_class;
317
318 } GSocketAddressAddressEnumeratorClass;
319
320 static GType _g_socket_address_address_enumerator_get_type (void);
321 G_DEFINE_TYPE (GSocketAddressAddressEnumerator, _g_socket_address_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
322
323 static void
324 g_socket_address_address_enumerator_finalize (GObject *object)
325 {
326   GSocketAddressAddressEnumerator *sockaddr_enum =
327     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (object);
328
329   if (sockaddr_enum->sockaddr)
330     g_object_unref (sockaddr_enum->sockaddr);
331
332   G_OBJECT_CLASS (_g_socket_address_address_enumerator_parent_class)->finalize (object);
333 }
334
335 static GSocketAddress *
336 g_socket_address_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
337                                           GCancellable              *cancellable,
338                                           GError                   **error)
339 {
340   GSocketAddressAddressEnumerator *sockaddr_enum =
341     G_SOCKET_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
342
343   if (sockaddr_enum->sockaddr)
344     {
345       GSocketAddress *ret = sockaddr_enum->sockaddr;
346
347       sockaddr_enum->sockaddr = NULL;
348       return ret;
349     }
350   else
351     return NULL;
352 }
353
354 static void
355 _g_socket_address_address_enumerator_init (GSocketAddressAddressEnumerator *enumerator)
356 {
357 }
358
359 static void
360 _g_socket_address_address_enumerator_class_init (GSocketAddressAddressEnumeratorClass *sockaddrenum_class)
361 {
362   GObjectClass *object_class = G_OBJECT_CLASS (sockaddrenum_class);
363   GSocketAddressEnumeratorClass *enumerator_class =
364     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (sockaddrenum_class);
365
366   enumerator_class->next = g_socket_address_address_enumerator_next;
367   object_class->finalize = g_socket_address_address_enumerator_finalize;
368 }
369
370 static GSocketAddressEnumerator *
371 g_socket_address_connectable_enumerate (GSocketConnectable *connectable)
372 {
373   GSocketAddressAddressEnumerator *sockaddr_enum;
374
375   sockaddr_enum = g_object_new (G_TYPE_SOCKET_ADDRESS_ADDRESS_ENUMERATOR, NULL);
376   sockaddr_enum->sockaddr = g_object_ref (connectable);
377
378   return (GSocketAddressEnumerator *)sockaddr_enum;
379 }
380
381 static GSocketAddressEnumerator *
382 g_socket_address_connectable_proxy_enumerate (GSocketConnectable *connectable)
383 {
384   GSocketAddressEnumerator *addr_enum = NULL;
385
386   g_assert (connectable != NULL);
387
388   if (G_IS_INET_SOCKET_ADDRESS (connectable) &&
389       !G_IS_PROXY_ADDRESS (connectable))
390     {
391       GInetAddress *addr;
392       guint port;
393       gchar *uri;
394       gchar *ip;
395
396       g_object_get (connectable, "address", &addr, "port", &port, NULL);
397
398       ip = g_inet_address_to_string (addr);
399       uri = _g_uri_from_authority ("none", ip, port, NULL);
400
401       addr_enum = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
402                                 "connectable", connectable,
403                                 "uri", uri,
404                                 NULL);
405
406       g_object_unref (addr);
407       g_free (ip);
408       g_free (uri);
409     }
410   else
411     {
412       addr_enum = g_socket_address_connectable_enumerate (connectable);
413     }
414
415   return addr_enum;
416 }