internal override void ReceiveEvent(DhcpFormat !dhcp) { // 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 offer SortedList offeredOptions = dhcp.GetOptions(); DhcpByteOption messageType = offeredOptions[DhcpMessageType.OptionCode] as DhcpByteOption; if (messageType == null || messageType.Value != (byte)DhcpFormat.MessageType.Offer) { 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 DhcpClientStateRequesting(client, serverAddress, offeredAddress, offeredOptions)); }
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); } }