Skip to content

Commit e5a3d79

Browse files
authored
Don't bind to IPv6 when the OS doesn't support it (#64734)
1 parent 2b34e0b commit e5a3d79

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Servers/Kestrel/Core/src/AnyIPListenOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Net;
6+
using System.Net.Sockets;
67
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
78
using Microsoft.Extensions.Logging;
89

@@ -11,7 +12,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core;
1112
internal sealed class AnyIPListenOptions : ListenOptions
1213
{
1314
internal AnyIPListenOptions(int port)
14-
: base(new IPEndPoint(IPAddress.IPv6Any, port))
15+
: base(new IPEndPoint(Socket.OSSupportsIPv6 ? IPAddress.IPv6Any : IPAddress.Any, port))
1516
{
1617
}
1718

src/Servers/Kestrel/Core/test/AddressBinderTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
99
using Microsoft.AspNetCore.InternalTesting;
10+
using Microsoft.DotNet.RemoteExecutor;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Logging;
1213
using Microsoft.Extensions.Logging.Abstractions;
@@ -62,6 +63,36 @@ public void ParseAddressDefaultsToAnyIPOnInvalidIPAddress(string host)
6263
Assert.False(https);
6364
}
6465

66+
[ConditionalFact]
67+
[RemoteExecutionSupported]
68+
public void ParseAddressOnOSWithoutIPv6()
69+
{
70+
var tmpDir = Directory.CreateTempSubdirectory();
71+
72+
try
73+
{
74+
var options = new RemoteInvokeOptions();
75+
options.StartInfo.WorkingDirectory = tmpDir.FullName;
76+
options.StartInfo.EnvironmentVariables["DOTNET_SYSTEM_NET_DISABLEIPV6"] = "1";
77+
78+
using var remoteHandle = RemoteExecutor.Invoke(static () =>
79+
{
80+
Assert.False(Socket.OSSupportsIPv6);
81+
82+
var listenOptions = AddressBinder.ParseAddress($"http://*:80", out var https);
83+
Assert.IsType<AnyIPListenOptions>(listenOptions);
84+
Assert.IsType<IPEndPoint>(listenOptions.EndPoint);
85+
Assert.Equal(IPAddress.Any, listenOptions.IPEndPoint.Address);
86+
Assert.Equal(80, listenOptions.IPEndPoint.Port);
87+
Assert.False(https);
88+
}, options);
89+
}
90+
finally
91+
{
92+
tmpDir.Delete(recursive: true);
93+
}
94+
}
95+
6596
[Fact]
6697
public void ParseAddressLocalhost()
6798
{

src/Servers/Kestrel/Core/test/Microsoft.AspNetCore.Server.Kestrel.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<Reference Include="Microsoft.Extensions.Logging" />
3131
<Reference Include="Microsoft.Extensions.Diagnostics.Testing" />
3232
<Reference Include="Microsoft.Extensions.TimeProvider.Testing" />
33+
<Reference Include="Microsoft.DotNet.RemoteExecutor" />
3334
</ItemGroup>
3435
</Project>

0 commit comments

Comments
 (0)