OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 14 #include "base/time.h" |
14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
15 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
| 20 class AddressList; |
19 class DnsQuery; | 21 class DnsQuery; |
20 class IOBufferWithSize; | 22 class IOBufferWithSize; |
21 | 23 |
22 namespace dns_protocol { | 24 namespace dns_protocol { |
23 struct Header; | 25 struct Header; |
24 } | 26 } |
25 | 27 |
26 // Parsed resource record. | 28 // Parsed resource record. |
27 struct NET_EXPORT_PRIVATE DnsResourceRecord { | 29 struct NET_EXPORT_PRIVATE DnsResourceRecord { |
28 DnsResourceRecord(); | 30 DnsResourceRecord(); |
(...skipping 25 matching lines...) Expand all Loading... |
54 // Returns current offset into the packet. | 56 // Returns current offset into the packet. |
55 size_t GetOffset() const { return cur_ - packet_; } | 57 size_t GetOffset() const { return cur_ - packet_; } |
56 | 58 |
57 // Parses a (possibly compressed) DNS name from the packet starting at | 59 // Parses a (possibly compressed) DNS name from the packet starting at |
58 // |pos|. Stores output (even partial) in |out| unless |out| is NULL. |out| | 60 // |pos|. Stores output (even partial) in |out| unless |out| is NULL. |out| |
59 // is stored in the dotted form, e.g., "example.com". Returns number of bytes | 61 // is stored in the dotted form, e.g., "example.com". Returns number of bytes |
60 // consumed or 0 on failure. | 62 // consumed or 0 on failure. |
61 // This is exposed to allow parsing compressed names within RRDATA for TYPEs | 63 // This is exposed to allow parsing compressed names within RRDATA for TYPEs |
62 // such as NS, CNAME, PTR, MX, SOA. | 64 // such as NS, CNAME, PTR, MX, SOA. |
63 // See RFC 1035 section 4.1.4. | 65 // See RFC 1035 section 4.1.4. |
64 int ParseName(const void* pos, std::string* out) const; | 66 int ReadName(const void* pos, std::string* out) const; |
65 | 67 |
66 // Parses the next resource record. Returns true if succeeded. | 68 // Parses the next resource record into |record|. Returns true if succeeded. |
67 bool ParseRecord(DnsResourceRecord* record); | 69 bool ReadRecord(DnsResourceRecord* record); |
68 | 70 |
69 private: | 71 private: |
70 const char* packet_; | 72 const char* packet_; |
71 size_t length_; | 73 size_t length_; |
72 // Current offset within the packet. | 74 // Current offset within the packet. |
73 const char* cur_; | 75 const char* cur_; |
74 }; | 76 }; |
75 | 77 |
76 // Buffer-holder for the DNS response allowing easy access to the header fields | 78 // Buffer-holder for the DNS response allowing easy access to the header fields |
77 // and resource records. After reading into |io_buffer| must call InitParse to | 79 // and resource records. After reading into |io_buffer| must call InitParse to |
78 // position the RR parser. | 80 // position the RR parser. |
79 class NET_EXPORT_PRIVATE DnsResponse { | 81 class NET_EXPORT_PRIVATE DnsResponse { |
80 public: | 82 public: |
| 83 // Possible results from ParseToAddressList |
| 84 enum Result { |
| 85 DNS_SUCCESS = 0, |
| 86 DNS_MALFORMED_RESPONSE, // DnsRecordParser failed before the end of |
| 87 // packet. |
| 88 DNS_NAME_MISMATCH, // Got an address but no ordered chain of CNAMEs |
| 89 // leads there. |
| 90 DNS_SIZE_MISMATCH, // Got an address but size does not match. |
| 91 DNS_CNAME_AFTER_ADDRESS, // Found CNAME after an address record. |
| 92 DNS_ADDRESS_TTL_MISMATCH, // TTL of all address records are not identical. |
| 93 DNS_NO_ADDRESSES, // No address records found. |
| 94 }; |
| 95 |
81 // Constructs an object with an IOBuffer large enough to read | 96 // Constructs an object with an IOBuffer large enough to read |
82 // one byte more than largest possible response, to detect malformed | 97 // one byte more than largest possible response, to detect malformed |
83 // responses. | 98 // responses. |
84 DnsResponse(); | 99 DnsResponse(); |
85 // Constructs response from |data|. Used for testing purposes only! | 100 // Constructs response from |data|. Used for testing purposes only! |
86 DnsResponse(const void* data, size_t length, size_t answer_offset); | 101 DnsResponse(const void* data, size_t length, size_t answer_offset); |
87 ~DnsResponse(); | 102 ~DnsResponse(); |
88 | 103 |
89 // Internal buffer accessor into which actual bytes of response will be | 104 // Internal buffer accessor into which actual bytes of response will be |
90 // read. | 105 // read. |
(...skipping 18 matching lines...) Expand all Loading... |
109 uint16 qtype() const; | 124 uint16 qtype() const; |
110 | 125 |
111 // Returns qname in dotted format. | 126 // Returns qname in dotted format. |
112 std::string GetDottedName() const; | 127 std::string GetDottedName() const; |
113 | 128 |
114 // Returns an iterator to the resource records in the answer section. | 129 // Returns an iterator to the resource records in the answer section. |
115 // The iterator is valid only in the scope of the DnsResponse. | 130 // The iterator is valid only in the scope of the DnsResponse. |
116 // This operation is idempotent. | 131 // This operation is idempotent. |
117 DnsRecordParser Parser() const; | 132 DnsRecordParser Parser() const; |
118 | 133 |
| 134 // Extracts an AddressList from this response. Returns OK if succeeded. |
| 135 // Otherwise returns a detailed error number. |
| 136 Result ParseToAddressList(AddressList* addr_list, base::TimeDelta* ttl) const; |
| 137 |
119 private: | 138 private: |
120 // Convenience for header access. | 139 // Convenience for header access. |
121 const dns_protocol::Header* header() const; | 140 const dns_protocol::Header* header() const; |
122 | 141 |
123 // Buffer into which response bytes are read. | 142 // Buffer into which response bytes are read. |
124 scoped_refptr<IOBufferWithSize> io_buffer_; | 143 scoped_refptr<IOBufferWithSize> io_buffer_; |
125 | 144 |
126 // Iterator constructed after InitParse positioned at the answer section. | 145 // Iterator constructed after InitParse positioned at the answer section. |
127 // It is never updated afterwards, so can be used in accessors. | 146 // It is never updated afterwards, so can be used in accessors. |
128 DnsRecordParser parser_; | 147 DnsRecordParser parser_; |
129 | 148 |
130 DISALLOW_COPY_AND_ASSIGN(DnsResponse); | 149 DISALLOW_COPY_AND_ASSIGN(DnsResponse); |
131 }; | 150 }; |
132 | 151 |
133 } // namespace net | 152 } // namespace net |
134 | 153 |
135 #endif // NET_DNS_DNS_RESPONSE_H_ | 154 #endif // NET_DNS_DNS_RESPONSE_H_ |
OLD | NEW |