Beispiel #1
0
        private static void OverlapNetwork(ProgramContext ac) {

            foreach (IPNetwork ipnetwork in ac.Networks) {
                bool overlap = IPNetwork.Overlap(ac.OverlapNetwork, ipnetwork);
                Console.WriteLine("{0} overlaps {1} : {2}", ac.OverlapNetwork, ipnetwork, overlap);
            }
        }
Beispiel #2
0
 private static void ListIPAddress(ProgramContext ac) {
     foreach (IPNetwork ipnetwork in ac.Networks) {
         foreach (IPAddress ipaddress in IPNetwork.ListIPAddress(ipnetwork)) {
             Console.WriteLine("{0}", ipaddress.ToString());
         }
     }
 }
Beispiel #3
0
 private static void ContainNetwork(ProgramContext ac)
 {
     foreach (IPNetwork ipnetwork in ac.Networks)
     {
         bool contain = IPNetwork.Contains(ac.ContainNetwork, ipnetwork);
         Console.WriteLine("{0} contains {1} : {2}", ac.ContainNetwork, ipnetwork, contain);
     }
 }
Beispiel #4
0
 private static void OverlapNetwork(ProgramContext ac)
 {
     foreach (IPNetwork ipnetwork in ac.Networks)
     {
         bool overlap = IPNetwork.Overlap(ac.OverlapNetwork, ipnetwork);
         Console.WriteLine("{0} overlaps {1} : {2}", ac.OverlapNetwork, ipnetwork, overlap);
     }
 }
Beispiel #5
0
        /**
         * Need a better way to do it
         * 
        private static void SubstractNetwork(ProgramContext ac) {
            
            IEnumerable<IPNetwork> result = null;
            if (!IPNetwork.TrySubstractNetwork(ac.Networks, ac.SubstractNetwork, out result)) {
                Console.WriteLine("Unable to substract subnet from these networks");
            }
            
            foreach (IPNetwork ipnetwork in result.OrderBy( s => s.ToString() )) {
                Console.WriteLine("{0}", ipnetwork);
                //Program.PrintNetwork(ac, ipnetwork);
            }
        }
        **/

        private static void WideSupernetNetworks(ProgramContext ac) {

            IPNetwork widesubnet = null;
            if (!IPNetwork.TryWideSubnet(ac.Networks, out widesubnet)) {
                Console.WriteLine("Unable to wide subnet these networks");
            }
            Program.PrintNetwork(ac, widesubnet);

        }
Beispiel #6
0
 private static void SupernetNetworks(ProgramContext ac)
 {
     IPNetwork[] supernet = null;
     if (!IPNetwork.TrySupernet(ac.Networks, out supernet))
     {
         Console.WriteLine("Unable to supernet these networks");
     }
     Program.PrintNetworks(ac, supernet, supernet.Length);
 }
Beispiel #7
0
        private static ProgramContext ParseArgs(string[] args)
        {
            int            c;
            Getopt         g  = new Getopt("ipnetwork", args, "inmcbfltud:Dhs:wWxC:o:S:");
            ProgramContext ac = new ProgramContext();

            while ((c = g.getopt()) != -1)
            {
                string optArg = g.Optarg;
                Program.Args[c].Run(ac, optArg);
            }

            List <string> ipnetworks = new List <string>();

            for (int i = g.Optind; i < args.Length; i++)
            {
                if (!string.IsNullOrEmpty(args[i]))
                {
                    ipnetworks.Add(args[i]);
                }
            }
            ac.NetworksString = ipnetworks.ToArray();
            Program.ParseIPNetworks(ac);

            if (ac.Networks.Length == 0)
            {
                Console.WriteLine("Provide at least one ipnetwork");
                ac.Action = ActionEnum.Usage;
            }

            if (ac.Action == ActionEnum.Supernet &&
                ipnetworks.Count < 2)
            {
                Console.WriteLine("Supernet action required at least two ipnetworks");
                ac.Action = ActionEnum.Usage;
            }

            if (ac.Action == ActionEnum.WideSupernet &&
                ipnetworks.Count < 2)
            {
                Console.WriteLine("WideSupernet action required at least two ipnetworks");
                ac.Action = ActionEnum.Usage;
            }

            if (Program.PrintNoValue(ac))
            {
                Program.PrintAll(ac);
            }

            if (g.Optind == 0)
            {
                Program.PrintAll(ac);
            }

            return(ac);
        }
