コード例 #1
0
        public static void OutputIPv4Network(IPNetwork <IPv4Address> net, IPv4Address?addr = null)
        {
            const int labelWidth   = 11;
            const int addressWidth = 21;

            ConsoleColor originalColor = Console.ForegroundColor;

            Action <string, string> outputInitialColumns = (label, address) =>
            {
                Console.ForegroundColor = Color.Label;
                Console.Write(CalcIPUtils.PadRightTo(label, labelWidth));
                Console.ForegroundColor = Color.IPAddress;
                Console.Write(CalcIPUtils.PadRightTo(address, addressWidth));
            };

            if (addr.HasValue)
            {
                outputInitialColumns("Address:", addr.ToString());
                OutputBinaryIPv4Address(addr.Value, net.SubnetMask);
                Console.WriteLine();

                outputInitialColumns(
                    "Netmask:",
                    net.CidrPrefix.HasValue
                        ? string.Format("{0} = {1}", net.SubnetMask, net.CidrPrefix.Value)
                        : net.SubnetMask.ToString()
                    );
                OutputBinaryIPv4Address(net.SubnetMask, overrideColor: Color.MaskBits);
                Console.WriteLine();

                outputInitialColumns("Wildcard:", net.CiscoWildcard.ToString());
                OutputBinaryIPv4Address(net.CiscoWildcard);
                Console.WriteLine();

                Console.ForegroundColor = Color.Label;
                Console.WriteLine("=>");
            }

            outputInitialColumns(
                "Network:",
                net.CidrPrefix.HasValue
                    ? string.Format("{0}/{1}", net.BaseAddress, net.CidrPrefix.Value)
                    : net.SubnetMask.ToString()
                );
            OutputBinaryIPv4Address(net.BaseAddress, net.SubnetMask, colorClass: true);
            Console.WriteLine();

            if (net.FirstHostAddress.HasValue)
            {
                outputInitialColumns("HostMin:", net.FirstHostAddress.Value.ToString());
                OutputBinaryIPv4Address(net.FirstHostAddress.Value);
                Console.WriteLine();
                outputInitialColumns("HostMax:", net.LastHostAddress.Value.ToString());
                OutputBinaryIPv4Address(net.LastHostAddress.Value);
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no hosts");
            }

            if (net.BroadcastAddress.HasValue)
            {
                outputInitialColumns("Broadcast:", net.BroadcastAddress.Value.ToString());
                OutputBinaryIPv4Address(net.BroadcastAddress.Value);
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no broadcast");
            }

            if (net.HostCount.CompareTo(0) > 0)
            {
                outputInitialColumns("Hosts/Net:", net.HostCount.ToString());
                var topBits     = CalcIPUtils.ByteToBinary(net.BaseAddress.Bytes[0]);
                var topMaskBits = CalcIPUtils.ByteToBinary(net.SubnetMask.Bytes[0]);
                Console.ForegroundColor = Color.ClassBits;
                if (topBits.StartsWith("0") && topMaskBits.StartsWith("1"))
                {
                    Console.Write("Class A");
                }
                else if (topBits.StartsWith("10") && topMaskBits.StartsWith("11"))
                {
                    Console.Write("Class B");
                }
                else if (topBits.StartsWith("110") && topMaskBits.StartsWith("111"))
                {
                    Console.Write("Class C");
                }
                else if (topMaskBits.StartsWith("1111"))
                {
                    if (topBits.StartsWith("1110"))
                    {
                        Console.Write("Class D (multicast)");
                    }
                    else if (topBits.StartsWith("1111"))
                    {
                        Console.Write("Class E (reserved)");
                    }
                }
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no hosts/net");
            }

            Console.ForegroundColor = originalColor;
        }
コード例 #2
0
        public static void OutputIPv6Network(IPNetwork <IPv6Address> net, IPv6Address?addr = null)
        {
            const int labelWidth   = 11;
            const int addressWidth = 46;

            ConsoleColor originalColor = Console.ForegroundColor;

            Action <string, string> outputInitialColumns = (label, address) =>
            {
                Console.ForegroundColor = Color.Label;
                Console.Write(CalcIPUtils.PadRightTo(label, labelWidth));
                Console.ForegroundColor = Color.IPAddress;
                Console.Write(CalcIPUtils.PadRightTo(address, addressWidth));
            };

            if (addr.HasValue)
            {
                outputInitialColumns("Address:", addr.ToString());
                OutputBinaryIPv6Address(addr.Value, net.SubnetMask);
                Console.WriteLine();

                outputInitialColumns(
                    "Netmask:",
                    net.CidrPrefix.HasValue
                        ? string.Format("{0} = {1}", net.SubnetMask, net.CidrPrefix.Value)
                        : net.SubnetMask.ToString()
                    );
                OutputBinaryIPv6Address(net.SubnetMask, overrideColor: Color.MaskBits);
                Console.WriteLine();

                outputInitialColumns("Wildcard:", net.CiscoWildcard.ToString());
                OutputBinaryIPv6Address(net.CiscoWildcard);
                Console.WriteLine();

                Console.ForegroundColor = Color.Label;
                Console.WriteLine("=>");
            }

            outputInitialColumns(
                "Network:",
                net.CidrPrefix.HasValue
                    ? string.Format("{0}/{1}", net.BaseAddress, net.CidrPrefix.Value)
                    : net.SubnetMask.ToString()
                );
            OutputBinaryIPv6Address(net.BaseAddress, net.SubnetMask);
            Console.WriteLine();

            if (net.FirstHostAddress.HasValue)
            {
                outputInitialColumns("HostMin:", net.FirstHostAddress.Value.ToString());
                OutputBinaryIPv6Address(net.FirstHostAddress.Value);
                Console.WriteLine();
                outputInitialColumns("HostMax:", net.LastHostAddress.Value.ToString());
                OutputBinaryIPv6Address(net.LastHostAddress.Value);
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no hosts");
            }

            if (net.BroadcastAddress.HasValue)
            {
                outputInitialColumns("Broadcast:", net.BroadcastAddress.Value.ToString());
                OutputBinaryIPv6Address(net.BroadcastAddress.Value);
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no broadcast");
            }

            if (net.HostCount.CompareTo(0) > 0)
            {
                outputInitialColumns("Hosts/Net:", net.HostCount.ToString());
                Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = Color.Label;
                Console.WriteLine("no hosts/net");
            }

            Console.ForegroundColor = originalColor;
        }