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 unsigned 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_MALFORMED_CNAME, // Could not parse CNAME out of RRDATA. |
| 89 DNS_NAME_MISMATCH, // Got an address but no ordered chain of CNAMEs |
| 90 // leads there. |
| 91 DNS_SIZE_MISMATCH, // Got an address but size does not match. |
| 92 DNS_CNAME_AFTER_ADDRESS, // Found CNAME after an address record. |
| 93 DNS_ADDRESS_TTL_MISMATCH, // TTL of all address records are not identical. |
| 94 DNS_NO_ADDRESSES, // No address records found. |
| 95 }; |
| 96 |
81 // Constructs an object with an IOBuffer large enough to read | 97 // Constructs an object with an IOBuffer large enough to read |
82 // one byte more than largest possible response, to detect malformed | 98 // one byte more than largest possible response, to detect malformed |
83 // responses. | 99 // responses. |
84 DnsResponse(); | 100 DnsResponse(); |
85 // Constructs response from |data|. Used for testing purposes only! | 101 // Constructs response from |data|. Used for testing purposes only! |
86 DnsResponse(const void* data, size_t length, size_t answer_offset); | 102 DnsResponse(const void* data, size_t length, size_t answer_offset); |
87 ~DnsResponse(); | 103 ~DnsResponse(); |
88 | 104 |
89 // Internal buffer accessor into which actual bytes of response will be | 105 // Internal buffer accessor into which actual bytes of response will be |
90 // read. | 106 // read. |
91 IOBufferWithSize* io_buffer() { return io_buffer_.get(); } | 107 IOBufferWithSize* io_buffer() { return io_buffer_.get(); } |
92 | 108 |
93 // Returns false if the packet is shorter than the header or does not match | 109 // Returns false if the packet is shorter than the header or does not match |
94 // |query| id or question. | 110 // |query| id or question. |
95 bool InitParse(int nbytes, const DnsQuery& query); | 111 bool InitParse(int nbytes, const DnsQuery& query); |
96 | 112 |
97 // Returns true if response is valid, that is, after successful InitParse. | 113 // Returns true if response is valid, that is, after successful InitParse. |
98 bool IsValid() const; | 114 bool IsValid() const; |
99 | 115 |
100 // All of the methods below are valid only if the response is valid. | 116 // All of the methods below are valid only if the response is valid. |
101 | 117 |
102 // Accessors for the header. | 118 // Accessors for the header. |
103 uint16 flags() const; // excluding rcode | 119 uint16 flags() const; // excluding rcode |
104 uint8 rcode() const; | 120 uint8 rcode() const; |
105 int answer_count() const; | 121 unsigned answer_count() const; |
106 | 122 |
107 // Accessors to the question. The qname is unparsed. | 123 // Accessors to the question. The qname is unparsed. |
108 base::StringPiece qname() const; | 124 base::StringPiece qname() const; |
109 uint16 qtype() const; | 125 uint16 qtype() const; |
110 | 126 |
111 // Returns qname in dotted format. | 127 // Returns qname in dotted format. |
112 std::string GetDottedName() const; | 128 std::string GetDottedName() const; |
113 | 129 |
114 // Returns an iterator to the resource records in the answer section. | 130 // Returns an iterator to the resource records in the answer section. |
115 // The iterator is valid only in the scope of the DnsResponse. | 131 // The iterator is valid only in the scope of the DnsResponse. |
116 // This operation is idempotent. | 132 // This operation is idempotent. |
117 DnsRecordParser Parser() const; | 133 DnsRecordParser Parser() const; |
118 | 134 |
| 135 // Extracts an AddressList from this response. Returns SUCCESS if succeeded. |
| 136 // Otherwise returns a detailed error number. |
| 137 Result ParseToAddressList(AddressList* addr_list, base::TimeDelta* ttl) const; |
| 138 |
119 private: | 139 private: |
120 // Convenience for header access. | 140 // Convenience for header access. |
121 const dns_protocol::Header* header() const; | 141 const dns_protocol::Header* header() const; |
122 | 142 |
123 // Buffer into which response bytes are read. | 143 // Buffer into which response bytes are read. |
124 scoped_refptr<IOBufferWithSize> io_buffer_; | 144 scoped_refptr<IOBufferWithSize> io_buffer_; |
125 | 145 |
126 // Iterator constructed after InitParse positioned at the answer section. | 146 // Iterator constructed after InitParse positioned at the answer section. |
127 // It is never updated afterwards, so can be used in accessors. | 147 // It is never updated afterwards, so can be used in accessors. |
128 DnsRecordParser parser_; | 148 DnsRecordParser parser_; |
129 | 149 |
130 DISALLOW_COPY_AND_ASSIGN(DnsResponse); | 150 DISALLOW_COPY_AND_ASSIGN(DnsResponse); |
131 }; | 151 }; |
132 | 152 |
133 } // namespace net | 153 } // namespace net |
134 | 154 |
135 #endif // NET_DNS_DNS_RESPONSE_H_ | 155 #endif // NET_DNS_DNS_RESPONSE_H_ |
OLD | NEW |