Exemplo n.º 1
0
 public static int RunSingleArgs(string[] args)
 {
     if (args[0] == "-m" || args[0] == "--minimize")
     {
         return(Minimize.PerformMinimize(args));
     }
     else if (args[0] == "-d" || args[0] == "--derange")
     {
         return(Derange.PerformDerange(args));
     }
     else if (args[0] == "-s" || args[0] == "--split")
     {
         return(Split.PerformSplit(args));
     }
     else if (args[0] == "-r" || args[0] == "--resize")
     {
         return(Resize.PerformResize(args));
     }
     else if (args[0] == "-e" || args[0] == "--enumerate")
     {
         return(Enumerate.PerformEnumerate(args));
     }
     else
     {
         return(ShowNet.PerformShowNet(args));
     }
 }
Exemplo n.º 2
0
        public static int PerformSplit(string[] args)
        {
            if (args.Length < 3)
            {
                Program.UsageAndExit();
            }

            var ipv4Match = Program.IPv4WithCidrRegex.Match(args[1]);

            if (ipv4Match.Success)
            {
                Tuple <IPv4Address, IPv4Network> addressAndNet = Program.ParseIPv4CidrSpec(ipv4Match);
                if (addressAndNet == null)
                {
                    return(1);
                }

                Console.WriteLine("Subnet to split:");
                ShowNet.OutputIPv4Network(addressAndNet.Item2, addressAndNet.Item2.BaseAddress);
                Console.WriteLine();

                BigInteger[] splits = ParseHostCountSpecs(args);
                if (splits == null)
                {
                    return(1);
                }

                IPNetwork <IPv4Address>[] subnets = SplitSubnet(addressAndNet.Item2, splits,
                                                                (addr, cidr) => new IPv4Network(addr, cidr));
                if (subnets == null)
                {
                    Console.WriteLine("Not enough addresses available for this split.");
                    return(0);
                }

                foreach (Tuple <BigInteger, IPNetwork <IPv4Address> > splitAndSubnet in splits.Zip(subnets, Tuple.Create))
                {
                    Console.WriteLine("Subnet for {0} hosts:", splitAndSubnet.Item1);
                    ShowNet.OutputIPv4Network(splitAndSubnet.Item2);
                    Console.WriteLine();
                }

                Console.WriteLine("Unused networks:");
                var maxUsedAddress = subnets
                                     .Select(s => s.LastAddressOfSubnet)
                                     .Max();
                var nextUnusedAddress = maxUsedAddress.Add(1);
                List <IPNetwork <IPv4Address> > unusedSubnets = Derange.RangeToSubnets(nextUnusedAddress,
                                                                                       addressAndNet.Item2.LastAddressOfSubnet, (addr, cidr) => new IPv4Network(addr, cidr));

                foreach (IPNetwork <IPv4Address> unusedSubnet in unusedSubnets)
                {
                    Console.WriteLine("{0}/{1}", unusedSubnet.BaseAddress, unusedSubnet.CidrPrefix.Value);
                }

                return(0);
            }

            var ipv6Match = Program.IPv6WithCidrRegex.Match(args[1]);

            if (ipv6Match.Success)
            {
                Tuple <IPv6Address, IPv6Network> addressAndNet = Program.ParseIPv6CidrSpec(ipv6Match);
                if (addressAndNet == null)
                {
                    return(1);
                }

                Console.WriteLine("Subnet to split:");
                ShowNet.OutputIPv6Network(addressAndNet.Item2, addressAndNet.Item2.BaseAddress);

                BigInteger[] splits = ParseHostCountSpecs(args);
                if (splits == null)
                {
                    return(1);
                }

                IPNetwork <IPv6Address>[] subnets = SplitSubnet(addressAndNet.Item2, splits,
                                                                (addr, cidr) => new IPv6Network(addr, cidr));
                if (subnets == null)
                {
                    Console.WriteLine("Not enough addresses available for this split.");
                    return(0);
                }

                foreach (Tuple <BigInteger, IPNetwork <IPv6Address> > splitAndSubnet in splits.Zip(subnets, Tuple.Create))
                {
                    Console.WriteLine("Subnet for {0} hosts:", splitAndSubnet.Item1);
                    ShowNet.OutputIPv6Network(addressAndNet.Item2);
                    Console.WriteLine();
                }

                Console.WriteLine("Unused networks:");
                var maxUsedAddress = subnets
                                     .Select(s => s.LastAddressOfSubnet)
                                     .Max();
                var nextUnusedAddress = maxUsedAddress.Add(1);
                List <IPNetwork <IPv6Address> > unusedSubnets = Derange.RangeToSubnets(nextUnusedAddress,
                                                                                       addressAndNet.Item2.SubnetMask, (addr, cidr) => new IPv6Network(addr, cidr));

                foreach (IPNetwork <IPv6Address> unusedSubnet in unusedSubnets)
                {
                    Console.WriteLine("{0}/{1}", unusedSubnet.BaseAddress, unusedSubnet.CidrPrefix.Value);
                }

                return(0);
            }

            Console.Error.WriteLine("Failed to parse {0} as a subnet specification.", args[1]);
            return(1);
        }
