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

Environment variable & AppCtx switch to force-disable IPV6 #45893

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@

namespace System.Net
{
internal static class SocketProtocolSupportPal
internal static partial class SocketProtocolSupportPal
{
public static bool OSSupportsIPv6 { get; } = IsSupported(AddressFamily.InterNetworkV6);
public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);

private static unsafe bool IsSupported(AddressFamily af)
{
IntPtr invalid = (IntPtr)(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

namespace System.Net
{
internal static class SocketProtocolSupportPal
internal static partial class SocketProtocolSupportPal
{
public static bool OSSupportsIPv6 { get; } = IsSupported(AddressFamily.InterNetworkV6);
public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);

private static bool IsSupported(AddressFamily af)
{
Interop.Winsock.EnsureInitialized();
Expand Down
31 changes: 31 additions & 0 deletions src/libraries/Common/src/System/Net/SocketProtocolSupportPal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net.Sockets;

namespace System.Net
{
internal static partial class SocketProtocolSupportPal
{
public static bool OSSupportsIPv6 { get; } = !IsIPV6Disabled && IsSupported(AddressFamily.InterNetworkV6);
public static bool OSSupportsIPv4 { get; } = IsSupported(AddressFamily.InterNetwork);
public static bool OSSupportsUnixDomainSockets { get; } = IsSupported(AddressFamily.Unix);

private static bool IsIPV6Disabled
{
get
{
const string DisableIpv6AppContextSettingName = "System.Net.Sockets.DisableIPv6";
const string DisableIpv6EnvironmentVariableName = "DOTNET_SYSTEM_NET_SOCKETS_DISABLEIPV6";

if (AppContext.TryGetSwitch(DisableIpv6AppContextSettingName, out bool disableIpV6))
{
return disableIpV6;
}

string? envVar = Environment.GetEnvironmentVariable(DisableIpv6EnvironmentVariableName);
return envVar != null && (envVar.Equals("true", StringComparison.OrdinalIgnoreCase) || envVar.Equals("1"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
Expand Down Expand Up @@ -72,6 +72,8 @@
Link="Common\System\Net\RangeValidationHelpers.cs" />
<Compile Include="$(CommonPath)System\Net\SocketAddress.cs"
Link="Common\System\Net\SocketAddress.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.cs"
Link="Common\System\Net\SocketProtocolSupportPal.cs" />
<Compile Include="$(CommonPath)System\Net\TcpValidationHelpers.cs"
Link="Common\System\Net\TcpValidationHelpers.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
Expand Down Expand Up @@ -109,7 +111,7 @@
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Windows.cs"
Link="Common\System\Net\SocketAddressPal.Windows.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Windows.cs"
Link="Common\System\Net\SocketProtocolSupportPal.Windows" />
Link="Common\System\Net\SocketProtocolSupportPal.Windows.cs" />
<!-- Interop -->
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
Expand Down Expand Up @@ -208,7 +210,7 @@
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
Link="Common\System\Net\SocketAddressPal.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Unix.cs"
Link="Common\System\Net\SocketProtocolSupportPal.Unix" />
Link="Common\System\Net\SocketProtocolSupportPal.Unix.cs" />
<Compile Include="$(CommonPath)System\Net\Sockets\SocketErrorPal.Unix.cs"
Link="Common\System\Net\Sockets\SocketErrorPal.Unix" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Net.Sockets.Tests
Expand Down Expand Up @@ -206,5 +207,31 @@ public void IOControl_UnknownValues_Unix_Throws()
}
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void DisableIpv6(bool appContextOrEnvironmentVariable)
{
RemoteInvokeOptions options = null;
if (!appContextOrEnvironmentVariable)
{
options = new RemoteInvokeOptions();
options.StartInfo.EnvironmentVariables.Add("DOTNET_SYSTEM_NET_SOCKETS_DISABLEIPV6", "1");
}

RemoteExecutor.Invoke(static appContextOrEnvironmentVariableInner =>
{
if (bool.Parse(appContextOrEnvironmentVariableInner))
{
AppContext.SetSwitch("System.Net.Sockets.DisableIPv6", true);
}

Assert.False(Socket.OSSupportsIPv6);

using Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
Assert.Equal(AddressFamily.InterNetwork, socket.AddressFamily);
}, appContextOrEnvironmentVariable.ToString(), options).Dispose();
}
}
}