rdata.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // Copyright (C) 2010-2016 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. #include <config.h>
  7. #include <exceptions/exceptions.h>
  8. #include <util/buffer.h>
  9. #include <util/encode/hex.h>
  10. #include <dns/name.h>
  11. #include <dns/messagerenderer.h>
  12. #include <dns/master_lexer.h>
  13. #include <dns/rdata.h>
  14. #include <dns/rrparamregistry.h>
  15. #include <dns/rrtype.h>
  16. #include <boost/lexical_cast.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <algorithm>
  19. #include <cctype>
  20. #include <string>
  21. #include <sstream>
  22. #include <iomanip>
  23. #include <ios>
  24. #include <ostream>
  25. #include <vector>
  26. #include <stdint.h>
  27. #include <string.h>
  28. using namespace std;
  29. using boost::lexical_cast;
  30. using namespace isc::util;
  31. namespace isc {
  32. namespace dns {
  33. namespace rdata {
  34. uint16_t
  35. Rdata::getLength() const {
  36. OutputBuffer obuffer(0);
  37. toWire(obuffer);
  38. return (obuffer.getLength());
  39. }
  40. // XXX: we need to specify std:: for string to help doxygen match the
  41. // function signature with that given in the header file.
  42. RdataPtr
  43. createRdata(const RRType& rrtype, const RRClass& rrclass,
  44. const std::string& rdata_string)
  45. {
  46. return (RRParamRegistry::getRegistry().createRdata(rrtype, rrclass,
  47. rdata_string));
  48. }
  49. RdataPtr
  50. createRdata(const RRType& rrtype, const RRClass& rrclass,
  51. isc::util::InputBuffer& buffer, size_t len)
  52. {
  53. if (len > MAX_RDLENGTH) {
  54. isc_throw(InvalidRdataLength, "RDLENGTH too large");
  55. }
  56. size_t old_pos = buffer.getPosition();
  57. RdataPtr rdata =
  58. RRParamRegistry::getRegistry().createRdata(rrtype, rrclass, buffer,
  59. len);
  60. if (buffer.getPosition() - old_pos != len) {
  61. isc_throw(InvalidRdataLength, "RDLENGTH mismatch: " <<
  62. buffer.getPosition() - old_pos << " != " << len);
  63. }
  64. return (rdata);
  65. }
  66. RdataPtr
  67. createRdata(const RRType& rrtype, const RRClass& rrclass, const Rdata& source)
  68. {
  69. return (RRParamRegistry::getRegistry().createRdata(rrtype, rrclass,
  70. source));
  71. }
  72. namespace {
  73. void
  74. fromtextError(bool& error_issued, const MasterLexer& lexer,
  75. MasterLoaderCallbacks& callbacks,
  76. const MasterToken* token, const char* reason)
  77. {
  78. // Don't be too noisy if there are many issues for single RDATA
  79. if (error_issued) {
  80. return;
  81. }
  82. error_issued = true;
  83. if (token == NULL) {
  84. callbacks.error(lexer.getSourceName(), lexer.getSourceLine(),
  85. "createRdata from text failed: " + string(reason));
  86. return;
  87. }
  88. switch (token->getType()) {
  89. case MasterToken::STRING:
  90. case MasterToken::QSTRING:
  91. callbacks.error(lexer.getSourceName(), lexer.getSourceLine(),
  92. "createRdata from text failed near '" +
  93. token->getString() + "': " + string(reason));
  94. break;
  95. case MasterToken::ERROR:
  96. callbacks.error(lexer.getSourceName(), lexer.getSourceLine(),
  97. "createRdata from text failed: " +
  98. token->getErrorText());
  99. break;
  100. default:
  101. // This case shouldn't happen based on how we use MasterLexer in
  102. // createRdata(), so we could assert() that here. But since it
  103. // depends on detailed behavior of other classes, we treat the case
  104. // in a bit less harsh way.
  105. isc_throw(Unexpected, "bug: createRdata() saw unexpected token type");
  106. }
  107. }
  108. }
  109. RdataPtr
  110. createRdata(const RRType& rrtype, const RRClass& rrclass,
  111. MasterLexer& lexer, const Name* origin,
  112. MasterLoader::Options options,
  113. MasterLoaderCallbacks& callbacks)
  114. {
  115. RdataPtr rdata;
  116. bool error_issued = false;
  117. try {
  118. rdata = RRParamRegistry::getRegistry().createRdata(
  119. rrtype, rrclass, lexer, origin, options, callbacks);
  120. } catch (const MasterLexer::LexerError& error) {
  121. fromtextError(error_issued, lexer, callbacks, &error.token_, "");
  122. } catch (const Exception& ex) {
  123. // Catching all isc::Exception is too broad, but right now we don't
  124. // have better granularity. When we complete #2518 we can make this
  125. // finer.
  126. fromtextError(error_issued, lexer, callbacks, NULL, ex.what());
  127. }
  128. // Other exceptions mean a serious implementation bug or fatal system
  129. // error; it doesn't make sense to catch and try to recover from them
  130. // here. Just propagate.
  131. // Consume to end of line / file.
  132. // Call callback via fromtextError once if there was an error.
  133. do {
  134. const MasterToken& token = lexer.getNextToken();
  135. switch (token.getType()) {
  136. case MasterToken::END_OF_LINE:
  137. return (rdata);
  138. case MasterToken::END_OF_FILE:
  139. callbacks.warning(lexer.getSourceName(), lexer.getSourceLine(),
  140. "file does not end with newline");
  141. return (rdata);
  142. default:
  143. rdata.reset(); // we'll return NULL
  144. fromtextError(error_issued, lexer, callbacks, &token,
  145. "extra input text");
  146. // Continue until we see EOL or EOF
  147. }
  148. } while (true);
  149. // We shouldn't reach here
  150. assert(false);
  151. return (RdataPtr()); // add explicit return to silence some compilers
  152. }
  153. int
  154. compareNames(const Name& n1, const Name& n2) {
  155. size_t len1 = n1.getLength();
  156. size_t len2 = n2.getLength();
  157. size_t cmplen = min(len1, len2);
  158. for (size_t i = 0; i < cmplen; ++i) {
  159. uint8_t c1 = tolower(n1.at(i));
  160. uint8_t c2 = tolower(n2.at(i));
  161. if (c1 < c2) {
  162. return (-1);
  163. } else if (c1 > c2) {
  164. return (1);
  165. }
  166. }
  167. return ((len1 == len2) ? 0 : (len1 < len2) ? -1 : 1);
  168. }
  169. namespace generic {
  170. struct GenericImpl {
  171. GenericImpl(const vector<uint8_t>& data) : data_(data) {}
  172. vector<uint8_t> data_;
  173. };
  174. Generic::Generic(isc::util::InputBuffer& buffer, size_t rdata_len) {
  175. if (rdata_len > MAX_RDLENGTH) {
  176. isc_throw(InvalidRdataLength, "RDLENGTH too large");
  177. }
  178. vector<uint8_t> data(rdata_len);
  179. if (rdata_len > 0) {
  180. buffer.readData(&data[0], rdata_len);
  181. }
  182. impl_ = new GenericImpl(data);
  183. }
  184. GenericImpl*
  185. Generic::constructFromLexer(MasterLexer& lexer) {
  186. const MasterToken& token = lexer.getNextToken(MasterToken::STRING);
  187. if (token.getString() != "\\#") {
  188. isc_throw(InvalidRdataText,
  189. "Missing the special token (\\#) for "
  190. "unknown RDATA encoding");
  191. }
  192. // Initialize with an absurd value.
  193. uint32_t rdlen = 65536;
  194. try {
  195. rdlen = lexer.getNextToken(MasterToken::NUMBER).getNumber();
  196. } catch (const MasterLexer::LexerError&) {
  197. isc_throw(InvalidRdataLength,
  198. "Unknown RDATA length is invalid");
  199. }
  200. if (rdlen > 65535) {
  201. isc_throw(InvalidRdataLength,
  202. "Unknown RDATA length is out of range: " << rdlen);
  203. }
  204. vector<uint8_t> data;
  205. if (rdlen > 0) {
  206. string hex_txt;
  207. string hex_part;
  208. // Whitespace is allowed within hex data, so read to the end of input.
  209. while (true) {
  210. const MasterToken& token =
  211. lexer.getNextToken(MasterToken::STRING, true);
  212. if ((token.getType() == MasterToken::END_OF_FILE) ||
  213. (token.getType() == MasterToken::END_OF_LINE)) {
  214. // Unget the last read token as createRdata() expects us
  215. // to leave it at the end-of-line or end-of-file when we
  216. // return.
  217. lexer.ungetToken();
  218. break;
  219. }
  220. token.getString(hex_part);
  221. hex_txt.append(hex_part);
  222. }
  223. try {
  224. isc::util::encode::decodeHex(hex_txt, data);
  225. } catch (const isc::BadValue& ex) {
  226. isc_throw(InvalidRdataText,
  227. "Invalid hex encoding of generic RDATA: " << ex.what());
  228. }
  229. }
  230. if (data.size() != rdlen) {
  231. isc_throw(InvalidRdataLength,
  232. "Size of unknown RDATA hex data doesn't match RDLENGTH: "
  233. << data.size() << " vs. " << rdlen);
  234. }
  235. return (new GenericImpl(data));
  236. }
  237. Generic::Generic(const std::string& rdata_string) :
  238. impl_(NULL)
  239. {
  240. // We use unique_ptr here because if there is an exception in this
  241. // constructor, the destructor is not called and there could be a
  242. // leak of the GenericImpl that constructFromLexer() returns.
  243. std::unique_ptr<GenericImpl> impl_ptr;
  244. try {
  245. std::istringstream ss(rdata_string);
  246. MasterLexer lexer;
  247. lexer.pushSource(ss);
  248. impl_ptr.reset(constructFromLexer(lexer));
  249. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  250. isc_throw(InvalidRdataText, "extra input text for unknown RDATA: "
  251. << rdata_string);
  252. }
  253. } catch (const MasterLexer::LexerError& ex) {
  254. isc_throw(InvalidRdataText, "Failed to construct unknown RDATA "
  255. "from '" << rdata_string << "': " << ex.what());
  256. }
  257. impl_ = impl_ptr.release();
  258. }
  259. Generic::Generic(MasterLexer& lexer, const Name*,
  260. MasterLoader::Options,
  261. MasterLoaderCallbacks&) :
  262. impl_(constructFromLexer(lexer))
  263. {
  264. }
  265. Generic::~Generic() {
  266. delete impl_;
  267. }
  268. Generic::Generic(const Generic& source) :
  269. Rdata(), impl_(new GenericImpl(*source.impl_))
  270. {}
  271. Generic&
  272. // Our check is better than the usual if (this == &source),
  273. // but cppcheck doesn't recognize it.
  274. // cppcheck-suppress operatorEqToSelf
  275. Generic::operator=(const Generic& source) {
  276. if (impl_ == source.impl_) {
  277. return (*this);
  278. }
  279. GenericImpl* newimpl = new GenericImpl(*source.impl_);
  280. delete impl_;
  281. impl_ = newimpl;
  282. return (*this);
  283. }
  284. namespace {
  285. class UnknownRdataDumper {
  286. public:
  287. UnknownRdataDumper(ostringstream& oss) : oss_(&oss) {}
  288. void operator()(const unsigned char d)
  289. {
  290. *oss_ << setw(2) << static_cast<unsigned int>(d);
  291. }
  292. private:
  293. ostringstream* oss_;
  294. };
  295. }
  296. string
  297. Generic::toText() const {
  298. ostringstream oss;
  299. oss << "\\# " << impl_->data_.size() << " ";
  300. oss.fill('0');
  301. oss << right << hex;
  302. for_each(impl_->data_.begin(), impl_->data_.end(), UnknownRdataDumper(oss));
  303. return (oss.str());
  304. }
  305. void
  306. Generic::toWire(isc::util::OutputBuffer& buffer) const {
  307. buffer.writeData(&impl_->data_[0], impl_->data_.size());
  308. }
  309. void
  310. Generic::toWire(AbstractMessageRenderer& renderer) const {
  311. renderer.writeData(&impl_->data_[0], impl_->data_.size());
  312. }
  313. namespace {
  314. inline int
  315. compare_internal(const GenericImpl& lhs, const GenericImpl& rhs) {
  316. size_t this_len = lhs.data_.size();
  317. size_t other_len = rhs.data_.size();
  318. size_t len = (this_len < other_len) ? this_len : other_len;
  319. int cmp;
  320. // TODO: is there a need to check len - should we just assert?
  321. // (Depends if it is possible for rdata to have zero length)
  322. if ((len != 0) &&
  323. ((cmp = memcmp(&lhs.data_[0], &rhs.data_[0], len)) != 0)) {
  324. return (cmp);
  325. } else {
  326. return ((this_len == other_len) ? 0 :
  327. (this_len < other_len) ? -1 : 1);
  328. }
  329. }
  330. }
  331. int
  332. Generic::compare(const Rdata& other) const {
  333. const Generic& other_rdata = dynamic_cast<const Generic&>(other);
  334. return (compare_internal(*impl_, *other_rdata.impl_));
  335. }
  336. std::ostream&
  337. operator<<(std::ostream& os, const Generic& rdata) {
  338. return (os << rdata.toText());
  339. }
  340. } // end of namespace generic
  341. } // end of namespace rdata
  342. }
  343. }