Пример #1
0
        public static Tuple <IPv6Address, IPv6Network> ParseIPv6SubnetSpec(Match match)
        {
            string addressString = match.Groups["addr"].Value;
            string maskString    = match.Groups["mask"].Value;
            bool   isWildcard    = match.Groups["wildcard"].Success;

            var address = IPv6Address.MaybeParse(addressString);

            if (!address.HasValue)
            {
                Console.Error.WriteLine("{0}: Invalid IPv6 address {1}", match.Value, addressString);
                return(null);
            }

            var mask = IPv6Address.MaybeParse(maskString);

            if (!mask.HasValue)
            {
                Console.Error.WriteLine("{0}: Invalid IPv6 subnet mask {1}", match.Value, maskString);
                return(null);
            }

            if (isWildcard)
            {
                mask = ~mask.Value;
            }

            return(Tuple.Create(address.Value, new IPv6Network(address.Value, mask.Value)));
        }
Пример #2
0
        public static Tuple <IPv6Address, IPv6Network> ParseIPv6CidrSpec(Match match)
        {
            string addressString = match.Groups["addr"].Value;
            string cidrString    = match.Groups["cidr"].Value;

            var address = IPv6Address.MaybeParse(addressString);

            if (!address.HasValue)
            {
                Console.Error.WriteLine("{0}: Invalid IPv6 address {1}", match.Value, addressString);
                return(null);
            }

            int cidr;

            if (!int.TryParse(cidrString, NumberStyles.None, CultureInfo.InvariantCulture, out cidr))
            {
                Console.Error.WriteLine("{0}: Invalid CIDR prefix {1}", match.Value, cidrString);
                return(null);
            }
            if (cidr > 128)
            {
                Console.Error.WriteLine("{0}: CIDR prefix {1} is too large (128 is the maximum for IPv6)", match.Value, cidr);
                return(null);
            }

            return(Tuple.Create(address.Value, new IPv6Network(address.Value, cidr)));
        }
Пример #3
0
        public static int PerformDerange(string[] args)
        {
            if (args.Length != 3)
            {
                Program.UsageAndExit();
            }

            var ipv4AddressOne = IPv4Address.MaybeParse(args[1]);

            if (ipv4AddressOne.HasValue)
            {
                var ipv4AddressTwo = IPv4Address.MaybeParse(args[2]);
                if (!ipv4AddressTwo.HasValue)
                {
                    Console.Error.WriteLine("Parsed IPv4 address {0} but failed to parse {1}.", ipv4AddressOne, args[2]);
                    return(1);
                }

                List <IPNetwork <IPv4Address> > subnets = RangeToSubnets(ipv4AddressOne.Value, ipv4AddressTwo.Value,
                                                                         (addr, cidr) => new IPv4Network(addr, cidr));

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

                return(0);
            }

            var ipv6AddressOne = IPv6Address.MaybeParse(args[1]);

            if (ipv6AddressOne.HasValue)
            {
                var ipv6AddressTwo = IPv6Address.MaybeParse(args[2]);
                if (!ipv6AddressTwo.HasValue)
                {
                    Console.Error.WriteLine("Parsed IPv6 address {0} but failed to parse {1}.", ipv6AddressOne, args[2]);
                    return(1);
                }

                List <IPNetwork <IPv6Address> > subnets = RangeToSubnets(ipv6AddressOne.Value, ipv6AddressTwo.Value,
                                                                         (addr, cidr) => new IPv6Network(addr, cidr));

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

                return(0);
            }

            Console.Error.WriteLine("Failed to parse address {0}.", args[1]);
            return(1);
        }
Пример #4
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);
        }