public static string TryGetNameInfo(IPAddress addr, out SocketError errorCode, out int nativeErrorCode)
{
//
// Use SocketException here to show operation not supported
// if, by some nefarious means, this method is called on an
// unsupported platform.
//
SocketAddress address = (new IPEndPoint(addr, 0)).Serialize();
StringBuilder hostname = new StringBuilder(1025); // NI_MAXHOST
int flags = (int)Interop.Winsock.NameInfoFlags.NI_NAMEREQD;
nativeErrorCode = 0;
// TODO #2891: Remove the copying step to improve performance. This requires a change in the contracts.
byte[] addressBuffer = new byte[address.Size];
for (int i = 0; i < address.Size; i++)
{
addressBuffer[i] = address[i];
}
errorCode =
Interop.Winsock.GetNameInfoW(
addressBuffer,
address.Size,
hostname,
hostname.Capacity,
null, // We don't want a service name
0, // so no need for buffer or length
flags);
if (errorCode != SocketError.Success)
{
return null;
}
return hostname.ToString();
}