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

[release/5.0] Support Async DNS lookup on older Windows from impersonificated conte… #47657

Merged
merged 2 commits into from
Feb 11, 2021
Merged
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 @@ -11,6 +11,8 @@ internal static partial class Interop
{
internal static partial class Winsock
{
internal const int WSA_E_CANCELLED = 10111;

internal const string GetAddrInfoExCancelFunctionName = "GetAddrInfoExCancel";

internal const int NS_ALL = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ public static unsafe string GetHostName()
SocketError errorCode = (SocketError)Interop.Winsock.GetAddrInfoExW(
hostName, null, Interop.Winsock.NS_ALL, IntPtr.Zero, &hints, &context->Result, IntPtr.Zero, &context->Overlapped, s_getAddrInfoExCallback, &context->CancelHandle);


if (errorCode == SocketError.TryAgain)
if (errorCode == SocketError.TryAgain || (int)errorCode == Interop.Winsock.WSA_E_CANCELLED)
{
// WSATRY_AGAIN indicates possible problem with reachability according to docs.
// However, if servers are really unreachable, we would still get IOPending here
// and final result would be posted via overlapped IO.
// synchronous failure here may signal issue when GetAddrInfoExW does not work from
// impersonated context.
// impersonated context. Windows 8 and Server 2012 fail for same reason with different errorCode.
GetAddrInfoExContext.FreeContext(context);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
Expand Down Expand Up @@ -57,6 +59,42 @@ void Asserts(WindowsIdentity currentWindowsIdentity)
Assert.NotEqual(currentWindowsIdentity.Name, WindowsIdentity.GetCurrent().Name);
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[OuterLoop]
public void RunImpersonated_NameResolution()
{
WindowsIdentity currentWindowsIdentity = WindowsIdentity.GetCurrent();

WindowsIdentity.RunImpersonated(_fixture.TestAccount.AccountTokenHandle, () =>
{
Assert.Equal(_fixture.TestAccount.AccountName, WindowsIdentity.GetCurrent().Name);

IPAddress[] a1 = Dns.GetHostAddressesAsync("").GetAwaiter().GetResult();
IPAddress[] a2 = Dns.GetHostAddresses("");

Assert.True(a1.Length > 0);
Assert.True(a1.SequenceEqual(a2));
});
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[OuterLoop]
public async Task RunImpersonatedAsync_NameResolution()
{
WindowsIdentity currentWindowsIdentity = WindowsIdentity.GetCurrent();

await WindowsIdentity.RunImpersonatedAsync(_fixture.TestAccount.AccountTokenHandle, async () =>
{
Assert.Equal(_fixture.TestAccount.AccountName, WindowsIdentity.GetCurrent().Name);

IPAddress[] a1 = await Dns.GetHostAddressesAsync("");
IPAddress[] a2 = Dns.GetHostAddresses("");

Assert.True(a1.Length > 0);
Assert.True(a1.SequenceEqual(a2));
});
}
}

public class WindowsIdentityFixture : IDisposable
Expand Down