Async 1.8.0
AsyncDnsResourceRecord.h
Go to the documentation of this file.
1
27#ifndef ASYNC_DNS_RESOURCE_RECORD_INCLUDED
28#define ASYNC_DNS_RESOURCE_RECORD_INCLUDED
29
30
31/****************************************************************************
32 *
33 * System Includes
34 *
35 ****************************************************************************/
36
37#include <string>
38#include <sstream>
39#include <map>
40#include <memory>
41#include <vector>
42
43
44/****************************************************************************
45 *
46 * Project Includes
47 *
48 ****************************************************************************/
49
50#include <AsyncIpAddress.h>
51
52
53/****************************************************************************
54 *
55 * Local Includes
56 *
57 ****************************************************************************/
58
59
60
61/****************************************************************************
62 *
63 * Forward declarations
64 *
65 ****************************************************************************/
66
67
68
69/****************************************************************************
70 *
71 * Namespace
72 *
73 ****************************************************************************/
74
75namespace Async
76{
77
78
79/****************************************************************************
80 *
81 * Forward declarations of classes inside of the declared namespace
82 *
83 ****************************************************************************/
84
85
86
87/****************************************************************************
88 *
89 * Defines & typedefs
90 *
91 ****************************************************************************/
92
93
94
95/****************************************************************************
96 *
97 * Exported Global Variables
98 *
99 ****************************************************************************/
100
101
102
103/****************************************************************************
104 *
105 * Class definitions
106 *
107 ****************************************************************************/
108
119class DnsResourceRecord
120{
121 public:
122 using Name = std::string;
123 using Ttl = uint32_t;
124
128 enum class Type { ANY, A, PTR, CNAME, SRV };
129
133 enum class Class { IN };
134
138 using List = std::vector<std::unique_ptr<DnsResourceRecord>>;
139
143 static constexpr Ttl MAX_TTL = 0x7fffffff;
144
148 static const Type staticType(void) { return Type::ANY; }
149
153 static const std::string& typeToString(Type type)
154 {
155 static const std::map<Type, std::string> type_map = {
156 {Type::A, "A"}, {Type::PTR, "PTR"}, {Type::CNAME, "CNAME"},
157 {Type::SRV, "SRV"}
158 };
159 static const std::string UNKNOWN = "?";
160 const auto& it = type_map.find(type);
161 if (it == type_map.end())
162 {
163 return UNKNOWN;
164 }
165 return it->second;
166 }
167
174 : m_name(name), m_ttl(ttl) {}
175
179 virtual ~DnsResourceRecord(void) {}
180
185 virtual DnsResourceRecord* clone(void) const = 0;
186
194 virtual bool operator==(const DnsResourceRecord& other) const
195 {
196 return (classId() == other.classId()) &&
197 (type() == other.type()) &&
198 (name() == other.name());
199 }
200
205 Class classId(void) const { return Class::IN; }
206
211 const char* classStr(void) const { return "IN"; }
212
217 virtual const Type type(void) const = 0;
218
223 const std::string& typeStr(void) const
224 {
225 return typeToString(type());
226 }
227
232 virtual std::string toString(void) const
233 {
234 std::ostringstream os;
235 os << name() << "\t" << ttl() << "\t" << classStr() << "\t"
236 << typeStr() << "\t";
237 return os.str();
238 }
239
244 void setName(const Name& name) { m_name = name; }
245
250 const Name& name(void) const { return m_name; }
251
256 void setTtl(Ttl ttl) { m_ttl = ttl; }
257
262 Ttl ttl(void) const { return m_ttl; }
263
264 private:
265 Name m_name;
266 Ttl m_ttl;
267
268}; /* class DnsResourceRecord */
269
270
279template <typename Derived>
281{
282 public:
286 using List = std::vector<std::unique_ptr<Derived>>;
287
295
300 virtual DnsResourceRecord* clone(void) const
301 {
302 return new Derived(static_cast<Derived const&>(*this));
303 }
304
312 virtual bool operator==(const DnsResourceRecord& other) const
313 {
314 const auto& lhs = static_cast<const Derived&>(*this);
315 const auto& rhs = static_cast<const Derived&>(other);
316 return lhs == rhs;
317 }
318
319 virtual bool operator==(const Derived& other) const = 0;
320
325 virtual const Type type(void) const override { return Derived::staticType(); }
326}; /* DnsResourceRecordCRTP */
327
328
340class DnsResourceRecordA : public DnsResourceRecordCRTP<DnsResourceRecordA>
341{
342 public:
344
348 static const Type staticType(void) { return Type::A; }
349
358
366 virtual bool operator==(const DnsResourceRecordA& other) const
367 {
368 return DnsResourceRecord::operator==(other) &&
369 (ip() == other.ip());
370 }
371
376 virtual std::string toString(void) const
377 {
378 std::ostringstream os;
379 os << DnsResourceRecord::toString() << ip();
380 return os.str();
381 }
382
387 void setIp(const Ip& ip) { m_ip = ip; }
388
393 const Ip& ip(void) const { return m_ip; }
394
395 private:
396 Ip m_ip;
397}; /* DnsResourceRecordA */
398
399
411class DnsResourceRecordPTR : public DnsResourceRecordCRTP<DnsResourceRecordPTR>
412{
413 public:
414 using DName = std::string;
415
419 static const Type staticType(void) { return Type::PTR; }
420
430
438 virtual bool operator==(const DnsResourceRecordPTR& other) const
439 {
440 return DnsResourceRecord::operator==(other) &&
441 (dname() == other.dname());
442 }
443
448 virtual std::string toString(void) const
449 {
450 std::ostringstream os;
452 return os.str();
453 }
454
459 void setName(const DName& dname) { m_dname = dname; }
460
465 const DName& dname(void) const { return m_dname; }
466
467 private:
468 DName m_dname;
469}; /* DnsResourceRecordPTR */
470
471
483class DnsResourceRecordCNAME :
484 public DnsResourceRecordCRTP<DnsResourceRecordCNAME>
485{
486 public:
487 using CName = std::string;
488
492 static const Type staticType(void) { return Type::CNAME; }
493
503
511 virtual bool operator==(const DnsResourceRecordCNAME& other) const
512 {
513 return DnsResourceRecord::operator==(other) &&
514 (cname() == other.cname());
515 }
516
521 virtual std::string toString(void) const
522 {
523 std::ostringstream os;
525 return os.str();
526 }
527
532 void setName(const CName& cname) { m_cname = cname; }
533
538 const CName& cname(void) const { return m_cname; }
539
540 private:
541 CName m_cname;
542}; /* DnsResourceRecordCNAME */
543
544
579class DnsResourceRecordSRV :
580 public DnsResourceRecordCRTP<DnsResourceRecordSRV>
581{
582 public:
583 using Prio = unsigned int;
584 using Weight = unsigned int;
585 using Port = unsigned int;
586 using Target = std::string;
587
591 static const Type staticType(void) { return Type::SRV; }
592
603 Port port, const Target& target)
605 m_prio(prio), m_weight(weight), m_port(port), m_target(target) {}
606
614 virtual bool operator==(const DnsResourceRecordSRV& other) const
615 {
616 return DnsResourceRecord::operator==(other) &&
617 (prio() == other.prio()) &&
618 (weight() == other.weight()) &&
619 (port() == other.port()) &&
620 (target() == other.target());
621 }
622
627 virtual std::string toString(void) const
628 {
629 std::ostringstream os;
630 os << DnsResourceRecord::toString() << prio() << " " << weight()
631 << " " << port() << " " << target();
632 return os.str();
633 }
634
639 void setPrio(Prio prio) { m_prio = prio; }
640
645 Prio prio(void) const { return m_prio; }
646
651 void setWeight(Weight weight) { m_weight = weight; }
652
657 Weight weight(void) const { return m_weight; }
658
663 void setPort(Port port) { m_port = port; }
664
669 Port port(void) const { return m_port; }
670
675 void setTarget(const Target& target) { m_target = target; }
676
681 const Target& target(void) const { return m_target; }
682
683 private:
684 Prio m_prio;
685 Weight m_weight;
686 Port m_port;
687 Target m_target;
688}; /* DnsResourceRecordSRV */
689
690
691} /* namespace Async */
692
693#endif /* ASYNC_DNS_RESOURCE_RECORD_INCLUDED */
694
695/*
696 * This file has not been truncated
697 */
Platform independent representation of an IP address.
A class for representing an A DNS resource record.
static const Type staticType(void)
The type for this specific class.
const Ip & ip(void) const
The IP address for this record.
void setIp(const Ip &ip)
Set the IP address for this record.
DnsResourceRecordA(const Name &name, Ttl ttl, const Ip &ip=Ip())
Constructor.
virtual std::string toString(void) const
The string representation of this record.
virtual bool operator==(const DnsResourceRecordA &other) const
Equality comparison operator.
A class for representing a CNAME DNS resource record.
virtual bool operator==(const DnsResourceRecordCNAME &other) const
Equality comparison operator.
const CName & cname(void) const
The FQDN for this record.
static const Type staticType(void)
The type for this specific class.
DnsResourceRecordCNAME(const Name &name, Ttl ttl, const CName &cname="")
Constructor.
virtual std::string toString(void) const
The string representation of this record.
void setName(const CName &cname)
Set the FQDN for this record.
A CRTP class for adding some type dependent functions.
DnsResourceRecordCRTP(const Name &name, Ttl ttl)
Constructor.
virtual bool operator==(const Derived &other) const =0
std::vector< std::unique_ptr< Derived > > List
The type for a list of resource records.
virtual bool operator==(const DnsResourceRecord &other) const
Equality comparison operator.
virtual const Type type(void) const override
The type of record.
virtual DnsResourceRecord * clone(void) const
Clone this class.
A class for representing a PTR DNS resource record.
virtual bool operator==(const DnsResourceRecordPTR &other) const
Equality comparison operator.
DnsResourceRecordPTR(const Name &name, Ttl ttl, const DName &dname="")
Constructor.
const DName & dname(void) const
The FQDN for this record.
static const Type staticType(void)
The type for this specific class.
virtual std::string toString(void) const
The string representation of this record.
void setName(const DName &dname)
Set the FQDN for this record.
A class for representing a SRV DNS resource record.
Prio prio(void) const
The prio for this record.
Port port(void) const
The network port for this record.
void setPrio(Prio prio)
Set the prio for this record.
DnsResourceRecordSRV(const Name &name, Ttl ttl, Prio prio, Weight weight, Port port, const Target &target)
Constructor.
Weight weight(void) const
The weight for this record.
void setWeight(Weight weight)
Set the weight for this record.
const Target & target(void) const
The FQDN for this record.
void setTarget(const Target &target)
Set the FQDN for this record.
virtual std::string toString(void) const
The string representation of this record.
static const Type staticType(void)
The type for this specific class.
void setPort(Port port)
Set the network port for this record.
virtual bool operator==(const DnsResourceRecordSRV &other) const
Equality comparison operator.
The base class for representing a DNS resource record.
const std::string & typeStr(void) const
The type of record as a string.
static constexpr Ttl MAX_TTL
The maximum allowed value for a TTL.
DnsResourceRecord(const Name &name, Ttl ttl)
Constructor.
virtual ~DnsResourceRecord(void)
Destructor.
virtual std::string toString(void) const
The string representation of this record.
const Name & name(void) const
The name of this record.
std::vector< std::unique_ptr< DnsResourceRecord > > List
The type for a list of resource records.
virtual bool operator==(const DnsResourceRecord &other) const
Equality comparison operator.
virtual DnsResourceRecord * clone(void) const =0
Clone this class.
static const std::string & typeToString(Type type)
The type for this specific class represented as a string.
void setName(const Name &name)
Set the name for this record.
virtual const Type type(void) const =0
The type of record.
const char * classStr(void) const
The DNS class for the record as a string.
Class classId(void) const
The DNS class for the record.
Ttl ttl(void) const
The TTL for this record.
void setTtl(Ttl ttl)
Set the TTL for this record.
static const Type staticType(void)
The type for this specific class.
A class for representing an IP address in an OS independent way.
Namespace for the asynchronous programming classes.