Exemple #1
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);
        }
Exemple #2
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 };
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }