dns_query.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2011 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_QUERY_H_
  5. #define NET_DNS_DNS_QUERY_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/ref_counted.h"
  11. #include "base/strings/string_piece.h"
  12. #include "net/base/net_export.h"
  13. namespace base {
  14. class BigEndianReader;
  15. } // namespace base
  16. namespace net {
  17. class OptRecordRdata;
  18. namespace dns_protocol {
  19. struct Header;
  20. } // namespace dns_protocol
  21. class IOBufferWithSize;
  22. // Represents on-the-wire DNS query message as an object.
  23. class NET_EXPORT_PRIVATE DnsQuery {
  24. public:
  25. enum class PaddingStrategy {
  26. // Query will not be padded. Recommended strategy when query will not be
  27. // encrypted.
  28. NONE,
  29. // Query will be padded to the next multiple of 128 octets. Recommended
  30. // strategy (per RFC 8467) when query will be encrypted, e.g. through
  31. // DNS-over-HTTPS.
  32. BLOCK_LENGTH_128,
  33. };
  34. // Constructs a query message from |qname| which *MUST* be in a valid
  35. // DNS name format, and |qtype|. The qclass is set to IN.
  36. // If |opt_rdata| is not null, an OPT record will be added to the "Additional"
  37. // section of the query.
  38. DnsQuery(uint16_t id,
  39. const base::StringPiece& qname,
  40. uint16_t qtype,
  41. const OptRecordRdata* opt_rdata = nullptr,
  42. PaddingStrategy padding_strategy = PaddingStrategy::NONE);
  43. // Constructs an empty query from a raw packet in |buffer|. If the raw packet
  44. // represents a valid DNS query in the wire format (RFC 1035), Parse() will
  45. // populate the empty query.
  46. explicit DnsQuery(scoped_refptr<IOBufferWithSize> buffer);
  47. // Copies are constructed with an independent cloned, not mirrored, buffer.
  48. DnsQuery(const DnsQuery& query);
  49. DnsQuery& operator=(const DnsQuery& query);
  50. ~DnsQuery();
  51. // Clones |this| verbatim, with ID field of the header set to |id|.
  52. std::unique_ptr<DnsQuery> CloneWithNewId(uint16_t id) const;
  53. // Returns true and populates the query if the internally stored raw packet
  54. // can be parsed. This should only be called when DnsQuery is constructed from
  55. // the raw buffer.
  56. // |valid_bytes| indicates the number of initialized bytes in the raw buffer.
  57. // E.g. if the buffer holds a packet received from the network, the buffer may
  58. // be allocated with the maximum size of a UDP packet, but |valid_bytes|
  59. // indicates the number of bytes actually received from the network. If the
  60. // parsing requires reading more than the number of initialized bytes, this
  61. // method fails and returns false.
  62. bool Parse(size_t valid_bytes);
  63. // DnsQuery field accessors.
  64. uint16_t id() const;
  65. base::StringPiece qname() const;
  66. uint16_t qtype() const;
  67. // Returns the Question section of the query. Used when matching the
  68. // response.
  69. base::StringPiece question() const;
  70. // Returns the size of the question section.
  71. size_t question_size() const;
  72. // IOBuffer accessor to be used for writing out the query. The buffer has
  73. // the same byte layout as the DNS query wire format.
  74. IOBufferWithSize* io_buffer() const { return io_buffer_.get(); }
  75. void set_flags(uint16_t flags);
  76. private:
  77. DnsQuery(const DnsQuery& orig, uint16_t id);
  78. void CopyFrom(const DnsQuery& orig);
  79. bool ReadHeader(base::BigEndianReader* reader, dns_protocol::Header* out);
  80. // After read, |out| is in the DNS format, e.g.
  81. // "\x03""www""\x08""chromium""\x03""com""\x00". Use DNSDomainToString to
  82. // convert to the dotted format "www.chromium.com" with no trailing dot.
  83. bool ReadName(base::BigEndianReader* reader, std::string* out);
  84. // Size of the DNS name (*NOT* hostname) we are trying to resolve; used
  85. // to calculate offsets.
  86. size_t qname_size_ = 0;
  87. // Contains query bytes to be consumed by higher level Write() call.
  88. scoped_refptr<IOBufferWithSize> io_buffer_;
  89. // Pointer to the dns header section.
  90. dns_protocol::Header* header_ = nullptr;
  91. };
  92. } // namespace net
  93. #endif // NET_DNS_DNS_QUERY_H_