123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- /*
- Technitium DNS Server
- Copyright (C) 2020 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace DnsServerCore.Dns.Zones
- {
- class ZoneTree<T> : DomainTree<T> where T : Zone
- {
- #region private
- private Node GetNextSubDomainZoneNode(Node current, int baseDepth)
- {
- int k = 0;
- while ((current != null) && (current.Depth >= baseDepth))
- {
- Node[] children = current.Children;
- if (children != null)
- {
- //find child node
- Node child = null;
- for (int i = k; i < children.Length; i++)
- {
- child = Volatile.Read(ref children[i]);
- if (child != null)
- {
- NodeValue value = child.Value;
- if (value != null)
- {
- T zone = value.Value;
- if (zone != null)
- {
- if (zone is SubDomainZone)
- return child; //child has value so return it
- if ((zone is PrimaryZone) || (zone is SecondaryZone) || (zone is StubZone) || (zone is ForwarderZone))
- {
- //skip to next child to avoid listing this auth zone's sub domains
- child = null; //set null to avoid child being set as current after the loop
- continue;
- }
- }
- }
- if (child.Children != null)
- break;
- }
- }
- if (child != null)
- {
- //make found child as current
- k = 0;
- current = child;
- continue; //start over
- }
- }
- //no child nodes available; move up to parent node
- k = current.K + 1;
- current = current.Parent;
- }
- return null;
- }
- private Node GetNextChildZoneNode(Node current, int baseDepth)
- {
- int k = 0;
- while ((current != null) && (current.Depth >= baseDepth))
- {
- if ((current.K != 39) || (current.Depth == baseDepth)) //[.] skip this node's children as its last for current sub zone
- {
- Node[] children = current.Children;
- if (children != null)
- {
- //find child node
- Node child = null;
- for (int i = k; i < children.Length; i++)
- {
- child = Volatile.Read(ref children[i]);
- if (child != null)
- {
- if (child.Value != null)
- return child; //child has value so return it
- if (child.K == 39) //[.]
- return child; //child node is last for current sub zone
- if (child.Children != null)
- break;
- }
- }
- if (child != null)
- {
- //make found child as current
- k = 0;
- current = child;
- continue; //start over
- }
- }
- }
- //no child nodes available; move up to parent node
- k = current.K + 1;
- current = current.Parent;
- }
- return null;
- }
- private byte[] GetNodeKey(Node node)
- {
- byte[] key = new byte[node.Depth];
- int i = node.Depth - 1;
- while (i > -1)
- {
- key[i--] = node.K;
- node = node.Parent;
- }
- return key;
- }
- private static bool KeysMatch(byte[] key1, byte[] key2)
- {
- //com.example.*.
- //com.example.*.www.
- //com.example.abc.www.
- int i = 0;
- int j = 0;
- while ((i < key1.Length) && (j < key2.Length))
- {
- if (key1[i] == 38) //[*]
- {
- if (i == key1.Length - 2)
- return true;
- //skip j to next label
- while (j < key2.Length)
- {
- if (key2[j] == 39) //[.]
- break;
- j++;
- }
- i++;
- continue;
- }
- if (key2[j] == 38) //[*]
- {
- if (j == key2.Length - 2)
- return true;
- //skip i to next label
- while (i < key1.Length)
- {
- if (key1[i] == 39) //[.]
- break;
- i++;
- }
- j++;
- continue;
- }
- if (key1[i] != key2[j])
- return false;
- i++;
- j++;
- }
- return (i == key1.Length) && (j == key2.Length);
- }
- private static bool IsKeySubDomain(byte[] mainKey, byte[] testKey)
- {
- //com.example.*.
- //com.example.*.www.
- //com.example.abc.www.
- int i = 0;
- int j = 0;
- while ((i < mainKey.Length) && (j < testKey.Length))
- {
- if (mainKey[i] == 38) //[*]
- {
- if (i == mainKey.Length - 2)
- return true;
- //skip j to next label
- while (j < testKey.Length)
- {
- if (testKey[j] == 39) //[.]
- break;
- j++;
- }
- i++;
- continue;
- }
- if (mainKey[i] != testKey[j])
- return false;
- i++;
- j++;
- }
- return (i == mainKey.Length) && (j < testKey.Length);
- }
- private NodeValue FindNodeValue(byte[] key, out Node closestNode, out NodeValue closestDelegation, out NodeValue closestAuthority)
- {
- closestNode = _root;
- closestDelegation = null;
- closestAuthority = null;
- Node wildcard = null;
- int i = 0;
- while (i <= key.Length)
- {
- //find authority zone
- NodeValue value = closestNode.Value;
- if (value != null)
- {
- T zoneValue = value.Value;
- if (zoneValue != null)
- {
- if (zoneValue is AuthZone)
- {
- if ((zoneValue is PrimaryZone) || (zoneValue is SecondaryZone) || (zoneValue is StubZone) || (zoneValue is ForwarderZone))
- {
- if (IsKeySubDomain(value.Key, key))
- {
- //hosted primary/secondary/stub/forwarder zone found
- closestDelegation = null;
- closestAuthority = value;
- }
- }
- else if ((zoneValue is SubDomainZone) && (closestDelegation == null) && zoneValue.ContainsNameServerRecords())
- {
- if (IsKeySubDomain(value.Key, key))
- {
- //delegated sub domain found
- closestDelegation = value;
- }
- }
- }
- else if ((zoneValue is CacheZone) && zoneValue.ContainsNameServerRecords())
- {
- if (IsKeySubDomain(value.Key, key))
- {
- closestDelegation = value;
- }
- }
- }
- }
- if (i == key.Length)
- break;
- Node[] children = closestNode.Children;
- if (children == null)
- break;
- Node child = Volatile.Read(ref children[38]); //[*]
- if (child != null)
- wildcard = child;
- child = Volatile.Read(ref children[key[i]]);
- if (child == null)
- {
- //no child found
- if (wildcard == null)
- return null; //no child or wildcard found
- //use wildcard node
- //skip to next label
- do
- {
- i++;
- if (key[i] == 39) //[.]
- break;
- }
- while (i < key.Length);
- closestNode = wildcard;
- wildcard = null;
- continue;
- }
- closestNode = child;
- i++;
- }
- {
- NodeValue value = closestNode.Value;
- if (value != null)
- {
- //match exact + wildcard keys
- if (KeysMatch(key, value.Key))
- return value; //found matching value
- }
- }
- if (wildcard != null)
- {
- //wildcard node found
- NodeValue value = wildcard.Value;
- if (value == null)
- {
- //find value from next [.] node
- Node[] children = wildcard.Children;
- if (children != null)
- {
- Node child = Volatile.Read(ref children[39]); //[.]
- if (child != null)
- {
- value = child.Value;
- if (value != null)
- {
- //match wildcard keys
- if (KeysMatch(key, value.Key))
- return value; //found matching wildcard value
- }
- }
- }
- }
- else
- {
- //match wildcard keys
- if (KeysMatch(key, value.Key))
- return value; //found matching wildcard value
- }
- }
- //value not found
- return null;
- }
- private bool SubDomainExists(byte[] key, Node closestNode)
- {
- if (!closestNode.HasChildren)
- return false;
- Node nextSubDomain = GetNextSubDomainZoneNode(closestNode, closestNode.Depth);
- if (nextSubDomain == null)
- return false;
- NodeValue value = nextSubDomain.Value;
- if (value == null)
- return false;
- return IsKeySubDomain(key, value.Key);
- }
- #endregion
- #region public
- public bool TryAdd(T zone)
- {
- return TryAdd(zone.Name, zone);
- }
- public override bool TryRemove(string domain, out T value)
- {
- if (typeof(T) == typeof(CacheZone))
- {
- bool removed = TryRemove(domain, out value, out Node closestNode);
- //remove all cache zones under current zone
- Node current = closestNode;
- do
- {
- current = current.GetNextNodeWithValue(closestNode.Depth);
- if (current == null)
- break;
- NodeValue v = current.Value;
- if (v != null)
- {
- T zone = v.Value;
- if (zone != null)
- {
- current.RemoveNodeValue(v.Key, out _); //remove node value
- current.CleanThisBranch();
- removed = true;
- }
- }
- }
- while (true);
- if (removed)
- closestNode.CleanThisBranch();
- return removed;
- }
- else
- {
- if (TryRemove(domain, out value, out Node closestNode))
- {
- if ((value != null) && ((value is PrimaryZone) || (value is SecondaryZone) || (value is StubZone) || (value is ForwarderZone)))
- {
- //remove all sub domains under current zone
- Node current = closestNode;
- do
- {
- current = GetNextSubDomainZoneNode(current, closestNode.Depth);
- if (current == null)
- break;
- NodeValue v = current.Value;
- if (v != null)
- {
- T zone = v.Value;
- if (zone != null)
- {
- current.RemoveNodeValue(v.Key, out _); //remove node value
- current.CleanThisBranch();
- }
- }
- }
- while (true);
- }
- closestNode.CleanThisBranch();
- return true;
- }
- return false;
- }
- }
- public List<T> GetZoneWithSubDomainZones(string domain)
- {
- if (domain == null)
- throw new ArgumentNullException(nameof(domain));
- List<T> zones = new List<T>();
- byte[] bKey = ConvertToByteKey(domain);
- NodeValue nodeValue = _root.FindNodeValue(bKey, out Node closestNode);
- if (nodeValue != null)
- {
- T zone = nodeValue.Value;
- if (zone != null)
- {
- if ((zone is PrimaryZone) || (zone is SecondaryZone) || (zone is StubZone) || (zone is ForwarderZone))
- {
- zones.Add(zone);
- Node current = closestNode;
- do
- {
- current = GetNextSubDomainZoneNode(current, closestNode.Depth);
- if (current == null)
- break;
- NodeValue value = current.Value;
- if (value != null)
- {
- zone = value.Value;
- if (zone != null)
- zones.Add(zone);
- }
- }
- while (true);
- }
- }
- }
- return zones;
- }
- public List<string> ListSubDomains(string domain)
- {
- if (domain == null)
- throw new ArgumentNullException(nameof(domain));
- List<string> zones = new List<string>();
- byte[] bKey = ConvertToByteKey(domain);
- _ = _root.FindNodeValue(bKey, out Node closestNode);
- Node current = closestNode;
- NodeValue value;
- do
- {
- value = current.Value;
- if (value != null)
- {
- if (bKey.Length < value.Key.Length)
- zones.Add(ConvertKeyToLabel(value.Key, bKey.Length));
- }
- else if ((current.K == 39) && (current.Depth > closestNode.Depth))
- {
- zones.Add(ConvertKeyToLabel(GetNodeKey(current), bKey.Length));
- }
- current = GetNextChildZoneNode(current, closestNode.Depth);
- }
- while (current != null);
- return zones;
- }
- public T FindZone(string domain, out T delegation, out T authority, out bool hasSubDomains)
- {
- if (domain == null)
- throw new ArgumentNullException(nameof(domain));
- byte[] key = ConvertToByteKey(domain);
- NodeValue nodeValue = FindNodeValue(key, out Node closestNode, out NodeValue closestDelegation, out NodeValue closestAuthority);
- if (nodeValue == null)
- {
- //zone not found
- if (closestDelegation != null)
- delegation = closestDelegation.Value;
- else
- delegation = null;
- if (closestAuthority != null)
- authority = closestAuthority.Value;
- else
- authority = null;
- if (authority == null)
- {
- //no authority so no subdomains
- hasSubDomains = false;
- }
- else
- {
- //check if current node has sub domains
- NodeValue value = closestNode.Value;
- if (value == null)
- hasSubDomains = SubDomainExists(key, closestNode);
- else
- hasSubDomains = IsKeySubDomain(key, value.Key);
- }
- return null;
- }
- T zoneValue = nodeValue.Value;
- if (zoneValue == null)
- {
- //zone value missing!
- delegation = null;
- authority = null;
- hasSubDomains = false;
- return null;
- }
- //zone found
- if (zoneValue is AuthZone)
- {
- if ((zoneValue is PrimaryZone) || (zoneValue is SecondaryZone) || (zoneValue is StubZone) || (zoneValue is ForwarderZone))
- {
- delegation = null;
- authority = zoneValue;
- }
- else
- {
- if (closestDelegation != null)
- delegation = closestDelegation.Value;
- else if ((zoneValue is SubDomainZone) && zoneValue.ContainsNameServerRecords())
- delegation = zoneValue;
- else
- delegation = null;
- if (closestAuthority != null)
- authority = closestAuthority.Value;
- else
- authority = null;
- }
- hasSubDomains = SubDomainExists(key, closestNode);
- }
- else if (zoneValue is CacheZone)
- {
- if (zoneValue.ContainsNameServerRecords())
- delegation = zoneValue;
- else if (closestDelegation != null)
- delegation = closestDelegation.Value;
- else
- delegation = null;
- authority = null; //cache does not use this value
- hasSubDomains = false; //cache does not use this value
- }
- else
- {
- throw new InvalidOperationException();
- }
- return zoneValue;
- }
- #endregion
- }
- }
|