private static void V6Release() { DhcpV6Message msg = new DhcpV6Message(IPAddress.Any, new IPEndPoint(serverAddr, serverPort)); msg.SetTransactionId(-1729018559); DhcpV6ClientIdOption dhcpClientId = new DhcpV6ClientIdOption(); dhcpClientId.GetOpaqueData().SetAscii("000100011dd31327001dbac1557b"); msg.PutDhcpOption(dhcpClientId); DhcpV6ServerIdOption dhcpServerId = new DhcpV6ServerIdOption(); dhcpServerId.GetOpaqueData().SetAscii("000100011dd31327001dbac1557b"); msg.PutDhcpOption(dhcpServerId); msg.SetMessageType(DhcpConstants.V6MESSAGE_TYPE_RELEASE); msg.SetIaNaOptions(new List <DhcpV6IaNaOption>() { new DhcpV6IaNaOption(new v6IaNaOption() { iaId = 285220282, t1 = 0, t2 = 0 }) }); var message = DhcpV6MessageHandler.HandleMessage( serverAddr, msg); Console.WriteLine(message.ToString()); }
public override void Decode(ByteBuffer buf) { int len = base.DecodeLength(buf); // since the linkAddr of the relay message is the interface // on which the relay itself received the message to be forwarded // we can assume that address is logically a server port IPEndPoint relayMsgLocalAddr = new IPEndPoint(relayMessage.GetLinkAddress(), DhcpConstants.V6_SERVER_PORT); // the peerAddr of the relay message is the source address of // the message which it received and is forwarding, thus the // peerAddress is either another relay or the client itself, // so we don't really know what port to logically set for the // inner message's remote port IPEndPoint relayMsgRemoteAddr = new IPEndPoint(relayMessage.GetPeerAddress(), 0); // create a new buffer that will hold just this RelayOption byte[] b = buf.getBytes(len); ByteBuffer _buf = ByteBuffer.allocate(len); _buf.put(b); // use the wrapped buffer which represents the contents of the message // contained within this relay option, but not any more, i.e. not beyond // what _this_ relay option reports its length to be dhcpMessage = DhcpV6Message.Decode(_buf, relayMsgLocalAddr.Address, relayMsgRemoteAddr); }
/// <summary> /// 是否開啟快速提交模式 /// </summary> /// <param name="requestMsg"></param> /// <param name="clientLink"></param> /// <returns></returns> private bool IsRapidCommit(DhcpV6Message requestMsg, link clientLink) { if ((requestMsg.HasOption(DhcpConstants.V6OPTION_RAPID_COMMIT) && DhcpServerPolicies.EffectivePolicyAsBoolean(requestMsg, clientLink, Property.SUPPORT_RAPID_COMMIT))) { return(true); } return(false); }
public override bool assertDhcpV6Message(DhcpV6Message message) { if (message != null && message.GetMessageType() == DhcpConstants.V6MESSAGE_TYPE_REPLY && message.GetTransactionId() == _expectedTransactionId) { Console.WriteLine("Got Type({0}) transactionID:{1:X} Passed, COUNT({2})", message.GetMessageType(), message.GetTransactionId(), (++count)); return(true); } return(false); }
public override DhcpV6Message AddOtherOptions(DhcpV6Message dhcpV6MessagePacket) { dhcpV6MessagePacket.SetMessageType(DhcpConstants.V6MESSAGE_TYPE_SOLICIT); dhcpV6MessagePacket.SetIaNaOptions(new List <DhcpV6IaNaOption>() { new DhcpV6IaNaOption(new v6IaNaOption() { iaId = 285220282, t1 = 0, t2 = 0 }) }); return(dhcpV6MessagePacket); }
public static DhcpV6Message HandleClientRequest(IPAddress linkAddress, DhcpV6Message dhcpMessage, IPAddress clientV4IP) { BaseDhcpV6Processor processor = null; switch (dhcpMessage.GetMessageType()) { case DhcpConstants.V6MESSAGE_TYPE_SOLICIT: processor = new DhcpV6SolicitProcessor(dhcpMessage, linkAddress, clientV4IP); break; case DhcpConstants.V6MESSAGE_TYPE_REQUEST: processor = new DhcpV6RequestProcessor(dhcpMessage, linkAddress, clientV4IP); break; case DhcpConstants.V6MESSAGE_TYPE_RENEW: processor = new DhcpV6RenewProcessor(dhcpMessage, linkAddress, clientV4IP); break; case DhcpConstants.V6MESSAGE_TYPE_RELEASE: processor = new DhcpV6ReleaseProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V6MESSAGE_TYPE_INFO_REQUEST: processor = new DhcpV6InfoRequestProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V6MESSAGE_TYPE_REBIND: processor = new DhcpV6RebindProcessor(dhcpMessage, linkAddress, clientV4IP); break; case DhcpConstants.V6MESSAGE_TYPE_DECLINE: processor = new DhcpV6DeclineProcessor(dhcpMessage, linkAddress); break; case DhcpConstants.V6MESSAGE_TYPE_CONFIRM: processor = new DhcpV6ConfirmProcessor(dhcpMessage, linkAddress); break; default: _log.Error("Unknown message type."); break; } if (processor != null) { return(processor.ProcessMessage()); } else { _log.Error(("No processor found for message type: " + dhcpMessage.GetMessageType())); } return(null); }
public override DhcpV6Message AddOtherOptions(DhcpV6Message dhcpV6MessagePacket) { DhcpV6Message replyDhcpV6Message = new DhcpV6Message(null, null); byte[] replyData = _replyPacke.GetData(); replyDhcpV6Message.Decode(ByteBuffer.allocate(replyData.Length).put(replyData).flip()); dhcpV6MessagePacket.SetMessageType(DhcpConstants.V6MESSAGE_TYPE_REQUEST); dhcpV6MessagePacket.PutDhcpOption(replyDhcpV6Message.GetDhcpServerIdOption()); dhcpV6MessagePacket.SetIaNaOptions(replyDhcpV6Message.GetIaNaOptions()); return(dhcpV6MessagePacket); }
public DhcpV6Message ProcessMessage() { try { if (!PreProcess()) { log.Warn("Message dropped by preProcess"); return(null); } if (log.IsDebugEnabled) { log.Debug(("Processing: " + this.requestMsg.ToStringWithOptions())); } else if (log.IsInfoEnabled) { log.Info(("Processing: " + this.requestMsg.ToString())); } // build a reply message using the local and remote sockets from the request this.replyMsg = new DhcpV6Message(this.requestMsg.GetLocalAddress(), this.requestMsg.GetRemoteAddress()); // copy the transaction ID into the reply replyMsg.SetTransactionId(this.requestMsg.GetTransactionId()); // MUST put Server Identifier DUID in ADVERTISE or REPLY message replyMsg.PutDhcpOption(dhcpServerIdOption); // copy Client Identifier DUID if given in client request message replyMsg.PutDhcpOption(this.requestMsg.GetDhcpClientIdOption()); if (!Process()) { log.Warn("Message dropped by processor"); return(null); } if (log.IsDebugEnabled) { log.DebugFormat("Returning: {0}", this.replyMsg.ToStringWithOptions()); } else if (log.IsInfoEnabled) { log.InfoFormat("Returning: {0}", this.replyMsg.ToString()); } } finally { if (!this.PostProcess()) { log.Warn("Message dropped by postProcess"); this.replyMsg = null; } } return(this.replyMsg); }
public DHCPv6SendMessage(int transactionId) { DhcpV6MessagePacket = new DhcpV6Message(IPAddress.Any, new IPEndPoint(IPAddress.Parse("ff02::1:2"), DEST_PORT)); DhcpV6MessagePacket.SetTransactionId(transactionId); DhcpV6ClientIdOption dhcpClientId = new DhcpV6ClientIdOption(); dhcpClientId.GetOpaqueData().SetHex(new byte[] { 0x00, 0x01, 0x00, 0x01, 0x20, 0x39, 0xBB, 0x3F, 0x98, 0x5F, 0xD3, 0x58, 0x7D, ++duid }); //.SetAscii("000100011dd31327001dbac1557b"); DhcpV6MessagePacket.PutDhcpOption(dhcpClientId); DhcpV6ElapsedTimeOption dhcpElapsedTime = new DhcpV6ElapsedTimeOption(); dhcpElapsedTime.SetUnsignedShort(1); DhcpV6MessagePacket.PutDhcpOption(dhcpElapsedTime); }
protected DhcpV6Message parseToDhcpV6Message() { DhcpV6Message msg = new DhcpV6Message(null, null); try { msg.Decode(ByteBuffer.allocate(_data.Length).put(_data).flip()); } catch (IOException e) { Console.WriteLine(e); msg = null; } return(msg); }
// NOTE: this is the magic method where the nio and net implementations come together public static DhcpV6Message HandleMessage(IPAddress localAddress, DhcpV6Message dhcpMessage, IPAddress v4IPAddress = null) { DhcpV6Message replyMessage = null; if (dhcpMessage is DhcpV6RelayMessage) { //if (dhcpMessage.GetMessageType() == DhcpConstants.V6MESSAGE_TYPE_RELAY_FORW) //{ // DhcpV6RelayMessage relayMessage = (DhcpV6RelayMessage)(dhcpMessage); // replyMessage = DhcpV6MessageHandler.HandleRelayForward(relayMessage); //} //else //{ // _log.ErrorFormat("Unsupported message type: {0}", dhcpMessage.GetMessageType()); //} } else { _log.InfoFormat("Handling client request on local client link address: {0}", localAddress.ToString()); replyMessage = DhcpV6MessageHandler.HandleClientRequest(localAddress, dhcpMessage, v4IPAddress); } return(replyMessage); }
public DhcpV6RequestProcessor(DhcpV6Message dhcpMessage, IPAddress linkAddress, IPAddress clientV4IP) : base(dhcpMessage, linkAddress) { _clientV4IP = clientV4IP; }
abstract public bool assertDhcpV6Message(DhcpV6Message message);
abstract public DhcpV6Message AddOtherOptions(DhcpV6Message dhcpV6MessagePacket);
public DhcpV6RebindProcessor(DhcpV6Message requestMsg, IPAddress clientLinkAddress, IPAddress clientV4IP) : base(requestMsg, clientLinkAddress) { this._clientV4IP = clientV4IP; }
public static bool EffectivePolicyAsBoolean(DhcpV6Message requestMsg, link link, Property prop) { return(bool.Parse(EffectivePolicy(requestMsg, link, prop))); }
public DhcpV6DeclineProcessor(DhcpV6Message requestMsg, IPAddress clientLinkAddress) : base(requestMsg, clientLinkAddress) { }
private static DhcpV6RelayMessage HandleRelayForward(DhcpV6RelayMessage relayMessage) { IPAddress linkAddr = relayMessage.GetLinkAddress(); _log.Info(("Handling relay forward on link address: " + linkAddr.ToString())); DhcpV6RelayOption relayOption = relayMessage.GetRelayOption(); if ((relayOption != null)) { DhcpV6Message relayOptionMessage = relayOption.GetDhcpMessage(); while ((relayOptionMessage != null)) { // check what kind of message is in the option if ((relayOptionMessage is DhcpV6RelayMessage)) { // encapsulated message is another relay message DhcpV6RelayMessage anotherRelayMessage = ((DhcpV6RelayMessage)(relayOptionMessage)); // flip this inner relay_forward into a relay_reply, // because we reuse the relay message "stack" for the reply anotherRelayMessage.SetMessageType(DhcpConstants.V6MESSAGE_TYPE_RELAY_REPL); // reset the client link reference linkAddr = anotherRelayMessage.GetLinkAddress(); // reset the current relay option reference to the // encapsulated relay message's relay option relayOption = anotherRelayMessage.GetRelayOption(); // reset the relayOptionMessage reference to recurse relayOptionMessage = relayOption.GetDhcpMessage(); } else { // we've peeled off all the layers of the relay message(s), // so now go handle the client request _log.Info(("Handling client request on remote client link address: " + linkAddr.ToString())); DhcpV6Message replyMessage = DhcpV6MessageHandler.HandleClientRequest(linkAddr, relayOptionMessage, null); if ((replyMessage != null)) { // replace the original client request message inside // the relayed message with the generated Reply message relayOption.SetDhcpMessage(replyMessage); // flip the outer-most relay_foward into a relay_reply relayMessage.SetMessageType(DhcpConstants.V6MESSAGE_TYPE_RELAY_REPL); // return the relay message we started with, // with each relay "layer" flipped from a relay_forward // to a relay_reply, and the lowest level relayOption // will contain our Reply for the client request return(relayMessage); } relayOptionMessage = null; // done with relayed messages } } } else { _log.Error("Relay message does not contain a relay option"); } // if we get here, no reply was generated return(null); }
//protected static Set<DhcpV6Message> recentMsgs = Collections.synchronizedSet(new HashSet<DhcpV6Message>()); //protected static Timer recentMsgPruner = new Timer("RecentMsgPruner"); protected BaseDhcpV6Processor(DhcpV6Message requestMsg, IPAddress clientLinkAddress) { this.requestMsg = requestMsg; this.clientLinkAddress = clientLinkAddress; }
/** * Sets the dhcp message. * * @param relayMessage the dhcp message */ public void SetDhcpMessage(DhcpV6Message dhcpMessage) { this.dhcpMessage = dhcpMessage; }
public DhcpV6SolicitProcessor(DhcpV6Message requestMsg, IPAddress clientLinkAddress, IPAddress clientV4IP) : base(requestMsg, clientLinkAddress) { _clientV4IP = clientV4IP; }
protected Dictionary <int, DhcpOption> RequestedOptions(Dictionary <int, DhcpOption> optionMap, DhcpV6Message requestMsg) { if (((optionMap != null) && optionMap.Count() > 0)) { List <int> requestedCodes = this.requestMsg.GetRequestedOptionCodes(); if (((requestedCodes != null) && requestedCodes.Count() > 0)) { Dictionary <int, DhcpOption> _optionMap = new Dictionary <int, DhcpOption>(); foreach (var option in optionMap) { if (requestedCodes.Contains(option.Key)) { _optionMap.Add(option.Key, option.Value); } } optionMap = _optionMap; } } return(optionMap); }