Skip to content

fixes for Socket constructor from handle - #34986

Merged
stephentoub merged 22 commits into
dotnet:masterfrom
wfurt:socketHandle
Apr 27, 2020
Merged

stephentoub merged 22 commits into
dotnet:masterfrom
wfurt:socketHandle

Conversation

@wfurt

@wfurt wfurt commented Apr 15, 2020

Copy link
Copy Markdown
Member

This is follow-up on #34727. There were some shortommings in the implementation, mostly on OSX.

OSX lacks SO_PROTOCOL, SO_DOMAIN and SO_ACCEPTCONN. We could get AddressFamily from LocalEndPoint and protocol is really not used in managed code. SO_ACCEPTCONN sets value for _isListening and has some functional impact. It was curios that the SO_ACCEPTCONN exists in headers but the getsockopt() with it was always failing.

While going through the Darwin kernel code I bump to function similar to Window's SO_PROTOCOL_INFOW. We can get back struct socket_info with all the glory details.

I did not want to duplicate all the structures in Interop so I decided to move it to the PAL and avoid calling SO_ACCEPTCONN from managed code.

I updated Ctor_SafeHandle_Listening_Success(). Since listener and listenerCopy share same OS handle, calling GetSocketOption() will always return same value. I replaced it with attempt to connect to verify that _isListening is actually set and Connect() throws. If that would get through, we would get socket error instead.

I also did more testing on Linux with "other" socket types e.g. Netlink. SocketAddressPal.GetAddressFamily() will throw if AddressFamily is truly Unknown.
Since LoadSocketTypeFromHandle() can now get all the info, I see no reason to keep it in place.

When Socket is creating using legacy constructor, accessing LocalEndPoint and RemoteEndPoint will return null. When it is created from handle, accessing RemoteEndPoint will throw as getpeername() will fail if socket is not connected. This is because _rightEndPoint is always set. I the extended check to verify that socket is connected before calling getpeername(). This should be set in all cases when remote address may be available.

contributes to #34861

Last mystery was the "@\0\0\0\0\0\0\0\0\0\0\0\0\0" mentioned in the test and observed when I was testing with socketpair() on OSX. General problem is that getpeername() and getsockname() return more data than necessary. However, first byte of sun_path in sockaddr_un is \0 so every C code would see that as empty string. The "@" was puzzling me for a while. It comes from here:

public override string ToString()
{
bool isAbstract = IsAbstract(_path);
if (isAbstract)
{
return string.Concat("@", _path.AsSpan(1));
}
else
{
return _path;
}
}

When sun_path is empty, IsAbstract is True so ToString() will return "@".

I was wonderign if we should fix it up for OSX but since the relevant functions don't live in PAL, I decided not to. I would be open to suggestions as it would be nice IMHO to get this consent across platforms.

Loading
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Net.Sockets enhancement Product code improvement that does NOT require public API changes/additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants