ResolverDnsCache.cs 8.3 KB

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