public void Receive(NetPacket packet)
        {
            packet.Reset();

            Console.WriteLine("Received {0} bytes", packet.Length);
            if (ValidHeaders(packet) == false)
            {
                SetState(ServerState.Failed);
                return;
            }

            try {
                DhcpFormat dhcp    = DhcpFormat.Parse(packet);
                SortedList options = dhcp.GetOptions();

                DhcpByteOption message = options[DhcpMessageType.OptionCode]
                                         as DhcpByteOption;
                if (message == null)
                {
                    Console.WriteLine("MessageType option not found");
                    SetState(ServerState.Failed);
                    return;
                }

                DhcpFormat.MessageType messageType =
                    (DhcpFormat.MessageType)message.Value;

                if (DhcpFormat.IsRequestMessage(messageType) == false)
                {
                    Console.WriteLine("Inappropriate message type: {0}",
                                      message.Value);
                    SetState(ServerState.Failed);
                    return;
                }

                DhcpFormat.MessageType expected = expectActions[expectIndex, 0];
                DhcpFormat.MessageType action   = expectActions[expectIndex, 1];
                expectIndex++;

                if (messageType != expected)
                {
                    Console.WriteLine("Unexpected message type: {0} != {1}",
                                      messageType, expected);
                    SetState(ServerState.Failed);
                    return;
                }

                if (DhcpFormat.IsResponseMessage(action))
                {
                    Respond(dhcp, action);
                }

                if (expectIndex == expectActions.Length / expectActions.Rank)
                {
                    SetState(ServerState.Finished);
                }
            }
            catch (Exception e) {
                Console.WriteLine("Bad Dhcp packet: {0}", e);
                SetState(ServerState.Failed);
            }
        }
示例#2
0
        private void WorkerMain()
        {
            DebugPrint("Worker starting\n");

            Random r = new Random();

            transactionID = (uint)r.Next();

            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 {
                    byte [] data = udpSession.PollCopyData(PollInterval);
                    if (data != null)
                    {
                        SimpleBuffer sb   = new SimpleBuffer(data);
                        DhcpFormat   dhcp = DhcpFormat.Parse(sb);

                        // 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;
                }
            }
        }