ZoneTree.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2020 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Threading;
  18. namespace DnsServerCore.Dns.Zones
  19. {
  20. class ZoneTree<T> : DomainTree<T> where T : Zone
  21. {
  22. #region private
  23. private Node GetNextSubDomainZoneNode(Node current, int baseDepth)
  24. {
  25. int k = 0;
  26. while ((current != null) && (current.Depth >= baseDepth))
  27. {
  28. Node[] children = current.Children;
  29. if (children != null)
  30. {
  31. //find child node
  32. Node child = null;
  33. for (int i = k; i < children.Length; i++)
  34. {
  35. child = Volatile.Read(ref children[i]);
  36. if (child != null)
  37. {
  38. NodeValue value = child.Value;
  39. if (value != null)
  40. {
  41. T zone = value.Value;
  42. if (zone != null)
  43. {
  44. if (zone is SubDomainZone)
  45. return child; //child has value so return it
  46. if ((zone is PrimaryZone) || (zone is SecondaryZone) || (zone is StubZone) || (zone is ForwarderZone))
  47. {
  48. //skip to next child to avoid listing this auth zone's sub domains
  49. child = null; //set null to avoid child being set as current after the loop
  50. continue;
  51. }
  52. }
  53. }
  54. if (child.Children != null)
  55. break;
  56. }
  57. }
  58. if (child != null)
  59. {
  60. //make found child as current
  61. k = 0;
  62. current = child;
  63. continue; //start over
  64. }
  65. }
  66. //no child nodes available; move up to parent node
  67. k = current.K + 1;
  68. current = current.Parent;
  69. }
  70. return null;
  71. }
  72. private Node GetNextChildZoneNode(Node current, int baseDepth)
  73. {
  74. int k = 0;
  75. while ((current != null) && (current.Depth >= baseDepth))
  76. {
  77. if ((current.K != 39) || (current.Depth == baseDepth)) //[.] skip this node's children as its last for current sub zone
  78. {
  79. Node[] children = current.Children;
  80. if (children != null)
  81. {
  82. //find child node
  83. Node child = null;
  84. for (int i = k; i < children.Length; i++)
  85. {
  86. child = Volatile.Read(ref children[i]);
  87. if (child != null)
  88. {
  89. if (child.Value != null)
  90. return child; //child has value so return it
  91. if (child.K == 39) //[.]
  92. return child; //child node is last for current sub zone
  93. if (child.Children != null)
  94. break;
  95. }
  96. }
  97. if (child != null)
  98. {
  99. //make found child as current
  100. k = 0;
  101. current = child;
  102. continue; //start over
  103. }
  104. }
  105. }
  106. //no child nodes available; move up to parent node
  107. k = current.K + 1;
  108. current = current.Parent;
  109. }
  110. return null;
  111. }
  112. private byte[] GetNodeKey(Node node)
  113. {
  114. byte[] key = new byte[node.Depth];
  115. int i = node.Depth - 1;
  116. while (i > -1)
  117. {
  118. key[i--] = node.K;
  119. node = node.Parent;
  120. }
  121. return key;
  122. }
  123. private static bool KeysMatch(byte[] key1, byte[] key2)
  124. {
  125. //com.example.*.
  126. //com.example.*.www.
  127. //com.example.abc.www.
  128. int i = 0;
  129. int j = 0;
  130. while ((i < key1.Length) && (j < key2.Length))
  131. {
  132. if (key1[i] == 38) //[*]
  133. {
  134. if (i == key1.Length - 2)
  135. return true;
  136. //skip j to next label
  137. while (j < key2.Length)
  138. {
  139. if (key2[j] == 39) //[.]
  140. break;
  141. j++;
  142. }
  143. i++;
  144. continue;
  145. }
  146. if (key2[j] == 38) //[*]
  147. {
  148. if (j == key2.Length - 2)
  149. return true;
  150. //skip i to next label
  151. while (i < key1.Length)
  152. {
  153. if (key1[i] == 39) //[.]
  154. break;
  155. i++;
  156. }
  157. j++;
  158. continue;
  159. }
  160. if (key1[i] != key2[j])
  161. return false;
  162. i++;
  163. j++;
  164. }
  165. return (i == key1.Length) && (j == key2.Length);
  166. }
  167. private static bool IsKeySubDomain(byte[] mainKey, byte[] testKey)
  168. {
  169. //com.example.*.
  170. //com.example.*.www.
  171. //com.example.abc.www.
  172. int i = 0;
  173. int j = 0;
  174. while ((i < mainKey.Length) && (j < testKey.Length))
  175. {
  176. if (mainKey[i] == 38) //[*]
  177. {
  178. if (i == mainKey.Length - 2)
  179. return true;
  180. //skip j to next label
  181. while (j < testKey.Length)
  182. {
  183. if (testKey[j] == 39) //[.]
  184. break;
  185. j++;
  186. }
  187. i++;
  188. continue;
  189. }
  190. if (mainKey[i] != testKey[j])
  191. return false;
  192. i++;
  193. j++;
  194. }
  195. return (i == mainKey.Length) && (j < testKey.Length);
  196. }
  197. private NodeValue FindNodeValue(byte[] key, out Node closestNode, out NodeValue closestDelegation, out NodeValue closestAuthority)
  198. {
  199. closestNode = _root;
  200. closestDelegation = null;
  201. closestAuthority = null;
  202. Node wildcard = null;
  203. int i = 0;
  204. while (i <= key.Length)
  205. {
  206. //find authority zone
  207. NodeValue value = closestNode.Value;
  208. if (value != null)
  209. {
  210. T zoneValue = value.Value;
  211. if (zoneValue != null)
  212. {
  213. if (zoneValue is AuthZone)
  214. {
  215. if ((zoneValue is PrimaryZone) || (zoneValue is SecondaryZone) || (zoneValue is StubZone) || (zoneValue is ForwarderZone))
  216. {
  217. if (IsKeySubDomain(value.Key, key))
  218. {
  219. //hosted primary/secondary/stub/forwarder zone found
  220. closestDelegation = null;
  221. closestAuthority = value;
  222. }
  223. }
  224. else if ((zoneValue is SubDomainZone) && (closestDelegation == null) && zoneValue.ContainsNameServerRecords())
  225. {
  226. if (IsKeySubDomain(value.Key, key))
  227. {
  228. //delegated sub domain found
  229. closestDelegation = value;
  230. }
  231. }
  232. }
  233. else if ((zoneValue is CacheZone) && zoneValue.ContainsNameServerRecords())
  234. {
  235. if (IsKeySubDomain(value.Key, key))
  236. {
  237. closestDelegation = value;
  238. }
  239. }
  240. }
  241. }
  242. if (i == key.Length)
  243. break;
  244. Node[] children = closestNode.Children;
  245. if (children == null)
  246. break;
  247. Node child = Volatile.Read(ref children[38]); //[*]
  248. if (child != null)
  249. wildcard = child;
  250. child = Volatile.Read(ref children[key[i]]);
  251. if (child == null)
  252. {
  253. //no child found
  254. if (wildcard == null)
  255. return null; //no child or wildcard found
  256. //use wildcard node
  257. //skip to next label
  258. do
  259. {
  260. i++;
  261. if (key[i] == 39) //[.]
  262. break;
  263. }
  264. while (i < key.Length);
  265. closestNode = wildcard;
  266. wildcard = null;
  267. continue;
  268. }
  269. closestNode = child;
  270. i++;
  271. }
  272. {
  273. NodeValue value = closestNode.Value;
  274. if (value != null)
  275. {
  276. //match exact + wildcard keys
  277. if (KeysMatch(key, value.Key))
  278. return value; //found matching value
  279. }
  280. }
  281. if (wildcard != null)
  282. {
  283. //wildcard node found
  284. NodeValue value = wildcard.Value;
  285. if (value == null)
  286. {
  287. //find value from next [.] node
  288. Node[] children = wildcard.Children;
  289. if (children != null)
  290. {
  291. Node child = Volatile.Read(ref children[39]); //[.]
  292. if (child != null)
  293. {
  294. value = child.Value;
  295. if (value != null)
  296. {
  297. //match wildcard keys
  298. if (KeysMatch(key, value.Key))
  299. return value; //found matching wildcard value
  300. }
  301. }
  302. }
  303. }
  304. else
  305. {
  306. //match wildcard keys
  307. if (KeysMatch(key, value.Key))
  308. return value; //found matching wildcard value
  309. }
  310. }
  311. //value not found
  312. return null;
  313. }
  314. private bool SubDomainExists(byte[] key, Node closestNode)
  315. {
  316. if (!closestNode.HasChildren)
  317. return false;
  318. Node nextSubDomain = GetNextSubDomainZoneNode(closestNode, closestNode.Depth);
  319. if (nextSubDomain == null)
  320. return false;
  321. NodeValue value = nextSubDomain.Value;
  322. if (value == null)
  323. return false;
  324. return IsKeySubDomain(key, value.Key);
  325. }
  326. #endregion
  327. #region public
  328. public bool TryAdd(T zone)
  329. {
  330. return TryAdd(zone.Name, zone);
  331. }
  332. public override bool TryRemove(string domain, out T value)
  333. {
  334. if (typeof(T) == typeof(CacheZone))
  335. {
  336. bool removed = TryRemove(domain, out value, out Node closestNode);
  337. //remove all cache zones under current zone
  338. Node current = closestNode;
  339. do
  340. {
  341. current = current.GetNextNodeWithValue(closestNode.Depth);
  342. if (current == null)
  343. break;
  344. NodeValue v = current.Value;
  345. if (v != null)
  346. {
  347. T zone = v.Value;
  348. if (zone != null)
  349. {
  350. current.RemoveNodeValue(v.Key, out _); //remove node value
  351. current.CleanThisBranch();
  352. removed = true;
  353. }
  354. }
  355. }
  356. while (true);
  357. if (removed)
  358. closestNode.CleanThisBranch();
  359. return removed;
  360. }
  361. else
  362. {
  363. if (TryRemove(domain, out value, out Node closestNode))
  364. {
  365. if ((value != null) && ((value is PrimaryZone) || (value is SecondaryZone) || (value is StubZone) || (value is ForwarderZone)))
  366. {
  367. //remove all sub domains under current zone
  368. Node current = closestNode;
  369. do
  370. {
  371. current = GetNextSubDomainZoneNode(current, closestNode.Depth);
  372. if (current == null)
  373. break;
  374. NodeValue v = current.Value;
  375. if (v != null)
  376. {
  377. T zone = v.Value;
  378. if (zone != null)
  379. {
  380. current.RemoveNodeValue(v.Key, out _); //remove node value
  381. current.CleanThisBranch();
  382. }
  383. }
  384. }
  385. while (true);
  386. }
  387. closestNode.CleanThisBranch();
  388. return true;
  389. }
  390. return false;
  391. }
  392. }
  393. public List<T> GetZoneWithSubDomainZones(string domain)
  394. {
  395. if (domain == null)
  396. throw new ArgumentNullException(nameof(domain));
  397. List<T> zones = new List<T>();
  398. byte[] bKey = ConvertToByteKey(domain);
  399. NodeValue nodeValue = _root.FindNodeValue(bKey, out Node closestNode);
  400. if (nodeValue != null)
  401. {
  402. T zone = nodeValue.Value;
  403. if (zone != null)
  404. {
  405. if ((zone is PrimaryZone) || (zone is SecondaryZone) || (zone is StubZone) || (zone is ForwarderZone))
  406. {
  407. zones.Add(zone);
  408. Node current = closestNode;
  409. do
  410. {
  411. current = GetNextSubDomainZoneNode(current, closestNode.Depth);
  412. if (current == null)
  413. break;
  414. NodeValue value = current.Value;
  415. if (value != null)
  416. {
  417. zone = value.Value;
  418. if (zone != null)
  419. zones.Add(zone);
  420. }
  421. }
  422. while (true);
  423. }
  424. }
  425. }
  426. return zones;
  427. }
  428. public List<string> ListSubDomains(string domain)
  429. {
  430. if (domain == null)
  431. throw new ArgumentNullException(nameof(domain));
  432. List<string> zones = new List<string>();
  433. byte[] bKey = ConvertToByteKey(domain);
  434. _ = _root.FindNodeValue(bKey, out Node closestNode);
  435. Node current = closestNode;
  436. NodeValue value;
  437. do
  438. {
  439. value = current.Value;
  440. if (value != null)
  441. {
  442. if (bKey.Length < value.Key.Length)
  443. zones.Add(ConvertKeyToLabel(value.Key, bKey.Length));
  444. }
  445. else if ((current.K == 39) && (current.Depth > closestNode.Depth))
  446. {
  447. zones.Add(ConvertKeyToLabel(GetNodeKey(current), bKey.Length));
  448. }
  449. current = GetNextChildZoneNode(current, closestNode.Depth);
  450. }
  451. while (current != null);
  452. return zones;
  453. }
  454. public T FindZone(string domain, out T delegation, out T authority, out bool hasSubDomains)
  455. {
  456. if (domain == null)
  457. throw new ArgumentNullException(nameof(domain));
  458. byte[] key = ConvertToByteKey(domain);
  459. NodeValue nodeValue = FindNodeValue(key, out Node closestNode, out NodeValue closestDelegation, out NodeValue closestAuthority);
  460. if (nodeValue == null)
  461. {
  462. //zone not found
  463. if (closestDelegation != null)
  464. delegation = closestDelegation.Value;
  465. else
  466. delegation = null;
  467. if (closestAuthority != null)
  468. authority = closestAuthority.Value;
  469. else
  470. authority = null;
  471. if (authority == null)
  472. {
  473. //no authority so no subdomains
  474. hasSubDomains = false;
  475. }
  476. else
  477. {
  478. //check if current node has sub domains
  479. NodeValue value = closestNode.Value;
  480. if (value == null)
  481. hasSubDomains = SubDomainExists(key, closestNode);
  482. else
  483. hasSubDomains = IsKeySubDomain(key, value.Key);
  484. }
  485. return null;
  486. }
  487. T zoneValue = nodeValue.Value;
  488. if (zoneValue == null)
  489. {
  490. //zone value missing!
  491. delegation = null;
  492. authority = null;
  493. hasSubDomains = false;
  494. return null;
  495. }
  496. //zone found
  497. if (zoneValue is AuthZone)
  498. {
  499. if ((zoneValue is PrimaryZone) || (zoneValue is SecondaryZone) || (zoneValue is StubZone) || (zoneValue is ForwarderZone))
  500. {
  501. delegation = null;
  502. authority = zoneValue;
  503. }
  504. else
  505. {
  506. if (closestDelegation != null)
  507. delegation = closestDelegation.Value;
  508. else if ((zoneValue is SubDomainZone) && zoneValue.ContainsNameServerRecords())
  509. delegation = zoneValue;
  510. else
  511. delegation = null;
  512. if (closestAuthority != null)
  513. authority = closestAuthority.Value;
  514. else
  515. authority = null;
  516. }
  517. hasSubDomains = SubDomainExists(key, closestNode);
  518. }
  519. else if (zoneValue is CacheZone)
  520. {
  521. if (zoneValue.ContainsNameServerRecords())
  522. delegation = zoneValue;
  523. else if (closestDelegation != null)
  524. delegation = closestDelegation.Value;
  525. else
  526. delegation = null;
  527. authority = null; //cache does not use this value
  528. hasSubDomains = false; //cache does not use this value
  529. }
  530. else
  531. {
  532. throw new InvalidOperationException();
  533. }
  534. return zoneValue;
  535. }
  536. #endregion
  537. }
  538. }