예제 #1
0
        public static IAsyncResult BeginResolve(string hostName, AsyncCallback requestCallback, object stateObject)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(null, hostName);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            IAsyncResult asyncResult = HostResolutionBeginHelper(hostName, false, false, false, requestCallback, stateObject);

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(null, asyncResult);
            }
            return(asyncResult);
        } // BeginResolve
예제 #2
0
        } // BeginResolve

        public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback requestCallback, object stateObject)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(null, address);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            IAsyncResult asyncResult = HostResolutionBeginHelper(address, true, true, requestCallback, stateObject);

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(null, asyncResult);
            }
            return(asyncResult);
        } // BeginResolve
예제 #3
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
예제 #4
0
파일: DNS.cs 프로젝트: vikas0380/corefx
        public static IPHostEntry Resolve(string hostName)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "Resolve", hostName);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostName == null)
            {
                throw new ArgumentNullException(nameof(hostName));
            }

            // See if it's an IP Address.
            IPAddress   address;
            IPHostEntry ipHostEntry;

            if (IPAddress.TryParse(hostName, out address) && (address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6))
            {
                try
                {
                    ipHostEntry = InternalGetHostByAddress(address, false);
                }
                catch (SocketException ex)
                {
                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.PrintError(NetEventSource.ComponentType.Socket, "DNS", "DNS.Resolve", ex.Message);
                    }

                    ipHostEntry = NameResolutionUtilities.GetUnresolvedAnswer(address);
                }
            }
            else
            {
                ipHostEntry = InternalGetHostByName(hostName, false);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "Resolve", ipHostEntry);
            }
            return(ipHostEntry);
        }
예제 #5
0
        public static IPHostEntry GetHostByName(string hostName)
        {
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostName == null)
            {
                throw new ArgumentNullException(nameof(hostName));
            }

            // See if it's an IP Address.
            IPAddress address;

            if (IPAddress.TryParse(hostName, out address))
            {
                return(NameResolutionUtilities.GetUnresolvedAnswer(address));
            }
            return(InternalGetHostByName(hostName));
        }
예제 #6
0
        public static IPHostEntry Resolve(string hostName)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(hostName, hostName);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostName is null)
            {
                throw new ArgumentNullException(nameof(hostName));
            }

            // See if it's an IP Address.
            IPHostEntry ipHostEntry;

            if (IPAddress.TryParse(hostName, out IPAddress? address) &&
                (address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6))
            {
                try
                {
                    ipHostEntry = GetHostEntryCore(address);
                }
                catch (SocketException ex)
                {
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Error(hostName, ex);
                    }
                    ipHostEntry = CreateHostEntryForAddress(address);
                }
            }
            else
            {
                ipHostEntry = GetHostEntryCore(hostName);
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(hostName, ipHostEntry);
            }
            return(ipHostEntry);
        }
예제 #7
0
        public static Task <IPHostEntry> GetHostEntryAsync(IPAddress address)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, address);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
            {
                throw new ArgumentException(SR.net_invalid_ip_addr, nameof(address));
            }

            return(RunAsync(s => GetHostEntryCore((IPAddress)s), address));
        }
예제 #8
0
        public static IPHostEntry GetHostByAddress(IPAddress address)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(address, address);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            IPHostEntry ipHostEntry = GetHostEntryCore(address);

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(address, ipHostEntry);
            }
            return(ipHostEntry);
        }
예제 #9
0
        public static IPHostEntry GetHostByAddress(string address)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(null, address);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            IPHostEntry ipHostEntry = InternalGetHostByAddress(IPAddress.Parse(address));

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(null, ipHostEntry);
            }
            return(ipHostEntry);
        } // GetHostByAddress
예제 #10
0
        public static IPHostEntry GetHostEntry(string hostNameOrAddress)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostNameOrAddress is null)
            {
                throw new ArgumentNullException(nameof(hostNameOrAddress));
            }

            // See if it's an IP Address.
            IPHostEntry ipHostEntry;

            if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
            {
                if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
                {
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Error(address, $"Invalid address '{address}'");
                    }
                    throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress)));
                }

                ipHostEntry = GetHostEntryCore(address);
            }
            else
            {
                ipHostEntry = GetHostEntryCore(hostNameOrAddress);
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(hostNameOrAddress, $"{ipHostEntry} with {ipHostEntry.AddressList.Length} entries");
            }
            return(ipHostEntry);
        }
