unicode: Simplify width table generation
[glib.git] / gio / gunixsocketaddress.c
blob70973b8eb3ac5aae45cee572ce9f7da129b6a2f9
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 "glibintl.h"
28 #include "gnetworking.h"
31 /**
32 * SECTION:gunixsocketaddress
33 * @short_description: UNIX GSocketAddress
34 * @include: gio/gunixsocketaddress.h
36 * Support for UNIX-domain (also known as local) sockets.
38 * UNIX domain sockets are generally visible in the filesystem.
39 * However, some systems support abstract socket names which are not
40 * visible in the filesystem and not affected by the filesystem
41 * permissions, visibility, etc. Currently this is only supported
42 * under Linux. If you attempt to use abstract sockets on other
43 * systems, function calls may return %G_IO_ERROR_NOT_SUPPORTED
44 * errors. You can use g_unix_socket_address_abstract_names_supported()
45 * to see if abstract names are supported.
47 * Note that `<gio/gunixsocketaddress.h>` belongs to the UNIX-specific GIO
48 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config file
49 * when using it.
52 /**
53 * GUnixSocketAddress:
55 * A UNIX-domain (local) socket address, corresponding to a
56 * struct sockaddr_un.
59 enum
61 PROP_0,
62 PROP_PATH,
63 PROP_PATH_AS_ARRAY,
64 PROP_ABSTRACT,
65 PROP_ADDRESS_TYPE
68 #define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
70 struct _GUnixSocketAddressPrivate
72 char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
73 we can guarantee zero termination of abstract
74 pathnames in the get_path() API */
75 gsize path_len; /* Not including any terminating zeros */
76 GUnixSocketAddressType address_type;
79 G_DEFINE_TYPE_WITH_PRIVATE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS)
81 static void
82 g_unix_socket_address_set_property (GObject *object,
83 guint prop_id,
84 const GValue *value,
85 GParamSpec *pspec)
87 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
88 const char *str;
89 GByteArray *array;
90 gsize len;
92 switch (prop_id)
94 case PROP_PATH:
95 str = g_value_get_string (value);
96 if (str)
98 g_strlcpy (address->priv->path, str,
99 sizeof (address->priv->path));
100 address->priv->path_len = strlen (address->priv->path);
102 break;
104 case PROP_PATH_AS_ARRAY:
105 array = g_value_get_boxed (value);
107 if (array)
109 /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
110 len = MIN (array->len, UNIX_PATH_MAX-1);
112 memcpy (address->priv->path, array->data, len);
113 address->priv->path[len] = 0; /* Ensure null-terminated */
114 address->priv->path_len = len;
116 break;
118 case PROP_ABSTRACT:
119 /* Only set it if it's not the default... */
120 if (g_value_get_boolean (value))
121 address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
122 break;
124 case PROP_ADDRESS_TYPE:
125 /* Only set it if it's not the default... */
126 if (g_value_get_enum (value) != G_UNIX_SOCKET_ADDRESS_PATH)
127 address->priv->address_type = g_value_get_enum (value);
128 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 static void
136 g_unix_socket_address_get_property (GObject *object,
137 guint prop_id,
138 GValue *value,
139 GParamSpec *pspec)
141 GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
142 GByteArray *array;
144 switch (prop_id)
146 case PROP_PATH:
147 g_value_set_string (value, address->priv->path);
148 break;
150 case PROP_PATH_AS_ARRAY:
151 array = g_byte_array_sized_new (address->priv->path_len);
152 g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
153 g_value_take_boxed (value, array);
154 break;
156 case PROP_ABSTRACT:
157 g_value_set_boolean (value, (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
158 address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED));
160 break;
162 case PROP_ADDRESS_TYPE:
163 g_value_set_enum (value, address->priv->address_type);
164 break;
166 default:
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 static GSocketFamily
172 g_unix_socket_address_get_family (GSocketAddress *address)
174 g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
176 return G_SOCKET_FAMILY_UNIX;
179 static gssize
180 g_unix_socket_address_get_native_size (GSocketAddress *address)
182 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
184 switch (addr->priv->address_type)
186 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
187 return G_STRUCT_OFFSET(struct sockaddr_un, sun_path);
188 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
189 return G_STRUCT_OFFSET(struct sockaddr_un, sun_path) + addr->priv->path_len + 1;
190 default:
191 return sizeof (struct sockaddr_un);
195 static gboolean
196 g_unix_socket_address_to_native (GSocketAddress *address,
197 gpointer dest,
198 gsize destlen,
199 GError **error)
201 GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
202 struct sockaddr_un *sock;
203 gssize socklen;
205 socklen = g_unix_socket_address_get_native_size (address);
206 if (destlen < socklen)
208 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
209 _("Not enough space for socket address"));
210 return FALSE;
213 sock = (struct sockaddr_un *) dest;
214 memset (sock, 0, socklen);
215 sock->sun_family = AF_UNIX;
217 switch (addr->priv->address_type)
219 case G_UNIX_SOCKET_ADDRESS_INVALID:
220 case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
221 break;
223 case G_UNIX_SOCKET_ADDRESS_PATH:
224 strcpy (sock->sun_path, addr->priv->path);
225 break;
227 case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
228 case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
229 if (!g_unix_socket_address_abstract_names_supported ())
231 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
232 _("Abstract UNIX domain socket addresses not supported on this system"));
233 return FALSE;
236 sock->sun_path[0] = 0;
237 memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
238 break;
241 return TRUE;
244 static void
245 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
248 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
250 gobject_class->set_property = g_unix_socket_address_set_property;
251 gobject_class->get_property = g_unix_socket_address_get_property;
253 gsocketaddress_class->get_family = g_unix_socket_address_get_family;
254 gsocketaddress_class->to_native = g_unix_socket_address_to_native;
255 gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
257 g_object_class_install_property (gobject_class,
258 PROP_PATH,
259 g_param_spec_string ("path",
260 P_("Path"),
261 P_("UNIX socket path"),
262 NULL,
263 G_PARAM_READWRITE |
264 G_PARAM_CONSTRUCT_ONLY |
265 G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
267 g_param_spec_boxed ("path-as-array",
268 P_("Path array"),
269 P_("UNIX socket path, as byte array"),
270 G_TYPE_BYTE_ARRAY,
271 G_PARAM_READWRITE |
272 G_PARAM_CONSTRUCT_ONLY |
273 G_PARAM_STATIC_STRINGS));
275 * GUnixSocketAddress:abstract:
277 * Whether or not this is an abstract address
279 * Deprecated: Use #GUnixSocketAddress:address-type, which
280 * distinguishes between zero-padded and non-zero-padded
281 * abstract addresses.
283 g_object_class_install_property (gobject_class, PROP_ABSTRACT,
284 g_param_spec_boolean ("abstract",
285 P_("Abstract"),
286 P_("Whether or not this is an abstract address"),
287 FALSE,
288 G_PARAM_READWRITE |
289 G_PARAM_CONSTRUCT_ONLY |
290 G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_ADDRESS_TYPE,
292 g_param_spec_enum ("address-type",
293 P_("Address type"),
294 P_("The type of UNIX socket address"),
295 G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
296 G_UNIX_SOCKET_ADDRESS_PATH,
297 G_PARAM_READWRITE |
298 G_PARAM_CONSTRUCT_ONLY |
299 G_PARAM_STATIC_STRINGS));
302 static void
303 g_unix_socket_address_init (GUnixSocketAddress *address)
305 address->priv = g_unix_socket_address_get_instance_private (address);
307 memset (address->priv->path, 0, sizeof (address->priv->path));
308 address->priv->path_len = -1;
309 address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
313 * g_unix_socket_address_new:
314 * @path: the socket path
316 * Creates a new #GUnixSocketAddress for @path.
318 * To create abstract socket addresses, on systems that support that,
319 * use g_unix_socket_address_new_abstract().
321 * Returns: a new #GUnixSocketAddress
323 * Since: 2.22
325 GSocketAddress *
326 g_unix_socket_address_new (const gchar *path)
328 return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
329 "path", path,
330 "abstract", FALSE,
331 NULL);
335 * g_unix_socket_address_new_abstract:
336 * @path: (array length=path_len) (element-type gchar): the abstract name
337 * @path_len: the length of @path, or -1
339 * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
340 * #GUnixSocketAddress for @path.
342 * Returns: a new #GUnixSocketAddress
344 * Deprecated: Use g_unix_socket_address_new_with_type().
346 GSocketAddress *
347 g_unix_socket_address_new_abstract (const gchar *path,
348 gint path_len)
350 return g_unix_socket_address_new_with_type (path, path_len,
351 G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
355 * g_unix_socket_address_new_with_type:
356 * @path: (array length=path_len) (element-type gchar): the name
357 * @path_len: the length of @path, or -1
358 * @type: a #GUnixSocketAddressType
360 * Creates a new #GUnixSocketAddress of type @type with name @path.
362 * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
363 * calling g_unix_socket_address_new().
365 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
366 * bytes of @path will be copied to the socket's path, and only those
367 * bytes will be considered part of the name. (If @path_len is -1,
368 * then @path is assumed to be NUL-terminated.) For example, if @path
369 * was "test", then calling g_socket_address_get_native_size() on the
370 * returned socket would return 7 (2 bytes of overhead, 1 byte for the
371 * abstract-socket indicator byte, and 4 bytes for the name "test").
373 * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
374 * @path_len bytes of @path will be copied to the socket's path, the
375 * rest of the path will be padded with 0 bytes, and the entire
376 * zero-padded buffer will be considered the name. (As above, if
377 * @path_len is -1, then @path is assumed to be NUL-terminated.) In
378 * this case, g_socket_address_get_native_size() will always return
379 * the full size of a `struct sockaddr_un`, although
380 * g_unix_socket_address_get_path_len() will still return just the
381 * length of @path.
383 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
384 * %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
385 * when connecting to a server created by another process, you must
386 * use the appropriate type corresponding to how that process created
387 * its listening socket.
389 * Returns: a new #GUnixSocketAddress
391 * Since: 2.26
393 GSocketAddress *
394 g_unix_socket_address_new_with_type (const gchar *path,
395 gint path_len,
396 GUnixSocketAddressType type)
398 GSocketAddress *address;
399 GByteArray *array;
401 if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
402 path_len = 0;
403 else if (path_len == -1)
404 path_len = strlen (path);
406 array = g_byte_array_sized_new (path_len);
408 g_byte_array_append (array, (guint8 *)path, path_len);
410 address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
411 "path-as-array", array,
412 "address-type", type,
413 NULL);
415 g_byte_array_unref (array);
417 return address;
421 * g_unix_socket_address_get_path:
422 * @address: a #GInetSocketAddress
424 * Gets @address's path, or for abstract sockets the "name".
426 * Guaranteed to be zero-terminated, but an abstract socket
427 * may contain embedded zeros, and thus you should use
428 * g_unix_socket_address_get_path_len() to get the true length
429 * of this string.
431 * Returns: the path for @address
433 * Since: 2.22
435 const char *
436 g_unix_socket_address_get_path (GUnixSocketAddress *address)
438 return address->priv->path;
442 * g_unix_socket_address_get_path_len:
443 * @address: a #GInetSocketAddress
445 * Gets the length of @address's path.
447 * For details, see g_unix_socket_address_get_path().
449 * Returns: the length of the path
451 * Since: 2.22
453 gsize
454 g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
456 return address->priv->path_len;
460 * g_unix_socket_address_get_address_type:
461 * @address: a #GInetSocketAddress
463 * Gets @address's type.
465 * Returns: a #GUnixSocketAddressType
467 * Since: 2.26
469 GUnixSocketAddressType
470 g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
472 return address->priv->address_type;
476 * g_unix_socket_address_get_is_abstract:
477 * @address: a #GInetSocketAddress
479 * Tests if @address is abstract.
481 * Returns: %TRUE if the address is abstract, %FALSE otherwise
483 * Since: 2.22
485 * Deprecated: Use g_unix_socket_address_get_address_type()
487 gboolean
488 g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
490 return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
491 address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
495 * g_unix_socket_address_abstract_names_supported:
497 * Checks if abstract UNIX domain socket names are supported.
499 * Returns: %TRUE if supported, %FALSE otherwise
501 * Since: 2.22
503 gboolean
504 g_unix_socket_address_abstract_names_supported (void)
506 #ifdef __linux__
507 return TRUE;
508 #else
509 return FALSE;
510 #endif