Exemplo n.º 1
0
        public static int ValidMask(IPV4Address madr)
        {
            uint bit    = 0x80000000;
            int  bitnum = 0;

            for ( ; bitnum < 32; bitnum++)
            {
                uint nextbit = bit >> bitnum;
                if ((nextbit & madr.address) == 0)
                {
                    break;
                }
            }
            if (bitnum >= 31)
            {
                Console.WriteLine("Invalid Mask. No bits left for host id");
                return(0);
            }
            Console.WriteLine("Mask length {0,1} bits", bitnum);
            for (bitnum++; bitnum < 32; bitnum++)
            {
                uint nextbit = bit >> bitnum;
                if ((nextbit & madr.address) != 0)
                {
                    Console.WriteLine("Invalid network mask. Expecting sequence of 0 bits");
                    return(0);
                }
            }
            return(bitnum);
        }
Exemplo n.º 2
0
        public void Show()
        {
            Console.WriteLine("IP Address {0,1} -----------------------------------------------", Image());
            Console.WriteLine("Address Class: {0,1}", Class());
            if (IsPrivate())
            {
                Console.WriteLine("Private address");
            }
            else
            {
                Console.WriteLine("Routable public address");
            }
            if (subnet_mask != null)
            {
                Console.WriteLine("Subnet Mask: {0,1}", subnet_mask.Image());

                UInt32 network_id, host_id;
                network_id = address & subnet_mask.address;
                host_id    = address ^ network_id;
                IPV4Address network = new IPV4Address();
                network.address = network_id;
                Console.WriteLine("Network Id {0,1}", network.Image());
                IPV4Address host = new IPV4Address();
                host.address = host_id;
                Console.WriteLine("Host Id {0,1}", host.address);
            }
        }
Exemplo n.º 3
0
        public bool SetMask(string mask)
        {
            if (subnet_bits != 0)
            {
                Console.WriteLine("Subnet mask already inferred to be {0,1} bits", subnet_bits);
                return(false);
            }
            IPV4Address maskadr = new IPV4Address();

            maskadr.Analyze(mask);
            if (maskadr.subnet_bits > 0)
            {
                Console.WriteLine("Syntax error in mask address");
                return(false);
            }
            int temp = ValidMask(maskadr);

            if (temp == 0)
            {
                return(false);
            }
            subnet_mask = maskadr;
            subnet_bits = temp;
            return(true);
        }
Exemplo n.º 4
0
        private void SetSubnetMaskBits(int snb)
        {
            uint mask = 0;
            uint bit  = 0x80000000;

            for (int bitnum = 0; bitnum < snb; bitnum++)
            {
                uint nextbit = bit >> bitnum;
                mask |= nextbit;
            }
            subnet_mask         = new IPV4Address();
            subnet_mask.address = mask;
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            CommandLine.Version v = new CommandLine.Version(0, 1, 1, "ipadr");

            Cli cli = new Cli();

            cli.usage_text = "ipaddr v4|v6 <adr> [<mask>]";
            cli.version    = v;
            cli.AddSubcommand("v4", "IPV4");
            cli.AddSubcommand("v6", "IPV6");

            cli.AddSwitch("m", "mask", "test operand - is valid subnet mask?", null, "false");

            cli.Analyze(args);
            if (cli.Present("help") || cli.Present("h"))
            {
                cli.Help();
            }
            if (cli.Present("m"))
            {
                Console.WriteLine("Mask validation");
                string mval = cli.GetString("m");
                Console.WriteLine("Mask value {0,0}", mval);
            }
            else
            {
                Console.WriteLine("No mask specified");
                if (cli.Present("mask"))
                {
                    Console.WriteLine("Mask value {0,0}", cli.GetString("mask"));
                }
            }
            foreach (string op in cli.Operands())
            {
                Console.WriteLine("Operand {0,1}", op);
            }

            string    sc = cli.subcommand;
            IPAddress adr;

            switch (sc)
            {
            case "v4":
                bool bstat = false;
                adr = new IPV4Address();
                string[] cliargs = cli.Operands();
                if (cliargs.Length >= 1)
                {
                    adr   = new IPV4Address();
                    bstat = adr.Analyze(cliargs[0]);
                    if (!bstat)
                    {
                        Console.WriteLine("Please provide a valid IP Address");
                        return;
                    }
                    if (cliargs.Length >= 2)
                    {
                        bstat = adr.SetMask(cliargs[1]);
                        if (!bstat)
                        {
                            Console.WriteLine("Invalid network mask");
                            return;
                        }
                    }
                    else
                    {
                        if (cli.Present("m"))
                        {
                            string mval = cli.GetString("m");
                            adr   = new IPV4Address();
                            bstat = adr.Analyze(cliargs[0]);
                            if (!bstat)
                            {
                                Console.WriteLine("Invalid address for mask validation");
                                return;
                            }
                            int mlen = IPV4Address.ValidMask((IPV4Address)adr);
                            if (mlen > 0)
                            {
                                Console.WriteLine("Valid Mask");
                                return;
                            }
                        }
                    }
                    adr.Show();
                }
                break;

            case "v6":
                adr = new IPV6Address();
                break;

            default:
                Console.WriteLine("Unrecognized subcommand {0,1}", sc);
                return;
            }
        }