1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.
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.
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.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
28 #include "gunixsocketaddress.h"
30 #include "gnetworking.h"
34 * SECTION:gunixsocketaddress
35 * @short_description: UNIX GSocketAddress
36 * @include: gio/gunixsocketaddress.h
38 * Support for UNIX-domain (also known as local) sockets.
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.
49 * Note that <filename><gio/gunixsocketaddress.h></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.
57 * A UNIX-domain (local) socket address, corresponding to a
58 * <type>struct sockaddr_un</type>.
60 G_DEFINE_TYPE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS);
71 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
73 struct _GUnixSocketAddressPrivate
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;
83 g_unix_socket_address_set_property (GObject *object,
88 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
96 str = g_value_get_string (value);
99 g_strlcpy (address->priv->path, str,
100 sizeof (address->priv->path));
101 address->priv->path_len = strlen (address->priv->path);
105 case PROP_PATH_AS_ARRAY:
106 array = g_value_get_boxed (value);
110 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
111 len = MIN (array->len, UNIX_PATH_MAX-1);
113 memcpy (address->priv->path, array->data, len);
114 address->priv->path[len] = 0; /* Ensure null-terminated */
115 address->priv->path_len = len;
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;
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);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 g_unix_socket_address_get_property (GObject *object,
142 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
148 g_value_set_string (value, address->priv->path);
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);
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));
163 case PROP_ADDRESS_TYPE:
164 g_value_set_enum (value, address->priv->address_type);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 g_unix_socket_address_get_family (GSocketAddress *address)
175 g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
177 return G_SOCKET_FAMILY_UNIX;
181 g_unix_socket_address_get_native_size (GSocketAddress *address)
183 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
185 switch (addr->priv->address_type)
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;
192 return sizeof (struct sockaddr_un);
197 g_unix_socket_address_to_native (GSocketAddress *address,
202 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
203 struct sockaddr_un *sock;
206 socklen = g_unix_socket_address_get_native_size (address);
207 if (destlen < socklen)
209 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
210 _("Not enough space for socket address"));
214 sock = (struct sockaddr_un *) dest;
215 memset (sock, 0, socklen);
216 sock->sun_family = AF_UNIX;
218 switch (addr->priv->address_type)
220 case G_UNIX_SOCKET_ADDRESS_INVALID:
221 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
224 case G_UNIX_SOCKET_ADDRESS_PATH:
225 strcpy (sock->sun_path, addr->priv->path);
228 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
229 case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
230 if (!g_unix_socket_address_abstract_names_supported ())
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"));
237 sock->sun_path[0] = 0;
238 memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
246 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
248 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
249 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
251 g_type_class_add_private (klass, sizeof (GUnixSocketAddressPrivate));
253 gobject_class->set_property = g_unix_socket_address_set_property;
254 gobject_class->get_property = g_unix_socket_address_get_property;
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;
260 g_object_class_install_property (gobject_class,
262 g_param_spec_string ("path",
264 P_("UNIX socket path"),
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",
272 P_("UNIX socket path, as byte array"),
275 G_PARAM_CONSTRUCT_ONLY |
276 G_PARAM_STATIC_STRINGS));
278 * GUnixSocketAddress:abstract:
280 * Whether or not this is an abstract address
282 * Deprecated: Use #GUnixSocketAddress:address-type, which
283 * distinguishes between zero-padded and non-zero-padded
284 * abstract addresses.
286 g_object_class_install_property (gobject_class, PROP_ABSTRACT,
287 g_param_spec_boolean ("abstract",
289 P_("Whether or not this is an abstract address"),
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",
297 P_("The type of UNIX socket address"),
298 G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
299 G_UNIX_SOCKET_ADDRESS_PATH,
301 G_PARAM_CONSTRUCT_ONLY |
302 G_PARAM_STATIC_STRINGS));
306 g_unix_socket_address_init (GUnixSocketAddress *address)
308 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
309 G_TYPE_UNIX_SOCKET_ADDRESS,
310 GUnixSocketAddressPrivate);
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;
318 * g_unix_socket_address_new:
319 * @path: the socket path
321 * Creates a new #GUnixSocketAddress for @path.
323 * To create abstract socket addresses, on systems that support that,
324 * use g_unix_socket_address_new_abstract().
326 * Returns: a new #GUnixSocketAddress
331 g_unix_socket_address_new (const gchar *path)
333 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
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
344 * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
345 * #GUnixSocketAddress for @path.
347 * Returns: a new #GUnixSocketAddress
349 * Deprecated: Use g_unix_socket_address_new_with_type().
352 g_unix_socket_address_new_abstract (const gchar *path,
355 return g_unix_socket_address_new_with_type (path, path_len,
356 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
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
365 * Creates a new #GUnixSocketAddress of type @type with name @path.
367 * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
368 * calling g_unix_socket_address_new().
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").
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
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.
394 * Returns: a new #GUnixSocketAddress
399 g_unix_socket_address_new_with_type (const gchar *path,
401 GUnixSocketAddressType type)
403 GSocketAddress *address;
406 if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
408 else if (path_len == -1)
409 path_len = strlen (path);
411 array = g_byte_array_sized_new (path_len);
413 g_byte_array_append (array, (guint8 *)path, path_len);
415 address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
416 "path-as-array", array,
417 "address-type", type,
420 g_byte_array_unref (array);
426 * g_unix_socket_address_get_path:
427 * @address: a #GInetSocketAddress
429 * Gets @address's path, or for abstract sockets the "name".
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
436 * Returns: the path for @address
441 g_unix_socket_address_get_path (GUnixSocketAddress *address)
443 return address->priv->path;
447 * g_unix_socket_address_get_path_len:
448 * @address: a #GInetSocketAddress
450 * Gets the length of @address's path.
452 * For details, see g_unix_socket_address_get_path().
454 * Returns: the length of the path
459 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
461 return address->priv->path_len;
465 * g_unix_socket_address_get_address_type:
466 * @address: a #GInetSocketAddress
468 * Gets @address's type.
470 * Returns: a #GUnixSocketAddressType
474 GUnixSocketAddressType
475 g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
477 return address->priv->address_type;
481 * g_unix_socket_address_get_is_abstract:
482 * @address: a #GInetSocketAddress
484 * Tests if @address is abstract.
486 * Returns: %TRUE if the address is abstract, %FALSE otherwise
490 * Deprecated: Use g_unix_socket_address_get_address_type()
493 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
495 return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
496 address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
500 * g_unix_socket_address_abstract_names_supported:
502 * Checks if abstract UNIX domain socket names are supported.
504 * Returns: %TRUE if supported, %FALSE otherwise
509 g_unix_socket_address_abstract_names_supported (void)