Exemplo n.º 3
0
        public static int PerformResize(string[] args)
        {
            if (args.Length != 3)
            {
                Program.UsageAndExit();
            }

            var ipv4CidrMatch   = Program.IPv4WithCidrRegex.Match(args[1]);
            var ipv4SubnetMatch = Program.IPv4WithSubnetRegex.Match(args[1]);

            if (ipv4CidrMatch.Success || ipv4SubnetMatch.Success)
            {
                IPv4Network initialNet = (ipv4CidrMatch.Success)
                    ? Program.ParseIPv4CidrSpec(ipv4CidrMatch)?.Item2
                    : Program.ParseIPv4SubnetSpec(ipv4SubnetMatch)?.Item2;
                if (initialNet == null)
                {
                    return(1);
                }

                int         cidrPrefix;
                IPv4Address newSubnetMask;
                if (int.TryParse(args[2], NumberStyles.None, CultureInfo.InvariantCulture, out cidrPrefix))
                {
                    if (cidrPrefix < 0 || cidrPrefix > 32)
                    {
                        Console.Error.WriteLine("IPv4 CIDR prefix {0} invalid; must be at last 0 and at most 32.", cidrPrefix);
                        return(1);
                    }

                    newSubnetMask = initialNet.BaseAddress.SubnetMaskFromCidrPrefix(cidrPrefix);
                }
                else
                {
                    IPv4Address?maybeSubnetMask = IPv4Address.MaybeParse(args[2]);
                    if (!maybeSubnetMask.HasValue)
                    {
                        Console.Error.WriteLine("Invalid IPv4 CIDR prefix or subnet mask spec: {0}", args[2]);
                        return(1);
                    }

                    newSubnetMask = maybeSubnetMask.Value;
                }

                int netComparison;
                List <IPNetwork <IPv4Address> > resized = ResizeNetwork(initialNet, newSubnetMask,
                                                                        (addr, mask) => new IPv4Network(addr, mask), out netComparison);

                Console.WriteLine("Original network:");
                ShowNet.OutputIPv4Network(initialNet);
                Console.WriteLine();

                if (netComparison < 0)
                {
                    Console.WriteLine("Supernet:");
                    ShowNet.OutputIPv4Network(resized[0]);
                    Console.WriteLine();
                }
                else if (netComparison == 0)
                {
                    Console.WriteLine("Same-sized net:");
                    ShowNet.OutputIPv4Network(resized[0]);
                    Console.WriteLine();
                }
                else
                {
                    for (int i = 0; i < resized.Count; ++i)
                    {
                        Console.WriteLine("Subnet {0}:", i + 1);
                        ShowNet.OutputIPv4Network(resized[i]);
                        Console.WriteLine();
                    }
                }

                return(0);
            }

            var ipv6CidrMatch   = Program.IPv6WithCidrRegex.Match(args[1]);
            var ipv6SubnetMatch = Program.IPv6WithCidrRegex.Match(args[1]);

            if (ipv6CidrMatch.Success || ipv6SubnetMatch.Success)
            {
                IPv6Network initialNet = (ipv6CidrMatch.Success)
                    ? Program.ParseIPv6CidrSpec(ipv6CidrMatch)?.Item2
                    : Program.ParseIPv6SubnetSpec(ipv6SubnetMatch)?.Item2;
                if (initialNet == null)
                {
                    return(1);
                }

                int         cidrPrefix;
                IPv6Address newSubnetMask;
                if (int.TryParse(args[2], NumberStyles.None, CultureInfo.InvariantCulture, out cidrPrefix))
                {
                    if (cidrPrefix < 0 || cidrPrefix > 128)
                    {
                        Console.Error.WriteLine("IPv6 CIDR prefix {0} invalid; must be at last 0 and at most 128.", cidrPrefix);
                        return(1);
                    }

                    newSubnetMask = initialNet.BaseAddress.SubnetMaskFromCidrPrefix(cidrPrefix);
                }
                else
                {
                    IPv6Address?maybeSubnetMask = IPv6Address.MaybeParse(args[2]);
                    if (!maybeSubnetMask.HasValue)
                    {
                        Console.Error.WriteLine("Invalid IPv6 CIDR prefix or subnet mask spec: {0}", args[2]);
                        return(1);
                    }

                    newSubnetMask = maybeSubnetMask.Value;
                }

                int netComparison;
                List <IPNetwork <IPv6Address> > resized = ResizeNetwork(initialNet, newSubnetMask,
                                                                        (addr, mask) => new IPv6Network(addr, mask), out netComparison);

                Console.WriteLine("Original network:");
                ShowNet.OutputIPv6Network(initialNet);
                Console.WriteLine();

                if (netComparison < 0)
                {
                    Console.WriteLine("Supernet:");
                    ShowNet.OutputIPv6Network(resized[0]);
                    Console.WriteLine();
                }
                else if (netComparison == 0)
                {
                    Console.WriteLine("Same-sized net:");
                    ShowNet.OutputIPv6Network(resized[0]);
                    Console.WriteLine();
                }
                else
                {
                    for (int i = 0; i < resized.Count; ++i)
                    {
                        Console.WriteLine("Subnet {0}:", i + 1);
                        ShowNet.OutputIPv6Network(resized[i]);
                        Console.WriteLine();
                    }
                }

                return(0);
            }

            Console.Error.WriteLine("Could not detect network spec type of {0}.", args[1]);
            return(1);
        }