Kea  2.1.7-git
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 
17 namespace isc {
18 namespace util {
19 class InputBuffer;
20 class OutputBuffer;
21 }
22 
23 namespace dns {
24 class AbstractMessageRenderer;
25 
31 public:
32  EmptyLabel(const char* file, size_t line, const char* what) :
33  NameParserException(file, line, what) {}
34 };
35 
41 public:
42  TooLongName(const char* file, size_t line, const char* what) :
43  NameParserException(file, line, what) {}
44 };
45 
51 public:
52  TooLongLabel(const char* file, size_t line, const char* what) :
53  NameParserException(file, line, what) {}
54 };
55 
63 public:
64  BadLabelType(const char* file, size_t line, const char* what) :
65  NameParserException(file, line, what) {}
66 };
67 
73 public:
74  BadEscape(const char* file, size_t line, const char* what) :
75  NameParserException(file, line, what) {}
76 };
77 
86 public:
87  IncompleteName(const char* file, size_t line, const char* what) :
88  NameParserException(file, line, what) {}
89 };
90 
97 public:
98  MissingNameOrigin(const char* file, size_t line, const char* what) :
99  NameParserException(file, line, what) {}
100 };
101 
118 public:
143  SUPERDOMAIN = 0,
144  SUBDOMAIN = 1,
145  EQUAL = 2,
146  COMMONANCESTOR = 3,
147  NONE = 4
148  };
149 
153 
154  NameComparisonResult(int order, unsigned int nlabels,
159  NameRelation relation) :
160  order_(order), nlabels_(nlabels), relation_(relation) {}
162 
166 
167  int getOrder() const { return (order_); }
170  unsigned int getCommonLabels() const { return (nlabels_); }
172  NameRelation getRelation() const { return (relation_); }
174 private:
175  int order_;
176  unsigned int nlabels_;
177  NameRelation relation_;
178 };
179 
223 class 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 
233 private:
235  typedef std::basic_string<uint8_t> NameString;
237  typedef std::vector<uint8_t> NameOffsets;
238 
245  Name() : length_(0), labelcount_(0) {}
246 public:
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 
315 
316  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  //
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  NameComparisonResult compare(const Name& other) const;
449 
450 public:
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  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  bool isWildcard() const;
693 
697 
698  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  static const Name& ROOT_NAME();
729 
730 private:
731  NameString ndata_;
732  NameOffsets offsets_;
733  unsigned int length_;
734  unsigned int labelcount_;
735 };
736 
737 inline const Name&
738 Name::ROOT_NAME() {
739  static Name root_name(".");
740  return (root_name);
741 }
742 
757 std::ostream&
758 operator<<(std::ostream& os, const Name& name);
759 
760 }
761 }
762 #endif // NAME_H
763 
764 // Local Variables:
765 // mode: c++
766 // End:
The Name class encapsulates DNS names.
Definition: name.h:223
TooLongLabel(const char *file, size_t line, const char *what)
Definition: name.h:52
Thrown when origin is NULL and is needed.
Definition: name.h:96
bool nequals(const Name &other) const
Return true iff two names are not equal.
Definition: name.h:474
TooLongName(const char *file, size_t line, const char *what)
Definition: name.h:42
Base class for name parser exceptions.
bool operator<(const Name &other) const
Same as lthan()
Definition: name.h:520
BadEscape(const char *file, size_t line, const char *what)
Definition: name.h:74
EmptyLabel(const char *file, size_t line, const char *what)
Definition: name.h:32
bool operator<=(const Name &other) const
Same as leq()
Definition: name.h:491
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:100
NameRelation getRelation() const
Returns the NameRelation of the comparison result.
Definition: name.h:172
bool operator==(const Name &other) const
Same as equals()
Definition: name.h:467
MissingNameOrigin(const char *file, size_t line, const char *what)
Definition: name.h:98
unsigned int getLabelCount() const
Returns the number of labels contained in the Name.
Definition: name.h:370
This is a supplemental class used only as a return value of Name::compare() and LabelSequence::compar...
Definition: name.h:117
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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 encounters too long a name...
Definition: name.h:40
A standard DNS module exception that is thrown if the name parser encounters an empty label in the mi...
Definition: name.h:30
bool operator!=(const Name &other) const
Same as nequals()
Definition: name.h:477
A standard DNS module exception that is thrown if the name parser encounters too long a label...
Definition: name.h:50
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
size_t getLength() const
Gets the length of the Name in its wire format.
Definition: name.h:360
unsigned int getCommonLabels() const
Returns the number of common labels of the comparison result.
Definition: name.h:170
A standard DNS module exception that is thrown if the name parser encounters an obsolete or incomplet...
Definition: name.h:62
bool operator>(const Name &other) const
Same as gthan()
Definition: name.h:534
Defines the logger used by the top-level component of kea-lfc.
IncompleteName(const char *file, size_t line, const char *what)
Definition: name.h:87
A standard DNS module exception that is thrown if the name parser fails to decode a back-slash escape...
Definition: name.h:72
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 finds the input (string or wire-for...
Definition: name.h:85
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition: buffer.h:81
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
bool operator>=(const Name &other) const
Same as geq()
Definition: name.h:506
NameRelation
The relation of two names under comparison.
Definition: name.h:142
Light-weight Accessor to Name data.
Definition: labelsequence.h:35