Exemple #1
0
 internal Header(ByteReader br)
 {
     _transactionID = br.ReadUInt16();
     _flags = br.ReadUInt16();
     _numQuestions = br.ReadUInt16();
     _numAnswers = br.ReadUInt16();
     _numAuthorities = br.ReadUInt16();
     _numAdditionals = br.ReadUInt16();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="DhcpMessage"/> class.
        /// <param name="data">Array containing the data to decode.</param>
        /// </summary>
        public DhcpMessage(byte[] data)
        {
            ByteReader byteReader = new ByteReader(data, ByteOrder.Network);

            _timestamp = DateTime.Now;

            _op = byteReader.ReadByte();
            _htype = byteReader.ReadByte();
            _hlen = byteReader.ReadByte();
            _hops = byteReader.ReadByte();
            _xid = byteReader.ReadUInt32();
            _secs = byteReader.ReadUInt16();
            _flags = byteReader.ReadBytes(2);
            _ciaddr = byteReader.ReadBytes(4);
            _yiaddr = byteReader.ReadBytes(4);
            _siaddr = byteReader.ReadBytes(4);
            _giaddr = byteReader.ReadBytes(4);
            _chaddr = byteReader.ReadBytes(16);
            _sname = byteReader.ReadBytes(64);
            _file = byteReader.ReadBytes(128);

            byte[] rawOptions = byteReader.ReadBytes(byteReader.AvailableBytes);
            int offset = 0;

            // if magic number is valid then process options
            if (offset + 4 < rawOptions.Length &&
                (BitConverter.ToUInt32(rawOptions, offset) == Constants.DHCP_OPTIONS_MAGIC_NUMBER
                || BitConverter.ToUInt32(rawOptions, offset) == Constants.DHCP_WIN_OPTIONS_MAGIC_NUMBER))
            {
                offset += 4;
                Boolean end = false;

                while (!end && offset < rawOptions.Length)
                {
                    DhcpOption option = (DhcpOption)rawOptions[offset];
                    offset++;

                    int optionLen;

                    switch (option)
                    {
                        case DhcpOption.Pad:
                            continue;
                        case DhcpOption.End:
                            end = true;
                            continue;
                        default:
                            optionLen = (int)rawOptions[offset];
                            offset++;
                            break;
                    }

                    byte[] optionData = new byte[optionLen];

                    Array.Copy(rawOptions, offset, optionData, 0, optionLen);
                    offset += optionLen;

                    this._options.Add(option, optionData);
                    this._optionDataSize += optionLen;
                }
            }
        }