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_BASE_DNS_QUERY_H_ | 5 #ifndef NET_BASE_DNS_QUERY_H_ |
6 #define NET_BASE_DNS_QUERY_H_ | 6 #define NET_BASE_DNS_QUERY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "net/base/address_family.h" | |
12 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
13 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
14 | 13 |
15 namespace net{ | 14 namespace net{ |
16 | 15 |
17 // A class that encapsulates bits and pieces related to DNS request processing. | 16 // Represents on-the-wire DNS query message as an object. |
18 class DnsQuery { | 17 class DnsQuery { |
19 public: | 18 public: |
20 // Constructs an object containing an IOBuffer with raw DNS query string | 19 // Constructs a query message from |dns_name| which *MUST* be in a valid |
21 // for |hostname| with the given |address_family|; |port| is here due to | 20 // DNS name format, and |qtype| which must be either kDNS_A or kDNS_AAA. |
22 // legacy -- getaddrinfo() takes service name, which is mapped to some | |
23 // port and returns sockaddr_in structures with ports filled in, so do we | |
24 // -- look at DnsResponse::Parse() to see where it is used. | |
25 | 21 |
26 // Every generated object has a random ID, hence two objects generated | 22 // Every generated object has a random ID, hence two objects generated |
27 // with the same set of constructor arguments are generally not equal; | 23 // with the same set of constructor arguments are generally not equal; |
28 // there is a 1/2^16 chance of them being equal due to size of |id_|. | 24 // there is a 1/2^16 chance of them being equal due to size of |id_|. |
29 DnsQuery(const std::string& hostname, AddressFamily address_family, int port); | 25 DnsQuery(const std::string& dns_name, uint16 qtype); |
30 ~DnsQuery(); | 26 ~DnsQuery(); |
31 | 27 |
32 // Returns true if the constructed object was valid. | 28 // Clones |this| verbatim with ID field of the header regenerated. |
33 bool IsValid() const { return io_buffer_.get() != NULL; } | 29 DnsQuery* CloneWithNewId() const; |
34 | 30 |
35 // DnsQuery field accessors. These should only be called on an object | 31 // DnsQuery field accessors. |
36 // for whom |IsValid| is true. | |
37 int port() const; | |
38 uint16 id() const; | 32 uint16 id() const; |
39 uint16 qtype() const; | 33 uint16 qtype() const; |
40 AddressFamily address_family() const; | 34 |
41 const std::string& hostname() const; | 35 // Returns the size of the Question section of the query. Used when |
| 36 // matching the response. |
| 37 size_t question_size() const; |
| 38 |
| 39 // Returns pointer to the Question section of the query. Used when |
| 40 // matching the response. |
| 41 const char* question_data() const; |
42 | 42 |
43 // IOBuffer accessor to be used for writing out the query. | 43 // IOBuffer accessor to be used for writing out the query. |
44 IOBufferWithSize* io_buffer() const; | 44 IOBufferWithSize* io_buffer() const { return io_buffer_; } |
45 | 45 |
46 private: | 46 private: |
47 // Port to be used by corresponding DnsResponse when filling sockaddr_ins | 47 // Copy constructor to be used only by CloneWithNewId; it's not public |
48 // to be returned. | 48 // since there is a semantic difference -- this does not construct an |
49 int port_; | 49 // exact copy! |
| 50 DnsQuery(const DnsQuery& rhs); |
50 | 51 |
51 // ID of the query. | 52 // Not implemented; just to prevent the assignment. |
52 uint16 id_; | 53 void operator=(const DnsQuery&); |
53 | 54 |
54 // Type of query, currently, either A or AAAA. | 55 // Randomizes ID field of the query message. |
55 uint16 qtype_; | 56 void RandomizeId(); |
56 | 57 |
57 // Address family of the query; used when constructing new object from | 58 // Size of the DNS name (*NOT* hostname) we are trying to resolve; used |
58 // this one. | 59 // to calculate offsets. |
59 AddressFamily address_family_; | 60 size_t dns_name_size_; |
60 | |
61 // Hostname that we are trying to resolve. | |
62 std::string hostname_; | |
63 | 61 |
64 // Contains query bytes to be consumed by higher level Write() call. | 62 // Contains query bytes to be consumed by higher level Write() call. |
65 scoped_refptr<IOBufferWithSize> io_buffer_; | 63 scoped_refptr<IOBufferWithSize> io_buffer_; |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(DnsQuery); | |
68 }; | 64 }; |
69 | 65 |
70 } // namespace net | 66 } // namespace net |
71 | 67 |
72 #endif // NET_BASE_DNS_QUERY_H_ | 68 #endif // NET_BASE_DNS_QUERY_H_ |
OLD | NEW |