mem-overflow: test malloc and realloc corner cases
[platform/upstream/glib.git] / gio / gunixsocketaddress.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 "gunixsocketaddress.h"
29 #include "glibintl.h"
30 #include "gnetworking.h"
31
32
33 /**
34  * SECTION:gunixsocketaddress
35  * @short_description: UNIX GSocketAddress
36  * @include: gio/gunixsocketaddress.h
37  *
38  * Support for UNIX-domain (also known as local) sockets.
39  *
40  * UNIX domain sockets are generally visible in the filesystem.
41  * However, some systems support abstract socket names which are not
42  * visible in the filesystem and not affected by the filesystem
43  * permissions, visibility, etc. Currently this is only supported
44  * under Linux. If you attempt to use abstract sockets on other
45  * systems, function calls may return %G_IO_ERROR_NOT_SUPPORTED
46  * errors. You can use g_unix_socket_address_abstract_names_supported()
47  * to see if abstract names are supported.
48  *
49  * Note that <filename>&lt;gio/gunixsocketaddress.h&gt;</filename> belongs to
50  * the UNIX-specific GIO interfaces, thus you have to use the
51  * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
52  */
53
54 /**
55  * GUnixSocketAddress:
56  *
57  * A UNIX-domain (local) socket address, corresponding to a
58  * <type>struct sockaddr_un</type>.
59  */
60 G_DEFINE_TYPE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS);
61
62 enum
63 {
64   PROP_0,
65   PROP_PATH,
66   PROP_PATH_AS_ARRAY,
67   PROP_ABSTRACT,
68   PROP_ADDRESS_TYPE
69 };
70
71 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
72
73 struct _GUnixSocketAddressPrivate
74 {
75   char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
76                                we can guarantee zero termination of abstract
77                                pathnames in the get_path() API */
78   gsize path_len; /* Not including any terminating zeros */
79   GUnixSocketAddressType address_type;
80 };
81
82 static void
83 g_unix_socket_address_set_property (GObject      *object,
84                                     guint         prop_id,
85                                     const GValue *value,
86                                     GParamSpec   *pspec)
87 {
88   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
89   const char *str;
90   GByteArray *array;
91   gsize len;
92
93   switch (prop_id)
94     {
95     case PROP_PATH:
96       str = g_value_get_string (value);
97       if (str)
98         {
99           g_strlcpy (address->priv->path, str,
100                      sizeof (address->priv->path));
101           address->priv->path_len = strlen (address->priv->path);
102         }
103       break;
104
105     case PROP_PATH_AS_ARRAY:
106       array = g_value_get_boxed (value);
107
108       if (array)
109         {
110           /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
111           len = MIN (array->len, UNIX_PATH_MAX-1);
112
113           memcpy (address->priv->path, array->data, len);
114           address->priv->path[len] = 0; /* Ensure null-terminated */
115           address->priv->path_len = len;
116         }
117       break;
118
119     case PROP_ABSTRACT:
120       /* Only set it if it's not the default... */
121       if (g_value_get_boolean (value))
122        address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
123       break;
124
125     case PROP_ADDRESS_TYPE:
126       /* Only set it if it's not the default... */
127       if (g_value_get_enum (value) != G_UNIX_SOCKET_ADDRESS_PATH)
128         address->priv->address_type = g_value_get_enum (value);
129       break;
130
131     default:
132       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133     }
134 }
135
136 static void
137 g_unix_socket_address_get_property (GObject    *object,
138                                     guint       prop_id,
139                                     GValue     *value,
140                                     GParamSpec *pspec)
141 {
142   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
143   GByteArray *array;
144
145   switch (prop_id)
146     {
147       case PROP_PATH:
148         g_value_set_string (value, address->priv->path);
149         break;
150
151       case PROP_PATH_AS_ARRAY:
152         array = g_byte_array_sized_new (address->priv->path_len);
153         g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
154         g_value_take_boxed (value, array);
155         break;
156
157       case PROP_ABSTRACT:
158         g_value_set_boolean (value, (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
159                                      address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED));
160
161         break;
162
163       case PROP_ADDRESS_TYPE:
164         g_value_set_enum (value, address->priv->address_type);
165         break;
166
167       default:
168         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169     }
170 }
171
172 static GSocketFamily
173 g_unix_socket_address_get_family (GSocketAddress *address)
174 {
175   g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
176
177   return G_SOCKET_FAMILY_UNIX;
178 }
179
180 static gssize
181 g_unix_socket_address_get_native_size (GSocketAddress *address)
182 {
183   GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
184
185   switch (addr->priv->address_type)
186     {
187     case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
188       return G_STRUCT_OFFSET(struct sockaddr_un, sun_path);
189     case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
190       return G_STRUCT_OFFSET(struct sockaddr_un, sun_path) + addr->priv->path_len + 1;
191     default:
192       return sizeof (struct sockaddr_un);
193     }
194 }
195
196 static gboolean
197 g_unix_socket_address_to_native (GSocketAddress *address,
198                                  gpointer        dest,
199                                  gsize           destlen,
200                                  GError        **error)
201 {
202   GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
203   struct sockaddr_un *sock;
204   gssize socklen;
205
206   socklen = g_unix_socket_address_get_native_size (address);
207   if (destlen < socklen)
208     {
209       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
210                            _("Not enough space for socket address"));
211       return FALSE;
212     }
213
214   sock = (struct sockaddr_un *) dest;
215   memset (sock, 0, socklen);
216   sock->sun_family = AF_UNIX;
217
218   switch (addr->priv->address_type)
219     {
220     case G_UNIX_SOCKET_ADDRESS_INVALID:
221     case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
222       break;
223
224     case G_UNIX_SOCKET_ADDRESS_PATH:
225       strcpy (sock->sun_path, addr->priv->path);
226       break;
227
228     case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
229     case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
230       if (!g_unix_socket_address_abstract_names_supported ())
231         {
232           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
233                                _("Abstract UNIX domain socket addresses not supported on this system"));
234           return FALSE;
235         }
236
237       sock->sun_path[0] = 0;
238       memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
239       break;
240     }
241
242   return TRUE;
243 }
244
245 static void
246 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
247 {
248   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
249   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
250
251   g_type_class_add_private (klass, sizeof (GUnixSocketAddressPrivate));
252
253   gobject_class->set_property = g_unix_socket_address_set_property;
254   gobject_class->get_property = g_unix_socket_address_get_property;
255
256   gsocketaddress_class->get_family = g_unix_socket_address_get_family;
257   gsocketaddress_class->to_native = g_unix_socket_address_to_native;
258   gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
259
260   g_object_class_install_property (gobject_class,
261                                    PROP_PATH,
262                                    g_param_spec_string ("path",
263                                                         P_("Path"),
264                                                         P_("UNIX socket path"),
265                                                         NULL,
266                                                         G_PARAM_READWRITE |
267                                                         G_PARAM_CONSTRUCT_ONLY |
268                                                         G_PARAM_STATIC_STRINGS));
269   g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
270                                    g_param_spec_boxed ("path-as-array",
271                                                        P_("Path array"),
272                                                        P_("UNIX socket path, as byte array"),
273                                                        G_TYPE_BYTE_ARRAY,
274                                                        G_PARAM_READWRITE |
275                                                        G_PARAM_CONSTRUCT_ONLY |
276                                                        G_PARAM_STATIC_STRINGS));
277   /**
278    * GUnixSocketAddress:abstract:
279    *
280    * Whether or not this is an abstract address
281    *
282    * Deprecated: Use #GUnixSocketAddress:address-type, which
283    * distinguishes between zero-padded and non-zero-padded
284    * abstract addresses.
285    */
286   g_object_class_install_property (gobject_class, PROP_ABSTRACT,
287                                    g_param_spec_boolean ("abstract",
288                                                          P_("Abstract"),
289                                                          P_("Whether or not this is an abstract address"),
290                                                          FALSE,
291                                                          G_PARAM_READWRITE |
292                                                          G_PARAM_CONSTRUCT_ONLY |
293                                                          G_PARAM_STATIC_STRINGS));
294   g_object_class_install_property (gobject_class, PROP_ADDRESS_TYPE,
295                                    g_param_spec_enum ("address-type",
296                                                       P_("Address type"),
297                                                       P_("The type of UNIX socket address"),
298                                                       G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
299                                                       G_UNIX_SOCKET_ADDRESS_PATH,
300                                                       G_PARAM_READWRITE |
301                                                       G_PARAM_CONSTRUCT_ONLY |
302                                                       G_PARAM_STATIC_STRINGS));
303 }
304
305 static void
306 g_unix_socket_address_init (GUnixSocketAddress *address)
307 {
308   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
309                                                G_TYPE_UNIX_SOCKET_ADDRESS,
310                                                GUnixSocketAddressPrivate);
311
312   memset (address->priv->path, 0, sizeof (address->priv->path));
313   address->priv->path_len = -1;
314   address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
315 }
316
317 /**
318  * g_unix_socket_address_new:
319  * @path: the socket path
320  *
321  * Creates a new #GUnixSocketAddress for @path.
322  *
323  * To create abstract socket addresses, on systems that support that,
324  * use g_unix_socket_address_new_abstract().
325  *
326  * Returns: a new #GUnixSocketAddress
327  *
328  * Since: 2.22
329  */
330 GSocketAddress *
331 g_unix_socket_address_new (const gchar *path)
332 {
333   return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
334                        "path", path,
335                        "abstract", FALSE,
336                        NULL);
337 }
338
339 /**
340  * g_unix_socket_address_new_abstract:
341  * @path: (array length=path_len) (element-type gchar): the abstract name
342  * @path_len: the length of @path, or -1
343  *
344  * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
345  * #GUnixSocketAddress for @path.
346  *
347  * Returns: a new #GUnixSocketAddress
348  *
349  * Deprecated: Use g_unix_socket_address_new_with_type().
350  */
351 GSocketAddress *
352 g_unix_socket_address_new_abstract (const gchar *path,
353                                     gint         path_len)
354 {
355   return g_unix_socket_address_new_with_type (path, path_len,
356                                               G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
357 }
358
359 /**
360  * g_unix_socket_address_new_with_type:
361  * @path: (array length=path_len) (element-type gchar): the name
362  * @path_len: the length of @path, or -1
363  * @type: a #GUnixSocketAddressType
364  *
365  * Creates a new #GUnixSocketAddress of type @type with name @path.
366  *
367  * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
368  * calling g_unix_socket_address_new().
369  *
370  * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
371  * bytes of @path will be copied to the socket's path, and only those
372  * bytes will be considered part of the name. (If @path_len is -1,
373  * then @path is assumed to be NUL-terminated.) For example, if @path
374  * was "test", then calling g_socket_address_get_native_size() on the
375  * returned socket would return 7 (2 bytes of overhead, 1 byte for the
376  * abstract-socket indicator byte, and 4 bytes for the name "test").
377  *
378  * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
379  * @path_len bytes of @path will be copied to the socket's path, the
380  * rest of the path will be padded with 0 bytes, and the entire
381  * zero-padded buffer will be considered the name. (As above, if
382  * @path_len is -1, then @path is assumed to be NUL-terminated.) In
383  * this case, g_socket_address_get_native_size() will always return
384  * the full size of a <literal>struct sockaddr_un</literal>, although
385  * g_unix_socket_address_get_path_len() will still return just the
386  * length of @path.
387  *
388  * %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
389  * %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
390  * when connecting to a server created by another process, you must
391  * use the appropriate type corresponding to how that process created
392  * its listening socket.
393  *
394  * Returns: a new #GUnixSocketAddress
395  *
396  * Since: 2.26
397  */
398 GSocketAddress *
399 g_unix_socket_address_new_with_type (const gchar            *path,
400                                      gint                    path_len,
401                                      GUnixSocketAddressType  type)
402 {
403   GSocketAddress *address;
404   GByteArray *array;
405
406   if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
407     path_len = 0;
408   else if (path_len == -1)
409     path_len = strlen (path);
410
411   array = g_byte_array_sized_new (path_len);
412
413   g_byte_array_append (array, (guint8 *)path, path_len);
414
415   address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
416                           "path-as-array", array,
417                           "address-type", type,
418                           NULL);
419
420   g_byte_array_unref (array);
421
422   return address;
423 }
424
425 /**
426  * g_unix_socket_address_get_path:
427  * @address: a #GInetSocketAddress
428  *
429  * Gets @address's path, or for abstract sockets the "name".
430  *
431  * Guaranteed to be zero-terminated, but an abstract socket
432  * may contain embedded zeros, and thus you should use
433  * g_unix_socket_address_get_path_len() to get the true length
434  * of this string.
435  *
436  * Returns: the path for @address
437  *
438  * Since: 2.22
439  */
440 const char *
441 g_unix_socket_address_get_path (GUnixSocketAddress *address)
442 {
443   return address->priv->path;
444 }
445
446 /**
447  * g_unix_socket_address_get_path_len:
448  * @address: a #GInetSocketAddress
449  *
450  * Gets the length of @address's path.
451  *
452  * For details, see g_unix_socket_address_get_path().
453  *
454  * Returns: the length of the path
455  *
456  * Since: 2.22
457  */
458 gsize
459 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
460 {
461   return address->priv->path_len;
462 }
463
464 /**
465  * g_unix_socket_address_get_address_type:
466  * @address: a #GInetSocketAddress
467  *
468  * Gets @address's type.
469  *
470  * Returns: a #GUnixSocketAddressType
471  *
472  * Since: 2.26
473  */
474 GUnixSocketAddressType
475 g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
476 {
477   return address->priv->address_type;
478 }
479
480 /**
481  * g_unix_socket_address_get_is_abstract:
482  * @address: a #GInetSocketAddress
483  *
484  * Tests if @address is abstract.
485  *
486  * Returns: %TRUE if the address is abstract, %FALSE otherwise
487  *
488  * Since: 2.22
489  *
490  * Deprecated: Use g_unix_socket_address_get_address_type()
491  */
492 gboolean
493 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
494 {
495   return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
496           address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
497 }
498
499 /**
500  * g_unix_socket_address_abstract_names_supported:
501  *
502  * Checks if abstract UNIX domain socket names are supported.
503  *
504  * Returns: %TRUE if supported, %FALSE otherwise
505  *
506  * Since: 2.22
507  */
508 gboolean
509 g_unix_socket_address_abstract_names_supported (void)
510 {
511 #ifdef __linux__
512   return TRUE;
513 #else
514   return FALSE;
515 #endif
516 }