Skip to content

Commit

Permalink
Fix static Socket.ConnectAsync for DnsEndPoint containing IPv6-mapped…
Browse files Browse the repository at this point in the history
… address string (dotnet#56102)

Fixes dotnet#56099
  • Loading branch information
antonfirsov authored Jul 23, 2021
1 parent bd3fb96 commit 1a3a042
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ async Task Core(MultiConnectSocketAsyncEventArgs internalArgs, Task<IPAddress[]>
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
attemptSocket = tempSocketIPv6 ??= (Socket.OSSupportsIPv6 ? new Socket(AddressFamily.InterNetworkV6, socketType, protocolType) : null);
if (attemptSocket is not null && address.IsIPv4MappedToIPv6)
{
// We need a DualMode socket to connect to an IPv6-mapped IPv4 address.
attemptSocket.DualMode = true;
}
}
else if (address.AddressFamily == AddressFamily.InterNetwork)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ public void Socket_StaticConnectAsync_Success(SocketImplementationType type)
}
}

[Fact]
public void Socket_StaticConnectAsync_IPv6MappedIPv4_Success()
{
using SocketTestServer server = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, IPAddress.Loopback, out int port);

SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new DnsEndPoint("[::FFFF:127.0.0.1]", port);
args.Completed += OnConnectAsyncCompleted;

ManualResetEvent complete = new ManualResetEvent(false);
args.UserToken = complete;

if (Socket.ConnectAsync(SocketType.Stream, ProtocolType.Tcp, args))
{
Assert.True(complete.WaitOne(TestSettings.PassingTestTimeout), "Timed out while waiting for connection");
}

Assert.Equal(SocketError.Success, args.SocketError);
Assert.Null(args.ConnectByNameError);
Assert.NotNull(args.ConnectSocket);
Assert.True(args.ConnectSocket.Connected);
args.ConnectSocket.Dispose();
}

[OuterLoop]
[Fact]
public void Socket_StaticConnectAsync_HostNotFound()
Expand Down

0 comments on commit 1a3a042

Please sign in to comment.