mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-29 13:23:31 +02:00
GUnixSocketAddress: handle abstract sockets with non-0-padded names
There are apparently two incompatible ways of naming abstract sockets: pad the sockaddr with 0s and use the entire thing as the name, or else don't, and just pass a shorter length value to the relevant functions. We previously only supported the former method. Add support for the latter. Also correctly handle "anonymous" unix sockaddrs (eg, the client side of a connection, or a socketpair() socket), and add unix domain socket support to the socket-client and socket-server test programs to make sure this all works. https://bugzilla.gnome.org/show_bug.cgi?id=615960
This commit is contained in:
@@ -36,6 +36,16 @@
|
||||
* @short_description: UNIX GSocketAddress
|
||||
*
|
||||
* Support for UNIX-domain (aka local) sockets.
|
||||
*
|
||||
* Unix domain sockets are generally visible in the filesystem.
|
||||
* 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
|
||||
* errors. You can use
|
||||
* g_unix_socket_address_abstract_names_supported() to see if abstract
|
||||
* names are supported.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -52,6 +62,7 @@ enum
|
||||
PROP_PATH,
|
||||
PROP_PATH_AS_ARRAY,
|
||||
PROP_ABSTRACT,
|
||||
PROP_ADDRESS_TYPE
|
||||
};
|
||||
|
||||
#define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
|
||||
@@ -62,7 +73,7 @@ struct _GUnixSocketAddressPrivate
|
||||
we can guarantee zero termination of abstract
|
||||
pathnames in the get_path() API */
|
||||
gsize path_len; /* Not including any terminating zeros */
|
||||
gboolean abstract;
|
||||
GUnixSocketAddressType address_type;
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -96,10 +107,6 @@ g_unix_socket_address_set_property (GObject *object,
|
||||
/* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
|
||||
len = MIN (array->len, UNIX_PATH_MAX-1);
|
||||
|
||||
/* Remove any trailing zeros from path_len */
|
||||
while (len > 0 && array->data[len-1] == 0)
|
||||
len--;
|
||||
|
||||
memcpy (address->priv->path, array->data, len);
|
||||
address->priv->path[len] = 0; /* Ensure null-terminated */
|
||||
address->priv->path_len = len;
|
||||
@@ -107,7 +114,26 @@ g_unix_socket_address_set_property (GObject *object,
|
||||
break;
|
||||
|
||||
case PROP_ABSTRACT:
|
||||
address->priv->abstract = g_value_get_boolean (value);
|
||||
/* If the caller already set PROP_ADDRESS_TYPE, don't let the
|
||||
* default value of PROP_ABSTRACT overwrite it.
|
||||
*/
|
||||
if (address->priv->address_type != G_UNIX_SOCKET_ADDRESS_INVALID)
|
||||
return;
|
||||
|
||||
if (g_value_get_boolean (value))
|
||||
address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
|
||||
else
|
||||
address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
|
||||
break;
|
||||
|
||||
case PROP_ADDRESS_TYPE:
|
||||
/* If the caller already set PROP_ABSTRACT, don't let the
|
||||
* default value of PROP_ADDRESS_TYPE overwrite it.
|
||||
*/
|
||||
if (address->priv->address_type != G_UNIX_SOCKET_ADDRESS_INVALID)
|
||||
return;
|
||||
|
||||
address->priv->address_type = g_value_get_enum (value);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -137,7 +163,13 @@ g_unix_socket_address_get_property (GObject *object,
|
||||
break;
|
||||
|
||||
case PROP_ABSTRACT:
|
||||
g_value_set_boolean (value, address->priv->abstract);
|
||||
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);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -156,7 +188,17 @@ g_unix_socket_address_get_family (GSocketAddress *address)
|
||||
static gssize
|
||||
g_unix_socket_address_get_native_size (GSocketAddress *address)
|
||||
{
|
||||
return sizeof (struct sockaddr_un);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
@@ -167,32 +209,43 @@ g_unix_socket_address_to_native (GSocketAddress *address,
|
||||
{
|
||||
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
|
||||
struct sockaddr_un *sock;
|
||||
gssize socklen;
|
||||
|
||||
if (destlen < sizeof (*sock))
|
||||
socklen = g_unix_socket_address_get_native_size (address);
|
||||
if (destlen < socklen)
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
||||
_("Not enough space for socket address"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (addr->priv->abstract &&
|
||||
!g_unix_socket_address_abstract_names_supported ())
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
_("Abstract unix domain socket addresses not supported on this system"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sock = (struct sockaddr_un *) dest;
|
||||
memset (sock, 0, socklen);
|
||||
sock->sun_family = AF_UNIX;
|
||||
memset (sock->sun_path, 0, sizeof (sock->sun_path));
|
||||
if (addr->priv->abstract)
|
||||
|
||||
switch (addr->priv->address_type)
|
||||
{
|
||||
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,
|
||||
_("Abstract unix domain socket addresses not supported on this system"));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
sock->sun_path[0] = 0;
|
||||
memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
|
||||
break;
|
||||
}
|
||||
else
|
||||
strcpy (sock->sun_path, addr->priv->path);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
@@ -229,6 +282,15 @@ g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
g_object_class_install_property (gobject_class, PROP_ABSTRACT,
|
||||
g_param_spec_boolean ("abstract",
|
||||
P_("Abstract"),
|
||||
@@ -237,6 +299,15 @@ g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
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));
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -248,6 +319,7 @@ g_unix_socket_address_init (GUnixSocketAddress *address)
|
||||
|
||||
memset (address->priv->path, 0, sizeof (address->priv->path));
|
||||
address->priv->path_len = -1;
|
||||
address->priv->address_type = G_UNIX_SOCKET_ADDRESS_INVALID;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -277,35 +349,71 @@ g_unix_socket_address_new (const gchar *path)
|
||||
* @path: the abstract name
|
||||
* @path_len: the length of @path, or -1
|
||||
*
|
||||
* Creates a new abstract #GUnixSocketAddress for @path.
|
||||
*
|
||||
* Unix domain sockets are generally visible in the filesystem. However, some
|
||||
* systems support abstract socket name which are not visible in the
|
||||
* filesystem and not affected by the filesystem permissions, visibility, etc.
|
||||
*
|
||||
* Note that not all systems (really only Linux) support abstract
|
||||
* socket names, so if you use them on other systems function calls may
|
||||
* return %G_IO_ERROR_NOT_SUPPORTED errors. You can use
|
||||
* g_unix_socket_address_abstract_names_supported() to see if abstract
|
||||
* names are supported.
|
||||
*
|
||||
* If @path_len is -1 then @path is assumed to be a zero terminated
|
||||
* string (although in general abstract names need not be zero terminated
|
||||
* and can have embedded nuls). All bytes after @path_len up to the max size
|
||||
* of an abstract unix domain name is filled with zero bytes.
|
||||
* Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
|
||||
* #GUnixSocketAddress for @path.
|
||||
*
|
||||
* Returns: a new #GUnixSocketAddress
|
||||
*
|
||||
* Since: 2.22
|
||||
* Deprecated: Use g_unix_socket_address_new_with_type().
|
||||
*/
|
||||
GSocketAddress *
|
||||
g_unix_socket_address_new_abstract (const gchar *path,
|
||||
int path_len)
|
||||
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:
|
||||
* @path: the name
|
||||
* @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().
|
||||
*
|
||||
* 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").
|
||||
*
|
||||
* 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
|
||||
* the full size of a <literal>struct sockaddr_un</literal>, although
|
||||
* 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.
|
||||
*
|
||||
* Returns: a new #GUnixSocketAddress
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
GSocketAddress *
|
||||
g_unix_socket_address_new_with_type (const gchar *path,
|
||||
gint path_len,
|
||||
GUnixSocketAddressType type)
|
||||
{
|
||||
GSocketAddress *address;
|
||||
GByteArray *array;
|
||||
|
||||
if (path_len == -1)
|
||||
if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
|
||||
path_len = 0;
|
||||
else if (path_len == -1)
|
||||
path_len = strlen (path);
|
||||
|
||||
array = g_byte_array_sized_new (path_len);
|
||||
@@ -314,7 +422,7 @@ g_unix_socket_address_new_abstract (const gchar *path,
|
||||
|
||||
address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
|
||||
"path-as-array", array,
|
||||
"abstract", TRUE,
|
||||
"address-type", type,
|
||||
NULL);
|
||||
|
||||
g_byte_array_unref (array);
|
||||
@@ -361,20 +469,39 @@ g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
|
||||
return address->priv->path_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_unix_socket_address_get_is_abstract:
|
||||
* @address: a #GInetSocketAddress
|
||||
*
|
||||
* Gets @address's path.
|
||||
* Tests if @address is abstract.
|
||||
*
|
||||
* Returns: %TRUE if the address is abstract, %FALSE otherwise
|
||||
*
|
||||
* Since: 2.22
|
||||
*
|
||||
* Deprecated: Use g_unix_socket_address_get_address_type()
|
||||
*/
|
||||
gboolean
|
||||
g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
|
||||
{
|
||||
return address->priv->abstract;
|
||||
return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
|
||||
address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user