From fb29d181c33e7796847734ed50e4f30bd4f5b6df Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 27 Jul 2019 12:34:12 +0530 Subject: [PATCH] DhcpServer: updated no offer handling in ProcessDhcpMessage() for Discover request. Fixed issue with space in host name option. Removed using hostname from reserved lease since hostname field will be removed from UI. Added hostname check in UpdateDnsAuthZone() to fix DHCP server failing to activate scope bug if client has not hostname. Added error handling in ActivateScope() to avoid partial binding of scope causing inconsistent state. --- DnsServerCore/Dhcp/DhcpServer.cs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/DnsServerCore/Dhcp/DhcpServer.cs b/DnsServerCore/Dhcp/DhcpServer.cs index 8dc71847..d7c1554e 100644 --- a/DnsServerCore/Dhcp/DhcpServer.cs +++ b/DnsServerCore/Dhcp/DhcpServer.cs @@ -305,7 +305,7 @@ namespace DnsServerCore.Dhcp Lease offer = scope.GetOffer(request); if (offer == null) - throw new DhcpServerException("DHCP Server failed to offer address: address unavailable due to address pool exhaustion."); + return null; //no offer available, do nothing List options = scope.GetOptions(request, scope.InterfaceAddress); if (options == null) @@ -426,9 +426,7 @@ namespace DnsServerCore.Dhcp if (clientDomainName == null) { if (request.HostName != null) - clientDomainName = request.HostName.HostName + "." + scope.DomainName; - else if ((leaseOffer.Type == LeaseType.Reserved) && !string.IsNullOrEmpty(leaseOffer.HostName) && !leaseOffer.HostName.EndsWith("." + scope.DomainName, StringComparison.OrdinalIgnoreCase)) - clientDomainName = leaseOffer.HostName + "." + scope.DomainName; //use hostname from reserved lease + clientDomainName = request.HostName.HostName.Replace(' ', '-') + "." + scope.DomainName; } if (clientDomainName != null) @@ -598,6 +596,9 @@ namespace DnsServerCore.Dhcp if (string.IsNullOrEmpty(scope.DomainName)) return; + if (string.IsNullOrEmpty(lease.HostName)) + return; + if (add) { //update forward zone @@ -720,7 +721,19 @@ namespace DnsServerCore.Dhcp lock (_activeScopeLock) { if (_activeScopeCount < 1) - BindUdpListener(_dhcpDefaultEP); + { + try + { + BindUdpListener(_dhcpDefaultEP); + } + catch + { + if (!interfaceAddress.Equals(IPAddress.Any)) + UnbindUdpListener(dhcpEP); + + throw; + } + } _activeScopeCount++; } @@ -770,7 +783,10 @@ namespace DnsServerCore.Dhcp _activeScopeCount--; if (_activeScopeCount < 1) + { + _activeScopeCount = 0; UnbindUdpListener(_dhcpDefaultEP); + } } if (_authoritativeZoneRoot != null)