private static byte[] BuildDataStructure(DHCPPacket packet)
        {
            byte[] mArray;

            try
            {
                mArray = new byte[0];
                AddOptionElement(new byte[] { packet.op }, ref mArray);
                AddOptionElement(new byte[] { packet.htype }, ref mArray);
                AddOptionElement(new byte[] { packet.hlen }, ref mArray);
                AddOptionElement(new byte[] { packet.hops }, ref mArray);
                AddOptionElement(packet.xid, ref mArray);
                AddOptionElement(packet.secs, ref mArray);
                AddOptionElement(packet.flags, ref mArray);
                AddOptionElement(packet.ciaddr, ref mArray);
                AddOptionElement(packet.yiaddr, ref mArray);
                AddOptionElement(packet.siaddr, ref mArray);
                AddOptionElement(packet.giaddr, ref mArray);
                AddOptionElement(packet.chaddr, ref mArray);
                AddOptionElement(packet.sname, ref mArray);
                AddOptionElement(packet.file, ref mArray);

                AddOptionElement(packet.mcookie, ref mArray);
                AddOptionElement(packet.options, ref mArray);
                return(mArray);
            }
            finally
            {
                mArray = null;
            }
        }
Ejemplo n.º 2
0
        void DisplayPacket(DHCPPacket packet) // hien thi goi tin vua nhan ra man hinh
        {
            // Hien thi goi tin dhcp vua nhan duoc len man hinh
            //rtb_DHCPMessage.Text += packet.ToText() + "\r\n";
            List <byte[]> Option   = packet.optionsplit();
            string        DHCPType = "";

            for (int i = 0; i < Option.Count(); i++)
            {
                if (Option[i][0] == 53)
                {
                    if (Option[i][2] == 1)
                    {
                        DHCPType = "DHCP Discover";
                    }

                    if (Option[i][2] == 2)
                    {
                        DHCPType = "DHCP Offer";
                    }

                    if (Option[i][2] == 3)
                    {
                        DHCPType = "DHCP Request";
                    }

                    if (Option[i][2] == 4)
                    {
                        DHCPType = "DHCP Decline";
                    }

                    if (Option[i][2] == 5)
                    {
                        DHCPType = "DHCP ACK";
                    }

                    if (Option[i][2] == 6)
                    {
                        DHCPType = "DHCP NACK";
                    }

                    if (Option[i][2] == 7)
                    {
                        DHCPType = "DHCP Release";
                    }

                    if (Option[i][2] == 8)
                    {
                        DHCPType = "DHCP Inform";
                    }
                }
            }
            // Display dhcp messeage
            //rtbMess.Text += packet.ToText() + "\r\n"; // d.ToText() la ham tra ve mot string tu DHCPPacket
            ListViewItem type = new ListViewItem(DHCPType);

            lv_Message.Items.Add(type);
            ListViewItem.ListViewSubItem time = new ListViewItem.ListViewSubItem(type, DateTime.Now.ToString());
            type.SubItems.Add(time);
        }
