Esempio n. 1
0
        public string StringToInt(string ipDir)
        {
            IPAddress  ipaddres = IPAddress.Parse(ipDir);
            BigInteger ipDirDec = IPNetwork.ToBigInteger(ipaddres);

            return(ipDirDec.ToString());
        }
        private int GetIpRangeId(IPNetwork network)
        {
            // The subnet should belong to the ip range with which it has the smallest gap.
            int        ipRangeId   = -1;
            BigInteger smallestGap = new BigInteger(16777216); // The largest number of addresses (10.0.0.0/8).
            BigInteger networkIP   = IPNetwork.ToBigInteger(network.Network);

            foreach (var ipRange in _vnet.IPRanges)
            {
                if (ipRange.IPNetwork != null)
                {
                    var irIP = IPNetwork.ToBigInteger(ipRange.IPNetwork.Network);
                    var gap  = BigInteger.Abs(networkIP - irIP);
                    if (smallestGap > gap)
                    {
                        smallestGap = gap;
                        ipRangeId   = ipRange.Id;
                        if (smallestGap == 0)
                        {
                            break;
                        }
                    }
                }
            }

            return(ipRangeId);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// IPV4 :
        ///
        /// Class              Leading bits    Default netmask
        ///     A (CIDR /8)	       00           255.0.0.0
        ///     A (CIDR /8)	       01           255.0.0.0
        ///     B (CIDR /16)	   10           255.255.0.0
        ///     C (CIDR /24)       11           255.255.255.0
        ///
        /// IPV6 : 64
        ///
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cidr"></param>
        /// <returns></returns>
        public bool TryGuessCidr(string ip, out byte cidr)
        {
            IPAddress ipaddress = null;
            bool      parsed    = IPAddress.TryParse(string.Format("{0}", ip), out ipaddress);

            if (parsed == false)
            {
                cidr = 0;
                return(false);
            }

            if (ipaddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                cidr = 64;
                return(true);
            }
            BigInteger uintIPAddress = IPNetwork.ToBigInteger(ipaddress);

            uintIPAddress = uintIPAddress >> 30;
            if (uintIPAddress <= 1)
            {
                cidr = 8;
                return(true);
            }
            else if (uintIPAddress <= 2)
            {
                cidr = 16;
                return(true);
            }
            else if (uintIPAddress <= 3)
            {
                cidr = 24;
                return(true);
            }

            cidr = 0;
            return(false);
        }
Esempio n. 4
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var results = new List <ValidationResult>();

            // Subnet name must be unique.
            if (VirtualNetwork.Subnets.Where(s => s.Name.Equals(Name)).FirstOrDefault() != null)
            {
                results.Add(new ValidationResult("Subnet name must be unique within a virtual network."));
            }
            // IpPerInstance must be > 0 for non fixed cidr.
            if (!Service.FixedSubnetCidr && IpPerInstance <= 0)
            {
                results.Add(new ValidationResult("Address/Instance must be greater than 0."));
            }
            // Number of instances need to be between min and max.
            if (!Service.FixedSubnetCidr && Service.MaxInstances > Service.MinInstances &&
                (ServiceInstances > Service.MaxInstances || ServiceInstances < Service.MinInstances))
            {
                results.Add(new ValidationResult($"The instance must be between {Service.MinInstances} and {Service.MaxInstances}."));
            }

            var       ipAddress = IPAddress.Parse(StartIP);
            IPNetwork network1  = IPNetwork.Parse("10.0.0.0/8");
            IPNetwork network2  = IPNetwork.Parse("172.16.0.0/12");
            IPNetwork network3  = IPNetwork.Parse("192.168.0.0/16");

            if (!network1.Contains(ipAddress) && !network2.Contains(ipAddress) && !network3.Contains(ipAddress))
            {
                results.Add(new ValidationResult($"The start ip is not in the range of RFC1918."));
                return(results);
            }

            var network = Network;

            if (IPRangeId < 0)
            {
                results.Add(new ValidationResult($"The subnet address range {network} is not contained in the virtual network's address spaces."));
                return(results);
            }
            // Validate CIDR block.
            if (!Equals(network.Network, ipAddress))
            {
                var current = IPNetwork.ToBigInteger(network.Network);
                var next    = current + network.Total;
                var nextIp  = IPNetwork.ToIPAddress(next, AddressFamily.InterNetwork);
                results.Add(new ValidationResult($"{StartIP}/{network.Cidr} is not a valid CIDR block. Try {network.Network} or {nextIp} instead."));
                return(results);
            }

            // Overlap with other subnets
            foreach (var subnet in VirtualNetwork.Subnets)
            {
                if (network.Overlap(subnet.Network))
                {
                    results.Add(new ValidationResult($"The subnet address range {network} overlaps with {subnet.Name} address range {subnet.Network}."));
                    break;
                }
            }

            return(results);
        }