Esempio n. 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));
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        private void WorkerMain()
        {
            DebugPrint("Worker starting\n");

            //TODO: Random r = new Random();
            //TODO: transactionID = (uint)r.Next();
            transactionID = (uint)INucleusCalls.Rdtsc();
            DateTime startTime = DateTime.Now;

            // Enter "Init" state of FSM
            ChangeState(new DhcpClientStateInitialize(this));

            while (workerDone == false)
            {
                // Check for timeouts
                DateTime now = DateTime.Now;
                if (now >= renewalTimeout)
                {
                    CancelRenewalTimeout();
                    @state.RenewalTimeoutEvent();
                }
                if (now >= rebindTimeout)
                {
                    CancelRebindTimeout();
                    @state.RebindTimeoutEvent();
                }
                if (now >= stateTimeout)
                {
                    CancelStateTimeout();
                    @state.StateTimeoutEvent();
                }

                // Poll for data
                try {
                    Bytes data = udp.PollReadData(PollInterval);
                    if (data != null)
                    {
                        DhcpFormat dhcp = DhcpFormat.Parse(data);
                        //delete data;
                        // Check transaction id is ours
                        if (dhcp.TransactionID != transactionID)
                        {
                            continue;
                        }

                        // Check client address is ours
                        if (dhcp.GetHardwareAddress() != MacAddress)
                        {
                            continue;
                        }

                        @state.ReceiveEvent(dhcp);
                    }
                }
                catch (InvalidDhcpFormatException idfe) {
                    DebugPrint(idfe.Message);
                }

                // XXX Temporary until process can run in background
                // from shell.  ie we'd like to run and renew lease
                // but shell blocks on running process and cleans up
                // after it for the time being.
                if (activeDhcpOptions != null)
                {
                    DebugPrint("Got options -- done\n");
                    break;
                }

                if (DateTime.Now - startTime > TimeSpan.FromSeconds(5))
                {
                    DebugPrint("Timed out\n");
                    break;
                }
            }
        }