private async Task DhcpRequest(DhcpMessage message) { await _requestLock.WaitAsync().ConfigureAwait(false); try { // Client specified an address they would like if (message.Options.ContainsKey(DhcpOptionCode.RequestedIpAddress)) { await KeepAddressRequest(message).ConfigureAwait(false); } else { var clientAddress = message.ClientIPAddress; if (clientAddress.Equals(IPAddress.Parse("0.0.0.0"))) { // A DHCP REQ should have an address throw new Exception("A DHCP Request must have an address specified"); } await LeaseManager.AddLease(clientAddress, message.ClientHardwareAddress, message.HostName) .ConfigureAwait(false); await this.SendAck(message, clientAddress).ConfigureAwait(false); } } catch (Exception ex) { Log.Error(ex.Message); } _requestLock.Release(); }
private async Task KeepAddressRequest(DhcpMessage message) { var addressRequestData = message.Options[DhcpOptionCode.RequestedIpAddress]; var addressRequest = new IPAddress(addressRequestData); Log.Debug($"[REQUEST] {message.ClientHardwareAddress} has requested to keep it's IP Address '{addressRequest}'"); if (addressRequest.IsInSameSubnet(Configuration.StartIpAddress, Configuration.SubnetMask) == false) { Log.Debug($"[REQUEST] {message.ClientHardwareAddress} request for '{addressRequest}' has been DENIED due to subnet mismatch"); await this.SendNak(message, addressRequest).ConfigureAwait(false); return; } var keepReservationResponse = await LeaseManager.KeepLeaseRequest(addressRequest, message.ClientHardwareAddress, message.HostName); if (keepReservationResponse) { await this.SendAck(message, addressRequest).ConfigureAwait(false); Log.Debug($"[REQUEST] {message.ClientHardwareAddress} has been approved!"); return; } await this.SendNak(message, addressRequest).ConfigureAwait(false); Log.Debug($"[REQUEST] {message.ClientHardwareAddress} has been DENIED."); }
private async Task DhcpDiscover(DhcpMessage message) { var addressRequest = await LeaseManager.GetNextLease() .ConfigureAwait(false); await this.SendOffer(message, addressRequest) .ConfigureAwait(false); }