summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/rrtype.h
blob: e75225304ad102d7ba50dd371f510d6e35bf9d81 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
///////////////
///////////////
///////////////   THIS FILE IS AUTOMATICALLY GENERATED BY gen-rdatacode.py.
///////////////   DO NOT EDIT!
///////////////
///////////////

// Copyright (C) 2010-2021 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef RRTYPE_H
#define RRTYPE_H 1

#include <stdint.h>

#include <string>
#include <ostream>

#include <dns/exceptions.h>

// Solaris x86 defines DS in <sys/regset.h>, which gets pulled in by Boost
#if defined(__sun) && defined(DS)
# undef DS
#endif

namespace isc {
namespace util {
class InputBuffer;
class OutputBuffer;
}

namespace dns {

// forward declarations
class AbstractMessageRenderer;

///
/// \brief A standard DNS module exception that is thrown if an RRType object
/// is being constructed from an unrecognized string.
///
class InvalidRRType : public DNSTextError {
public:
    InvalidRRType(const char* file, size_t line, const char* what) :
        DNSTextError(file, line, what) {}
};

///
/// \brief A standard DNS module exception that is thrown if an RRType object
/// is being constructed from a incomplete (too short) wire-format data.
///
class IncompleteRRType : public isc::dns::Exception {
public:
    IncompleteRRType(const char* file, size_t line, const char* what) :
        isc::dns::Exception(file, line, what) {}
};

///
/// The \c RRType class encapsulates DNS resource record types.
///
/// This class manages the 16-bit integer type codes in quite a straightforward
/// way.  The only non trivial task is to handle textual representations of
/// RR types, such as "A", "AAAA", or "TYPE65534".
///
/// This class consults a helper \c RRParamRegistry class, which is a registry
/// of RR related parameters and has the singleton object.  This registry
/// provides a mapping between RR type codes and their "well-known" textual
/// representations.
/// Parameters of RR types defined by DNS protocol standards are automatically
/// registered at initialization time and are ensured to be always available for
/// applications unless the application explicitly modifies the registry.
///
/// For convenience, this class defines constant class objects corresponding to
/// standard RR types.  These are generally referred to as the form of
/// <code>RRType::{type-text}()</code>.
/// For example, \c RRType::NS() is an \c RRType object corresponding to the NS
/// resource record (type code 2).
/// Note that these constants are used through a "proxy" function.
/// This is because they may be used to initialize another non-local (e.g.
/// global or namespace-scope) static object as follows:
///
/// \code
/// namespace foo {
/// const RRType default_type = RRType::A();
/// } \endcode
///
/// In order to ensure that the constant RRType object has been initialized
/// before the initialization for \c default_type, we need help from
/// the proxy function.
///
/// In the current implementation, the initialization of the well-known
/// static objects is not thread safe.  The same consideration as the
/// \c RRParamRegistry class applies.  We may extend the implementation so
/// that the initialization is ensured to be thread safe in a future version.
///
/// Note to developers: since it's expected that some of these constant
/// \c RRType objects are frequently used in a performance sensitive path,
/// we define these proxy functions as inline.  This makes sense only when
/// the corresponding static objects are defined only once even if they used
/// in different source files.  Sufficiently modern compilers should meet
/// this assumption, but if we encounter memory bloat due to this problem with
/// particular compilers we need to revisit the design or think about
/// workaround.
class RRType {
public:
    ///
    /// \name Constructors and Destructor
    ///
    //@{
    /// Constructor from an integer type code.
    ///
    /// This constructor never throws an exception.
    ///
    /// \param typecode An 16-bit integer code corresponding to the RRType.
    explicit RRType(uint16_t typecode) : typecode_(typecode) {}
    /// Constructor from a string.
    ///
    /// A valid string is one of "well-known" textual type representations
    /// such as "A", "AAAA", or "NS", or in the standard format for "unknown"
    /// RR types as defined in RFC3597, i.e., "TYPEnnnn".
    ///
    /// More precisely, the "well-known" representations are the ones stored
    /// in the \c RRParamRegistry registry (see the class description).
    ///
    /// As for the format of "TYPEnnnn", "nnnn" must represent a valid 16-bit
    /// unsigned integer, which may contain leading 0's as long as it consists
    /// of at most 5 characters (inclusive).
    /// For example, "TYPE1" and "TYPE001" are valid and represent the same
    /// RR type, but "TYPE65536" and "TYPE000001" are invalid.
    /// A "TYPEnnnn" representation is valid even if the corresponding type code
    /// is registered in the \c RRParamRegistry object.  For example, both
    /// "A" and "TYPE1" are valid and represent the same RR type.
    ///
    /// All of these representations are case insensitive; "NS" and "ns", and
    /// "TYPE1" and "type1" are all valid and represent the same RR types,
    /// respectively.
    ///
    /// If the given string is not recognized as a valid representation of
    /// an RR type, an exception of class \c InvalidRRType will be thrown.
    ///
    /// \param typestr A string representation of the \c RRType
    explicit RRType(const std::string& typestr);
    /// Constructor from wire-format data.
    ///
    /// The \c buffer parameter normally stores a complete DNS message
    /// containing the RRType to be constructed.  The current read position of
    /// the buffer points to the head of the type.
    ///
    /// If the given data does not large enough to contain a 16-bit integer,
    /// an exception of class \c IncompleteRRType will be thrown.
    ///
    /// \param buffer A buffer storing the wire format data.
    explicit RRType(isc::util::InputBuffer& buffer);
    ///
    /// We use the default copy constructor intentionally.
    //@}
    /// We use the default copy assignment operator intentionally.
    ///

