From 866f30047d4ba71c71436083b953dfa812ffdee3 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 13 Jun 2020 13:31:57 +0530 Subject: [PATCH] Zone: added GetReverseZone() methods. --- DnsServerCore/Dns/Zones/Zone.cs | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/DnsServerCore/Dns/Zones/Zone.cs b/DnsServerCore/Dns/Zones/Zone.cs index 407fdbb5..b644d750 100644 --- a/DnsServerCore/Dns/Zones/Zone.cs +++ b/DnsServerCore/Dns/Zones/Zone.cs @@ -17,8 +17,12 @@ along with this program. If not, see . */ +using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using TechnitiumLibrary.Net; using TechnitiumLibrary.Net.Dns; namespace DnsServerCore.Dns.Zones @@ -41,6 +45,44 @@ namespace DnsServerCore.Dns.Zones #endregion + #region static + + public static string GetReverseZone(IPAddress address, IPAddress subnetMask) + { + return GetReverseZone(address, subnetMask.GetSubnetMaskWidth()); + } + + public static string GetReverseZone(IPAddress address, int subnetMaskWidth) + { + int addressByteCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(subnetMaskWidth) / 8)); + byte[] addressBytes = address.GetAddressBytes(); + string reverseZone = ""; + + switch (address.AddressFamily) + { + case AddressFamily.InterNetwork: + for (int i = 0; i < addressByteCount; i++) + reverseZone = addressBytes[i] + "." + reverseZone; + + reverseZone += "in-addr.arpa"; + break; + + case AddressFamily.InterNetworkV6: + for (int i = 0; i < addressByteCount; i++) + reverseZone = (addressBytes[i] & 0x0F).ToString("X") + "." + (addressBytes[i] >> 4).ToString("X") + "." + reverseZone; + + reverseZone += "ip6.arpa"; + break; + + default: + throw new NotSupportedException("AddressFamily not supported."); + } + + return reverseZone; + } + + #endregion + #region public public List ListAllRecords()