name.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __NAME_H
  15. #define __NAME_H 1
  16. #include <stdint.h>
  17. #include <string>
  18. #include <vector>
  19. #include <exceptions/exceptions.h>
  20. namespace isc {
  21. namespace util {
  22. class InputBuffer;
  23. class OutputBuffer;
  24. }
  25. namespace dns {
  26. class AbstractMessageRenderer;
  27. ///
  28. /// \brief Base class for name parser exceptions.
  29. ///
  30. class NameParserException : public Exception {
  31. public:
  32. NameParserException(const char* file, size_t line, const char* what) :
  33. isc::Exception(file, line, what) {}
  34. };
  35. ///
  36. /// \brief A standard DNS module exception that is thrown if the name parser
  37. /// encounters an empty label in the middle of a name.
  38. ///
  39. class EmptyLabel : public NameParserException {
  40. public:
  41. EmptyLabel(const char* file, size_t line, const char* what) :
  42. NameParserException(file, line, what) {}
  43. };
  44. ///
  45. /// \brief A standard DNS module exception that is thrown if the name parser
  46. /// encounters too long a name.
  47. ///
  48. class TooLongName : public NameParserException {
  49. public:
  50. TooLongName(const char* file, size_t line, const char* what) :
  51. NameParserException(file, line, what) {}
  52. };
  53. ///
  54. /// \brief A standard DNS module exception that is thrown if the name parser
  55. /// encounters too long a label.
  56. ///
  57. class TooLongLabel : public NameParserException {
  58. public:
  59. TooLongLabel(const char* file, size_t line, const char* what) :
  60. NameParserException(file, line, what) {}
  61. };
  62. ///
  63. /// \brief A standard DNS module exception that is thrown if the name parser
  64. /// encounters an obsolete or incomplete label type. In effect "obsolete" only
  65. /// applies to bitstring labels, which would begin with "\[". Incomplete cases
  66. /// include an incomplete escaped sequence such as "\12".
  67. ///
  68. class BadLabelType : public NameParserException {
  69. public:
  70. BadLabelType(const char* file, size_t line, const char* what) :
  71. NameParserException(file, line, what) {}
  72. };
  73. ///
  74. /// \brief A standard DNS module exception that is thrown if the name parser
  75. /// fails to decode a "\"-escaped sequence.
  76. ///
  77. class BadEscape : public NameParserException {
  78. public:
  79. BadEscape(const char* file, size_t line, const char* what) :
  80. NameParserException(file, line, what) {}
  81. };
  82. ///
  83. /// \brief A standard DNS module exception that is thrown if the name parser
  84. /// finds the input (string or wire-format %data) is incomplete.
  85. ///
  86. /// An attempt of constructing a name from an empty string will trigger this
  87. /// exception.
  88. ///
  89. class IncompleteName : public NameParserException {
  90. public:
  91. IncompleteName(const char* file, size_t line, const char* what) :
  92. NameParserException(file, line, what) {}
  93. };
  94. ///
  95. /// This is a supplemental class used only as a return value of Name::compare().
  96. /// It encapsulate a tuple of the comparison: ordering, number of common labels,
  97. /// and relationship as follows:
  98. /// - ordering: relative ordering under the DNSSEC order relation
  99. /// - labels: the number of common significant labels of the two names being
  100. /// compared
  101. /// - relationship: see NameComparisonResult::NameRelation
  102. ///
  103. class NameComparisonResult {
  104. public:
  105. /// The relation of two names under comparison.
  106. /// Its semantics for the case of
  107. /// <code>name1->compare(name2)</code> (where name1 and name2 are instances
  108. /// of the Name class) is as follows:
  109. /// - SUPERDOMAIN: name1 properly contains name2; name2 is a proper
  110. /// subdomain of name1
  111. /// - SUBDOMAIN: name1 is a proper subdomain of name2
  112. /// - EQUAL: name1 and name2 are equal
  113. /// - COMMONANCESTOR: name1 and name2 share a common ancestor
  114. ///
  115. /// Note that in our implementation there's always a hierarchical
  116. /// relationship between any two names since all names are absolute and
  117. /// they at least share the trailing empty label.
  118. /// So, for example, the relationship between "com." and "net." is
  119. /// "commonancestor". This may be counter intuitive and inconvenient, but
  120. /// we'll keep this design at the moment until we decide whether and how to
  121. /// handle "non absolute" names (see the description of the \c Name class).
  122. /// If we want to (re)introduce the notion of non absolute names, we'll
  123. /// want to distinguish "com" and "com.", and the current definition would
  124. /// be more compatible for that purpose.
  125. /// If, on the other hand, we finally decide we really don't need that
  126. /// notion, we'll probably reconsider the design here, too.
  127. enum NameRelation {
  128. SUPERDOMAIN = 0,
  129. SUBDOMAIN = 1,
  130. EQUAL = 2,
  131. COMMONANCESTOR = 3
  132. };
  133. ///
  134. /// \name Constructors and Destructor
  135. ///
  136. //@{
  137. /// \brief Constructor from a comparison tuple
  138. ///
  139. /// This constructor simply initializes the object in the straightforward
  140. /// way.
  141. NameComparisonResult(int order, unsigned int nlabels,
  142. NameRelation relation) :
  143. order_(order), nlabels_(nlabels), relation_(relation) {}
  144. //@}
  145. ///
  146. /// \name Getter Methods
  147. ///
  148. //@{
  149. /// Returns the ordering of the comparison result
  150. int getOrder() const { return (order_); }
  151. /// Returns the number of common labels of the comparison result
  152. unsigned int getCommonLabels() const { return (nlabels_); }
  153. /// Returns the NameRelation of the comparison result
  154. NameRelation getRelation() const { return (relation_); }
  155. //@}
  156. private:
  157. int order_;
  158. unsigned int nlabels_;
  159. NameRelation relation_;
  160. };
  161. ///
  162. /// The \c Name class encapsulates DNS names.
  163. ///
  164. /// It provides interfaces to construct a name from string or wire-format %data,
  165. /// transform a name into a string or wire-format %data, compare two names, get
  166. /// access to various properties of a name, etc.
  167. ///
  168. /// Notes to developers: Internally, a name object maintains the name %data
  169. /// in wire format as an instance of \c std::string. Since many string
  170. /// implementations adopt copy-on-write %data sharing, we expect this approach
  171. /// will make copying a name less expensive in typical cases. If this is
  172. /// found to be a significant performance bottleneck later, we may reconsider
  173. /// the internal representation or perhaps the API.
  174. ///
  175. /// A name object also maintains a vector of offsets (\c offsets_ member),
  176. /// each of which is the offset to a label of the name: The n-th element of
  177. /// the vector specifies the offset to the n-th label. For example, if the
  178. /// object represents "www.example.com", the elements of the offsets vector
  179. /// are 0, 4, 12, and 16. Note that the offset to the trailing dot (16) is
  180. /// included. In the BIND9 DNS library from which this implementation is
  181. /// derived, the offsets are optional, probably due to performance
  182. /// considerations (in fact, offsets can always be calculated from the name
  183. /// %data, and in that sense are redundant). In our implementation, however,
  184. /// we always build and maintain the offsets. We believe we need more low
  185. /// level, specialized %data structure and interface where we really need to
  186. /// pursue performance, and would rather keep this generic API and
  187. /// implementation simpler.
  188. ///
  189. /// While many other DNS APIs introduce an "absolute or relative"
  190. /// attribute of names as defined in RFC1035, names are always "absolute" in
  191. /// the initial design of this API.
  192. /// In fact, separating absolute and relative would confuse API users
  193. /// unnecessarily. For example, it's not so intuitive to consider the
  194. /// comparison result of an absolute name with a relative name.
  195. /// We've looked into how the concept of absolute names is used in BIND9,
  196. /// and found that in many cases names are generally absolute.
  197. /// The only reasonable case of separating absolute and relative is in a master
  198. /// file parser, where a relative name must be a complete name with an "origin"
  199. /// name, which must be absolute. So, in this initial design, we chose a
  200. /// simpler approach: the API generally handles names as absolute; when we
  201. /// introduce a parser of master files, we'll introduce the notion of relative
  202. /// names as a special case.
  203. ///
  204. class Name {
  205. // LabelSequences use knowledge about the internal data structure
  206. // of this class for efficiency (they use the offsets_ vector and
  207. // the ndata_ string)
  208. friend class LabelSequence;
  209. ///
  210. /// \name Constructors and Destructor
  211. ///
  212. //@{
  213. private:
  214. /// The default constructor
  215. ///
  216. /// This is used internally in the class implementation, but at least at
  217. /// the moment defined as private because it will construct an incomplete
  218. /// object in that it doesn't have any labels. We may reconsider this
  219. /// design choice as we see more applications of the class.
  220. Name() : length_(0), labelcount_(0) {}
  221. public:
  222. /// Constructor from a string
  223. ///
  224. /// If the given string does not represent a valid DNS name, an exception
  225. /// of class \c EmptyLabel, \c TooLongLabel, \c BadLabelType, \c BadEscape,
  226. /// \c TooLongName, or \c IncompleteName will be thrown.
  227. /// In addition, if resource allocation for the new name fails, a
  228. /// corresponding standard exception will be thrown.
  229. ///
  230. /// \param namestr A string representation of the name to be constructed.
  231. /// \param downcase Whether to convert upper case alphabets to lower case.
  232. explicit Name(const std::string& namestr, bool downcase = false);
  233. /// Constructor from wire-format %data.
  234. ///
  235. /// The \c buffer parameter normally stores a complete DNS message
  236. /// containing the name to be constructed. The current read position of
  237. /// the buffer points to the head of the name.
  238. ///
  239. /// The input %data may or may not be compressed; if it's compressed, this
  240. /// method will automatically decompress it.
  241. ///
  242. /// If the given %data does not represent a valid DNS name, an exception
  243. /// of class \c DNSMessageFORMERR will be thrown.
  244. /// In addition, if resource allocation for the new name fails, a
  245. /// corresponding standard exception will be thrown.
  246. ///
  247. /// \param buffer A buffer storing the wire format %data.
  248. /// \param downcase Whether to convert upper case alphabets to lower case.
  249. explicit Name(isc::util::InputBuffer& buffer, bool downcase = false);
  250. ///
  251. /// We use the default copy constructor intentionally.
  252. //@}
  253. /// We use the default copy assignment operator intentionally.
  254. ///
  255. ///
  256. /// \name Getter Methods
  257. ///
  258. //@{
  259. /// \brief Provides one-byte name %data in wire format at the specified
  260. /// position.
  261. ///
  262. /// This method returns the unsigned 8-bit value of wire-format \c Name
  263. /// %data at the given position from the head.
  264. ///
  265. /// For example, if \c n is a \c Name object for "example.com",
  266. /// \c n.at(3) would return \c 'a', and \c n.at(7) would return \c 'e'.
  267. /// Note that \c n.at(0) would be 7 (decimal), the label length of
  268. /// "example", instead of \c 'e', because it returns a %data portion
  269. /// in wire-format. Likewise, \c n.at(8) would return 3 (decimal)
  270. /// instead of <code>'.'</code>
  271. ///
  272. /// This method would be useful for an application to examine the
  273. /// wire-format name %data without dumping the %data into a buffer,
  274. /// which would involve %data copies and would be less efficient.
  275. /// One common usage of this method would be something like this:
  276. /// \code for (size_t i = 0; i < name.getLength(); ++i) {
  277. /// uint8_t c = name.at(i);
  278. /// // do something with c
  279. /// } \endcode
  280. ///
  281. /// Parameter \c pos must be in the valid range of the name %data, that is,
  282. /// must be less than \c Name.getLength(). Otherwise, an exception of
  283. /// class \c OutOfRange will be thrown.
  284. /// This method never throws an exception in other ways.
  285. ///
  286. /// \param pos The position in the wire format name %data to be returned.
  287. /// \return An unsigned 8-bit integer corresponding to the name %data
  288. /// at the position of \c pos.
  289. uint8_t at(size_t pos) const
  290. {
  291. if (pos >= length_) {
  292. isc_throw(OutOfRange, "Out of range access in Name::at()");
  293. }
  294. return (ndata_[pos]);
  295. }
  296. /// \brief Gets the length of the <code>Name</code> in its wire format.
  297. ///
  298. /// This method never throws an exception.
  299. ///
  300. /// \return the length (the number of octets in wire format) of the
  301. /// <code>Name</code>
  302. size_t getLength() const { return (length_); }
  303. /// \brief Returns the number of labels contained in the <code>Name</code>.
  304. ///
  305. /// Note that an empty label (corresponding to a trailing '.') is counted
  306. /// as a single label, so the return value of this method must be >0.
  307. ///
  308. /// This method never throws an exception.
  309. ///
  310. /// \return the number of labels
  311. unsigned int getLabelCount() const { return (labelcount_); }
  312. //@}
  313. ///
  314. /// \name Converter methods
  315. ///
  316. //@{
  317. /// \brief Convert the Name to a string.
  318. ///
  319. /// This method returns a <code>std::string</code> object representing the
  320. /// Name as a string. Unless <code>omit_final_dot</code> is
  321. /// <code>true</code>, the returned string ends with a dot '.'; the default
  322. /// is <code>false</code>. The default value of this parameter is
  323. /// <code>true</code>; converted names will have a trailing dot by default.
  324. ///
  325. /// This function assumes the name is in proper uncompressed wire format.
  326. /// If it finds an unexpected label character including compression pointer,
  327. /// an exception of class \c BadLabelType will be thrown.
  328. /// In addition, if resource allocation for the result string fails, a
  329. /// corresponding standard exception will be thrown.
  330. //
  331. /// \param omit_final_dot whether to omit the trailing dot in the output.
  332. /// \return a string representation of the <code>Name</code>.
  333. std::string toText(bool omit_final_dot = false) const;
  334. /// \brief Render the <code>Name</code> in the wire format with compression.
  335. ///
  336. /// This method dumps the Name in wire format with help of \c renderer,
  337. /// which encapsulates output buffer and name compression algorithm to
  338. /// render the name.
  339. ///
  340. /// If resource allocation in rendering process fails, a corresponding
  341. /// standard exception will be thrown.
  342. ///
  343. /// \param renderer DNS message rendering context that encapsulates the
  344. /// output buffer and name compression information.
  345. void toWire(AbstractMessageRenderer& renderer) const;
  346. /// \brief Render the <code>Name</code> in the wire format without
  347. /// compression.
  348. ///
  349. /// If resource allocation in rendering process fails, a corresponding
  350. /// standard exception will be thrown. This can be avoided by preallocating
  351. /// a sufficient size of \c buffer. Specifically, if
  352. /// <code>buffer.getCapacity() - buffer.getLength() >= Name::MAX_WIRE</code>
  353. /// then this method should not throw an exception.
  354. ///
  355. /// \param buffer An output buffer to store the wire %data.
  356. void toWire(isc::util::OutputBuffer& buffer) const;
  357. //@}
  358. ///
  359. /// \name Comparison methods
  360. ///
  361. //@{
  362. /// \brief Compare two <code>Name</code>s.
  363. ///
  364. /// This method compares the <code>Name</code> and <code>other</code> and
  365. /// returns the result in the form of a <code>NameComparisonResult</code>
  366. /// object.
  367. ///
  368. /// Note that this is case-insensitive comparison.
  369. ///
  370. /// This method never throws an exception.
  371. ///
  372. /// \param other the right-hand operand to compare against.
  373. /// \return a <code>NameComparisonResult</code> object representing the
  374. /// comparison result.
  375. NameComparisonResult compare(const Name& other) const;
  376. /// \brief Return true iff two names are equal.
  377. ///
  378. /// Semantically this could be implemented based on the result of the
  379. /// \c compare() method, but the actual implementation uses different code
  380. /// that simply performs character-by-character comparison (case
  381. /// insensitive for the name label parts) on the two names. This is because
  382. /// it would be much faster and the simple equality check would be pretty
  383. /// common.
  384. ///
  385. /// This method never throws an exception.
  386. ///
  387. /// \param other the <code>Name</code> object to compare against.
  388. /// \return true if the two names are equal; otherwise false.
  389. bool equals(const Name& other) const;
  390. /// Same as equals()
  391. bool operator==(const Name& other) const { return (equals(other)); }
  392. /// \brief Return true iff two names are not equal.
  393. ///
  394. /// This method simply negates the result of \c equal() method, and in that
  395. /// sense it's redundant. The separate method is provided just for
  396. /// convenience.
  397. bool nequals(const Name& other) const { return (!(equals(other))); }
  398. /// Same as nequals()
  399. bool operator!=(const Name& other) const { return (nequals(other)); }
  400. /// \brief Less-than or equal comparison for Name against <code>other</code>
  401. ///
  402. /// The comparison is based on the result of the \c compare() method.
  403. ///
  404. /// This method never throws an exception.
  405. ///
  406. /// \param other the <code>Name</code> object to compare against.
  407. /// \return true if <code>compare(other).getOrder() <= 0</code>;
  408. /// otherwise false.
  409. bool leq(const Name& other) const;
  410. /// Same as leq()
  411. bool operator<=(const Name& other) const { return (leq(other)); }
  412. /// \brief Greater-than or equal comparison for Name against
  413. /// <code>other</code>
  414. ///
  415. /// The comparison is based on the result of the \c compare() method.
  416. ///
  417. /// This method never throws an exception.
  418. ///
  419. /// \param other the <code>Name</code> object to compare against.
  420. /// \return true if <code>compare(other).getOrder() >= 0</code>;
  421. /// otherwise false.
  422. bool geq(const Name& other) const;
  423. /// Same as geq()
  424. bool operator>=(const Name& other) const { return (geq(other)); }
  425. /// \brief Less-than comparison for Name against <code>other</code>
  426. ///
  427. /// The comparison is based on the result of the \c compare() method.
  428. ///
  429. /// This method never throws an exception.
  430. ///
  431. /// \param other the <code>Name</code> object to compare against.
  432. /// \return true if <code>compare(other).getOrder() < 0</code>;
  433. /// otherwise false.
  434. bool lthan(const Name& other) const;
  435. /// Same as lthan()
  436. bool operator<(const Name& other) const { return (lthan(other)); }
  437. /// \brief Greater-than comparison for Name against <code>other</code>
  438. ///
  439. /// The comparison is based on the result of the \c compare() method.
  440. ////
  441. /// This method never throws an exception.
  442. ///
  443. /// \param other the <code>Name</code> object to compare against.
  444. /// \return true if <code>compare(other).getOrder() > 0</code>;
  445. /// otherwise false.
  446. bool gthan(const Name& other) const;
  447. /// Same as gthan()
  448. bool operator>(const Name& other) const { return (gthan(other)); }
  449. //@}
  450. ///
  451. /// \name Transformer methods
  452. ///
  453. //@{
  454. /// \brief Extract a specified subpart of Name.
  455. ///
  456. /// <code>name.split(first, n)</code> constructs a new name starting from
  457. /// the <code>first</code>-th label of the \c name, and subsequent \c n
  458. /// labels including the \c first one. Since names in this current
  459. /// implementation are always "absolute", if the specified range doesn't
  460. /// contain the trailing dot of the original \c name, then a dot will be
  461. /// appended to the resulting name. As a result, the number of labels
  462. /// will be <code>n + 1</code>, rather than \c n. For example,
  463. /// when \c n is <code>Name("www.example.com")</code>,
  464. /// both <code>n.split(1, 2)</code> and <code>n.split(1, 3)</code>
  465. /// will produce a name corresponding to "example.com.", which has 3 labels.
  466. /// Note also that labels are counted from 0, and so <code>first = 1</code>
  467. /// in this example specified the label "example", not "www".
  468. ///
  469. /// Parameter \c n must be larger than 0, and the range specified by
  470. /// \c first and \c n must not exceed the valid range of the original name;
  471. /// otherwise, an exception of class \c OutOfRange will be thrown.
  472. ///
  473. /// Note to developers: we may want to have different versions (signatures)
  474. /// of this method. For example, we want to split the Name based on a given
  475. /// suffix name.
  476. ///
  477. /// \param first The start position (in labels) of the extracted name
  478. /// \param n Number of labels of the extracted name
  479. /// \return A new Name object based on the Name containing <code>n</code>
  480. /// labels including and following the <code>first</code> label.
  481. Name split(unsigned int first, unsigned int n) const;
  482. /// \brief Extract a specified super domain name of Name.
  483. ///
  484. /// This function constructs a new \c Name object that is a super domain
  485. /// of \c this name.
  486. /// The new name is \c level labels upper than \c this name.
  487. /// For example, when \c name is www.example.com,
  488. /// <code>name.split(1)</code> will return a \c Name object for example.com.
  489. /// \c level can be 0, in which case this method returns a copy of
  490. /// \c this name.
  491. /// The possible maximum value for \c level is
  492. /// <code>this->getLabelCount()-1</code>, in which case this method
  493. /// returns a root name.
  494. ///
  495. /// One common expected usage of this method is to iterate over super
  496. /// domains of a given name, label by label, as shown in the following
  497. /// sample code:
  498. /// \code // if name is www.example.com...
  499. /// for (int i = 0; i < name.getLabelCount(); ++i) {
  500. /// Name upper_name(name.split(i));
  501. /// // upper_name'll be www.example.com., example.com., com., and then .
  502. /// }
  503. /// \endcode
  504. ///
  505. /// \c level must be smaller than the number of labels of \c this name;
  506. /// otherwise an exception of class \c OutOfRange will be thrown.
  507. /// In addition, if resource allocation for the new name fails, a
  508. /// corresponding standard exception will be thrown.
  509. ///
  510. /// Note to developers: probably as easily imagined, this method is a
  511. /// simple wrapper to one usage of the other
  512. /// <code>split(unsigned int, unsigned int) const</code> method and is
  513. /// redundant in some sense.
  514. /// We provide the "redundant" method for convenience, however, because
  515. /// the expected usage shown above seems to be common, and the parameters
  516. /// to the other \c split(unsigned int, unsigned int) const to implement
  517. /// it may not be very intuitive.
  518. ///
  519. /// We are also aware that it is generally discouraged to add a public
  520. /// member function that could be implemented using other member functions.
  521. /// We considered making it a non member function, but we could not come
  522. /// up with an intuitive function name to represent the specific service.
  523. /// Some other BIND 10 developers argued, probably partly because of the
  524. /// counter intuitive function name, a different signature of \c split
  525. /// would be better to improve code readability.
  526. /// While that may be a matter of personal preference, we accepted the
  527. /// argument. One major goal of public APIs like this is wider acceptance
  528. /// from internal/external developers, so unless there is a clear advantage
  529. /// it would be better to respect the preference of the API users.
  530. ///
  531. /// Since this method doesn't have to be a member function in other way,
  532. /// it is intentionally implemented only using public interfaces of the
  533. /// \c Name class; it doesn't refer to private members of the class even if
  534. /// it could.
  535. /// This way we hope we can avoid damaging the class encapsulation,
  536. /// which is a major drawback of public member functions.
  537. /// As such if and when this "method" has to be extended, it should be
  538. /// implemented without the privilege of being a member function unless
  539. /// there is a very strong reason to do so. In particular a minor
  540. /// performance advantage shouldn't justify that approach.
  541. ///
  542. /// \param level The number of labels to be removed from \c this name to
  543. /// create the super domain name.
  544. /// (0 <= \c level < <code>this->getLabelCount()</code>)
  545. /// \return A new \c Name object to be created.
  546. Name split(unsigned int level) const;
  547. /// \brief Reverse the labels of a name
  548. ///
  549. /// This method reverses the labels of a name. For example, if
  550. /// \c this is "www.example.com.", this method will return
  551. /// "com.example.www." (This is useful because DNSSEC sort order
  552. /// is equivalent to a lexical sort of label-reversed names.)
  553. Name reverse() const;
  554. /// \brief Concatenate two names.
  555. ///
  556. /// This method appends \c suffix to \c this Name. The trailing dot of
  557. /// \c this Name will be removed. For example, if \c this is "www."
  558. /// and \c suffix is "example.com.", a successful return of this method
  559. /// will be a name of "www.example.com."
  560. ///
  561. ///The resulting length of the concatenated name must not exceed
  562. /// \c Name::MAX_WIRE; otherwise an exception of class
  563. /// \c TooLongName will be thrown.
  564. ///
  565. /// \param suffix a Name object to be appended to the Name.
  566. /// \return a new Name object concatenating \c suffix to \c this Name.
  567. Name concatenate(const Name& suffix) const;
  568. /// \brief Downcase all upper case alphabet characters in the name.
  569. ///
  570. /// This method modifies the calling object so that it can perform the
  571. /// conversion as fast as possible and can be exception free.
  572. ///
  573. /// The return value of this version of \c downcase() is a reference to
  574. /// the calling object (i.e., \c *this) so that the caller can use the
  575. /// result of downcasing in a single line. For example, if variable
  576. /// \c n is a \c Name class object possibly containing upper case
  577. /// characters, and \c b is an \c OutputBuffer class object, then the
  578. /// following code will dump the name in wire format to \c b with
  579. /// downcasing upper case characters:
  580. ///
  581. /// \code n.downcase().toWire(b); \endcode
  582. ///
  583. /// Since this method modifies the calling object, a \c const name object
  584. /// cannot call it. If \c n is a \c const Name class object, it must first
  585. /// be copied to a different object and the latter must be used for the
  586. /// downcase modification.
  587. ///
  588. /// \return A reference to the calling object with being downcased.
  589. Name& downcase();
  590. //@}
  591. ///
  592. /// \name Testing methods
  593. ///
  594. //@{
  595. /// \brief Test if this is a wildcard name.
  596. ///
  597. /// \return \c true if the least significant label of this Name is
  598. /// <code>'*'</code>; otherwise \c false.
  599. bool isWildcard() const;
  600. //@}
  601. ///
  602. /// \name Protocol constants
  603. ///
  604. //@{
  605. /// \brief Max allowable length of domain names.
  606. static const size_t MAX_WIRE = 255;
  607. /// \brief Max allowable labels of domain names.
  608. ///
  609. /// This is <code>ceil(MAX_WIRE / 2)</code>, and is equal to the number of
  610. /// labels of name "a.a.a.a....a." (127 "a"'s and trailing dot).
  611. static const size_t MAX_LABELS = 128;
  612. /// \brief Max allowable length of labels of a domain name.
  613. static const size_t MAX_LABELLEN = 63;
  614. /// \brief Max possible pointer value for name compression.
  615. ///
  616. /// This is the highest number of 14-bit unsigned integer. Name compression
  617. /// pointers are identified as a 2-byte value starting with the upper two
  618. /// bit being 11.
  619. static const uint16_t MAX_COMPRESS_POINTER = 0x3fff;
  620. /// \brief A 8-bit masked value indicating a start of compression pointer.
  621. static const uint16_t COMPRESS_POINTER_MARK8 = 0xc0;
  622. /// \brief A 16-bit masked value indicating a start of compression pointer.
  623. static const uint16_t COMPRESS_POINTER_MARK16 = 0xc000;
  624. //@}
  625. ///
  626. /// \name Well-known name constants
  627. ///
  628. //@{
  629. /// \brief Root name (i.e. ".").
  630. static const Name& ROOT_NAME();
  631. //@}
  632. private:
  633. std::string ndata_;
  634. std::vector<unsigned char> offsets_;
  635. unsigned int length_;
  636. unsigned int labelcount_;
  637. };
  638. inline const Name&
  639. Name::ROOT_NAME() {
  640. static Name root_name(".");
  641. return (root_name);
  642. }
  643. ///
  644. /// \brief Insert the name as a string into stream.
  645. ///
  646. /// This method convert the \c name into a string and inserts it into the
  647. /// output stream \c os.
  648. ///
  649. /// This function overloads the global operator<< to behave as described in
  650. /// ostream::operator<< but applied to \c Name objects.
  651. ///
  652. /// \param os A \c std::ostream object on which the insertion operation is
  653. /// performed.
  654. /// \param name The \c Name object output by the operation.
  655. /// \return A reference to the same \c std::ostream object referenced by
  656. /// parameter \c os after the insertion operation.
  657. std::ostream&
  658. operator<<(std::ostream& os, const Name& name);
  659. }
  660. }
  661. #endif // __NAME_H
  662. // Local Variables:
  663. // mode: c++
  664. // End: