private void SendAck(DhcpMessage message, AddressLease lease) { Trace.TraceInformation("{0} Sending Dhcp Acknowledge.", Thread.CurrentThread.ManagedThreadId); DhcpMessage response = new DhcpMessage(); response.Operation = DhcpOperation.BootReply; response.Hardware = HardwareType.Ethernet; response.HardwareAddressLength = 6; response.SecondsElapsed = message.SecondsElapsed; response.SessionId = message.SessionId; response.AssignedAddress = lease.Address.ToArray(); response.ClientHardwareAddress = message.ClientHardwareAddress; response.AddOption(DhcpOption.DhcpMessageType, (Byte)DhcpMessageType.Ack); response.AddOption(DhcpOption.AddressRequest, lease.Address.ToArray()); AddDhcpOptions(response); this.SendReply(response); if (OnNewClient != null) { OnNewClient(this, new OnNewDHCPClientEvent(lease.Owner.ToString(), lease.Address)); } Trace.TraceInformation("{0} Dhcp Acknowledge Sent:{1}", Thread.CurrentThread.ManagedThreadId, lease.Address.ToString()); }
private void SendOffer(DhcpMessage message, AddressLease offer) { Trace.TraceInformation("{0} Sending Dhcp Offer.", Thread.CurrentThread.ManagedThreadId); DhcpMessage response = new DhcpMessage(); response.Operation = DhcpOperation.BootReply; response.Hardware = HardwareType.Ethernet; response.HardwareAddressLength = 6; response.SecondsElapsed = message.SecondsElapsed; response.SessionId = message.SessionId; response.Flags = message.Flags; response.AssignedAddress = offer.Address.ToArray(); response.ClientHardwareAddress = message.ClientHardwareAddress; response.AddOption(DhcpOption.DhcpMessageType, (Byte)DhcpMessageType.Offer); response.AddOption(DhcpOption.AddressRequest, offer.Address.ToArray()); AddDhcpOptions(response); Byte[] paramList = message.GetOptionData(DhcpOption.ParameterList); if (paramList != null) { response.OptionOrdering = paramList; } this.SendReply(response); Trace.TraceInformation("{0} Dhcp Offer Sent.", Thread.CurrentThread.ManagedThreadId); }
void removeReservation(PhysicalAddress pf, InternetAddress ip) { m_Reservations.Remove(pf); if (!ip.IsEmpty) { AddressLease offer = this.m_InactiveLeases.Values[0]; offer.Address = ip; this.m_ActiveLeases.Add(offer.Address, offer); } }
private void SendAck(DhcpMessage message, AddressLease lease) { Trace.TraceInformation("{0} Sending Dhcp Acknowledge.", Thread.CurrentThread.ManagedThreadId); DhcpMessage response = new DhcpMessage(); response.Operation = DhcpOperation.BootReply; response.Hardware = HardwareType.Ethernet; response.HardwareAddressLength = 6; response.SecondsElapsed = message.SecondsElapsed; response.SessionId = message.SessionId; response.AssignedAddress = lease.Address.ToArray(); response.ClientHardwareAddress = message.ClientHardwareAddress; response.AddOption(DhcpOption.DhcpMessageType, (Byte)DhcpMessageType.Ack); response.AddOption(DhcpOption.AddressRequest, lease.Address.ToArray()); AddDhcpOptions(response); this.SendReply(response); if (OnNewClient != null) OnNewClient(this, new OnNewDHCPClientEvent(lease.Owner.ToString(), lease.Address)); Trace.TraceInformation("{0} Dhcp Acknowledge Sent:{1}", Thread.CurrentThread.ManagedThreadId, lease.Address.ToString()); }
private void DhcpRequest(DhcpMessage message) { Byte[] addressRequestData = message.GetOptionData(DhcpOption.AddressRequest); if (addressRequestData == null) { addressRequestData = message.ClientAddress; } InternetAddress addressRequest = new InternetAddress(addressRequestData); if (addressRequest.IsEmpty) { this.SendNak(message); return; } // Assume we're on an ethernet network Byte[] hardwareAddressData = new Byte[6]; Array.Copy(message.ClientHardwareAddress, hardwareAddressData, 6); PhysicalAddress clientHardwareAddress = new PhysicalAddress(hardwareAddressData); AddressLease assignment = null; Boolean ack = false; // If this client is explicitly allowed, or they are not denied and the allow any flag is set if (this.m_Acl.ContainsKey(clientHardwareAddress) && this.m_Acl[clientHardwareAddress] || !this.m_Acl.ContainsKey(clientHardwareAddress) && this.m_AllowAny) { if (this.m_Reservations.ContainsKey(clientHardwareAddress)) { assignment = new AddressLease(clientHardwareAddress, this.m_Reservations[clientHardwareAddress], DateTime.Now.Add(this.m_LeaseDuration)); if (addressRequest.Equals(assignment.Address)) { ack = true; } } else { lock (this.m_LeaseSync) { if (this.m_ActiveLeases.ContainsKey(addressRequest) && (this.m_ActiveLeases[addressRequest].Owner.Equals(clientHardwareAddress) || this.m_ActiveLeases[addressRequest].SessionId == message.SessionId)) { assignment = this.m_ActiveLeases[addressRequest]; assignment.Acknowledged = true; assignment.Owner = clientHardwareAddress; assignment.Expiration = DateTime.Now.Add(this.m_LeaseDuration); ack = true; } } } } if (ack) { this.SendAck(message, assignment); } else { this.SendNak(message); } }
private void DhcpDiscover(DhcpMessage message) { Byte[] addressRequestData = message.GetOptionData(DhcpOption.AddressRequest); if (addressRequestData == null) { addressRequestData = message.ClientAddress; } InternetAddress addressRequest = new InternetAddress(addressRequestData); // Assume we're on an ethernet network Byte[] hardwareAddressData = new Byte[6]; Array.Copy(message.ClientHardwareAddress, hardwareAddressData, 6); PhysicalAddress clientHardwareAddress = new PhysicalAddress(hardwareAddressData); AddressLease offer = null; // If this client is explicitly allowed, or they are not denied and the allow any flag is set if (this.m_Acl.ContainsKey(clientHardwareAddress) && this.m_Acl[clientHardwareAddress] || !this.m_Acl.ContainsKey(clientHardwareAddress) && this.m_AllowAny) { if (this.m_Reservations.ContainsKey(clientHardwareAddress)) { offer = new AddressLease(clientHardwareAddress, this.m_Reservations[clientHardwareAddress], DateTime.Now.Add(this.m_LeaseDuration)); } else { lock (this.m_LeaseSync) { if (!addressRequest.Equals(InternetAddress.Empty)) { if (this.m_InactiveLeases.ContainsKey(addressRequest)) { offer = this.m_InactiveLeases[addressRequest]; this.m_InactiveLeases.Remove(addressRequest); this.m_ActiveLeases.Add(addressRequest, offer); } else if (this.m_ActiveLeases.ContainsKey(addressRequest) && this.m_ActiveLeases[addressRequest].Owner.Equals(clientHardwareAddress)) { offer = this.m_ActiveLeases[addressRequest]; } } else if (this.m_InactiveLeases.Count > 0) { offer = this.m_InactiveLeases.Values[0]; this.m_InactiveLeases.Remove(offer.Address); this.m_ActiveLeases.Add(offer.Address, offer); } } } } if (offer == null) { this.SendNak(message); } else { offer.Acknowledged = false; offer.Expiration = DateTime.Now.Add(this.m_OfferTimeout); offer.SessionId = message.SessionId; offer.Owner = clientHardwareAddress; this.SendOffer(message, offer); } }