AuthZoneNode.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. using System;
  17. using System.Collections.Generic;
  18. using System.Threading;
  19. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  20. namespace DnsServerCore.Dns.Trees
  21. {
  22. class AuthZoneNode : IDisposable
  23. {
  24. #region variables
  25. SubDomainZone _parentSideZone;
  26. ApexZone _apexZone;
  27. #endregion
  28. #region constructors
  29. public AuthZoneNode(SubDomainZone parentSideZone, ApexZone zone)
  30. {
  31. _parentSideZone = parentSideZone;
  32. _apexZone = zone;
  33. }
  34. #endregion
  35. #region IDisposable
  36. bool _disposed;
  37. public void Dispose()
  38. {
  39. if (_disposed)
  40. return;
  41. if (_apexZone is not null)
  42. _apexZone.Dispose();
  43. _disposed = true;
  44. }
  45. #endregion
  46. #region public
  47. public bool TryAdd(ApexZone apexZone)
  48. {
  49. return Interlocked.CompareExchange(ref _apexZone, apexZone, null) is null;
  50. }
  51. public bool TryAdd(SubDomainZone parentSideZone)
  52. {
  53. return Interlocked.CompareExchange(ref _parentSideZone, parentSideZone, null) is null;
  54. }
  55. public bool TryRemove(out ApexZone apexZone)
  56. {
  57. apexZone = _apexZone;
  58. return ReferenceEquals(Interlocked.CompareExchange(ref _apexZone, null, apexZone), apexZone);
  59. }
  60. public bool TryRemove(out SubDomainZone parentSideZone)
  61. {
  62. parentSideZone = _parentSideZone;
  63. return ReferenceEquals(Interlocked.CompareExchange(ref _parentSideZone, null, parentSideZone), parentSideZone);
  64. }
  65. public SubDomainZone GetOrAddParentSideZone(Func<SubDomainZone> valueFactory)
  66. {
  67. SubDomainZone newParentSideZone = null;
  68. while (true)
  69. {
  70. SubDomainZone parentSideZone = _parentSideZone;
  71. if (parentSideZone is not null)
  72. return parentSideZone;
  73. if (newParentSideZone is null)
  74. newParentSideZone = valueFactory();
  75. if (TryAdd(newParentSideZone))
  76. return newParentSideZone;
  77. }
  78. }
  79. public IReadOnlyList<DnsResourceRecord> QueryRecords(DnsResourceRecordType type, bool dnssecOk)
  80. {
  81. if ((_apexZone is null) || (type == DnsResourceRecordType.DS))
  82. {
  83. if (_parentSideZone is null)
  84. return Array.Empty<DnsResourceRecord>();
  85. return _parentSideZone.QueryRecords(type, dnssecOk);
  86. }
  87. return _apexZone.QueryRecords(type, dnssecOk);
  88. }
  89. public AuthZone GetAuthZone(string zoneName)
  90. {
  91. if ((_apexZone is not null) && _apexZone.Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase))
  92. return _apexZone;
  93. return _parentSideZone;
  94. }
  95. #endregion
  96. #region properties
  97. public string Name
  98. {
  99. get
  100. {
  101. if (_parentSideZone is not null)
  102. return _parentSideZone.Name;
  103. if (_apexZone is not null)
  104. return _apexZone.Name;
  105. return null;
  106. }
  107. }
  108. public SubDomainZone ParentSideZone
  109. { get { return _parentSideZone; } }
  110. public ApexZone ApexZone
  111. { get { return _apexZone; } }
  112. public bool IsActive
  113. {
  114. get
  115. {
  116. if (_apexZone is not null)
  117. return _apexZone.IsActive;
  118. if (_parentSideZone is not null)
  119. return _parentSideZone.IsActive;
  120. return false;
  121. }
  122. }
  123. #endregion
  124. }
  125. }