public void Release(DhcpV4Message ackMsg) { msg = BuildReleaseMessage(ackMsg); //ChannelFuture future = channel.write(msg, server); //future.addListener(this); Console.WriteLine(msg.ToString()); }
private static void V4Request() { 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()); DhcpV4MsgTypeOption msgTypeOption = new DhcpV4MsgTypeOption(); msgTypeOption.SetUnsignedByte((short)DhcpConstants.V4MESSAGE_TYPE_REQUEST); V4Message.PutDhcpOption(msgTypeOption); V4Message.PutDhcpOption(new DhcpV4RequestedIpAddressOption( new v4RequestedIpAddressOption() { code = DhcpConstants.V4OPTION_REQUESTED_IP, ipAddress = "192.168.61.140" })); V4Message.SetDhcpV4ServerIdOption( new DhcpV4ServerIdOption( new v4ServerIdOption() { code = DhcpConstants.V4OPTION_SERVERID, ipAddress = "192.168.61.48" })); var message = DhcpV4MessageHandler.HandleMessage( IPAddress.Parse("192.168.61.48"), V4Message); Console.WriteLine(message.ToString()); }
public void Discover() { msg = BuildDiscoverMessage(mac); //ChannelFuture future = channel.write(msg, server); //future.addListener(this); Console.WriteLine(msg.ToString()); Request(msg); }
private bool IsRapidCommit(DhcpV4Message requestMsg, link clientLink) { if (_requestMsg.HasOption(DhcpConstants.V4OPTION_RAPID_COMMIT) && DhcpServerPolicies.EffectivePolicyAsBoolean(requestMsg, clientLink, Property.SUPPORT_RAPID_COMMIT)) { return(true); } return(false); }
//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()); }
/** * Effective policy. * * @param requestMsg the request msg * @param link the link * @param prop the prop * @return the string */ public static string EffectivePolicy(DhcpV4Message requestMsg, link link, Property prop) { string policy = null; if ((requestMsg != null) && (link != null)) { List <linkFilter> linkFilters = link.linkFilters; if (linkFilters != null) { foreach (linkFilter linkFilter in linkFilters) { if (DhcpServerConfiguration.MsgMatchesFilter(requestMsg, linkFilter)) { policy = GetPolicy(linkFilter.policies, prop.Key()); } } // if the client request matches at least one filter on the link, // and that filter has configured a value for the policy, then return // that value from the last filter that the client matches if (policy != null) { return(policy); } } } if (link != null) { // client does not match a link filter // get the value of the policy on the link, if any policy = GetPolicy(link.policies, prop.Key()); if (policy != null) { return(policy); } } return(GlobalPolicy(prop)); }
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); }
//Construct an DhcpV4InformRequest processor. //@param requestMsg the Inform message //@param clientLinkAddress the client link address public DhcpV4InformProcessor(DhcpV4Message requestMsg, IPAddress clientLinkAddress) : base(requestMsg, clientLinkAddress) { }
private Dictionary <int, DhcpOption> RequestedOptions(Dictionary <int, DhcpOption> optionDictionary, DhcpV4Message requestMsg) { if (optionDictionary != null && optionDictionary.Count > 0) { List <int> requestedCodes = requestMsg.GetRequestedOptionCodes(); if (requestedCodes != null && requestedCodes.Count > 0) { Dictionary <int, DhcpOption> newOptionDictionary = new Dictionary <int, DhcpOption>(); foreach (var req in requestedCodes) { if (optionDictionary.ContainsKey(req)) { newOptionDictionary[req] = optionDictionary[req]; } } optionDictionary = newOptionDictionary; } } return(optionDictionary); }
protected BaseDhcpV4Processor(DhcpV4Message requestMsg, IPAddress clientLinkAddress) { _requestMsg = requestMsg; _clientLinkAddress = clientLinkAddress; }
public static DhcpV4Message HandleMessage(IPAddress localAddress, DhcpV4Message dhcpMessage) { DhcpV4Message replyMessage = null; if (dhcpMessage.GetOp() == DhcpConstants.V4_OP_REQUEST) { IPAddress linkAddress = null; if (dhcpMessage.GetGiAddr().Equals(DhcpConstants.ZEROADDR_V4)) { linkAddress = localAddress; //log.Info("Handling client request on local client link address: " + // linkAddress.ToString()); } else { linkAddress = dhcpMessage.GetGiAddr(); log.Info("Handling client request on remote client link address: " + linkAddress.ToString()); } DhcpV4MsgTypeOption msgTypeOption = (DhcpV4MsgTypeOption) dhcpMessage.GetDhcpOption(DhcpConstants.V4OPTION_MESSAGE_TYPE); if (msgTypeOption != null) { short msgType = msgTypeOption.GetUnsignedByte(); DhcpV4MessageProcessor processor = null; switch (msgType) { case DhcpConstants.V4MESSAGE_TYPE_DISCOVER: processor = new DhcpV4DiscoverProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V4MESSAGE_TYPE_REQUEST: processor = new DhcpV4RequestProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V4MESSAGE_TYPE_DECLINE: processor = new DhcpV4DeclineProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V4MESSAGE_TYPE_RELEASE: processor = new DhcpV4ReleaseProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V4MESSAGE_TYPE_INFORM: processor = new DhcpV4InformProcessor(dhcpMessage, linkAddress); break; default: log.Error("Unknown message type."); break; } if (processor != null) { return(processor.ProcessMessage(localAddress)); } else { log.Error("No processor found for message type: " + msgType); } } else { log.Error("No message type option found in request."); } return(null); } else { log.Error("Unsupported op code: " + dhcpMessage.GetOp()); } return(replyMessage); }
public DhcpV4DiscoverProcessor(DhcpV4Message requestMsg, IPAddress clientLinkAddress) : base(requestMsg, clientLinkAddress) { }