EpcTools
An event based multi-threaded C++ development framework.
dnsrecord.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 __DNSRECORD_H
19 #define __DNSRECORD_H
20 
23 
24 #include <list>
25 
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <arpa/inet.h>
29 #include <memory.h>
30 #include <time.h>
31 
32 #include "estring.h"
33 
34 namespace DNS
35 {
38 
40  class Question
41  {
42  public:
47  Question( const std::string& qname, ns_type qtype, ns_class qclass )
48  : m_qname( qname ),
49  m_qtype( qtype ),
50  m_qclass( qclass )
51  {
52  }
53 
56  EString &getQName() { return m_qname; }
59  ns_type getQType() { return m_qtype; }
62  ns_class getQClass() { return m_qclass; }
63 
65  Void dump()
66  {
67  std::cout << "Question:"
68  << " qtype=" << m_qtype
69  << " qclass=" << m_qclass
70  << " qname=" << m_qname
71  << std::endl;
72  }
73 
74  private:
75  Question() {}
76 
77  EString m_qname;
78  ns_type m_qtype;
79  ns_class m_qclass;
80  };
81 
84 
86  class QuestionList : public std::list<Question*>
87  {
88  public:
89  QuestionList() {}
90  ~QuestionList()
91  {
92  while ( !empty() )
93  {
94  Question *q = *begin();
95  erase( begin() );
96  delete q;
97  }
98  }
99  };
101 
104 
107  {
108  public:
115  ResourceRecord( const std::string &name,
116  ns_type rtype,
117  ns_class rclass,
118  int32_t ttl )
119  : m_name( name ),
120  m_type( rtype ),
121  m_class( rclass ),
122  m_ttl( ttl ),
123  m_expires( time(NULL) + ttl )
124  {
125  }
127  virtual ~ResourceRecord() {}
128 
131  const EString &getName() { return m_name; }
134  ns_type getType() { return m_type; }
137  ns_class getClass() { return m_class; }
142  uint32_t getTTL() { return m_ttl; }
145  time_t getExpires() { return m_expires; }
146 
149  Bool isExpired() { return m_expires <= time(NULL); }
150 
152  virtual Void dump()
153  {
154  std::cout << "ResourceRecord:"
155  << " type=" << getType()
156  << " class=" << getClass()
157  << " ttl=" << getTTL()
158  << " expires=" << getExpires()
159  << " name=" << getName()
160  << std::endl;
161  }
162 
163  private:
164  EString m_name;
165  ns_type m_type;
166  ns_class m_class;
167  int32_t m_ttl;
168  time_t m_expires;
169  };
170 
173 
175  class ResourceRecordList : public std::list<ResourceRecord*>
176  {
177  public:
178  ResourceRecordList() {}
179  ~ResourceRecordList()
180  {
181  while ( !empty() )
182  {
183  ResourceRecord *rr = *begin();
184  erase( begin() );
185  delete rr;
186  }
187  }
188  };
190 
193 
195  class RRecordA : public ResourceRecord
196  {
197  public:
202  RRecordA( const std::string &name,
203  int32_t ttl,
204  const struct in_addr &address )
205  : ResourceRecord(name, ns_t_a, ns_c_in, ttl)
206  {
207  memcpy( &m_address, &address, sizeof(m_address) );
208  }
209 
212  const struct in_addr &getAddress() { return m_address; }
213 
217  {
218  char address[ INET_ADDRSTRLEN ];
219  inet_ntop( AF_INET, &m_address, address, sizeof(address) );
220  return EString( address );
221  }
222 
224  virtual Void dump()
225  {
226  std::cout << "RRecordA:"
227  << " type=" << getType()
228  << " class=" << getClass()
229  << " ttl=" << getTTL()
230  << " expires=" << getExpires()
231  << " address=" << getAddressString()
232  << " name=" << getName()
233  << std::endl;
234  }
235 
236  private:
237  struct in_addr m_address;
238  };
239 
242 
244  class RRecordNS : public ResourceRecord
245  {
246  public:
251  RRecordNS( const std::string &name,
252  int32_t ttl,
253  const std::string &ns )
254  : ResourceRecord( name, ns_t_cname, ns_c_in, ttl ),
255  m_namedserver( ns )
256  {
257  }
258 
259  const EString &getNamedServer() { return m_namedserver; }
260 
262  virtual Void dump()
263  {
264  std::cout << "RRecordNS:"
265  << " type=" << getType()
266  << " class=" << getClass()
267  << " ttl=" << getTTL()
268  << " expires=" << getExpires()
269  << " ns=" << getNamedServer()
270  << " name=" << getName()
271  << std::endl;
272  }
273 
274  private:
275  EString m_namedserver;
276  };
277 
280 
283  {
284  public:
289  RRecordAAAA( const std::string &name,
290  int32_t ttl,
291  const struct in6_addr &address )
292  : ResourceRecord( name, ns_t_aaaa, ns_c_in, ttl )
293  {
294  memcpy( &m_address, &address, sizeof(m_address) );
295  }
296 
297  const struct in6_addr &getAddress() { return m_address; }
298 
300  {
301  char address[ INET6_ADDRSTRLEN ];
302  inet_ntop( AF_INET6, &m_address, address, sizeof(address) );
303  return EString( address );
304  }
305 
307  virtual Void dump()
308  {
309  std::cout << "RRecordAAAA:"
310  << " type=" << getType()
311  << " class=" << getClass()
312  << " ttl=" << getTTL()
313  << " expires=" << getExpires()
314  << " address=" << getAddressString()
315  << " name=" << getName()
316  << std::endl;
317  }
318 
319  private:
320  struct in6_addr m_address;
321  };
322 
325 
328  {
329  public:
334  RRecordCNAME( const std::string &name,
335  int32_t ttl,
336  const std::string &alias )
337  : ResourceRecord( name, ns_t_cname, ns_c_in, ttl ),
338  m_alias( alias )
339  {
340  }
341 
342  const EString &getAlias() { return m_alias; }
343 
345  virtual Void dump()
346  {
347  std::cout << "RRecordCNAME:"
348  << " type=" << getType()
349  << " class=" << getClass()
350  << " ttl=" << getTTL()
351  << " expires=" << getExpires()
352  << " alias=" << getAlias()
353  << " name=" << getName()
354  << std::endl;
355  }
356 
357  private:
358  EString m_alias;
359  };
360 
363 
365  class RRecordSRV : public ResourceRecord
366  {
367  public:
375  RRecordSRV( const std::string &name,
376  int32_t ttl,
377  uint16_t priority,
378  uint16_t weight,
379  uint16_t port,
380  const std::string &target)
381  : ResourceRecord( name, ns_t_srv, ns_c_in, ttl ),
382  m_priority( priority ),
383  m_weight( weight ),
384  m_port( port ),
385  m_target( target )
386  {
387  }
388 
391  uint16_t getPriority() { return m_priority; }
394  uint16_t getWeight() { return m_weight; }
397  uint16_t getPort() { return m_port; }
400  const EString &getTarget() { return m_target; }
401 
403  virtual Void dump()
404  {
405  std::cout << "RRecordSRV:"
406  << " type=" << getType()
407  << " class=" << getClass()
408  << " ttl=" << getTTL()
409  << " expires=" << getExpires()
410  << " priority=" << getPriority()
411  << " weight=" << getWeight()
412  << " port=" << getPort()
413  << " target=" << getTarget()
414  << " name=" << getName()
415  << std::endl;
416  }
417 
418  private:
419  uint16_t m_priority;
420  uint16_t m_weight;
421  uint16_t m_port;
422  EString m_target;
423  };
424 
427 
430  {
431  public:
448  RRecordNAPTR( const std::string &name,
449  int32_t ttl,
450  uint16_t order,
451  uint16_t preference,
452  const std::string &flags,
453  const std::string &service,
454  const std::string &regexp,
455  const std::string &replacement )
456  : ResourceRecord( name, ns_t_naptr, ns_c_in, ttl ),
457  m_order( order ),
458  m_preference( preference ),
459  m_flags( flags ),
460  m_service( service ),
461  m_regexp( regexp ),
462  m_replacement( replacement )
463  {
464  }
465 
470  const uint16_t getOrder() const { return m_order; }
475  const uint16_t getPreference() const { return m_preference; }
478  const EString &getFlags() { return m_flags; }
482  const EString &getService() { return m_service; }
485  const EString &getRegexp() { return m_regexp; }
488  const EString &getReplacement() { return m_replacement; }
489 
491  virtual Void dump()
492  {
493  std::cout << "RRecordNAPTR:"
494  << " type=" << getType()
495  << " class=" << getClass()
496  << " ttl=" << getTTL()
497  << " expires=" << getExpires()
498  << " order=" << getOrder()
499  << " preference=" << getPreference()
500  << " flags=" << getFlags()
501  << " service=" << getService()
502  << " regexp=" << getRegexp()
503  << " replacement=" << getReplacement()
504  << " name=" << getName()
505  << std::endl;
506  }
507 
508  private:
509  uint16_t m_order;
510  uint16_t m_preference;
511  EString m_flags;
512  EString m_service;
513  EString m_regexp;
514  EString m_replacement;
515  };
516 }
517 
518 #endif // #ifdef __DNSRECORD_H
DNS::RRecordA::RRecordA
RRecordA(const std::string &name, int32_t ttl, const struct in_addr &address)
Class constructor.
Definition: dnsrecord.h:202
DNS::ResourceRecord::isExpired
Bool isExpired()
Determines if this resource record has expired.
Definition: dnsrecord.h:149
DNS::RRecordSRV::dump
virtual Void dump()
Prints the contents fo this SRV record.
Definition: dnsrecord.h:403
DNS::RRecordAAAA::getAddressString
EString getAddressString()
Definition: dnsrecord.h:299
DNS::ResourceRecord::getExpires
time_t getExpires()
Retrieves the expiration time of this resource record.
Definition: dnsrecord.h:145
DNS::RRecordNAPTR::getReplacement
const EString & getReplacement()
Retrieves the replacement string associated with this NAPTR record.
Definition: dnsrecord.h:488
DNS::RRecordCNAME::dump
virtual Void dump()
Prints the contents fo this CNAME record.
Definition: dnsrecord.h:345
DNS::RRecordSRV::getTarget
const EString & getTarget()
The domain name of the target host.
Definition: dnsrecord.h:400
DNS::RRecordAAAA::getAddress
const struct in6_addr & getAddress()
Definition: dnsrecord.h:297
DNS::Question::getQType
ns_type getQType()
Retrieves the query type for the requested resource.
Definition: dnsrecord.h:59
DNS::ResourceRecord::getTTL
uint32_t getTTL()
Retrieves the time interval (in seconds) that the resource record may be cached before it should be d...
Definition: dnsrecord.h:142
DNS::RRecordNAPTR
Represents an NAPTR resource record.
Definition: dnsrecord.h:429
DNS::Question::getQName
EString & getQName()
Retrieves the query name of the requested resource.
Definition: dnsrecord.h:56
DNS::RRecordCNAME::getAlias
const EString & getAlias()
Definition: dnsrecord.h:342
DNS::RRecordAAAA::dump
virtual Void dump()
Prints the contents fo this AAAA record.
Definition: dnsrecord.h:307
DNS::RRecordSRV::getWeight
uint16_t getWeight()
Retrieves.
Definition: dnsrecord.h:394
DNS::ResourceRecord
Represents a DNS Resource Record.
Definition: dnsrecord.h:106
DNS::RRecordNAPTR::dump
virtual Void dump()
Prints the contents fo this NAPTR record.
Definition: dnsrecord.h:491
DNS::ResourceRecord::dump
virtual Void dump()
Prints the contents of this resource record.
Definition: dnsrecord.h:152
DNS::RRecordSRV::getPriority
uint16_t getPriority()
Retrieves the priority of this target host.
Definition: dnsrecord.h:391
DNS::RRecordNAPTR::getOrder
const uint16_t getOrder() const
Retrieves the order in which the NAPTR records MUST be processed in order to accurately represent the...
Definition: dnsrecord.h:470
DNS::RRecordNS::getNamedServer
const EString & getNamedServer()
Definition: dnsrecord.h:259
DNS::RRecordSRV
Represents an SRV resource record.
Definition: dnsrecord.h:365
DNS::RRecordA::getAddress
const struct in_addr & getAddress()
Retrieves the IP address for this A record.
Definition: dnsrecord.h:212
DNS::Question::getQClass
ns_class getQClass()
Retrieves the class of the query.
Definition: dnsrecord.h:62
DNS::RRecordNAPTR::RRecordNAPTR
RRecordNAPTR(const std::string &name, int32_t ttl, uint16_t order, uint16_t preference, const std::string &flags, const std::string &service, const std::string &regexp, const std::string &replacement)
Class constructor.
Definition: dnsrecord.h:448
DNS::ResourceRecord::getName
const EString & getName()
Retrieves the domain name to which this resource record pertains.
Definition: dnsrecord.h:131
DNS::RRecordNAPTR::getFlags
const EString & getFlags()
Retrieves the flags associated with this NAPTR record.
Definition: dnsrecord.h:478
DNS::RRecordNAPTR::getService
const EString & getService()
Retrieves the Service Parameters applicable to this this delegation path.
Definition: dnsrecord.h:482
DNS::RRecordSRV::RRecordSRV
RRecordSRV(const std::string &name, int32_t ttl, uint16_t priority, uint16_t weight, uint16_t port, const std::string &target)
Class constructor.
Definition: dnsrecord.h:375
DNS::RRecordNAPTR::getPreference
const uint16_t getPreference() const
Retrieves he order in which NAPTR records with equal Order values SHOULD be processed,...
Definition: dnsrecord.h:475
DNS::ResourceRecord::~ResourceRecord
virtual ~ResourceRecord()
Class destructor.
Definition: dnsrecord.h:127
DNS::RRecordA
Represents an A resource record.
Definition: dnsrecord.h:195
DNS::RRecordNS
Represents an NS resource record.
Definition: dnsrecord.h:244
DNS::ResourceRecord::ResourceRecord
ResourceRecord(const std::string &name, ns_type rtype, ns_class rclass, int32_t ttl)
Class constructor.
Definition: dnsrecord.h:115
DNS::RRecordA::getAddressString
EString getAddressString()
Convert the IPv4 address to a string.
Definition: dnsrecord.h:216
DNS::Question::Question
Question(const std::string &qname, ns_type qtype, ns_class qclass)
Class constructor.
Definition: dnsrecord.h:47
DNS::RRecordCNAME
Represents an CNAME resource record.
Definition: dnsrecord.h:327
DNS
Definition: dnscache.h:33
EString
String class.
Definition: estring.h:30
DNS::ResourceRecord::getClass
ns_class getClass()
Retrieves the class of the data in the RDATA field.
Definition: dnsrecord.h:137
DNS::Question
Represents the question for the name server.
Definition: dnsrecord.h:40
DNS::RRecordAAAA
Represents an AAAA resource record.
Definition: dnsrecord.h:282
DNS::RRecordNAPTR::getRegexp
const EString & getRegexp()
Retrieves the regular expression associated with this NAPTR record.
Definition: dnsrecord.h:485
DNS::ResourceRecord::getType
ns_type getType()
Retrieves the resource type.
Definition: dnsrecord.h:134
estring.h
Encapsulates and extends a std::string object.
DNS::RRecordNS::RRecordNS
RRecordNS(const std::string &name, int32_t ttl, const std::string &ns)
Class constructor.
Definition: dnsrecord.h:251
DNS::RRecordA::dump
virtual Void dump()
Prints the contents fo this A record.
Definition: dnsrecord.h:224
DNS::RRecordAAAA::RRecordAAAA
RRecordAAAA(const std::string &name, int32_t ttl, const struct in6_addr &address)
Class constructor.
Definition: dnsrecord.h:289
DNS::RRecordNS::dump
virtual Void dump()
Prints the contents fo this NS record.
Definition: dnsrecord.h:262
DNS::RRecordSRV::getPort
uint16_t getPort()
Retrieves the relative weight for entries with the same priority.
Definition: dnsrecord.h:397
DNS::Question::dump
Void dump()
Prints the contents the stdout.
Definition: dnsrecord.h:65
DNS::RRecordCNAME::RRecordCNAME
RRecordCNAME(const std::string &name, int32_t ttl, const std::string &alias)
Class constructor.
Definition: dnsrecord.h:334