Beispiel #8
0
        /**
         * Need a better way to do it
         *
         * private static void SubstractNetwork(ProgramContext ac) {
         *
         *  IEnumerable<IPNetwork> result = null;
         *  if (!IPNetwork.TrySubstractNetwork(ac.Networks, ac.SubstractNetwork, out result)) {
         *      Console.WriteLine("Unable to substract subnet from these networks");
         *  }
         *
         *  foreach (IPNetwork ipnetwork in result.OrderBy( s => s.ToString() )) {
         *      Console.WriteLine("{0}", ipnetwork);
         *      //Program.PrintNetwork(ac, ipnetwork);
         *  }
         * }
         **/

        private static void WideSupernetNetworks(ProgramContext ac)
        {
            IPNetwork widesubnet = null;

            if (!IPNetwork.TryWideSubnet(ac.Networks, out widesubnet))
            {
                Console.WriteLine("Unable to wide subnet these networks");
            }
            Program.PrintNetwork(ac, widesubnet);
        }
Beispiel #9
0
 private static void ListIPAddress(ProgramContext ac)
 {
     foreach (IPNetwork ipnetwork in ac.Networks)
     {
         foreach (IPAddress ipaddress in ipnetwork.ListIPAddress())
         {
             Console.WriteLine("{0}", ipaddress.ToString());
         }
     }
 }
Beispiel #10
0
        private static void PrintNetworks(ProgramContext ac, IEnumerable <IPNetwork> ipnetworks, BigInteger networkLength)
        {
            int i = 0;

            foreach (IPNetwork ipn in ipnetworks)
            {
                i++;
                Program.PrintNetwork(ac, ipn);
                Program.PrintSeparator(networkLength, i);
            }
        }
Beispiel #11
0
        private static void PrintNetworks(ProgramContext ac)
        {
            int i = 0;

            foreach (IPNetwork ipnetwork in ac.Networks)
            {
                i++;
                Program.PrintNetwork(ac, ipnetwork);
                Program.PrintSeparator(ac.Networks.Length, i);
            }
        }
Beispiel #12
0
        private static void ParseIPNetworks(ProgramContext ac)
        {
            List <IPNetwork> ipnetworks = new List <IPNetwork>();

            foreach (string ips in ac.NetworksString)
            {
                IPNetwork ipnetwork = null;
                if (!Program.TryParseIPNetwork(ips, ac.CidrParse, ac.CidrParsed, out ipnetwork))
                {
                    Console.WriteLine("Unable to parse ipnetwork {0}", ips);
                    continue;
                }
                ipnetworks.Add(ipnetwork);
            }
            ac.Networks = ipnetworks.ToArray();
        }
Beispiel #13
0
        private static bool PrintNoValue(ProgramContext ac)
        {
            if (ac == null)
            {
                throw new ArgumentNullException("ac");
            }

            return(ac.IPNetwork == false &&
                   ac.Network == false &&
                   ac.Netmask == false &&
                   ac.Cidr == false &&
                   ac.Broadcast == false &&
                   ac.FirstUsable == false &&
                   ac.LastUsable == false &&
                   ac.Total == false &&
                   ac.Usable == false);
        }
Beispiel #14
0
        private static void PrintAll(ProgramContext ac)
        {
            if (ac == null)
            {
                throw new ArgumentNullException("ac");
            }

            ac.IPNetwork   = true;
            ac.Network     = true;
            ac.Netmask     = true;
            ac.Cidr        = true;
            ac.Broadcast   = true;
            ac.FirstUsable = true;
            ac.LastUsable  = true;
            ac.Usable      = true;
            ac.Total       = true;
        }
