DwmDns-0.1.0
DwmDnsResourceRecord.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/DwmDns/tags/DwmDns-0.1.0/classes/include/DwmDnsResourceRecord.hh 10136 $
3 // @(#) $Id: DwmDnsResourceRecord.hh 10136 2018-01-28 06:01:09Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2018
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMDNSRESOURCERECORD_HH_
43 #define _DWMDNSRESOURCERECORD_HH_
44 
45 #include "DwmDnsPortability.hh"
46 
47 #include <cstdint>
48 #include <iostream>
49 #include <map>
50 #include <string>
51 #include <typeinfo>
52 #ifdef HAVE_VARIANT
53 #include <variant>
54 #define MY_VARIANT std::variant
55 #define MY_GET_IF std::get_if
56 #else
57 #include <mpark_variant>
58 #define MY_VARIANT mpark::variant
59 #define MY_GET_IF mpark::get_if
60 #endif
61 
62 #include "DwmDnsLabelPositions.hh"
63 #include "DwmDnsRRDataA.hh"
64 #include "DwmDnsRRDataNS.hh"
65 #include "DwmDnsRRDataCNAME.hh"
66 #include "DwmDnsRRDataAAAA.hh"
67 #include "DwmDnsRRDataSOA.hh"
68 #include "DwmDnsRRDataMB.hh"
69 #include "DwmDnsRRDataMG.hh"
70 #include "DwmDnsRRDataMR.hh"
71 #include "DwmDnsRRDataPTR.hh"
72 #include "DwmDnsRRDataHINFO.hh"
73 #include "DwmDnsRRDataMINFO.hh"
74 #include "DwmDnsRRDataMX.hh"
75 #include "DwmDnsRRDataTXT.hh"
76 #include "DwmDnsRRDataRP.hh"
77 #include "DwmDnsRRDataKEY.hh"
78 #include "DwmDnsRRDataLOC.hh"
79 #include "DwmDnsRRDataSRV.hh"
80 #include "DwmDnsRRDataCERT.hh"
81 #include "DwmDnsRRDataDS.hh"
82 #include "DwmDnsRRDataSSHFP.hh"
83 #include "DwmDnsRRDataRRSIG.hh"
84 #include "DwmDnsRRDataNSEC.hh"
85 #include "DwmDnsRRDataDNSKEY.hh"
86 #include "DwmDnsRRDataDHCID.hh"
87 #include "DwmDnsRRDataNSEC3.hh"
89 #include "DwmDnsRRDataTLSA.hh"
90 #include "DwmDnsRRDataSMIMEA.hh"
92 #include "DwmDnsRRDataURI.hh"
93 #include "DwmDnsRRDataCAA.hh"
94 #include "DwmDnsRRDataUnknown.hh"
95 
96 namespace Dwm {
97 
98  namespace Dns {
99 
100  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
105  {
106  public:
107  //----------------------------------------------------------------------
109  //----------------------------------------------------------------------
110  typedef enum {
111  k_typeA = 1,
112  k_typeNS = 2,
113  k_typeMD = 3,
114  k_typeMF = 4,
116  k_typeSOA = 6,
117  k_typeMB = 7,
118  k_typeMG = 8,
119  k_typeMR = 9,
120  k_typeNULL = 10,
121  k_typeWKS = 11,
122  k_typePTR = 12,
123  k_typeHINFO = 13,
124  k_typeMINFO = 14,
125  k_typeMX = 15,
126  k_typeTXT = 16,
127  k_typeRP = 17,
128  k_typeKEY = 25,
129  k_typeAAAA = 28,
130  k_typeLOC = 29,
131  k_typeSRV = 33,
132  k_typeCERT = 37,
133  k_typeOPT = 41,
134  k_typeDS = 43,
135  k_typeSSHFP = 44,
136  k_typeRRSIG = 46,
137  k_typeNSEC = 47,
139  k_typeDHCID = 49,
140  k_typeNSEC3 = 50,
142  k_typeTLSA = 52,
144  k_typeCDS = 59,
147  k_typeSPF = 99,
148  k_typeURI = 256,
149  k_typeCAA = 257,
150  k_typeUNKNOWN = 0xFFFF
151  } TypeEnum;
152 
153  //----------------------------------------------------------------------
155  //----------------------------------------------------------------------
156  typedef enum {
157  k_classIN = 1,
158  k_classCH = 3,
159  k_classHS = 4,
160  k_classUNKNOWN = 0xFFFF
161  } ClassEnum;
162 
163  //----------------------------------------------------------------------
165  //----------------------------------------------------------------------
166  ResourceRecord();
167 
168  //----------------------------------------------------------------------
170  //----------------------------------------------------------------------
171  ResourceRecord(const ResourceRecord & rr);
172 
173  //----------------------------------------------------------------------
175  //----------------------------------------------------------------------
177 
178  //----------------------------------------------------------------------
180  //----------------------------------------------------------------------
181  ~ResourceRecord() = default;
182 
183  //----------------------------------------------------------------------
185  //----------------------------------------------------------------------
186  bool operator == (const ResourceRecord & rr) const;
187 
188  //----------------------------------------------------------------------
190  //----------------------------------------------------------------------
191  const std::string & Name() const;
192 
193  //----------------------------------------------------------------------
195  //----------------------------------------------------------------------
196  const std::string & Name(const std::string & name);
197 
198  //----------------------------------------------------------------------
200  //----------------------------------------------------------------------
201  TypeEnum Type() const;
202 
203  //----------------------------------------------------------------------
205  //----------------------------------------------------------------------
206  TypeEnum Type(TypeEnum typeEnum);
207 
208  //----------------------------------------------------------------------
210  //----------------------------------------------------------------------
211  uint16_t Class() const;
212 
213  //----------------------------------------------------------------------
215  //----------------------------------------------------------------------
216  uint16_t Class(uint16_t cl);
217 
218  //----------------------------------------------------------------------
220  //----------------------------------------------------------------------
221  uint32_t TTL() const;
222 
223  //----------------------------------------------------------------------
225  //----------------------------------------------------------------------
226  uint32_t TTL(uint32_t ttl);
227 
228  //----------------------------------------------------------------------
234  //----------------------------------------------------------------------
235  uint8_t *Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen,
236  LabelPositions & lps) const;
237 
238  //----------------------------------------------------------------------
244  //----------------------------------------------------------------------
245  const uint8_t *Decode(const uint8_t *pkt, const uint8_t *ptr,
246  uint16_t pktlen);
247 
248  //----------------------------------------------------------------------
250  //----------------------------------------------------------------------
251  template <typename T>
252  const T * Data() const
253  {
254  return MY_GET_IF<T>(&_rrdata);
255  }
256 
257  //----------------------------------------------------------------------
264  //----------------------------------------------------------------------
265  template <typename T>
266  const T *Data(const T & rrdata, uint16_t rrClass = k_classIN,
267  uint16_t rrType = T::k_rrtype)
268  {
269  _class = rrClass;
270  _type = (TypeEnum)rrType;
271  _rrdata = rrdata;
272  return MY_GET_IF<T>(&_rrdata);
273  }
274 
275  //----------------------------------------------------------------------
277  //----------------------------------------------------------------------
278  template <typename T>
279  T * Data()
280  {
281  return MY_GET_IF<T>(&_rrdata);
282  }
283 
284  //----------------------------------------------------------------------
286  //----------------------------------------------------------------------
287  friend std::ostream & operator << (std::ostream & os,
288  const ResourceRecord & rr);
289 
290  //----------------------------------------------------------------------
292  //----------------------------------------------------------------------
293  static std::string TypeName(uint16_t rrtype);
294 
295  //----------------------------------------------------------------------
297  //----------------------------------------------------------------------
298  static std::string ClassName(uint16_t rrclass);
299 
300  protected:
301  std::string _name;
302  TypeEnum _type;
303  uint16_t _class;
304  uint32_t _ttl;
305  MY_VARIANT<RRDataA, // 0
306  RRDataNS, // 1
307  RRDataCNAME, // 2
308  RRDataSOA, // 3
309  RRDataMB, // 4
310  RRDataMG, // 5
311  RRDataMR, // 6
312  RRDataPTR, // 7
313  RRDataHINFO, // 8
314  RRDataMINFO, // 9
315  RRDataMX, // 10
316  RRDataTXT, // 11
317  RRDataRP, // 12
318  RRDataKEY, // 13
319  RRDataAAAA, // 14
320  RRDataLOC, // 15
321  RRDataSRV, // 16
322  RRDataCERT, // 17
323  RRDataDS, // 18
324  RRDataSSHFP, // 19
325  RRDataRRSIG, // 20
326  RRDataNSEC, // 21
327  RRDataDNSKEY, // 22
328  RRDataDHCID, // 23
329  RRDataNSEC3, // 24
330  RRDataNSEC3PARAM, // 25
331  RRDataTLSA, // 26
332  RRDataSMIMEA, // 27
333  RRDataOPENPGPKEY, // 28
334  RRDataURI, // 29
335  RRDataCAA, // 30
336  RRDataUnknown // 31
337  > _rrdata;
338 
339  static std::map<uint16_t,std::string> _classNames;
340  static std::map<uint16_t,std::string> _typeNames;
341 
342 #if 0
343  // xxx : no longer used, but could be revived in the future if
344  // I need to be able to fetch contained RRDATA by resource record
345  // type enumeration.
346  static constexpr std::pair<uint16_t,size_t> k_rrTypeArr[] = {
347  { k_typeA, 0 },
348  { k_typeNS, 1 },
349  { k_typeCNAME, 2 },
350  { k_typeSOA, 3 },
351  { k_typeMB, 4 },
352  { k_typeMG, 5 },
353  { k_typeMR, 6 },
354  { k_typePTR, 7 },
355  { k_typeHINFO, 8 },
356  { k_typeMINFO, 9 },
357  { k_typeMX, 10 },
358  { k_typeTXT, 11 },
359  { k_typeRP, 12 },
360  { k_typeKEY, 13 },
361  { k_typeAAAA, 14 },
362  { k_typeLOC, 15 },
363  { k_typeSRV, 16 },
364  { k_typeCERT, 17 },
365  { k_typeDS, 18 },
366  { k_typeSSHFP, 19 },
367  { k_typeRRSIG, 20 },
368  { k_typeNSEC, 21 },
369  { k_typeDNSKEY, 22 },
370  { k_typeDHCID, 23 },
371  { k_typeNSEC3, 24 },
372  { k_typeNSEC3PARAM, 25 },
373  { k_typeTLSA, 26 },
374  { k_typeSMIMEA, 27 },
375  { k_typeCDS, 18 },
376  { k_typeCDNSKEY, 22 },
377  { k_typeOPENPGPKEY, 28 },
378  { k_typeURI, 29 },
379  { k_typeCAA, 30 }
380  };
381 
382  //----------------------------------------------------------------------
384  //----------------------------------------------------------------------
385  static constexpr auto k_rrTypeArrSize =
386  sizeof k_rrTypeArr / sizeof k_rrTypeArr[0];
387 
388  //----------------------------------------------------------------------
390  //----------------------------------------------------------------------
391  static constexpr size_t RRDataIndex(uint16_t rrtype,
392  int range = k_rrTypeArrSize)
393  {
394  return (k_rrTypeArr[range - 1].first == rrtype) ?
395  k_rrTypeArr[range - 1].second
396  : RRDataIndex(rrtype, range - 1);
397  }
398 #endif
399 
400  std::ostream & PrintData(std::ostream & os) const;
401  };
402 
403 
404  } // namespace Dns
405 
406 } // namespace Dwm
407 
408 #endif // _DWMDNSRESOURCERECORD_HH_
marks the start of a zone of authority
Definition: DwmDnsResourceRecord.hh:117
URI (RFC7553)
Definition: DwmDnsResourceRecord.hh:149
Encapsulates KEY resource record data.
Definition: DwmDnsRRDataKEY.hh:62
Dwm::Dns::RRDataSSHFP class definition.
ResourceRecord()
Default constructor.
Dwm::Dns::RRDataNSEC3.
Dwm::Dns::RRDataRP class definition.
Encapsulates OPENPGKPEY resource record data.
Definition: DwmDnsRRDataOPENPGPKEY.hh:60
DNSKEY (RFC4034)
Definition: DwmDnsResourceRecord.hh:139
Dwm::Dns::LabelPositions class definition.
Dwm::Dns::RRDataCERT class definition.
Dwm::Dns::RRDataSMIMEA class definition.
Encapsulates CERT resource record data.
Definition: DwmDnsRRDataCERT.hh:60
host address
Definition: DwmDnsResourceRecord.hh:112
Encapsulates DNSKEY resource record data.
Definition: DwmDnsRRDataDNSKEY.hh:59
NSEC3 (RFC5155)
Definition: DwmDnsResourceRecord.hh:141
Encapsulates NSEC3PARAM resource record data.
Definition: DwmDnsRRDataNSEC3PARAM.hh:60
Encapsulates MX resource record data.
Definition: DwmDnsRRDataMX.hh:59
certificate (RFC4398)
Definition: DwmDnsResourceRecord.hh:133
Dwm::Dns::RRDataCNAME class definition.
Dwm::Dns::RRDataMB class definition.
Encapsulates NSEC3 resource record data.
Definition: DwmDnsRRDataNSEC3.hh:60
null RR (EXPERIMENTAL)
Definition: DwmDnsResourceRecord.hh:121
CDS (RFC7344)
Definition: DwmDnsResourceRecord.hh:145
Encapsulates a DNS resource record.
Definition: DwmDnsResourceRecord.hh:104
NSEC3PARAM (RFC5155)
Definition: DwmDnsResourceRecord.hh:142
T * Data()
Returns a pointer to mutable contained RRDATA.
Definition: DwmDnsResourceRecord.hh:279
Dwm::Dns::RRDataSOA class definition.
RFC4408 but obsoleted in RFC7208.
Definition: DwmDnsResourceRecord.hh:148
Dwm::Dns::RRDataNS class definition.
Dwm::Dns::RRDataNSEC3PARAM class definition.
CDNSKEY (RFC7344)
Definition: DwmDnsResourceRecord.hh:146
Dwm::Dns::RRDataNSEC class definition.
Encapsulates SRV resource record data.
Definition: DwmDnsRRDataSRV.hh:59
const std::string & Name() const
Returns the contained domain name.
friend std::ostream & operator<<(std::ostream &os, const ResourceRecord &rr)
Prints the resource record to an ostream in human readable form.
DS (RFC4034)
Definition: DwmDnsResourceRecord.hh:135
cert. authority auth. (RFC6844)
Definition: DwmDnsResourceRecord.hh:150
Encapsulates NSEC resource record data.
Definition: DwmDnsRRDataNSEC.hh:61
Encapsulates SMIMEA resource record data.
Definition: DwmDnsRRDataSMIMEA.hh:59
Encapsulates PTR resource record data.
Definition: DwmDnsRRDataPTR.hh:58
location (LOC) (RFC1876)
Definition: DwmDnsResourceRecord.hh:131
host information
Definition: DwmDnsResourceRecord.hh:124
ResourceRecord & operator=(const ResourceRecord &rr)
operator =
SSH fingerprint (RFC4255)
Definition: DwmDnsResourceRecord.hh:136
Dwm::Dns::RRDataDS class definition.
ClassEnum
Resource record classes.
Definition: DwmDnsResourceRecord.hh:156
Encapsulates MINFO resource record data.
Definition: DwmDnsRRDataMINFO.hh:59
DHCID (RFC4701)
Definition: DwmDnsResourceRecord.hh:140
Encapsulates URI resource record data.
Definition: DwmDnsRRDataURI.hh:59
mail rename domain name (EXPERIMENTAL)
Definition: DwmDnsResourceRecord.hh:120
Encapsulates MR resource record data.
Definition: DwmDnsRRDataMR.hh:58
static std::string ClassName(uint16_t rrclass)
Returns the mnemonic for the given recource record class @ rrclass.
Dwm::Dns::RRDataDHCID class definition.
Dwm::Dns::RRDataMG class definition.
CHAOS.
Definition: DwmDnsResourceRecord.hh:159
Dwm::Dns::RRDataKEY class definition.
Dwm::Dns::RRDataCAA class definition.
Dwm::Dns::RRDataPTR class definition.
mail forwarder (obsolete, use MX)
Definition: DwmDnsResourceRecord.hh:115
host IPv6 address (RFC3596)
Definition: DwmDnsResourceRecord.hh:130
Encapsulates MG resource record data.
Definition: DwmDnsRRDataMG.hh:58
Dwm::Dns::RRDataTXT class definition.
Dwm::Dns::RRDataLOC class definition.
Internet.
Definition: DwmDnsResourceRecord.hh:158
canonical name for an alias
Definition: DwmDnsResourceRecord.hh:116
server selection (RFC2782)
Definition: DwmDnsResourceRecord.hh:132
Encapsulates RP resource record data.
Definition: DwmDnsRRDataRP.hh:59
Responsible Person (RFC1183)
Definition: DwmDnsResourceRecord.hh:128
Encapsulates NS resource record data.
Definition: DwmDnsRRDataNS.hh:58
Hesiod.
Definition: DwmDnsResourceRecord.hh:160
mailbox domain name (EXPERIMENTAL)
Definition: DwmDnsResourceRecord.hh:118
Encapsulates CAA resource record data.
Definition: DwmDnsRRDataCAA.hh:59
Dwm::Dns::RRDataAAAA class definition.
mail exchange
Definition: DwmDnsResourceRecord.hh:126
Dwm::Dns::RRDataSRV class definition.
Dwm::Dns::RRDataOPENPGPKEY class definition.
Encapsulates TLSA resource record data.
Definition: DwmDnsRRDataTLSA.hh:59
Dwm::Dns::RRDataMR class definition.
Definition: DwmDnsEtcHosts.hh:60
uint32_t TTL() const
Returns the TTL.
Encapsulates HINFO resource record data.
Definition: DwmDnsRRDataHINFO.hh:59
Dwm::Dns::RRDataURI class definition.
mailbox or mail list information
Definition: DwmDnsResourceRecord.hh:125
TLSA (RFC6698)
Definition: DwmDnsResourceRecord.hh:143
const T * Data() const
Returns a pointer to const of the contained RRDATA.
Definition: DwmDnsResourceRecord.hh:252
bool operator==(const ResourceRecord &rr) const
Equal-to operator, mostly for unit testing.
Encapsulates LOC resource record data.
Definition: DwmDnsRRDataLOC.hh:58
Dwm::Dns::RRDataMX class definition.
public key (RFC2535)
Definition: DwmDnsResourceRecord.hh:129
const uint8_t * Decode(const uint8_t *pkt, const uint8_t *ptr, uint16_t pktlen)
Decodes the resource record from buffer pkt of length pktlen, starting at ptr.
Encapsulates SSHFP recource record data.
Definition: DwmDnsRRDataSSHFP.hh:59
~ResourceRecord()=default
Destructor.
mail group member (EXPERIMENTAL)
Definition: DwmDnsResourceRecord.hh:119
const T * Data(const T &rrdata, uint16_t rrClass=k_classIN, uint16_t rrType=T::k_rrtype)
Sets and returns the RRDATA of the resource record.
Definition: DwmDnsResourceRecord.hh:266
Dwm::Dns::RRDataUnknown class definition.
Dwm::Dns::RRDataA class definition.
well known service description
Definition: DwmDnsResourceRecord.hh:122
Dwm::Dns::RRDataRRSIG class definition.
authoritative nameserver
Definition: DwmDnsResourceRecord.hh:113
OPENPGPKEY (RFC7929)
Definition: DwmDnsResourceRecord.hh:147
Encapsulates SOA resource record data.
Definition: DwmDnsRRDataSOA.hh:59
Encapsulates RRSIG resource record data.
Definition: DwmDnsRRDataRRSIG.hh:59
uint8_t * Encode(uint8_t *pkt, uint8_t *ptr, uint16_t pktlen, LabelPositions &lps) const
Encodes the resource record into buffer @ pkt of length pktlen, starting at ptr.
Dwm::Dns::RRDataTLSA class definition.
TypeEnum Type() const
Returns the contained type.
uint16_t Class() const
Returns the contained class.
A helper class used when performing label sequence compression.
Definition: DwmDnsLabelPositions.hh:57
Dwm::Dns::RRDataHINFO class definition.
domain name pointer
Definition: DwmDnsResourceRecord.hh:123
Dwm::Dns::RRDataMINFO class definition.
Encapsulates AAAA resource record data.
Definition: DwmDnsRRDataAAAA.hh:62
mail destination (old, use MX)
Definition: DwmDnsResourceRecord.hh:114
SMIMEA (RFC8162)
Definition: DwmDnsResourceRecord.hh:144
static std::string TypeName(uint16_t rrtype)
Returns the mnemonic for the given resource record type @ rrtype.
Encapsulates DHCID resource record data.
Definition: DwmDnsRRDataDHCID.hh:59
OPT (for EDNS, RFC6891)
Definition: DwmDnsResourceRecord.hh:134
RRSIG (RFC4034)
Definition: DwmDnsResourceRecord.hh:137
Encapsulates resource record data for unknown resource record types.
Definition: DwmDnsRRDataUnknown.hh:58
Dwm::Dns::RRDataDNSKEY class definition.
NSEC (RFC4034)
Definition: DwmDnsResourceRecord.hh:138
Encapsulates TXT resource record data.
Definition: DwmDnsRRDataTXT.hh:60
Encapsulates CNAME resource record data.
Definition: DwmDnsRRDataCNAME.hh:58
Encapsulates MB resource record data.
Definition: DwmDnsRRDataMB.hh:58
Encapsulates DS resource record data.
Definition: DwmDnsRRDataDS.hh:59
TypeEnum
Resource record types.
Definition: DwmDnsResourceRecord.hh:110
Encapsulates A resource record data.
Definition: DwmDnsRRDataA.hh:62
text strings
Definition: DwmDnsResourceRecord.hh:127