FindFirstNonLoopbackAddress

    public class InetUtils {  

        public static IPAddress FindFirstNonLoopbackAddress() {
            IPAddress iPAddress = null;
            try {
                int num = int.MaxValue;
                NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface networkInterface in allNetworkInterfaces) {
                    if (networkInterface.OperationalStatus != OperationalStatus.Up || networkInterface.IsReceiveOnly) {
                        continue;
                    }
                    LogUtil.LogInfo( string.Format("Testing interface: {0}, {1}", networkInterface.Name, networkInterface.Id));
                    IPInterfaceProperties iPProperties = networkInterface.GetIPProperties();
                    IPv4InterfaceProperties iPv4Properties = iPProperties.GetIPv4Properties();
                    if (iPv4Properties.Index < num || iPAddress == null) {
                        num = iPv4Properties.Index;
                    } else if (iPAddress != null) {
                        continue;
                    } 
                    foreach (UnicastIPAddressInformation unicastAddress in iPProperties.UnicastAddresses) {
                        IPAddress address = unicastAddress.Address;
                        LogUtil.LogInfo(string.Format("Found Ip : {0}", address.ToString()));
                        if (IsInet4Address(address) && !IsLoopbackAddress(address)  ) {
                            LogUtil.LogInfo(string.Format("Found non-loopback interface: {0}", address.ToString()));
                            iPAddress = address;
                        }
                    }
                }
            } catch (Exception exception) {
                LogUtil.LogError(exception, "Cannot get first non-loopback address");
            }
            if (iPAddress != null) {
                return iPAddress;
            }
            return GetHostAddress();
        }

        internal static bool IsInet4Address(IPAddress address) {
            return address.AddressFamily == AddressFamily.InterNetwork;
        }

        internal static bool IsLoopbackAddress(IPAddress address) {
            return IPAddress.IsLoopback(address);
        }  

        internal static IPAddress ResolveHostAddress(string hostName) {
            IPAddress result = null;
            try {
                IPAddress[] hostAddresses = Dns.GetHostAddresses(hostName);
                if (hostAddresses != null) {
                    if (hostAddresses.Length != 0) {
                        IPAddress[] array = hostAddresses;
                        foreach (IPAddress iPAddress in array) {
                            if (iPAddress.AddressFamily.Equals(AddressFamily.InterNetwork)) {
                                result = iPAddress;
                                return result;
                            }
                        }
                        return result;
                    }
                    return result;
                }
                return result;
            } catch (Exception exception) { 
                return result;
            }
        }

        internal static string ResolveHostName() {
            string text = null;
            try {
                text = Dns.GetHostName();
                if (!string.IsNullOrEmpty(text)) {
                    IPHostEntry hostEntry = Dns.GetHostEntry(text);
                    if (hostEntry != null) {
                        return hostEntry.HostName;
                    }
                    return text;
                }
                return text;
            } catch (Exception exception) { 
                return text;
            }
        }

        internal static string GetHostName() {
            return ResolveHostName();
        }

        internal static IPAddress GetHostAddress() {
            string hostName = GetHostName();
            if (!string.IsNullOrEmpty(hostName)) {
                return ResolveHostAddress(hostName);
            }
            return null;
        }

        internal static bool IsSiteLocalAddress(IPAddress address) {
            string text = address.ToString();
            if (!text.StartsWith("10.") && !text.StartsWith("172.16.")) {
                return text.StartsWith("192.168.");
            }
            return true;
        }
    }

 

posted on 2023-09-15 15:57  jonney_wang  阅读(62)  评论(0编辑  收藏  举报

导航