ResolverDnsCache.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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.ApplicationCommon;
  16. using DnsServerCore.Dns.Applications;
  17. using DnsServerCore.Dns.ZoneManagers;
  18. using System;
  19. using System.Net;
  20. using TechnitiumLibrary;
  21. using TechnitiumLibrary.Net.Dns;
  22. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  23. namespace DnsServerCore.Dns
  24. {
  25. class ResolverDnsCache : IDnsCache
  26. {
  27. #region variables
  28. readonly DnsApplicationManager _dnsApplicationManager;
  29. readonly AuthZoneManager _authZoneManager;
  30. readonly CacheZoneManager _cacheZoneManager;
  31. readonly LogManager _log;
  32. readonly bool _skipDnsAppAuthoritativeRequestHandlers;
  33. #endregion
  34. #region constructor
  35. public ResolverDnsCache(DnsApplicationManager dnsApplicationManager, AuthZoneManager authZoneManager, CacheZoneManager cacheZoneManager, LogManager log, bool skipDnsAppAuthoritativeRequestHandlers)
  36. {
  37. _dnsApplicationManager = dnsApplicationManager;
  38. _authZoneManager = authZoneManager;
  39. _cacheZoneManager = cacheZoneManager;
  40. _log = log;
  41. _skipDnsAppAuthoritativeRequestHandlers = skipDnsAppAuthoritativeRequestHandlers;
  42. }
  43. #endregion
  44. #region private
  45. private DnsDatagram DnsApplicationQueryClosestDelegation(DnsDatagram request)
  46. {
  47. if (_skipDnsAppAuthoritativeRequestHandlers || (_dnsApplicationManager.DnsAuthoritativeRequestHandlers.Count < 1) || (request.Question.Count != 1))
  48. return null;
  49. IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 0);
  50. DnsQuestionRecord question = request.Question[0];
  51. string currentDomain = question.Name;
  52. while (true)
  53. {
  54. DnsDatagram nsRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord(currentDomain, DnsResourceRecordType.NS, DnsClass.IN) });
  55. foreach (IDnsAuthoritativeRequestHandler requestHandler in _dnsApplicationManager.DnsAuthoritativeRequestHandlers)
  56. {
  57. try
  58. {
  59. DnsDatagram nsResponse = requestHandler.ProcessRequestAsync(nsRequest, localEP, DnsTransportProtocol.Tcp, false).Sync();
  60. if (nsResponse is not null)
  61. {
  62. if ((nsResponse.Answer.Count > 0) && (nsResponse.Answer[0].Type == DnsResourceRecordType.NS))
  63. return new DnsDatagram(request.Identifier, true, nsResponse.OPCODE, nsResponse.AuthoritativeAnswer, nsResponse.Truncation, nsResponse.RecursionDesired, nsResponse.RecursionAvailable, nsResponse.AuthenticData, nsResponse.CheckingDisabled, nsResponse.RCODE, request.Question, null, nsResponse.Answer, nsResponse.Additional);
  64. else if ((nsResponse.Authority.Count > 0) && (nsResponse.FindFirstAuthorityType() == DnsResourceRecordType.NS))
  65. return new DnsDatagram(request.Identifier, true, nsResponse.OPCODE, nsResponse.AuthoritativeAnswer, nsResponse.Truncation, nsResponse.RecursionDesired, nsResponse.RecursionAvailable, nsResponse.AuthenticData, nsResponse.CheckingDisabled, nsResponse.RCODE, request.Question, null, nsResponse.Authority, nsResponse.Additional);
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. if (_log is not null)
  71. _log.Write(ex);
  72. }
  73. }
  74. //get parent domain
  75. int i = currentDomain.IndexOf('.');
  76. if (i < 0)
  77. break;
  78. currentDomain = currentDomain.Substring(i + 1);
  79. }
  80. return null;
  81. }
  82. #endregion
  83. #region public
  84. public DnsDatagram QueryClosestDelegation(DnsDatagram request)
  85. {
  86. DnsDatagram authResponse = DnsApplicationQueryClosestDelegation(request);
  87. if (authResponse is null)
  88. authResponse = _authZoneManager.QueryClosestDelegation(request);
  89. DnsDatagram cacheResponse = _cacheZoneManager.QueryClosestDelegation(request);
  90. if ((authResponse is not null) && (authResponse.Authority.Count > 0))
  91. {
  92. if ((cacheResponse is not null) && (cacheResponse.Authority.Count > 0))
  93. {
  94. DnsResourceRecord authResponseFirstAuthority = authResponse.FindFirstAuthorityRecord();
  95. DnsResourceRecord cacheResponseFirstAuthority = cacheResponse.FindFirstAuthorityRecord();
  96. if (cacheResponseFirstAuthority.Name.Length > authResponseFirstAuthority.Name.Length)
  97. return cacheResponse;
  98. }
  99. return authResponse;
  100. }
  101. else
  102. {
  103. return cacheResponse;
  104. }
  105. }
  106. public virtual DnsDatagram Query(DnsDatagram request, bool serveStaleAndResetExpiry = false, bool findClosestNameServers = false)
  107. {
  108. DnsDatagram authResponse = null;
  109. if (!_skipDnsAppAuthoritativeRequestHandlers)
  110. {
  111. foreach (IDnsAuthoritativeRequestHandler requestHandler in _dnsApplicationManager.DnsAuthoritativeRequestHandlers)
  112. {
  113. try
  114. {
  115. authResponse = requestHandler.ProcessRequestAsync(request, new IPEndPoint(IPAddress.Any, 0), DnsTransportProtocol.Tcp, true).Sync();
  116. if (authResponse is not null)
  117. {
  118. if ((authResponse.RCODE != DnsResponseCode.NoError) || (authResponse.Answer.Count > 0) || (authResponse.Authority.Count == 0) || authResponse.IsFirstAuthoritySOA())
  119. return authResponse;
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. if (_log is not null)
  125. _log.Write(ex);
  126. }
  127. }
  128. }
  129. if (authResponse is null)
  130. {
  131. authResponse = _authZoneManager.Query(request, true);
  132. if (authResponse is not null)
  133. {
  134. if ((authResponse.RCODE != DnsResponseCode.NoError) || (authResponse.Answer.Count > 0) || (authResponse.Authority.Count == 0) || authResponse.IsFirstAuthoritySOA())
  135. return authResponse;
  136. }
  137. }
  138. DnsDatagram cacheResponse = _cacheZoneManager.Query(request, serveStaleAndResetExpiry, findClosestNameServers);
  139. if (cacheResponse is not null)
  140. {
  141. if ((cacheResponse.RCODE != DnsResponseCode.NoError) || (cacheResponse.Answer.Count > 0) || (cacheResponse.Authority.Count == 0) || cacheResponse.IsFirstAuthoritySOA())
  142. return cacheResponse;
  143. }
  144. if ((authResponse is not null) && (authResponse.Authority.Count > 0))
  145. {
  146. if ((cacheResponse is not null) && (cacheResponse.Authority.Count > 0))
  147. {
  148. DnsResourceRecord authResponseFirstAuthority = authResponse.FindFirstAuthorityRecord();
  149. DnsResourceRecord cacheResponseFirstAuthority = cacheResponse.FindFirstAuthorityRecord();
  150. if (cacheResponseFirstAuthority.Name.Length > authResponseFirstAuthority.Name.Length)
  151. return cacheResponse;
  152. }
  153. return authResponse;
  154. }
  155. else
  156. {
  157. return cacheResponse;
  158. }
  159. }
  160. public void CacheResponse(DnsDatagram response, bool isDnssecBadCache = false, string zoneCut = null)
  161. {
  162. _cacheZoneManager.CacheResponse(response, isDnssecBadCache, zoneCut);
  163. }
  164. #endregion
  165. }
  166. }