+++ /dev/null
-// 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.Runtime.InteropServices;
-
-internal static partial class Interop
-{
- internal static partial class Winsock
- {
- [DllImport(Interop.Libraries.Ws2_32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
- internal static extern IntPtr gethostbyname(
- [In] string host
- );
- }
-}
<AssemblyName>System.Net.NameResolution</AssemblyName>
<ProjectGuid>{1714448C-211E-48C1-8B7E-4EE667D336A1}</ProjectGuid>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
- </PropertyGroup>
+ </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Windows_NT-Debug|AnyCPU'" />
<Compile Include="$(CommonPath)\Interop\Windows\Winsock\Interop.gethostbyaddr.cs">
<Link>Interop\Windows\Winsock\Interop.gethostbyaddr.cs</Link>
</Compile>
- <Compile Include="$(CommonPath)\Interop\Windows\Winsock\Interop.gethostbyname.cs">
- <Link>Interop\Windows\Winsock\Interop.gethostbyname.cs</Link>
- </Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Winsock\Interop.gethostname.cs">
<Link>Interop\Windows\Winsock\Interop.gethostname.cs</Link>
</Compile>
<Reference Include="System.Threading.Tasks" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
-</Project>
+</Project>
\ No newline at end of file
{
return NameResolutionUtilities.GetUnresolvedAnswer(address);
}
- return InternalGetHostByName(hostName, false);
+ return InternalGetHostByName(hostName);
}
- private static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6)
+ private static IPHostEntry InternalGetHostByName(string hostName)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(null, hostName);
IPHostEntry ipHostEntry = null;
{
throw SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode);
}
-
+
if (NetEventSource.IsEnabled) NetEventSource.Exit(null, ipHostEntry);
return ipHostEntry;
} // GetHostByName
}
else
{
- ipHostEntry = InternalGetHostByName(hostName, false);
+ ipHostEntry = InternalGetHostByName(hostName);
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(null, ipHostEntry);
}
else
{
- hostEntry = InternalGetHostByName(result.hostName, result.includeIPv6);
+ hostEntry = InternalGetHostByName(result.hostName);
}
}
catch (OutOfMemoryException)
}
else
{
- ipHostEntry = InternalGetHostByName(hostNameOrAddress, true);
+ ipHostEntry = InternalGetHostByName(hostNameOrAddress);
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(null, ipHostEntry);
{
// InternalGetHostByName works with IP addresses (and avoids a reverse-lookup), but we need
// explicit handling in order to do the ArgumentException and guarantee the behavior.
- addresses = InternalGetHostByName(hostNameOrAddress, true).AddressList;
+ addresses = InternalGetHostByName(hostNameOrAddress).AddressList;
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(null, addresses);
return HostEntry;
} // NativeToHostEntry
- public static IPHostEntry GetHostByName(string hostName)
- {
- //
- // IPv6 disabled: use gethostbyname() to obtain DNS information.
- //
- IntPtr nativePointer =
- Interop.Winsock.gethostbyname(
- hostName);
-
- if (nativePointer == IntPtr.Zero)
- {
- // Need to do this first since if we wait the last error code might be overwritten.
- SocketException socketException = new SocketException();
-
- IPAddress address;
- if (IPAddress.TryParse(hostName, out address))
- {
- IPHostEntry ipHostEntry = NameResolutionUtilities.GetUnresolvedAnswer(address);
- if (NetEventSource.IsEnabled) NetEventSource.Exit(null, ipHostEntry);
- return ipHostEntry;
- }
-
- throw socketException;
- }
-
- return NativeToHostEntry(nativePointer);
- }
-
public static IPHostEntry GetHostByAddr(IPAddress address)
{
// TODO #2891: Optimize this (or decide if this legacy code can be removed):
}
[Fact]
- public void GetHostByName_LocalHost()
- {
- IPHostEntry hostEntry = NameResolutionPal.GetHostByName("localhost");
- Assert.NotNull(hostEntry);
- Assert.NotNull(hostEntry.HostName);
- Assert.NotNull(hostEntry.AddressList);
- Assert.NotNull(hostEntry.Aliases);
- }
-
- public static object[][] InvalidHostNames = new object[][] {
- new object[] { ":" },
- new object[] { "..." }
- };
-
- [Theory, MemberData(nameof(InvalidHostNames))]
- public void GetHostByName_InvalidHostName_Throws(string hostName)
- {
- Assert.ThrowsAny<SocketException>(() => NameResolutionPal.GetHostByName(hostName));
- }
-
- [ActiveIssue(20245, TestPlatforms.AnyUnix)]
- [Fact]
- public void GetHostByName_HostName()
- {
- string hostName = NameResolutionPal.GetHostName();
- Assert.NotNull(hostName);
-
- IPHostEntry hostEntry = NameResolutionPal.GetHostByName(hostName);
- Assert.NotNull(hostEntry);
- Assert.NotNull(hostEntry.HostName);
- Assert.NotNull(hostEntry.AddressList);
- Assert.NotNull(hostEntry.Aliases);
- }
-
- [Fact]
public void GetHostByAddr_LocalHost()
{
Assert.NotNull(NameResolutionPal.GetHostByAddr(new IPAddress(0x0100007f)));
}
[Fact]
- public void GetHostByName_LocalHost_GetHostByAddr()
- {
- IPHostEntry hostEntry1 = NameResolutionPal.GetHostByName("localhost");
- Assert.NotNull(hostEntry1);
- IPHostEntry hostEntry2 = NameResolutionPal.GetHostByAddr(hostEntry1.AddressList[0]);
- Assert.NotNull(hostEntry2);
-
- IPAddress[] list1 = hostEntry1.AddressList;
- IPAddress[] list2 = hostEntry2.AddressList;
-
- for (int i = 0; i < list1.Length; i++)
- {
- Assert.NotEqual(-1, Array.IndexOf(list2, list1[i]));
- }
- }
-
- [Fact]
- public void GetHostByName_HostName_GetHostByAddr()
- {
- IPHostEntry hostEntry1 = NameResolutionPal.GetHostByName(System.Net.Test.Common.Configuration.Http.Http2Host);
- Assert.NotNull(hostEntry1);
-
- IPAddress[] list1 = hostEntry1.AddressList;
- Assert.InRange(list1.Length, 1, Int32.MaxValue);
-
- foreach (IPAddress addr1 in list1)
- {
- IPHostEntry hostEntry2 = NameResolutionPal.GetHostByAddr(addr1);
- Assert.NotNull(hostEntry2);
-
- IPAddress[] list2 = hostEntry2.AddressList;
- Assert.InRange(list2.Length, 1, list1.Length);
-
- foreach (IPAddress addr2 in list2)
- {
- Assert.NotEqual(-1, Array.IndexOf(list1, addr2));
- }
- }
- }
-
- [Fact]
public void TryGetAddrInfo_LocalHost()
{
IPHostEntry hostEntry;