| 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" | 
|   12  |   16  | 
|   13 namespace net{ |   17 namespace net { | 
|   14  |   18  | 
|   15 class DnsQuery; |   19 class DnsQuery; | 
|   16 class IOBufferWithSize; |   20 class IOBufferWithSize; | 
|   17  |   21  | 
|   18 // Represents on-the-wire DNS response as an object; allows extracting |   22 namespace dns_protocol { | 
|   19 // records. |   23 struct Header; | 
 |   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. | 
|   20 class NET_EXPORT_PRIVATE DnsResponse { |   73 class NET_EXPORT_PRIVATE DnsResponse { | 
|   21  public: |   74  public: | 
|   22   // Constructs an object with an IOBuffer large enough to read |   75   // Constructs an object with an IOBuffer large enough to read | 
|   23   // one byte more than largest possible response, to detect malformed |   76   // one byte more than largest possible response, to detect malformed | 
|   24   // responses; |query| is a pointer to the DnsQuery for which |this| |   77   // responses. | 
|   25   // is supposed to be a response. |   78   DnsResponse(); | 
|   26   explicit DnsResponse(DnsQuery* query); |   79   // Constructs response from |data|. Used for testing purposes only! | 
 |   80   DnsResponse(const void* data, size_t length, size_t answer_offset); | 
|   27   ~DnsResponse(); |   81   ~DnsResponse(); | 
|   28  |   82  | 
|   29   // Internal buffer accessor into which actual bytes of response will be |   83   // Internal buffer accessor into which actual bytes of response will be | 
|   30   // read. |   84   // read. | 
|   31   IOBufferWithSize* io_buffer() { return io_buffer_.get(); } |   85   IOBufferWithSize* io_buffer() { return io_buffer_.get(); } | 
|   32  |   86  | 
|   33   // Parses response of size nbytes and puts address into |ip_addresses|, |   87   // Returns false if the packet is shorter than the header or does not match | 
|   34   // returns net_error code in case of failure. |   88   // |query| id or question. | 
|   35   int Parse(int nbytes, IPAddressList* ip_addresses); |   89   bool InitParse(int nbytes, const DnsQuery& query); | 
 |   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; | 
|   36  |  101  | 
|   37  private: |  102  private: | 
|   38   // The matching query; |this| is the response for |query_|.  We do not |  103   // Convenience for header access. | 
|   39   // own it, lifetime of |this| should be within the limits of lifetime of |  104   const dns_protocol::Header* header() const; | 
|   40   // |query_|. |  | 
|   41   const DnsQuery* const query_; |  | 
|   42  |  105  | 
|   43   // Buffer into which response bytes are read. |  106   // Buffer into which response bytes are read. | 
|   44   scoped_refptr<IOBufferWithSize> io_buffer_; |  107   scoped_refptr<IOBufferWithSize> io_buffer_; | 
|   45  |  108  | 
 |  109   // Iterator constructed after InitParse positioned at the answer section. | 
 |  110   DnsRecordParser parser_; | 
 |  111  | 
|   46   DISALLOW_COPY_AND_ASSIGN(DnsResponse); |  112   DISALLOW_COPY_AND_ASSIGN(DnsResponse); | 
|   47 }; |  113 }; | 
|   48  |  114  | 
|   49 }  // namespace net |  115 }  // namespace net | 
|   50  |  116  | 
|   51 #endif  // NET_DNS_DNS_RESPONSE_H_ |  117 #endif  // NET_DNS_DNS_RESPONSE_H_ | 
| OLD | NEW |