Example #1
0
            public void Read1()
            {
                MAC    mac      = MAC.Parse("FF:FF:FF:FF:FF:FF");
                string name     = "Adapter 1";
                IP     localip  = IP.Parse("192.168.1.2");
                IP     Subnet   = IP.Parse("255.255.255.0");
                IP     DefaultG = IP.Parse("192.168.1.1");
                IP     DNS      = IP.Parse("1.1.1.1");

                Adapter     a   = new Adapter(mac, 1, name, localip, Subnet, DefaultG, DNS, 1, true);
                Adapter     a2  = null;
                XmlDocument doc = new XmlDocument();

                doc.Load("XMLFile.xml");

                foreach (XmlNode node in doc.DocumentElement)
                {
                    if (node.Name == "AdapterRead1")
                    {
                        foreach (XmlNode n in node.ChildNodes)
                        {
                            if (n.Name == "Adapter")
                            {
                                a2 = Adapter.FromXML(n);
                            }
                        }
                    }
                }

                Assert.True(a.Equals(a2));
            }
Example #2
0
        public void Equals1()
        {
            MAC    mac      = MAC.Parse("FF:FF:FF:FF:FF:FF");
            string name     = "Adapter 1";
            IP     localip  = IP.Parse("192.168.1.2");
            IP     Subnet   = IP.Parse("255.255.255.0");
            IP     DefaultG = IP.Parse("192.168.1.1");
            IP     DNS      = IP.Parse("1.1.1.1");

            Adapter a = new Adapter(mac, 1, name, localip, Subnet, DefaultG, DNS, 1, true);


            Assert.True(a.Equals(a));
        }
Example #3
0
            public void Read1()
            {
                NSHG.System s1;
                NSHG.System s2 = null;
                Dictionary <MAC, NetworkInterface> a = new Dictionary <MAC, NetworkInterface>();

                MAC    mac      = MAC.Parse("FF:FF:FF:FF:FF:FF");
                string name     = "Adapter 1";
                IP     localip  = IP.Parse("192.168.1.2");
                IP     Subnet   = IP.Parse("255.255.255.0");
                IP     DefaultG = IP.Parse("192.168.1.1");
                IP     DNS      = IP.Parse("1.1.1.1");

                a.Add(mac, new Adapter(mac, 1, name, localip, Subnet, DefaultG, DNS, 1, true));

                s1 = new NSHG.System(1, a, false);


                XmlDocument doc = new XmlDocument();

                doc.Load("XMLFile.xml");

                foreach (XmlNode node in doc.DocumentElement)
                {
                    if (node.Name == "SystemRead1")
                    {
                        foreach (XmlNode n in node.ChildNodes)
                        {
                            if (n.Name == "System")
                            {
                                s2 = NSHG.System.FromXML(n);
                            }
                        }
                    }
                }

                Network net = Network.NewNet();

                net.Systems.Add(s1.ID, s1);
                net.SaveNetwork("sys1.xml", null);


                Assert.True(s1.Equals(s2));
            }
Example #4
0
        public void Equals3()
        {
            MAC    mac1      = MAC.Parse("FF:FF:FF:FF:FF:FF");
            string name1     = "Adapter 1";
            IP     localip1  = IP.Parse("192.168.1.2");
            IP     Subnet1   = IP.Parse("255.255.255.0");
            IP     DefaultG1 = IP.Parse("192.168.1.1");
            IP     DNS1      = IP.Parse("1.1.1.1");

            MAC    mac2      = MAC.Parse("FF:FF:FF:FF:FF:FF");
            string name2     = "Adapter 1";
            IP     localip2  = IP.Parse("192.168.1.2");
            IP     Subnet2   = IP.Parse("255.255.255.0");
            IP     DefaultG2 = IP.Parse("192.168.1.1");
            IP     DNS2      = IP.Parse("1.1.1.1");

            Adapter a1 = new Adapter(mac1, 1, name1, localip1, Subnet1, DefaultG1, DNS1, 1, true);
            Adapter a2 = new Adapter(mac2, 1, name2, localip2, Subnet2, DefaultG2, DNS2, 1, true);

            Assert.True(a1.Equals(a2));
        }
