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); } }
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); } }
public void Start() { Trace.TraceInformation("Dhcp Server Starting..."); this.m_ActiveLeases.Clear(); this.m_InactiveLeases.Clear(); for (InternetAddress address = this.m_StartAddress.Copy(); address.CompareTo(this.m_EndAddress) <= 0; address = address.NextAddress()) { if ((!address.Equals(this.StartAddress)) && !this.m_Reservations.ContainsValue(address)) { this.m_InactiveLeases.Add(address, new AddressLease(null, address, DateTime.MinValue)); } } if (this.m_DhcpInterfaceAddress == null) { if (this.m_DhcpInterface == null) { Trace.TraceInformation("Enumerating Network Interfaces."); foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback) { this.m_DhcpInterface = nic; } else if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && nic.OperationalStatus == OperationalStatus.Up) { Trace.TraceInformation("Using Network Interface \"{0}\".", nic.Name); this.m_DhcpInterface = nic; break; } } #if TRACE if (this.m_DhcpInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback) { Trace.TraceInformation("Active Ethernet Network Interface Not Found. Using Loopback."); } #endif } foreach (UnicastIPAddressInformation interfaceAddress in this.m_DhcpInterface.GetIPProperties().UnicastAddresses) { if (interfaceAddress.Address.AddressFamily == AddressFamily.InterNetwork) { this.m_DhcpInterfaceAddress = interfaceAddress.Address; } } } if (this.m_DhcpInterfaceAddress == null) { Trace.TraceError("Unabled to Set Dhcp Interface Address. Check the networkInterface property of your config file."); throw new InvalidOperationException("Unabled to Set Dhcp Interface Address."); } this.m_Abort = false; this.m_CleanupTimer = new Timer(new TimerCallback(this.CleanUp), null, 60000, 30000); this.m_DhcpSocket = configureSocket(); this.Listen(); Trace.TraceInformation("Dhcp Service Started."); }