Skip to content

Commit 90a7b23

Browse files
committed
rebase
1 parent 1bad124 commit 90a7b23

26 files changed

+348
-478
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal static partial class Sys
1414

1515
internal static unsafe string GetHostName()
1616
{
17+
#if !TARGET_WASI
1718
const int HOST_NAME_MAX = 255;
1819
const int ArrLength = HOST_NAME_MAX + 1;
1920

@@ -34,6 +35,9 @@ internal static unsafe string GetHostName()
3435
name[ArrLength - 1] = 0;
3536

3637
return Marshal.PtrToStringUTF8((IntPtr)name)!;
38+
#else
39+
return "localhost";
40+
#endif
3741
}
3842
}
3943
}

src/libraries/Common/src/Interop/Unix/System.Native/Interop.HostEntry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal enum GetAddrInfoErrorFlags : int
2121
EAI_BADARG = 6, // One or more input arguments were invalid.
2222
EAI_NOMORE = 7, // No more entries are present in the list.
2323
EAI_MEMORY = 8, // Out of memory.
24+
EAI_SYSTEM = 9, // Other system error; errno is set to indicate the error.
2425
}
2526

2627
[StructLayout(LayoutKind.Sequential)]

src/libraries/System.Net.NameResolution/Directory.Build.props

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<StrongNameKeyId>Microsoft</StrongNameKeyId>
55
<IncludePlatformAttributes>true</IncludePlatformAttributes>
6-
<!-- WASI until https://github.com/dotnet/runtime/issues/98957 -->
7-
<UnsupportedOSPlatforms>browser;wasi</UnsupportedOSPlatforms>
6+
<UnsupportedOSPlatforms>browser</UnsupportedOSPlatforms>
87
</PropertyGroup>
98
</Project>

src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
1010
<PropertyGroup>
1111
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
12-
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == 'wasi' or '$(TargetPlatformIdentifier)' == ''">SR.SystemNetNameResolution_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
13-
<ApiExclusionListPath Condition="'$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == 'wasi'">ExcludeApiList.PNSE.Browser.txt</ApiExclusionListPath>
12+
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == ''">SR.SystemNetNameResolution_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
13+
<ApiExclusionListPath Condition="'$(TargetPlatformIdentifier)' == 'browser'">ExcludeApiList.PNSE.Browser.txt</ApiExclusionListPath>
14+
<DefineConstants Condition="'$(TargetPlatformIdentifier)' == 'wasi'">$(DefineConstants);TARGET_WASI</DefineConstants>
1415
</PropertyGroup>
1516

1617
<ItemGroup Condition="'$(GeneratePlatformNotSupportedAssemblyMessage)' == ''">
1718
<Compile Include="System\Net\Dns.cs" />
1819
<Compile Include="System\Net\IPHostEntry.cs" />
19-
<Compile Include="System\Net\NetEventSource.NameResolution.cs" />
20-
<Compile Include="System\Net\NameResolutionMetrics.cs" />
21-
<Compile Include="System\Net\NameResolutionTelemetry.cs" />
20+
<Compile Include="System\Net\NetEventSource.NameResolution.cs" Condition="'$(TargetPlatformIdentifier)' != 'wasi'" />
21+
<Compile Include="System\Net\NameResolutionMetrics.cs" Condition="'$(TargetPlatformIdentifier)' != 'wasi'" />
22+
<Compile Include="System\Net\NameResolutionTelemetry.cs" Condition="'$(TargetPlatformIdentifier)' != 'wasi'" />
2223
<!-- Logging -->
2324
<Compile Include="$(CommonPath)System\Net\Logging\NetEventSource.Common.cs"
2425
Link="Common\System\Net\Logging\NetEventSource.Common.cs" />
@@ -96,7 +97,40 @@
9697
Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
9798
</ItemGroup>
9899