Ejemplo n.º 3
0
        internal static void IPv4Handler(byte[] packetData)
        {
            IPPacket ip_packet = new IPPacket(packetData);

            Kernel.debugger.Send("[Received] " + ip_packet.ToString());
            if (ip_packet.SourceIP == null)
            {
                Kernel.debugger.Send("SourceIP null in IPv4Handler!");
            }
            ARPCache.Update(ip_packet.SourceIP, ip_packet.SourceMAC);

            if ((NetworkStack.AddressMap.ContainsKey(ip_packet.DestinationIP.Hash) == true) ||
                (ip_packet.DestinationIP.address[3] == 255))
            {
                switch (ip_packet.Protocol)
                {
                case 1:
                    ICMPPacket.ICMPHandler(packetData);
                    break;

                /*case 6:
                 *  TCPPacket.TCPHandler(packetData);
                 *  break;*/
                case 17:
                    UDPPacket.UDPHandler(packetData);
                    break;
                }
            }
            else if (NetworkStack.MACMap.ContainsKey(ip_packet.DestinationMAC.Hash))
            {
                DHCPPacket.DHCPHandler(packetData);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// IPv4 handler.
        /// </summary>
        /// <param name="packetData">Packet data.</param>
        /// <exception cref="sys.ArgumentOutOfRangeException">Thrown on fatal error (contact support).</exception>
        /// <exception cref="sys.IO.IOException">Thrown on IO error.</exception>
        /// <exception cref="sys.ArgumentException">Thrown on fatal error (contact support).</exception>
        /// <exception cref="sys.OverflowException">Thrown if packetData array length is greater than Int32.MaxValue.</exception>
        internal static void IPv4Handler(byte[] packetData)
        {
            var ip_packet = new IPPacket(packetData);

            if (ip_packet.SourceIP == null)
            {
                Global.mDebugger.Send("SourceIP null in IPv4Handler!");
            }
            ARPCache.Update(ip_packet.SourceIP, ip_packet.SourceMAC);

            if ((NetworkStack.AddressMap.ContainsKey(ip_packet.DestinationIP.Hash) == true) ||
                (ip_packet.DestinationIP.address[3] == 255))
            {
                switch (ip_packet.Protocol)
                {
                case 1:
                    ICMPPacket.ICMPHandler(packetData);
                    break;

                case 6:
                    TCPPacket.TCPHandler(packetData);
                    break;

                case 17:
                    UDPPacket.UDPHandler(packetData);
                    break;
                }
            }
            else if (NetworkStack.MACMap.ContainsKey(ip_packet.DestinationMAC.Hash))
            {
                DHCPPacket.DHCPHandler(packetData);
            }
        }
Ejemplo n.º 5
0
        void SendPacket(DHCPPacket d) // send
        {
            IPAddress  ipadd = IPAddress.Parse("255.255.255.255");
            IPEndPoint ipend = new IPEndPoint(ipadd, 6700);

            byte[] send = d.DHCPPacketToBytes();
            udpclient.Send(send, send.Length, ipend);
        }
Ejemplo n.º 6
0
        void Send_DHCPRequest(DHCPPacket packet, byte[] dhcpserver, int type)
        {
            /*
             * type = 1: selecting
             * type = 2: renew, bound, rebind
             * type = 3: reboot
             */
            // send dhcp request
            DHCPPacket n_packet = new DHCPPacket();

            n_packet.Init();
            n_packet.op    = 1;
            n_packet.htype = 1;
            n_packet.hlen  = 6;
            n_packet.hops  = 0;
            for (int i = 0; i < packet.xid.Length; i++)
            {
                n_packet.xid[i] = packet.xid[i];
            }

            xid.Add(ByteArrayToString(packet.xid));

            if (type == 2)
            {
                n_packet.ciaddr = ip.GetAddressBytes();
            }

            for (int i = 0; i < MacAddr.Length; i++)
            {
                n_packet.chaddr[i] = MacAddr[i];
            }

            option op_field = new option();

            op_field.add(new byte[] { 99, 139, 83, 99 }); // add dhcp magic option
            op_field.add(new byte[] { 53, 1, 3 });        // add messeage type dhcp request
            if (type == 1 || type == 3)
            {
                op_field.add(new byte[] { 50, 4 }); // add request ip address
                op_field.add(packet.yiaddr);        // ip address
            }
            if (type == 1)
            {
                op_field.add(new byte[] { 54, 4 });      // add dhcp server identify
                op_field.add(dhcpserver);                // add dhcp server identify
            }
            op_field.add(new byte[] { 55, 3, 1, 3, 6 }); // add parament request list
            op_field.add(new byte[] { 255 });            // add end

            n_packet.options = new byte[op_field.size];
            for (int i = 0; i < op_field.size; i++)
            {
                n_packet.options[i] = op_field.data[i];
            }
            //
            SendPacket(n_packet);
        }
Ejemplo n.º 7
0
        void SendPacket(DHCPPacket packet) // Nhan mot goi DHCP packet chuyen thanh dang byte va gui di
        {
            IPAddress  ipadd = IPAddress.Parse("255.255.255.255");
            IPEndPoint ipend = new IPEndPoint(ipadd, 6800);

            byte[] send = packet.DHCPPacketToBytes();
            udpclient.Send(send, send.Length, ipend);
            //MessageBox.Show(ByteArrayToString(send));
        }
Ejemplo n.º 8
0
        public Client()
        {
            InitializeComponent();
            ip             = IPAddress.Parse("0.0.0.0"); // Gan cac thong so mac dinh luc moi khoi dong
            defaultgateway = IPAddress.Parse("0.0.0.0");
            dns            = IPAddress.Parse("0.0.0.0");
            subnetmask     = IPAddress.Parse("255.255.255.255");

            var macAddr =
                (
                    from nic in NetworkInterface.GetAllNetworkInterfaces()
                    where nic.OperationalStatus == OperationalStatus.Up
                    select nic.GetPhysicalAddress().ToString()
                ).FirstOrDefault(); // Lay dia chi mac

            try
            {
                MacAddr = StringToByteArray(macAddr);
            }
            catch (Exception)
            {
                MessageBox.Show("Không tìm thấy card mạng");
                MacAddr = new byte[] { 0, 0, 0, 0, 0, 0 };
            }  // Gan dia chi mac vao mang MacAddr

            CheckForIllegalCrossThreadCalls = false;

            udpclient = new UdpClient(6700); // Mo port
            //Thread dung de lang nghe
            Listening_thread = new Thread(new ThreadStart(Listening));
            Listening_thread.IsBackground = true;
            Listening_thread.Start();

            //Thread dung de hien thi parameter cua client
            Display_Para_thread = new Thread(new ThreadStart(Display_Parameter));
            Display_Para_thread.IsBackground = true;
            Display_Para_thread.Start();

            //Thread dung de tu dong gia han ip khi het thoi gian
            time = 0;
            AutoExtend_thread = new Thread(new ThreadStart(Auto_Extend));
            AutoExtend_thread.IsBackground = true;
            AutoExtend_thread.Start();

            haveip = false; // Chua co ip

            autoextend = false;

            DHCPServer_IP = new byte[] { 0, 0, 0, 0 }; // Dia chi ip cua server dhcp

            offer_saved = new DHCPPacket();

            ListPacket = new List <DHCPPacket>();

            xid = new HashSet <string>();
        }
Ejemplo n.º 9
0
        void Send_DHCPAck(DHCPPacket packet, IPAddress IP) // take dhcp discover and allocated ip and send offer packet
        {
            // send dhcp offer
            DHCPPacket n_packet = new DHCPPacket();

            n_packet.Init();
            n_packet.op    = 2;
            n_packet.htype = 1;
            n_packet.hlen  = 6;
            n_packet.hops  = 0;
            for (int i = 0; i < packet.xid.Length; i++)
            {
                n_packet.xid[i] = packet.xid[i];
            }

            n_packet.yiaddr = IP.GetAddressBytes();

            for (int i = 0; i < packet.flags.Length; i++)
            {
                n_packet.flags[i] = packet.flags[i];
            }

            for (int i = 0; i < packet.giaddr.Length; i++)
            {
                n_packet.giaddr[i] = packet.giaddr[i];
            }

            for (int i = 0; i < packet.chaddr.Length; i++)
            {
                n_packet.chaddr[i] = packet.chaddr[i];
            }

            option op_field = new option();

            op_field.add(new byte[] { 99, 139, 83, 99 });                                                                                                                                                // add dhcp magic option
            op_field.add(new byte[] { 53, 1, 5 });                                                                                                                                                       // add messeage type dhcp ack
            op_field.add(new byte[] { 54, 4 });                                                                                                                                                          // add dhcp server identify
            op_field.add(network_config.dhcpserver.GetAddressBytes());
            op_field.add(new byte[] { 51, 4, (byte)(network_config.leasetime), (byte)(network_config.leasetime >> 8), (byte)(network_config.leasetime >> 16), (byte)(network_config.leasetime >> 24) }); // add ip lease time (120 s)
            op_field.add(new byte[] { 1, 4 });                                                                                                                                                           // add subnetmask
            op_field.add(network_config.subnetmask.GetAddressBytes());
            op_field.add(new byte[] { 3, 4 });                                                                                                                                                           // add defualt gateway
            op_field.add(network_config.defaultgateway.GetAddressBytes());
            op_field.add(new byte[] { 6, 4 });                                                                                                                                                           // add dns server
            op_field.add(network_config.dns.GetAddressBytes());
            op_field.add(new byte[] { 255 });                                                                                                                                                            // add end

            n_packet.options = new byte[op_field.size];
            for (int i = 0; i < op_field.size; i++)
            {
                n_packet.options[i] = op_field.data[i];
            }
            //
            SendPacket(n_packet);
        }
Ejemplo n.º 10
0
        HashSet <string> xid; // Cac  xid dang giao tiep

        void Listening()
        {
            while (true)
            {
                IPEndPoint IpEnd     = new IPEndPoint(IPAddress.Any, 0);
                Byte[]     recvBytes = udpclient.Receive(ref IpEnd);
                DHCPPacket packet    = new DHCPPacket();
                packet.BytesToDHCPPacket(recvBytes);
                ListPacket.Add(packet);
                DisplayPacket(packet);
                HandlePacket(packet);
            }
        }
Ejemplo n.º 11
0
 void Listening() // Lang nghe goi dhcp
 {
     while (true)
     {
         IPEndPoint IpEnd     = new IPEndPoint(IPAddress.Any, 0);
         Byte[]     recvBytes = udpclient.Receive(ref IpEnd);
         DHCPPacket packet    = new DHCPPacket();
         packet.BytesToDHCPPacket(recvBytes); // chuyen goi tin tu dang byte sang dang DHCPPacket
         ListPacket.Add(packet);
         Display_Message(packet);             // Hien thi goi len mang hinh
         HandlePacket(packet);                // xu li goi vua nhan
     }
 }
Ejemplo n.º 12
0
        void Send_DHCPRelease()
        {
            // send dhcp release
            DHCPPacket packet = new DHCPPacket();

            packet.Init();
            packet.op    = 1;
            packet.htype = 1;
            packet.hlen  = 6;
            packet.hops  = 0;
            Random _random = new Random();

            packet.xid[0] = (byte)_random.Next(0, 255);
            packet.xid[1] = (byte)_random.Next(0, 255);
            packet.xid[2] = (byte)_random.Next(0, 255);
            packet.xid[3] = (byte)_random.Next(0, 255);

            packet.ciaddr = ip.GetAddressBytes();

            for (int i = 0; i < MacAddr.Length; i++)
            {
                packet.chaddr[i] = MacAddr[i];
            }
            option op_field = new option();

            op_field.add(new byte[] { 99, 139, 83, 99 }); // add dhcp magic option
            op_field.add(new byte[] { 53, 1, 7 });        // add messeage type dhcp request
            op_field.add(new byte[] { 54, 4 });           // add dhcp server identify
            op_field.add(DHCPServer_IP);
            op_field.add(new byte[] { 255 });
            packet.options = new byte[op_field.size];
            for (int i = 0; i < op_field.size; i++)
            {
                packet.options[i] = op_field.data[i];
            }
            //
            SendPacket(packet);

            haveip         = false;
            ip             = IPAddress.Parse("0.0.0.0");
            defaultgateway = IPAddress.Parse("0.0.0.0");
            subnetmask     = IPAddress.Parse("255.255.255.255");
            dns            = IPAddress.Parse("0.0.0.0");
            time           = 0;
            DHCPServer_IP  = new byte[] { 0, 0, 0, 0 };
        }
Ejemplo n.º 13
0
        void Send_DHCPNak(DHCPPacket packet) // take dhcp discover and allocated ip and send offer packet
        {
            // send dhcp NACK
            DHCPPacket n_packet = new DHCPPacket();

            n_packet.Init();
            n_packet.op    = 2;
            n_packet.htype = 1;
            n_packet.hlen  = 6;
            n_packet.hops  = 0;
            for (int i = 0; i < packet.xid.Length; i++)
            {
                n_packet.xid[i] = packet.xid[i];
            }

            for (int i = 0; i < packet.flags.Length; i++)
            {
                n_packet.flags[i] = packet.flags[i];
            }

            for (int i = 0; i < packet.giaddr.Length; i++)
            {
                n_packet.giaddr[i] = packet.giaddr[i];
            }

            for (int i = 0; i < packet.chaddr.Length; i++)
            {
                n_packet.chaddr[i] = packet.chaddr[i];
            }

            option op_field = new option();

            op_field.add(new byte[] { 99, 139, 83, 99 }); // add dhcp magic option
            op_field.add(new byte[] { 53, 1, 6 });        // add messeage type dhcp nack
            op_field.add(new byte[] { 54, 4 });           // add dhcp server identify
            op_field.add(network_config.dhcpserver.GetAddressBytes());
            op_field.add(new byte[] { 255 });             // add end

            n_packet.options = new byte[op_field.size];
            for (int i = 0; i < op_field.size; i++)
            {
                n_packet.options[i] = op_field.data[i];
            }
            //
            SendPacket(n_packet);
        }
Ejemplo n.º 14
0
        } // Chuyen tu byte array sang string

        void Send_DHCPDiscover()
        {
            // send dhcp discover
            DHCPPacket packet = new DHCPPacket();

            packet.Init();
            packet.op    = 1;
            packet.htype = 1;
            packet.hlen  = 6;
            packet.hops  = 0;
            Random _random = new Random();

            packet.xid[0] = (byte)_random.Next(0, 255);
            packet.xid[1] = (byte)_random.Next(0, 255);
            packet.xid[2] = (byte)_random.Next(0, 255);
            packet.xid[3] = (byte)_random.Next(0, 255);

            xid.Add(ByteArrayToString(packet.xid));

            for (int i = 0; i < MacAddr.Length; i++)
            {
                packet.chaddr[i] = MacAddr[i];
            }

            option op_field = new option();

            op_field.add(new byte[] { 99, 139, 83, 99 }); // add dhcp magic option
            op_field.add(new byte[] { 53, 1, 1 });        // add messeage type dhcp discover
            op_field.add(new byte[] { 55, 3, 1, 3, 6 });  // add parament request list
            op_field.add(new byte[] { 255 });             // add end

            packet.options = new byte[op_field.size];
            for (int i = 0; i < op_field.size; i++)
            {
                packet.options[i] = op_field.data[i];
            }
            //
            SendPacket(packet);
        }
Ejemplo n.º 15
0
        protected override void Run()
        {
            /**
             * Packet creation and parsing tests
             **/

            /** Ethernet Packet Parsing Test **/
            byte[] ethernetPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x0C, 0x29, 0xD5, 0xDB, 0x9D, 0x08, 0x00
            };
            EthernetPacket ethernetPacket = new EthernetPacket(ethernetPacketData);

            Equals(ethernetPacketData, ethernetPacket.RawData);

            /** IP Packet Parsing Test **/
            byte[] ipPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x0C, 0x29, 0xD5, 0xDB, 0x9D, 0x08, 0x00, 0x45, 0x00, 0x01, 0x16, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x39,
                0xD8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF
            };
            IPPacket ipPacket = new IPPacket(ipPacketData);

            Equals(ipPacketData, ipPacket.RawData);

            /** UDP Packet Parsing Test **/
            byte[] udpPacketData = new byte[]
            {
                0x98, 0xFA, 0x9B, 0xD4, 0xEB, 0x29, 0xD8, 0xCE, 0x3A, 0x89, 0x3E, 0xD9, 0x08, 0x00, 0x45, 0x00, 0x00, 0x22, 0x0C, 0x74, 0x40, 0x00, 0x40, 0x11, 0xAA,
                0xBE, 0xC0, 0xA8, 0x01, 0x02, 0xC0, 0xA8, 0x01, 0x46, 0x10, 0x92, 0x10, 0x92, 0x00, 0x0E, 0x37, 0x22, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21
            };
            UDPPacket udpPacket = new UDPPacket(udpPacketData);

            Equals(udpPacketData, udpPacket.RawData);

            /** DNS Packet Parsing Test **/
            byte[] dnsPacketData = new byte[]
            {
                0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x98, 0xFA, 0x9B, 0xD4, 0xEB, 0x29, 0x08, 0x00, 0x45, 0x00, 0x00, 0x38, 0xC3, 0x1C, 0x00, 0x00, 0x80, 0x11, 0x00,
                0x00, 0xC0, 0xA8, 0x01, 0x46, 0xC0, 0xA8, 0x01, 0xFE, 0xF0, 0x66, 0x00, 0x35, 0x00, 0x24, 0x84, 0xCA, 0xD6, 0x80, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x69, 0x74, 0x74, 0x65, 0x72, 0x03, 0x63, 0x6F, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x01
            };
            DNSPacket dnsPacket = new DNSPacket(dnsPacketData);

            Equals(dnsPacketData, dnsPacket.RawData);

            /** DHCP Packet Parsing Test **/
            byte[] dhcpPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x08, 0x00, 0x45, 0xC0, 0x01, 0x59, 0x46, 0x3F, 0x00, 0x00, 0x40, 0x11, 0x6F,
                0xEF, 0xC0, 0xA8, 0x01, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x43, 0x00, 0x44, 0x01, 0x45, 0xD3, 0xC8, 0x02, 0x01, 0x06, 0x00, 0x84, 0xA9, 0x5A, 0x66,
                0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x47, 0xC0, 0xA8, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x34, 0xE1, 0x2D, 0xA3, 0x06,
                0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36, 0x04, 0xC0, 0xA8, 0x01, 0xFE, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x3A, 0x04, 0x00,
                0x00, 0xA8, 0xC0, 0x3B, 0x04, 0x00, 0x01, 0x27, 0x50, 0x1C, 0x04, 0xC0, 0xA8, 0x01, 0xFF, 0x51, 0x12, 0x03, 0xFF, 0xFF, 0x44, 0x45, 0x53, 0x4B, 0x54,
                0x4F, 0x50, 0x2D, 0x49, 0x51, 0x48, 0x4A, 0x33, 0x31, 0x43, 0x06, 0x04, 0xC0, 0xA8, 0x01, 0xFE, 0x0F, 0x03, 0x6C, 0x61, 0x6E, 0x03, 0x04, 0xC0, 0xA8,
                0x01, 0xFE, 0x01, 0x04, 0xFF, 0xFF, 0xFF, 0x00, 0xFF
            };
            DHCPPacket dhcpPacket = new DHCPPacket(dhcpPacketData);

            Equals(dhcpPacketData, dhcpPacket.RawData);

            /** TCP Packet Parsing Test **/
            byte[] tcpPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x00, 0x45, 0x00, 0x00, 0x3C, 0x64, 0x92, 0x40, 0x00, 0x40, 0x06, 0x51,
                0xA2, 0xC0, 0xA8, 0x01, 0xD3, 0xC0, 0xA8, 0x01, 0x64, 0xA8, 0xAB, 0x10, 0x92, 0x67, 0x7C, 0xCE, 0x18, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x02, 0x72, 0x10,
                0x5F, 0xF0, 0x00, 0x00, 0x02, 0x04, 0x05, 0xB4, 0x04, 0x02, 0x08, 0x0A, 0x58, 0x1A, 0xAA, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07
            };
            TCPPacket tcpPacket = new TCPPacket(tcpPacketData);

            Equals(tcpPacket.SourcePort, 43179);
            Equals(tcpPacket.DestinationPort, 4242);
            Equals(tcpPacket.SequenceNumber, 0x677CCE18);
            Equals(tcpPacket.AckNumber, 0);
            Equals(tcpPacket.TCPFlags, Flags.SYN);
            Equals(tcpPacket.WindowSize, 29200);
            Equals(tcpPacket.Checksum, 0x5FF0);
            Equals(tcpPacket.UrgentPointer, 0);

            /** ARP Packet Parsing Test **/
            byte[] arpPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xB8, 0xD9, 0x4D,
                0xC1, 0xA5, 0xFC, 0xC0, 0xA8, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            ARPPacket arpPacket = new ARPPacket(arpPacketData);

            Equals(arpPacketData, arpPacket.RawData);

            TestController.Completed();

            /**
             * Clients tests
             **/
            var dhcpCLient = new DHCPClient();

            dhcpCLient.Close();

            var dnsClient = new DnsClient();

            dnsClient.Close();

            var udpClient = new UdpClient();

            udpClient.Close();

            var tcpClient = new TcpClient(4242);

            tcpClient.Close();

            var icmpClient = new ICMPClient();

            icmpClient.Close();
        }
        private static byte[] BuildDataStructure(DHCPPacket packet)
        {
            byte[] mArray;

            try
            {
                mArray = new byte[0];
                AddOptionElement(new byte[] { packet.op }, ref mArray);
                AddOptionElement(new byte[] { packet.htype }, ref mArray);
                AddOptionElement(new byte[] { packet.hlen }, ref mArray);
                AddOptionElement(new byte[] { packet.hops }, ref mArray);
                AddOptionElement(packet.xid, ref mArray);
                AddOptionElement(packet.secs, ref mArray);
                AddOptionElement(packet.flags, ref mArray);
                AddOptionElement(packet.ciaddr, ref mArray);
                AddOptionElement(packet.yiaddr, ref mArray);
                AddOptionElement(packet.siaddr, ref mArray);
                AddOptionElement(packet.giaddr, ref mArray);
                AddOptionElement(packet.chaddr, ref mArray);
                AddOptionElement(packet.sname, ref mArray);
                AddOptionElement(packet.file, ref mArray);

                AddOptionElement(packet.mcookie, ref mArray);
                AddOptionElement(packet.options, ref mArray);
                return mArray;
            }
            finally
            {
                mArray = null;
            }
        }
