Repository navigation
fixes for Socket constructor from handle - #34986
Merged
Merged
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
_isListeningand has some functional impact. It was curios that theSO_ACCEPTCONNexists in headers but thegetsockopt()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 backstruct socket_infowith 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 trulyUnknown.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 thatgetpeername()andgetsockname()return more data than necessary. However, first byte ofsun_pathinsockaddr_unis\0so every C code would see that as empty string. The "@" was puzzling me for a while. It comes from here:runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UnixDomainSocketEndPoint.cs
Lines 109 to 120 in 03fc3ab
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.