示例#1
0
        /// <summary>
        /// Get all local ip addresses
        /// </summary>
        /// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
        /// <returns>List with all local ip adresses</returns>
        public static List <string> GetLocalIpList(LocalAddrType addrType)
        {
            List <string> targetList = new List <string>();

            GetLocalIpList(targetList, addrType);
            return(targetList);
        }
示例#2
0
 /// <summary>
 /// Get first detected local ip address
 /// </summary>
 /// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
 /// <returns>IP address if available. Else - string.Empty</returns>
 public static string GetLocalIp(LocalAddrType addrType)
 {
     lock (IpList) {
         IpList.Clear();
         GetLocalIpList(IpList, addrType);
         return(IpList.Count == 0 ? string.Empty : IpList[0]);
     }
 }
示例#3
0
        /// <summary>
        /// Get all local ip addresses (non alloc version)
        /// </summary>
        /// <param name="targetList">result list</param>
        /// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
        public static void GetLocalIpList(List <string> targetList, LocalAddrType addrType)
        {
            bool ipv4 = (addrType & LocalAddrType.IPv4) == LocalAddrType.IPv4;
            bool ipv6 = (addrType & LocalAddrType.IPv6) == LocalAddrType.IPv6;

            try
            {
                foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
                {
                    //Skip loopback and disabled network interfaces
                    if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
                        ni.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }

                    var ipProps = ni.GetIPProperties();

                    //Skip address without gateway
                    if (ipProps.GatewayAddresses.Count == 0)
                    {
                        continue;
                    }

                    foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
                    {
                        var address = ip.Address;
                        if ((ipv4 && address.AddressFamily == AddressFamily.InterNetwork) ||
                            (ipv6 && address.AddressFamily == AddressFamily.InterNetworkV6))
                        {
                            targetList.Add(address.ToString());
                        }
                    }
                }
            }
            catch
            {
                //ignored
            }

            //Fallback mode (unity android)
            if (targetList.Count == 0)
            {
#if NETCORE
                var hostTask = Dns.GetHostEntryAsync(Dns.GetHostName());
                hostTask.Wait();
                var host = hostTask.Result;
#else
                var host = Dns.GetHostEntry(Dns.GetHostName());
#endif
                foreach (IPAddress ip in host.AddressList)
                {
                    if ((ipv4 && ip.AddressFamily == AddressFamily.InterNetwork) ||
                        (ipv6 && ip.AddressFamily == AddressFamily.InterNetworkV6))
                    {
                        targetList.Add(ip.ToString());
                    }
                }
            }
            if (targetList.Count == 0)
            {
                if (ipv4)
                {
                    targetList.Add("127.0.0.1");
                }
                if (ipv6)
                {
                    targetList.Add("::1");
                }
            }
        }
示例#4
0
        public static void GetLocalIpList(List <string> targetList, LocalAddrType addrType)
        {
            var ipv4 = (addrType & LocalAddrType.IPv4) == LocalAddrType.IPv4;
            var ipv6 = (addrType & LocalAddrType.IPv6) == LocalAddrType.IPv6;

#if WINRT && !UNITY_EDITOR
            foreach (HostName localHostName in NetworkInformation.GetHostNames())
            {
                if (localHostName.IPInformation != null &&
                    ((ipv4 && localHostName.Type == HostNameType.Ipv4) ||
                     (ipv6 && localHostName.Type == HostNameType.Ipv6)))
                {
                    targetList.Add(localHostName.ToString());
                }
            }
#else
            try
            {
                foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
                {
                    //Skip loopback
                    if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                    {
                        continue;
                    }

                    var ipProps = ni.GetIPProperties();

                    //Skip address without gateway
                    if (ipProps.GatewayAddresses.Count == 0)
                    {
                        continue;
                    }

                    foreach (var ip in ipProps.UnicastAddresses)
                    {
                        var address = ip.Address;
                        if ((ipv4 && address.AddressFamily == AddressFamily.InterNetwork) ||
                            (ipv6 && address.AddressFamily == AddressFamily.InterNetworkV6))
                        {
                            targetList.Add(address.ToString());
                        }
                    }
                }
            }
            catch
            {
                //ignored
            }

            //Fallback mode (unity android)
            if (targetList.Count == 0)
            {
#if NETCORE
                var hostTask = Dns.GetHostEntryAsync(Dns.GetHostName());
                hostTask.Wait();
                var host = hostTask.Result;
#else
                var host = Dns.GetHostEntry(Dns.GetHostName());
#endif
                foreach (var ip in host.AddressList)
                {
                    if ((ipv4 && ip.AddressFamily == AddressFamily.InterNetwork) ||
                        (ipv6 && ip.AddressFamily == AddressFamily.InterNetworkV6))
                    {
                        targetList.Add(ip.ToString());
                    }
                }
            }
#endif
            if (targetList.Count == 0)
            {
                if (ipv4)
                {
                    targetList.Add("127.0.0.1");
                }
                if (ipv6)
                {
                    targetList.Add("::1");
                }
            }
        }
示例#5
0
        /// <summary>
        /// Get all local ip addresses (non alloc version)
        /// </summary>
        /// <param name="targetList">result list</param>
        /// <param name="addrType">type of address (IPv4, IPv6 or both)</param>
        public static void GetLocalIpList(IList <string> targetList, LocalAddrType addrType)
        {
            bool ipv4 = (addrType & LocalAddrType.IPv4) == LocalAddrType.IPv4;
            bool ipv6 = (addrType & LocalAddrType.IPv6) == LocalAddrType.IPv6;

            try
            {
                foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
                {
                    //Skip loopback and disabled network interfaces
                    if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
                        ni.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }

                    IPInterfaceProperties ipProps = ni.GetIPProperties();

                    //Skip address without gateway
                    if (ipProps.GatewayAddresses.Count == 0)
                    {
                        continue;
                    }

                    foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
                    {
                        IPAddress address = ip.Address;
                        if ((ipv4 && address.AddressFamily == AddressFamily.InterNetwork) ||
                            (ipv6 && address.AddressFamily == AddressFamily.InterNetworkV6))
                        {
                            targetList.Add(address.ToString());
                        }
                    }
                }
            }
            catch
            {
                //ignored
            }

            //Fallback mode (unity android)
            if (targetList.Count == 0)
            {
                IPAddress[] addresses = ResolveAddresses(Dns.GetHostName());
                foreach (IPAddress ip in addresses)
                {
                    if ((ipv4 && ip.AddressFamily == AddressFamily.InterNetwork) ||
                        (ipv6 && ip.AddressFamily == AddressFamily.InterNetworkV6))
                    {
                        targetList.Add(ip.ToString());
                    }
                }
            }
            if (targetList.Count == 0)
            {
                if (ipv4)
                {
                    targetList.Add("127.0.0.1");
                }

                if (ipv6)
                {
                    targetList.Add("::1");
                }
            }
        }