Exemplo n.º 1
0
        public static List <IPNetwork <TAddress> > ResizeNetwork <TAddress>(IPNetwork <TAddress> initialNet,
                                                                            TAddress newSubnetMask, Func <TAddress, TAddress, IPNetwork <TAddress> > createSubnet, out int netComparison)
            where TAddress : struct, IIPAddress <TAddress>
        {
            int initialHostBits = initialNet.CiscoWildcard.Bytes.Sum(b => CalcIPUtils.BytePopCount[b]);
            int newNetBits      = newSubnetMask.Bytes.Sum(b => CalcIPUtils.BytePopCount[b]);
            int newHostBits     = newSubnetMask.BitwiseNot().Bytes.Sum(b => CalcIPUtils.BytePopCount[b]);

            if (newHostBits > initialHostBits)
            {
                // supernet
                netComparison = -1;

                TAddress             unraveledInitialBaseAddress = CalcIPUtils.UnravelAddress(initialNet.BaseAddress, initialNet.SubnetMask);
                IPNetwork <TAddress> unraveledShortenedNet       = createSubnet(unraveledInitialBaseAddress, newSubnetMask.SubnetMaskFromCidrPrefix(newNetBits));
                TAddress             wovenNewBaseAddress         = CalcIPUtils.WeaveAddress(unraveledShortenedNet.BaseAddress, newSubnetMask);
                var newNet = createSubnet(wovenNewBaseAddress, newSubnetMask);

                return(new List <IPNetwork <TAddress> > {
                    newNet
                });
            }
            else if (newHostBits == initialHostBits)
            {
                // samenet
                netComparison = 0;

                TAddress unraveledBaseAddress = CalcIPUtils.UnravelAddress(initialNet.BaseAddress, initialNet.SubnetMask);
                TAddress wovenNewBaseAddress  = CalcIPUtils.WeaveAddress(unraveledBaseAddress, newSubnetMask);
                var      newNet = createSubnet(wovenNewBaseAddress, newSubnetMask);

                return(new List <IPNetwork <TAddress> > {
                    newNet
                });
            }
            else
            {
                // subnet(s)
                netComparison = 1;

                TAddress unraveledBaseAddress = CalcIPUtils.UnravelAddress(initialNet.BaseAddress, initialNet.SubnetMask);
                TAddress unraveledLastAddress = CalcIPUtils.UnravelAddress(initialNet.LastAddressOfSubnet, initialNet.SubnetMask);

                var ret = new List <IPNetwork <TAddress> >();

                TAddress currentUnraveledBaseAddress = unraveledBaseAddress;
                while (currentUnraveledBaseAddress.CompareTo(unraveledLastAddress) <= 0)
                {
                    TAddress wovenNewBaseAddress = CalcIPUtils.WeaveAddress(currentUnraveledBaseAddress, newSubnetMask);
                    var      newNet = createSubnet(wovenNewBaseAddress, newSubnetMask);

                    ret.Add(newNet);

                    currentUnraveledBaseAddress = newNet.NextSubnetBaseAddress;
                }

                return(ret);
            }
        }
Exemplo n.º 2
0
        public static IEnumerable <TAddress> EnumerateNetwork <TAddress>(IPNetwork <TAddress> network)
            where TAddress : struct, IIPAddress <TAddress>
        {
            BigInteger hostCount      = network.HostCount;
            TAddress   currentAddress = CalcIPUtils.UnravelAddress(network.BaseAddress, network.SubnetMask);

            for (BigInteger i = BigInteger.Zero; i < hostCount; ++i)
            {
                yield return(CalcIPUtils.WeaveAddress(currentAddress, network.SubnetMask));

                currentAddress = currentAddress.Add(1);
            }
        }