Esempio n. 1
0
        // Copied from the old version of DNS.cs
        // Returns a list of our local addresses by calling gethostbyname with null.
        //
        private static IPHostEntry GetLocalHost()
        {
        #if MONO
            return(Dns.GetHostEntry(string.Empty));
        #else
            //
            // IPv6 Changes: If IPv6 is enabled, we can't simply use the
            //               old IPv4 gethostbyname(null). Instead we need
            //               to do a more complete lookup.
            //
            if (Socket.SupportsIPv6)
            {
                //
                // IPv6 enabled: use getaddrinfo() of the local host name
                // to obtain this information. Need to get the machines
                // name as well - do that here so that we don't need to
                // Assert DNS permissions.
                //
                StringBuilder hostname  = new StringBuilder(HostNameBufferLength);
                SocketError   errorCode =
                    UnsafeNclNativeMethods.OSSOCK.gethostname(
                        hostname,
                        HostNameBufferLength);

                if (errorCode != SocketError.Success)
                {
                    throw new SocketException();
                }

                return(Dns.GetHostByName(hostname.ToString()));
            }
            else
            {
                //
                // IPv6 disabled: use gethostbyname() to obtain information.
                //
                IntPtr nativePointer =
                    UnsafeNclNativeMethods.OSSOCK.gethostbyname(
                        null);

                if (nativePointer == IntPtr.Zero)
                {
                    throw new SocketException();
                }

                return(Dns.NativeToHostEntry(nativePointer));
            }
        #endif
        } // GetLocalHost