Beispiel #1
0
        public static DhcpPacket GenerateDhcpDiscoverPacket(Parameters parameters)
        {
            var packet = new DhcpPacket(new byte[244]);

            // Generate a Transaction ID for this request
            var xid = new byte[4];
            xid[0] = 0xE7;
            xid[1] = 0x03;
            xid[2] = 0x00;
            xid[3] = 0x00;
            //XidRandom.NextBytes(xid);

            byte[] macAddress = parameters.MacAddress;

            // Actual DHCPDISCOVER packet
            byte[] data = packet._data;

            Array.Copy(xid, 0, data, 4, 4);
            Array.Copy(macAddress, 0, data, 28, 6);

            // Set the OP Code to BOOTREQUEST
            data[0] = 1;
            // Set the Hardware Address Type to Ethernet
            data[1] = 1;
            // Set the Hardware Address Length (number of bytes)
            data[2] = 6;
            // Set the Broadcast Flag
            data[10] = 128;
            // Set the Magic Cookie values
            data[236] = 99; // D
            data[237] = 130; // H
            data[238] = 83; // C
            data[239] = 99; // P
            // Set the DHCPDiscover Message Type Option
            data[240] = 53;
            data[241] = 1;
            data[242] = 1;

            // End Option
            data[243] = 255;
            return packet;
        }
Beispiel #2
0
        private void Write(Packet packet)
        {
            var ethPacket = packet as EthernetPacket;
            if (ethPacket == null)
                return;
            var ipPacket = ethPacket.Extract(typeof (IPv4Packet)) as IPv4Packet;
            if (ipPacket == null)
                return;
            var udpPacket = ipPacket.Extract(typeof (UdpPacket)) as UdpPacket;
            if (udpPacket == null)
                return;

            var sb = new StringBuilder();
            sb.Append(String.Format("{0:yyyyMMdd HHmmss:fff}", DateTime.Now));
            sb.Append("\t");
            sb.Append(ethPacket.SourceHwAddress);
            sb.Append("\t");
            sb.Append(ethPacket.DestinationHwAddress);
            sb.Append("\t");
            sb.Append(ipPacket.SourceAddress);
            sb.Append("\t");
            sb.Append(ipPacket.DestinationAddress);
            sb.Append("\t");
            sb.Append(udpPacket.SourcePort);
            sb.Append("\t");
            sb.Append(udpPacket.DestinationPort);
            sb.Append("\t");

            var dhcpPacket = new DhcpPacket(udpPacket.PayloadData);
            dhcpPacket.ReadPacket(_parameters, sb);
            sb.Append("\r\n");

            String loggingText = sb.ToString();
            File.AppendAllText(_parameters.LogFilename, loggingText);
            Console.Write(loggingText);
        }