From f5b0439fc081843b67dfa7cbcf964506d547cd58 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 12 Sep 2020 13:06:45 +0530 Subject: [PATCH] DhcpServer: added error handling to fix issue when exception is thrown causing the client to not receive IP address. --- DnsServerCore/Dhcp/DhcpServer.cs | 35 ++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/DnsServerCore/Dhcp/DhcpServer.cs b/DnsServerCore/Dhcp/DhcpServer.cs index b6a3263b..08cb31b1 100644 --- a/DnsServerCore/Dhcp/DhcpServer.cs +++ b/DnsServerCore/Dhcp/DhcpServer.cs @@ -647,23 +647,32 @@ namespace DnsServerCore.Dhcp if (!DnsClient.IsDomainNameValid(domain)) return; - if (add) + try { - //update forward zone - _authZoneManager.CreatePrimaryZone(scope.DomainName, _authZoneManager.ServerDomain, false); - _authZoneManager.SetRecords(domain, DnsResourceRecordType.A, scope.DnsTtl, new DnsResourceRecordData[] { new DnsARecord(address) }); + if (add) + { + //update forward zone + _authZoneManager.CreatePrimaryZone(scope.DomainName, _authZoneManager.ServerDomain, false); + _authZoneManager.SetRecords(domain, DnsResourceRecordType.A, scope.DnsTtl, new DnsResourceRecordData[] { new DnsARecord(address) }); - //update reverse zone - _authZoneManager.CreatePrimaryZone(Zone.GetReverseZone(address, scope.SubnetMask), _authZoneManager.ServerDomain, false); - _authZoneManager.SetRecords(Zone.GetReverseZone(address, 32), DnsResourceRecordType.PTR, scope.DnsTtl, new DnsResourceRecordData[] { new DnsPTRRecord(domain) }); + //update reverse zone + _authZoneManager.CreatePrimaryZone(Zone.GetReverseZone(address, scope.SubnetMask), _authZoneManager.ServerDomain, false); + _authZoneManager.SetRecords(Zone.GetReverseZone(address, 32), DnsResourceRecordType.PTR, scope.DnsTtl, new DnsResourceRecordData[] { new DnsPTRRecord(domain) }); + } + else + { + //remove from forward zone + _authZoneManager.DeleteRecords(domain, DnsResourceRecordType.A); + + //remove from reverse zone + _authZoneManager.DeleteRecords(Zone.GetReverseZone(address, 32), DnsResourceRecordType.PTR); + } } - else + catch (Exception ex) { - //remove from forward zone - _authZoneManager.DeleteRecords(domain, DnsResourceRecordType.A); - - //remove from reverse zone - _authZoneManager.DeleteRecords(Zone.GetReverseZone(address, 32), DnsResourceRecordType.PTR); + LogManager log = _log; + if (log != null) + log.Write(ex); } }