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