Kea 2.5.6
name.h
Go to the documentation of this file.
1// Copyright (C) 2009-2019 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 1
9
10#include <stdint.h>
11
12#include <string>
13#include <vector>
14
15#include <dns/exceptions.h>
16
17namespace isc {
18namespace util {
19class InputBuffer;
20class OutputBuffer;
21}
22
23namespace dns {
24class AbstractMessageRenderer;
25
31public:
32 EmptyLabel(const char* file, size_t line, const char* what) :
33 NameParserException(file, line, what) {}
34};
35
41public:
42 TooLongName(const char* file, size_t line, const char* what) :
43 NameParserException(file, line, what) {}
44};
45
51public:
52 TooLongLabel(const char* file, size_t line, const char* what) :
53 NameParserException(file, line, what) {}
54};
55
63public:
64 BadLabelType(const char* file, size_t line, const char* what) :
65 NameParserException(file, line, what) {}
66};
67
73public:
74 BadEscape(const char* file, size_t line, const char* what) :
75 NameParserException(file, line, what) {}
76};
77
86public:
87 IncompleteName(const char* file, size_t line, const char* what) :
88 NameParserException(file, line, what) {}
89};
90
97public:
98 MissingNameOrigin(const char* file, size_t line, const char* what) :
99 NameParserException(file, line, what) {}
100};
101
118public:
145 EQUAL = 2,
147 NONE = 4
148 };
149
153
154
158 NameComparisonResult(int order, unsigned int nlabels,
159 NameRelation relation) :
160 order_(order), nlabels_(nlabels), relation_(relation) {}
162
166
167
168 int getOrder() const { return (order_); }
170 unsigned int getCommonLabels() const { return (nlabels_); }
172 NameRelation getRelation() const { return (relation_); }
174private:
175 int order_;
176 unsigned int nlabels_;
177 NameRelation relation_;
178};
179
223class Name {
224 // LabelSequences use knowledge about the internal data structure
225 // of this class for efficiency (they use the offsets_ vector and
226 // the ndata_ string)
227 friend class LabelSequence;
228
232
233private:
235 typedef std::basic_string<uint8_t> NameString;
237 typedef std::vector<uint8_t> NameOffsets;
238
245 Name() : length_(0), labelcount_(0) {}
246public:
257 explicit Name(const std::string& namestr, bool downcase = false);
258
286 Name(const char* name_data, size_t data_len, const Name* origin,
287 bool downcase = false);
288
305 explicit Name(isc::util::InputBuffer& buffer, bool downcase = false);
308
309
311
315
316
346 uint8_t at(size_t pos) const
347 {
348 if (pos >= length_) {
349 isc_throw(OutOfRange, "Out of range access in Name::at()");
350 }
351 return (ndata_[pos]);
352 }
353
360 size_t getLength() const { return (length_); }
361
370 unsigned int getLabelCount() const { return (labelcount_); }
372
376
377
390 //
393 std::string toText(bool omit_final_dot = false) const;
394
403 std::string toRawText(bool omit_final_dot = false) const;
404
416 void toWire(AbstractMessageRenderer& renderer) const;
417
428 void toWire(isc::util::OutputBuffer& buffer) const;
430
434
435
448 NameComparisonResult compare(const Name& other) const;
449
450public:
464 bool equals(const Name& other) const;
465
467 bool operator==(const Name& other) const { return (equals(other)); }
468
474 bool nequals(const Name& other) const { return (!(equals(other))); }
475
477 bool operator!=(const Name& other) const { return (nequals(other)); }
478
488 bool leq(const Name& other) const;
489
491 bool operator<=(const Name& other) const { return (leq(other)); }
492
503 bool geq(const Name& other) const;
504
506 bool operator>=(const Name& other) const { return (geq(other)); }
507
517 bool lthan(const Name& other) const;
518
520 bool operator<(const Name& other) const { return (lthan(other)); }
521
531 bool gthan(const Name& other) const;
532
534 bool operator>(const Name& other) const { return (gthan(other)); }
536
540
541
568 Name split(unsigned int first, unsigned int n) const;
569
634 Name split(unsigned int level) const;
635
642 Name reverse() const;
643
657 Name concatenate(const Name& suffix) const;
658
680 Name& downcase();
682
686
687
691 bool isWildcard() const;
693
697
698
699 static const size_t MAX_WIRE = 255;
700
705 static const size_t MAX_LABELS = 128;
706
708 static const size_t MAX_LABELLEN = 63;
709
715 static const uint16_t MAX_COMPRESS_POINTER = 0x3fff;
717 static const uint16_t COMPRESS_POINTER_MARK8 = 0xc0;
719 static const uint16_t COMPRESS_POINTER_MARK16 = 0xc000;
721
725
726
727 static const Name& ROOT_NAME();
729
730private:
731 NameString ndata_;
732 NameOffsets offsets_;
733 unsigned int length_;
734 unsigned int labelcount_;
735};
736
737inline const Name&
739 static Name root_name(".");
740 return (root_name);
741}
742
757std::ostream&
758operator<<(std::ostream& os, const Name& name);
759
760}
761}
762#endif // NAME_H
763
764// Local Variables:
765// mode: c++
766// End:
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...
A standard DNS module exception that is thrown if the name parser fails to decode a back-slash escape...
Definition: name.h:72
BadEscape(const char *file, size_t line, const char *what)
Definition: name.h:74
A standard DNS module exception that is thrown if the name parser encounters an obsolete or incomplet...
Definition: name.h:62
BadLabelType(const char *file, size_t line, const char *what)
Definition: name.h:64
A standard DNS module exception that is thrown if the name parser encounters an empty label in the mi...
Definition: name.h:30
EmptyLabel(const char *file, size_t line, const char *what)
Definition: name.h:32
A standard DNS module exception that is thrown if the name parser finds the input (string or wire-for...
Definition: name.h:85
IncompleteName(const char *file, size_t line, const char *what)
Definition: name.h:87
Light-weight Accessor to Name data.
Definition: labelsequence.h:35
Thrown when origin is NULL and is needed.
Definition: name.h:96
MissingNameOrigin(const char *file, size_t line, const char *what)
Definition: name.h:98
This is a supplemental class used only as a return value of Name::compare() and LabelSequence::compar...
Definition: name.h:117
unsigned int getCommonLabels() const
Returns the number of common labels of the comparison result.
Definition: name.h:170
int getOrder() const
Returns the ordering of the comparison result.
Definition: name.h:168
NameComparisonResult(int order, unsigned int nlabels, NameRelation relation)
Constructor from a comparison tuple.
Definition: name.h:158
NameRelation getRelation() const
Returns the NameRelation of the comparison result.
Definition: name.h:172
NameRelation
The relation of two names under comparison.
Definition: name.h:142
Base class for name parser exceptions.
The Name class encapsulates DNS names.
Definition: name.h:223
static const size_t MAX_LABELLEN
Max allowable length of labels of a domain name.
Definition: name.h:708
bool operator>=(const Name &other) const
Same as geq()
Definition: name.h:506
static const uint16_t MAX_COMPRESS_POINTER
Max possible pointer value for name compression.
Definition: name.h:715
Name reverse() const
Reverse the labels of a name.
Definition: name.cc:613
bool lthan(const Name &other) const
Less-than comparison for Name against other
Definition: name.cc:562
NameComparisonResult compare(const Name &other) const
Compare two Names.
Definition: name.cc:518
bool equals(const Name &other) const
Return true iff two names are equal.
Definition: name.cc:525
static const uint16_t COMPRESS_POINTER_MARK16
A 16-bit masked value indicating a start of compression pointer.
Definition: name.h:719
bool geq(const Name &other) const
Greater-than or equal comparison for Name against other
Definition: name.cc:557
static const size_t MAX_WIRE
Max allowable length of domain names.
Definition: name.h:699
Name split(unsigned int first, unsigned int n) const
Extract a specified subpart of Name.
Definition: name.cc:642
bool nequals(const Name &other) const
Return true iff two names are not equal.
Definition: name.h:474
bool operator<(const Name &other) const
Same as lthan()
Definition: name.h:520
uint8_t at(size_t pos) const
Provides one-byte name data in wire format at the specified position.
Definition: name.h:346
bool operator!=(const Name &other) const
Same as nequals()
Definition: name.h:477
std::string toText(bool omit_final_dot=false) const
Convert the Name to a string.
Definition: name.cc:506
void toWire(AbstractMessageRenderer &renderer) const
Render the Name in the wire format with compression.
Definition: name.cc:501
bool operator>(const Name &other) const
Same as gthan()
Definition: name.h:534
unsigned int getLabelCount() const
Returns the number of labels contained in the Name.
Definition: name.h:370
bool operator<=(const Name &other) const
Same as leq()
Definition: name.h:491
static const size_t MAX_LABELS
Max allowable labels of domain names.
Definition: name.h:705
bool gthan(const Name &other) const
Greater-than comparison for Name against other
Definition: name.cc:567
bool operator==(const Name &other) const
Same as equals()
Definition: name.h:467
static const Name & ROOT_NAME()
Root name (i.e. ".").
Definition: name.h:738
Name concatenate(const Name &suffix) const
Concatenate two names.
Definition: name.cc:577
std::string toRawText(bool omit_final_dot=false) const
Convert the LabelSequence to a string without escape sequences.
Definition: name.cc:512
size_t getLength() const
Gets the length of the Name in its wire format.
Definition: name.h:360
static const uint16_t COMPRESS_POINTER_MARK8
A 8-bit masked value indicating a start of compression pointer.
Definition: name.h:717
bool leq(const Name &other) const
Less-than or equal comparison for Name against other
Definition: name.cc:552
Name & downcase()
Downcase all upper case alphabet characters in the name.
Definition: name.cc:689
bool isWildcard() const
Test if this is a wildcard name.
Definition: name.cc:572
A standard DNS module exception that is thrown if the name parser encounters too long a label.
Definition: name.h:50
TooLongLabel(const char *file, size_t line, const char *what)
Definition: name.h:52
A standard DNS module exception that is thrown if the name parser encounters too long a name.
Definition: name.h:40
TooLongName(const char *file, size_t line, const char *what)
Definition: name.h:42
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:294
#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:172
Defines the logger used by the top-level component of kea-lfc.