99-
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser' or '$(TargetPlatformIdentifier)' == 'wasi'">
100+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'wasi'">
101+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetHostName.cs"
102+
Link="Common\Interop\Unix\System.Native\Interop.GetHostName.cs" />
103+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ErrNo.cs"
104+
Link="Common\Interop\Unix\System.Native\Interop.ErrNo.cs"/>
105+
106+
<Compile Include="System\Net\NameResolutionTelemetry.Wasi.cs" />
107+
108+
<Compile Include="System\Net\NameResolutionPal.Unix.cs" />
109+
<Compile Include="$(CommonPath)System\Net\InteropIPAddressExtensions.Unix.cs"
110+
Link="Common\System\Net\InteropIPAddressExtensions.Unix.cs" />
111+
<Compile Include="$(CommonPath)System\Net\SocketAddressPal.Unix.cs"
112+
Link="Common\System\Net\Internals\SocketAddressPal.Unix.cs" />
113+
<Compile Include="$(CommonPath)System\Net\SocketProtocolSupportPal.Unix.cs"
114+
Link="Common\System\Net\SocketProtocolSupportPal.Unix" />
115+
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
116+
Link="Common\Interop\CoreLib\Unix\Interop.Errors.cs" />
117+
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
118+
Link="Common\Interop\Unix\Interop.Libraries.cs" />
119+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Close.cs"
120+
Link="Common\Interop\Unix\System.Native\Interop.Close.cs" />
121+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetNameInfo.cs"
122+
Link="Common\Interop\Unix\System.Native\Interop.GetNameInfo.cs" />
123+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.HostEntry.cs"
124+
Link="Common\Interop\Unix\System.Native\Interop.HostEntries.cs" />
125+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.IPAddress.cs"
126+
Link="Common\Interop\Unix\System.Native\Interop.IPAddress.cs" />
127+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Socket.cs"
128+
Link="Common\Interop\Unix\System.Native\Interop.Socket.cs" />
129+
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SocketAddress.cs"
130+
Link="Common\Interop\Unix\System.Native\Interop.SocketAddress.cs" />
131+
</ItemGroup>
132+
133+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser'">
100134
<Compile Include="System\Net\Dns.Browser.cs" />
101135
</ItemGroup>
102136

src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Sockets;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using System.Runtime.Versioning;
1011

