Exemple #1
0
 private static IPAddress ParseIPV6(string ip)
 {
     if (IPv6Address.TryParse(ip, out IPv6Address result))
     {
         return(new IPAddress(result.Address, result.ScopeId));
     }
     return(null);
 }
Exemple #2
0
        private static IPAddress ParseIPV6(string ip)
        {
            IPv6Address newIPv6Address;

            if (IPv6Address.TryParse(ip, out newIPv6Address))
            {
                return(new IPAddress(newIPv6Address.Address, newIPv6Address.ScopeId));
            }
            return(null);
        }
Exemple #3
0
        public static IPv6Address Parse(string ipString)
        {
            if (ipString == null)
            {
                throw new ArgumentNullException("ipString");
            }
            IPv6Address result;

            if (IPv6Address.TryParse(ipString, out result))
            {
                return(result);
            }
            throw new FormatException("Not a valid IPv6 address");
        }
Exemple #4
0
        public static bool TryParse(string ipString, out IPv6Address result)
        {
            result = null;
            if (ipString == null)
            {
                return(false);
            }
            if (ipString.Length > 2 && ipString[0] == '[' && ipString[ipString.Length - 1] == ']')
            {
                ipString = ipString.Substring(1, ipString.Length - 2);
            }
            if (ipString.Length < 2)
            {
                return(false);
            }
            int num  = 0;
            int num2 = 0;
            int num3 = ipString.LastIndexOf('/');

            if (num3 != -1)
            {
                string prefix = ipString.Substring(num3 + 1);
                if (!IPv6Address.TryParse(prefix, out num))
                {
                    num = -1;
                }
                if (num < 0 || num > 128)
                {
                    return(false);
                }
                ipString = ipString.Substring(0, num3);
            }
            else
            {
                num3 = ipString.LastIndexOf('%');
                if (num3 != -1)
                {
                    string prefix2 = ipString.Substring(num3 + 1);
                    if (!IPv6Address.TryParse(prefix2, out num2))
                    {
                        num2 = 0;
                    }
                    ipString = ipString.Substring(0, num3);
                }
            }
            ushort[] array = new ushort[8];
            bool     flag  = false;
            int      num4  = ipString.LastIndexOf(':');

            if (num4 == -1)
            {
                return(false);
            }
            int num5 = 0;

            if (num4 < ipString.Length - 1)
            {
                string text = ipString.Substring(num4 + 1);
                if (text.IndexOf('.') != -1)
                {
                    IPAddress ipaddress;
                    if (!IPAddress.TryParse(text, out ipaddress))
                    {
                        return(false);
                    }
                    long internalIPv4Address = ipaddress.InternalIPv4Address;
                    array[6] = (ushort)(((int)(internalIPv4Address & 255L) << 8) + (int)(internalIPv4Address >> 8 & 255L));
                    array[7] = (ushort)(((int)(internalIPv4Address >> 16 & 255L) << 8) + (int)(internalIPv4Address >> 24 & 255L));
                    if (num4 > 0 && ipString[num4 - 1] == ':')
                    {
                        ipString = ipString.Substring(0, num4 + 1);
                    }
                    else
                    {
                        ipString = ipString.Substring(0, num4);
                    }
                    flag = true;
                    num5 = 2;
                }
            }
            int num6 = ipString.IndexOf("::");

            if (num6 != -1)
            {
                int num7 = IPv6Address.Fill(array, ipString.Substring(num6 + 2));
                if (num7 == -1)
                {
                    return(false);
                }
                if (num7 + num5 > 8)
                {
                    return(false);
                }
                int num8 = 8 - num5 - num7;
                for (int i = num7; i > 0; i--)
                {
                    array[i + num8 - 1] = array[i - 1];
                    array[i - 1]        = 0;
                }
                int num9 = IPv6Address.Fill(array, ipString.Substring(0, num6));
                if (num9 == -1)
                {
                    return(false);
                }
                if (num9 + num7 + num5 > 7)
                {
                    return(false);
                }
            }
            else if (IPv6Address.Fill(array, ipString) != 8 - num5)
            {
                return(false);
            }
            bool flag2 = false;

            for (int j = 0; j < num5; j++)
            {
                if (array[j] != 0 || (j == 5 && array[j] != 65535))
                {
                    flag2 = true;
                }
            }
            if (flag && !flag2)
            {
                for (int k = 0; k < 5; k++)
                {
                    if (array[k] != 0)
                    {
                        return(false);
                    }
                }
                if (array[5] != 0 && array[5] != 65535)
                {
                    return(false);
                }
            }
            result = new IPv6Address(array, num, num2);
            return(true);
        }