System.Net.NameResolutionPal.TryGetNameInfo C# (CSharp) Method

TryGetNameInfo() public static method

public static TryGetNameInfo ( IPAddress addr, SocketError &errorCode, int &nativeErrorCode ) : string
addr IPAddress
errorCode SocketError
nativeErrorCode int
return string
        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();
        }

Usage Example

Example #1
0
        } // GetHostByAddress

        // Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).
        private static IPHostEntry InternalGetHostByAddress(IPAddress address)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, address);
            }

            //
            // Try to get the data for the host from it's address
            //
            // We need to call getnameinfo first, because getaddrinfo w/ the ipaddress string
            // will only return that address and not the full list.

            // Do a reverse lookup to get the host name.
            SocketError errorCode;
            int         nativeErrorCode;
            string      name = NameResolutionPal.TryGetNameInfo(address, out errorCode, out nativeErrorCode);

            if (errorCode == SocketError.Success)
            {
                // Do the forward lookup to get the IPs for that host name
                IPHostEntry hostEntry;
                errorCode = NameResolutionPal.TryGetAddrInfo(name, out hostEntry, out nativeErrorCode);
                if (errorCode == SocketError.Success)
                {
                    return(hostEntry);
                }

                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error(null, SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode));
                }

                // One of two things happened:
                // 1. There was a ptr record in dns, but not a corollary A/AAA record.
                // 2. The IP was a local (non-loopback) IP that resolved to a connection specific dns suffix.
                //    - Workaround, Check "Use this connection's dns suffix in dns registration" on that network
                //      adapter's advanced dns settings.

                // Just return the resolved host name and no IPs.
                return(hostEntry);
            }

            throw SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode);
        } // InternalGetHostByAddress
All Usage Examples Of System.Net.NameResolutionPal::TryGetNameInfo