Beispiel #15
0
        public static void Main(string[] args)
        {
            ProgramContext ac = Program.ParseArgs(args);

            if (ac.Action == ActionEnum.Subnet)
            {
                Program.SubnetNetworks(ac);
            }
            else if (ac.Action == ActionEnum.Supernet)
            {
                Program.SupernetNetworks(ac);
            }
            else if (ac.Action == ActionEnum.WideSupernet)
            {
                Program.WideSupernetNetworks(ac);
            }
            else if (ac.Action == ActionEnum.PrintNetworks)
            {
                Program.PrintNetworks(ac);
            }
            else if (ac.Action == ActionEnum.ContainNetwork)
            {
                Program.ContainNetwork(ac);
            }
            else if (ac.Action == ActionEnum.OverlapNetwork)
            {
                Program.OverlapNetwork(ac);
            }
            else if (ac.Action == ActionEnum.ListIPAddress)
            {
                Program.ListIPAddress(ac);

                /**
                 * Need a better way to do it
                 *
                 * } else if (ac.Action == ActionEnum.SubstractNetwork) {
                 *  Program.SubstractNetwork(ac);
                 *
                 */
            }
            else
            {
                Program.Usage();
            }
        }
        private static void PrintNetwork(ProgramContext ac, IPNetwork ipn)
        {
            using (var sw = new StringWriter())
            {
                if (ac.IPNetwork)
                {
                    sw.WriteLine("IPNetwork   : {0}", ipn.ToString());
                }
                if (ac.Network)
                {
                    sw.WriteLine("Network     : {0}", ipn.Network.ToString());
                }
                if (ac.Netmask)
                {
                    sw.WriteLine("Netmask     : {0}", ipn.Netmask.ToString());
                }
                if (ac.Cidr)
                {
                    sw.WriteLine("Cidr        : {0}", ipn.Cidr);
                }
                if (ac.Broadcast)
                {
                    sw.WriteLine("Broadcast   : {0}", ipn.Broadcast.ToString());
                }
                if (ac.FirstUsable)
                {
                    sw.WriteLine("FirstUsable : {0}", ipn.FirstUsable.ToString());
                }
                if (ac.LastUsable)
                {
                    sw.WriteLine("LastUsable  : {0}", ipn.LastUsable.ToString());
                }
                if (ac.Usable)
                {
                    sw.WriteLine("Usable      : {0}", ipn.Usable);
                }
                if (ac.Total)
                {
                    sw.WriteLine("Total       : {0}", ipn.Total);
                }

                Console.Write(sw.ToString());
            }
        }
Beispiel #17
0
        private static void SubnetNetworks(ProgramContext ac)
        {
            BigInteger i = 0;

            foreach (IPNetwork ipnetwork in ac.Networks)
            {
                i++;
                int networkLength = ac.Networks.Length;
                IPNetworkCollection ipnetworks = null;
                if (!ipnetwork.TrySubnet(ac.SubnetCidr, out ipnetworks))
                {
                    Console.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", ipnetwork, ac.SubnetCidr);
                    Program.PrintSeparator(networkLength, i);
                    continue;
                }

                Program.PrintNetworks(ac, ipnetworks, ipnetworks.Count);
                Program.PrintSeparator(networkLength, i);
            }
        }
Beispiel #18
0
 private static void PrintNetworks(ProgramContext ac) {
     int i = 0;
     foreach (IPNetwork ipnetwork in ac.Networks) {
         i++;
         Program.PrintNetwork(ac, ipnetwork);
         Program.PrintSeparator(ac.Networks.Length, i);
     }
 }
Beispiel #19
0
        private static ProgramContext ParseArgs(string[] args) {
            int c;
            Getopt g = new Getopt("ipnetwork", args, "inmcbfltud:Dhs:wWxC:o:S:");
            ProgramContext ac = new ProgramContext();

			while ((c = g.getopt()) != -1) {
                string optArg = g.Optarg;
                Program.Args[c].Run(ac, optArg);
            }

            List<string> ipnetworks = new List<string>();
            for (int i = g.Optind; i < args.Length; i++) {
                if (!string.IsNullOrEmpty(args[i])) {
                    ipnetworks.Add(args[i]);
                }
            }
            ac.NetworksString = ipnetworks.ToArray();
            Program.ParseIPNetworks(ac);

            if (ac.Networks.Length == 0) {
                Console.WriteLine("Provide at least one ipnetwork");
                ac.Action = ActionEnum.Usage;
            }

            if (ac.Action == ActionEnum.Supernet
                && ipnetworks.Count < 2) {
                Console.WriteLine("Supernet action required at least two ipnetworks");
                ac.Action = ActionEnum.Usage;
            }

            if (ac.Action == ActionEnum.WideSupernet
                && ipnetworks.Count < 2) {
                Console.WriteLine("WideSupernet action required at least two ipnetworks");
                ac.Action = ActionEnum.Usage;
            }

            if (Program.PrintNoValue(ac)) {
                Program.PrintAll(ac);
            }

            if (g.Optind == 0) {
                Program.PrintAll(ac);
            }

            return ac;
        }