Example #5
0
        public static GroupAdapter FromXML(XmlNode Parent, Action <string> Log = null)
        {
            string      Name        = null;
            uint        sysID       = 0;
            MAC         MacAddress  = null;
            IP          LocalIP     = null;
            IP          SubnetMask  = null;
            List <uint> OtherEndids = new List <uint>();
            bool        Connected   = false;

            foreach (XmlNode n in Parent.ChildNodes)
            {
                switch (n.Name.ToLower())
                {
                case "name":
                    Name = n.InnerText;
                    break;

                case "sysid":
                    try
                    {
                        sysID = uint.Parse(n.InnerText);
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read Adapter system ID, invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;

                case "mac":
                case "macaddress":
                    try
                    {
                        MacAddress = MAC.Parse(n.InnerText);
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read MAC address, invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;

                case "localip":
                    try
                    {
                        LocalIP = IP.Parse(n.InnerText);
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read IP address, invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;

                case "subnetmask":
                    try
                    {
                        SubnetMask = IP.Parse(n.InnerText);
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read SubnetMask, invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;

                case "connectedsystems":
                case "otherends":
                    try
                    {
                        foreach (XmlNode node in n.ChildNodes)
                        {
                            OtherEndids.Add(uint.Parse(node.InnerText));
                        }
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read Connected System(otherend), invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;

                case "connected":
                    try
                    {
                        Connected = bool.Parse(n.InnerText);
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Failed to read Connected status, invalid formatting");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    break;
                }
            }
            if (MacAddress == null || sysID == 0)
            {
                throw new ArgumentException("MacAddress or SYSID not provided");
            }

            GroupAdapter a = new GroupAdapter(MacAddress, sysID, Name, LocalIP, SubnetMask, OtherEndids, Connected, Log: Log);

            return(a);
        }
Example #6
0
        public override void Command(string CommandString)
        {
            string[] Commands = CommandString.Split(' ');
            if (Commands.Length > 0)
            {
                switch (Commands[0])
                {
                case "add":
                    if (Commands.Length >= 6)
                    {
                        Entry e = new Entry();
                        try
                        {
                            e.Destination = IP.Parse(Commands[1]);
                            e.Netmask     = IP.Parse(Commands[2]);
                            if (Commands[3].ToLower() != "null")
                            {
                                e.Gateway = IP.Parse(Commands[3]);
                            }
                            e.Interface = MAC.Parse(Commands[4]);
                            e.Metric    = uint.Parse(Commands[5]);
                            NewStaticEntry(e);
                        }
                        catch (Exception ex)
                        {
                            Log("Unable to Parse Parameters");
                            Log(ex.ToString());
                        }
                    }
                    else
                    if ((Commands?[1] ?? "") == "help")
                    {
                        Log("add [Destination IP] [Subnetmask] [Gateway] [Interface] [Metric]");
                        Log("    [Destination IP]  Destination packet will be compared with");
                        Log("    [Subnet Mask]     Subnetmask that will be applied to packet when comparing with destination IP");
                        Log("    [Gateway]         Gateway Packet could be forwarded to be, use 'null' if no gateway is used");
                        Log("    [Interface]       The Mac address of the Network Interface that the packet will be sent on");
                        Log("    [Metric]          The Metric of the rule aka its weight, if it is higher it will be picked over other rules that accept the destination");
                    }
                    else
                    {
                        Log("Invalid parameters use 'add help' for more info");
                    }
                    break;

                case "table":
                    Log("Destination    Netmask       Gateway    interface       metric");
                    foreach (Entry e in Entries.Values)
                    {
                        Log(e.Destination + "   " + e.Netmask + "   " + (e.Gateway?.ToString() ?? "null") + "   " + e.Interface + "   " + e.Metric);
                    }

                    Log(" ");
                    Log("Static Entries");
                    foreach (Entry e in StaticEntries.Values)
                    {
                        Log(e.Destination + "   " + e.Netmask + "   " + (e.Gateway?.ToString() ?? "null") + "   " + e.Interface + "   " + e.Metric);
                    }
                    break;

                case "help":
                    Log("add [Destination IP] [Subnetmask] [Gateway] [Interface] [Metric]  -Adds a new static route");
                    Log("Table  -displays routing table");
                    break;
                }
            }
            else
            {
                Log("Error, use help for more info");
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            {
                Console.WriteLine("NSHG.IP");
                IP ip0 = new IP(new Byte[] { 0, 0, 0, 0 });
                Output(ip0.Equals(ip0), "ip.Equals (Identical)");

                Console.WriteLine("NSHG.IP");
                IP ip1234  = new IP(new Byte[] { 1, 2, 3, 4, 5 }, 0);
                IP ip12342 = new IP(new Byte[] { 1, 2, 3, 4 });
                Output(ip12342.Equals(ip1234), "ip.from array size > 4");


                IP ip255  = new IP(new Byte[] { 255, 255, 255, 255 });
                IP ip2552 = new IP(new Byte[] { 255, 255, 255, 255 });
                Output(ip255.Equals(ip2552), "ip.Equals (Non Identical)");

                IP ip3 = new IP(new Byte[] { 0, 0, 0, 0 });
                Output(ip0.Equals(ip3), "ip.Equals (Non Identical)");

                IP ip4 = new IP(new Byte[] { 0, 0, 0, 1 });
                Output(!ip0.Equals(ip4), "ip.Equals (Non Identical, Not Equal)");

                // Parse
                // Normal Parse's
                IP ip5   = IP.Parse("192.168.1.1");
                IP ip192 = new IP(new Byte[] { 192, 168, 1, 1 });
                Output(ip5.Equals(ip192), "ip.Parse(\"192.168.1.1\")");

                ip5 = IP.Parse("0.0.0.0");
                Output(ip5.Equals(ip0), "ip.Parse(\"0.0.0.0\")");

                ip5 = IP.Parse("255.255.255.255");
                Output(ip5.Equals(ip255), "ip.Parse(\"255.255.255.255\")");


                // Address Segments > 255
                bool result = false;
                try
                {
                    IP ip = IP.Parse("256.256.256.256");
                }
                catch (OverflowException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (Overflow)");
                }

                // Null Input
                result = false;
                try
                {
                    IP ip = IP.Parse("");
                }
                catch (ArgumentNullException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (Null)");
                }

                // Not Enough Segments
                result = false;
                try
                {
                    IP ip = IP.Parse("1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1)");
                }
                result = false;
                try
                {
                    IP ip = IP.Parse("1.1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1.1)");
                }
                result = false;
                try
                {
                    IP ip = IP.Parse("1.1.1");
                }
                catch (ArgumentOutOfRangeException)
                {
                    result = true;
                }
                finally
                {
                    Output(result, "IP.Parse (1.1.1)");
                }
            }// IP

            {
                MAC m = new MAC(new Byte[] { 255, 255, 255, 255, 255, 255 });

                m = MAC.Parse("FF:FF:FF:FF:FF:00");
                Console.WriteLine(m.ToString());
                foreach (Byte b in m.ToBytes())
                {
                    Console.WriteLine(b);
                }
            }// MAC

            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();

                byte[] expected, actual;

                expected = new byte[]
                {
                    0x45, 0x00, 0x00, 0x28, 0x6e, 0x9c, 0x40, 0x00, 0x80, 0x06, 0x77, 0xd7, 0x0a, 0x90, 0xe3, 0x64, 0x28, 0x43, 0xfe, 0x24
                };

                IPv4Header ipv4 = new IPv4Header(0x6E9C, true, false, 128, (IPv4Header.ProtocolType) 6, IP.Parse("10.144.227.100"), IP.Parse("40.67.254.36"), new byte[0], new byte[20]);



                actual = ipv4.ToBytes();

                foreach (byte b in expected)
                {
                    Console.Write(b.ToString("X"));
                    Console.Write(" ");
                }
                Console.WriteLine();


                foreach (byte b in actual)
                {
                    Console.Write(b.ToString("X"));
                    Console.Write(" ");
                }
                Console.WriteLine();
            }// IP  Header

            {
            }// TCP Header


            Console.ReadLine();
        }