Exemplo n.º 1
0
        /// <summary>
        /// Initialize host address information.
        /// </summary>
        static UUID()
        {
            bool isRealAddress = false;
            bool isIPv6        = false;
            int  addr1         = 0x00000000;
            int  addr2         = 0x00000000;
            int  addr3         = 0x00000000;
            int  addr4         = 0x00000000;

            try
            {
                IPAddress addr = NetworkUtils.GetLocalHostAddress();

                byte[] ipBytes = NO_BYTES;
                if (addr != null)
                {
                    ipBytes = addr.GetAddressBytes();
                }

                switch (ipBytes.Length)
                {
                case 4:
                {
                    int ip = (ipBytes[0] & 0xFF) << 24 | (ipBytes[1] & 0xFF) << 16 | (ipBytes[2] & 0xFF) << 8 | (ipBytes[3] & 0xFF);
                    if (ip != 0x00000000 && ip != 0x7F000001)
                    {
                        isRealAddress = true;
                        addr1         = ip;
                    }
                }
                break;


                case 16:
                {
                    int ip1 = (ipBytes[0] & 0xFF) << 24 | (ipBytes[1] & 0xFF) << 16 | (ipBytes[2] & 0xFF) << 8 | (ipBytes[3] & 0xFF);
                    int ip2 = (ipBytes[4] & 0xFF) << 24 | (ipBytes[5] & 0xFF) << 16 | (ipBytes[6] & 0xFF) << 8 | (ipBytes[7] & 0xFF);
                    int ip3 = (ipBytes[8] & 0xFF) << 24 | (ipBytes[9] & 0xFF) << 16 | (ipBytes[10] & 0xFF) << 8 | (ipBytes[11] & 0xFF);
                    int ip4 = (ipBytes[12] & 0xFF) << 24 | (ipBytes[13] & 0xFF) << 16 | (ipBytes[14] & 0xFF) << 8 | (ipBytes[15] & 0xFF);

                    if (!(ip1 == 0x00000000 && ip2 == 0x00000000 && ip3 == 0x00000000 && (ip4 == 0x00000000 || ip4 == 0x00000001)) && !((ipBytes[0] & 0xFF) == 0xFE && (ipBytes[1] & 0xC0) == 0x80))
                    // link-local
                    {
                        isRealAddress = true;
                        isIPv6        = true;
                        addr1         = ip1;
                        addr2         = ip2;
                        addr3         = ip3;
                        addr4         = ip4;
                    }
                }
                break;
                }
            }
            catch (Exception)
            {}

            s_isRealAddress = isRealAddress;
            s_isIPv6        = isIPv6;
            s_addr1         = addr1;
            s_addr2         = addr2;
            s_addr3         = addr3;
            s_addr4         = addr4;
        }