    ///
    /// \name Converter methods
    ///
    //@{
    /// \brief Convert the \c RRType to a string.
    ///
    /// If a "well known" textual representation for the type code is registered
    /// in the RR parameter registry (see the class description), that will be
    /// used as the return value of this method.  Otherwise, this method creates
    /// a new string for an "unknown" RR type in the format defined in RFC3597,
    /// i.e., "TYPEnnnn", and returns it.
    ///
    /// If resource allocation for the string fails, a corresponding standard
    /// exception will be thrown.
    ///
    /// \return A string representation of the \c RRType.
    const std::string toText() const;
    /// \brief Render the \c RRType in the wire format.
    ///
    /// This method renders the type code in network byte order via \c renderer,
    /// which encapsulates output buffer and other rendering contexts.
    ///
    /// If resource allocation in rendering process fails, a corresponding
    /// standard exception will be thrown.
    ///
    /// \param renderer DNS message rendering context that encapsulates the
    /// output buffer in which the RRType is to be stored.
    void toWire(AbstractMessageRenderer& renderer) const;
    /// \brief Render the \c RRType in the wire format.
    ///
    /// This method renders the type code in network byte order into the
    /// \c buffer.
    ///
    /// If resource allocation in rendering process fails, a corresponding
    /// standard exception will be thrown.
    ///
    /// \param buffer An output buffer to store the wire data.
    void toWire(isc::util::OutputBuffer& buffer) const;
    //@}

    ///
    /// \name Getter Methods
    ///
    //@{
    /// \brief Returns the RR type code as a 16-bit unsigned integer.
    ///
    /// This method never throws an exception.
    ///
    /// \return An 16-bit integer code corresponding to the RRType.
    uint16_t getCode() const { return (typecode_); }
    //@}

    ///
    /// \name Comparison methods
    ///
    //@{
    /// \brief Return true iff two RRTypes are equal.
    ///
    /// Two RRTypes are equal iff their type codes are equal.
    ///
    /// This method never throws an exception.
    ///
    /// \param other the \c RRType object to compare against.
    /// \return true if the two RRTypes are equal; otherwise false.
    bool equals(const RRType& other) const
    { return (typecode_ == other.typecode_); }
    /// \brief Same as \c equals().
    bool operator==(const RRType& other) const { return (equals(other)); }

    /// \brief Return true iff two RRTypes are not equal.
    ///
    /// This method never throws an exception.
    ///
    /// \param other the \c RRType object to compare against.
    /// \return true if the two RRTypes are not equal; otherwise false.
    bool nequals(const RRType& other) const
    { return (typecode_ != other.typecode_); }
    /// \brief Same as \c nequals().
    bool operator!=(const RRType& other) const { return (nequals(other)); }

    /// \brief Less-than comparison for RRType against \c other
    ///
    /// We define the less-than relationship based on their type codes;
    /// one RRType is less than the other iff the code of the former is less
    /// than that of the other as unsigned integers.
    /// The relationship is meaningless in terms of DNS protocol; the only
    /// reason we define this method is that RRType objects can be stored in
    /// STL containers without requiring user-defined less-than relationship.
    /// We therefore don't define other comparison operators.
    ///
    /// This method never throws an exception.
    ///
    /// \param other the \c RRType object to compare against.
    /// \return true if \c this RRType is less than the \c other; otherwise
    /// false.
    bool operator<(const RRType& other) const
    { return (typecode_ < other.typecode_); }
    //@}

