OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DNS_DNS_QUERY_H_ | 5 #ifndef NET_DNS_DNS_QUERY_H_ |
6 #define NET_DNS_DNS_QUERY_H_ | 6 #define NET_DNS_DNS_QUERY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include "base/basictypes.h" |
10 | |
11 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/string_piece.h" |
12 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
13 #include "net/base/rand_callback.h" | |
14 | 13 |
15 namespace net { | 14 namespace net { |
16 | 15 |
17 class IOBufferWithSize; | 16 class IOBufferWithSize; |
18 | 17 |
19 // Represents on-the-wire DNS query message as an object. | 18 // Represents on-the-wire DNS query message as an object. |
| 19 // TODO(szym): add support for the OPT pseudo-RR (EDNS0/DNSSEC). |
20 class NET_EXPORT_PRIVATE DnsQuery { | 20 class NET_EXPORT_PRIVATE DnsQuery { |
21 public: | 21 public: |
22 // Constructs a query message from |qname| which *MUST* be in a valid | 22 // Constructs a query message from |qname| which *MUST* be in a valid |
23 // DNS name format, and |qtype| which must be either kDNS_A or kDNS_AAAA. | 23 // DNS name format, and |qtype|. The qclass is set to IN. |
24 | 24 DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype); |
25 // Every generated object has a random ID, hence two objects generated | |
26 // with the same set of constructor arguments are generally not equal; | |
27 // there is a 1/2^16 chance of them being equal due to size of |id_|. | |
28 DnsQuery(const std::string& qname, | |
29 uint16 qtype, | |
30 const RandIntCallback& rand_int_cb); | |
31 ~DnsQuery(); | 25 ~DnsQuery(); |
32 | 26 |
33 // Clones |this| verbatim, with ID field of the header regenerated. | 27 // Clones |this| verbatim, with ID field of the header set to |id|. |
34 DnsQuery* CloneWithNewId() const; | 28 DnsQuery* CloneWithNewId(uint16 id) const; |
35 | 29 |
36 // DnsQuery field accessors. | 30 // DnsQuery field accessors. |
37 uint16 id() const; | 31 uint16 id() const; |
| 32 base::StringPiece qname() const; |
38 uint16 qtype() const; | 33 uint16 qtype() const; |
39 | 34 |
40 // Returns the size of the Question section of the query. Used when | 35 // Returns the Question section of the query. Used when matching the |
41 // matching the response. | 36 // response. |
42 size_t question_size() const; | 37 base::StringPiece question() const; |
43 | |
44 // Returns pointer to the Question section of the query. Used when | |
45 // matching the response. | |
46 const char* question_data() const; | |
47 | 38 |
48 // IOBuffer accessor to be used for writing out the query. | 39 // IOBuffer accessor to be used for writing out the query. |
49 IOBufferWithSize* io_buffer() const { return io_buffer_; } | 40 IOBufferWithSize* io_buffer() const { return io_buffer_; } |
50 | 41 |
51 private: | 42 private: |
52 const std::string qname() const; | 43 DnsQuery(const DnsQuery& orig, uint16 id); |
53 | |
54 // Randomizes ID field of the query message. | |
55 void RandomizeId(); | |
56 | 44 |
57 // Size of the DNS name (*NOT* hostname) we are trying to resolve; used | 45 // Size of the DNS name (*NOT* hostname) we are trying to resolve; used |
58 // to calculate offsets. | 46 // to calculate offsets. |
59 size_t qname_size_; | 47 size_t qname_size_; |
60 | 48 |
61 // Contains query bytes to be consumed by higher level Write() call. | 49 // Contains query bytes to be consumed by higher level Write() call. |
62 scoped_refptr<IOBufferWithSize> io_buffer_; | 50 scoped_refptr<IOBufferWithSize> io_buffer_; |
63 | 51 |
64 // PRNG function for generating IDs. | |
65 RandIntCallback rand_int_cb_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(DnsQuery); | 52 DISALLOW_COPY_AND_ASSIGN(DnsQuery); |
68 }; | 53 }; |
69 | 54 |
70 } // namespace net | 55 } // namespace net |
71 | 56 |
72 #endif // NET_DNS_DNS_QUERY_H_ | 57 #endif // NET_DNS_DNS_QUERY_H_ |
OLD | NEW |