Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        private static void OutputBinaryIPv4Address(IPv4Address addr, IPv4Address?subnetMask = null, bool colorClass = false,
                                                    ConsoleColor?overrideColor = null)
        {
            byte[] bytes     = addr.Bytes;
            byte[] maskBytes = subnetMask?.Bytes;

            for (int i = 0; i < bytes.Length; ++i)
            {
                byte b = bytes[i];
                byte?m = maskBytes?[i];

                string bits     = CalcIPUtils.ByteToBinary(b);
                string maskBits = m.HasValue
                    ? CalcIPUtils.ByteToBinary(m.Value)
                    : null;

                if (overrideColor.HasValue)
                {
                    // simply output the address
                    Console.ForegroundColor = overrideColor.Value;
                    Console.Write(bits);
                }
                else if (maskBits == null)
                {
                    // simple output here too
                    Console.ForegroundColor = Color.HostBits;
                    Console.Write(bits);
                }
                else
                {
                    // we must differentiate

                    if (i == 0 && colorClass)
                    {
                        // check if this is a classful network
                        if (maskBits[0] == '0')
                        {
                            // first bit isn't part of the network
                            colorClass = false;
                        }
                        else if (bits[0] == '1' && maskBits[1] == '0')
                        {
                            // first bit, 1, is part of the network, but second isn't
                            colorClass = false;
                        }
                        else if (bits[1] == '1' && maskBits[2] == '0')
                        {
                            // first two bits, both 1, are part of the network, but third isn't
                            colorClass = false;
                        }
                        else if (bits[2] == '1' && maskBits[3] == '0')
                        {
                            // first three bits, all 1, are part of the network, but fourth isn't
                            colorClass = false;
                        }
                    }

                    for (int bit = 0; bit < 8; ++bit)
                    {
                        // assign color
                        if (maskBits != null && maskBits[bit] == '1')
                        {
                            Console.ForegroundColor = Color.NetBits;
                        }
                        else
                        {
                            Console.ForegroundColor = Color.HostBits;
                        }

                        if (i == 0 && colorClass)
                        {
                            // the old-style class might be relevant
                            if (bit == 0)
                            {
                                Console.ForegroundColor = Color.ClassBits;
                            }
                            else if (bit == 1 && bits[0] == '1')
                            {
                                Console.ForegroundColor = Color.ClassBits;
                            }
                            else if (bit == 2 && bits.StartsWith("11"))
                            {
                                Console.ForegroundColor = Color.ClassBits;
                            }
                            else if (bit == 3 && bits.StartsWith("111"))
                            {
                                Console.ForegroundColor = Color.ClassBits;
                            }
                        }

                        Console.Write(bits[bit]);
                    }
                }

                if (i < bytes.Length - 1)
                {
                    // add separator (dot)
                    Console.ForegroundColor = Color.AddressSeparator;
                    Console.Write('.');
                }
            }
        }