Esempio n. 1
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);
        }
Esempio n. 2
0
        public static int PerformMinimize(string[] args)
        {
            if (args.Length < 2)
            {
                Program.UsageAndExit();
            }

            var firstIPv4SubnetMatch = Program.IPv4WithSubnetRegex.Match(args[1]);
            var firstIPv4CidrMatch   = Program.IPv4WithCidrRegex.Match(args[1]);

            if (firstIPv4SubnetMatch.Success || firstIPv4CidrMatch.Success)
            {
                var         subnets  = new List <IPv4Network>();
                IPv4Network firstNet = (firstIPv4CidrMatch.Success)
                    ? Program.ParseIPv4CidrSpec(firstIPv4CidrMatch)?.Item2
                    : Program.ParseIPv4SubnetSpec(firstIPv4SubnetMatch)?.Item2;
                if (firstNet == null)
                {
                    return(1);
                }
                subnets.Add(firstNet);

                for (int i = 2; i < args.Length; ++i)
                {
                    var cidrMatch = Program.IPv4WithCidrRegex.Match(args[i]);
                    if (cidrMatch.Success)
                    {
                        var subnet = Program.ParseIPv4CidrSpec(cidrMatch)?.Item2;
                        if (subnet == null)
                        {
                            return(1);
                        }
                        subnets.Add(subnet);
                        continue;
                    }

                    var subnetMatch = Program.IPv4WithSubnetRegex.Match(args[i]);
                    if (subnetMatch.Success)
                    {
                        var subnet = Program.ParseIPv4SubnetSpec(subnetMatch)?.Item2;
                        if (subnet == null)
                        {
                            return(1);
                        }
                        subnets.Add(subnet);
                        continue;
                    }

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

                MinimizeAndOutput(subnets, (addr, mask) => new IPv4Network(addr, mask));

                return(0);
            }

            var firstIPv6SubnetMatch = Program.IPv6WithSubnetRegex.Match(args[1]);
            var firstIPv6CidrMatch   = Program.IPv6WithCidrRegex.Match(args[1]);

            if (firstIPv6SubnetMatch.Success || firstIPv6CidrMatch.Success)
            {
                var         subnets  = new List <IPv6Network>();
                IPv6Network firstNet = (firstIPv6CidrMatch.Success)
                    ? Program.ParseIPv6CidrSpec(firstIPv6CidrMatch)?.Item2
                    : Program.ParseIPv6SubnetSpec(firstIPv6SubnetMatch)?.Item2;
                if (firstNet == null)
                {
                    return(1);
                }
                subnets.Add(firstNet);

                for (int i = 2; i < args.Length; ++i)
                {
                    var cidrMatch = Program.IPv6WithCidrRegex.Match(args[i]);
                    if (cidrMatch.Success)
                    {
                        var subnet = Program.ParseIPv6CidrSpec(cidrMatch)?.Item2;
                        if (subnet == null)
                        {
                            return(1);
                        }
                        subnets.Add(subnet);
                        continue;
                    }

                    var subnetMatch = Program.IPv6WithSubnetRegex.Match(args[i]);
                    if (subnetMatch.Success)
                    {
                        var subnet = Program.ParseIPv6SubnetSpec(subnetMatch)?.Item2;
                        if (subnet == null)
                        {
                            return(1);
                        }
                        subnets.Add(subnet);
                        continue;
                    }

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

                MinimizeAndOutput(subnets, (addr, mask) => new IPv6Network(addr, mask));

                return(0);
            }

            Console.Error.WriteLine("Could not detect network spec type of {0}.", args[1]);
            return(1);
        }
Esempio n. 3
0
 public bool Equals(IPv6Network other)
 {
     return(BaseAddress == other.BaseAddress && SubnetMask == other.SubnetMask);
 }