    // BEGIN_WELL_KNOWN_TYPE_DECLARATIONS
    static const RRType& TSIG();
    static const RRType& A();
    static const RRType& AFSDB();
    static const RRType& CAA();
    static const RRType& CNAME();
    static const RRType& DLV();
    static const RRType& DNAME();
    static const RRType& DNSKEY();
    static const RRType& DS();
    static const RRType& HINFO();
    static const RRType& MINFO();
    static const RRType& MX();
    static const RRType& NAPTR();
    static const RRType& NS();
    static const RRType& NSEC3();
    static const RRType& NSEC3PARAM();
    static const RRType& NSEC();
    static const RRType& OPT();
    static const RRType& PTR();
    static const RRType& RP();
    static const RRType& RRSIG();
    static const RRType& SOA();
    static const RRType& SPF();
    static const RRType& SSHFP();
    static const RRType& TKEY();
    static const RRType& TLSA();
    static const RRType& TXT();
    static const RRType& AAAA();
    static const RRType& DHCID();
    static const RRType& SRV();
    static const RRType& IXFR();
    static const RRType& AXFR();
    static const RRType& ANY();
    static const RRType& MD();
    static const RRType& MF();
    static const RRType& MB();
    static const RRType& MG();
    static const RRType& MR();
    static const RRType& NXT();
    static const RRType& A6();
    static const RRType& MAILA();
    static const RRType& Null();
    static const RRType& WKS();
    static const RRType& X25();
    static const RRType& RT();
    static const RRType& NSAP();
    static const RRType& NSAP_PTR();
    static const RRType& SIG();
    static const RRType& ISDN();
    static const RRType& KEY();
    static const RRType& PX();
    static const RRType& GPOS();
    static const RRType& LOC();
    static const RRType& KX();
    static const RRType& CERT();
    static const RRType& APL();
    static const RRType& IPSECKEY();
    static const RRType& HIP();
    static const RRType& UNSPEC();
    static const RRType& NID();
    static const RRType& L32();
    static const RRType& L64();
    static const RRType& LP();
    static const RRType& MAILB();
    static const RRType& URI();
    // END_WELL_KNOWN_TYPE_DECLARATIONS

private:
    uint16_t typecode_;
};

// BEGIN_WELL_KNOWN_TYPE_DEFINITIONS
inline const RRType&
RRType::TSIG() {
    static RRType rrtype(250);
    return (rrtype);
}

inline const RRType&
RRType::A() {
    static RRType rrtype(1);
    return (rrtype);
}

inline const RRType&
RRType::AFSDB() {
    static RRType rrtype(18);
    return (rrtype);
}

inline const RRType&
RRType::CAA() {
    static RRType rrtype(257);
    return (rrtype);
}

inline const RRType&
RRType::CNAME() {
    static RRType rrtype(5);
    return (rrtype);
}

inline const RRType&
RRType::DLV() {
    static RRType rrtype(32769);
    return (rrtype);
}

inline const RRType&
RRType::DNAME() {
    static RRType rrtype(39);
    return (rrtype);
}

inline const RRType&
RRType::DNSKEY() {
    static RRType rrtype(48);
    return (rrtype);
}

inline const RRType&
RRType::DS() {
    static RRType rrtype(43);
    return (rrtype);
}

inline const RRType&
RRType::HINFO() {
    static RRType rrtype(13);
    return (rrtype);
}

inline const RRType&
RRType::MINFO() {
    static RRType rrtype(14);
    return (rrtype);
}

inline const RRType&
RRType::MX() {
    static RRType rrtype(15);
    return (rrtype);
}

inline const RRType&
RRType::NAPTR() {
    static RRType rrtype(35);
    return (rrtype);
}

inline const RRType&
RRType::NS() {
    static RRType rrtype(2);
    return (rrtype);
}

inline const RRType&
RRType::NSEC3() {
    static RRType rrtype(50);
    return (rrtype);
}

inline const RRType&
RRType::NSEC3PARAM() {
    static RRType rrtype(51);
    return (rrtype);
}

inline const RRType&
RRType::NSEC() {
    static RRType rrtype(47);
    return (rrtype);
}

inline const RRType&
RRType::OPT() {
    static RRType rrtype(41);
    return (rrtype);
}

inline const RRType&
RRType::PTR() {
    static RRType rrtype(12);
    return (rrtype);
}

inline const RRType&
RRType::RP() {
    static RRType rrtype(17);
    return (rrtype);
}

inline const RRType&
RRType::RRSIG() {
    static RRType rrtype(46);
    return (rrtype);
}

inline const RRType&
RRType::SOA() {
    static RRType rrtype(6);
    return (rrtype);
}

inline const RRType&
RRType::SPF() {
    static RRType rrtype(99);
    return (rrtype);
}

inline const RRType&
RRType::SSHFP() {
    static RRType rrtype(44);
    return (rrtype);
}

inline const RRType&
RRType::TKEY() {
    static RRType rrtype(249);
    return (rrtype);
}

inline const RRType&
RRType::TLSA() {
    static RRType rrtype(52);
    return (rrtype);
}

inline const RRType&
RRType::TXT() {
    static RRType rrtype(16);
    return (rrtype);
}

inline const RRType&
RRType::AAAA() {
    static RRType rrtype(28);
    return (rrtype);
}

inline const RRType&
RRType::DHCID() {
    static RRType rrtype(49);
    return (rrtype);
}

inline const RRType&
RRType::SRV() {
    static RRType rrtype(33);
    return (rrtype);
}

inline const RRType&
RRType::IXFR() {
    static RRType rrtype(251);
    return (rrtype);
}

inline const RRType&
RRType::AXFR() {
    static RRType rrtype(252);
    return (rrtype);
}

inline const RRType&
RRType::ANY() {
    static RRType rrtype(255);
    return (rrtype);
}

inline const RRType&
RRType::MD() {
    static RRType rrtype(3);
    return (rrtype);
}

inline const RRType&
RRType::MF() {
    static RRType rrtype(4);
    return (rrtype);
}

inline const RRType&
RRType::MB() {
    static RRType rrtype(7);
    return (rrtype);
}

inline const RRType&
RRType::MG() {
    static RRType rrtype(8);
    return (rrtype);
}

inline const RRType&
RRType::MR() {
    static RRType rrtype(9);
    return (rrtype);
}

inline const RRType&
RRType::NXT() {
    static RRType rrtype(30);
    return (rrtype);
}

inline const RRType&
RRType::A6() {
    static RRType rrtype(38);
    return (rrtype);
}

inline const RRType&
RRType::MAILA() {
    static RRType rrtype(254);
    return (rrtype);
}

inline const RRType&
RRType::Null() {
    static RRType rrtype(10);
    return (rrtype);
}

inline const RRType&
RRType::WKS() {
    static RRType rrtype(11);
    return (rrtype);
}

inline const RRType&
RRType::X25() {
    static RRType rrtype(19);
    return (rrtype);
}

inline const RRType&
RRType::RT() {
    static RRType rrtype(21);
    return (rrtype);
}

inline const RRType&
RRType::NSAP() {
    static RRType rrtype(22);
    return (rrtype);
}

inline const RRType&
RRType::NSAP_PTR() {
    static RRType rrtype(23);
    return (rrtype);
}

inline const RRType&
RRType::SIG() {
    static RRType rrtype(24);
    return (rrtype);
}

inline const RRType&
RRType::ISDN() {
    static RRType rrtype(20);
    return (rrtype);
}

inline const RRType&
RRType::KEY() {
    static RRType rrtype(25);
    return (rrtype);
}

inline const RRType&
RRType::PX() {
    static RRType rrtype(26);
    return (rrtype);
}

inline const RRType&
RRType::GPOS() {
    static RRType rrtype(27);
    return (rrtype);
}

inline const RRType&
RRType::LOC() {
    static RRType rrtype(29);
    return (rrtype);
}

inline const RRType&
RRType::KX() {
    static RRType rrtype(36);
    return (rrtype);
}

inline const RRType&
RRType::CERT() {
    static RRType rrtype(37);
    return (rrtype);
}

inline const RRType&
RRType::APL() {
    static RRType rrtype(42);
    return (rrtype);
}

inline const RRType&
RRType::IPSECKEY() {
    static RRType rrtype(45);
    return (rrtype);
}

inline const RRType&
RRType::HIP() {
    static RRType rrtype(55);
    return (rrtype);
}

inline const RRType&
RRType::UNSPEC() {
    static RRType rrtype(103);
    return (rrtype);
}

inline const RRType&
RRType::NID() {
    static RRType rrtype(104);
    return (rrtype);
}

inline const RRType&
RRType::L32() {
    static RRType rrtype(105);
    return (rrtype);
}

inline const RRType&
RRType::L64() {
    static RRType rrtype(106);
    return (rrtype);
}

inline const RRType&
RRType::LP() {
    static RRType rrtype(107);
    return (rrtype);
}

inline const RRType&
RRType::MAILB() {
    static RRType rrtype(253);
    return (rrtype);
}

inline const RRType&
RRType::URI() {
    static RRType rrtype(256);
    return (rrtype);
}

// END_WELL_KNOWN_TYPE_DEFINITIONS

///
/// \brief Insert the \c RRType as a string into stream.
///
/// This method convert the \c rrtype into a string and inserts it into the
/// output stream \c os.
///
/// This function overloads the global operator<< to behave as described in
/// ostream::operator<< but applied to \c RRType objects.
///
/// \param os A \c std::ostream object on which the insertion operation is
/// performed.
/// \param rrtype The \c RRType object output by the operation.
/// \return A reference to the same \c std::ostream object referenced by
/// parameter \c os after the insertion operation.
std::ostream&
operator<<(std::ostream& os, const RRType& rrtype);
}
}
#endif  // RRTYPE_H

// Local Variables:
// mode: c++
// End: