soa_6.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <exceptions/exceptions.h>
  16. #include <util/buffer.h>
  17. #include <dns/name.h>
  18. #include <dns/master_lexer.h>
  19. #include <dns/master_loader.h>
  20. #include <dns/master_loader_callbacks.h>
  21. #include <dns/messagerenderer.h>
  22. #include <dns/rdata.h>
  23. #include <dns/rdataclass.h>
  24. #include <dns/rdata/generic/detail/lexer_util.h>
  25. #include <boost/static_assert.hpp>
  26. #include <boost/lexical_cast.hpp>
  27. #include <string>
  28. #include <sstream>
  29. using namespace std;
  30. using boost::lexical_cast;
  31. using namespace isc::util;
  32. using isc::dns::rdata::generic::detail::createNameFromLexer;
  33. // BEGIN_ISC_NAMESPACE
  34. // BEGIN_RDATA_NAMESPACE
  35. SOA::SOA(InputBuffer& buffer, size_t) :
  36. mname_(buffer), rname_(buffer)
  37. {
  38. // we don't need rdata_len for parsing. if necessary, the caller will
  39. // check consistency.
  40. buffer.readData(numdata_, sizeof(numdata_));
  41. }
  42. namespace {
  43. void
  44. fillParameters(MasterLexer& lexer, uint8_t numdata[20]) {
  45. // Copy serial, refresh, retry, expire, minimum. We accept the extended
  46. // TTL-compatible style for the latter four.
  47. OutputBuffer buffer(20);
  48. buffer.writeUint32(lexer.getNextToken(MasterToken::NUMBER).getNumber());
  49. for (int i = 0; i < 4; ++i) {
  50. buffer.writeUint32(RRTTL(lexer.getNextToken(MasterToken::STRING).
  51. getString()).getValue());
  52. }
  53. memcpy(numdata, buffer.getData(), buffer.getLength());
  54. }
  55. }
  56. /// \brief Constructor from string.
  57. ///
  58. /// The given string must represent a valid SOA RDATA. There can be extra
  59. /// space characters at the beginning or end of the text (which are simply
  60. /// ignored), but other extra text, including a new line, will make the
  61. /// construction fail with an exception.
  62. ///
  63. /// The MNAME and RNAME must be absolute since there's no parameter that
  64. /// specifies the origin name; if these are not absolute, \c MissingNameOrigin
  65. /// exception will be thrown.
  66. ///
  67. /// See the construction that takes \c MasterLexer for other fields.
  68. ///
  69. /// \throw Others Exception from the Name and RRTTL constructors.
  70. /// \throw InvalidRdataText Other general syntax errors.
  71. SOA::SOA(const std::string& soastr) :
  72. // Fill in dummy name and replace them soon below.
  73. mname_(Name::ROOT_NAME()), rname_(Name::ROOT_NAME())
  74. {
  75. try {
  76. std::istringstream ss(soastr);
  77. MasterLexer lexer;
  78. lexer.pushSource(ss);
  79. mname_ = createNameFromLexer(lexer, NULL);
  80. rname_ = createNameFromLexer(lexer, NULL);
  81. fillParameters(lexer, numdata_);
  82. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  83. isc_throw(InvalidRdataText, "extra input text for SOA: "
  84. << soastr);
  85. }
  86. } catch (const MasterLexer::LexerError& ex) {
  87. isc_throw(InvalidRdataText, "Failed to construct SOA from '" <<
  88. soastr << "': " << ex.what());
  89. }
  90. }
  91. /// \brief Constructor with a context of MasterLexer.
  92. ///
  93. /// The \c lexer should point to the beginning of valid textual representation
  94. /// of an SOA RDATA. The MNAME and RNAME fields can be non absolute if
  95. /// \c origin is non NULL, in which case \c origin is used to make them
  96. /// absolute.
  97. ///
  98. /// The REFRESH, RETRY, EXPIRE, and MINIMUM fields can be either a valid
  99. /// decimal representation of an unsigned 32-bit integer or other
  100. /// valid textual representation of \c RRTTL such as "1H" (which means 3600).
  101. ///
  102. /// \throw MasterLexer::LexerError General parsing error such as missing field.
  103. /// \throw Other Exceptions from the Name and RRTTL constructors if
  104. /// construction of textual fields as these objects fail.
  105. ///
  106. /// \param lexer A \c MasterLexer object parsing a master file for the
  107. /// RDATA to be created
  108. /// \param origin If non NULL, specifies the origin of MNAME and RNAME when
  109. /// they are non absolute.
  110. SOA::SOA(MasterLexer& lexer, const Name* origin,
  111. MasterLoader::Options, MasterLoaderCallbacks&) :
  112. mname_(createNameFromLexer(lexer, origin)),
  113. rname_(createNameFromLexer(lexer, origin))
  114. {
  115. fillParameters(lexer, numdata_);
  116. }
  117. SOA::SOA(const Name& mname, const Name& rname, uint32_t serial,
  118. uint32_t refresh, uint32_t retry, uint32_t expire, uint32_t minimum) :
  119. mname_(mname), rname_(rname)
  120. {
  121. OutputBuffer b(20);
  122. b.writeUint32(serial);
  123. b.writeUint32(refresh);
  124. b.writeUint32(retry);
  125. b.writeUint32(expire);
  126. b.writeUint32(minimum);
  127. assert(b.getLength() == sizeof(numdata_));
  128. memcpy(numdata_, b.getData(), sizeof(numdata_));
  129. }
  130. SOA::SOA(const SOA& other) :
  131. Rdata(), mname_(other.mname_), rname_(other.rname_)
  132. {
  133. memcpy(numdata_, other.numdata_, sizeof(numdata_));
  134. }
  135. void
  136. SOA::toWire(OutputBuffer& buffer) const {
  137. mname_.toWire(buffer);
  138. rname_.toWire(buffer);
  139. buffer.writeData(numdata_, sizeof(numdata_));
  140. }
  141. void
  142. SOA::toWire(AbstractMessageRenderer& renderer) const {
  143. renderer.writeName(mname_);
  144. renderer.writeName(rname_);
  145. renderer.writeData(numdata_, sizeof(numdata_));
  146. }
  147. Serial
  148. SOA::getSerial() const {
  149. InputBuffer b(numdata_, sizeof(numdata_));
  150. return (Serial(b.readUint32()));
  151. }
  152. uint32_t
  153. SOA::getMinimum() const {
  154. // Make sure the buffer access is safe.
  155. BOOST_STATIC_ASSERT(sizeof(numdata_) ==
  156. sizeof(uint32_t) * 4 + sizeof(uint32_t));
  157. InputBuffer b(&numdata_[sizeof(uint32_t) * 4], sizeof(uint32_t));
  158. return (b.readUint32());
  159. }
  160. string
  161. SOA::toText() const {
  162. InputBuffer b(numdata_, sizeof(numdata_));
  163. uint32_t serial = b.readUint32();
  164. uint32_t refresh = b.readUint32();
  165. uint32_t retry = b.readUint32();
  166. uint32_t expire = b.readUint32();
  167. uint32_t minimum = b.readUint32();
  168. return (mname_.toText() + " " + rname_.toText() + " " +
  169. lexical_cast<string>(serial) + " " +
  170. lexical_cast<string>(refresh) + " " +
  171. lexical_cast<string>(retry) + " " +
  172. lexical_cast<string>(expire) + " " +
  173. lexical_cast<string>(minimum));
  174. }
  175. int
  176. SOA::compare(const Rdata& other) const {
  177. const SOA& other_soa = dynamic_cast<const SOA&>(other);
  178. int order = compareNames(mname_, other_soa.mname_);
  179. if (order != 0) {
  180. return (order);
  181. }
  182. order = compareNames(rname_, other_soa.rname_);
  183. if (order != 0) {
  184. return (order);
  185. }
  186. return (memcmp(numdata_, other_soa.numdata_, sizeof(numdata_)));
  187. }
  188. // END_RDATA_NAMESPACE
  189. // END_ISC_NAMESPACE