Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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());
         }
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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);

        }
Ejemplo n.º 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);
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
         }
     }
 }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 16
0
        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());
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 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();

        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
     }            
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 26
0
 public void Run(ProgramContext ac, string arg) {
     if (this.OnArgParsed != null) {
         this.OnArgParsed(ac, arg);
     }
 }
Ejemplo n.º 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);

        }
Ejemplo n.º 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());
        }