ResolverDnsCache.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2020 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.ZoneManagers;
  16. using TechnitiumLibrary.Net.Dns;
  17. namespace DnsServerCore.Dns
  18. {
  19. class ResolverDnsCache : IDnsCache
  20. {
  21. #region variables
  22. readonly protected AuthZoneManager _authZoneManager;
  23. readonly protected CacheZoneManager _cacheZoneManager;
  24. #endregion
  25. #region constructor
  26. public ResolverDnsCache(AuthZoneManager authZoneManager, CacheZoneManager cacheZoneManager)
  27. {
  28. _authZoneManager = authZoneManager;
  29. _cacheZoneManager = cacheZoneManager;
  30. }
  31. #endregion
  32. #region public
  33. public virtual DnsDatagram Query(DnsDatagram request, bool serveStale = false)
  34. {
  35. DnsDatagram authResponse = _authZoneManager.Query(request);
  36. if (authResponse.Answer.Count > 0)
  37. return authResponse;
  38. DnsDatagram cacheResponse = _cacheZoneManager.Query(request, serveStale);
  39. if (cacheResponse.Answer.Count > 0)
  40. return cacheResponse;
  41. if ((authResponse.Authority.Count > 0) && (cacheResponse.Authority.Count > 0))
  42. {
  43. if (authResponse.Authority[0].Name.Length > cacheResponse.Authority[0].Name.Length)
  44. return authResponse;
  45. return cacheResponse;
  46. }
  47. else if (authResponse.Authority.Count > 0)
  48. {
  49. return authResponse;
  50. }
  51. else
  52. {
  53. return cacheResponse;
  54. }
  55. }
  56. public void CacheResponse(DnsDatagram response)
  57. {
  58. _cacheZoneManager.CacheResponse(response);
  59. }
  60. #endregion
  61. }
  62. }