public static string GetPrefixIP(ADDRESSFAM Addfam)
        {
            string ip    = GetIP(Addfam);
            int    index = ip.LastIndexOf('.') + 1;

            return(ip.Substring(0, index));
        }
        public static string GetExtensionIP(ADDRESSFAM Addfam)
        {
            string ip    = GetIP(Addfam);
            int    index = ip.LastIndexOf('.') + 1;

            return(ip.Substring(index, ip.Length - index));
        }
Esempio n. 3
0
        public static byte[] getIP()
        {
            byte[]              output = new byte[4];
            ADDRESSFAM          Addfam = ADDRESSFAM.IPv4;
            LinkedList <byte[]> list   = new LinkedList <byte[]>();

            foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
            {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
                NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
                NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

                if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif
                {
                    foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                    {
                        //IPv4
                        if (Addfam == ADDRESSFAM.IPv4)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                if (ip.Address.ToString() != "127.0.0.1")
                                {
                                    output = getIP(ip.Address.ToString());
                                }
                                list.AddLast(output);

                                if (ip.Address.ToString().StartsWith("192.168"))
                                {
                                    return(output);
                                }
                            }
                        }

                        //IPv6
                        else if (Addfam == ADDRESSFAM.IPv6)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                if (ip.Address.ToString() != "127.0.0.1")
                                {
                                    output = getIP(ip.Address.ToString());
                                }
                                list.AddLast(output);

                                if (ip.Address.ToString().StartsWith("192.168"))
                                {
                                    return(output);
                                }
                            }
                        }
                    }
                }
            }

            return(output);
        }
    public static void GetAllIPs(List <string> list, ADDRESSFAM Addfam, bool includeDetails)
    {
        if (list == null)
        {
            return;
        }
        list.Clear();

        //Return null if ADDRESSFAM is Ipv6 but Os does not support it
        if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
            return;
        }

        foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
        {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

            bool isCandidate = (item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2);

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            // as of MacOS (10.13) and iOS (12.1), OperationalStatus seems to be always "Unknown".
            isCandidate = isCandidate && item.OperationalStatus == OperationalStatus.Up;
#endif
            if (isCandidate)
#endif
            {
                foreach (var info in item.GetIPProperties().UnicastAddresses)
                {
                    //IPv4
                    if (Addfam == ADDRESSFAM.IPv4)
                    {
                        if (info.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            string s = info.Address.ToString();
                            if (includeDetails)
                            {
                                s += "  " + item.Description.PadLeft(6) + item.NetworkInterfaceType.ToString().PadLeft(10);
                            }
                            list.Add(s);
                        }
                    }

                    //IPv6
                    else if (Addfam == ADDRESSFAM.IPv6)
                    {
                        if (info.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            list.Add(info.Address.ToString());
                        }
                    }
                }
            }
        }
    }
    public static string GetIP(ADDRESSFAM Addfam)
    {
        string        ret = "";
        List <string> IPs = GetAllIPs(Addfam, false);

        if (IPs.Count > 0)
        {
            ret = IPs[IPs.Count - 1];
        }
        return(ret);
    }
Esempio n. 6
0
    public static string GetIP(ADDRESSFAM Addfam)
    {
        //Return null if ADDRESSFAM is Ipv6 but Os does not support it
        if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
            return(null);
        }

        string output = "";

        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

            if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
            //if (item.NetworkInterfaceType == _type1 && item.OperationalStatus == OperationalStatus.Up)
#endif
            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
                    //IPv4
                    if (Addfam == ADDRESSFAM.IPv4)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            output += ip.Address.ToString() + " (" + ip.PrefixOrigin.ToString() + ")\n\t ";
                            //Debug.Log(ip.AddressValidLifetime);
                            //Debug.Log(ip.PrefixOrigin.ToString());
                            //Debug.Log(ip.SuffixOrigin.ToString());
                            //break;
                        }
                    }

                    //IPv6
                    else if (Addfam == ADDRESSFAM.IPv6)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            output += ip.Address.ToString();
                        }
                    }
                }
                //break;
            }
        }
        return(output);
    }
Esempio n. 7
0
    public static IPAddress GetIP(ADDRESSFAM Addfam)
    {
        //Return null if ADDRESSFAM is Ipv6 but Os does not support it
        if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
            return(null);
        }

        IPAddress output = IPAddress.Any;

        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

            if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up)
#endif
            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
                    //IPv4
                    if (Addfam == ADDRESSFAM.IPv4)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            output = ip.Address;
                        }
                    }

                    //IPv6
                    else if (Addfam == ADDRESSFAM.IPv6)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            output = ip.Address;
                        }
                    }
                }
            }
        }
        return(output);
    }
Esempio n. 8
0
    public static string GetIP(ADDRESSFAM addfam)
    {
        if (addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
        {
            return((string)null);
        }
        string empty = string.Empty;

        foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
        {
            NetworkInterfaceType networkInterfaceType1 = NetworkInterfaceType.Wireless80211;
            NetworkInterfaceType networkInterfaceType2 = NetworkInterfaceType.Ethernet;
            if ((networkInterface.NetworkInterfaceType == networkInterfaceType1 || networkInterface.NetworkInterfaceType == networkInterfaceType2) && networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                foreach (UnicastIPAddressInformation unicastAddress in networkInterface.GetIPProperties().UnicastAddresses)
                {
                    switch (addfam)
                    {
                    case ADDRESSFAM.IPv4:
                        if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            empty = unicastAddress.Address.ToString();
                            continue;
                        }
                        continue;

                    case ADDRESSFAM.IPv6:
                        if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            empty = unicastAddress.Address.ToString();
                            continue;
                        }
                        continue;

                    default:
                        continue;
                    }
                }
            }
        }
        return(empty);
    }
Esempio n. 9
0
        // Get IP address by AddressFamily and domain
        private string GetIPAddress(string hostName, ADDRESSFAM AF)
        {
            if (AF == ADDRESSFAM.IPv6 && !System.Net.Sockets.Socket.OSSupportsIPv6)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(hostName))
            {
                return(null);
            }
            System.Net.IPHostEntry host;
            string connectIP = "";

            try
            {
                host = System.Net.Dns.GetHostEntry(hostName);
                foreach (System.Net.IPAddress ip in host.AddressList)
                {
                    if (AF == ADDRESSFAM.IPv4)
                    {
                        if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            connectIP = ip.ToString();
                        }
                    }
                    else if (AF == ADDRESSFAM.IPv6)
                    {
                        if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            connectIP = ip.ToString();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log("GetIPAddress error: {0}" + e.Message);
            }
            return(connectIP);
        }