[glib] GSocket: set protocol when using g_socket_new_from_fd()



commit 87d06109ab325a4a68e151015381e7e1b33bdf7d
Author: Dan Winship <danw gnome org>
Date:   Tue Aug 17 18:34:13 2010 -0400

    GSocket: set protocol when using g_socket_new_from_fd()
    
    Otherwise, attempting to create a GSocketConnection from the socket
    will likely return the wrong type, since the protocol won't match any
    of the registered subtypes.
    
    Also add the start of a GSocket test program (from davidz).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=627171

 gio/gsocket.c         |   29 ++++++++++++++--
 gio/tests/.gitignore  |    1 +
 gio/tests/Makefile.am |    6 +++-
 gio/tests/socket.c    |   86 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+), 5 deletions(-)
---
diff --git a/gio/gsocket.c b/gio/gsocket.c
index dbedc37..9ff3597 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -377,13 +377,34 @@ g_socket_details_from_fd (GSocket *socket)
     {
      case G_SOCKET_FAMILY_IPV4:
      case G_SOCKET_FAMILY_IPV6:
+       socket->priv->family = address.ss_family;
+       switch (socket->priv->type)
+	 {
+	 case G_SOCKET_TYPE_STREAM:
+	   socket->priv->protocol = G_SOCKET_PROTOCOL_TCP;
+	   break;
+
+	 case G_SOCKET_TYPE_DATAGRAM:
+	   socket->priv->protocol = G_SOCKET_PROTOCOL_UDP;
+	   break;
+
+	 case G_SOCKET_TYPE_SEQPACKET:
+	   socket->priv->protocol = G_SOCKET_PROTOCOL_SCTP;
+	   break;
+
+	 default:
+	   break;
+	 }
+       break;
+
      case G_SOCKET_FAMILY_UNIX:
-      socket->priv->family = address.ss_family;
-      break;
+       socket->priv->family = G_SOCKET_FAMILY_UNIX;
+       socket->priv->protocol = G_SOCKET_PROTOCOL_DEFAULT;
+       break;
 
      default:
-      socket->priv->family = G_SOCKET_FAMILY_INVALID;
-      break;
+       socket->priv->family = G_SOCKET_FAMILY_INVALID;
+       break;
     }
 
   if (socket->priv->family != G_SOCKET_FAMILY_INVALID)
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index 692e258..2613ef2 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -62,6 +62,7 @@ resolver
 send-data
 simple-async-result
 sleepy-stream
+socket
 socket-client
 socket-server
 srvtarget
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 8776b0c..6bd5f9d 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -42,6 +42,7 @@ TEST_PROGS +=	 		\
 	gdbus-addresses		\
 	network-address		\
 	gdbus-message		\
+	socket			\
 	$(NULL)
 
 if OS_UNIX
@@ -196,6 +197,9 @@ srvtarget_LDADD		  = $(progs_ldadd)
 network_address_SOURCE	  = network-address.c
 network_address_LDADD	  = $(progs_ldadd)
 
+socket_SOURCE		  = socket.c
+socket_LDADD		  = $(progs_ldadd)
+
 contexts_SOURCES	  = contexts.c
 contexts_LDADD		  = $(progs_ldadd) \
 	$(top_builddir)/gthread/libgthread-2.0.la
@@ -392,7 +396,7 @@ EXTRA_DIST += \
 	appinfo-test-gnome.desktop	\
 	appinfo-test-notgnome.desktop	\
 	gdbus-testserver.py
- 
+
 MISC_STUFF = test.mo
 
 test.mo: de.po
diff --git a/gio/tests/socket.c b/gio/tests/socket.c
new file mode 100644
index 0000000..9c4c0d6
--- /dev/null
+++ b/gio/tests/socket.c
@@ -0,0 +1,86 @@
+/* GLib testing framework examples and tests
+ *
+ * Copyright (C) 2008-2010 Red Hat, Inc.
+ *
+ * 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
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * 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
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: David Zeuthen <davidz redhat com>
+ */
+
+#include <gio/gio.h>
+
+#ifdef G_OS_UNIX
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <gio/gunixconnection.h>
+#endif
+
+#ifdef G_OS_UNIX
+static void
+test_unix_from_fd (void)
+{
+  gint fd;
+  GError *error;
+  GSocket *s;
+
+  fd = socket (AF_UNIX, SOCK_STREAM, 0);
+  g_assert_cmpint (fd, !=, -1);
+
+  error = NULL;
+  s = g_socket_new_from_fd (fd, &error);
+  g_assert_no_error (error);
+  g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
+  g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
+  g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
+  g_object_unref (s);
+}
+
+static void
+test_unix_connection (void)
+{
+  gint fd;
+  GError *error;
+  GSocket *s;
+  GSocketConnection *c;
+
+  fd = socket (AF_UNIX, SOCK_STREAM, 0);
+  g_assert_cmpint (fd, !=, -1);
+
+  error = NULL;
+  s = g_socket_new_from_fd (fd, &error);
+  g_assert_no_error (error);
+  c = g_socket_connection_factory_create_connection (s);
+  g_assert (G_IS_UNIX_CONNECTION (c));
+  g_object_unref (c);
+  g_object_unref (s);
+}
+#endif /* G_OS_UNIX */
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+#ifdef G_OS_UNIX
+  g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
+  g_test_add_func ("/socket/unix-connection", test_unix_connection);
+#endif
+
+  return g_test_run();
+}
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]