dns_transaction.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2012 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_TRANSACTION_H_
  5. #define NET_DNS_DNS_TRANSACTION_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/time/time.h"
  13. #include "net/base/request_priority.h"
  14. #include "net/dns/opt_record_rdata.h"
  15. #include "net/dns/public/secure_dns_mode.h"
  16. #include "net/dns/record_rdata.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. #include "url/gurl.h"
  19. namespace net {
  20. class DnsResponse;
  21. class DnsSession;
  22. class NetLogWithSource;
  23. class ResolveContext;
  24. // The hostname probed by CreateDohProbeRunner().
  25. inline constexpr base::StringPiece kDohProbeHostname = "www.gstatic.com";
  26. // DnsTransaction implements a stub DNS resolver as defined in RFC 1034.
  27. // The DnsTransaction takes care of retransmissions, name server fallback (or
  28. // round-robin), suffix search, and simple response validation ("does it match
  29. // the query") to fight poisoning.
  30. //
  31. // Destroying DnsTransaction cancels the underlying network effort.
  32. class NET_EXPORT_PRIVATE DnsTransaction {
  33. public:
  34. // Called with the response or nullptr if no matching response was received.
  35. // Note that the `GetDottedName()` of the response may be different than the
  36. // original `hostname` (passed to `DnsTransactionFactory::CreateTransaction()`
  37. // as a result of suffix search.
  38. using ResponseCallback =
  39. base::OnceCallback<void(int neterror, const DnsResponse* response)>;
  40. virtual ~DnsTransaction() = default;
  41. // Returns the original |hostname|.
  42. virtual const std::string& GetHostname() const = 0;
  43. // Returns the |qtype|.
  44. virtual uint16_t GetType() const = 0;
  45. // Starts the transaction. Always completes asynchronously.
  46. virtual void Start(ResponseCallback callback) = 0;
  47. virtual void SetRequestPriority(RequestPriority priority) = 0;
  48. };
  49. // Startable/Cancellable object to represent a DNS probe sequence.
  50. class DnsProbeRunner {
  51. public:
  52. // Destruction cancels the probes.
  53. virtual ~DnsProbeRunner() = default;
  54. // Starts all applicable probes that are not already running. May be called
  55. // multiple times, but should not be called after destruction of the
  56. // DnsTransactionFactory.
  57. //
  58. // Set |network_change| to indicate if this start or restart was triggered by
  59. // a network connection change. Only used for logging and metrics.
  60. virtual void Start(bool network_change) = 0;
  61. // Gets the delay until the next scheduled probe to the specified DoH server.
  62. // Returns base::TimeDelta() if no probe scheduled.
  63. virtual base::TimeDelta GetDelayUntilNextProbeForTest(
  64. size_t doh_server_index) const = 0;
  65. };
  66. // Creates DnsTransaction which performs asynchronous DNS search.
  67. // It does NOT perform caching, aggregation or prioritization of transactions.
  68. //
  69. // Destroying the factory does NOT affect any already created DnsTransactions.
  70. //
  71. // DnsProbeRunners, however, will safely abort themselves on destruction of
  72. // their creating factory, and they should only be started or restarted while
  73. // the factory is still alive.
  74. class NET_EXPORT_PRIVATE DnsTransactionFactory {
  75. public:
  76. DnsTransactionFactory();
  77. virtual ~DnsTransactionFactory();
  78. // Creates DnsTransaction for the given |hostname| and |qtype| (assuming
  79. // QCLASS is IN). |hostname| should be in the dotted form. A dot at the end
  80. // implies the domain name is fully-qualified and will be exempt from suffix
  81. // search. |hostname| should not be an IP literal.
  82. //
  83. // The |net_log| is used as the parent log.
  84. //
  85. // |secure| specifies whether DNS lookups should be performed using DNS-over-
  86. // HTTPS (DoH) or using plaintext DNS.
  87. //
  88. // When |fast_timeout| is true, the transaction will timeout quickly after
  89. // making its DNS attempts, without necessarily waiting long enough to allow
  90. // slower-than-average requests to complete. Intended as an optimization for
  91. // cases where the caller has reasonable fallback options to the transaction
  92. // and it would be beneficial to move on to those options sooner on signals
  93. // that the transaction is potentially slow or problematic.
  94. [[nodiscard]] virtual std::unique_ptr<DnsTransaction> CreateTransaction(
  95. std::string hostname,
  96. uint16_t qtype,
  97. const NetLogWithSource& net_log,
  98. bool secure,
  99. SecureDnsMode secure_dns_mode,
  100. ResolveContext* resolve_context,
  101. bool fast_timeout) = 0;
  102. // Creates a runner to run the DoH probe sequence for all configured DoH
  103. // resolvers.
  104. [[nodiscard]] virtual std::unique_ptr<DnsProbeRunner> CreateDohProbeRunner(
  105. ResolveContext* resolve_context) = 0;
  106. // The given EDNS0 option will be included in all DNS queries performed by
  107. // transactions from this factory.
  108. virtual void AddEDNSOption(std::unique_ptr<OptRecordRdata::Opt> opt) = 0;
  109. // Returns the default SecureDnsMode in the config.
  110. virtual SecureDnsMode GetSecureDnsModeForTest() = 0;
  111. // Creates a DnsTransactionFactory which creates DnsTransactionImpl using the
  112. // |session|.
  113. [[nodiscard]] static std::unique_ptr<DnsTransactionFactory> CreateFactory(
  114. DnsSession* session);
  115. base::WeakPtrFactory<DnsTransactionFactory> weak_factory_{this};
  116. };
  117. } // namespace net
  118. #endif // NET_DNS_DNS_TRANSACTION_H_