111,126
社区成员
发帖
与我相关
我的任务
分享
private static IPHostEntry NativeToHostEntry(IntPtr nativePointer)
{
hostent hostent = (hostent) Marshal.PtrToStructure(nativePointer, typeof(hostent));
IPHostEntry entry = new IPHostEntry();
if (hostent.h_name != IntPtr.Zero)
{
entry.HostName = Marshal.PtrToStringAnsi(hostent.h_name);
}
ArrayList list = new ArrayList();
IntPtr ptr = hostent.h_addr_list;
nativePointer = Marshal.ReadIntPtr(ptr);
while (nativePointer != IntPtr.Zero)
{
int newAddress = Marshal.ReadInt32(nativePointer);
list.Add(new IPAddress(newAddress));
ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
nativePointer = Marshal.ReadIntPtr(ptr);
}
entry.AddressList = new IPAddress[list.Count];
list.CopyTo(entry.AddressList, 0);
list.Clear();
ptr = hostent.h_aliases;
nativePointer = Marshal.ReadIntPtr(ptr);
while (nativePointer != IntPtr.Zero)
{
string str = Marshal.PtrToStringAnsi(nativePointer);
list.Add(str);
ptr = IntPtrHelper.Add(ptr, IntPtr.Size);
nativePointer = Marshal.ReadIntPtr(ptr);
}
entry.Aliases = new string[list.Count];
list.CopyTo(entry.Aliases, 0);
return entry;
}
public static IPHostEntry GetHostEntry(string hostNameOrAddress)
{
IPAddress address;
IPHostEntry hostByName;
if (Logging.On)
{
Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
}
s_DnsPermission.Demand();
if (hostNameOrAddress == null)
{
throw new ArgumentNullException("hostNameOrAddress");
}
if (TryParseAsIP(hostNameOrAddress, out address))
{
if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
{
throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
}
hostByName = InternalGetHostByAddress(address, true, false);
}
else
{
hostByName = InternalGetHostByName(hostNameOrAddress, true);
}
if (Logging.On)
{
Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", hostByName);
}
return hostByName;
}
[Obsolete("Resolve is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public static IPHostEntry Resolve(string hostName)
{
IPAddress address;
IPHostEntry hostByName;
if (Logging.On)
{
Logging.Enter(Logging.Sockets, "DNS", "Resolve", hostName);
}
s_DnsPermission.Demand();
if (hostName == null)
{
throw new ArgumentNullException("hostName");
}
if (TryParseAsIP(hostName, out address) && ((address.AddressFamily != AddressFamily.InterNetworkV6) || Socket.LegacySupportsIPv6))
{
hostByName = InternalGetHostByAddress(address, false, false);
}
else
{
hostByName = InternalGetHostByName(hostName, false);
}
if (Logging.On)
{
Logging.Exit(Logging.Sockets, "DNS", "Resolve", hostByName);
}
return hostByName;
}