From bbe9aebfa0c79b7dae9067a6cf4992ad0eeed1aa Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 14 Jan 2023 13:41:02 +0530 Subject: [PATCH] DnsResourceRecordExtension: removed get/set type of extension methods. --- .../DnsResourceRecordExtension.cs | 161 +++--------------- 1 file changed, 24 insertions(+), 137 deletions(-) diff --git a/DnsServerCore/Dns/ResourceRecords/DnsResourceRecordExtension.cs b/DnsServerCore/Dns/ResourceRecords/DnsResourceRecordExtension.cs index ce5e017a..05d9492d 100644 --- a/DnsServerCore/Dns/ResourceRecords/DnsResourceRecordExtension.cs +++ b/DnsServerCore/Dns/ResourceRecords/DnsResourceRecordExtension.cs @@ -23,53 +23,37 @@ using System.Linq; using System.Net; using System.Net.Sockets; using TechnitiumLibrary; -using TechnitiumLibrary.Net.Dns; using TechnitiumLibrary.Net.Dns.ResourceRecords; namespace DnsServerCore.Dns.ResourceRecords { static class DnsResourceRecordExtension { - public static void SetGlueRecords(this DnsResourceRecord record, IReadOnlyList glueRecords) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) - { - rrInfo = new DnsResourceRecordInfo(); - record.Tag = rrInfo; - } - - rrInfo.GlueRecords = glueRecords; - } - public static void SetGlueRecords(this DnsResourceRecord record, string glueAddresses) - { - SetGlueRecords(record, glueAddresses.Split(IPAddress.Parse, ',')); - } - - public static void SetGlueRecords(this DnsResourceRecord record, IReadOnlyList glueAddresses) { if (record.RDATA is not DnsNSRecordData nsRecord) throw new InvalidOperationException(); string domain = nsRecord.NameServer; - DnsResourceRecord[] glueRecords = new DnsResourceRecord[glueAddresses.Count]; + IReadOnlyList glueAddressesList = glueAddresses.Split(IPAddress.Parse, ','); + DnsResourceRecord[] glueRecords = new DnsResourceRecord[glueAddressesList.Count]; for (int i = 0; i < glueRecords.Length; i++) { - switch (glueAddresses[i].AddressFamily) + switch (glueAddressesList[i].AddressFamily) { case AddressFamily.InterNetwork: - glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.A, DnsClass.IN, record.TTL, new DnsARecordData(glueAddresses[i])); + glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.A, DnsClass.IN, record.TTL, new DnsARecordData(glueAddressesList[i])); break; case AddressFamily.InterNetworkV6: - glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.AAAA, DnsClass.IN, record.TTL, new DnsAAAARecordData(glueAddresses[i])); + glueRecords[i] = new DnsResourceRecord(domain, DnsResourceRecordType.AAAA, DnsClass.IN, record.TTL, new DnsAAAARecordData(glueAddressesList[i])); break; } } - SetGlueRecords(record, glueRecords); + record.GetAuthRecordInfo().GlueRecords = glueRecords; } public static void SyncGlueRecords(this DnsResourceRecord record, IReadOnlyList allGlueRecords) @@ -94,10 +78,7 @@ namespace DnsServerCore.Dns.ResourceRecords } } - if (foundGlueRecords.Count > 0) - SetGlueRecords(record, foundGlueRecords); - else - SetGlueRecords(record, Array.Empty()); + record.GetAuthRecordInfo().GlueRecords = foundGlueRecords; } public static void SyncGlueRecords(this DnsResourceRecord record, IReadOnlyCollection deletedGlueRecords, IReadOnlyCollection addedGlueRecords) @@ -108,14 +89,16 @@ namespace DnsServerCore.Dns.ResourceRecords bool updated = false; List updatedGlueRecords = new List(); - IReadOnlyList existingGlueRecords = GetGlueRecords(record); - - foreach (DnsResourceRecord existingGlueRecord in existingGlueRecords) + IReadOnlyList existingGlueRecords = record.GetAuthRecordInfo().GlueRecords; + if (existingGlueRecords is not null) { - if (deletedGlueRecords.Contains(existingGlueRecord)) - updated = true; //skipped to delete existing glue record - else - updatedGlueRecords.Add(existingGlueRecord); + foreach (DnsResourceRecord existingGlueRecord in existingGlueRecords) + { + if (deletedGlueRecords.Contains(existingGlueRecord)) + updated = true; //skipped to delete existing glue record + else + updatedGlueRecords.Add(existingGlueRecord); + } } string domain = nsRecord.NameServer; @@ -136,121 +119,25 @@ namespace DnsServerCore.Dns.ResourceRecords } if (updated) - SetGlueRecords(record, updatedGlueRecords); + record.GetAuthRecordInfo().GlueRecords = updatedGlueRecords; } - public static IReadOnlyList GetGlueRecords(this DnsResourceRecord record) + public static AuthRecordInfo GetAuthRecordInfo(this DnsResourceRecord record) { - if (record.Tag is DnsResourceRecordInfo rrInfo) + if (record.Tag is not AuthRecordInfo rrInfo) { - IReadOnlyList glueRecords = rrInfo.GlueRecords; - if (glueRecords is null) - return Array.Empty(); - - return glueRecords; - } - - return Array.Empty(); - } - - public static bool IsDisabled(this DnsResourceRecord record) - { - if (record.Tag is DnsResourceRecordInfo rrInfo) - return rrInfo.Disabled; - - return false; - } - - public static void Disable(this DnsResourceRecord record) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) - { - rrInfo = new DnsResourceRecordInfo(); + rrInfo = new AuthRecordInfo(); record.Tag = rrInfo; } - rrInfo.Disabled = true; + return rrInfo; } - public static void Enable(this DnsResourceRecord record) + public static CacheRecordInfo GetCacheRecordInfo(this DnsResourceRecord record) { - if (record.Tag is DnsResourceRecordInfo rrInfo) - rrInfo.Disabled = false; - } - - public static string GetComments(this DnsResourceRecord record) - { - if (record.Tag is DnsResourceRecordInfo rrInfo) - return rrInfo.Comments; - - return null; - } - - public static void SetComments(this DnsResourceRecord record, string value) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) + if (record.Tag is not CacheRecordInfo rrInfo) { - rrInfo = new DnsResourceRecordInfo(); - record.Tag = rrInfo; - } - - rrInfo.Comments = value; - } - - public static DateTime GetDeletedOn(this DnsResourceRecord record) - { - if (record.Tag is DnsResourceRecordInfo rrInfo) - return rrInfo.DeletedOn; - - return DateTime.MinValue; - } - - public static void SetDeletedOn(this DnsResourceRecord record, DateTime value) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) - { - rrInfo = new DnsResourceRecordInfo(); - record.Tag = rrInfo; - } - - rrInfo.DeletedOn = value; - } - - public static void SetPrimaryNameServers(this DnsResourceRecord record, IReadOnlyList primaryNameServers) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) - { - rrInfo = new DnsResourceRecordInfo(); - record.Tag = rrInfo; - } - - rrInfo.PrimaryNameServers = primaryNameServers; - } - - public static void SetPrimaryNameServers(this DnsResourceRecord record, string primaryNameServers) - { - SetPrimaryNameServers(record, primaryNameServers.Split(NameServerAddress.Parse, ',')); - } - - public static IReadOnlyList GetPrimaryNameServers(this DnsResourceRecord record) - { - if (record.Tag is DnsResourceRecordInfo rrInfo) - { - IReadOnlyList primaryNameServers = rrInfo.PrimaryNameServers; - if (primaryNameServers is null) - return Array.Empty(); - - return primaryNameServers; - } - - return Array.Empty(); - } - - public static DnsResourceRecordInfo GetRecordInfo(this DnsResourceRecord record) - { - if (record.Tag is not DnsResourceRecordInfo rrInfo) - { - rrInfo = new DnsResourceRecordInfo(); + rrInfo = new CacheRecordInfo(); record.Tag = rrInfo; }