Updated Italian translation
[glib.git] / gio / gunixsocketaddress.c
blobf4fc077584761b655e46b9ded6ba5533c2cbd226
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, see <http://www.gnu.org/licenses/>.
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
22 #include <config.h>
23 #include <glib.h>
24 #include <string.h>
26 #include "gunixsocketaddress.h"
27 #include "gsocketconnectable.h"
28 #include "glibintl.h"
29 #include "gnetworking.h"
32 /**
33 * SECTION:gunixsocketaddress
34 * @short_description: UNIX GSocketAddress
35 * @include: gio/gunixsocketaddress.h
37 * Support for UNIX-domain (also known as local) sockets.
39 * UNIX domain sockets are generally visible in the filesystem.
40 * However, some systems support abstract socket names which are not
41 * visible in the filesystem and not affected by the filesystem
42 * permissions, visibility, etc. Currently this is only supported
43 * under Linux. If you attempt to use abstract sockets on other
44 * systems, function calls may return %G_IO_ERROR_NOT_SUPPORTED
45 * errors. You can use g_unix_socket_address_abstract_names_supported()
46 * to see if abstract names are supported.
48 * Note that `<gio/gunixsocketaddress.h>` belongs to the UNIX-specific GIO
49 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config file
50 * when using it.
53 /**
54 * GUnixSocketAddress:
56 * A UNIX-domain (local) socket address, corresponding to a
57 * struct sockaddr_un.
60 enum
62 PROP_0,
63 PROP_PATH,
64 PROP_PATH_AS_ARRAY,
65 PROP_ABSTRACT,
66 PROP_ADDRESS_TYPE
69 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
71 struct _GUnixSocketAddressPrivate
73 char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
74 we can guarantee zero termination of abstract
75 pathnames in the get_path() API */
76 gsize path_len; /* Not including any terminating zeros */
77 GUnixSocketAddressType address_type;
80 static void g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
81 static gchar *g_unix_socket_address_connectable_to_string (GSocketConnectable *connectable);
83 G_DEFINE_TYPE_WITH_CODE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS,
84 G_ADD_PRIVATE (GUnixSocketAddress)
85 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
86 g_unix_socket_address_connectable_iface_init))
88 static void
89 g_unix_socket_address_set_property (GObject *object,
90 guint prop_id,
91 const GValue *value,
92 GParamSpec *pspec)
94 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
95 const char *str;
96 GByteArray *array;
97 gsize len;
99 switch (prop_id)
101 case PROP_PATH:
102 str = g_value_get_string (value);
103 if (str)
105 g_strlcpy (address->priv->path, str,
106 sizeof (address->priv->path));
107 address->priv->path_len = strlen (address->priv->path);
109 break;
111 case PROP_PATH_AS_ARRAY:
112 array = g_value_get_boxed (value);
114 if (array)
116 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
117 len = MIN (array->len, UNIX_PATH_MAX-1);
119 memcpy (address->priv->path, array->data, len);
120 address->priv->path[len] = 0; /* Ensure null-terminated */
121 address->priv->path_len = len;
123 break;
125 case PROP_ABSTRACT:
126 /* Only set it if it's not the default... */
127 if (g_value_get_boolean (value))
128 address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
129 break;
131 case PROP_ADDRESS_TYPE:
132 /* Only set it if it's not the default... */
133 if (g_value_get_enum (value) != G_UNIX_SOCKET_ADDRESS_PATH)
134 address->priv->address_type = g_value_get_enum (value);
135 break;
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 static void
143 g_unix_socket_address_get_property (GObject *object,
144 guint prop_id,
145 GValue *value,
146 GParamSpec *pspec)
148 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
149 GByteArray *array;
151 switch (prop_id)
153 case PROP_PATH:
154 g_value_set_string (value, address->priv->path);
155 break;
157 case PROP_PATH_AS_ARRAY:
158 array = g_byte_array_sized_new (address->priv->path_len);
159 g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
160 g_value_take_boxed (value, array);
161 break;
163 case PROP_ABSTRACT:
164 g_value_set_boolean (value, (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
165 address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED));
167 break;
169 case PROP_ADDRESS_TYPE:
170 g_value_set_enum (value, address->priv->address_type);
171 break;
173 default:
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 static GSocketFamily
179 g_unix_socket_address_get_family (GSocketAddress *address)
181 g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
183 return G_SOCKET_FAMILY_UNIX;
186 static gssize
187 g_unix_socket_address_get_native_size (GSocketAddress *address)
189 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
191 switch (addr->priv->address_type)
193 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
194 return G_STRUCT_OFFSET(struct sockaddr_un, sun_path);
195 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
196 return G_STRUCT_OFFSET(struct sockaddr_un, sun_path) + addr->priv->path_len + 1;
197 default:
198 return sizeof (struct sockaddr_un);
202 static gboolean
203 g_unix_socket_address_to_native (GSocketAddress *address,
204 gpointer dest,
205 gsize destlen,
206 GError **error)
208 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
209 struct sockaddr_un *sock;
210 gssize socklen;
212 socklen = g_unix_socket_address_get_native_size (address);
213 if (destlen < socklen)
215 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
216 _("Not enough space for socket address"));
217 return FALSE;
220 sock = (struct sockaddr_un *) dest;
221 memset (sock, 0, socklen);
222 sock->sun_family = AF_UNIX;
224 switch (addr->priv->address_type)
226 case G_UNIX_SOCKET_ADDRESS_INVALID:
227 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
228 break;
230 case G_UNIX_SOCKET_ADDRESS_PATH:
231 strcpy (sock->sun_path, addr->priv->path);
232 break;
234 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
235 case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
236 if (!g_unix_socket_address_abstract_names_supported ())
238 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
239 _("Abstract UNIX domain socket addresses not supported on this system"));
240 return FALSE;
243 sock->sun_path[0] = 0;
244 memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
245 break;
248 return TRUE;
251 static void
252 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
254 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
255 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
257 gobject_class->set_property = g_unix_socket_address_set_property;
258 gobject_class->get_property = g_unix_socket_address_get_property;
260 gsocketaddress_class->get_family = g_unix_socket_address_get_family;
261 gsocketaddress_class->to_native = g_unix_socket_address_to_native;
262 gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
264 g_object_class_install_property (gobject_class,
265 PROP_PATH,
266 g_param_spec_string ("path",
267 P_("Path"),
268 P_("UNIX socket path"),
269 NULL,
270 G_PARAM_READWRITE |
271 G_PARAM_CONSTRUCT_ONLY |
272 G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
274 g_param_spec_boxed ("path-as-array",
275 P_("Path array"),
276 P_("UNIX socket path, as byte array"),
277 G_TYPE_BYTE_ARRAY,
278 G_PARAM_READWRITE |
279 G_PARAM_CONSTRUCT_ONLY |
280 G_PARAM_STATIC_STRINGS));
282 * GUnixSocketAddress:abstract:
284 * Whether or not this is an abstract address
286 * Deprecated: Use #GUnixSocketAddress:address-type, which
287 * distinguishes between zero-padded and non-zero-padded
288 * abstract addresses.
290 g_object_class_install_property (gobject_class, PROP_ABSTRACT,
291 g_param_spec_boolean ("abstract",
292 P_("Abstract"),
293 P_("Whether or not this is an abstract address"),
294 FALSE,
295 G_PARAM_READWRITE |
296 G_PARAM_CONSTRUCT_ONLY |
297 G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_ADDRESS_TYPE,
299 g_param_spec_enum ("address-type",
300 P_("Address type"),
301 P_("The type of UNIX socket address"),
302 G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
303 G_UNIX_SOCKET_ADDRESS_PATH,
304 G_PARAM_READWRITE |
305 G_PARAM_CONSTRUCT_ONLY |
306 G_PARAM_STATIC_STRINGS));
309 static void
310 g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
312 GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
314 iface->enumerate = parent_iface->enumerate;
315 iface->proxy_enumerate = parent_iface->proxy_enumerate;
316 iface->to_string = g_unix_socket_address_connectable_to_string;
319 static gchar *
320 g_unix_socket_address_connectable_to_string (GSocketConnectable *connectable)
322 GUnixSocketAddress *ua;
323 GString *out;
324 const gchar *path;
325 gsize path_len, i;
327 ua = G_UNIX_SOCKET_ADDRESS (connectable);
329 /* Anonymous sockets have no path. */
330 if (ua->priv->address_type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
331 return g_strdup ("anonymous");
333 path = g_unix_socket_address_get_path (ua);
334 path_len = g_unix_socket_address_get_path_len (ua);
335 out = g_string_sized_new (path_len);
337 /* Return the #GUnixSocketAddress:path, but with all non-printable characters
338 * (including nul bytes) escaped to hex. */
339 for (i = 0; i < path_len; i++)
341 guint8 c = path[i];
343 if (g_ascii_isprint (path[i]))
344 g_string_append_c (out, c);
345 else
346 g_string_append_printf (out, "\\x%02x", (guint) c);
349 return g_string_free (out, FALSE);
352 static void
353 g_unix_socket_address_init (GUnixSocketAddress *address)
355 address->priv = g_unix_socket_address_get_instance_private (address);
357 memset (address->priv->path, 0, sizeof (address->priv->path));
358 address->priv->path_len = -1;
359 address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
363 * g_unix_socket_address_new:
364 * @path: the socket path
366 * Creates a new #GUnixSocketAddress for @path.
368 * To create abstract socket addresses, on systems that support that,
369 * use g_unix_socket_address_new_abstract().
371 * Returns: a new #GUnixSocketAddress
373 * Since: 2.22
375 GSocketAddress *
376 g_unix_socket_address_new (const gchar *path)
378 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
379 "path", path,
380 "abstract", FALSE,
381 NULL);
385 * g_unix_socket_address_new_abstract:
386 * @path: (array length=path_len) (element-type gchar): the abstract name
387 * @path_len: the length of @path, or -1
389 * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
390 * #GUnixSocketAddress for @path.
392 * Returns: a new #GUnixSocketAddress
394 * Deprecated: Use g_unix_socket_address_new_with_type().
396 GSocketAddress *
397 g_unix_socket_address_new_abstract (const gchar *path,
398 gint path_len)
400 return g_unix_socket_address_new_with_type (path, path_len,
401 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
405 * g_unix_socket_address_new_with_type:
406 * @path: (array length=path_len) (element-type gchar): the name
407 * @path_len: the length of @path, or -1
408 * @type: a #GUnixSocketAddressType
410 * Creates a new #GUnixSocketAddress of type @type with name @path.
412 * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
413 * calling g_unix_socket_address_new().
415 * If @type is %G_UNIX_SOCKET_ADDRESS_ANONYMOUS, @path and @path_len will be
416 * ignored.
418 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
419 * bytes of @path will be copied to the socket's path, and only those
420 * bytes will be considered part of the name. (If @path_len is -1,
421 * then @path is assumed to be NUL-terminated.) For example, if @path
422 * was "test", then calling g_socket_address_get_native_size() on the
423 * returned socket would return 7 (2 bytes of overhead, 1 byte for the
424 * abstract-socket indicator byte, and 4 bytes for the name "test").
426 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
427 * @path_len bytes of @path will be copied to the socket's path, the
428 * rest of the path will be padded with 0 bytes, and the entire
429 * zero-padded buffer will be considered the name. (As above, if
430 * @path_len is -1, then @path is assumed to be NUL-terminated.) In
431 * this case, g_socket_address_get_native_size() will always return
432 * the full size of a `struct sockaddr_un`, although
433 * g_unix_socket_address_get_path_len() will still return just the
434 * length of @path.
436 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
437 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
438 * when connecting to a server created by another process, you must
439 * use the appropriate type corresponding to how that process created
440 * its listening socket.
442 * Returns: a new #GUnixSocketAddress
444 * Since: 2.26
446 GSocketAddress *
447 g_unix_socket_address_new_with_type (const gchar *path,
448 gint path_len,
449 GUnixSocketAddressType type)
451 GSocketAddress *address;
452 GByteArray *array;
454 if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
455 path_len = 0;
456 else if (path_len == -1)
457 path_len = strlen (path);
459 array = g_byte_array_sized_new (path_len);
461 g_byte_array_append (array, (guint8 *)path, path_len);
463 address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
464 "path-as-array", array,
465 "address-type", type,
466 NULL);
468 g_byte_array_unref (array);
470 return address;
474 * g_unix_socket_address_get_path:
475 * @address: a #GInetSocketAddress
477 * Gets @address's path, or for abstract sockets the "name".
479 * Guaranteed to be zero-terminated, but an abstract socket
480 * may contain embedded zeros, and thus you should use
481 * g_unix_socket_address_get_path_len() to get the true length
482 * of this string.
484 * Returns: the path for @address
486 * Since: 2.22
488 const char *
489 g_unix_socket_address_get_path (GUnixSocketAddress *address)
491 return address->priv->path;
495 * g_unix_socket_address_get_path_len:
496 * @address: a #GInetSocketAddress
498 * Gets the length of @address's path.
500 * For details, see g_unix_socket_address_get_path().
502 * Returns: the length of the path
504 * Since: 2.22
506 gsize
507 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
509 return address->priv->path_len;
513 * g_unix_socket_address_get_address_type:
514 * @address: a #GInetSocketAddress
516 * Gets @address's type.
518 * Returns: a #GUnixSocketAddressType
520 * Since: 2.26
522 GUnixSocketAddressType
523 g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
525 return address->priv->address_type;
529 * g_unix_socket_address_get_is_abstract:
530 * @address: a #GInetSocketAddress
532 * Tests if @address is abstract.
534 * Returns: %TRUE if the address is abstract, %FALSE otherwise
536 * Since: 2.22
538 * Deprecated: Use g_unix_socket_address_get_address_type()
540 gboolean
541 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
543 return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
544 address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
548 * g_unix_socket_address_abstract_names_supported:
550 * Checks if abstract UNIX domain socket names are supported.
552 * Returns: %TRUE if supported, %FALSE otherwise
554 * Since: 2.22
556 gboolean
557 g_unix_socket_address_abstract_names_supported (void)
559 #ifdef __linux__
560 return TRUE;
561 #else
562 return FALSE;
563 #endif