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>
27 #include <sys/socket.h>
30 #include "gunixsocketaddress.h"
32 #include "gnetworkingprivate.h"
37 * SECTION:gunixsocketaddress
38 * @short_description: Unix #GSocketAddress
40 * Support for UNIX-domain (aka local) sockets.
46 * A UNIX-domain (local) socket address, corresponding to a
47 * <type>struct sockaddr_un</type>.
49 G_DEFINE_TYPE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS);
59 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
61 struct _GUnixSocketAddressPrivate
63 char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
64 we can guarantee zero termination of abstract
65 pathnames in the get_path() API */
66 gsize path_len; /* Not including any terminating zeros */
71 g_unix_socket_address_set_property (GObject *object,
76 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
84 str = g_value_get_string (value);
87 g_strlcpy (address->priv->path, str,
88 sizeof (address->priv->path));
89 address->priv->path_len = strlen (address->priv->path);
93 case PROP_PATH_AS_ARRAY:
94 array = g_value_get_boxed (value);
98 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
99 len = MIN (array->len, UNIX_PATH_MAX-1);
101 /* Remove any trailing zeros from path_len */
102 while (len > 0 && array->data[len-1] == 0)
105 memcpy (address->priv->path, array->data, len);
106 address->priv->path[len] = 0; /* Ensure null-terminated */
107 address->priv->path_len = len;
112 address->priv->abstract = g_value_get_boolean (value);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 g_unix_socket_address_get_property (GObject *object,
126 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
132 g_value_set_string (value, address->priv->path);
135 case PROP_PATH_AS_ARRAY:
136 array = g_byte_array_sized_new (address->priv->path_len);
137 g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
138 g_value_take_boxed (value, array);
142 g_value_set_boolean (value, address->priv->abstract);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_unix_socket_address_get_family (GSocketAddress *address)
153 g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
155 return G_SOCKET_FAMILY_UNIX;
159 g_unix_socket_address_get_native_size (GSocketAddress *address)
161 return sizeof (struct sockaddr_un);
165 g_unix_socket_address_to_native (GSocketAddress *address,
170 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
171 struct sockaddr_un *sock;
173 if (destlen < sizeof (*sock))
175 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
176 _("Not enough space for socket address"));
180 if (addr->priv->abstract &&
181 !g_unix_socket_address_abstract_names_supported ())
183 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
184 _("Abstract unix domain socket addresses not supported on this system"));
188 sock = (struct sockaddr_un *) dest;
189 sock->sun_family = AF_UNIX;
190 memset (sock->sun_path, 0, sizeof (sock->sun_path));
191 if (addr->priv->abstract)
193 sock->sun_path[0] = 0;
194 memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
197 strcpy (sock->sun_path, addr->priv->path);
203 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
206 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
208 g_type_class_add_private (klass, sizeof (GUnixSocketAddressPrivate));
210 gobject_class->set_property = g_unix_socket_address_set_property;
211 gobject_class->get_property = g_unix_socket_address_get_property;
213 gsocketaddress_class->get_family = g_unix_socket_address_get_family;
214 gsocketaddress_class->to_native = g_unix_socket_address_to_native;
215 gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
217 g_object_class_install_property (gobject_class,
219 g_param_spec_string ("path",
221 P_("UNIX socket path"),
224 G_PARAM_CONSTRUCT_ONLY |
225 G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
227 g_param_spec_boxed ("path-as-array",
229 P_("UNIX socket path, as byte array"),
232 G_PARAM_CONSTRUCT_ONLY |
233 G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_ABSTRACT,
235 g_param_spec_boolean ("abstract",
237 P_("Whether or not this is an abstract address"),
240 G_PARAM_CONSTRUCT_ONLY |
241 G_PARAM_STATIC_STRINGS));
245 g_unix_socket_address_init (GUnixSocketAddress *address)
247 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
248 G_TYPE_UNIX_SOCKET_ADDRESS,
249 GUnixSocketAddressPrivate);
251 memset (address->priv->path, 0, sizeof (address->priv->path));
252 address->priv->path_len = -1;
256 * g_unix_socket_address_new:
257 * @path: the socket path
259 * Creates a new #GUnixSocketAddress for @path.
261 * To create abstract socket addresses, on systems that support that,
262 * use g_unix_socket_address_new_abstract().
264 * Returns: a new #GUnixSocketAddress
269 g_unix_socket_address_new (const gchar *path)
271 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
278 * g_unix_socket_address_new_abstract:
279 * @path: the abstract name
280 * @path_len: the length of @path, or -1
282 * Creates a new abstract #GUnixSocketAddress for @path.
284 * Unix domain sockets are generally visible in the filesystem. However, some
285 * systems support abstract socket name which are not visible in the
286 * filesystem and not affected by the filesystem permissions, visibility, etc.
288 * Note that not all systems (really only Linux) support abstract
289 * socket names, so if you use them on other systems function calls may
290 * return %G_IO_ERROR_NOT_SUPPORTED errors. You can use
291 * g_unix_socket_address_abstract_names_supported() to see if abstract
292 * names are supported.
294 * If @path_len is -1 then @path is assumed to be a zero terminated
295 * string (although in general abstract names need not be zero terminated
296 * and can have embedded nuls). All bytes after @path_len up to the max size
297 * of an abstract unix domain name is filled with zero bytes.
299 * Returns: a new #GUnixSocketAddress
304 g_unix_socket_address_new_abstract (const gchar *path,
307 GSocketAddress *address;
311 path_len = strlen (path);
313 array = g_byte_array_sized_new (path_len);
315 g_byte_array_append (array, (guint8 *)path, path_len);
317 address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
318 "path-as-array", array,
322 g_byte_array_unref (array);
328 * g_unix_socket_address_get_path:
329 * @address: a #GInetSocketAddress
331 * Gets @address's path, or for abstract sockets the "name".
333 * Guaranteed to be zero-terminated, but an abstract socket
334 * may contain embedded zeros, and thus you should use
335 * g_unix_socket_address_get_path_len() to get the true length
338 * Returns: the path for @address
343 g_unix_socket_address_get_path (GUnixSocketAddress *address)
345 return address->priv->path;
349 * g_unix_socket_address_get_path_len:
350 * @address: a #GInetSocketAddress
352 * Gets the length of @address's path.
354 * For details, see g_unix_socket_address_get_path().
356 * Returns: the length of the path
361 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
363 return address->priv->path_len;
367 * g_unix_socket_address_get_is_abstract:
368 * @address: a #GInetSocketAddress
370 * Gets @address's path.
372 * Returns: %TRUE if the address is abstract, %FALSE otherwise
377 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
379 return address->priv->abstract;
383 * g_unix_socket_address_abstract_names_supported:
385 * Checks if abstract unix domain socket names are supported.
387 * Returns: %TRUE if supported, %FALSE otherwise
392 g_unix_socket_address_abstract_names_supported (void)
401 #define __G_UNIX_SOCKET_ADDRESS_C__
402 #include "gioaliasdef.c"