rdata.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <algorithm>
  15. #include <cctype>
  16. #include <string>
  17. #include <sstream>
  18. #include <iomanip>
  19. #include <ios>
  20. #include <ostream>
  21. #include <vector>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <boost/lexical_cast.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <util/buffer.h>
  27. #include <dns/name.h>
  28. #include <dns/messagerenderer.h>
  29. #include <dns/rdata.h>
  30. #include <dns/rrparamregistry.h>
  31. #include <dns/rrtype.h>
  32. using namespace std;
  33. using namespace boost;
  34. using namespace isc::util;
  35. namespace isc {
  36. namespace dns {
  37. namespace rdata {
  38. // XXX: we need to specify std:: for string to help doxygen match the
  39. // function signature with that given in the header file.
  40. RdataPtr
  41. createRdata(const RRType& rrtype, const RRClass& rrclass,
  42. const std::string& rdata_string)
  43. {
  44. return (RRParamRegistry::getRegistry().createRdata(rrtype, rrclass,
  45. rdata_string));
  46. }
  47. RdataPtr
  48. createRdata(const RRType& rrtype, const RRClass& rrclass,
  49. isc::util::InputBuffer& buffer, size_t len)
  50. {
  51. if (len > MAX_RDLENGTH) {
  52. isc_throw(InvalidRdataLength, "RDLENGTH too large");
  53. }
  54. size_t old_pos = buffer.getPosition();
  55. RdataPtr rdata =
  56. RRParamRegistry::getRegistry().createRdata(rrtype, rrclass, buffer,
  57. len);
  58. if (buffer.getPosition() - old_pos != len) {
  59. isc_throw(InvalidRdataLength, "RDLENGTH mismatch: " <<
  60. buffer.getPosition() - old_pos << " != " << len);
  61. }
  62. return (rdata);
  63. }
  64. RdataPtr
  65. createRdata(const RRType& rrtype, const RRClass& rrclass, const Rdata& source)
  66. {
  67. return (RRParamRegistry::getRegistry().createRdata(rrtype, rrclass,
  68. source));
  69. }
  70. int
  71. compareNames(const Name& n1, const Name& n2) {
  72. size_t len1 = n1.getLength();
  73. size_t len2 = n2.getLength();
  74. size_t cmplen = min(len1, len2);
  75. for (size_t i = 0; i < cmplen; ++i) {
  76. uint8_t c1 = tolower(n1.at(i));
  77. uint8_t c2 = tolower(n2.at(i));
  78. if (c1 < c2) {
  79. return (-1);
  80. } else if (c1 > c2) {
  81. return (1);
  82. }
  83. }
  84. return ((len1 == len2) ? 0 : (len1 < len2) ? -1 : 1);
  85. }
  86. namespace generic {
  87. struct GenericImpl {
  88. GenericImpl(const vector<uint8_t>& data) : data_(data) {}
  89. vector<uint8_t> data_;
  90. };
  91. Generic::Generic(isc::util::InputBuffer& buffer, size_t rdata_len) {
  92. if (rdata_len > MAX_RDLENGTH) {
  93. isc_throw(InvalidRdataLength, "RDLENGTH too large");
  94. }
  95. vector<uint8_t> data(rdata_len);
  96. buffer.readData(&data[0], rdata_len);
  97. impl_ = new GenericImpl(data);
  98. }
  99. Generic::Generic(const string& rdata_string) {
  100. istringstream iss(rdata_string);
  101. string unknown_mark;
  102. iss >> unknown_mark;
  103. if (unknown_mark != "\\#") {
  104. isc_throw(InvalidRdataText,
  105. "Missing the special token (\\#) for generic RDATA encoding");
  106. }
  107. // RDLENGTH: read into a string so that we can easily reject invalid tokens
  108. string rdlen_txt;
  109. iss >> rdlen_txt;
  110. istringstream iss_rdlen(rdlen_txt);
  111. int32_t rdlen;
  112. iss_rdlen >> rdlen;
  113. if (iss_rdlen.rdstate() != ios::eofbit) {
  114. isc_throw(InvalidRdataText,
  115. "Invalid representation for a generic RDLENGTH");
  116. }
  117. if (rdlen < 0 || rdlen > 0xffff) {
  118. isc_throw(InvalidRdataLength, "RDATA length is out of range");
  119. }
  120. iss >> ws; // skip any white spaces
  121. // Hexadecimal encoding of RDATA: each segment must consist of an even
  122. // number of hex digits.
  123. vector<uint8_t> data;
  124. while (!iss.eof() && data.size() < rdlen) {
  125. // extract two characters, which should compose a single byte of data.
  126. char buf[2];
  127. iss.read(buf, sizeof(buf));
  128. if ((iss.rdstate() & (ios::badbit | ios::failbit)) != 0) {
  129. isc_throw(InvalidRdataText,
  130. "Invalid hex encoding of generic RDATA");
  131. }
  132. // convert it to a single byte integer as a hex digit.
  133. istringstream iss_byte(string(buf, sizeof(buf)));
  134. unsigned int ch;
  135. iss_byte >> hex >> ch;
  136. if (iss_byte.rdstate() != ios::eofbit) {
  137. isc_throw(InvalidRdataText,
  138. "Invalid hex encoding of generic RDATA");
  139. }
  140. data.push_back(ch);
  141. iss >> ws; // skip spaces
  142. }
  143. if (!iss.eof()) {
  144. isc_throw(InvalidRdataLength,
  145. "RDLENGTH is too small for generic RDATA");
  146. }
  147. if (data.size() != rdlen) {
  148. isc_throw(InvalidRdataLength,
  149. "Generic RDATA code doesn't match RDLENGTH");
  150. }
  151. impl_ = new GenericImpl(data);
  152. }
  153. Generic::~Generic() {
  154. delete impl_;
  155. }
  156. Generic::Generic(const Generic& source) :
  157. Rdata(), impl_(new GenericImpl(*source.impl_))
  158. {}
  159. Generic&
  160. Generic::operator=(const Generic& source) {
  161. if (impl_ == source.impl_) {
  162. return (*this);
  163. }
  164. GenericImpl* newimpl = new GenericImpl(*source.impl_);
  165. delete impl_;
  166. impl_ = newimpl;
  167. return (*this);
  168. }
  169. namespace {
  170. class UnknownRdataDumper {
  171. public:
  172. UnknownRdataDumper(ostringstream& oss) : oss_(&oss) {}
  173. void operator()(const unsigned char d)
  174. {
  175. *oss_ << setw(2) << static_cast<unsigned int>(d);
  176. }
  177. private:
  178. ostringstream* oss_;
  179. };
  180. }
  181. string
  182. Generic::toText() const {
  183. ostringstream oss;
  184. oss << "\\# " << impl_->data_.size() << " ";
  185. oss.fill('0');
  186. oss << right << hex;
  187. for_each(impl_->data_.begin(), impl_->data_.end(), UnknownRdataDumper(oss));
  188. return (oss.str());
  189. }
  190. void
  191. Generic::toWire(isc::util::OutputBuffer& buffer) const {
  192. buffer.writeData(&impl_->data_[0], impl_->data_.size());
  193. }
  194. void
  195. Generic::toWire(AbstractMessageRenderer& renderer) const {
  196. renderer.writeData(&impl_->data_[0], impl_->data_.size());
  197. }
  198. namespace {
  199. inline int
  200. compare_internal(const GenericImpl& lhs, const GenericImpl& rhs) {
  201. size_t this_len = lhs.data_.size();
  202. size_t other_len = rhs.data_.size();
  203. size_t len = (this_len < other_len) ? this_len : other_len;
  204. int cmp;
  205. if ((cmp = memcmp(&lhs.data_[0], &rhs.data_[0], len))
  206. != 0) {
  207. return (cmp);
  208. } else {
  209. return ((this_len == other_len) ? 0 :
  210. (this_len < other_len) ? -1 : 1);
  211. }
  212. }
  213. }
  214. int
  215. Generic::compare(const Rdata& other) const {
  216. const Generic& other_rdata = dynamic_cast<const Generic&>(other);
  217. return (compare_internal(*impl_, *other_rdata.impl_));
  218. }
  219. std::ostream&
  220. operator<<(std::ostream& os, const Generic& rdata) {
  221. return (os << rdata.toText());
  222. }
  223. } // end of namespace generic
  224. } // end of namespace rdata
  225. }
  226. }