Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ReceiveFrom with dual mode socket #92086

Merged
merged 3 commits into from
Sep 15, 2023
Merged

fix ReceiveFrom with dual mode socket #92086

merged 3 commits into from
Sep 15, 2023

Conversation

wfurt
Copy link
Member

@wfurt wfurt commented Sep 14, 2023

Fixes #92006

This is regression against 7.0
It seems like the old internal code handled cases when IPv4 EndPoint was passed to ReceiveFrom on dual mode socket and the received address is actually IPv4 mapped into IPv6. But that case fails now and we fail to set RemoteEndPoint leaving whatever was there - in case of the provided repro creating loop or possibly sending message to wrong peer.

After thinking about it I see no reason why IPEndPoint.Create needs to really care about AddressFamily as long as it is IP. The documentation claims that it needs to be correct type but it does not talk about IPv4 vs IPv6. And we allocate new EndPoint so the actual value of the old one does not matter. It will also make it easier to consume new API from #88970 as we exposed SocketAddresses to consumers and they may need to eat with it.

The regression was introduced by PR #89841

@wfurt wfurt added this to the 9.0.0 milestone Sep 14, 2023
@wfurt wfurt self-assigned this Sep 14, 2023
@ghost
Copy link

ghost commented Sep 14, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

fixes #92006
This is regression from #89841. It seems like the old internal code handled cases when IPv4 EP was passed to ReceiveFrom on dual mode socket and the received address is actually IPv4 mapped into IPv6. But that case failed now and we would fail to set RemoteEndPoint leaving what ever was there - in case of the provided repro creating loop or possibly sending message to wrong peer.

After thinking about it I see no reason why IPEndPoint.Create needs to really care about AddressFamily as long as it is IP. The documentation claims that it needs to be correct type but it does not talk about IPv4 vs IPv6. And we allocate new EndPoint so the actual value of the old one does not matter. It will also make it easier to consume new API from #88970 as we exposed SocketAddresses to consumers and they may need to eat with it.

Author: wfurt
Assignees: wfurt
Labels:

area-System.Net.Sockets

Milestone: 9.0.0

{
throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, socketAddress.Family.ToString(), GetType().FullName, AddressFamily.ToString()), nameof(socketAddress));
throw new ArgumentException(SR.Format(SR.net_InvalidAddressFamily, socketAddress.Family.ToString(), GetType().FullName), nameof(socketAddress));
}

int minSize = AddressFamily == AddressFamily.InterNetworkV6 ? SocketAddress.IPv6AddressSize : SocketAddress.IPv4AddressSize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're no longer requiring that socketAddress.Family == AddressFamily, does this size check still matter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to but than we can possibly get exception when we access the underlying buffer and it is too small. So I feel the extra check provides better feedback.

Comment on lines +930 to 933
if (_remoteEndPoint!.AddressFamily == AddressFamily.InterNetworkV6 && _socketAddress!.Family == AddressFamily.InterNetwork)
{
_remoteEndPoint = _remoteEndPoint!.Create(_socketAddress);
_remoteEndPoint = new IPEndPoint(_socketAddress.GetIPAddress().MapToIPv6(), _socketAddress.GetPort());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something IPEndPoint.Create(SocketAddress) should be doing instead of doing it at this one call site?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is tricky - we have cases when the address is IPv4 and Create would just work - but we want IPv6 IPv4Mapped addresses for DualMode sockets. I guess mostly for compatibility reasons so this is handled as special case.

{
_remoteEndPoint = new IPEndPoint(_socketAddress.GetIPAddress().MapToIPv6(), _socketAddress.GetPort());
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here: if this logic were moved into IPEndPoint.Create, would that a) address the duplication between these two call sites and b) potentially avoid bugs elsewhere? Or would that introduce problems?

@wfurt wfurt merged commit a6c64c3 into dotnet:main Sep 15, 2023
105 checks passed
@wfurt wfurt deleted the dualUdp branch September 15, 2023 04:40
@karelz
Copy link
Member

karelz commented Sep 15, 2023

/backport to release/8.0

@github-actions
Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/runtime/actions/runs/6195384920

@ghost ghost locked as resolved and limited conversation to collaborators Oct 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect port from Socket.ReceiveFromAsync(SocketAsyncEventArgs)
3 participants