Ejemplo n.º 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;
                }
            }
        }
Ejemplo n.º 2
0
        public DhcpMessage(DhcpMessageOpCode op, DhcpMessageHardwareAddressType hardwareAddressType, byte[] xid, byte[] secs, DhcpMessageFlags flags, IPAddress ciaddr, IPAddress yiaddr, IPAddress siaddr, IPAddress giaddr, byte[] clientHardwareAddress, string sname, string file, IReadOnlyCollection <DhcpOption> options)
        {
            if (ciaddr.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Address family not supported.", nameof(ciaddr));
            }

            if (yiaddr.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Address family not supported.", nameof(yiaddr));
            }

            if (siaddr.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Address family not supported.", nameof(siaddr));
            }

            if (giaddr.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Address family not supported.", nameof(giaddr));
            }

            if (clientHardwareAddress == null)
            {
                throw new ArgumentNullException(nameof(clientHardwareAddress));
            }

            if (clientHardwareAddress.Length > 16)
            {
                throw new ArgumentException("Client hardware address cannot exceed 16 bytes.", nameof(clientHardwareAddress));
            }

            if (xid.Length != 4)
            {
                throw new ArgumentException("Transaction ID must be 4 bytes.", nameof(xid));
            }

            if (secs.Length != 2)
            {
                throw new ArgumentException("Seconds elapsed must be 2 bytes.", nameof(secs));
            }

            _op    = op;
            _htype = hardwareAddressType;
            _hlen  = Convert.ToByte(clientHardwareAddress.Length);
            _hops  = 0;

            _xid = xid;

            _secs  = secs;
            _flags = flags;

            _ciaddr = ciaddr;
            _yiaddr = yiaddr;
            _siaddr = siaddr;
            _giaddr = giaddr;

            _clientHardwareAddress = clientHardwareAddress;
            _chaddr = new byte[16];
            Buffer.BlockCopy(_clientHardwareAddress, 0, _chaddr, 0, _clientHardwareAddress.Length);

            _sname = new byte[64];
            if (sname != null)
            {
                _serverHostName = sname;

                byte[] buffer = Encoding.ASCII.GetBytes(sname);
                if (buffer.Length >= 64)
                {
                    throw new ArgumentException("Server host name cannot exceed 63 bytes.", nameof(sname));
                }

                Buffer.BlockCopy(buffer, 0, _sname, 0, buffer.Length);
            }

            _file = new byte[128];
            if (file != null)
            {
                _bootFileName = file;

                byte[] buffer = Encoding.ASCII.GetBytes(file);
                if (buffer.Length >= 128)
                {
                    throw new ArgumentException("Boot file name cannot exceed 127 bytes.", nameof(file));
                }

                Buffer.BlockCopy(buffer, 0, _file, 0, buffer.Length);
            }

            _options = options;

            foreach (DhcpOption option in _options)
            {
                if (option.Code == DhcpOptionCode.ServerIdentifier)
                {
                    _serverIdentifier = option as ServerIdentifierOption;
                    break;
                }
            }
        }