예제 #11
0
        public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(hostNameOrAddress, hostNameOrAddress);
            }
            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostNameOrAddress is null)
            {
                throw new ArgumentNullException(nameof(hostNameOrAddress));
            }

            // See if it's an IP Address.
            IPAddress[] addresses;
            if (IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
            {
                if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
                {
                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Error(address, $"Invalid address '{address}'");
                    }
                    throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress)));
                }

                addresses = new IPAddress[] { address };
            }
            else
            {
                addresses = GetHostAddressesCore(hostNameOrAddress);
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(hostNameOrAddress, addresses);
            }
            return(addresses);
        }
예제 #12
0
파일: DNS.cs 프로젝트: vikas0380/corefx
        public static IPHostEntry GetHostByAddress(IPAddress address)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostByAddress", "");
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            IPHostEntry ipHostEntry = InternalGetHostByAddress(address, false);

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostByAddress", ipHostEntry);
            }
            return(ipHostEntry);
        } // GetHostByAddress
예제 #13
0
파일: DNS.cs 프로젝트: vikas0380/corefx
        } // GetHostEntry

        public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostAddresses", hostNameOrAddress);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostNameOrAddress == null)
            {
                throw new ArgumentNullException(nameof(hostNameOrAddress));
            }

            // See if it's an IP Address.
            IPAddress address;

            IPAddress[] addresses;
            if (IPAddress.TryParse(hostNameOrAddress, out address))
            {
                if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
                {
                    throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress)));
                }
                addresses = new IPAddress[] { address };
            }
            else
            {
                // InternalGetHostByName works with IP addresses (and avoids a reverse-lookup), but we need
                // explicit handling in order to do the ArgumentException and guarantee the behavior.
                addresses = InternalGetHostByName(hostNameOrAddress, true).AddressList;
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostAddresses", addresses);
            }
            return(addresses);
        }
예제 #14
0
파일: DNS.cs 프로젝트: vikas0380/corefx
        } // EndGetHostByName()

        public static IPHostEntry GetHostEntry(string hostNameOrAddress)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostEntry", hostNameOrAddress);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostNameOrAddress == null)
            {
                throw new ArgumentNullException(nameof(hostNameOrAddress));
            }

            // See if it's an IP Address.
            IPAddress   address;
            IPHostEntry ipHostEntry;

            if (IPAddress.TryParse(hostNameOrAddress, out address))
            {
                if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
                {
                    throw new ArgumentException(SR.Format(SR.net_invalid_ip_addr, nameof(hostNameOrAddress)));
                }

                ipHostEntry = InternalGetHostByAddress(address, true);
            }
            else
            {
                ipHostEntry = InternalGetHostByName(hostNameOrAddress, true);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostEntry", ipHostEntry);
            }
            return(ipHostEntry);
        }
예제 #15
0
        /// <summary>Gets the host name of the local machine.</summary>
        public static string GetHostName()
        {
            ValueStopwatch stopwatch = NameResolutionTelemetry.Log.BeforeResolution(string.Empty);

            string name;

            try
            {
                name = NameResolutionPal.GetHostName();
            }
            catch when(LogFailure(stopwatch))
            {
                Debug.Fail("LogFailure should return false");
                throw;
            }

            NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Info(null, name);
            }
            return(name);
        }
예제 #16
0
        private static IPHostEntry InternalGetHostByName(string hostName)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(null, hostName);
            }
            IPHostEntry ipHostEntry = null;

            ValidateHostName(hostName);

            int         nativeErrorCode;
            SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, out ipHostEntry, out nativeErrorCode);

            if (errorCode != SocketError.Success)
            {
                throw SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode);
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Exit(null, ipHostEntry);
            }
            return(ipHostEntry);
        } // GetHostByName
예제 #17
0
        private static object GetHostEntryOrAddressesCore(string hostName, bool justAddresses)
        {
            ValidateHostName(hostName);

            SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, out string?newHostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);
예제 #18
0
파일: DNS.cs 프로젝트: thiagodin/corefx
        } // InternalGetHostByAddress

        /*****************************************************************************
         * Function :    gethostname
         *
         * Abstract:     Queries the hostname from DNS
         *
         * Input Parameters:
         *
         * Returns: String
         ******************************************************************************/

        /// <devdoc>
        ///    <para>Gets the host name of the local machine.</para>
        /// </devdoc>
        public static string GetHostName()
        {
            GlobalLog.Print("Dns.GetHostName");
            return(NameResolutionPal.GetHostName());
        }
