GNOME Bugzilla – Bug 627171
g_socket_new_from_fd() doesn't set the right protocol
Last modified: 2010-10-22 18:59:09 UTC
Will attach a test case that demonstrates this.
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...
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...
actually i guess we could just make the obvious mappings from priv->type to priv->protocol in the IP case as well...
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).
Created attachment 168150 [details] [review] GSocket: set protocol when using g_socket_new_from_fd() oops, forgot to git add the test program
belatedly pushed this Attachment 168150 [details] pushed as 87d0610 - GSocket: set protocol when using g_socket_new_from_fd()