private 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); } 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, nameof(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 SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode); } } else { ipHostEntry = NameResolutionPal.GetHostByName(hostName); } if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exit(NetEventSource.ComponentType.Socket, "DNS", "GetHostByName", ipHostEntry); } return(ipHostEntry); } // GetHostByName
public static unsafe IPHostEntry GetHostByName(string hostName) { Interop.Sys.HostEntry entry; int err = Interop.Sys.GetHostByName(hostName, &entry); if (err != 0) { throw SocketExceptionFactory.CreateSocketException(GetSocketErrorForErrno(err), err); } return(CreateIPHostEntry(entry)); }
public static unsafe IPHostEntry GetHostByAddr(IPAddress addr) { // TODO #2891: Optimize this (or decide if this legacy code can be removed): Interop.Sys.IPAddress address = addr.GetNativeIPAddress(); Interop.Sys.HostEntry entry; int err = Interop.Sys.GetHostByAddress(&address, &entry); if (err != 0) { throw SocketExceptionFactory.CreateSocketException(GetSocketErrorForErrno(err), err); } return(CreateIPHostEntry(entry)); }
private static IPHostEntry InternalGetHostByName(string hostName, bool includeIPv6) { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null, hostName); } IPHostEntry ipHostEntry = null; ValidateHostName(hostName); // // 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 SocketExceptionFactory.CreateSocketException(errorCode, nativeErrorCode); } } else { ipHostEntry = NameResolutionPal.GetHostByName(hostName); } if (NetEventSource.IsEnabled) { NetEventSource.Exit(null, ipHostEntry); } return(ipHostEntry); } // GetHostByName
} // 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
public static unsafe IPHostEntry GetHostByName(string hostName) { if (hostName == "") { // To match documented behavior on Windows, if an empty string is passed in, use the local host's name. hostName = Dns.GetHostName(); } Interop.Sys.HostEntry entry; int err = Interop.Sys.GetHostByName(hostName, &entry); if (err != 0) { throw SocketExceptionFactory.CreateSocketException(GetSocketErrorForErrno(err), err); } return(CreateIPHostEntry(entry)); }
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
} // GetHostByAddress // Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods). private static IPHostEntry InternalGetHostByAddress(IPAddress address, bool includeIPv6) { if (GlobalLog.IsEnabled) { 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 (NetEventSource.Log.IsEnabled()) { NetEventSource.Exception( NetEventSource.ComponentType.Socket, "DNS", "InternalGetHostByAddress", 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); } // // 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