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.1 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>
26 #include "gunixsocketaddress.h"
27 #include "gsocketconnectable.h"
29 #include "gnetworking.h"
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
56 * A UNIX-domain (local) socket address, corresponding to a
70 #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
;
82 static void g_unix_socket_address_connectable_iface_init (GSocketConnectableIface
*iface
);
83 static gchar
*g_unix_socket_address_connectable_to_string (GSocketConnectable
*connectable
);
85 G_DEFINE_TYPE_WITH_CODE (GUnixSocketAddress
, g_unix_socket_address
, G_TYPE_SOCKET_ADDRESS
,
86 G_ADD_PRIVATE (GUnixSocketAddress
)
87 G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE
,
88 g_unix_socket_address_connectable_iface_init
))
91 g_unix_socket_address_set_property (GObject
*object
,
96 GUnixSocketAddress
*address
= G_UNIX_SOCKET_ADDRESS (object
);
104 str
= g_value_get_string (value
);
107 g_strlcpy (address
->priv
->path
, str
,
108 sizeof (address
->priv
->path
));
109 address
->priv
->path_len
= strlen (address
->priv
->path
);
113 case PROP_PATH_AS_ARRAY
:
114 array
= g_value_get_boxed (value
);
118 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
119 len
= MIN (array
->len
, UNIX_PATH_MAX
-1);
122 memcpy (address
->priv
->path
, array
->data
, len
);
124 address
->priv
->path
[len
] = 0; /* Ensure null-terminated */
125 address
->priv
->path_len
= len
;
130 /* Only set it if it's not the default... */
131 if (g_value_get_boolean (value
))
132 address
->priv
->address_type
= G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
;
135 case PROP_ADDRESS_TYPE
:
136 /* Only set it if it's not the default... */
137 if (g_value_get_enum (value
) != G_UNIX_SOCKET_ADDRESS_PATH
)
138 address
->priv
->address_type
= g_value_get_enum (value
);
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
147 g_unix_socket_address_get_property (GObject
*object
,
152 GUnixSocketAddress
*address
= G_UNIX_SOCKET_ADDRESS (object
);
158 g_value_set_string (value
, address
->priv
->path
);
161 case PROP_PATH_AS_ARRAY
:
162 array
= g_byte_array_sized_new (address
->priv
->path_len
);
163 g_byte_array_append (array
, (guint8
*)address
->priv
->path
, address
->priv
->path_len
);
164 g_value_take_boxed (value
, array
);
168 g_value_set_boolean (value
, (address
->priv
->address_type
== G_UNIX_SOCKET_ADDRESS_ABSTRACT
||
169 address
->priv
->address_type
== G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
));
173 case PROP_ADDRESS_TYPE
:
174 g_value_set_enum (value
, address
->priv
->address_type
);
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
183 g_unix_socket_address_get_family (GSocketAddress
*address
)
185 g_assert (PF_UNIX
== G_SOCKET_FAMILY_UNIX
);
187 return G_SOCKET_FAMILY_UNIX
;
191 g_unix_socket_address_get_native_size (GSocketAddress
*address
)
193 GUnixSocketAddress
*addr
= G_UNIX_SOCKET_ADDRESS (address
);
195 switch (addr
->priv
->address_type
)
197 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS
:
198 return G_STRUCT_OFFSET(struct sockaddr_un
, sun_path
);
199 case G_UNIX_SOCKET_ADDRESS_ABSTRACT
:
200 return G_STRUCT_OFFSET(struct sockaddr_un
, sun_path
) + addr
->priv
->path_len
+ 1;
202 return sizeof (struct sockaddr_un
);
207 g_unix_socket_address_to_native (GSocketAddress
*address
,
212 GUnixSocketAddress
*addr
= G_UNIX_SOCKET_ADDRESS (address
);
213 struct sockaddr_un
*sock
;
216 socklen
= g_unix_socket_address_get_native_size (address
);
217 if (destlen
< socklen
)
219 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NO_SPACE
,
220 _("Not enough space for socket address"));
224 sock
= (struct sockaddr_un
*) dest
;
225 memset (sock
, 0, socklen
);
226 sock
->sun_family
= AF_UNIX
;
228 switch (addr
->priv
->address_type
)
230 case G_UNIX_SOCKET_ADDRESS_INVALID
:
231 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS
:
234 case G_UNIX_SOCKET_ADDRESS_PATH
:
235 strcpy (sock
->sun_path
, addr
->priv
->path
);
238 case G_UNIX_SOCKET_ADDRESS_ABSTRACT
:
239 case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
:
240 if (!g_unix_socket_address_abstract_names_supported ())
242 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
243 _("Abstract UNIX domain socket addresses not supported on this system"));
247 sock
->sun_path
[0] = 0;
248 memcpy (sock
->sun_path
+1, addr
->priv
->path
, addr
->priv
->path_len
);
256 g_unix_socket_address_class_init (GUnixSocketAddressClass
*klass
)
258 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
259 GSocketAddressClass
*gsocketaddress_class
= G_SOCKET_ADDRESS_CLASS (klass
);
261 gobject_class
->set_property
= g_unix_socket_address_set_property
;
262 gobject_class
->get_property
= g_unix_socket_address_get_property
;
264 gsocketaddress_class
->get_family
= g_unix_socket_address_get_family
;
265 gsocketaddress_class
->to_native
= g_unix_socket_address_to_native
;
266 gsocketaddress_class
->get_native_size
= g_unix_socket_address_get_native_size
;
268 g_object_class_install_property (gobject_class
,
270 g_param_spec_string ("path",
272 P_("UNIX socket path"),
275 G_PARAM_CONSTRUCT_ONLY
|
276 G_PARAM_STATIC_STRINGS
));
277 g_object_class_install_property (gobject_class
, PROP_PATH_AS_ARRAY
,
278 g_param_spec_boxed ("path-as-array",
280 P_("UNIX socket path, as byte array"),
283 G_PARAM_CONSTRUCT_ONLY
|
284 G_PARAM_STATIC_STRINGS
));
286 * GUnixSocketAddress:abstract:
288 * Whether or not this is an abstract address
290 * Deprecated: Use #GUnixSocketAddress:address-type, which
291 * distinguishes between zero-padded and non-zero-padded
292 * abstract addresses.
294 g_object_class_install_property (gobject_class
, PROP_ABSTRACT
,
295 g_param_spec_boolean ("abstract",
297 P_("Whether or not this is an abstract address"),
300 G_PARAM_CONSTRUCT_ONLY
|
301 G_PARAM_STATIC_STRINGS
));
302 g_object_class_install_property (gobject_class
, PROP_ADDRESS_TYPE
,
303 g_param_spec_enum ("address-type",
305 P_("The type of UNIX socket address"),
306 G_TYPE_UNIX_SOCKET_ADDRESS_TYPE
,
307 G_UNIX_SOCKET_ADDRESS_PATH
,
309 G_PARAM_CONSTRUCT_ONLY
|
310 G_PARAM_STATIC_STRINGS
));
314 g_unix_socket_address_connectable_iface_init (GSocketConnectableIface
*iface
)
316 GSocketConnectableIface
*parent_iface
= g_type_interface_peek_parent (iface
);
318 iface
->enumerate
= parent_iface
->enumerate
;
319 iface
->proxy_enumerate
= parent_iface
->proxy_enumerate
;
320 iface
->to_string
= g_unix_socket_address_connectable_to_string
;
324 g_unix_socket_address_connectable_to_string (GSocketConnectable
*connectable
)
326 GUnixSocketAddress
*ua
;
331 ua
= G_UNIX_SOCKET_ADDRESS (connectable
);
333 /* Anonymous sockets have no path. */
334 if (ua
->priv
->address_type
== G_UNIX_SOCKET_ADDRESS_ANONYMOUS
)
335 return g_strdup ("anonymous");
337 path
= g_unix_socket_address_get_path (ua
);
338 path_len
= g_unix_socket_address_get_path_len (ua
);
339 out
= g_string_sized_new (path_len
);
341 /* Return the #GUnixSocketAddress:path, but with all non-printable characters
342 * (including nul bytes) escaped to hex. */
343 for (i
= 0; i
< path_len
; i
++)
347 if (g_ascii_isprint (path
[i
]))
348 g_string_append_c (out
, c
);
350 g_string_append_printf (out
, "\\x%02x", (guint
) c
);
353 return g_string_free (out
, FALSE
);
357 g_unix_socket_address_init (GUnixSocketAddress
*address
)
359 address
->priv
= g_unix_socket_address_get_instance_private (address
);
361 memset (address
->priv
->path
, 0, sizeof (address
->priv
->path
));
362 address
->priv
->path_len
= -1;
363 address
->priv
->address_type
= G_UNIX_SOCKET_ADDRESS_PATH
;
367 * g_unix_socket_address_new:
368 * @path: the socket path
370 * Creates a new #GUnixSocketAddress for @path.
372 * To create abstract socket addresses, on systems that support that,
373 * use g_unix_socket_address_new_abstract().
375 * Returns: a new #GUnixSocketAddress
380 g_unix_socket_address_new (const gchar
*path
)
382 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS
,
389 * g_unix_socket_address_new_abstract:
390 * @path: (array length=path_len) (element-type gchar): the abstract name
391 * @path_len: the length of @path, or -1
393 * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
394 * #GUnixSocketAddress for @path.
396 * Returns: a new #GUnixSocketAddress
398 * Deprecated: Use g_unix_socket_address_new_with_type().
401 g_unix_socket_address_new_abstract (const gchar
*path
,
404 return g_unix_socket_address_new_with_type (path
, path_len
,
405 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
);
409 * g_unix_socket_address_new_with_type:
410 * @path: (array length=path_len) (element-type gchar): the name
411 * @path_len: the length of @path, or -1
412 * @type: a #GUnixSocketAddressType
414 * Creates a new #GUnixSocketAddress of type @type with name @path.
416 * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
417 * calling g_unix_socket_address_new().
419 * If @type is %G_UNIX_SOCKET_ADDRESS_ANONYMOUS, @path and @path_len will be
422 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
423 * bytes of @path will be copied to the socket's path, and only those
424 * bytes will be considered part of the name. (If @path_len is -1,
425 * then @path is assumed to be NUL-terminated.) For example, if @path
426 * was "test", then calling g_socket_address_get_native_size() on the
427 * returned socket would return 7 (2 bytes of overhead, 1 byte for the
428 * abstract-socket indicator byte, and 4 bytes for the name "test").
430 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
431 * @path_len bytes of @path will be copied to the socket's path, the
432 * rest of the path will be padded with 0 bytes, and the entire
433 * zero-padded buffer will be considered the name. (As above, if
434 * @path_len is -1, then @path is assumed to be NUL-terminated.) In
435 * this case, g_socket_address_get_native_size() will always return
436 * the full size of a `struct sockaddr_un`, although
437 * g_unix_socket_address_get_path_len() will still return just the
440 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
441 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
442 * when connecting to a server created by another process, you must
443 * use the appropriate type corresponding to how that process created
444 * its listening socket.
446 * Returns: a new #GUnixSocketAddress
451 g_unix_socket_address_new_with_type (const gchar
*path
,
453 GUnixSocketAddressType type
)
455 GSocketAddress
*address
;
458 if (type
== G_UNIX_SOCKET_ADDRESS_ANONYMOUS
)
460 else if (path_len
== -1)
461 path_len
= strlen (path
);
463 array
= g_byte_array_sized_new (path_len
);
465 g_byte_array_append (array
, (guint8
*)path
, path_len
);
467 address
= g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS
,
468 "path-as-array", array
,
469 "address-type", type
,
472 g_byte_array_unref (array
);
478 * g_unix_socket_address_get_path:
479 * @address: a #GInetSocketAddress
481 * Gets @address's path, or for abstract sockets the "name".
483 * Guaranteed to be zero-terminated, but an abstract socket
484 * may contain embedded zeros, and thus you should use
485 * g_unix_socket_address_get_path_len() to get the true length
488 * Returns: the path for @address
493 g_unix_socket_address_get_path (GUnixSocketAddress
*address
)
495 return address
->priv
->path
;
499 * g_unix_socket_address_get_path_len:
500 * @address: a #GInetSocketAddress
502 * Gets the length of @address's path.
504 * For details, see g_unix_socket_address_get_path().
506 * Returns: the length of the path
511 g_unix_socket_address_get_path_len (GUnixSocketAddress
*address
)
513 return address
->priv
->path_len
;
517 * g_unix_socket_address_get_address_type:
518 * @address: a #GInetSocketAddress
520 * Gets @address's type.
522 * Returns: a #GUnixSocketAddressType
526 GUnixSocketAddressType
527 g_unix_socket_address_get_address_type (GUnixSocketAddress
*address
)
529 return address
->priv
->address_type
;
533 * g_unix_socket_address_get_is_abstract:
534 * @address: a #GInetSocketAddress
536 * Tests if @address is abstract.
538 * Returns: %TRUE if the address is abstract, %FALSE otherwise
542 * Deprecated: Use g_unix_socket_address_get_address_type()
545 g_unix_socket_address_get_is_abstract (GUnixSocketAddress
*address
)
547 return (address
->priv
->address_type
== G_UNIX_SOCKET_ADDRESS_ABSTRACT
||
548 address
->priv
->address_type
== G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
);
552 * g_unix_socket_address_abstract_names_supported:
554 * Checks if abstract UNIX domain socket names are supported.
556 * Returns: %TRUE if supported, %FALSE otherwise
561 g_unix_socket_address_abstract_names_supported (void)