mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-14 14:27:14 +01: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:
parent
5e892de8af
commit
19d8cc3375
@ -1556,9 +1556,12 @@ g_inet_socket_address_get_type
|
|||||||
<FILE>gunixsocketaddress</FILE>
|
<FILE>gunixsocketaddress</FILE>
|
||||||
<TITLE>GUnixSocketAddress</TITLE>
|
<TITLE>GUnixSocketAddress</TITLE>
|
||||||
GUnixSocketAddress
|
GUnixSocketAddress
|
||||||
|
GUnixSocketAddressType
|
||||||
g_unix_socket_address_new
|
g_unix_socket_address_new
|
||||||
g_unix_socket_address_new_abstract
|
g_unix_socket_address_new_abstract
|
||||||
|
g_unix_socket_address_new_with_type
|
||||||
g_unix_socket_address_get_is_abstract
|
g_unix_socket_address_get_is_abstract
|
||||||
|
g_unix_socket_address_get_address_type
|
||||||
g_unix_socket_address_get_path
|
g_unix_socket_address_get_path
|
||||||
g_unix_socket_address_get_path_len
|
g_unix_socket_address_get_path_len
|
||||||
g_unix_socket_address_abstract_names_supported
|
g_unix_socket_address_abstract_names_supported
|
||||||
|
@ -983,8 +983,8 @@ g_socket_type_get_type G_GNUC_CONST
|
|||||||
g_socket_protocol_get_type G_GNUC_CONST
|
g_socket_protocol_get_type G_GNUC_CONST
|
||||||
g_socket_msg_flags_get_type G_GNUC_CONST
|
g_socket_msg_flags_get_type G_GNUC_CONST
|
||||||
g_resolver_error_get_type G_GNUC_CONST
|
g_resolver_error_get_type G_GNUC_CONST
|
||||||
g_zlib_compressor_format_get_type G_GNUC_CONST
|
g_zlib_compressor_format_get_type
|
||||||
g_settings_bind_flags_get_type G_GNUC_CONST
|
g_settings_bind_flags_get_type
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1047,9 +1047,15 @@ g_inet_socket_address_new
|
|||||||
#ifdef G_OS_UNIX
|
#ifdef G_OS_UNIX
|
||||||
g_unix_socket_address_get_type G_GNUC_CONST
|
g_unix_socket_address_get_type G_GNUC_CONST
|
||||||
g_unix_socket_address_new
|
g_unix_socket_address_new
|
||||||
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
g_unix_socket_address_new_abstract
|
g_unix_socket_address_new_abstract
|
||||||
|
#endif
|
||||||
|
g_unix_socket_address_new_with_type
|
||||||
g_unix_socket_address_abstract_names_supported
|
g_unix_socket_address_abstract_names_supported
|
||||||
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
g_unix_socket_address_get_is_abstract
|
g_unix_socket_address_get_is_abstract
|
||||||
|
#endif
|
||||||
|
g_unix_socket_address_get_address_type
|
||||||
g_unix_socket_address_get_path
|
g_unix_socket_address_get_path
|
||||||
g_unix_socket_address_get_path_len
|
g_unix_socket_address_get_path_len
|
||||||
#endif
|
#endif
|
||||||
|
@ -698,6 +698,40 @@ typedef enum {
|
|||||||
G_ZLIB_COMPRESSOR_FORMAT_RAW
|
G_ZLIB_COMPRESSOR_FORMAT_RAW
|
||||||
} GZlibCompressorFormat;
|
} GZlibCompressorFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GUnixSocketAddressType:
|
||||||
|
* @G_UNIX_SOCKET_ADDRESS_INVALID: invalid
|
||||||
|
* @G_UNIX_SOCKET_ADDRESS_ANONYMOUS: anonymous
|
||||||
|
* @G_UNIX_SOCKET_ADDRESS_PATH: a filesystem path
|
||||||
|
* @G_UNIX_SOCKET_ADDRESS_ABSTRACT: an abstract name
|
||||||
|
* @G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED: an abstract name, 0-padded
|
||||||
|
* to the full length of a unix socket name
|
||||||
|
*
|
||||||
|
* The type of name used by a #GUnixSocketAddress.
|
||||||
|
* %G_UNIX_SOCKET_ADDRESS_PATH indicates a traditional unix domain
|
||||||
|
* socket bound to a filesystem path. %G_UNIX_SOCKET_ADDRESS_ANONYMOUS
|
||||||
|
* indicates a socket not bound to any name (eg, a client-side socket,
|
||||||
|
* or a socket created with socketpair()).
|
||||||
|
*
|
||||||
|
* For abstract sockets, there are two incompatible ways of naming
|
||||||
|
* them: the man pages suggest using the entire <literal>struct
|
||||||
|
* sockaddr_un</literal> as the name, padding the unused parts of the
|
||||||
|
* %sun_path field with zeroes; this corresponds to
|
||||||
|
* %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED. However, many programs
|
||||||
|
* instead just use a portion of %sun_path, and pass an appropriate
|
||||||
|
* smaller length to bind() or connect(). This is
|
||||||
|
* %G_UNIX_SOCKET_ADDRESS_ABSTRACT.
|
||||||
|
*
|
||||||
|
* Since: 2.26
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
G_UNIX_SOCKET_ADDRESS_INVALID,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_ANONYMOUS,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_PATH,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
|
||||||
|
} GUnixSocketAddressType;
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __GIO_ENUMS_H__ */
|
#endif /* __GIO_ENUMS_H__ */
|
||||||
|
@ -217,9 +217,13 @@ g_socket_address_new_from_native (gpointer native,
|
|||||||
if (family == AF_INET)
|
if (family == AF_INET)
|
||||||
{
|
{
|
||||||
struct sockaddr_in *addr = (struct sockaddr_in *) native;
|
struct sockaddr_in *addr = (struct sockaddr_in *) native;
|
||||||
GInetAddress *iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
|
GInetAddress *iaddr;
|
||||||
GSocketAddress *sockaddr;
|
GSocketAddress *sockaddr;
|
||||||
|
|
||||||
|
if (len < sizeof (*addr))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin_addr), AF_INET);
|
||||||
sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
|
sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin_port));
|
||||||
g_object_unref (iaddr);
|
g_object_unref (iaddr);
|
||||||
return sockaddr;
|
return sockaddr;
|
||||||
@ -228,9 +232,13 @@ g_socket_address_new_from_native (gpointer native,
|
|||||||
if (family == AF_INET6)
|
if (family == AF_INET6)
|
||||||
{
|
{
|
||||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) native;
|
||||||
GInetAddress *iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
|
GInetAddress *iaddr;
|
||||||
GSocketAddress *sockaddr;
|
GSocketAddress *sockaddr;
|
||||||
|
|
||||||
|
if (len < sizeof (*addr))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
iaddr = g_inet_address_new_from_bytes ((guint8 *) &(addr->sin6_addr), AF_INET6);
|
||||||
sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
|
sockaddr = g_inet_socket_address_new (iaddr, g_ntohs (addr->sin6_port));
|
||||||
g_object_unref (iaddr);
|
g_object_unref (iaddr);
|
||||||
return sockaddr;
|
return sockaddr;
|
||||||
@ -240,11 +248,30 @@ g_socket_address_new_from_native (gpointer native,
|
|||||||
if (family == AF_UNIX)
|
if (family == AF_UNIX)
|
||||||
{
|
{
|
||||||
struct sockaddr_un *addr = (struct sockaddr_un *) native;
|
struct sockaddr_un *addr = (struct sockaddr_un *) native;
|
||||||
|
gint path_len = len - G_STRUCT_OFFSET (struct sockaddr_un, sun_path);
|
||||||
|
|
||||||
if (addr->sun_path[0] == 0)
|
if (path_len == 0)
|
||||||
return g_unix_socket_address_new_abstract (addr->sun_path+1,
|
{
|
||||||
sizeof (addr->sun_path) - 1);
|
return g_unix_socket_address_new_with_type ("", 0,
|
||||||
return g_unix_socket_address_new (addr->sun_path);
|
G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
|
||||||
|
}
|
||||||
|
else if (addr->sun_path[0] == 0)
|
||||||
|
{
|
||||||
|
if (len < sizeof (*addr))
|
||||||
|
{
|
||||||
|
return g_unix_socket_address_new_with_type (addr->sun_path + 1,
|
||||||
|
path_len - 1,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return g_unix_socket_address_new_with_type (addr->sun_path + 1,
|
||||||
|
path_len - 1,
|
||||||
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return g_unix_socket_address_new (addr->sun_path);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -36,6 +36,16 @@
|
|||||||
* @short_description: UNIX GSocketAddress
|
* @short_description: UNIX GSocketAddress
|
||||||
*
|
*
|
||||||
* Support for UNIX-domain (aka local) sockets.
|
* 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,
|
||||||
PROP_PATH_AS_ARRAY,
|
PROP_PATH_AS_ARRAY,
|
||||||
PROP_ABSTRACT,
|
PROP_ABSTRACT,
|
||||||
|
PROP_ADDRESS_TYPE
|
||||||
};
|
};
|
||||||
|
|
||||||
#define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
|
#define UNIX_PATH_MAX sizeof (((struct sockaddr_un *) 0)->sun_path)
|
||||||
@ -62,7 +73,7 @@ struct _GUnixSocketAddressPrivate
|
|||||||
we can guarantee zero termination of abstract
|
we can guarantee zero termination of abstract
|
||||||
pathnames in the get_path() API */
|
pathnames in the get_path() API */
|
||||||
gsize path_len; /* Not including any terminating zeros */
|
gsize path_len; /* Not including any terminating zeros */
|
||||||
gboolean abstract;
|
GUnixSocketAddressType address_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
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 */
|
/* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
|
||||||
len = MIN (array->len, UNIX_PATH_MAX-1);
|
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);
|
memcpy (address->priv->path, array->data, len);
|
||||||
address->priv->path[len] = 0; /* Ensure null-terminated */
|
address->priv->path[len] = 0; /* Ensure null-terminated */
|
||||||
address->priv->path_len = len;
|
address->priv->path_len = len;
|
||||||
@ -107,7 +114,26 @@ g_unix_socket_address_set_property (GObject *object,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PROP_ABSTRACT:
|
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -137,7 +163,13 @@ g_unix_socket_address_get_property (GObject *object,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PROP_ABSTRACT:
|
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -156,7 +188,17 @@ g_unix_socket_address_get_family (GSocketAddress *address)
|
|||||||
static gssize
|
static gssize
|
||||||
g_unix_socket_address_get_native_size (GSocketAddress *address)
|
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
|
static gboolean
|
||||||
@ -167,32 +209,43 @@ g_unix_socket_address_to_native (GSocketAddress *address,
|
|||||||
{
|
{
|
||||||
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
|
GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
|
||||||
struct sockaddr_un *sock;
|
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,
|
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
||||||
_("Not enough space for socket address"));
|
_("Not enough space for socket address"));
|
||||||
return FALSE;
|
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;
|
sock = (struct sockaddr_un *) dest;
|
||||||
|
memset (sock, 0, socklen);
|
||||||
sock->sun_family = AF_UNIX;
|
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;
|
sock->sun_path[0] = 0;
|
||||||
memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
|
memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
strcpy (sock->sun_path, addr->priv->path);
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
@ -229,6 +282,15 @@ g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
|
|||||||
G_PARAM_READWRITE |
|
G_PARAM_READWRITE |
|
||||||
G_PARAM_CONSTRUCT_ONLY |
|
G_PARAM_CONSTRUCT_ONLY |
|
||||||
G_PARAM_STATIC_STRINGS));
|
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_object_class_install_property (gobject_class, PROP_ABSTRACT,
|
||||||
g_param_spec_boolean ("abstract",
|
g_param_spec_boolean ("abstract",
|
||||||
P_("Abstract"),
|
P_("Abstract"),
|
||||||
@ -237,6 +299,15 @@ g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
|
|||||||
G_PARAM_READWRITE |
|
G_PARAM_READWRITE |
|
||||||
G_PARAM_CONSTRUCT_ONLY |
|
G_PARAM_CONSTRUCT_ONLY |
|
||||||
G_PARAM_STATIC_STRINGS));
|
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
|
static void
|
||||||
@ -248,6 +319,7 @@ g_unix_socket_address_init (GUnixSocketAddress *address)
|
|||||||
|
|
||||||
memset (address->priv->path, 0, sizeof (address->priv->path));
|
memset (address->priv->path, 0, sizeof (address->priv->path));
|
||||||
address->priv->path_len = -1;
|
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: the abstract name
|
||||||
* @path_len: the length of @path, or -1
|
* @path_len: the length of @path, or -1
|
||||||
*
|
*
|
||||||
* Creates a new abstract #GUnixSocketAddress for @path.
|
* Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
|
||||||
*
|
* #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.
|
|
||||||
*
|
*
|
||||||
* Returns: a new #GUnixSocketAddress
|
* Returns: a new #GUnixSocketAddress
|
||||||
*
|
*
|
||||||
* Since: 2.22
|
* Deprecated: Use g_unix_socket_address_new_with_type().
|
||||||
*/
|
*/
|
||||||
GSocketAddress *
|
GSocketAddress *
|
||||||
g_unix_socket_address_new_abstract (const gchar *path,
|
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;
|
GSocketAddress *address;
|
||||||
GByteArray *array;
|
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);
|
path_len = strlen (path);
|
||||||
|
|
||||||
array = g_byte_array_sized_new (path_len);
|
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,
|
address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
|
||||||
"path-as-array", array,
|
"path-as-array", array,
|
||||||
"abstract", TRUE,
|
"address-type", type,
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
g_byte_array_unref (array);
|
g_byte_array_unref (array);
|
||||||
@ -361,20 +469,39 @@ g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
|
|||||||
return address->priv->path_len;
|
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:
|
* g_unix_socket_address_get_is_abstract:
|
||||||
* @address: a #GInetSocketAddress
|
* @address: a #GInetSocketAddress
|
||||||
*
|
*
|
||||||
* Gets @address's path.
|
* Tests if @address is abstract.
|
||||||
*
|
*
|
||||||
* Returns: %TRUE if the address is abstract, %FALSE otherwise
|
* Returns: %TRUE if the address is abstract, %FALSE otherwise
|
||||||
*
|
*
|
||||||
* Since: 2.22
|
* Since: 2.22
|
||||||
|
*
|
||||||
|
* Deprecated: Use g_unix_socket_address_get_address_type()
|
||||||
*/
|
*/
|
||||||
gboolean
|
gboolean
|
||||||
g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,11 +55,19 @@ struct _GUnixSocketAddressClass
|
|||||||
GType g_unix_socket_address_get_type (void) G_GNUC_CONST;
|
GType g_unix_socket_address_get_type (void) G_GNUC_CONST;
|
||||||
|
|
||||||
GSocketAddress *g_unix_socket_address_new (const gchar *path);
|
GSocketAddress *g_unix_socket_address_new (const gchar *path);
|
||||||
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
GSocketAddress *g_unix_socket_address_new_abstract (const gchar *path,
|
GSocketAddress *g_unix_socket_address_new_abstract (const gchar *path,
|
||||||
int path_len);
|
gint path_len);
|
||||||
|
#endif
|
||||||
|
GSocketAddress *g_unix_socket_address_new_with_type (const gchar *path,
|
||||||
|
gint path_len,
|
||||||
|
GUnixSocketAddressType type);
|
||||||
const char * g_unix_socket_address_get_path (GUnixSocketAddress *address);
|
const char * g_unix_socket_address_get_path (GUnixSocketAddress *address);
|
||||||
gsize g_unix_socket_address_get_path_len (GUnixSocketAddress *address);
|
gsize g_unix_socket_address_get_path_len (GUnixSocketAddress *address);
|
||||||
|
GUnixSocketAddressType g_unix_socket_address_get_address_type (GUnixSocketAddress *address);
|
||||||
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
gboolean g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address);
|
gboolean g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address);
|
||||||
|
#endif
|
||||||
|
|
||||||
gboolean g_unix_socket_address_abstract_names_supported (void);
|
gboolean g_unix_socket_address_abstract_names_supported (void);
|
||||||
|
|
||||||
|
@ -151,6 +151,7 @@ gschema_compile_SOURCES = gschema-compile.c
|
|||||||
gschema_compile_LDADD = $(progs_ldadd)
|
gschema_compile_LDADD = $(progs_ldadd)
|
||||||
|
|
||||||
EXTRA_DIST += \
|
EXTRA_DIST += \
|
||||||
|
socket-common.c \
|
||||||
org.gtk.test.gschema \
|
org.gtk.test.gschema \
|
||||||
org.gtk.test.gschema.xml \
|
org.gtk.test.gschema.xml \
|
||||||
de.po \
|
de.po \
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
|
#include <gio/gunixsocketaddress.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "socket-common.c"
|
||||||
|
|
||||||
GMainLoop *loop;
|
GMainLoop *loop;
|
||||||
|
|
||||||
gboolean verbose = FALSE;
|
gboolean verbose = FALSE;
|
||||||
@ -11,6 +14,7 @@ gboolean non_blocking = FALSE;
|
|||||||
gboolean use_udp = FALSE;
|
gboolean use_udp = FALSE;
|
||||||
gboolean use_source = FALSE;
|
gboolean use_source = FALSE;
|
||||||
int cancel_timeout = 0;
|
int cancel_timeout = 0;
|
||||||
|
gboolean unix_socket = FALSE;
|
||||||
|
|
||||||
static GOptionEntry cmd_entries[] = {
|
static GOptionEntry cmd_entries[] = {
|
||||||
{"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
|
{"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
|
||||||
@ -23,24 +27,11 @@ static GOptionEntry cmd_entries[] = {
|
|||||||
"Enable non-blocking i/o", NULL},
|
"Enable non-blocking i/o", NULL},
|
||||||
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
||||||
"Use GSource to wait for non-blocking i/o", NULL},
|
"Use GSource to wait for non-blocking i/o", NULL},
|
||||||
|
{"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
|
||||||
|
"Use a unix socket instead of IP", NULL},
|
||||||
{NULL}
|
{NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static char *
|
|
||||||
socket_address_to_string (GSocketAddress *address)
|
|
||||||
{
|
|
||||||
GInetAddress *inet_address;
|
|
||||||
char *str, *res;
|
|
||||||
int port;
|
|
||||||
|
|
||||||
inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
|
|
||||||
str = g_inet_address_to_string (inet_address);
|
|
||||||
port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
|
|
||||||
res = g_strdup_printf ("%s:%d", str, port);
|
|
||||||
g_free (str);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
source_ready (gpointer data,
|
source_ready (gpointer data,
|
||||||
GIOCondition condition)
|
GIOCondition condition)
|
||||||
@ -104,6 +95,7 @@ main (int argc,
|
|||||||
GSocketAddress *src_address;
|
GSocketAddress *src_address;
|
||||||
GSocketAddress *address;
|
GSocketAddress *address;
|
||||||
GSocketType socket_type;
|
GSocketType socket_type;
|
||||||
|
GSocketFamily socket_family;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
GOptionContext *context;
|
GOptionContext *context;
|
||||||
GCancellable *cancellable;
|
GCancellable *cancellable;
|
||||||
@ -124,7 +116,7 @@ main (int argc,
|
|||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
{
|
{
|
||||||
g_printerr ("%s: %s\n", argv[0], "Need to specify hostname");
|
g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,18 +137,38 @@ main (int argc,
|
|||||||
else
|
else
|
||||||
socket_type = G_SOCKET_TYPE_STREAM;
|
socket_type = G_SOCKET_TYPE_STREAM;
|
||||||
|
|
||||||
socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, 0, &error);
|
if (unix_socket)
|
||||||
|
socket_family = G_SOCKET_FAMILY_UNIX;
|
||||||
|
else
|
||||||
|
socket_family = G_SOCKET_FAMILY_IPV4;
|
||||||
|
|
||||||
|
socket = g_socket_new (socket_family, socket_type, 0, &error);
|
||||||
if (socket == NULL)
|
if (socket == NULL)
|
||||||
{
|
{
|
||||||
g_printerr ("%s: %s\n", argv[0], error->message);
|
g_printerr ("%s: %s\n", argv[0], error->message);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
connectable = g_network_address_parse (argv[1], 7777, &error);
|
if (unix_socket)
|
||||||
if (connectable == NULL)
|
|
||||||
{
|
{
|
||||||
g_printerr ("%s: %s\n", argv[0], error->message);
|
GSocketAddress *addr;
|
||||||
return 1;
|
|
||||||
|
addr = socket_address_from_string (argv[1]);
|
||||||
|
if (addr == NULL)
|
||||||
|
{
|
||||||
|
g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
connectable = G_SOCKET_CONNECTABLE (addr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connectable = g_network_address_parse (argv[1], 7777, &error);
|
||||||
|
if (connectable == NULL)
|
||||||
|
{
|
||||||
|
g_printerr ("%s: %s\n", argv[0], error->message);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enumerator = g_socket_connectable_enumerate (connectable);
|
enumerator = g_socket_connectable_enumerate (connectable);
|
||||||
|
56
gio/tests/socket-common.c
Normal file
56
gio/tests/socket-common.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* #included into both socket-client.c and socket-server.c */
|
||||||
|
|
||||||
|
static const char *unix_socket_address_types[] = {
|
||||||
|
"invalid",
|
||||||
|
"anonymous",
|
||||||
|
"path",
|
||||||
|
"abstract",
|
||||||
|
"padded"
|
||||||
|
};
|
||||||
|
|
||||||
|
static char *
|
||||||
|
socket_address_to_string (GSocketAddress *address)
|
||||||
|
{
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
if (G_IS_INET_SOCKET_ADDRESS (address))
|
||||||
|
{
|
||||||
|
GInetAddress *inet_address;
|
||||||
|
char *str;
|
||||||
|
int port;
|
||||||
|
|
||||||
|
inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
|
||||||
|
str = g_inet_address_to_string (inet_address);
|
||||||
|
port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
|
||||||
|
res = g_strdup_printf ("%s:%d", str, port);
|
||||||
|
g_free (str);
|
||||||
|
}
|
||||||
|
else if (G_IS_UNIX_SOCKET_ADDRESS (address))
|
||||||
|
{
|
||||||
|
GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);
|
||||||
|
|
||||||
|
res = g_strdup_printf ("%s:%s",
|
||||||
|
unix_socket_address_types[g_unix_socket_address_get_address_type (uaddr)],
|
||||||
|
g_unix_socket_address_get_path (uaddr));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocketAddress *
|
||||||
|
socket_address_from_string (const char *name)
|
||||||
|
{
|
||||||
|
int i, len;
|
||||||
|
|
||||||
|
for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
|
||||||
|
{
|
||||||
|
len = strlen (unix_socket_address_types[i]);
|
||||||
|
if (!strncmp (name, unix_socket_address_types[i], len) &&
|
||||||
|
name[len] == ':')
|
||||||
|
{
|
||||||
|
return g_unix_socket_address_new_with_type (name + len + 1, -1,
|
||||||
|
(GUnixSocketAddressType)i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
@ -1,6 +1,10 @@
|
|||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
|
#include <gio/gunixsocketaddress.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "socket-common.c"
|
||||||
|
|
||||||
GMainLoop *loop;
|
GMainLoop *loop;
|
||||||
|
|
||||||
@ -11,6 +15,7 @@ gboolean non_blocking = FALSE;
|
|||||||
gboolean use_udp = FALSE;
|
gboolean use_udp = FALSE;
|
||||||
gboolean use_source = FALSE;
|
gboolean use_source = FALSE;
|
||||||
int cancel_timeout = 0;
|
int cancel_timeout = 0;
|
||||||
|
gboolean unix_socket = FALSE;
|
||||||
|
|
||||||
static GOptionEntry cmd_entries[] = {
|
static GOptionEntry cmd_entries[] = {
|
||||||
{"port", 'p', 0, G_OPTION_ARG_INT, &port,
|
{"port", 'p', 0, G_OPTION_ARG_INT, &port,
|
||||||
@ -27,24 +32,11 @@ static GOptionEntry cmd_entries[] = {
|
|||||||
"Enable non-blocking i/o", NULL},
|
"Enable non-blocking i/o", NULL},
|
||||||
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
||||||
"Use GSource to wait for non-blocking i/o", NULL},
|
"Use GSource to wait for non-blocking i/o", NULL},
|
||||||
|
{"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
|
||||||
|
"Use a unix socket instead of IP", NULL},
|
||||||
{NULL}
|
{NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static char *
|
|
||||||
socket_address_to_string (GSocketAddress *address)
|
|
||||||
{
|
|
||||||
GInetAddress *inet_address;
|
|
||||||
char *str, *res;
|
|
||||||
int the_port;
|
|
||||||
|
|
||||||
inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
|
|
||||||
str = g_inet_address_to_string (inet_address);
|
|
||||||
the_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
|
|
||||||
res = g_strdup_printf ("%s:%d", str, the_port);
|
|
||||||
g_free (str);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
source_ready (gpointer data,
|
source_ready (gpointer data,
|
||||||
GIOCondition condition)
|
GIOCondition condition)
|
||||||
@ -108,9 +100,11 @@ main (int argc,
|
|||||||
GSocketAddress *src_address;
|
GSocketAddress *src_address;
|
||||||
GSocketAddress *address;
|
GSocketAddress *address;
|
||||||
GSocketType socket_type;
|
GSocketType socket_type;
|
||||||
|
GSocketFamily socket_family;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
GOptionContext *context;
|
GOptionContext *context;
|
||||||
GCancellable *cancellable;
|
GCancellable *cancellable;
|
||||||
|
char *display_addr;
|
||||||
|
|
||||||
g_thread_init (NULL);
|
g_thread_init (NULL);
|
||||||
|
|
||||||
@ -124,6 +118,12 @@ main (int argc,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (unix_socket && argc != 2)
|
||||||
|
{
|
||||||
|
g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (cancel_timeout)
|
if (cancel_timeout)
|
||||||
{
|
{
|
||||||
cancellable = g_cancellable_new ();
|
cancellable = g_cancellable_new ();
|
||||||
@ -141,7 +141,12 @@ main (int argc,
|
|||||||
else
|
else
|
||||||
socket_type = G_SOCKET_TYPE_STREAM;
|
socket_type = G_SOCKET_TYPE_STREAM;
|
||||||
|
|
||||||
socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, 0, &error);
|
if (unix_socket)
|
||||||
|
socket_family = G_SOCKET_FAMILY_UNIX;
|
||||||
|
else
|
||||||
|
socket_family = G_SOCKET_FAMILY_IPV4;
|
||||||
|
|
||||||
|
socket = g_socket_new (socket_family, socket_type, 0, &error);
|
||||||
|
|
||||||
if (socket == NULL)
|
if (socket == NULL)
|
||||||
{
|
{
|
||||||
@ -152,7 +157,20 @@ main (int argc,
|
|||||||
if (non_blocking)
|
if (non_blocking)
|
||||||
g_socket_set_blocking (socket, FALSE);
|
g_socket_set_blocking (socket, FALSE);
|
||||||
|
|
||||||
src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
|
if (unix_socket)
|
||||||
|
{
|
||||||
|
src_address = socket_address_from_string (argv[1]);
|
||||||
|
if (src_address == NULL)
|
||||||
|
{
|
||||||
|
g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
|
||||||
|
}
|
||||||
|
|
||||||
if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
|
if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
|
||||||
{
|
{
|
||||||
g_printerr ("Can't bind socket: %s\n", error->message);
|
g_printerr ("Can't bind socket: %s\n", error->message);
|
||||||
@ -168,7 +186,16 @@ main (int argc,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_print ("listening on port %d...\n", port);
|
address = g_socket_get_local_address (socket, &error);
|
||||||
|
if (!address)
|
||||||
|
{
|
||||||
|
g_printerr ("Error getting local address: %s\n",
|
||||||
|
error->message);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
display_addr = socket_address_to_string (address);
|
||||||
|
g_print ("listening on %s...\n", display_addr);
|
||||||
|
g_free (display_addr);
|
||||||
|
|
||||||
ensure_condition (socket, "accept", cancellable, G_IO_IN);
|
ensure_condition (socket, "accept", cancellable, G_IO_IN);
|
||||||
new_socket = g_socket_accept (socket, cancellable, &error);
|
new_socket = g_socket_accept (socket, cancellable, &error);
|
||||||
@ -190,8 +217,9 @@ main (int argc,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_print ("got a new connection from %s\n",
|
display_addr = socket_address_to_string (address);
|
||||||
socket_address_to_string (address));
|
g_print ("got a new connection from %s\n", display_addr);
|
||||||
|
g_free(display_addr);
|
||||||
g_object_unref (address);
|
g_object_unref (address);
|
||||||
|
|
||||||
recv_socket = new_socket;
|
recv_socket = new_socket;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include <gio/gunixfdmessage.h>
|
#include <gio/gunixfdmessage.h>
|
||||||
|
#include <gio/gunixsocketaddress.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -40,10 +41,12 @@ create_fd_list (gint *fd_list)
|
|||||||
static void
|
static void
|
||||||
test_fds (void)
|
test_fds (void)
|
||||||
{
|
{
|
||||||
|
GError *err = NULL;
|
||||||
GUnixFDMessage *message;
|
GUnixFDMessage *message;
|
||||||
GUnixFDMessage **mv;
|
GUnixFDMessage **mv;
|
||||||
GUnixFDList *list, *l2;
|
GUnixFDList *list, *l2;
|
||||||
GSocket *sockets[2];
|
GSocket *sockets[2];
|
||||||
|
GSocketAddress *addr;
|
||||||
const gint *peek;
|
const gint *peek;
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
gint fd_list[40];
|
gint fd_list[40];
|
||||||
@ -81,24 +84,32 @@ test_fds (void)
|
|||||||
g_assert_cmpint (stolen[2], ==, sv[2]);
|
g_assert_cmpint (stolen[2], ==, sv[2]);
|
||||||
g_free (stolen);
|
g_free (stolen);
|
||||||
|
|
||||||
g_unix_fd_message_append_fd (message, sv[0], NULL);
|
g_unix_fd_message_append_fd (message, sv[0], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
s = close (sv[0]);
|
s = close (sv[0]);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
g_unix_fd_message_append_fd (message, sv[1], NULL);
|
g_unix_fd_message_append_fd (message, sv[1], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
s = close (sv[1]);
|
s = close (sv[1]);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
|
|
||||||
s = close (g_unix_fd_list_get (list, 0, NULL));
|
s = close (g_unix_fd_list_get (list, 0, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 1, NULL));
|
s = close (g_unix_fd_list_get (list, 1, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 0, NULL));
|
s = close (g_unix_fd_list_get (list, 0, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 1, NULL));
|
s = close (g_unix_fd_list_get (list, 1, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 0, NULL));
|
s = close (g_unix_fd_list_get (list, 0, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 1, NULL));
|
s = close (g_unix_fd_list_get (list, 1, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
|
|
||||||
g_object_unref (message);
|
g_object_unref (message);
|
||||||
@ -111,34 +122,48 @@ test_fds (void)
|
|||||||
s = pipe (sv);
|
s = pipe (sv);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
|
|
||||||
s = g_unix_fd_list_append (list, sv[0], NULL);
|
s = g_unix_fd_list_append (list, sv[0], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, >=, 0);
|
g_assert_cmpint (s, >=, 0);
|
||||||
s = g_unix_fd_list_append (list, sv[1], NULL);
|
s = g_unix_fd_list_append (list, sv[1], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, >=, 0);
|
g_assert_cmpint (s, >=, 0);
|
||||||
|
|
||||||
s = close (sv[0]);
|
s = close (sv[0]);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (sv[1]);
|
s = close (sv[1]);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 0, NULL));
|
s = close (g_unix_fd_list_get (list, 0, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
s = close (g_unix_fd_list_get (list, 1, NULL));
|
s = close (g_unix_fd_list_get (list, 1, &err));
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
|
|
||||||
s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
|
s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
|
||||||
g_assert_cmpint (s, ==, 0);
|
g_assert_cmpint (s, ==, 0);
|
||||||
|
|
||||||
sockets[0] = g_socket_new_from_fd (sv[0], NULL);
|
sockets[0] = g_socket_new_from_fd (sv[0], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert (G_IS_SOCKET (sockets[0]));
|
g_assert (G_IS_SOCKET (sockets[0]));
|
||||||
sockets[1] = g_socket_new_from_fd (sv[1], NULL);
|
sockets[1] = g_socket_new_from_fd (sv[1], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert (G_IS_SOCKET (sockets[1]));
|
g_assert (G_IS_SOCKET (sockets[1]));
|
||||||
|
|
||||||
|
addr = g_socket_get_local_address (sockets[0], &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
|
g_assert (G_IS_UNIX_SOCKET_ADDRESS (addr));
|
||||||
|
g_assert_cmpint (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (addr)), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
|
||||||
|
g_assert_cmpint (g_unix_socket_address_get_path_len (G_UNIX_SOCKET_ADDRESS (addr)), ==, 0);
|
||||||
|
g_object_unref (addr);
|
||||||
|
|
||||||
buffer[0] = 0xff;
|
buffer[0] = 0xff;
|
||||||
ov.buffer = buffer;
|
ov.buffer = buffer;
|
||||||
ov.size = 1;
|
ov.size = 1;
|
||||||
s = g_socket_send_message (sockets[0], NULL, &ov, 1,
|
s = g_socket_send_message (sockets[0], NULL, &ov, 1,
|
||||||
(GSocketControlMessage **) &message,
|
(GSocketControlMessage **) &message,
|
||||||
1, 0, NULL, NULL);
|
1, 0, NULL, &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 1);
|
g_assert_cmpint (s, ==, 1);
|
||||||
g_object_unref (message);
|
g_object_unref (message);
|
||||||
|
|
||||||
@ -149,7 +174,8 @@ test_fds (void)
|
|||||||
iv.size = 1;
|
iv.size = 1;
|
||||||
s = g_socket_receive_message (sockets[1], NULL, &iv, 1,
|
s = g_socket_receive_message (sockets[1], NULL, &iv, 1,
|
||||||
(GSocketControlMessage ***) &mv,
|
(GSocketControlMessage ***) &mv,
|
||||||
&nm, &flags, NULL, NULL);
|
&nm, &flags, NULL, &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
g_assert_cmpint (s, ==, 1);
|
g_assert_cmpint (s, ==, 1);
|
||||||
g_object_unref (sockets[0]);
|
g_object_unref (sockets[0]);
|
||||||
g_object_unref (sockets[1]);
|
g_object_unref (sockets[1]);
|
||||||
@ -164,7 +190,8 @@ test_fds (void)
|
|||||||
|
|
||||||
peek = g_unix_fd_list_peek_fds (list, &s);
|
peek = g_unix_fd_list_peek_fds (list, &s);
|
||||||
g_assert_cmpint (s, ==, 2);
|
g_assert_cmpint (s, ==, 2);
|
||||||
sv[0] = g_unix_fd_list_get (list, 1, NULL);
|
sv[0] = g_unix_fd_list_get (list, 1, &err);
|
||||||
|
g_assert_no_error (err);
|
||||||
|
|
||||||
strcpy (buffer, "failure to say failure to say 'i love gnome-panel!'.");
|
strcpy (buffer, "failure to say failure to say 'i love gnome-panel!'.");
|
||||||
s = write (sv[0], buffer, strlen (buffer) + 1);
|
s = write (sv[0], buffer, strlen (buffer) + 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user