dns_server_iterator.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DNS_DNS_SERVER_ITERATOR_H_
  5. #define NET_DNS_DNS_SERVER_ITERATOR_H_
  6. #include <stddef.h>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "net/base/net_export.h"
  10. #include "net/dns/public/secure_dns_mode.h"
  11. namespace net {
  12. class DnsSession;
  13. class ResolveContext;
  14. // Iterator used to get the next server to try for a DNS transaction.
  15. // Each iterator should be scoped to a single query. A new query, therefore,
  16. // requires a new iterator.
  17. //
  18. // Finds the first eligible server below the global failure limits
  19. // (|max_failures|), or if no eligible servers are below failure limits, the
  20. // eligible one with the oldest last failure. Global failures are tracked by
  21. // ResolveContext.
  22. //
  23. // If |session| goes out of date, this iterator will report that no attempts are
  24. // available and thus cease to return anything.
  25. class NET_EXPORT_PRIVATE DnsServerIterator {
  26. public:
  27. DnsServerIterator(size_t nameservers_size,
  28. size_t starting_index,
  29. int max_times_returned,
  30. int max_failures,
  31. const ResolveContext* resolve_context,
  32. const DnsSession* session);
  33. virtual ~DnsServerIterator();
  34. // Not copy or moveable.
  35. DnsServerIterator(const DnsServerIterator&) = delete;
  36. DnsServerIterator& operator=(const DnsServerIterator&) = delete;
  37. DnsServerIterator(DnsServerIterator&&) = delete;
  38. // Return the index of the next server to be attempted.
  39. // Should only be called if AttemptAvailable() is true.
  40. virtual size_t GetNextAttemptIndex() = 0;
  41. virtual bool AttemptAvailable() = 0;
  42. protected:
  43. // The number of times each server index was returned.
  44. std::vector<int> times_returned_;
  45. // The number of attempts that will be made per server.
  46. int max_times_returned_;
  47. // The failure limit before a server is skipped in the attempt ordering.
  48. // Servers past their failure limit will only be used once all remaining
  49. // servers are also past their failure limit.
  50. int max_failures_;
  51. raw_ptr<const ResolveContext> resolve_context_;
  52. // The first server index to try when GetNextAttemptIndex() is called.
  53. size_t next_index_;
  54. raw_ptr<const DnsSession> session_;
  55. };
  56. // Iterator used to get the next server to try for a DoH transaction.
  57. // Each iterator should be scoped to a single query. A new query, therefore,
  58. // requires a new iterator.
  59. //
  60. // Finds the first eligible server below the global failure limits
  61. // (|max_failures|), or if no eligible servers are below failure limits, the
  62. // eligible one with the oldest last failure. Global failures are tracked by
  63. // ResolveContext.
  64. //
  65. // Once a server is returned |max_times_returned| times, it is ignored.
  66. //
  67. // If in AUTOMATIC mode, DoH servers are only eligible if "available". See
  68. // GetDohServerAvailability() for details.
  69. class NET_EXPORT_PRIVATE DohDnsServerIterator : public DnsServerIterator {
  70. public:
  71. DohDnsServerIterator(size_t nameservers_size,
  72. size_t starting_index,
  73. int max_times_returned,
  74. int max_failures,
  75. const SecureDnsMode& secure_dns_mode,
  76. const ResolveContext* resolve_context,
  77. const DnsSession* session)
  78. : DnsServerIterator(nameservers_size,
  79. starting_index,
  80. max_times_returned,
  81. max_failures,
  82. resolve_context,
  83. session),
  84. secure_dns_mode_(secure_dns_mode) {}
  85. ~DohDnsServerIterator() override = default;
  86. // Not copy or moveable.
  87. DohDnsServerIterator(const DohDnsServerIterator&) = delete;
  88. DohDnsServerIterator& operator=(const DohDnsServerIterator&) = delete;
  89. DohDnsServerIterator(DohDnsServerIterator&&) = delete;
  90. size_t GetNextAttemptIndex() override;
  91. // Return true if any servers in the list still has attempts available.
  92. // False otherwise. An attempt is possible if any server, that is available,
  93. // is under max_times_returned_ tries.
  94. bool AttemptAvailable() override;
  95. private:
  96. SecureDnsMode secure_dns_mode_;
  97. };
  98. // Iterator used to get the next server to try for a classic DNS transaction.
  99. // Each iterator should be scoped to a single query. A new query, therefore,
  100. // requires a new iterator.
  101. //
  102. // Finds the first eligible server below the global failure limits
  103. // (|max_failures|), or if no eligible servers are below failure limits, the
  104. // eligible one with the oldest last failure. Global failures are tracked by
  105. // ResolveContext.
  106. // Once a server is returned |max_times_returned| times, it is ignored.
  107. class NET_EXPORT_PRIVATE ClassicDnsServerIterator : public DnsServerIterator {
  108. public:
  109. ClassicDnsServerIterator(size_t nameservers_size,
  110. size_t starting_index,
  111. int max_times_returned,
  112. int max_failures,
  113. const ResolveContext* resolve_context,
  114. const DnsSession* session)
  115. : DnsServerIterator(nameservers_size,
  116. starting_index,
  117. max_times_returned,
  118. max_failures,
  119. resolve_context,
  120. session) {}
  121. ~ClassicDnsServerIterator() override = default;
  122. // Not copy or moveable.
  123. ClassicDnsServerIterator(const ClassicDnsServerIterator&) = delete;
  124. ClassicDnsServerIterator& operator=(const ClassicDnsServerIterator&) = delete;
  125. ClassicDnsServerIterator(ClassicDnsServerIterator&&) = delete;
  126. size_t GetNextAttemptIndex() override;
  127. // Return true if any servers in the list still has attempts available.
  128. // False otherwise. An attempt is possible if any server is under
  129. // max_times_returned_ tries.
  130. bool AttemptAvailable() override;
  131. };
  132. } // namespace net
  133. #endif // NET_DNS_DNS_SERVER_ITERATOR_H_