ns_2.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2010 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. #include <config.h>
  15. #include <string>
  16. #include <util/buffer.h>
  17. #include <dns/name.h>
  18. #include <dns/messagerenderer.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <dns/rdata/generic/detail/lexer_util.h>
  22. using namespace std;
  23. using namespace isc::util;
  24. // BEGIN_ISC_NAMESPACE
  25. // BEGIN_RDATA_NAMESPACE
  26. /// \brief Constructor from string.
  27. ///
  28. /// The given string must represent a valid NS RDATA. There can be extra
  29. /// space characters at the beginning or end of the text (which are simply
  30. /// ignored), but other extra text, including a new line, will make the
  31. /// construction fail with an exception.
  32. ///
  33. /// The NSDNAME must be absolute since there's no parameter that
  34. /// specifies the origin name; if it is not absolute, \c
  35. /// MissingNameOrigin exception will be thrown. These must not be
  36. /// represented as a quoted string.
  37. ///
  38. /// \throw Others Exception from the Name and RRTTL constructors.
  39. /// \throw InvalidRdataText Other general syntax errors.
  40. NS::NS(const std::string& namestr) :
  41. // Fill in dummy name and replace them soon below.
  42. nsname_(Name::ROOT_NAME())
  43. {
  44. try {
  45. std::istringstream ss(namestr);
  46. MasterLexer lexer;
  47. lexer.pushSource(ss);
  48. nsname_ = createNameFromLexer(lexer, NULL);
  49. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  50. isc_throw(InvalidRdataText, "extra input text for NS: "
  51. << namestr);
  52. }
  53. } catch (const MasterLexer::LexerError& ex) {
  54. isc_throw(InvalidRdataText, "Failed to construct NS from '" <<
  55. namestr << "': " << ex.what());
  56. }
  57. }
  58. NS::NS(InputBuffer& buffer, size_t) :
  59. nsname_(buffer)
  60. {
  61. // we don't need rdata_len for parsing. if necessary, the caller will
  62. // check consistency.
  63. }
  64. /// \brief Constructor with a context of MasterLexer.
  65. ///
  66. /// The \c lexer should point to the beginning of valid textual
  67. /// representation of an NS RDATA. The NSDNAME field can be
  68. /// non-absolute if \c origin is non-NULL, in which case \c origin is
  69. /// used to make it absolute. It must not be represented as a quoted
  70. /// string.
  71. ///
  72. /// \throw MasterLexer::LexerError General parsing error such as missing field.
  73. /// \throw Other Exceptions from the Name and RRTTL constructors if
  74. /// construction of textual fields as these objects fail.
  75. ///
  76. /// \param lexer A \c MasterLexer object parsing a master file for the
  77. /// RDATA to be created
  78. /// \param origin If non NULL, specifies the origin of NSDNAME when it
  79. /// is non-absolute.
  80. NS::NS(MasterLexer& lexer, const Name* origin,
  81. MasterLoader::Options, MasterLoaderCallbacks&) :
  82. nsname_(createNameFromLexer(lexer, origin))
  83. {}
  84. NS::NS(const NS& other) :
  85. Rdata(), nsname_(other.nsname_)
  86. {}
  87. void
  88. NS::toWire(OutputBuffer& buffer) const {
  89. nsname_.toWire(buffer);
  90. }
  91. void
  92. NS::toWire(AbstractMessageRenderer& renderer) const {
  93. renderer.writeName(nsname_);
  94. }
  95. string
  96. NS::toText() const {
  97. return (nsname_.toText());
  98. }
  99. int
  100. NS::compare(const Rdata& other) const {
  101. const NS& other_ns = dynamic_cast<const NS&>(other);
  102. return (compareNames(nsname_, other_ns.nsname_));
  103. }
  104. const Name&
  105. NS::getNSName() const {
  106. return (nsname_);
  107. }
  108. // END_RDATA_NAMESPACE
  109. // END_ISC_NAMESPACE