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_RESPONSE_H_ | 5 #ifndef NET_DNS_DNS_RESPONSE_H_ |
6 #define NET_DNS_DNS_RESPONSE_H_ | 6 #define NET_DNS_DNS_RESPONSE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
13 #include "base/string_piece.h" | |
14 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
15 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
16 | 12 |
17 namespace net { | 13 namespace net{ |
18 | 14 |
19 class DnsQuery; | 15 class DnsQuery; |
20 class IOBufferWithSize; | 16 class IOBufferWithSize; |
21 | 17 |
22 namespace dns_protocol { | 18 // Represents on-the-wire DNS response as an object; allows extracting |
23 struct Header; | 19 // records. |
24 } | |
25 | |
26 // Parsed resource record. | |
27 struct NET_EXPORT_PRIVATE DnsResourceRecord { | |
28 std::string name; // in dotted form | |
29 uint16 type; | |
30 uint16 klass; | |
31 uint32 ttl; | |
32 base::StringPiece rdata; // points to the original response buffer | |
33 }; | |
34 | |
35 // Iterator to walk over resource records of the DNS response packet. | |
36 class NET_EXPORT_PRIVATE DnsRecordParser { | |
37 public: | |
38 // Construct an uninitialized iterator. | |
39 DnsRecordParser(); | |
40 | |
41 // Construct an iterator to process the |packet| of given |length|. | |
42 // |offset| points to the beginning of the answer section. | |
43 DnsRecordParser(const void* packet, size_t length, size_t offset); | |
44 | |
45 // Returns |true| if initialized. | |
46 bool IsValid() const { return packet_ != NULL; } | |
47 | |
48 // Returns |true| if no more bytes remain in the packet. | |
49 bool AtEnd() const { return cur_ == packet_ + length_; } | |
50 | |
51 // Parses a (possibly compressed) DNS name from the packet starting at | |
52 // |pos|. Stores output (even partial) in |out| unless |out| is NULL. |out| | |
53 // is stored in the dotted form, e.g., "example.com". Returns number of bytes | |
54 // consumed or 0 on failure. | |
55 // This is exposed to allow parsing compressed names within RRDATA for TYPEs | |
56 // such as NS, CNAME, PTR, MX, SOA. | |
57 // See RFC 1035 section 4.1.4. | |
58 int ParseName(const void* pos, std::string* out) const; | |
59 | |
60 // Parses the next resource record. Returns true if succeeded. | |
61 bool ParseRecord(DnsResourceRecord* record); | |
62 | |
63 private: | |
64 const char* packet_; | |
65 size_t length_; | |
66 // Current offset within the packet. | |
67 const char* cur_; | |
68 }; | |
69 | |
70 // Buffer-holder for the DNS response allowing easy access to the header fields | |
71 // and resource records. After reading into |io_buffer| must call InitParse to | |
72 // position the RR parser. | |
73 class NET_EXPORT_PRIVATE DnsResponse { | 20 class NET_EXPORT_PRIVATE DnsResponse { |
74 public: | 21 public: |
75 // Constructs an object with an IOBuffer large enough to read | 22 // Constructs an object with an IOBuffer large enough to read |
76 // one byte more than largest possible response, to detect malformed | 23 // one byte more than largest possible response, to detect malformed |
77 // responses. | 24 // responses; |query| is a pointer to the DnsQuery for which |this| |
78 DnsResponse(); | 25 // is supposed to be a response. |
79 // Constructs response from |data|. Used for testing purposes only! | 26 explicit DnsResponse(DnsQuery* query); |
80 DnsResponse(const void* data, size_t length, size_t answer_offset); | |
81 ~DnsResponse(); | 27 ~DnsResponse(); |
82 | 28 |
83 // Internal buffer accessor into which actual bytes of response will be | 29 // Internal buffer accessor into which actual bytes of response will be |
84 // read. | 30 // read. |
85 IOBufferWithSize* io_buffer() { return io_buffer_.get(); } | 31 IOBufferWithSize* io_buffer() { return io_buffer_.get(); } |
86 | 32 |
87 // Returns false if the packet is shorter than the header or does not match | 33 // Parses response of size nbytes and puts address into |ip_addresses|, |
88 // |query| id or question. | 34 // returns net_error code in case of failure. |
89 bool InitParse(int nbytes, const DnsQuery& query); | 35 int Parse(int nbytes, IPAddressList* ip_addresses); |
90 | |
91 // Accessors for the header. | |
92 uint8 flags0() const; // first byte of flags | |
93 uint8 flags1() const; // second byte of flags excluding rcode | |
94 uint8 rcode() const; | |
95 int answer_count() const; | |
96 | |
97 // Returns an iterator to the resource records in the answer section. Must be | |
98 // called after InitParse. The iterator is valid only in the scope of the | |
99 // DnsResponse. | |
100 DnsRecordParser Parser() const; | |
101 | 36 |
102 private: | 37 private: |
103 // Convenience for header access. | 38 // The matching query; |this| is the response for |query_|. We do not |
104 const dns_protocol::Header* header() const; | 39 // own it, lifetime of |this| should be within the limits of lifetime of |
| 40 // |query_|. |
| 41 const DnsQuery* const query_; |
105 | 42 |
106 // Buffer into which response bytes are read. | 43 // Buffer into which response bytes are read. |
107 scoped_refptr<IOBufferWithSize> io_buffer_; | 44 scoped_refptr<IOBufferWithSize> io_buffer_; |
108 | 45 |
109 // Iterator constructed after InitParse positioned at the answer section. | |
110 DnsRecordParser parser_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(DnsResponse); | 46 DISALLOW_COPY_AND_ASSIGN(DnsResponse); |
113 }; | 47 }; |
114 | 48 |
115 } // namespace net | 49 } // namespace net |
116 | 50 |
117 #endif // NET_DNS_DNS_RESPONSE_H_ | 51 #endif // NET_DNS_DNS_RESPONSE_H_ |
OLD | NEW |