示例#1
0
        internal override void ReceiveEvent(DhcpFormat dhcp)
        {
            //DebugStub.WriteLine("FSM DHCP packet SELECTING.\n");

            // Check if message is in response to our request
            if (dhcp.BootMessageType != DhcpFormat.BootType.Reply ||
                dhcp.TransactionID != client.TransactionID ||
                dhcp.GetHardwareAddress() != client.MacAddress)
            {
                DebugStub.WriteLine("FSM DHCP bad id.\n");
                return;
            }

            IPv4 serverAddress = dhcp.NextServerIPAddress;

            // Check if offered address is valid (ie not zero
            // and below class E)
            IPv4 offeredAddress = dhcp.YourIPAddress;

            if (offeredAddress == IPv4.Any || offeredAddress.IsMulticast())
            {
                DebugStub.WriteLine("FSM DHCP multicast addr.\n");
                return;
            }

            // Check if message is an offer
            SortedList     offeredOptions = dhcp.GetOptions();
            DhcpByteOption messageType
                = offeredOptions[DhcpMessageType.OptionCode] as DhcpByteOption;

            if (messageType == null ||
                messageType.Value != (byte)DhcpFormat.MessageType.Offer)
            {
                DebugStub.WriteLine("FSM DHCP not an offer.\n");
                return;
            }

            // Must have parameters
            byte [] parameters = new byte [] {
                DhcpSubnetMask.OptionCode,
                DhcpRouter.OptionCode,
                // DhcpDomainNameServer.OptionCode
            };

            foreach (byte p in parameters)
            {
                IDhcpOption ido = offeredOptions[p] as IDhcpOption;
                if (ido == null)
                {
                    DebugStub.WriteLine("FSM DHCP missing option 0x{0:x2}.\n", DebugStub.ArgList(p));
                    return;
                }
            }

            client.CancelStateTimeout();
            client.ChangeState(new DhcpClientStateRequesting(client,
                                                             serverAddress,
                                                             offeredAddress,
                                                             offeredOptions));
        }
示例#2
0
        private static void TakeOption(SortedList offeredOptions,
                                       byte optionCode,
                                       DhcpFormat dhcpFormat)
        {
            IDhcpOption option = offeredOptions[optionCode] as IDhcpOption;

            if (option != null)
            {
                dhcpFormat.AddOption(option);
            }
        }
示例#3
0
        public void AddOption(IDhcpOption !option)
        {
            int newLength = option.PayloadLength + 2 + optionsUsedLength;

            if (newLength > this.options.Length)
            {
                newLength = (newLength + 64) & ~3;
                byte [] newOptions = new byte [newLength];
                this.options.CopyTo(newOptions, 0);
                this.options = newOptions;
            }
            option.Pack(this.options, ref optionsUsedLength);
        }
示例#4
0
        public DhcpPacketBuilder WithOption(IDhcpOption optionToAdd)
        {
            var existing = _options.SingleOrDefault(x => x.GetType() == optionToAdd.GetType());

            if (existing != null)
            {
                _options.Remove(existing);
            }

            _options.Add(optionToAdd);

            return(this);
        }
示例#5
0
        public void AddOption(IDhcpOption option)
        {
            int newLength = option.PayloadLength + 2 + optionsUsedLength;

            if (newLength > this.options.Length)
            {
                newLength = (newLength + 64) & ~3;
                byte [] newOptions = new byte [newLength];
                Buffer.MoveMemory(newOptions, this.options, 0, 0, this.options.Length);
                this.options = newOptions;
            }
            option.Pack(this.options, ref optionsUsedLength);
        }
        public void ReplaceExistingOption()
        {
            var updatedOptions = new IDhcpOption[]
            {
                new DhcpTimeOffsetOption(0),
                new DhcpSubnetMaskOption(IPAddress.Broadcast)
            };

            var packet = DhcpPacketBuilder.Create()
                         .WithOption(new DhcpTimeOffsetOption(0))
                         .WithOptions(updatedOptions)
                         .Build();

            Assert.Equal(updatedOptions[0], packet.GetOption <DhcpTimeOffsetOption>());
        }
        public void AddOption()
        {
            var options = new IDhcpOption[]
            {
                new DhcpTimeOffsetOption(0),
                new DhcpSubnetMaskOption(IPAddress.Broadcast),
            };

            var packet = DhcpPacketBuilder.Create()
                         .WithOptions(options)
                         .Build();

            Assert.Equal(options[0], packet.GetOption <DhcpTimeOffsetOption>());
            Assert.Equal(options[1], packet.GetOption <DhcpSubnetMaskOption>());
        }
示例#8
0
        internal override void ReceiveEvent(DhcpFormat dhcp)
        {
            //DebugStub.WriteLine("FSM DHCP packet REQUESTING.\n");

            // Check if message is in response to our request
            if (dhcp.BootMessageType != DhcpFormat.BootType.Reply ||
                dhcp.TransactionID != client.TransactionID ||
                dhcp.GetHardwareAddress() != client.MacAddress)
            {
                return;
            }

            IPv4 serverAddress = dhcp.NextServerIPAddress;

            // Check if offered address is valid (ie not zero
            // and below class E)
            IPv4 offeredAddress = dhcp.YourIPAddress;

            if (offeredAddress == IPv4.Any || offeredAddress.IsMulticast())
            {
                return;
            }

            // Check if message is an ack
            SortedList     offeredOptions = dhcp.GetOptions();
            DhcpByteOption messageType
                = offeredOptions[DhcpMessageType.OptionCode] as DhcpByteOption;

            if (messageType == null)
            {
                return;
            }

            switch (messageType.Value)
            {
            case (byte)DhcpFormat.MessageType.Ack:
                break;

            case (byte)DhcpFormat.MessageType.Nak:
                client.ChangeState(new DhcpClientStateInitialize(client));
                return;

            default:
                return;
            }

            // Must have parameters
            byte [] parameters = new byte [] {
                DhcpSubnetMask.OptionCode,
                DhcpRouter.OptionCode,
                // DhcpDomainNameServer.OptionCode
            };

            foreach (byte p in parameters)
            {
                IDhcpOption ido = offeredOptions[p] as IDhcpOption;
                if (ido == null)
                {
                    return;
                }
            }

            client.CancelStateTimeout();
            client.ChangeState(new DhcpClientStateBound(client,
                                                        serverAddress,
                                                        offeredAddress,
                                                        offeredOptions));
        }