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 "gnetworkingprivate.h"
35 * SECTION:gunixsocketaddress
36 * @short_description: UNIX GSocketAddress
38 * Support for UNIX-domain (aka local) sockets.
44 * A UNIX-domain (local) socket address, corresponding to a
45 * <type>struct sockaddr_un</type>.
47 G_DEFINE_TYPE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS);
57 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
59 struct _GUnixSocketAddressPrivate
61 char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
62 we can guarantee zero termination of abstract
63 pathnames in the get_path() API */
64 gsize path_len; /* Not including any terminating zeros */
69 g_unix_socket_address_set_property (GObject *object,
74 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
82 str = g_value_get_string (value);
85 g_strlcpy (address->priv->path, str,
86 sizeof (address->priv->path));
87 address->priv->path_len = strlen (address->priv->path);
91 case PROP_PATH_AS_ARRAY:
92 array = g_value_get_boxed (value);
96 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
97 len = MIN (array->len, UNIX_PATH_MAX-1);
99 /* Remove any trailing zeros from path_len */
100 while (len > 0 && array->data[len-1] == 0)
103 memcpy (address->priv->path, array->data, len);
104 address->priv->path[len] = 0; /* Ensure null-terminated */
105 address->priv->path_len = len;
110 address->priv->abstract = g_value_get_boolean (value);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 g_unix_socket_address_get_property (GObject *object,
124 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
130 g_value_set_string (value, address->priv->path);
133 case PROP_PATH_AS_ARRAY:
134 array = g_byte_array_sized_new (address->priv->path_len);
135 g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
136 g_value_take_boxed (value, array);
140 g_value_set_boolean (value, address->priv->abstract);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 g_unix_socket_address_get_family (GSocketAddress *address)
151 g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
153 return G_SOCKET_FAMILY_UNIX;
157 g_unix_socket_address_get_native_size (GSocketAddress *address)
159 return sizeof (struct sockaddr_un);
163 g_unix_socket_address_to_native (GSocketAddress *address,
168 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
169 struct sockaddr_un *sock;
171 if (destlen < sizeof (*sock))
173 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
174 _("Not enough space for socket address"));
178 if (addr->priv->abstract &&
179 !g_unix_socket_address_abstract_names_supported ())
181 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
182 _("Abstract unix domain socket addresses not supported on this system"));
186 sock = (struct sockaddr_un *) dest;
187 sock->sun_family = AF_UNIX;
188 memset (sock->sun_path, 0, sizeof (sock->sun_path));
189 if (addr->priv->abstract)
191 sock->sun_path[0] = 0;
192 memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
195 strcpy (sock->sun_path, addr->priv->path);
201 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
203 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
204 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
206 g_type_class_add_private (klass, sizeof (GUnixSocketAddressPrivate));
208 gobject_class->set_property = g_unix_socket_address_set_property;
209 gobject_class->get_property = g_unix_socket_address_get_property;
211 gsocketaddress_class->get_family = g_unix_socket_address_get_family;
212 gsocketaddress_class->to_native = g_unix_socket_address_to_native;
213 gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
215 g_object_class_install_property (gobject_class,
217 g_param_spec_string ("path",
219 P_("UNIX socket path"),
222 G_PARAM_CONSTRUCT_ONLY |
223 G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
225 g_param_spec_boxed ("path-as-array",
227 P_("UNIX socket path, as byte array"),
230 G_PARAM_CONSTRUCT_ONLY |
231 G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_ABSTRACT,
233 g_param_spec_boolean ("abstract",
235 P_("Whether or not this is an abstract address"),
238 G_PARAM_CONSTRUCT_ONLY |
239 G_PARAM_STATIC_STRINGS));
243 g_unix_socket_address_init (GUnixSocketAddress *address)
245 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
246 G_TYPE_UNIX_SOCKET_ADDRESS,
247 GUnixSocketAddressPrivate);
249 memset (address->priv->path, 0, sizeof (address->priv->path));
250 address->priv->path_len = -1;
254 * g_unix_socket_address_new:
255 * @path: the socket path
257 * Creates a new #GUnixSocketAddress for @path.
259 * To create abstract socket addresses, on systems that support that,
260 * use g_unix_socket_address_new_abstract().
262 * Returns: a new #GUnixSocketAddress
267 g_unix_socket_address_new (const gchar *path)
269 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
276 * g_unix_socket_address_new_abstract:
277 * @path: the abstract name
278 * @path_len: the length of @path, or -1
280 * Creates a new abstract #GUnixSocketAddress for @path.
282 * Unix domain sockets are generally visible in the filesystem. However, some
283 * systems support abstract socket name which are not visible in the
284 * filesystem and not affected by the filesystem permissions, visibility, etc.
286 * Note that not all systems (really only Linux) support abstract
287 * socket names, so if you use them on other systems function calls may
288 * return %G_IO_ERROR_NOT_SUPPORTED errors. You can use
289 * g_unix_socket_address_abstract_names_supported() to see if abstract
290 * names are supported.
292 * If @path_len is -1 then @path is assumed to be a zero terminated
293 * string (although in general abstract names need not be zero terminated
294 * and can have embedded nuls). All bytes after @path_len up to the max size
295 * of an abstract unix domain name is filled with zero bytes.
297 * Returns: a new #GUnixSocketAddress
302 g_unix_socket_address_new_abstract (const gchar *path,
305 GSocketAddress *address;
309 path_len = strlen (path);
311 array = g_byte_array_sized_new (path_len);
313 g_byte_array_append (array, (guint8 *)path, path_len);
315 address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
316 "path-as-array", array,
320 g_byte_array_unref (array);
326 * g_unix_socket_address_get_path:
327 * @address: a #GInetSocketAddress
329 * Gets @address's path, or for abstract sockets the "name".
331 * Guaranteed to be zero-terminated, but an abstract socket
332 * may contain embedded zeros, and thus you should use
333 * g_unix_socket_address_get_path_len() to get the true length
336 * Returns: the path for @address
341 g_unix_socket_address_get_path (GUnixSocketAddress *address)
343 return address->priv->path;
347 * g_unix_socket_address_get_path_len:
348 * @address: a #GInetSocketAddress
350 * Gets the length of @address's path.
352 * For details, see g_unix_socket_address_get_path().
354 * Returns: the length of the path
359 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
361 return address->priv->path_len;
365 * g_unix_socket_address_get_is_abstract:
366 * @address: a #GInetSocketAddress
368 * Gets @address's path.
370 * Returns: %TRUE if the address is abstract, %FALSE otherwise
375 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
377 return address->priv->abstract;
381 * g_unix_socket_address_abstract_names_supported:
383 * Checks if abstract unix domain socket names are supported.
385 * Returns: %TRUE if supported, %FALSE otherwise
390 g_unix_socket_address_abstract_names_supported (void)
399 #define __G_UNIX_SOCKET_ADDRESS_C__
400 #include "gioaliasdef.c"