diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs
index 5cc0c19772753..888c243521f83 100644
--- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs
+++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs
@@ -14,6 +14,11 @@ internal static partial class Sys
internal static unsafe string GetHostName()
{
+ if (OperatingSystem.IsWasi())
+ {
+ return "localhost";
+ }
+
const int HOST_NAME_MAX = 255;
const int ArrLength = HOST_NAME_MAX + 1;
diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.HostEntry.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.HostEntry.cs
index b8bc38524a59c..8d00d95ebf214 100644
--- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.HostEntry.cs
+++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.HostEntry.cs
@@ -21,6 +21,7 @@ internal enum GetAddrInfoErrorFlags : int
EAI_BADARG = 6, // One or more input arguments were invalid.
EAI_NOMORE = 7, // No more entries are present in the list.
EAI_MEMORY = 8, // Out of memory.
+ EAI_SYSTEM = 9, // Other system error; errno is set to indicate the error.
}
[StructLayout(LayoutKind.Sequential)]
diff --git a/src/libraries/System.Net.NameResolution/Directory.Build.props b/src/libraries/System.Net.NameResolution/Directory.Build.props
index bc799605d32ed..ce244cbea5619 100644
--- a/src/libraries/System.Net.NameResolution/Directory.Build.props
+++ b/src/libraries/System.Net.NameResolution/Directory.Build.props
@@ -3,7 +3,6 @@
Microsoft
true
-
- browser;wasi
+ browser
\ No newline at end of file
diff --git a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
index c5c8b4ffc9a58..582fe6cc88241 100644
--- a/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
+++ b/src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
@@ -9,14 +9,14 @@
$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))
- SR.SystemNetNameResolution_PlatformNotSupported
- ExcludeApiList.PNSE.Browser.txt
+ SR.SystemNetNameResolution_PlatformNotSupported
+ ExcludeApiList.PNSE.Browser.txt
-
+
@@ -80,6 +80,8 @@
Link="Common\Interop\CoreLib\Unix\Interop.Errors.cs" />
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
index c8a526ccc4b3c..5bedc17d253f8 100644
--- a/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
+++ b/src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
@@ -7,6 +7,7 @@
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
+using System.Runtime.Versioning;
namespace System.Net
{
@@ -37,6 +38,8 @@ public static string GetHostName()
public static IPHostEntry GetHostEntry(IPAddress address)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(address);
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -68,7 +71,7 @@ public static IPHostEntry GetHostEntry(string hostNameOrAddress, AddressFamily f
// See if it's an IP Address.
IPHostEntry ipHostEntry;
- if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
+ if (NameResolutionPal.SupportsGetNameInfo && IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
{
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
{
@@ -147,6 +150,8 @@ public static Task GetHostEntryAsync(string hostNameOrAddress, Addr
public static Task GetHostEntryAsync(IPAddress address)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(address);
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -156,6 +161,8 @@ public static Task GetHostEntryAsync(IPAddress address)
}
return RunAsync(static (s, activity) => {
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
IPHostEntry ipHostEntry = GetHostEntryCore((IPAddress)s, AddressFamily.Unspecified, activity);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info((IPAddress)s, $"{ipHostEntry} with {ipHostEntry.AddressList.Length} entries");
return ipHostEntry;
@@ -170,6 +177,8 @@ public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCall
public static IPHostEntry EndGetHostEntry(IAsyncResult asyncResult)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(asyncResult);
return TaskToAsyncResult.End(asyncResult);
@@ -244,6 +253,8 @@ public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, Async
public static IPAddress[] EndGetHostAddresses(IAsyncResult asyncResult)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(asyncResult);
return TaskToAsyncResult.End(asyncResult);
@@ -269,6 +280,8 @@ public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? re
[Obsolete("EndGetHostByName has been deprecated. Use EndGetHostEntry instead.")]
public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(asyncResult);
return TaskToAsyncResult.End(asyncResult);
@@ -277,6 +290,8 @@ public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
[Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
public static IPHostEntry GetHostByAddress(string address)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(address);
IPHostEntry ipHostEntry = GetHostEntryCore(IPAddress.Parse(address), AddressFamily.Unspecified);
@@ -288,6 +303,8 @@ public static IPHostEntry GetHostByAddress(string address)
[Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
public static IPHostEntry GetHostByAddress(IPAddress address)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
ArgumentNullException.ThrowIfNull(address);
IPHostEntry ipHostEntry = GetHostEntryCore(address, AddressFamily.Unspecified);
@@ -303,7 +320,7 @@ public static IPHostEntry Resolve(string hostName)
// See if it's an IP Address.
IPHostEntry ipHostEntry;
- if (IPAddress.TryParse(hostName, out IPAddress? address) &&
+ if (NameResolutionPal.SupportsGetNameInfo && IPAddress.TryParse(hostName, out IPAddress? address) &&
(address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6))
{
try
@@ -332,6 +349,8 @@ public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestC
[Obsolete("EndResolve has been deprecated. Use EndGetHostEntry instead.")]
public static IPHostEntry EndResolve(IAsyncResult asyncResult)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
IPHostEntry ipHostEntry;
try
@@ -414,6 +433,8 @@ private static IPAddress[] GetHostAddressesCore(IPAddress address, AddressFamily
// Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).
private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAddresses, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
// Try to get the data for the host from its address.
// We need to call getnameinfo first, because getaddrinfo w/ the ipaddress string
// will only return that address and not the full list.
@@ -500,7 +521,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
object asyncState;
// See if it's an IP Address.
- if (IPAddress.TryParse(hostName, out IPAddress? ipAddress))
+ if (NameResolutionPal.SupportsGetNameInfo && IPAddress.TryParse(hostName, out IPAddress? ipAddress))
{
if (throwOnIIPAny && (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.IPv6Any)))
{
diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
index 54ec640a3b071..c2924830c07be 100644
--- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
+++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
@@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using System.Runtime.Versioning;
namespace System.Net
{
@@ -15,6 +16,9 @@ internal static partial class NameResolutionPal
{
public const bool SupportsGetAddrInfoAsync = false;
+ [UnsupportedOSPlatformGuard("wasi")]
+ public static bool SupportsGetNameInfo => !OperatingSystem.IsWasi();
+
#pragma warning disable IDE0060
internal static Task? GetAddrInfoAsync(string hostName, bool justAddresses, AddressFamily family, CancellationToken cancellationToken) =>
throw new NotSupportedException();
@@ -39,6 +43,9 @@ private static SocketError GetSocketErrorForNativeError(int error)
return SocketError.HostNotFound;
case (int)Interop.Sys.GetAddrInfoErrorFlags.EAI_MEMORY:
throw new OutOfMemoryException();
+ case (int)Interop.Sys.GetAddrInfoErrorFlags.EAI_SYSTEM:
+ Debug.Fail($"Unexpected error: {error} errno: {Interop.Sys.GetErrNo()}");
+ return SocketError.SocketError;
default:
Debug.Fail($"Unexpected error: {error}");
return SocketError.SocketError;
@@ -146,6 +153,8 @@ public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses,
public static unsafe string? TryGetNameInfo(IPAddress addr, out SocketError socketError, out int nativeErrorCode)
{
+ if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
+
byte* buffer = stackalloc byte[Interop.Sys.NI_MAXHOST + 1 /*for null*/];
byte isIPv6;
diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs
index 72fc7685dffe9..a044e20461273 100644
--- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs
+++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs
@@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
+using System.Runtime.Versioning;
namespace System.Net
{
@@ -43,6 +44,8 @@ static void Initialize()
}
}
+ public const bool SupportsGetNameInfo = true;
+
public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, AddressFamily addressFamily, out string? hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode)
{
Interop.Winsock.EnsureInitialized();
diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs
index 07a252fbd250c..defa4da90564b 100644
--- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs
+++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs
@@ -59,7 +59,7 @@ protected override void OnEventCommand(EventCommandEventArgs command)
private void ResolutionFailed() => WriteEvent(ResolutionFailedEventId);
[NonEvent]
- public static bool AnyDiagnosticsEnabled() => Log.IsEnabled() || NameResolutionMetrics.IsEnabled() || NameResolutionActivity.IsTracingEnabled();
+ public static bool AnyDiagnosticsEnabled() => !OperatingSystem.IsWasi() && (Log.IsEnabled() || NameResolutionMetrics.IsEnabled() || NameResolutionActivity.IsTracingEnabled());
[NonEvent]
public NameResolutionActivity BeforeResolution(object hostNameOrAddress, long startingTimestamp = 0)
@@ -91,6 +91,8 @@ public NameResolutionActivity BeforeResolution(object hostNameOrAddress, long st
[NonEvent]
public void AfterResolution(object hostNameOrAddress, in NameResolutionActivity activity, object? answer, Exception? exception = null)
{
+ if (OperatingSystem.IsWasi()) return;
+
if (!activity.Stop(answer, exception, out TimeSpan duration))
{
// We stopped the System.Diagnostics.Activity at this point and neither metrics nor EventSource is enabled.
diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs
index 71820b8cc72f7..d1073db5d3986 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs
@@ -69,14 +69,14 @@ public async Task Dns_GetHostAddressesAsync_NullHost_Fail()
await Assert.ThrowsAsync(() => Dns.GetHostAddressesAsync(null));
}
- [Fact]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void DnsBeginGetHostAddresses_BadName_Throws()
{
IAsyncResult asyncObject = Dns.BeginGetHostAddresses("BadName", null, null);
Assert.ThrowsAny(() => Dns.EndGetHostAddresses(asyncObject));
}
- [Fact]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void DnsBeginGetHostAddresses_BadIpString_ReturnsAddress()
{
IAsyncResult asyncObject = Dns.BeginGetHostAddresses("0.0.1.1", null, null);
@@ -86,7 +86,7 @@ public void DnsBeginGetHostAddresses_BadIpString_ReturnsAddress()
Assert.Equal(IPAddress.Parse("0.0.1.1"), results[0]);
}
- [Fact]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void DnsBeginGetHostAddresses_MachineName_MatchesGetHostAddresses()
{
IAsyncResult asyncObject = Dns.BeginGetHostAddresses(TestSettings.LocalHost, null, null);
diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByAddressTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByAddressTest.cs
index a9072c6506faa..6a6b237b0d938 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByAddressTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByAddressTest.cs
@@ -8,6 +8,7 @@
namespace System.Net.NameResolution.Tests
{
+ [SkipOnPlatform(TestPlatforms.Wasi, "WASI has no getnameinfo")]
public class GetHostByAddressTest
{
[Fact]
diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByNameTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByNameTest.cs
index 56f52afb1b7da..479a5cd39ac35 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByNameTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByNameTest.cs
@@ -19,7 +19,7 @@ public void DnsObsoleteBeginGetHostByName_BadName_Throws()
Assert.ThrowsAny(() => Dns.EndGetHostByName(asyncObject));
}
- [Fact]
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void DnsObsoleteBeginGetHostByName_IPv4String_ReturnsOnlyGivenIP()
{
IAsyncResult asyncObject = Dns.BeginGetHostByName(IPAddress.Loopback.ToString(), null, null);
@@ -106,6 +106,7 @@ public void DnsObsoleteGetHostByName_IPv6String_ReturnsOnlyGivenIP()
[ActiveIssue("https://github.com/dotnet/runtime/issues/1488", TestPlatforms.OSX)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotArm64Process))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/27622")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/51377", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
+ [ActiveIssue("https://github.com/dotnet/runtime/issues/107339", TestPlatforms.Wasi)]
public void DnsObsoleteGetHostByName_EmptyString_ReturnsHostName()
{
IPHostEntry entry = Dns.GetHostByName("");
diff --git a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
index a4a7174897a39..c4988cef81672 100644
--- a/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
+++ b/src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
@@ -148,10 +148,16 @@ public async Task Dns_GetHostEntry_NullStringHost_Fail()
{
Assert.Throws(() => Dns.GetHostEntry((string)null));
await Assert.ThrowsAsync(() => Dns.GetHostEntryAsync((string)null));
+ }
+
+ [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
+ public async Task Dns_GetHostEntry_NullStringHost_Fail_Obsolete()
+ {
await Assert.ThrowsAsync(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, (string)null, null));
}
[Fact]
+ [SkipOnPlatform(TestPlatforms.Wasi, "WASI has no getnameinfo")]
public async Task Dns_GetHostEntryAsync_NullIPAddressHost_Fail()
{
Assert.Throws(() => Dns.GetHostEntry((IPAddress)null));
@@ -168,6 +174,7 @@ public static IEnumerable