From 3da24cf3a2deb9a0cee295d36b5a213fa3bcd683 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 10 Jul 2021 13:23:43 +0530 Subject: [PATCH] PrimarySubDomainZone: updated code to use CommitAndIncrementSerial(). Implemented UpdateRecord(). --- .../Dns/Zones/PrimarySubDomainZone.cs | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/DnsServerCore/Dns/Zones/PrimarySubDomainZone.cs b/DnsServerCore/Dns/Zones/PrimarySubDomainZone.cs index f7ad5f1c..bd335c8c 100644 --- a/DnsServerCore/Dns/Zones/PrimarySubDomainZone.cs +++ b/DnsServerCore/Dns/Zones/PrimarySubDomainZone.cs @@ -17,6 +17,7 @@ along with this program. If not, see . */ +using System; using System.Collections.Generic; using TechnitiumLibrary.Net.Dns; @@ -33,7 +34,7 @@ namespace DnsServerCore.Dns.Zones #region constructor public PrimarySubDomainZone(PrimaryZone primaryZone, string name) - : base(name) + : base(primaryZone, name) { _primaryZone = primaryZone; } @@ -44,9 +45,10 @@ namespace DnsServerCore.Dns.Zones public override void SetRecords(DnsResourceRecordType type, IReadOnlyList records) { - base.SetRecords(type, records); + if (!SetRecords(type, records, out IReadOnlyList deletedRecords)) + throw new DnsServerException("Failed to set records. Please try again."); - _primaryZone.IncrementSoaSerial(); + _primaryZone.CommitAndIncrementSerial(deletedRecords, records); _primaryZone.TriggerNotify(); } @@ -54,15 +56,15 @@ namespace DnsServerCore.Dns.Zones { base.AddRecord(record); - _primaryZone.IncrementSoaSerial(); + _primaryZone.CommitAndIncrementSerial(null, new DnsResourceRecord[] { record }); _primaryZone.TriggerNotify(); } public override bool DeleteRecords(DnsResourceRecordType type) { - if (base.DeleteRecords(type)) + if (_entries.TryRemove(type, out IReadOnlyList removedRecords)) { - _primaryZone.IncrementSoaSerial(); + _primaryZone.CommitAndIncrementSerial(removedRecords); _primaryZone.TriggerNotify(); return true; @@ -71,11 +73,11 @@ namespace DnsServerCore.Dns.Zones return false; } - public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData record) + public override bool DeleteRecord(DnsResourceRecordType type, DnsResourceRecordData rdata) { - if (base.DeleteRecord(type, record)) + if (DeleteRecord(type, rdata, out DnsResourceRecord deletedRecord)) { - _primaryZone.IncrementSoaSerial(); + _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord }); _primaryZone.TriggerNotify(); return true; @@ -84,6 +86,21 @@ namespace DnsServerCore.Dns.Zones return false; } + public override void UpdateRecord(DnsResourceRecord oldRecord, DnsResourceRecord newRecord) + { + if (oldRecord.Type == DnsResourceRecordType.SOA) + throw new InvalidOperationException("Cannot update record: use SetRecords() for " + oldRecord.Type.ToString() + " record"); + + if (oldRecord.Type != newRecord.Type) + throw new InvalidOperationException("Old and new record types do not match."); + + DeleteRecord(oldRecord.Type, oldRecord.RDATA, out DnsResourceRecord deletedRecord); + base.AddRecord(newRecord); + + _primaryZone.CommitAndIncrementSerial(new DnsResourceRecord[] { deletedRecord }, new DnsResourceRecord[] { newRecord }); + _primaryZone.TriggerNotify(); + } + #endregion } }