-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Modified Dns.GetHostAddressesAsync to be truly async #26850
Changes from 9 commits
e31d807
77ccb1c
a6cff67
898719f
c173a88
a375f91
795e5be
a9a3403
91864e5
524b5fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Net.Internals; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Net.Sockets | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal unsafe struct AddressInfoEx | ||
{ | ||
internal AddressInfoHints ai_flags; | ||
internal AddressFamily ai_family; | ||
internal SocketType ai_socktype; | ||
internal ProtocolFamily ai_protocol; | ||
internal int ai_addrlen; | ||
internal IntPtr ai_canonname; // Ptr to the canonical name - check for NULL | ||
internal byte* ai_addr; // Ptr to the sockaddr structure | ||
internal IntPtr ai_blob; // Unused ptr to blob data about provider | ||
internal int ai_bloblen; | ||
internal IntPtr ai_provider; // Unused ptr to the namespace provider guid | ||
internal AddressInfoEx* ai_next; // Next structure in linked list | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Net.Sockets; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Winsock | ||
{ | ||
internal unsafe delegate void LPLOOKUPSERVICE_COMPLETION_ROUTINE([In] int dwError, [In] int dwBytes, [In] NativeOverlapped* lpOverlapped); | ||
|
||
[DllImport(Interop.Libraries.Ws2_32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] | ||
internal static extern unsafe int GetAddrInfoExW( | ||
[In] string pName, | ||
[In] string pServiceName, | ||
[In] int dwNamespace, | ||
[In] IntPtr lpNspId, | ||
[In] ref AddressInfoEx pHints, | ||
[Out] out AddressInfoEx* ppResult, | ||
[In] IntPtr timeout, | ||
[In] ref NativeOverlapped lpOverlapped, | ||
[In] LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, | ||
[Out] out IntPtr lpNameHandle | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: We try to use the same names as in the native definition, so e.g. |
||
|
||
[DllImport("ws2_32.dll", ExactSpelling = true, SetLastError = true)] | ||
internal static extern unsafe void FreeAddrInfoEx([In] AddressInfoEx* pAddrInfo); | ||
|
||
[DllImport("ws2_32.dll", ExactSpelling = true, SetLastError = true)] | ||
internal static extern int GetAddrInfoExCancel([In] ref IntPtr lpHandle); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed now, right? Looks like the only remaining usage is just using it for its name. |
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,10 +103,15 @@ public static unsafe void SetPort(byte[] buffer, ushort port) | |
} | ||
|
||
public static unsafe uint GetIPv4Address(byte[] buffer) | ||
{ | ||
return GetIPv4Address(new ReadOnlySpan<byte>(buffer)); | ||
} | ||
|
||
public static unsafe uint GetIPv4Address(ReadOnlySpan<byte> buffer) | ||
{ | ||
uint ipAddress; | ||
Interop.Error err; | ||
fixed (byte* rawAddress = buffer) | ||
fixed (byte* rawAddress = &MemoryMarshal.GetReference(buffer)) | ||
{ | ||
err = Interop.Sys.GetIPv4Address(rawAddress, buffer.Length, &ipAddress); | ||
} | ||
|
@@ -115,11 +120,16 @@ public static unsafe uint GetIPv4Address(byte[] buffer) | |
return ipAddress; | ||
} | ||
|
||
public static unsafe void GetIPv6Address(byte[] buffer, Span<byte> address, out uint scope) | ||
public static void GetIPv6Address(byte[] buffer, Span<byte> address, out uint scope) | ||
{ | ||
GetIPv6Address(new ReadOnlySpan<byte>(buffer), address, out scope); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this overload? I'd expect the call site passing in the byte[] to just work with the span-based overload due to the implicit array->span cast. |
||
|
||
public static unsafe void GetIPv6Address(ReadOnlySpan<byte> buffer, Span<byte> address, out uint scope) | ||
{ | ||
uint localScope; | ||
Interop.Error err; | ||
fixed (byte* rawAddress = buffer) | ||
fixed (byte* rawAddress = &MemoryMarshal.GetReference(buffer)) | ||
fixed (byte* ipAddress = &MemoryMarshal.GetReference(address)) | ||
{ | ||
err = Interop.Sys.GetIPv6Address(rawAddress, buffer.Length, ipAddress, address.Length, &localScope); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,11 @@ public static unsafe void SetPort(byte[] buffer, ushort port) | |
} | ||
|
||
public static unsafe uint GetIPv4Address(byte[] buffer) | ||
{ | ||
return GetIPv4Address(new ReadOnlySpan<byte>(buffer)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same "do we need this overload" question |
||
|
||
public static unsafe uint GetIPv4Address(ReadOnlySpan<byte> buffer) | ||
{ | ||
unchecked | ||
{ | ||
|
@@ -49,7 +54,12 @@ public static unsafe uint GetIPv4Address(byte[] buffer) | |
} | ||
} | ||
|
||
public static unsafe void GetIPv6Address(byte[] buffer, Span<byte> address, out uint scope) | ||
public static void GetIPv6Address(byte[] buffer, Span<byte> address, out uint scope) | ||
{ | ||
GetIPv6Address(new ReadOnlySpan<byte>(buffer), address, out scope); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
|
||
public static unsafe void GetIPv6Address(ReadOnlySpan<byte> buffer, Span<byte> address, out uint scope) | ||
{ | ||
for (int i = 0; i < address.Length; i++) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,17 +41,22 @@ public static IPHostEntry GetHostByName(string hostName) | |
return InternalGetHostByName(hostName, false); | ||
} | ||
|
||
private static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6) | ||
private static void ValidateHostName(string hostName) | ||
{ | ||
if (NetEventSource.IsEnabled) NetEventSource.Enter(null, hostName); | ||
IPHostEntry ipHostEntry = null; | ||
|
||
if (hostName.Length > MaxHostName // If 255 chars, the last one must be a dot. | ||
|| hostName.Length == MaxHostName && hostName[MaxHostName - 1] != '.') | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(hostName), SR.Format(SR.net_toolong, | ||
nameof(hostName), MaxHostName.ToString(NumberFormatInfo.CurrentInfo))); | ||
} | ||
} | ||
|
||
private static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6) | ||
{ | ||
if (NetEventSource.IsEnabled) NetEventSource.Enter(null, hostName); | ||
IPHostEntry ipHostEntry = null; | ||
|
||
ValidateHostName(hostName); | ||
|
||
// | ||
// IPv6 Changes: IPv6 requires the use of getaddrinfo() rather | ||
|
@@ -252,42 +257,19 @@ public static IPHostEntry Resolve(string hostName) | |
return ipHostEntry; | ||
} | ||
|
||
private class ResolveAsyncResult : ContextAwareResult | ||
{ | ||
// Forward lookup | ||
internal ResolveAsyncResult(string hostName, object myObject, bool includeIPv6, object myState, AsyncCallback myCallBack) : | ||
base(myObject, myState, myCallBack) | ||
{ | ||
this.hostName = hostName; | ||
this.includeIPv6 = includeIPv6; | ||
} | ||
|
||
// Reverse lookup | ||
internal ResolveAsyncResult(IPAddress address, object myObject, bool includeIPv6, object myState, AsyncCallback myCallBack) : | ||
base(myObject, myState, myCallBack) | ||
{ | ||
this.includeIPv6 = includeIPv6; | ||
this.address = address; | ||
} | ||
|
||
internal readonly string hostName; | ||
internal bool includeIPv6; | ||
internal IPAddress address; | ||
} | ||
|
||
private static void ResolveCallback(object context) | ||
{ | ||
ResolveAsyncResult result = (ResolveAsyncResult)context; | ||
DnsResolveAsyncResult result = (DnsResolveAsyncResult)context; | ||
IPHostEntry hostEntry; | ||
try | ||
{ | ||
if (result.address != null) | ||
if (result.IpAddress != null) | ||
{ | ||
hostEntry = InternalGetHostByAddress(result.address, result.includeIPv6); | ||
hostEntry = InternalGetHostByAddress(result.IpAddress, result.IncludeIPv6); | ||
} | ||
else | ||
{ | ||
hostEntry = InternalGetHostByName(result.hostName, result.includeIPv6); | ||
hostEntry = InternalGetHostByName(result.HostName, result.IncludeIPv6); | ||
} | ||
} | ||
catch (OutOfMemoryException) | ||
|
@@ -315,20 +297,20 @@ private static IAsyncResult HostResolutionBeginHelper(string hostName, bool just | |
if (NetEventSource.IsEnabled) NetEventSource.Info(null, hostName); | ||
|
||
// See if it's an IP Address. | ||
IPAddress address; | ||
ResolveAsyncResult asyncResult; | ||
if (IPAddress.TryParse(hostName, out address)) | ||
IPAddress ipAddress; | ||
DnsResolveAsyncResult asyncResult; | ||
if (IPAddress.TryParse(hostName, out ipAddress)) | ||
{ | ||
if (throwOnIIPAny && (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))) | ||
if (throwOnIIPAny && (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.IPv6Any))) | ||
{ | ||
throw new ArgumentException(SR.net_invalid_ip_addr, nameof(hostName)); | ||
} | ||
|
||
asyncResult = new ResolveAsyncResult(address, null, includeIPv6, state, requestCallback); | ||
asyncResult = new DnsResolveAsyncResult(ipAddress, null, includeIPv6, state, requestCallback); | ||
|
||
if (justReturnParsedIp) | ||
{ | ||
IPHostEntry hostEntry = NameResolutionUtilities.GetUnresolvedAnswer(address); | ||
IPHostEntry hostEntry = NameResolutionUtilities.GetUnresolvedAnswer(ipAddress); | ||
asyncResult.StartPostingAsyncOp(false); | ||
asyncResult.InvokeCallback(hostEntry); | ||
asyncResult.FinishPostingAsyncOp(); | ||
|
@@ -337,19 +319,29 @@ private static IAsyncResult HostResolutionBeginHelper(string hostName, bool just | |
} | ||
else | ||
{ | ||
asyncResult = new ResolveAsyncResult(hostName, null, includeIPv6, state, requestCallback); | ||
asyncResult = new DnsResolveAsyncResult(hostName, null, includeIPv6, state, requestCallback); | ||
} | ||
|
||
// Set up the context, possibly flow. | ||
asyncResult.StartPostingAsyncOp(false); | ||
|
||
// Start the resolve. | ||
Task.Factory.StartNew( | ||
s => ResolveCallback(s), | ||
asyncResult, | ||
CancellationToken.None, | ||
TaskCreationOptions.DenyChildAttach, | ||
TaskScheduler.Default); | ||
// If the OS supports it and 'hostName' is not an IP Address, resolve the name asynchronously | ||
// instead of calling the synchronous version in the ThreadPool. | ||
if (NameResolutionPal.SupportsGetAddrInfoAsync && ipAddress == null) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can you add a comment about the conditions that let's us take this path? |
||
ValidateHostName(hostName); | ||
NameResolutionPal.GetAddrInfoAsync(asyncResult); | ||
} | ||
else | ||
{ | ||
// Start the resolve. | ||
Task.Factory.StartNew( | ||
s => ResolveCallback(s), | ||
asyncResult, | ||
CancellationToken.None, | ||
TaskCreationOptions.DenyChildAttach, | ||
TaskScheduler.Default); | ||
} | ||
|
||
// Finish the flowing, maybe it completed? This does nothing if we didn't initiate the flowing above. | ||
asyncResult.FinishPostingAsyncOp(); | ||
|
@@ -371,7 +363,7 @@ private static IAsyncResult HostResolutionBeginHelper(IPAddress address, bool fl | |
if (NetEventSource.IsEnabled) NetEventSource.Info(null, address); | ||
|
||
// Set up the context, possibly flow. | ||
ResolveAsyncResult asyncResult = new ResolveAsyncResult(address, null, includeIPv6, state, requestCallback); | ||
DnsResolveAsyncResult asyncResult = new DnsResolveAsyncResult(address, null, includeIPv6, state, requestCallback); | ||
if (flowContext) | ||
{ | ||
asyncResult.StartPostingAsyncOp(false); | ||
|
@@ -399,7 +391,7 @@ private static IPHostEntry HostResolutionEndHelper(IAsyncResult asyncResult) | |
{ | ||
throw new ArgumentNullException(nameof(asyncResult)); | ||
} | ||
ResolveAsyncResult castedResult = asyncResult as ResolveAsyncResult; | ||
DnsResolveAsyncResult castedResult = asyncResult as DnsResolveAsyncResult; | ||
if (castedResult == null) | ||
{ | ||
throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult)); | ||
|
@@ -611,7 +603,7 @@ public static IPHostEntry EndResolve(IAsyncResult asyncResult) | |
} | ||
catch (SocketException ex) | ||
{ | ||
IPAddress address = ((ResolveAsyncResult)asyncResult).address; | ||
IPAddress address = ((DnsResolveAsyncResult)asyncResult).IpAddress; | ||
if (address == null) | ||
throw; // BeginResolve was called with a HostName, not an IPAddress | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing license header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done