2008-12-12 19:13:55 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-12-12 19:13:55 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2008-12-12 19:13:55 +01:00
|
|
|
*
|
|
|
|
* Authors: Christian Kellner <gicmo@gnome.org>
|
|
|
|
* Samuel Cormier-Iijima <sciyoshi@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gunixsocketaddress.h"
|
2015-10-04 16:24:24 +02:00
|
|
|
#include "gsocketconnectable.h"
|
2008-12-12 19:13:55 +01:00
|
|
|
#include "glibintl.h"
|
2010-11-11 15:57:25 +01:00
|
|
|
#include "gnetworking.h"
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gunixsocketaddress
|
2009-05-28 06:30:21 +02:00
|
|
|
* @short_description: UNIX GSocketAddress
|
2010-05-09 05:28:17 +02:00
|
|
|
* @include: gio/gunixsocketaddress.h
|
2008-12-12 19:13:55 +01:00
|
|
|
*
|
2010-05-09 05:28:17 +02:00
|
|
|
* Support for UNIX-domain (also known as local) sockets.
|
2010-04-20 23:23:49 +02:00
|
|
|
*
|
2010-05-09 05:28:17 +02:00
|
|
|
* UNIX domain sockets are generally visible in the filesystem.
|
2010-04-20 23:23:49 +02:00
|
|
|
* However, some systems support abstract socket names which are not
|
|
|
|
* visible in the filesystem and not affected by the filesystem
|
|
|
|
* permissions, visibility, etc. Currently this is only supported
|
|
|
|
* under Linux. If you attempt to use abstract sockets on other
|
|
|
|
* systems, function calls may return %G_IO_ERROR_NOT_SUPPORTED
|
2010-05-09 05:28:17 +02:00
|
|
|
* errors. You can use g_unix_socket_address_abstract_names_supported()
|
|
|
|
* to see if abstract names are supported.
|
|
|
|
*
|
2014-02-09 08:07:26 +01:00
|
|
|
* Note that `<gio/gunixsocketaddress.h>` belongs to the UNIX-specific GIO
|
2014-02-06 02:17:46 +01:00
|
|
|
* interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config file
|
|
|
|
* when using it.
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GUnixSocketAddress:
|
|
|
|
*
|
|
|
|
* A UNIX-domain (local) socket address, corresponding to a
|
2014-01-31 11:58:17 +01:00
|
|
|
* struct sockaddr_un.
|
2009-05-28 00:20:08 +02:00
|
|
|
*/
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_PATH,
|
2009-05-18 21:30:33 +02:00
|
|
|
PROP_PATH_AS_ARRAY,
|
|
|
|
PROP_ABSTRACT,
|
2010-04-20 23:23:49 +02:00
|
|
|
PROP_ADDRESS_TYPE
|
2008-12-12 19:13:55 +01:00
|
|
|
};
|
|
|
|
|
2018-06-09 01:24:08 +02:00
|
|
|
#ifndef UNIX_PATH_MAX
|
2020-01-20 13:41:34 +01:00
|
|
|
#define UNIX_PATH_MAX G_SIZEOF_MEMBER (struct sockaddr_un, sun_path)
|
2018-06-09 01:24:08 +02:00
|
|
|
#endif
|
2009-05-18 21:30:33 +02:00
|
|
|
|
2008-12-12 19:13:55 +01:00
|
|
|
struct _GUnixSocketAddressPrivate
|
|
|
|
{
|
2009-05-18 21:30:33 +02:00
|
|
|
char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
|
|
|
|
we can guarantee zero termination of abstract
|
|
|
|
pathnames in the get_path() API */
|
|
|
|
gsize path_len; /* Not including any terminating zeros */
|
2010-04-20 23:23:49 +02:00
|
|
|
GUnixSocketAddressType address_type;
|
2008-12-12 19:13:55 +01:00
|
|
|
};
|
|
|
|
|
2015-10-04 16:24:24 +02:00
|
|
|
static void g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
|
|
|
|
static gchar *g_unix_socket_address_connectable_to_string (GSocketConnectable *connectable);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS,
|
|
|
|
G_ADD_PRIVATE (GUnixSocketAddress)
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
|
|
|
|
g_unix_socket_address_connectable_iface_init))
|
2013-06-11 01:29:58 +02:00
|
|
|
|
2008-12-12 19:13:55 +01:00
|
|
|
static void
|
2009-05-18 21:30:33 +02:00
|
|
|
g_unix_socket_address_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2008-12-12 19:13:55 +01:00
|
|
|
{
|
2009-05-18 21:30:33 +02:00
|
|
|
GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
|
|
|
|
const char *str;
|
|
|
|
GByteArray *array;
|
|
|
|
gsize len;
|
2008-12-12 19:13:55 +01:00
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_PATH:
|
|
|
|
str = g_value_get_string (value);
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
g_strlcpy (address->priv->path, str,
|
|
|
|
sizeof (address->priv->path));
|
|
|
|
address->priv->path_len = strlen (address->priv->path);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PATH_AS_ARRAY:
|
|
|
|
array = g_value_get_boxed (value);
|
|
|
|
|
|
|
|
if (array)
|
|
|
|
{
|
|
|
|
/* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
|
|
|
|
len = MIN (array->len, UNIX_PATH_MAX-1);
|
|
|
|
|
2016-12-02 11:03:16 +01:00
|
|
|
if (len != 0)
|
|
|
|
memcpy (address->priv->path, array->data, len);
|
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
address->priv->path[len] = 0; /* Ensure null-terminated */
|
|
|
|
address->priv->path_len = len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ABSTRACT:
|
2013-04-23 19:38:33 +02:00
|
|
|
/* Only set it if it's not the default... */
|
2010-04-20 23:23:49 +02:00
|
|
|
if (g_value_get_boolean (value))
|
2013-04-23 19:38:33 +02:00
|
|
|
address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
|
2010-04-20 23:23:49 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ADDRESS_TYPE:
|
2013-04-23 19:38:33 +02:00
|
|
|
/* Only set it if it's not the default... */
|
|
|
|
if (g_value_get_enum (value) != G_UNIX_SOCKET_ADDRESS_PATH)
|
|
|
|
address->priv->address_type = g_value_get_enum (value);
|
2009-05-18 21:30:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
2008-12-12 19:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_socket_address_get_property (GObject *object,
|
2009-05-18 21:30:33 +02:00
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2008-12-12 19:13:55 +01:00
|
|
|
{
|
|
|
|
GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
|
2009-05-18 21:30:33 +02:00
|
|
|
GByteArray *array;
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_PATH:
|
2009-05-18 21:30:33 +02:00
|
|
|
g_value_set_string (value, address->priv->path);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_PATH_AS_ARRAY:
|
|
|
|
array = g_byte_array_sized_new (address->priv->path_len);
|
|
|
|
g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
|
|
|
|
g_value_take_boxed (value, array);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ABSTRACT:
|
2010-04-20 23:23:49 +02:00
|
|
|
g_value_set_boolean (value, (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
|
|
|
|
address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ADDRESS_TYPE:
|
|
|
|
g_value_set_enum (value, address->priv->address_type);
|
2009-05-18 21:30:33 +02:00
|
|
|
break;
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
default:
|
2009-05-18 21:30:33 +02:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2008-12-12 19:13:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSocketFamily
|
|
|
|
g_unix_socket_address_get_family (GSocketAddress *address)
|
|
|
|
{
|
|
|
|
g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
|
|
|
|
|
|
|
|
return G_SOCKET_FAMILY_UNIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gssize
|
|
|
|
g_unix_socket_address_get_native_size (GSocketAddress *address)
|
|
|
|
{
|
2010-04-20 23:23:49 +02:00
|
|
|
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
|
|
|
|
|
|
|
|
switch (addr->priv->address_type)
|
|
|
|
{
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
|
|
|
|
return G_STRUCT_OFFSET(struct sockaddr_un, sun_path);
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
|
|
|
|
return G_STRUCT_OFFSET(struct sockaddr_un, sun_path) + addr->priv->path_len + 1;
|
|
|
|
default:
|
|
|
|
return sizeof (struct sockaddr_un);
|
|
|
|
}
|
2008-12-12 19:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_unix_socket_address_to_native (GSocketAddress *address,
|
|
|
|
gpointer dest,
|
2009-05-18 14:10:07 +02:00
|
|
|
gsize destlen,
|
|
|
|
GError **error)
|
2008-12-12 19:13:55 +01:00
|
|
|
{
|
|
|
|
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
|
|
|
|
struct sockaddr_un *sock;
|
2010-04-20 23:23:49 +02:00
|
|
|
gssize socklen;
|
2008-12-12 19:13:55 +01:00
|
|
|
|
2010-04-20 23:23:49 +02:00
|
|
|
socklen = g_unix_socket_address_get_native_size (address);
|
|
|
|
if (destlen < socklen)
|
2009-05-18 14:10:07 +02:00
|
|
|
{
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
|
|
|
_("Not enough space for socket address"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
sock = (struct sockaddr_un *) dest;
|
2010-04-20 23:23:49 +02:00
|
|
|
memset (sock, 0, socklen);
|
2008-12-12 19:13:55 +01:00
|
|
|
sock->sun_family = AF_UNIX;
|
2010-04-20 23:23:49 +02:00
|
|
|
|
|
|
|
switch (addr->priv->address_type)
|
2009-05-18 21:30:33 +02:00
|
|
|
{
|
2010-04-20 23:23:49 +02:00
|
|
|
case G_UNIX_SOCKET_ADDRESS_INVALID:
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_PATH:
|
|
|
|
strcpy (sock->sun_path, addr->priv->path);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
|
|
|
|
if (!g_unix_socket_address_abstract_names_supported ())
|
|
|
|
{
|
|
|
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2012-08-17 04:47:43 +02:00
|
|
|
_("Abstract UNIX domain socket addresses not supported on this system"));
|
2010-04-20 23:23:49 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
sock->sun_path[0] = 0;
|
|
|
|
memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
|
2010-04-20 23:23:49 +02:00
|
|
|
break;
|
2009-05-18 21:30:33 +02:00
|
|
|
}
|
2008-12-12 19:13:55 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->set_property = g_unix_socket_address_set_property;
|
|
|
|
gobject_class->get_property = g_unix_socket_address_get_property;
|
|
|
|
|
|
|
|
gsocketaddress_class->get_family = g_unix_socket_address_get_family;
|
|
|
|
gsocketaddress_class->to_native = g_unix_socket_address_to_native;
|
|
|
|
gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
2009-05-18 21:30:33 +02:00
|
|
|
PROP_PATH,
|
|
|
|
g_param_spec_string ("path",
|
|
|
|
P_("Path"),
|
|
|
|
P_("UNIX socket path"),
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
|
|
|
|
g_param_spec_boxed ("path-as-array",
|
|
|
|
P_("Path array"),
|
|
|
|
P_("UNIX socket path, as byte array"),
|
|
|
|
G_TYPE_BYTE_ARRAY,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2010-04-20 23:23:49 +02:00
|
|
|
/**
|
|
|
|
* GUnixSocketAddress:abstract:
|
|
|
|
*
|
|
|
|
* Whether or not this is an abstract address
|
|
|
|
*
|
|
|
|
* Deprecated: Use #GUnixSocketAddress:address-type, which
|
|
|
|
* distinguishes between zero-padded and non-zero-padded
|
|
|
|
* abstract addresses.
|
|
|
|
*/
|
2009-05-18 21:30:33 +02:00
|
|
|
g_object_class_install_property (gobject_class, PROP_ABSTRACT,
|
|
|
|
g_param_spec_boolean ("abstract",
|
|
|
|
P_("Abstract"),
|
|
|
|
P_("Whether or not this is an abstract address"),
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2010-04-20 23:23:49 +02:00
|
|
|
g_object_class_install_property (gobject_class, PROP_ADDRESS_TYPE,
|
|
|
|
g_param_spec_enum ("address-type",
|
|
|
|
P_("Address type"),
|
|
|
|
P_("The type of UNIX socket address"),
|
|
|
|
G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
|
|
|
|
G_UNIX_SOCKET_ADDRESS_PATH,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2008-12-12 19:13:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 16:24:24 +02:00
|
|
|
static void
|
|
|
|
g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
|
|
|
|
{
|
|
|
|
GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
|
|
|
|
|
|
|
|
iface->enumerate = parent_iface->enumerate;
|
|
|
|
iface->proxy_enumerate = parent_iface->proxy_enumerate;
|
|
|
|
iface->to_string = g_unix_socket_address_connectable_to_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
g_unix_socket_address_connectable_to_string (GSocketConnectable *connectable)
|
|
|
|
{
|
|
|
|
GUnixSocketAddress *ua;
|
|
|
|
GString *out;
|
|
|
|
const gchar *path;
|
|
|
|
gsize path_len, i;
|
|
|
|
|
|
|
|
ua = G_UNIX_SOCKET_ADDRESS (connectable);
|
|
|
|
|
|
|
|
/* Anonymous sockets have no path. */
|
|
|
|
if (ua->priv->address_type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
|
|
|
|
return g_strdup ("anonymous");
|
|
|
|
|
|
|
|
path = g_unix_socket_address_get_path (ua);
|
|
|
|
path_len = g_unix_socket_address_get_path_len (ua);
|
|
|
|
out = g_string_sized_new (path_len);
|
|
|
|
|
|
|
|
/* Return the #GUnixSocketAddress:path, but with all non-printable characters
|
|
|
|
* (including nul bytes) escaped to hex. */
|
|
|
|
for (i = 0; i < path_len; i++)
|
|
|
|
{
|
|
|
|
guint8 c = path[i];
|
|
|
|
|
|
|
|
if (g_ascii_isprint (path[i]))
|
|
|
|
g_string_append_c (out, c);
|
|
|
|
else
|
|
|
|
g_string_append_printf (out, "\\x%02x", (guint) c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (out, FALSE);
|
|
|
|
}
|
|
|
|
|
2008-12-12 19:13:55 +01:00
|
|
|
static void
|
|
|
|
g_unix_socket_address_init (GUnixSocketAddress *address)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
address->priv = g_unix_socket_address_get_instance_private (address);
|
2008-12-12 19:13:55 +01:00
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
memset (address->priv->path, 0, sizeof (address->priv->path));
|
|
|
|
address->priv->path_len = -1;
|
2013-04-23 19:38:33 +02:00
|
|
|
address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
|
2008-12-12 19:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_socket_address_new:
|
|
|
|
* @path: the socket path
|
|
|
|
*
|
|
|
|
* Creates a new #GUnixSocketAddress for @path.
|
|
|
|
*
|
2009-05-18 21:30:33 +02:00
|
|
|
* To create abstract socket addresses, on systems that support that,
|
|
|
|
* use g_unix_socket_address_new_abstract().
|
|
|
|
*
|
2008-12-12 19:13:55 +01:00
|
|
|
* Returns: a new #GUnixSocketAddress
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
|
|
|
GSocketAddress *
|
|
|
|
g_unix_socket_address_new (const gchar *path)
|
|
|
|
{
|
|
|
|
return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
|
|
|
|
"path", path,
|
2009-05-18 21:30:33 +02:00
|
|
|
"abstract", FALSE,
|
2008-12-12 19:13:55 +01:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
/**
|
|
|
|
* g_unix_socket_address_new_abstract:
|
2010-12-29 13:51:44 +01:00
|
|
|
* @path: (array length=path_len) (element-type gchar): the abstract name
|
2009-05-18 21:30:33 +02:00
|
|
|
* @path_len: the length of @path, or -1
|
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
|
|
|
|
* #GUnixSocketAddress for @path.
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* Returns: a new #GUnixSocketAddress
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* Deprecated: Use g_unix_socket_address_new_with_type().
|
|
|
|
*/
|
|
|
|
GSocketAddress *
|
|
|
|
g_unix_socket_address_new_abstract (const gchar *path,
|
|
|
|
gint path_len)
|
|
|
|
{
|
|
|
|
return g_unix_socket_address_new_with_type (path, path_len,
|
|
|
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_socket_address_new_with_type:
|
2010-12-29 13:51:44 +01:00
|
|
|
* @path: (array length=path_len) (element-type gchar): the name
|
2010-04-20 23:23:49 +02:00
|
|
|
* @path_len: the length of @path, or -1
|
|
|
|
* @type: a #GUnixSocketAddressType
|
|
|
|
*
|
|
|
|
* Creates a new #GUnixSocketAddress of type @type with name @path.
|
|
|
|
*
|
|
|
|
* If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
|
|
|
|
* calling g_unix_socket_address_new().
|
|
|
|
*
|
2015-10-04 16:25:03 +02:00
|
|
|
* If @type is %G_UNIX_SOCKET_ADDRESS_ANONYMOUS, @path and @path_len will be
|
|
|
|
* ignored.
|
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
|
|
|
|
* bytes of @path will be copied to the socket's path, and only those
|
|
|
|
* bytes will be considered part of the name. (If @path_len is -1,
|
|
|
|
* then @path is assumed to be NUL-terminated.) For example, if @path
|
|
|
|
* was "test", then calling g_socket_address_get_native_size() on the
|
|
|
|
* returned socket would return 7 (2 bytes of overhead, 1 byte for the
|
|
|
|
* abstract-socket indicator byte, and 4 bytes for the name "test").
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
|
|
|
|
* @path_len bytes of @path will be copied to the socket's path, the
|
|
|
|
* rest of the path will be padded with 0 bytes, and the entire
|
|
|
|
* zero-padded buffer will be considered the name. (As above, if
|
|
|
|
* @path_len is -1, then @path is assumed to be NUL-terminated.) In
|
|
|
|
* this case, g_socket_address_get_native_size() will always return
|
2014-02-06 14:04:52 +01:00
|
|
|
* the full size of a `struct sockaddr_un`, although
|
2010-04-20 23:23:49 +02:00
|
|
|
* g_unix_socket_address_get_path_len() will still return just the
|
|
|
|
* length of @path.
|
|
|
|
*
|
|
|
|
* %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
|
|
|
|
* %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
|
|
|
|
* when connecting to a server created by another process, you must
|
|
|
|
* use the appropriate type corresponding to how that process created
|
|
|
|
* its listening socket.
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
|
|
|
* Returns: a new #GUnixSocketAddress
|
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* Since: 2.26
|
2009-05-18 21:30:33 +02:00
|
|
|
*/
|
|
|
|
GSocketAddress *
|
2010-04-20 23:23:49 +02:00
|
|
|
g_unix_socket_address_new_with_type (const gchar *path,
|
|
|
|
gint path_len,
|
|
|
|
GUnixSocketAddressType type)
|
2009-05-18 21:30:33 +02:00
|
|
|
{
|
|
|
|
GSocketAddress *address;
|
|
|
|
GByteArray *array;
|
|
|
|
|
2010-04-20 23:23:49 +02:00
|
|
|
if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
|
|
|
|
path_len = 0;
|
|
|
|
else if (path_len == -1)
|
2009-05-18 21:30:33 +02:00
|
|
|
path_len = strlen (path);
|
|
|
|
|
|
|
|
array = g_byte_array_sized_new (path_len);
|
|
|
|
|
|
|
|
g_byte_array_append (array, (guint8 *)path, path_len);
|
|
|
|
|
|
|
|
address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
|
|
|
|
"path-as-array", array,
|
2010-04-20 23:23:49 +02:00
|
|
|
"address-type", type,
|
2009-05-18 21:30:33 +02:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_byte_array_unref (array);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_socket_address_get_path:
|
|
|
|
* @address: a #GInetSocketAddress
|
|
|
|
*
|
|
|
|
* Gets @address's path, or for abstract sockets the "name".
|
|
|
|
*
|
|
|
|
* Guaranteed to be zero-terminated, but an abstract socket
|
|
|
|
* may contain embedded zeros, and thus you should use
|
|
|
|
* g_unix_socket_address_get_path_len() to get the true length
|
|
|
|
* of this string.
|
|
|
|
*
|
|
|
|
* Returns: the path for @address
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
g_unix_socket_address_get_path (GUnixSocketAddress *address)
|
|
|
|
{
|
|
|
|
return address->priv->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_socket_address_get_path_len:
|
|
|
|
* @address: a #GInetSocketAddress
|
|
|
|
*
|
|
|
|
* Gets the length of @address's path.
|
|
|
|
*
|
|
|
|
* For details, see g_unix_socket_address_get_path().
|
|
|
|
*
|
|
|
|
* Returns: the length of the path
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
|
|
|
gsize
|
|
|
|
g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
|
|
|
|
{
|
|
|
|
return address->priv->path_len;
|
|
|
|
}
|
|
|
|
|
2010-04-20 23:23:49 +02:00
|
|
|
/**
|
|
|
|
* g_unix_socket_address_get_address_type:
|
|
|
|
* @address: a #GInetSocketAddress
|
|
|
|
*
|
|
|
|
* Gets @address's type.
|
|
|
|
*
|
|
|
|
* Returns: a #GUnixSocketAddressType
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
GUnixSocketAddressType
|
|
|
|
g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
|
|
|
|
{
|
|
|
|
return address->priv->address_type;
|
|
|
|
}
|
|
|
|
|
2009-05-18 21:30:33 +02:00
|
|
|
/**
|
|
|
|
* g_unix_socket_address_get_is_abstract:
|
|
|
|
* @address: a #GInetSocketAddress
|
|
|
|
*
|
2010-04-20 23:23:49 +02:00
|
|
|
* Tests if @address is abstract.
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if the address is abstract, %FALSE otherwise
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2010-04-20 23:23:49 +02:00
|
|
|
*
|
|
|
|
* Deprecated: Use g_unix_socket_address_get_address_type()
|
2009-05-18 21:30:33 +02:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
|
|
|
|
{
|
2010-04-20 23:23:49 +02:00
|
|
|
return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
|
|
|
|
address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
|
2009-05-18 21:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_socket_address_abstract_names_supported:
|
|
|
|
*
|
2012-08-17 04:47:43 +02:00
|
|
|
* Checks if abstract UNIX domain socket names are supported.
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
2009-05-19 11:53:42 +02:00
|
|
|
* Returns: %TRUE if supported, %FALSE otherwise
|
2009-05-18 21:30:33 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_unix_socket_address_abstract_names_supported (void)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|