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