Example #1
0
        private void ParseOptions(Stream s, List <DhcpOption> options)
        {
            while (true)
            {
                DhcpOption option = DhcpOption.Parse(s);
                if (option.Code == DhcpOptionCode.End)
                {
                    break;
                }

                if (option.Code == DhcpOptionCode.Pad)
                {
                    continue;
                }

                bool optionExists = false;

                foreach (DhcpOption existingOption in options)
                {
                    if (existingOption.Code == option.Code)
                    {
                        //option already exists so append current option value into existing option
                        existingOption.AppendOptionValue(option);
                        optionExists = true;
                        break;
                    }
                }

                if (optionExists)
                {
                    continue;
                }

                //add option to list
                options.Add(option);

                switch (option.Code)
                {
                case DhcpOptionCode.DhcpMessageType:
                    _dhcpMessageType = option as DhcpMessageTypeOption;
                    break;

                case DhcpOptionCode.ClientIdentifier:
                    _clientIdentifier = option as ClientIdentifierOption;
                    break;

                case DhcpOptionCode.HostName:
                    _hostName = option as HostNameOption;
                    break;

                case DhcpOptionCode.ClientFullyQualifiedDomainName:
                    _clientFullyQualifiedDomainName = option as ClientFullyQualifiedDomainNameOption;
                    break;

                case DhcpOptionCode.ParameterRequestList:
                    _parameterRequestList = option as ParameterRequestListOption;
                    break;

                case DhcpOptionCode.MaximumDhcpMessageSize:
                    _maximumDhcpMessageSize = option as MaximumDhcpMessageSizeOption;
                    break;

                case DhcpOptionCode.ServerIdentifier:
                    _serverIdentifier = option as ServerIdentifierOption;
                    break;

                case DhcpOptionCode.RequestedIpAddress:
                    _requestedIpAddress = option as RequestedIpAddressOption;
                    break;

                case DhcpOptionCode.OptionOverload:
                    _optionOverload = option as OptionOverloadOption;
                    break;
                }
            }
        }
Example #2
0
        public DhcpMessage(Stream s)
        {
            byte[] buffer = new byte[4];

            s.ReadBytes(buffer, 0, 4);
            _op    = (DhcpMessageOpCode)buffer[0];
            _htype = (DhcpMessageHardwareAddressType)buffer[1];
            _hlen  = buffer[2];
            _hops  = buffer[3];

            _xid = s.ReadBytes(4);

            s.ReadBytes(buffer, 0, 4);
            _secs = new byte[2];
            Buffer.BlockCopy(buffer, 0, _secs, 0, 2);
            Array.Reverse(buffer);
            _flags = (DhcpMessageFlags)BitConverter.ToUInt16(buffer, 0);

            s.ReadBytes(buffer, 0, 4);
            _ciaddr = new IPAddress(buffer);

            s.ReadBytes(buffer, 0, 4);
            _yiaddr = new IPAddress(buffer);

            s.ReadBytes(buffer, 0, 4);
            _siaddr = new IPAddress(buffer);

            s.ReadBytes(buffer, 0, 4);
            _giaddr = new IPAddress(buffer);

            _chaddr = s.ReadBytes(16);
            _clientHardwareAddress = new byte[_hlen];
            Buffer.BlockCopy(_chaddr, 0, _clientHardwareAddress, 0, _hlen);

            _sname = s.ReadBytes(64);
            _file  = s.ReadBytes(128);

            //read options
            List <DhcpOption> options = new List <DhcpOption>();

            _options = options;

            s.ReadBytes(buffer, 0, 4);
            uint magicCookie = BitConverter.ToUInt32(buffer, 0);

            if (magicCookie == MAGIC_COOKIE)
            {
                ParseOptions(s, options);

                if (_optionOverload != null)
                {
                    if (_optionOverload.Value.HasFlag(OptionOverloadValue.FileFieldUsed))
                    {
                        using (MemoryStream mS = new MemoryStream(_file))
                        {
                            ParseOptions(mS, options);
                        }
                    }

                    if (_optionOverload.Value.HasFlag(OptionOverloadValue.SnameFieldUsed))
                    {
                        using (MemoryStream mS = new MemoryStream(_sname))
                        {
                            ParseOptions(mS, options);
                        }
                    }
                }

                //parse all option values
                foreach (DhcpOption option in options)
                {
                    option.ParseOptionValue();
                }
            }

            if (_clientIdentifier == null)
            {
                _clientIdentifier = new ClientIdentifierOption((byte)_htype, _clientHardwareAddress);
            }

            if (_maximumDhcpMessageSize != null)
            {
                _maximumDhcpMessageSize = new MaximumDhcpMessageSizeOption(576);
            }
        }