//Process the client request. Find appropriate configuration based on any //criteria in the request message that can be matched against the server's //configuration, then formulate a response message containing the options //to be sent to the client. //@return a Reply DhcpMessage public DhcpV4Message ProcessMessage(IPAddress localAddress) { Monitor.Enter(_lock); try { //設定ServerId為管理IP _dhcpV4ServerIdOption.SetIpAddress(localAddress.ToString()); if (!PreProcess()) { return(null); } // build a reply message using the local and remote sockets from the request _replyMsg = new DhcpV4Message(_requestMsg.GetLocalAddress(), _requestMsg.GetRemoteAddress()); _replyMsg.SetOp(DhcpConstants.V4_OP_REPLY); // copy fields from request to reply _replyMsg.SetHtype(_requestMsg.GetHtype()); _replyMsg.SetHlen(_requestMsg.GetHlen()); _replyMsg.SetTransactionId(_requestMsg.GetTransactionId()); _replyMsg.SetFlags(_requestMsg.GetFlags()); _replyMsg.SetGiAddr(_requestMsg.GetGiAddr()); _replyMsg.SetChAddr(_requestMsg.GetChAddr()); // MUST put Server Identifier in REPLY message _replyMsg.PutDhcpOption(_dhcpV4ServerIdOption); if (!Process()) { log.Warn("Message dropped by processor"); return(null); } } catch (Exception ex) { log.WarnFormat("BaseDhcpV4Processor ProcessMessage Faile. exMessage:{0} exStackTrace:{1}", ex.Message, ex.StackTrace); return(null); } finally { if (!PostProcess()) { log.Warn("Message dropped by postProcess"); _replyMsg = null; } Monitor.Exit(_lock); } return(_replyMsg); }
/** * Builds the discover message. * * @return the dhcp message */ private DhcpV4Message BuildDiscoverMessage(byte[] chAddr) { DhcpV4Message msg = new DhcpV4Message(IPAddress.Parse("0.0.0.0"), new IPEndPoint(serverAddr, serverPort)); msg.SetOp((short)DhcpConstants.V4_OP_REQUEST); msg.SetTransactionId(GetRandomLong()); msg.SetHtype((short)1); // ethernet msg.SetHlen((byte)6); msg.SetChAddr(chAddr); msg.SetGiAddr(clientAddr); // look like a relay to the DHCP server DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption(); msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_DISCOVER); msg.PutDhcpOption(msgTypeOption); return(msg); }
private DhcpV4Message BuildReleaseMessage(DhcpV4Message ack) { DhcpV4Message msg = new DhcpV4Message(null, new IPEndPoint(serverAddr, serverPort)); msg.SetOp((short)DhcpConstants.V4_OP_REQUEST); msg.SetTransactionId(ack.GetTransactionId()); msg.SetHtype((short)1); // ethernet msg.SetHlen((byte)6); msg.SetChAddr(ack.GetChAddr()); msg.SetGiAddr(clientAddr); // look like a relay to the DHCP server msg.SetCiAddr(ack.GetYiAddr()); DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption(); msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_RELEASE); msg.PutDhcpOption(msgTypeOption); return(msg); }
private static void V4Discover() { var V4Message = new DhcpV4Message(IPAddress.Any, new IPEndPoint(IPAddress.Parse("192.168.61.48"), DhcpConstants.V4_SERVER_PORT)); V4Message.SetOp((short)DhcpConstants.V4_OP_REQUEST); //V4_OP_REQUEST V4Message.SetGiAddr(IPAddress.Any); V4Message.SetChAddr(PhysicalAddress.Parse("9C-EB-E8-28-92-D4").GetAddressBytes()); V4Message.SetHlen(6); V4Message.SetHtype((byte)6); V4Message.SetTransactionId(-1729018559); DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption(); msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_DISCOVER); V4Message.PutDhcpOption(msgTypeOption); var message = DhcpV4MessageHandler.HandleMessage( IPAddress.Parse("192.168.61.48"), V4Message); Console.WriteLine(message.ToString()); }
private DhcpV4Message BuildRequestMessage(DhcpV4Message offer) { DhcpV4Message msg = new DhcpV4Message(IPAddress.Parse("0.0.0.0"), new IPEndPoint(serverAddr, serverPort)); msg.SetOp((short)DhcpConstants.V4_OP_REQUEST); msg.SetTransactionId(offer.GetTransactionId()); msg.SetHtype((short)1); // ethernet msg.SetHlen((byte)6); msg.SetChAddr(offer.GetChAddr()); msg.SetGiAddr(clientAddr); // look like a relay to the DHCP server DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption(); msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_REQUEST); msg.PutDhcpOption(msgTypeOption); DhcpV4RequestedIpAddressOption reqIpOption = new DhcpV4RequestedIpAddressOption(); reqIpOption.SetIpAddress(offer.GetYiAddr().ToString()); msg.PutDhcpOption(reqIpOption); return(msg); }