Beispiel #20
0
        private static void ParseIPNetworks(ProgramContext ac) {

            List<IPNetwork> ipnetworks = new List<IPNetwork>();
            foreach (string ips in ac.NetworksString) {
                IPNetwork ipnetwork = null;
                if (!Program.TryParseIPNetwork(ips, ac.CidrParse, ac.CidrParsed, out ipnetwork)) {
                    Console.WriteLine("Unable to parse ipnetwork {0}", ips);
                    continue;
                }
                ipnetworks.Add(ipnetwork);
            }
            ac.Networks = ipnetworks.ToArray();

        }
Beispiel #21
0
        private static bool PrintNoValue(ProgramContext ac) {
            if (ac == null) {
                throw new ArgumentNullException("ac");
            }

            return ac.IPNetwork == false
                  && ac.Network == false
                  && ac.Netmask == false
                  && ac.Cidr == false
                  && ac.Broadcast == false
                  && ac.FirstUsable == false
                  && ac.LastUsable == false
                  && ac.Total == false
                  && ac.Usable == false;
        }
Beispiel #22
0
        private static void PrintAll(ProgramContext ac) {
            if (ac == null) {
                throw new ArgumentNullException("ac");
            }

            ac.IPNetwork = true;
            ac.Network = true;
            ac.Netmask = true;
            ac.Cidr = true;
            ac.Broadcast = true;
            ac.FirstUsable = true;
            ac.LastUsable = true;
            ac.Usable = true;
            ac.Total = true;
        }
Beispiel #23
0
        private static void SubnetNetworks(ProgramContext ac) {
            BigInteger i = 0;
            foreach (IPNetwork ipnetwork in ac.Networks) {
                i++;
                int networkLength = ac.Networks.Length;
                IPNetworkCollection ipnetworks = null;
                if (!IPNetwork.TrySubnet(ipnetwork, ac.SubnetCidr, out ipnetworks)) {
                    Console.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", ipnetwork, ac.SubnetCidr);
                    Program.PrintSeparator(networkLength, i);
                    continue;
                }

                Program.PrintNetworks(ac, ipnetworks, ipnetworks.Count);
                Program.PrintSeparator(networkLength, i);
            }
        }
Beispiel #24
0
 private static void ContainNetwork(ProgramContext ac) {
     foreach (IPNetwork ipnetwork in ac.Networks) {
         bool contain = IPNetwork.Contains(ac.ContainNetwork, ipnetwork);
         Console.WriteLine("{0} contains {1} : {2}", ac.ContainNetwork, ipnetwork, contain);
     }            
 }
Beispiel #25
0
 private static void PrintNetworks(ProgramContext ac, IEnumerable<IPNetwork> ipnetworks, BigInteger networkLength) {
     int i = 0;
     foreach (IPNetwork ipn in ipnetworks) {
         i++;
         Program.PrintNetwork(ac, ipn);
         Program.PrintSeparator(networkLength, i);
     }
 }
Beispiel #26
0
 public void Run(ProgramContext ac, string arg) {
     if (this.OnArgParsed != null) {
         this.OnArgParsed(ac, arg);
     }
 }
Beispiel #27
0
        private static void SupernetNetworks(ProgramContext ac) {

            IPNetwork[] supernet = null;
            if (!IPNetwork.TrySupernet(ac.Networks, out supernet)) {
                Console.WriteLine("Unable to supernet these networks");
            }
            Program.PrintNetworks(ac, supernet, supernet.Length);

        }
Beispiel #28
0
        private static void PrintNetwork(ProgramContext ac, IPNetwork ipn) {

            StringWriter sw = new StringWriter();
            if (ac.IPNetwork)   sw.WriteLine("IPNetwork   : {0}", ipn.ToString());
            if (ac.Network)     sw.WriteLine("Network     : {0}", ipn.Network.ToString());
            if (ac.Netmask)     sw.WriteLine("Netmask     : {0}", ipn.Netmask.ToString());
            if (ac.Cidr)        sw.WriteLine("Cidr        : {0}", ipn.Cidr);
            if (ac.Broadcast)   sw.WriteLine("Broadcast   : {0}", ipn.Broadcast.ToString());
            if (ac.FirstUsable) sw.WriteLine("FirstUsable : {0}", ipn.FirstUsable.ToString());
            if (ac.LastUsable)  sw.WriteLine("LastUsable  : {0}", ipn.LastUsable.ToString());
            if (ac.Usable)      sw.WriteLine("Usable      : {0}", ipn.Usable);
            if (ac.Total)       sw.WriteLine("Total       : {0}", ipn.Total);
            Console.Write(sw.ToString());
        }