rdata.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <exceptions/exceptions.h>
  15. #include <util/buffer.h>
  16. #include <dns/name.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/master_lexer.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rrparamregistry.h>
  21. #include <dns/rrtype.h>
  22. #include <boost/lexical_cast.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <algorithm>
  25. #include <cctype>
  26. #include <string>
  27. #include <sstream>
  28. #include <iomanip>
  29. #include <ios>
  30. #include <ostream>
  31. #include <vector>
  32. #include <stdint.h>
  33. #include <string.h>
  34. using namespace std;
  35. using boost::lexical_cast;
  36. using namespace isc::util;
  37. namespace isc {
  38. namespace dns {
  39. namespace rdata {
  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. void
  185. Generic::constructHelper(const std::string& rdata_string) {
  186. istringstream iss(rdata_string);
  187. string unknown_mark;
  188. iss >> unknown_mark;
  189. if (unknown_mark != "\\#") {
  190. isc_throw(InvalidRdataText,
  191. "Missing the special token (\\#) for generic RDATA encoding");
  192. }
  193. // RDLENGTH: read into a string so that we can easily reject invalid tokens
  194. string rdlen_txt;
  195. iss >> rdlen_txt;
  196. istringstream iss_rdlen(rdlen_txt);
  197. int32_t rdlen;
  198. iss_rdlen >> rdlen;
  199. if (iss_rdlen.rdstate() != ios::eofbit) {
  200. isc_throw(InvalidRdataText,
  201. "Invalid representation for a generic RDLENGTH");
  202. }
  203. if (rdlen < 0 || rdlen > 0xffff) {
  204. isc_throw(InvalidRdataLength, "RDATA length is out of range");
  205. }
  206. iss >> ws; // skip any white spaces
  207. // Hexadecimal encoding of RDATA: each segment must consist of an even
  208. // number of hex digits.
  209. vector<uint8_t> data;
  210. while (!iss.eof() && data.size() < rdlen) {
  211. // extract two characters, which should compose a single byte of data.
  212. char buf[2];
  213. iss.read(buf, sizeof(buf));
  214. if ((iss.rdstate() & (ios::badbit | ios::failbit)) != 0) {
  215. isc_throw(InvalidRdataText,
  216. "Invalid hex encoding of generic RDATA");
  217. }
  218. // convert it to a single byte integer as a hex digit.
  219. istringstream iss_byte(string(buf, sizeof(buf)));
  220. unsigned int ch;
  221. iss_byte >> hex >> ch;
  222. if (iss_byte.rdstate() != ios::eofbit) {
  223. isc_throw(InvalidRdataText,
  224. "Invalid hex encoding of generic RDATA");
  225. }
  226. data.push_back(ch);
  227. iss >> ws; // skip spaces
  228. }
  229. if (!iss.eof()) {
  230. isc_throw(InvalidRdataLength,
  231. "RDLENGTH is too small for generic RDATA");
  232. }
  233. if (data.size() != rdlen) {
  234. isc_throw(InvalidRdataLength,
  235. "Generic RDATA code doesn't match RDLENGTH");
  236. }
  237. impl_ = new GenericImpl(data);
  238. }
  239. Generic::Generic(const std::string& rdata_string) {
  240. constructHelper(rdata_string);
  241. }
  242. Generic::Generic(MasterLexer& lexer, const Name*,
  243. MasterLoader::Options,
  244. MasterLoaderCallbacks&)
  245. {
  246. std::string s;
  247. while (true) {
  248. const MasterToken& token = lexer.getNextToken();
  249. if ((token.getType() == MasterToken::END_OF_FILE) ||
  250. (token.getType() == MasterToken::END_OF_LINE)) {
  251. lexer.ungetToken(); // let the upper layer handle the end-of token
  252. break;
  253. }
  254. if (!s.empty()) {
  255. s += " ";
  256. }
  257. s += token.getString();
  258. }
  259. constructHelper(s);
  260. }
  261. Generic::~Generic() {
  262. delete impl_;
  263. }
  264. Generic::Generic(const Generic& source) :
  265. Rdata(), impl_(new GenericImpl(*source.impl_))
  266. {}
  267. Generic&
  268. // Our check is better than the usual if (this == &source),
  269. // but cppcheck doesn't recognize it.
  270. // cppcheck-suppress operatorEqToSelf
  271. Generic::operator=(const Generic& source) {
  272. if (impl_ == source.impl_) {
  273. return (*this);
  274. }
  275. GenericImpl* newimpl = new GenericImpl(*source.impl_);
  276. delete impl_;
  277. impl_ = newimpl;
  278. return (*this);
  279. }
  280. namespace {
  281. class UnknownRdataDumper {
  282. public:
  283. UnknownRdataDumper(ostringstream& oss) : oss_(&oss) {}
  284. void operator()(const unsigned char d)
  285. {
  286. *oss_ << setw(2) << static_cast<unsigned int>(d);
  287. }
  288. private:
  289. ostringstream* oss_;
  290. };
  291. }
  292. string
  293. Generic::toText() const {
  294. ostringstream oss;
  295. oss << "\\# " << impl_->data_.size() << " ";
  296. oss.fill('0');
  297. oss << right << hex;
  298. for_each(impl_->data_.begin(), impl_->data_.end(), UnknownRdataDumper(oss));
  299. return (oss.str());
  300. }
  301. void
  302. Generic::toWire(isc::util::OutputBuffer& buffer) const {
  303. buffer.writeData(&impl_->data_[0], impl_->data_.size());
  304. }
  305. void
  306. Generic::toWire(AbstractMessageRenderer& renderer) const {
  307. renderer.writeData(&impl_->data_[0], impl_->data_.size());
  308. }
  309. namespace {
  310. inline int
  311. compare_internal(const GenericImpl& lhs, const GenericImpl& rhs) {
  312. size_t this_len = lhs.data_.size();
  313. size_t other_len = rhs.data_.size();
  314. size_t len = (this_len < other_len) ? this_len : other_len;
  315. int cmp;
  316. // TODO: is there a need to check len - should we just assert?
  317. // (Depends if it is possible for rdata to have zero length)
  318. if ((len != 0) &&
  319. ((cmp = memcmp(&lhs.data_[0], &rhs.data_[0], len)) != 0)) {
  320. return (cmp);
  321. } else {
  322. return ((this_len == other_len) ? 0 :
  323. (this_len < other_len) ? -1 : 1);
  324. }
  325. }
  326. }
  327. int
  328. Generic::compare(const Rdata& other) const {
  329. const Generic& other_rdata = dynamic_cast<const Generic&>(other);
  330. return (compare_internal(*impl_, *other_rdata.impl_));
  331. }
  332. std::ostream&
  333. operator<<(std::ostream& os, const Generic& rdata) {
  334. return (os << rdata.toText());
  335. }
  336. } // end of namespace generic
  337. } // end of namespace rdata
  338. }
  339. }