glib/gio/tests/socket-common.c
Dan Winship 19d8cc3375 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
2010-04-22 11:54:41 -04:00

57 lines
1.4 KiB
C

/* #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;
}