Kea 3.3.3
name.h
Go to the documentation of this file.
1// Copyright (C) 2009-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef NAME_H
8#define NAME_H
9
10#include <dns/exceptions.h>
11#include <dns/messagerenderer.h>
12#include <util/buffer.h>
13
14#include <cstdint>
15#include <string>
16#include <vector>
17
18namespace isc {
19namespace dns {
25public:
26 EmptyLabel(const char* file, size_t line, const char* what) :
27 NameParserException(file, line, what) {}
28};
29
35public:
36 TooLongName(const char* file, size_t line, const char* what) :
37 NameParserException(file, line, what) {}
38};
39
45public:
46 TooLongLabel(const char* file, size_t line, const char* what) :
47 NameParserException(file, line, what) {}
48};
49
57public:
58 BadLabelType(const char* file, size_t line, const char* what) :
59 NameParserException(file, line, what) {}
60};
61
67public:
68 BadEscape(const char* file, size_t line, const char* what) :
69 NameParserException(file, line, what) {}
70};
71
80public:
81 IncompleteName(const char* file, size_t line, const char* what) :
82 NameParserException(file, line, what) {}
83};
84
91public:
92 MissingNameOrigin(const char* file, size_t line, const char* what) :
93 NameParserException(file, line, what) {}
94};
95
112public:
136 enum NameRelation : uint16_t {
139 EQUAL = 2,
142 };
143
147
148
152 NameComparisonResult(int order, unsigned int nlabels,
153 NameRelation relation) :
154 order_(order), nlabels_(nlabels), relation_(relation) {}
155
156
160
161
162 int getOrder() const { return (order_); }
164 unsigned int getCommonLabels() const { return (nlabels_); }
166 NameRelation getRelation() const { return (relation_); }
168private:
169 int order_;
170 unsigned int nlabels_;
171 NameRelation relation_;
172};
173
217class Name {
218 // LabelSequences use knowledge about the internal data structure
219 // of this class for efficiency (they use the offsets_ vector and
220 // the ndata_ string)
221 friend class LabelSequence;
222
226
227private:
229 typedef std::vector<uint8_t> NameString;
231 typedef std::vector<uint8_t> NameOffsets;
232
239 Name() : length_(0), labelcount_(0) {}
240public:
251 explicit Name(const std::string& namestr, bool downcase = false);
252
280 Name(const char* name_data, size_t data_len, const Name* origin,
281 bool downcase = false);
282
299 explicit Name(isc::util::InputBuffer& buff, bool downcase = false);
302
303
305
309
310
340 uint8_t at(size_t pos) const
341 {
342 if (pos >= length_) {
343 isc_throw(OutOfRange, "Out of range access in Name::at()");
344 }
345 return (ndata_[pos]);
346 }
347
354 size_t getLength() const { return (length_); }
355
364 unsigned int getLabelCount() const { return (labelcount_); }
366
370
371
384 //
387 std::string toText(bool omit_final_dot = false) const;
388
397 std::string toRawText(bool omit_final_dot = false) const;
398
410 void toWire(AbstractMessageRenderer& renderer) const;
411
422 void toWire(isc::util::OutputBuffer& buff) const;
424
428
429
442 NameComparisonResult compare(const Name& other) const;
443
444public:
458 bool equals(const Name& other) const;
459
461 bool operator==(const Name& other) const { return (equals(other)); }
462
468 bool nequals(const Name& other) const { return (!(equals(other))); }
469
471 bool operator!=(const Name& other) const { return (nequals(other)); }
472
482 bool leq(const Name& other) const;
483
485 bool operator<=(const Name& other) const { return (leq(other)); }
486
497 bool geq(const Name& other) const;
498
500 bool operator>=(const Name& other) const { return (geq(other)); }
501
511 bool lthan(const Name& other) const;
512
514 bool operator<(const Name& other) const { return (lthan(other)); }
515
525 bool gthan(const Name& other) const;
526
528 bool operator>(const Name& other) const { return (gthan(other)); }
530
534
535
562 Name split(unsigned int first, unsigned int n) const;
563
628 Name split(unsigned int level) const;
629
636 Name reverse() const;
637
651 Name concatenate(const Name& suffix) const;
652
674 Name& downcase();
676
680
681
685 bool isWildcard() const;
687
691
692
693 static const size_t MAX_WIRE = 255;
694
699 static const size_t MAX_LABELS = 128;
700
702 static const size_t MAX_LABELLEN = 63;
703
709 static const uint16_t MAX_COMPRESS_POINTER = 0x3fff;
711 static const uint16_t COMPRESS_POINTER_MARK8 = 0xc0;
713 static const uint16_t COMPRESS_POINTER_MARK16 = 0xc000;
715
719
720
721 static const Name& ROOT_NAME();
723
724private:
725 NameString ndata_;
726 NameOffsets offsets_;
727 unsigned int length_;
728 unsigned int labelcount_;
729};
730
731inline const Name&
733 static Name root_name(".");
734 return (root_name);
735}
736
751std::ostream&
752operator<<(std::ostream& os, const Name& name);
753
754}
755}
756#endif // NAME_H
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
BadEscape(const char *file, size_t line, const char *what)
Definition name.h:68
BadLabelType(const char *file, size_t line, const char *what)
Definition name.h:58
EmptyLabel(const char *file, size_t line, const char *what)
Definition name.h:26
IncompleteName(const char *file, size_t line, const char *what)
Definition name.h:81
MissingNameOrigin(const char *file, size_t line, const char *what)
Definition name.h:92
This is a supplemental class used only as a return value of Name::compare() and LabelSequence::compar...
Definition name.h:111
unsigned int getCommonLabels() const
Returns the number of common labels of the comparison result.
Definition name.h:164
int getOrder() const
Returns the ordering of the comparison result.
Definition name.h:162
NameComparisonResult(int order, unsigned int nlabels, NameRelation relation)
Constructor from a comparison tuple.
Definition name.h:152
NameRelation getRelation() const
Returns the NameRelation of the comparison result.
Definition name.h:166
NameRelation
The relation of two names under comparison.
Definition name.h:136
NameParserException(const char *file, size_t line, const char *what)
The Name class encapsulates DNS names.
Definition name.h:217
static const size_t MAX_LABELLEN
Max allowable length of labels of a domain name.
Definition name.h:702
bool operator>=(const Name &other) const
Same as geq().
Definition name.h:500
static const uint16_t MAX_COMPRESS_POINTER
Max possible pointer value for name compression.
Definition name.h:709
Name reverse() const
Reverse the labels of a name.
Definition name.cc:616
bool lthan(const Name &other) const
Less-than comparison for Name against other.
Definition name.cc:565
NameComparisonResult compare(const Name &other) const
Compare two Names.
Definition name.cc:521
bool equals(const Name &other) const
Return true iff two names are equal.
Definition name.cc:528
static const uint16_t COMPRESS_POINTER_MARK16
A 16-bit masked value indicating a start of compression pointer.
Definition name.h:713
bool geq(const Name &other) const
Greater-than or equal comparison for Name against other.
Definition name.cc:560
static const size_t MAX_WIRE
Max allowable length of domain names.
Definition name.h:693
Name split(unsigned int first, unsigned int n) const
Extract a specified subpart of Name.
Definition name.cc:645
friend class LabelSequence
Definition name.h:221
bool nequals(const Name &other) const
Return true iff two names are not equal.
Definition name.h:468
bool operator<(const Name &other) const
Same as lthan().
Definition name.h:514
uint8_t at(size_t pos) const
Provides one-byte name data in wire format at the specified position.
Definition name.h:340
bool operator!=(const Name &other) const
Same as nequals().
Definition name.h:471
std::string toText(bool omit_final_dot=false) const
Convert the Name to a string.
Definition name.cc:509
void toWire(AbstractMessageRenderer &renderer) const
Render the Name in the wire format with compression.
Definition name.cc:504
bool operator>(const Name &other) const
Same as gthan().
Definition name.h:528
unsigned int getLabelCount() const
Returns the number of labels contained in the Name.
Definition name.h:364
bool operator<=(const Name &other) const
Same as leq().
Definition name.h:485
static const size_t MAX_LABELS
Max allowable labels of domain names.
Definition name.h:699
bool gthan(const Name &other) const
Greater-than comparison for Name against other.
Definition name.cc:570
bool operator==(const Name &other) const
Same as equals().
Definition name.h:461
static const Name & ROOT_NAME()
Root name (i.e. ".").
Definition name.h:732
Name concatenate(const Name &suffix) const
Concatenate two names.
Definition name.cc:580
std::string toRawText(bool omit_final_dot=false) const
Convert the LabelSequence to a string without escape sequences.
Definition name.cc:515
size_t getLength() const
Gets the length of the Name in its wire format.
Definition name.h:354
static const uint16_t COMPRESS_POINTER_MARK8
A 8-bit masked value indicating a start of compression pointer.
Definition name.h:711
bool leq(const Name &other) const
Less-than or equal comparison for Name against other.
Definition name.cc:555
Name & downcase()
Downcase all upper case alphabet characters in the name.
Definition name.cc:693
bool isWildcard() const
Test if this is a wildcard name.
Definition name.cc:575
TooLongLabel(const char *file, size_t line, const char *what)
Definition name.h:46
TooLongName(const char *file, size_t line, const char *what)
Definition name.h:36
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition buffer.h:81
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:355
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition edns.cc:163
Defines the logger used by the top-level component of kea-lfc.