After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 627171 - g_socket_new_from_fd() doesn't set the right protocol
g_socket_new_from_fd() doesn't set the right protocol
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: network
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2010-08-17 15:50 UTC by David Zeuthen (not reading bugmail)
Modified: 2010-10-22 18:59 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Test case (2.18 KB, text/plain)
2010-08-17 15:53 UTC, David Zeuthen (not reading bugmail)
  Details
GSocket: set protocol when using g_socket_new_from_fd() (2.52 KB, patch)
2010-08-17 22:36 UTC, Dan Winship
none Details | Review
GSocket: set protocol when using g_socket_new_from_fd() (5.32 KB, patch)
2010-08-17 22:37 UTC, Dan Winship
committed Details | Review

Description David Zeuthen (not reading bugmail) 2010-08-17 15:50:40 UTC
Will attach a test case that demonstrates this.
Comment 1 David Zeuthen (not reading bugmail) 2010-08-17 15:53:31 UTC
Created attachment 168103 [details]
Test case

Result of running these tests

 $ ./socket
 /socket/unix-from-fd: **
 ERROR:socket.c:47:test_unix_from_fd: assertion failed (g_socket_get_protocol (s) == G_SOCKET_PROTOCOL_DEFAULT): (-1 == 0)
Aborted (core dumped)

 $ ./socket -p /socket/unix-connection
 /socket/unix-connection: **
 ERROR:socket.c:66:test_unix_connection: assertion failed: (G_IS_UNIX_CONNECTION (c))
 Aborted (core dumped)

As you can see, the former is the cause of the latter. So if we fix the former then the latter should be fixed too.

Actually, we don't really have any GSocket test cases (only samples) - this could be the beginning of a set of tests...
Comment 2 Dan Winship 2010-08-17 16:16:15 UTC
so the problem here is that g_socket_details_from_fd() sets priv->type and priv->family, but not priv->protocol, which there doesn't seem to be any easy portable method to figure out.

On Linux, we can use the SO_PROTOCOL sockopt and we're done. For non-Linux...

David points out that G_SOCKET_PROTOCOL_DEFAULT is the only usable value for G_SOCKET_FAMILY_UNIX, so we can fill that in automatically in that case.

For IP sockets, our best bet is probably to try an IPPROTO_TCP or IPPROTO_UDP sockopt and see whether we get back an answer, or ENOPROTOOPT. For TCP, TCP_NODELAY is totally portable, and it looks like SCTP defines SCTP_NODELAY as well. There don't seem to be any portable UDP sockopts, but we could just use process of elimination...
Comment 3 Dan Winship 2010-08-17 16:17:11 UTC
actually i guess we could just make the obvious mappings from priv->type to priv->protocol in the IP case as well...
Comment 4 Dan Winship 2010-08-17 22:36:46 UTC
Created attachment 168149 [details] [review]
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).
Comment 5 Dan Winship 2010-08-17 22:37:45 UTC
Created attachment 168150 [details] [review]
GSocket: set protocol when using g_socket_new_from_fd()

oops, forgot to git add the test program
Comment 6 Dan Winship 2010-10-22 18:59:06 UTC
belatedly pushed this

Attachment 168150 [details] pushed as 87d0610 - GSocket: set protocol when using g_socket_new_from_fd()