Ejemplo n.º 17
0
        void HandlePacket(DHCPPacket packet)
        {
            // Phan biet cac goi tin request co phai selecting hay khong
            bool flag1 = false;
            // Xu li goi tin dhcp
            List <byte[]> Option = packet.optionsplit();

            for (int i = 0; i < Option.Count(); i++) // dhcp server identify khi goi request_new hoac release
            {
                if (Option[i][0] == 54)
                {
                    if (!network_config.dhcpserver.Equals(new IPAddress(new byte[] { Option[i][2], Option[i][3], Option[i][4], Option[i][5] })))
                    {
                        return;
                    }
                    flag1 = true; // Neu la goi tin request thi se o trang thai selecting
                    break;
                }
            }
            for (int i = 0; i < Option.Count(); i++)
            {
                if (Option[i][0] == 53)
                {
                    if (Option[i][2] == 1) // xac dinh dhcp discover
                    {
                        xid.Add(ByteArrayToString(packet.xid));

                        byte[] b_MacAddress = new byte[packet.hlen];
                        for (int j = 0; j < packet.hlen; j++)
                        {
                            b_MacAddress[j] = packet.chaddr[j];
                        }
                        string s_MacAddress = ByteArrayToString(b_MacAddress);
                        for (int z = 0; z < table.Count(); z++)
                        {
                            if (s_MacAddress == table[z].macaddress)
                            {
                                Send_DHCPOffer(packet, IPAddress.Parse(table[z].ip));
                                return;
                            }
                        }

                        IPAddress IP = allocate();
                        if (IP.Equals(IPAddress.Parse("0.0.0.0"))) // if out of IP
                        {
                            return;
                        }
                        Send_DHCPOffer(packet, IP);
                    }
                    else if (Option[i][2] == 3) // Xac dinh dhcp request
                    {
                        byte[] p = new byte[6];
                        if (!xid.Contains(ByteArrayToString(packet.xid))) // neu xid khong thuoc cac xid dang giap tiep
                        {
                            return;
                        }
                        IPAddress IP;
                        IP = new IPAddress(packet.ciaddr);                          // request o dang rebound, ip -> ciaddr
                        if (packet.ciaddr.SequenceEqual(new byte[] { 0, 0, 0, 0 })) // chua co ip -> dhcp dang o dang new
                        {
                            for (int j = 0; j < Option.Count(); j++)
                            {
                                if (Option[j][0] == 50) // request IP address
                                {
                                    IP = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] });
                                    break;
                                }
                            }
                        }

                        byte[] b_MacAddress = new byte[packet.hlen];
                        for (int j = 0; j < packet.hlen; j++)
                        {
                            b_MacAddress[j] = packet.chaddr[j];
                        }
                        string s_MacAddress = ByteArrayToString(b_MacAddress);

                        Item item = new Item();
                        item.macaddress = s_MacAddress;
                        item.ip         = IP.ToString();
                        Int64 epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // setup item
                        item.time = epoch + network_config.leasetime;

                        bool flag2 = false;

                        for (int j = 0; j < table.Count(); j++)
                        {
                            if (table[j].ip == item.ip)
                            {
                                if (table[j].macaddress == item.macaddress) // goi tin dang xin gia han
                                {
                                    table.RemoveAt(j);                      // xoa goi cu
                                    flag2 = true;
                                }
                                else
                                {
                                    Send_DHCPNak(packet);
                                    return;
                                }
                            }
                        }
                        if (flag1 || flag2)           // La goi tin dhcp request seleting thi xac nhan, neu la cac goi tin renew, reboot thi phai co san IP trong bang
                        {
                            table.Add(item);          // add goi moi vo
                            Send_DHCPAck(packet, IP); // send goi ack
                        }
                    }
                    else // goi dhcp release
                    {
                        IPAddress IP           = new IPAddress(packet.ciaddr);
                        byte[]    b_MacAddress = new byte[packet.hlen];
                        for (int j = 0; j < packet.hlen; j++)
                        {
                            b_MacAddress[j] = packet.chaddr[j];
                        }
                        string s_mac_address = ByteArrayToString(b_MacAddress);
                        for (int j = 0; j < table.Count(); j++) // Duyet ban va xoa di ip
                        {
                            if (table[j].ip == IP.ToString() || table[j].macaddress == s_mac_address)
                            {
                                table.RemoveAt(j);
                                j--;
                            }
                        }
                    }
                    break;
                }
            }
        }
