diff --git a/DnsServerCore/Zone.cs b/DnsServerCore/Zone.cs index c4cdfe88..3c8992d5 100644 --- a/DnsServerCore/Zone.cs +++ b/DnsServerCore/Zone.cs @@ -95,109 +95,37 @@ namespace DnsServerCore return path; } - #endregion - - #region public static - - public static Zone CreateZone(Zone rootZone, string domain) + private void SetRecord(DnsResourceRecord[] resourceRecords) { - Zone currentZone = rootZone; - string[] path = ConvertDomainToPath(domain); + if (resourceRecords.Length < 1) + return; - for (int i = 0; i < path.Length; i++) - { - string nextZoneLabel = path[i]; + string domain = resourceRecords[0].Name; + DnsResourceRecordType type = resourceRecords[0].Type; - ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; - currentSubZoneLock.EnterWriteLock(); - try - { - if (currentZone._subZone.ContainsKey(nextZoneLabel)) - { - currentZone = currentZone._subZone[nextZoneLabel]; - } - else - { - string zoneName = nextZoneLabel; - - if (currentZone._name != "") - zoneName += "." + currentZone._name; - - Zone nextZone = new Zone(zoneName, currentZone._authoritativeZone); - currentZone._subZone.Add(nextZoneLabel, nextZone); - - currentZone = nextZone; - } - } - finally - { - currentSubZoneLock.ExitWriteLock(); - } - } - - return currentZone; - } - - public static Zone GetClosestZone(Zone rootZone, string domain) - { - Zone currentZone = rootZone; - string[] path = ConvertDomainToPath(domain); - - for (int i = 0; i < path.Length; i++) - { - string nextZoneLabel = path[i]; - - ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; - currentSubZoneLock.EnterReadLock(); - try - { - if (currentZone._subZone.ContainsKey(nextZoneLabel)) - currentZone = currentZone._subZone[nextZoneLabel]; - else - return currentZone; - } - finally - { - currentSubZoneLock.ExitReadLock(); - } - } - - return currentZone; - } - - public static void DeleteZone(Zone rootZone, string domain) - { - Zone currentZone = rootZone; - string[] path = ConvertDomainToPath(domain); - - //find parent zone - for (int i = 0; i < path.Length - 1; i++) - { - string nextZoneLabel = path[i]; - - ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; - currentSubZoneLock.EnterReadLock(); - try - { - if (currentZone._subZone.ContainsKey(nextZoneLabel)) - currentZone = currentZone._subZone[nextZoneLabel]; - else - return; - } - finally - { - currentSubZoneLock.ExitReadLock(); - } - } - - currentZone._subZoneLock.EnterWriteLock(); + _zoneEntriesLock.EnterWriteLock(); try { - currentZone._subZone.Remove(path[path.Length - 1]); + Dictionary zoneTypeEntries; + + if (_zoneEntries.ContainsKey(domain)) + { + zoneTypeEntries = _zoneEntries[domain]; + } + else + { + zoneTypeEntries = new Dictionary(); + _zoneEntries.Add(domain, zoneTypeEntries); + } + + if (zoneTypeEntries.ContainsKey(type)) + zoneTypeEntries[type] = resourceRecords; + else + zoneTypeEntries.Add(type, resourceRecords); } finally { - currentZone._subZoneLock.ExitWriteLock(); + _zoneEntriesLock.ExitWriteLock(); } } @@ -360,6 +288,112 @@ namespace DnsServerCore return new DnsDatagram(null, null, answer, authority, additional); } + #endregion + + #region public static + + public static Zone CreateZone(Zone rootZone, string domain) + { + Zone currentZone = rootZone; + string[] path = ConvertDomainToPath(domain); + + for (int i = 0; i < path.Length; i++) + { + string nextZoneLabel = path[i]; + + ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; + currentSubZoneLock.EnterWriteLock(); + try + { + if (currentZone._subZone.ContainsKey(nextZoneLabel)) + { + currentZone = currentZone._subZone[nextZoneLabel]; + } + else + { + string zoneName = nextZoneLabel; + + if (currentZone._name != "") + zoneName += "." + currentZone._name; + + Zone nextZone = new Zone(zoneName, currentZone._authoritativeZone); + currentZone._subZone.Add(nextZoneLabel, nextZone); + + currentZone = nextZone; + } + } + finally + { + currentSubZoneLock.ExitWriteLock(); + } + } + + return currentZone; + } + + public static Zone GetClosestZone(Zone rootZone, string domain) + { + Zone currentZone = rootZone; + string[] path = ConvertDomainToPath(domain); + + for (int i = 0; i < path.Length; i++) + { + string nextZoneLabel = path[i]; + + ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; + currentSubZoneLock.EnterReadLock(); + try + { + if (currentZone._subZone.ContainsKey(nextZoneLabel)) + currentZone = currentZone._subZone[nextZoneLabel]; + else + return currentZone; + } + finally + { + currentSubZoneLock.ExitReadLock(); + } + } + + return currentZone; + } + + public static void DeleteZone(Zone rootZone, string domain) + { + Zone currentZone = rootZone; + string[] path = ConvertDomainToPath(domain); + + //find parent zone + for (int i = 0; i < path.Length - 1; i++) + { + string nextZoneLabel = path[i]; + + ReaderWriterLockSlim currentSubZoneLock = currentZone._subZoneLock; + currentSubZoneLock.EnterReadLock(); + try + { + if (currentZone._subZone.ContainsKey(nextZoneLabel)) + currentZone = currentZone._subZone[nextZoneLabel]; + else + return; + } + finally + { + currentSubZoneLock.ExitReadLock(); + } + } + + currentZone._subZoneLock.EnterWriteLock(); + try + { + currentZone._subZone.Remove(path[path.Length - 1]); + } + finally + { + currentZone._subZoneLock.ExitWriteLock(); + } + } + public static DnsDatagram Query(Zone rootZone, DnsDatagram request, bool enableIPv6) { bool authoritativeAnswer = false; @@ -602,40 +636,6 @@ namespace DnsServerCore SetRecord(resourceRecords); } - public void SetRecord(DnsResourceRecord[] resourceRecords) - { - if (resourceRecords.Length < 1) - return; - - string domain = resourceRecords[0].Name; - DnsResourceRecordType type = resourceRecords[0].Type; - - _zoneEntriesLock.EnterWriteLock(); - try - { - Dictionary zoneTypeEntries; - - if (_zoneEntries.ContainsKey(domain)) - { - zoneTypeEntries = _zoneEntries[domain]; - } - else - { - zoneTypeEntries = new Dictionary(); - _zoneEntries.Add(domain, zoneTypeEntries); - } - - if (zoneTypeEntries.ContainsKey(type)) - zoneTypeEntries[type] = resourceRecords; - else - zoneTypeEntries.Add(type, resourceRecords); - } - finally - { - _zoneEntriesLock.ExitWriteLock(); - } - } - public DnsResourceRecord[] GetRecord(string domain, DnsResourceRecordType type) { _zoneEntriesLock.EnterReadLock();