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

GetHostByAddr() public static method

public static GetHostByAddr ( IPAddress address ) : IPHostEntry
address IPAddress
return IPHostEntry
        public static IPHostEntry GetHostByAddr(IPAddress address)
        {
            // TODO #2891: Optimize this (or decide if this legacy code can be removed):
            int addressAsInt = unchecked((int)address.GetAddress());

#if BIGENDIAN
            // TODO #2891: above code needs testing for BIGENDIAN.

            addressAsInt = (int)(((uint)addressAsInt << 24) | (((uint)addressAsInt & 0x0000FF00) << 8) |
                (((uint)addressAsInt >> 8) & 0x0000FF00) | ((uint)addressAsInt >> 24));
#endif

            IntPtr nativePointer =
                Interop.Winsock.gethostbyaddr(
                    ref addressAsInt,
                    Marshal.SizeOf<int>(),
                    ProtocolFamily.InterNetwork);
            
            if (nativePointer != IntPtr.Zero)
            {
                return NativeToHostEntry(nativePointer);
            }

            throw new SocketException();
        }

Usage Example

Example #1
0
        } // GetHostByName

        // Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).
        internal static IPHostEntry InternalGetHostByAddress(IPAddress address, bool includeIPv6)
        {
            GlobalLog.Print("Dns.InternalGetHostByAddress: " + address.ToString());
            //
            // IPv6 Changes: We need to use the new getnameinfo / getaddrinfo functions
            //               for resolution of IPv6 addresses.
            //

            if (SocketProtocolSupportPal.OSSupportsIPv6 || includeIPv6)
            {
                //
                // 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 (Logging.On)
                    {
                        Logging.Exception(Logging.Sockets, "DNS",
                                          "InternalGetHostByAddress", new InternalSocketException(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 new InternalSocketException(errorCode, nativeErrorCode);
            }

            //
            // If IPv6 is not enabled (maybe config switch) but we've been
            // given an IPv6 address then we need to bail out now.
            //
            else
            {
                if (address.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    //
                    // Protocol not supported
                    //
                    throw new SocketException((int)SocketError.ProtocolNotSupported);
                }
                //
                // Use gethostbyaddr() to try to resolve the IP address
                //
                // End IPv6 Changes
                //
                return(NameResolutionPal.GetHostByAddr(address));
            }
        } // InternalGetHostByAddress