mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-22 01:47:52 +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:
@@ -151,6 +151,7 @@ gschema_compile_SOURCES = gschema-compile.c
|
||||
gschema_compile_LDADD = $(progs_ldadd)
|
||||
|
||||
EXTRA_DIST += \
|
||||
socket-common.c \
|
||||
org.gtk.test.gschema \
|
||||
org.gtk.test.gschema.xml \
|
||||
de.po \
|
||||
|
@@ -1,9 +1,12 @@
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixsocketaddress.h>
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "socket-common.c"
|
||||
|
||||
GMainLoop *loop;
|
||||
|
||||
gboolean verbose = FALSE;
|
||||
@@ -11,6 +14,7 @@ gboolean non_blocking = FALSE;
|
||||
gboolean use_udp = FALSE;
|
||||
gboolean use_source = FALSE;
|
||||
int cancel_timeout = 0;
|
||||
gboolean unix_socket = FALSE;
|
||||
|
||||
static GOptionEntry cmd_entries[] = {
|
||||
{"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
|
||||
@@ -23,24 +27,11 @@ static GOptionEntry cmd_entries[] = {
|
||||
"Enable non-blocking i/o", NULL},
|
||||
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
||||
"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}
|
||||
};
|
||||
|
||||
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
|
||||
source_ready (gpointer data,
|
||||
GIOCondition condition)
|
||||
@@ -104,6 +95,7 @@ main (int argc,
|
||||
GSocketAddress *src_address;
|
||||
GSocketAddress *address;
|
||||
GSocketType socket_type;
|
||||
GSocketFamily socket_family;
|
||||
GError *error = NULL;
|
||||
GOptionContext *context;
|
||||
GCancellable *cancellable;
|
||||
@@ -124,7 +116,7 @@ main (int argc,
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -145,18 +137,38 @@ main (int argc,
|
||||
else
|
||||
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)
|
||||
{
|
||||
g_printerr ("%s: %s\n", argv[0], error->message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
connectable = g_network_address_parse (argv[1], 7777, &error);
|
||||
if (connectable == NULL)
|
||||
if (unix_socket)
|
||||
{
|
||||
g_printerr ("%s: %s\n", argv[0], error->message);
|
||||
return 1;
|
||||
GSocketAddress *addr;
|
||||
|
||||
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);
|
||||
|
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/gunixsocketaddress.h>
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "socket-common.c"
|
||||
|
||||
GMainLoop *loop;
|
||||
|
||||
@@ -11,6 +15,7 @@ gboolean non_blocking = FALSE;
|
||||
gboolean use_udp = FALSE;
|
||||
gboolean use_source = FALSE;
|
||||
int cancel_timeout = 0;
|
||||
gboolean unix_socket = FALSE;
|
||||
|
||||
static GOptionEntry cmd_entries[] = {
|
||||
{"port", 'p', 0, G_OPTION_ARG_INT, &port,
|
||||
@@ -27,24 +32,11 @@ static GOptionEntry cmd_entries[] = {
|
||||
"Enable non-blocking i/o", NULL},
|
||||
{"use-source", 's', 0, G_OPTION_ARG_NONE, &use_source,
|
||||
"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}
|
||||
};
|
||||
|
||||
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
|
||||
source_ready (gpointer data,
|
||||
GIOCondition condition)
|
||||
@@ -108,9 +100,11 @@ main (int argc,
|
||||
GSocketAddress *src_address;
|
||||
GSocketAddress *address;
|
||||
GSocketType socket_type;
|
||||
GSocketFamily socket_family;
|
||||
GError *error = NULL;
|
||||
GOptionContext *context;
|
||||
GCancellable *cancellable;
|
||||
char *display_addr;
|
||||
|
||||
g_thread_init (NULL);
|
||||
|
||||
@@ -124,6 +118,12 @@ main (int argc,
|
||||
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)
|
||||
{
|
||||
cancellable = g_cancellable_new ();
|
||||
@@ -141,7 +141,12 @@ main (int argc,
|
||||
else
|
||||
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)
|
||||
{
|
||||
@@ -152,7 +157,20 @@ main (int argc,
|
||||
if (non_blocking)
|
||||
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))
|
||||
{
|
||||
g_printerr ("Can't bind socket: %s\n", error->message);
|
||||
@@ -168,7 +186,16 @@ main (int argc,
|
||||
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);
|
||||
new_socket = g_socket_accept (socket, cancellable, &error);
|
||||
@@ -190,8 +217,9 @@ main (int argc,
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_print ("got a new connection from %s\n",
|
||||
socket_address_to_string (address));
|
||||
display_addr = socket_address_to_string (address);
|
||||
g_print ("got a new connection from %s\n", display_addr);
|
||||
g_free(display_addr);
|
||||
g_object_unref (address);
|
||||
|
||||
recv_socket = new_socket;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixfdmessage.h>
|
||||
#include <gio/gunixsocketaddress.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -40,10 +41,12 @@ create_fd_list (gint *fd_list)
|
||||
static void
|
||||
test_fds (void)
|
||||
{
|
||||
GError *err = NULL;
|
||||
GUnixFDMessage *message;
|
||||
GUnixFDMessage **mv;
|
||||
GUnixFDList *list, *l2;
|
||||
GSocket *sockets[2];
|
||||
GSocketAddress *addr;
|
||||
const gint *peek;
|
||||
char buffer[1024];
|
||||
gint fd_list[40];
|
||||
@@ -81,24 +84,32 @@ test_fds (void)
|
||||
g_assert_cmpint (stolen[2], ==, sv[2]);
|
||||
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]);
|
||||
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]);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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_object_unref (message);
|
||||
@@ -111,34 +122,48 @@ test_fds (void)
|
||||
s = pipe (sv);
|
||||
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);
|
||||
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);
|
||||
|
||||
s = close (sv[0]);
|
||||
g_assert_cmpint (s, ==, 0);
|
||||
s = close (sv[1]);
|
||||
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);
|
||||
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);
|
||||
|
||||
s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
|
||||
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]));
|
||||
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]));
|
||||
|
||||
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;
|
||||
ov.buffer = buffer;
|
||||
ov.size = 1;
|
||||
s = g_socket_send_message (sockets[0], NULL, &ov, 1,
|
||||
(GSocketControlMessage **) &message,
|
||||
1, 0, NULL, NULL);
|
||||
1, 0, NULL, &err);
|
||||
g_assert_no_error (err);
|
||||
g_assert_cmpint (s, ==, 1);
|
||||
g_object_unref (message);
|
||||
|
||||
@@ -149,7 +174,8 @@ test_fds (void)
|
||||
iv.size = 1;
|
||||
s = g_socket_receive_message (sockets[1], NULL, &iv, 1,
|
||||
(GSocketControlMessage ***) &mv,
|
||||
&nm, &flags, NULL, NULL);
|
||||
&nm, &flags, NULL, &err);
|
||||
g_assert_no_error (err);
|
||||
g_assert_cmpint (s, ==, 1);
|
||||
g_object_unref (sockets[0]);
|
||||
g_object_unref (sockets[1]);
|
||||
@@ -164,7 +190,8 @@ test_fds (void)
|
||||
|
||||
peek = g_unix_fd_list_peek_fds (list, &s);
|
||||
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!'.");
|
||||
s = write (sv[0], buffer, strlen (buffer) + 1);
|
||||
|
Reference in New Issue
Block a user