Use IPGlobalProperties on all platforms for getting host name (#9530)

This commit is contained in:
Ilya
2019-05-08 16:25:02 -07:00
committed by Aditya Patwardhan
parent 3947c9d93f
commit 78d64664ce
2 changed files with 8 additions and 27 deletions
@@ -517,12 +517,6 @@ namespace System.Management.Automation
return Unix.NativeMethods.SetDate(&tm) == 0;
}
// Hostname in this context seems to be the FQDN
internal static string NonWindowsGetHostName()
{
return Unix.NativeMethods.GetFullyQualifiedName() ?? string.Empty;
}
internal static bool NonWindowsIsSameFileSystemItem(string pathOne, string pathTwo)
{
return Unix.NativeMethods.IsSameFileSystemItem(pathOne, pathTwo);
@@ -643,10 +637,6 @@ namespace System.Management.Automation
[DllImport(psLib, CharSet = CharSet.Ansi)]
internal static extern uint GetCurrentThreadId();
[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string GetFullyQualifiedName();
// This is a struct tm from <time.h>
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct UnixTm
@@ -7,6 +7,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation.Language;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -206,26 +207,16 @@ namespace System.Management.Automation
internal static string GetHostName()
{
// Note: non-windows CoreCLR does not support System.Net yet
if (Platform.IsWindows)
{
return WinGetHostName();
}
else
{
return Platform.NonWindowsGetHostName();
}
}
internal static string WinGetHostName()
{
System.Net.NetworkInformation.IPGlobalProperties ipProperties =
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
string hostname = ipProperties.HostName;
if (!string.IsNullOrEmpty(ipProperties.DomainName))
string domainName = ipProperties.DomainName;
// CoreFX on Unix calls GLibc getdomainname()
// which returns "(none)" if a domain name is not set by setdomainname()
if (!string.IsNullOrEmpty(domainName) && !domainName.Equals("(none)", StringComparison.Ordinal))
{
hostname = hostname + "." + ipProperties.DomainName;
hostname = hostname + "." + domainName;
}
return hostname;