예제 #19
0
파일: DNS.cs 프로젝트: zuhuizou/corefx
        internal static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, "DNS", "GetHostByName", hostName);
            }
            IPHostEntry ipHostEntry = null;

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("Dns.GetHostByName: " + hostName);
            }

            NameResolutionPal.EnsureSocketsAreInitialized();

            if (hostName.Length > MaxHostName || // If 255 chars, the last one must be a dot.
                hostName.Length == MaxHostName && hostName[MaxHostName - 1] != '.')
            {
                throw new ArgumentOutOfRangeException(nameof(hostName), SR.Format(SR.net_toolong,
                                                                                  "hostName", MaxHostName.ToString(NumberFormatInfo.CurrentInfo)));
            }

            //
            // IPv6 Changes: IPv6 requires the use of getaddrinfo() rather
            //               than the traditional IPv4 gethostbyaddr() / gethostbyname().
            //               getaddrinfo() is also protocol independent in that it will also
            //               resolve IPv4 names / addresses. As a result, it is the preferred
            //               resolution mechanism on platforms that support it (Windows 5.1+).
            //               If getaddrinfo() is unsupported, IPv6 resolution does not work.
            //
            // Consider    : If IPv6 is disabled, we could detect IPv6 addresses
            //               and throw an unsupported platform exception.
            //
            // Note        : Whilst getaddrinfo is available on WinXP+, we only
            //               use it if IPv6 is enabled (platform is part of that
            //               decision). This is done to minimize the number of
            //               possible tests that are needed.
            //
            if (includeIPv6 || SocketProtocolSupportPal.OSSupportsIPv6)
            {
                //
                // IPv6 enabled: use getaddrinfo() to obtain DNS information.
                //
                int         nativeErrorCode;
                SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, out ipHostEntry, out nativeErrorCode);
                if (errorCode != SocketError.Success)
                {
                    throw new InternalSocketException(errorCode, nativeErrorCode);
                }
            }
            else
            {
                ipHostEntry = NameResolutionPal.GetHostByName(hostName);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostByName", ipHostEntry);
            }
            return(ipHostEntry);
        } // GetHostByName
예제 #20
0
파일: DNS.cs 프로젝트: thiagodin/corefx
        } // 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
예제 #21
0
        // Helpers for async GetHostByName, ResolveToAddresses, and Resolve - they're almost identical
        // If hostName is an IPString and justReturnParsedIP==true then no reverse lookup will be attempted, but the original address is returned.
        private static IAsyncResult HostResolutionBeginHelper(string hostName, bool justReturnParsedIp, bool throwOnIIPAny, AsyncCallback requestCallback, object state)
        {
            if (hostName == null)
            {
                throw new ArgumentNullException(nameof(hostName));
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Info(null, hostName);
            }

            // See if it's an IP Address.
            IPAddress             ipAddress;
            DnsResolveAsyncResult asyncResult;

            if (IPAddress.TryParse(hostName, out ipAddress))
            {
                if (throwOnIIPAny && (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.IPv6Any)))
                {
                    throw new ArgumentException(SR.net_invalid_ip_addr, nameof(hostName));
                }

                asyncResult = new DnsResolveAsyncResult(ipAddress, null, state, requestCallback);

                if (justReturnParsedIp)
                {
                    IPHostEntry hostEntry = NameResolutionUtilities.GetUnresolvedAnswer(ipAddress);
                    asyncResult.StartPostingAsyncOp(false);
                    asyncResult.InvokeCallback(hostEntry);
                    asyncResult.FinishPostingAsyncOp();
                    return(asyncResult);
                }
            }
            else
            {
                asyncResult = new DnsResolveAsyncResult(hostName, null, state, requestCallback);
            }

            // Set up the context, possibly flow.
            asyncResult.StartPostingAsyncOp(false);

            // If the OS supports it and 'hostName' is not an IP Address, resolve the name asynchronously
            // instead of calling the synchronous version in the ThreadPool.
            if (NameResolutionPal.SupportsGetAddrInfoAsync && ipAddress == null)
            {
                ValidateHostName(hostName);
                NameResolutionPal.GetAddrInfoAsync(asyncResult);
            }
            else
            {
                // Start the resolve.
                Task.Factory.StartNew(
                    s => ResolveCallback(s),
                    asyncResult,
                    CancellationToken.None,
                    TaskCreationOptions.DenyChildAttach,
                    TaskScheduler.Default);
            }

            // Finish the flowing, maybe it completed?  This does nothing if we didn't initiate the flowing above.
            asyncResult.FinishPostingAsyncOp();
            return(asyncResult);
        }