CacheZoneTree.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 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 DnsServerCore.Dns.Zones;
  16. namespace DnsServerCore.Dns.Trees
  17. {
  18. class CacheZoneTree : ZoneTree<CacheZone, CacheZone, CacheZone>
  19. {
  20. #region protected
  21. protected override void GetClosestValuesForZone(CacheZone zoneValue, out CacheZone closestSubDomain, out CacheZone closestDelegation, out CacheZone closestAuthority)
  22. {
  23. if (zoneValue.ContainsNameServerRecords())
  24. {
  25. //ns records found
  26. closestSubDomain = null;
  27. closestDelegation = zoneValue;
  28. }
  29. else
  30. {
  31. closestSubDomain = zoneValue;
  32. closestDelegation = null;
  33. }
  34. closestAuthority = null;
  35. }
  36. #endregion
  37. #region public
  38. public bool TryRemoveTree(string domain, out CacheZone value, out int removedEntries)
  39. {
  40. bool removed = TryRemove(domain, out value, out Node currentNode);
  41. if (removed)
  42. removedEntries = value.TotalEntries;
  43. else
  44. removedEntries = 0;
  45. //remove all cache zones under current zone
  46. Node current = currentNode;
  47. do
  48. {
  49. current = current.GetNextNodeWithValue(currentNode.Depth);
  50. if (current is null)
  51. break;
  52. NodeValue v = current.Value;
  53. if (v is not null)
  54. {
  55. CacheZone zone = v.Value;
  56. if (zone is not null)
  57. {
  58. current.RemoveNodeValue(v.Key, out _); //remove node value
  59. current.CleanThisBranch();
  60. removed = true;
  61. removedEntries += zone.TotalEntries;
  62. }
  63. }
  64. }
  65. while (true);
  66. if (removed)
  67. currentNode.CleanThisBranch();
  68. return removed;
  69. }
  70. public CacheZone FindZone(string domain, out CacheZone closest, out CacheZone delegation)
  71. {
  72. byte[] key = ConvertToByteKey(domain);
  73. CacheZone zoneValue = FindZoneNode(key, false, out _, out _, out _, out CacheZone closestSubDomain, out CacheZone closestDelegation, out _);
  74. if (zoneValue is null)
  75. {
  76. //zone not found
  77. closest = closestSubDomain; //required for DNAME
  78. delegation = closestDelegation;
  79. return null;
  80. }
  81. else
  82. {
  83. //zone found
  84. closest = null; //not required
  85. if (zoneValue.ContainsNameServerRecords())
  86. delegation = zoneValue;
  87. else if (closestDelegation is not null)
  88. delegation = closestDelegation;
  89. else
  90. delegation = null;
  91. return zoneValue;
  92. }
  93. }
  94. #endregion
  95. }
  96. }