1112
namespace System.Net
1213
{
@@ -37,6 +38,9 @@ public static string GetHostName()
3738

3839
public static IPHostEntry GetHostEntry(IPAddress address)
3940
{
41+
#if TARGET_WASI
42+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
43+
#endif // TARGET_WASI
4044
ArgumentNullException.ThrowIfNull(address);
4145

4246
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -68,6 +72,7 @@ public static IPHostEntry GetHostEntry(string hostNameOrAddress, AddressFamily f
6872

6973
// See if it's an IP Address.
7074
IPHostEntry ipHostEntry;
75+
#if !TARGET_WASI
7176
if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
7277
{
7378
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -79,6 +84,7 @@ public static IPHostEntry GetHostEntry(string hostNameOrAddress, AddressFamily f
7984
ipHostEntry = GetHostEntryCore(address, family);
8085
}
8186
else
87+
#endif // TARGET_WASI
8288
{
8389
ipHostEntry = GetHostEntryCore(hostNameOrAddress, family);
8490
}
@@ -147,6 +153,9 @@ public static Task<IPHostEntry> GetHostEntryAsync(string hostNameOrAddress, Addr
147153

148154
public static Task<IPHostEntry> GetHostEntryAsync(IPAddress address)
149155
{
156+
#if TARGET_WASI
157+
throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
158+
#else
150159
ArgumentNullException.ThrowIfNull(address);
151160

152161
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -160,6 +169,7 @@ public static Task<IPHostEntry> GetHostEntryAsync(IPAddress address)
160169
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info((IPAddress)s, $"{ipHostEntry} with {ipHostEntry.AddressList.Length} entries");
161170
return ipHostEntry;
162171
}, address, CancellationToken.None);
172+
#endif // TARGET_WASI
163173
}
164174

165175
public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject) =>
@@ -170,6 +180,9 @@ public static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCall
170180

171181
public static IPHostEntry EndGetHostEntry(IAsyncResult asyncResult)
172182
{
183+
#if TARGET_WASI
184+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
185+
#endif // TARGET_WASI
173186
ArgumentNullException.ThrowIfNull(asyncResult);
174187

175188
return TaskToAsyncResult.End<IPHostEntry>(asyncResult);
@@ -192,6 +205,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress, AddressFami
192205

193206
// See if it's an IP Address.
194207
IPAddress[] addresses;
208+
#if TARGET_WASI
195209
if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
196210
{
197211
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
@@ -203,6 +217,7 @@ public static IPAddress[] GetHostAddresses(string hostNameOrAddress, AddressFami
203217
addresses = (family == AddressFamily.Unspecified || address.AddressFamily == family) ? new IPAddress[] { address } : Array.Empty<IPAddress>();
204218
}
205219
else
220+
#endif // TARGET_WASI
206221
{
207222
addresses = GetHostAddressesCore(hostNameOrAddress, family);
208223
}
@@ -244,6 +259,9 @@ public static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, Async
244259

245260
public static IPAddress[] EndGetHostAddresses(IAsyncResult asyncResult)
246261
{
262+
#if TARGET_WASI
263+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
264+
#endif // TARGET_WASI
247265
ArgumentNullException.ThrowIfNull(asyncResult);
248266

249267
return TaskToAsyncResult.End<IPAddress[]>(asyncResult);
@@ -269,6 +287,9 @@ public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? re
269287
[Obsolete("EndGetHostByName has been deprecated. Use EndGetHostEntry instead.")]
270288
public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
271289
{
290+
#if TARGET_WASI
291+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
292+
#endif // TARGET_WASI
272293
ArgumentNullException.ThrowIfNull(asyncResult);
273294

274295
return TaskToAsyncResult.End<IPHostEntry>(asyncResult);
@@ -277,6 +298,9 @@ public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
277298
[Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
278299
public static IPHostEntry GetHostByAddress(string address)
279300
{
301+
#if TARGET_WASI
302+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
303+
#endif // TARGET_WASI
280304
ArgumentNullException.ThrowIfNull(address);
281305

282306
IPHostEntry ipHostEntry = GetHostEntryCore(IPAddress.Parse(address), AddressFamily.Unspecified);
@@ -288,6 +312,9 @@ public static IPHostEntry GetHostByAddress(string address)
288312
[Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
289313
public static IPHostEntry GetHostByAddress(IPAddress address)
290314
{
315+
#if TARGET_WASI
316+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
317+
#endif // TARGET_WASI
291318
ArgumentNullException.ThrowIfNull(address);
292319

293320
IPHostEntry ipHostEntry = GetHostEntryCore(address, AddressFamily.Unspecified);
@@ -303,6 +330,7 @@ public static IPHostEntry Resolve(string hostName)
303330

304331
// See if it's an IP Address.
305332
IPHostEntry ipHostEntry;
333+
#if !TARGET_WASI
306334
if (IPAddress.TryParse(hostName, out IPAddress? address) &&
307335
(address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6))
308336
{
@@ -317,6 +345,7 @@ public static IPHostEntry Resolve(string hostName)
317345
}
318346
}
319347
else
348+
#endif // TARGET_WASI
320349
{
321350
ipHostEntry = GetHostEntryCore(hostName, AddressFamily.Unspecified);
322351
}
@@ -332,6 +361,9 @@ public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestC
332361
[Obsolete("EndResolve has been deprecated. Use EndGetHostEntry instead.")]
333362
public static IPHostEntry EndResolve(IAsyncResult asyncResult)
334363
{
364+
#if TARGET_WASI
365+
if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185
366+
#endif // TARGET_WASI
335367
IPHostEntry ipHostEntry;
336368

337369
try
@@ -405,13 +437,16 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr
405437
return result;
406438
}
407439

440+
[UnsupportedOSPlatform("wasi")]
408441
private static IPHostEntry GetHostEntryCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
409442
(IPHostEntry)GetHostEntryOrAddressesCore(address, justAddresses: false, addressFamily, activityOrDefault);
410443

444+
[UnsupportedOSPlatform("wasi")]
411445
private static IPAddress[] GetHostAddressesCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
412446
(IPAddress[])GetHostEntryOrAddressesCore(address, justAddresses: true, addressFamily, activityOrDefault);
413447

414448
// Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).
449+
[UnsupportedOSPlatform("wasi")]
415450
private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAddresses, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default)
416451
{
417452
// Try to get the data for the host from its address.
@@ -499,6 +534,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
499534

500535
object asyncState;
501536

537+
#if !TARGET_WASI
502538
// See if it's an IP Address.
503539
if (IPAddress.TryParse(hostName, out IPAddress? ipAddress))
504540
{
@@ -518,6 +554,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
518554
asyncState = family == AddressFamily.Unspecified ? (object)ipAddress : new KeyValuePair<IPAddress, AddressFamily>(ipAddress, family);
519555
}
520556
else
557+
#endif // TARGET_WASI
521558
{
522559
if (NameResolutionPal.SupportsGetAddrInfoAsync)
523560
{
@@ -558,8 +595,13 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
558595
{
559596
string h => GetHostAddressesCore(h, AddressFamily.Unspecified, activity),
560597
KeyValuePair<string, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value, activity),
598+
#if !TARGET_WASI
561599
IPAddress a => GetHostAddressesCore(a, AddressFamily.Unspecified, activity),
562600
KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value, activity),
601+
#else
602+
IPAddress => throw new PlatformNotSupportedException(),
603+
KeyValuePair<IPAddress, AddressFamily> => throw new PlatformNotSupportedException(),
604+
#endif // TARGET_WASI
563605
_ => null
564606
}, asyncState, cancellationToken);
565607
}
@@ -569,8 +611,13 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
569611
{
570612
string h => GetHostEntryCore(h, AddressFamily.Unspecified, activity),
571613
KeyValuePair<string, AddressFamily> t => GetHostEntryCore(t.Key, t.Value, activity),
614+
#if !TARGET_WASI
572615
IPAddress a => GetHostEntryCore(a, AddressFamily.Unspecified, activity),
573616
KeyValuePair<IPAddress, AddressFamily> t => GetHostEntryCore(t.Key, t.Value, activity),
617+
#else
618+
IPAddress => throw new PlatformNotSupportedException(),
619+
KeyValuePair<IPAddress, AddressFamily> => throw new PlatformNotSupportedException(),
620+
#endif // TARGET_WASI
574621
_ => null
575622
}, asyncState, cancellationToken);
576623
}

src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ private static SocketError GetSocketErrorForNativeError(int error)
3939
return SocketError.HostNotFound;
4040
case (int)Interop.Sys.GetAddrInfoErrorFlags.EAI_MEMORY:
4141
throw new OutOfMemoryException();
42+
case (int)Interop.Sys.GetAddrInfoErrorFlags.EAI_SYSTEM:
43+
#if !TARGET_WASI
44+
return SocketError.SocketError;
45+
#else
46+
throw new Exception("ErrNo: " + Interop.Sys.GetErrNo());
47+
#endif
4248
default:
4349
Debug.Fail($"Unexpected error: {error}");
4450
return SocketError.SocketError;
@@ -146,6 +152,7 @@ public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses,
146152

147153
public static unsafe string? TryGetNameInfo(IPAddress addr, out SocketError socketError, out int nativeErrorCode)
148154
{
155+
#if !TARGET_WASI
149156
byte* buffer = stackalloc byte[Interop.Sys.NI_MAXHOST + 1 /*for null*/];
150157

151158
byte isIPv6;
@@ -178,6 +185,9 @@ public static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses,
178185
socketError = GetSocketErrorForNativeError(error);
179186
nativeErrorCode = error;
180187
return socketError == SocketError.Success ? Marshal.PtrToStringUTF8((IntPtr)buffer) : null;
188+
#else
189+
throw new PlatformNotSupportedException();
190+
#endif
181191
}
182192

183193
public static string GetHostName() => Interop.Sys.GetHostName();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Diagnostics.Tracing;
7+
using System.Net.Sockets;
8+
using System.Threading;
9+
using System.Runtime.Versioning;
10+
11+
namespace System.Net
12+
{
13+
#pragma warning disable IDE0060
14+
#pragma warning disable CA1822
15+
internal sealed class NameResolutionTelemetry : EventSource
16+
{
17+
public static readonly NameResolutionTelemetry Log = new NameResolutionTelemetry();
18+
19+
[NonEvent]
20+
public static bool AnyDiagnosticsEnabled()
21+
{
22+
return false;
23+
}
24+
25+
[NonEvent]
26+
public NameResolutionActivity BeforeResolution(object hostNameOrAddress, long startingTimestamp = 0)
27+
{
28+
return default;
29+
}
30+
31+
[NonEvent]
32+
public void AfterResolution(object hostNameOrAddress, in NameResolutionActivity activity, object? answer, Exception? exception = null)
33+
{
34+
}
35+
}
36+
37+
internal readonly struct NameResolutionActivity
38+
{
39+
public static bool IsTracingEnabled()
40+
{
41+
return false;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)