Ejemplo n.º 18
0
 public DetailPacket(DHCPPacket packet)
 {
     InitializeComponent();
     rtbMessage.Text = packet.ToText();
 }
Ejemplo n.º 19
0
        void HandlePacket(DHCPPacket packet)
        {
            // Xu li khi nhan goi DHCP tu server
            byte[] p = new byte[6];
            bool   d = xid.Contains(ByteArrayToString(packet.xid));

            if (!d) // neu xid khong thuoc cac xid da gui
            {
                return;
            }
            List <byte[]> Option = packet.optionsplit();

            for (int i = 0; i < Option.Count(); i++)
            {
                if (Option[i][0] == 53)    // dhcp messeage type
                {
                    if (Option[i][2] == 2) // xac dinh dhcp offer
                    {
                        if (!DHCPServer_IP.SequenceEqual(new byte[] { 0, 0, 0, 0 }))
                        {
                            return; // if we already have choose dhcp server, so not doing anything
                        }
                        for (int j = 0; j < Option.Count(); j++)
                        {
                            if (Option[j][0] == 54) // server dhcp minh da chon
                            {
                                for (int z = 0; z < 4; z++)
                                {
                                    DHCPServer_IP[z] = Option[j][z + 2];
                                }
                                offer_saved = packet;
                                break;
                            }
                        }
                        Send_DHCPRequest(packet, DHCPServer_IP, 1); // gui goi tin request gom goi dhcp offer va dhcp server da chon
                    }
                    else if (Option[i][2] == 5)                     // Xac dinh day la goi dhcp ack
                    {
                        FileStream fs;
                        try
                        {
                            fs = new FileStream("IP", FileMode.Create);
                            byte[] save = offer_saved.DHCPPacketToBytes();
                            fs.Write(save, 0, save.Length);
                            fs.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Can not save offer_packet");
                        }
                        ip = new IPAddress(packet.yiaddr);
                        for (int j = 0; j < Option.Count(); j++)
                        {
                            if (Option[j][0] == 1)
                            {
                                subnetmask = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] });
                            }
                            if (Option[j][0] == 3)
                            {
                                defaultgateway = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] });
                            }
                            if (Option[j][0] == 6)
                            {
                                dns = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] });
                            }
                            if (Option[j][0] == 51)
                            {
                                Int64 epoch     = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
                                Int64 leasetime = BitConverter.ToInt32(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }, 0);
                                time = epoch + leasetime;
                                t1   = epoch + (int)(leasetime * 0.5);
                                t2   = epoch + (int)(leasetime * 0.875);
                            }
                        }
                        haveip = true;
                    }
                    else // Goi tin DHCP Nak
                    {
                        for (int j = 0; j < Option.Count(); j++)
                        {
                            if (Option[j][0] == 54) // server dhcp minh da chon
                            {
                                for (int z = 0; z < 4; z++)
                                {
                                    if (DHCPServer_IP[z] != Option[j][z + 2]) // Neu khong phai server dhcp da chon
                                    {
                                        return;
                                    }
                                }
                                Send_DHCPDiscover(); // Chuyen ve trang thai requesting
                                return;
                            }
                        }
                    }
                    return;
                }
            }
        }
