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