ResolverDnsCache.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 = DnsApplicationQueryClosestDelegation(request);
  82. if (authResponse is null)
  83. authResponse = _dnsServer.AuthZoneManager.QueryClosestDelegation(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 = null;
  104. if (!_skipDnsAppAuthoritativeRequestHandlers)
  105. {
  106. foreach (IDnsAuthoritativeRequestHandler requestHandler in _dnsServer.DnsApplicationManager.DnsAuthoritativeRequestHandlers)
  107. {
  108. try
  109. {
  110. authResponse = requestHandler.ProcessRequestAsync(request, new IPEndPoint(IPAddress.Any, 0), DnsTransportProtocol.Tcp, true).Sync();
  111. if (authResponse is not null)
  112. {
  113. if ((authResponse.RCODE != DnsResponseCode.NoError) || (authResponse.Answer.Count > 0) || (authResponse.Authority.Count == 0) || authResponse.IsFirstAuthoritySOA())
  114. return authResponse;
  115. }
  116. }
  117. catch (DnsClientException ex)
  118. {
  119. _dnsServer.ResolverLogManager?.Write(ex);
  120. }
  121. catch (Exception ex)
  122. {
  123. _dnsServer.LogManager?.Write(ex);
  124. }
  125. }
  126. }
  127. if (authResponse is null)
  128. {
  129. authResponse = _dnsServer.AuthZoneManager.Query(request, true);
  130. if (authResponse is not null)
  131. {
  132. if ((authResponse.RCODE != DnsResponseCode.NoError) || (authResponse.Answer.Count > 0) || (authResponse.Authority.Count == 0) || authResponse.IsFirstAuthoritySOA())
  133. return authResponse;
  134. }
  135. }
  136. DnsDatagram cacheResponse = _dnsServer.CacheZoneManager.Query(request, serveStale, findClosestNameServers, resetExpiry);
  137. if (cacheResponse is not null)
  138. {
  139. if ((cacheResponse.RCODE != DnsResponseCode.NoError) || (cacheResponse.Answer.Count > 0) || (cacheResponse.Authority.Count == 0) || cacheResponse.IsFirstAuthoritySOA())
  140. return cacheResponse;
  141. }
  142. if ((authResponse is not null) && (authResponse.Authority.Count > 0))
  143. {
  144. if ((cacheResponse is not null) && (cacheResponse.Authority.Count > 0))
  145. {
  146. DnsResourceRecord authResponseFirstAuthority = authResponse.FindFirstAuthorityRecord();
  147. DnsResourceRecord cacheResponseFirstAuthority = cacheResponse.FindFirstAuthorityRecord();
  148. if (cacheResponseFirstAuthority.Name.Length > authResponseFirstAuthority.Name.Length)
  149. return cacheResponse;
  150. }
  151. return authResponse;
  152. }
  153. else
  154. {
  155. return cacheResponse;
  156. }
  157. }
  158. public void CacheResponse(DnsDatagram response, bool isDnssecBadCache = false, string zoneCut = null)
  159. {
  160. _dnsServer.CacheZoneManager.CacheResponse(response, isDnssecBadCache, zoneCut);
  161. }
  162. #endregion
  163. }
  164. }