Ejemplo n.º 1
0
        private void Respond(DhcpFormat request, DhcpFormat.MessageType m)
        {
            DhcpFormat response = new DhcpFormat(m);

            response.TransactionID = request.TransactionID;

            EthernetAddress hwAddress = request.GetHardwareAddress();

            response.SetHardwareAddress(hwAddress);

            switch (m)
            {
            case DhcpFormat.MessageType.Offer:
                response.NextServerIPAddress = ServerAddress;
                response.AddOption(
                    DhcpIPAddressLeaseTime.Create(RebindingTime)
                    );
                goto case DhcpFormat.MessageType.Ack;

            case DhcpFormat.MessageType.Ack:
                response.YourIPAddress = HostAddress;
                assignedAddress        = HostAddress;
                FillOptions(request, response);
                break;

            case DhcpFormat.MessageType.Nak:
                // Nothing to do
                break;

            default:
                return;
            }
            SendResponsePacket(response);
        }
Ejemplo n.º 2
0
        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);
            }
        }