summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/dns_packet_parser.h
blob: 88c6a03b84ff759819ae973ae7b7a92b2546b3b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CLOUD_PRINT_GCP20_PROTOTYPE_DNS_PACKET_PARSER_H_
#define CLOUD_PRINT_GCP20_PROTOTYPE_DNS_PACKET_PARSER_H_

#include <string>

#include "net/dns/dns_protocol.h"
#include "net/dns/dns_response.h"

// Parsed response record.
struct DnsQueryRecord {
  DnsQueryRecord() : qtype(0), qclass(0) {}
  ~DnsQueryRecord() {}

  std::string qname;  // in dotted form
  uint16 qtype;
  uint16 qclass;
};

// Iterator to walk over records of the DNS response packet. Encapsulates
// |DnsRecordParser| object for using its functionality.
class DnsPacketParser {
 public:
  // Constructs an iterator to process the |packet| of given |length|.
  DnsPacketParser(const char* packet, size_t length);

  // Destroys the object.
  ~DnsPacketParser() {}

  bool IsValid() const { return record_parser_.IsValid() && is_header_read_; }

  // Returns |true| if no more bytes remain in the packet.
  bool AtEnd() const { return record_parser_.AtEnd(); }

  // Returns header of DNS packet.
  const net::dns_protocol::Header& header() const { return header_; }

  // Parses the next query record into |record|. Returns true if succeeded.
  bool ReadRecord(DnsQueryRecord* record);

  // Parses the next resource record into |record|. Returns true if succeeded.
  bool ReadRecord(net::DnsResourceRecord* record) {
    return record_parser_.ReadRecord(record);
  }

 private:
  // Returns current offset into the packet.
  size_t GetOffset() const { return record_parser_.GetOffset(); }

  // Parses a (possibly compressed) DNS name from the packet starting at
  // |pos|. Stores output (even partial) in |out| unless |out| is NULL. |out|
  // is stored in the dotted form, e.g., "example.com". Returns number of bytes
  // consumed or 0 on failure.
  // This is exposed to allow parsing compressed names within RRDATA for TYPEs
  // such as NS, CNAME, PTR, MX, SOA.
  // See RFC 1035 section 4.1.4.
  unsigned ReadName(std::string* out) const {
    return record_parser_.ReadName(packet_ + GetOffset(), out);
  }

  const char* packet_;
  size_t length_;

  // Contents parsed header_;
  net::dns_protocol::Header header_;
  bool is_header_read_;

  // Encapsulated parser.
  net::DnsRecordParser record_parser_;

  DISALLOW_COPY_AND_ASSIGN(DnsPacketParser);
};

#endif  // CLOUD_PRINT_GCP20_PROTOTYPE_DNS_PACKET_PARSER_H_