Ejemplo n.º 20
0
        protected override void Run()
        {
            /**
             * Packet creation and parsing tests
             **/

            /** Ethernet Packet Parsing Test **/
            byte[] ethernetPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x0C, 0x29, 0xD5, 0xDB, 0x9D, 0x08, 0x00
            };
            EthernetPacket ethernetPacket = new EthernetPacket(ethernetPacketData);

            Equals(ethernetPacketData, ethernetPacket.RawData);

            /** IP Packet Parsing Test **/
            byte[] ipPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x0C, 0x29, 0xD5, 0xDB, 0x9D, 0x08, 0x00, 0x45, 0x00, 0x01, 0x16, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x39,
                0xD8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF
            };
            IPPacket ipPacket = new IPPacket(ipPacketData);

            Equals(ipPacketData, ipPacket.RawData);

            /** UDP Packet Parsing Test **/
            byte[] udpPacketData = new byte[]
            {
                0x98, 0xFA, 0x9B, 0xD4, 0xEB, 0x29, 0xD8, 0xCE, 0x3A, 0x89, 0x3E, 0xD9, 0x08, 0x00, 0x45, 0x00, 0x00, 0x22, 0x0C, 0x74, 0x40, 0x00, 0x40, 0x11, 0xAA,
                0xBE, 0xC0, 0xA8, 0x01, 0x02, 0xC0, 0xA8, 0x01, 0x46, 0x10, 0x92, 0x10, 0x92, 0x00, 0x0E, 0x37, 0x22, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21
            };
            UDPPacket udpPacket = new UDPPacket(udpPacketData);

            Equals(udpPacketData, udpPacket.RawData);

            /** DNS Packet Parsing Test **/
            byte[] dnsPacketData = new byte[]
            {
                0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x98, 0xFA, 0x9B, 0xD4, 0xEB, 0x29, 0x08, 0x00, 0x45, 0x00, 0x00, 0x38, 0xC3, 0x1C, 0x00, 0x00, 0x80, 0x11, 0x00,
                0x00, 0xC0, 0xA8, 0x01, 0x46, 0xC0, 0xA8, 0x01, 0xFE, 0xF0, 0x66, 0x00, 0x35, 0x00, 0x24, 0x84, 0xCA, 0xD6, 0x80, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x06, 0x67, 0x69, 0x74, 0x74, 0x65, 0x72, 0x03, 0x63, 0x6F, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x01
            };
            DNSPacket dnsPacket = new DNSPacket(dnsPacketData);

            Equals(dnsPacketData, dnsPacket.RawData);

            /** DHCP Packet Parsing Test **/
            byte[] dhcpPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x08, 0x00, 0x45, 0xC0, 0x01, 0x59, 0x46, 0x3F, 0x00, 0x00, 0x40, 0x11, 0x6F,
                0xEF, 0xC0, 0xA8, 0x01, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x43, 0x00, 0x44, 0x01, 0x45, 0xD3, 0xC8, 0x02, 0x01, 0x06, 0x00, 0x84, 0xA9, 0x5A, 0x66,
                0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x47, 0xC0, 0xA8, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x34, 0xE1, 0x2D, 0xA3, 0x06,
                0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36, 0x04, 0xC0, 0xA8, 0x01, 0xFE, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x3A, 0x04, 0x00,
                0x00, 0xA8, 0xC0, 0x3B, 0x04, 0x00, 0x01, 0x27, 0x50, 0x1C, 0x04, 0xC0, 0xA8, 0x01, 0xFF, 0x51, 0x12, 0x03, 0xFF, 0xFF, 0x44, 0x45, 0x53, 0x4B, 0x54,
                0x4F, 0x50, 0x2D, 0x49, 0x51, 0x48, 0x4A, 0x33, 0x31, 0x43, 0x06, 0x04, 0xC0, 0xA8, 0x01, 0xFE, 0x0F, 0x03, 0x6C, 0x61, 0x6E, 0x03, 0x04, 0xC0, 0xA8,
                0x01, 0xFE, 0x01, 0x04, 0xFF, 0xFF, 0xFF, 0x00, 0xFF
            };
            DHCPPacket dhcpPacket = new DHCPPacket(dhcpPacketData);

            Equals(dhcpPacketData, dhcpPacket.RawData);

            /** ARP Packet Parsing Test **/
            byte[] arpPacketData = new byte[]
            {
                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xB8, 0xD9, 0x4D, 0xC1, 0xA5, 0xFC, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xB8, 0xD9, 0x4D,
                0xC1, 0xA5, 0xFC, 0xC0, 0xA8, 0x01, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            ARPPacket arpPacket = new ARPPacket(arpPacketData);

            Equals(arpPacketData, arpPacket.RawData);

            TestController.Completed();

            /**
             * Clients tests
             **/
            var dhcpCLient = new DHCPClient();

            dhcpCLient.Close();

            var dnsClient = new DnsClient();

            dnsClient.Close();

            var udpClient = new UdpClient();

            udpClient.Close();

            var icmpClient = new ICMPClient();

            icmpClient.Close();
        }