EpcTools
An event based multi-threaded C++ development framework.
dnsparser.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2017 Sprint
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __DNSPARSER_H
19 #define __DNSPARSER_H
20 
22 
23 #include "estring.h"
24 #include "dnsquery.h"
25 
26 #define LABEL_LENGTH(a) ((int)(((a[0] & 0xc0) == 0) ? a[0] & 0x3f : -1))
27 #define LABEL_OFFSET(a) ((int)(((a[0] & 0xc0) == 0xc0) ? ((a[0] & 0x3f) << 8) + a[1] : -1))
28 #define GET_INT16(a,b) ((((int)(a[b])) << 8) + (int)a[b+1])
29 #define GET_INT32(a,b) ((((int)a[b]) << 24) + (((int)a[b+1]) << 16) + (((int)a[b+2]) << 8) + ((int)a[b+3]))
30 
31 namespace DNS
32 {
33  class MessageBuffer
34  {
35  public:
36  MessageBuffer()
37  {
38  setData( NULL, 0 );
39  }
40 
41  MessageBuffer( unsigned char *data, int len )
42  {
43  setData( data, len );
44  }
45 
46  MessageBuffer &setData( unsigned char *data, int len )
47  {
48  m_data = data;
49  m_len = len;
50  m_ofs = 0;
51 
52  return *this;
53  }
54 
55  unsigned char *getPointer() { return m_ofs >= m_len ? NULL : m_data + m_ofs; }
56  unsigned char *getPointer( int ofs ) { return ofs >= m_len ? NULL : m_data + ofs; }
57  bool validateLength( int ofs, int len ) { return (ofs + len) >= m_len ? false : true; }
58 
59  int incrementOffset( int len ) { return m_ofs = m_ofs + len > m_len ? m_len : m_ofs + len; }
60  int getOffset() { return m_ofs; }
61  int setOffset( int ofs ) { return m_ofs = ofs; }
62  int calculateOffset( unsigned char *ptr ) { return (int)(ptr - m_data); }
63 
64  bool isValid() { return m_data && m_len > 0; }
65 
66  private:
67 
68  unsigned char * m_data;
69  int m_len;
70  int m_ofs;
71  };
72 
73  class Parser
74  {
75  public:
76  Parser( QueryPtr &q, unsigned char *rdata, int rlen );
77 
78  void parse();
79 
80  private:
81  Parser() {}
82 
83  Question* parseQuestion();
84  ResourceRecord* parseResourceRecord();
85  ResourceRecord* parseRR();
86  ResourceRecord* parseA();
87  ResourceRecord* parseNS();
88  ResourceRecord* parseCNAME();
89  ResourceRecord* parseAAAA();
90  ResourceRecord* parseSRV();
91  ResourceRecord* parseNAPTR();
92 
93  void parseHeader();
94  void parseDomainName( EString &dn );
95  void parseCharacterString( EString &cs );
96 
97  QueryPtr m_query;
98  MessageBuffer m_data;
99 
100  int m_qdcount;
101  int m_ancount;
102  int m_nscount;
103  int m_arcount;
104 
105  EString m_name;
106  ns_type m_type;
107  ns_class m_class;
108  int m_ttl;
109  int m_rdlength;
110  unsigned char *m_rdata;
111 
112  // RFC 1035
113  static const int HDR_FIXED_SIZE = 12;
114  static const int HDR_QDCOUNT_OFS = 4;
115  static const int HDR_ANCOUNT_OFS = 6;
116  static const int HDR_NSCOUNT_OFS = 8;
117  static const int HDR_ARCOUNT_OFS = 10;
118 
119  static const int Q_FIXED_SIZE = 4;
120  static const int Q_QTYPE_OFS = 0;
121  static const int Q_QCLASS_OFS = 2;
122 
123  static const int RR_FIXED_SIZE = 10;
124  static const int RR_TYPE_OFS = 0;
125  static const int RR_CLASS_OFS = 2;
126  static const int RR_TTL_OFS = 4;
127  static const int RR_RDLENGTH_OFS = 8;
128  static const int RR_RDATA_OFS = 10;
129 
130  static const int A_FIXED_SIZE = 0;
131  static const int A_ADDRESS_OFS = 0;
132 
133  static const int CNAME_FIXED_SIZE = 0;
134  static const int CNAME_TARGET_OFS = 0;
135 
136  // RFC 3596
137  static const int AAAA_FIXED_SIZE = 0;
138  static const int AAAA_ADDRESS_OFS = 0;
139 
140  // RFC 2915
141  static const int NAPTR_FIXED_SIZE = 4;
142  static const int NAPTR_ORDER_OFS = 0;
143  static const int NAPTR_PREFERENCE_OFS = 2;
144 
145  // RFC 2782
146  static const int SRV_FIXED_SIZE = 6;
147  static const int SRV_PRIORITY_OFS = 0;
148  static const int SRV_WEIGHT_OFS = 2;
149  static const int SRV_PORT_OFS = 4;
150  static const int SRV_TARGET_OFS = 6;
151  };
152 }
153 
155 
156 #endif // #ifndef DNSPARSER_H
dnsquery.h
Contains the definition of the DNS query related classes.
DNS
Definition: dnscache.h:33
EString
String class.
Definition: estring.h:30
DNS::QueryPtr
std::shared_ptr< Query > QueryPtr
A typedef to std::shared_ptr<Query>.
Definition: dnsquery.h:40
estring.h
Encapsulates and extends a std::string object.