示例#1
0
 internal void CreateOptionElement(DhcpOptionType optionType, byte[] dataToAdd, ref byte[] source)
 {
     try
     {
         var newOption = new byte[dataToAdd.Length + 2];
         //add the code, and data length
         newOption[0] = (byte)optionType;
         newOption[1] = (byte)dataToAdd.Length;
         //add the code to put in
         Array.Copy(dataToAdd, 0, newOption, 2, dataToAdd.Length);
         //copy the data to the out array
         if (source == null)
         {
             Array.Resize(ref source, newOption.Length);
         }
         else
         {
             Array.Resize(ref source, source.Length + newOption.Length);
         }
         Array.Copy(newOption, 0, source, source.Length - newOption.Length, newOption.Length);
     }
     catch (Exception e)
     {
         throw new Exception($"{GetType().FullName}.CreateOptionElement:{e.Message}");
     }
 }
示例#2
0
        public DhcpOption CreateFromRaw(DhcpOptionType type, byte[] rawData)
        {
            switch (type)
            {
            case DhcpOptionType.SubnetMask:
                return(DhcpSubnetMaskOption.CreateFromRaw(rawData));

            case DhcpOptionType.Router:
                return(DhcpRouterOption.CreateFromRaw(rawData));

            case DhcpOptionType.DomainNameServer:
                return(DhcpDomainNameServerOption.CreateFromRaw(rawData));

            case DhcpOptionType.DomainName:
                return(DhcpDomainNameOption.CreateFromRaw(rawData));

            case DhcpOptionType.ServerIdentifier:
                return(DhcpServerIdentifierOption.CreateFromRaw(rawData));

            case DhcpOptionType.ClientIdentifier:
                return(DhcpClientIdentifier.CreateFromRaw(rawData));

            default:
                return(null);
            }
        }
示例#3
0
        public static DhcpPacket CreateFromRaw(byte[] rawPacket)
        {
            DhcpPacket packet = new DhcpPacket();

            using (var reader = new BinaryReader(new MemoryStream(rawPacket)))
            {
                packet.MessageType = (DhcpMessageType)reader.ReadByte();
                HardwareAddressType hardwareAddressType = (HardwareAddressType)reader.ReadByte();
                byte hardwareAddressLength = reader.ReadByte();
                packet.Hops              = reader.ReadByte();
                packet.TransactionId     = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
                packet.SecondsElapsed    = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16());
                packet.Flags             = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16());
                packet.ClientAddress     = new IPAddress(reader.ReadBytes(4));
                packet.YourClientAddress = new IPAddress(reader.ReadBytes(4));
                packet.NextServerAddress = new IPAddress(reader.ReadBytes(4));
                packet.RelayAgentAddress = new IPAddress(reader.ReadBytes(4));
                packet.HardwareAddress   = new HardwareAddress(hardwareAddressType,
                                                               reader.ReadBytes(hardwareAddressLength));
                reader.ReadBytes(16 - hardwareAddressLength); // padding
                packet.ServerName   = Encoding.ASCII.GetString(reader.ReadBytes(64));
                packet.BootFileName = Encoding.ASCII.GetString(reader.ReadBytes(128));

                int packetMagicCookie = IPAddress.NetworkToHostOrder(reader.ReadInt32());
                if (packetMagicCookie != MagicCookie)
                {
                    throw new InvalidDataException("Magic cookie not found!");
                }

                byte messageType = reader.ReadByte();
                if (messageType != 53)
                {
                    throw new InvalidDataException("Options should start with Message type!");
                }
                reader.ReadByte(); // length
                packet.MessageType = (DhcpMessageType)reader.ReadByte();

                var  optionFactory = new DhcpOptionFactory();
                byte optionType    = reader.ReadByte();
                while (optionType != 255)
                {
                    DhcpOptionType type       = (DhcpOptionType)optionType;
                    byte           length     = reader.ReadByte();
                    byte[]         optionData = reader.ReadBytes(length);

                    DhcpOption option = optionFactory.CreateFromRaw(type, optionData);
                    if (option != null)
                    {
                        packet.Options.Add(option);
                    }
                    else
                    {
                        Console.WriteLine($"WARN: Unsupported option type: {type}");
                    }
                    optionType = reader.ReadByte();
                }
            }
            return(packet);
        }
示例#4
0
        internal byte[] GetOptionData(DhcpOptionType optionType)
        {
            var code = (byte)optionType;

            try
            {
                //loop through look for the bit that states that the identifier is there
                for (int i = 0; i < options.Length; i++)
                {
                    if (options[i] == (byte)DhcpOptionType.End)
                    {
                        break;
                    }
                    //at the start we have the code + length
                    //i has the code, i+1 = length of data, i+1+n = data skip
                    if (options[i] == code)
                    {
                        var len  = options[i + 1];
                        var data = new byte[len];
                        Array.Copy(options, i + 2, data, 0, len);
                        return(data);
                    }
                    else
                    {
                        // jump to next option message
                        i += 1 + options[i + 1];
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception($"{GetType().FullName}.GetOptionData:{e.Message}");
            }

            return(null);
        }