rdatafields.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <stdint.h>
  15. #include <cassert>
  16. #include <vector>
  17. #include <exceptions/exceptions.h>
  18. #include <util/buffer.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/name.h>
  21. #include <dns/rdata.h>
  22. #include <dns/rdatafields.h>
  23. using namespace std;
  24. using namespace isc::dns;
  25. using namespace isc::dns::rdata;
  26. using isc::util::OutputBuffer;
  27. using isc::util::InputBuffer;
  28. namespace isc {
  29. namespace dns {
  30. namespace rdata {
  31. /// This is a helper class for \c RdataFields.
  32. ///
  33. /// It manages a local storage for the data when \c RdataFields is constructed
  34. /// from an \c Rdata.
  35. /// To minimize construction overhead in the other case, an instance of
  36. /// this class is instantiated only when necessary - we don't need the vectors
  37. /// when only rendering.
  38. struct RdataFields::RdataFieldsDetail {
  39. RdataFieldsDetail(const vector<FieldSpec>& fields,
  40. const uint8_t* data, size_t data_length) :
  41. allocated_fields_(fields),
  42. allocated_data_(data, data + data_length)
  43. {}
  44. const vector<FieldSpec> allocated_fields_;
  45. const vector<uint8_t> allocated_data_;
  46. };
  47. namespace {
  48. // This class is used to divide the content of RDATA into \c RdataField
  49. // fields via message rendering logic.
  50. // The idea is to identify domain name fields in the writeName() method,
  51. // and determine whether they are compressible using the "compress"
  52. // parameter.
  53. // Other types of data are simply copied into the internal buffer, and
  54. // consecutive such fields are combined into a single \c RdataField field.
  55. //
  56. // Technically, this use of inheritance may be considered a violation of
  57. // Liskov Substitution Principle in that it doesn't actually compress domain
  58. // names, and some of the methods are not expected to be used.
  59. // In fact, skip() or trim() may not be make much sense in this context.
  60. // Nevertheless we keep this idea at the moment. Since the usage is limited
  61. // (it's only used within this file, and only used with \c Rdata variants),
  62. // it's hopefully an acceptable practice.
  63. class RdataFieldComposer : public AbstractMessageRenderer {
  64. public:
  65. RdataFieldComposer() :
  66. truncated_(false), length_limit_(65535),
  67. mode_(CASE_INSENSITIVE), last_data_pos_(0)
  68. {}
  69. virtual ~RdataFieldComposer() {}
  70. virtual bool isTruncated() const { return (truncated_); }
  71. virtual size_t getLengthLimit() const { return (length_limit_); }
  72. virtual CompressMode getCompressMode() const { return (mode_); }
  73. virtual void setTruncated() { truncated_ = true; }
  74. virtual void setLengthLimit(size_t len) { length_limit_ = len; }
  75. virtual void setCompressMode(CompressMode mode) { mode_ = mode; }
  76. virtual void writeName(const LabelSequence&, bool) {}
  77. virtual void writeName(const Name& name, bool compress) {
  78. extendData();
  79. const RdataFields::Type field_type =
  80. compress ? RdataFields::COMPRESSIBLE_NAME :
  81. RdataFields::INCOMPRESSIBLE_NAME;
  82. // TODO: When we get rid of need for getBuffer, we can output the name
  83. // to a buffer and then write the buffer inside
  84. name.toWire(getBuffer());
  85. fields_.push_back(RdataFields::FieldSpec(field_type,
  86. name.getLength()));
  87. last_data_pos_ = getLength();
  88. }
  89. virtual void clear() {
  90. isc_throw(Unexpected, "unexpected clear() for RdataFieldComposer");
  91. }
  92. bool truncated_;
  93. size_t length_limit_;
  94. CompressMode mode_;
  95. vector<RdataFields::FieldSpec> fields_;
  96. vector<RdataFields::FieldSpec>& getFields() {
  97. extendData();
  98. return (fields_);
  99. }
  100. // We use generict write* methods, with the exception of writeName.
  101. // So new data can arrive without us knowing it, this considers all new
  102. // data to be just data and extends the fields to take it into account.
  103. size_t last_data_pos_;
  104. void extendData() {
  105. // No news, return to work
  106. if (getLength() == last_data_pos_) {
  107. return;
  108. }
  109. // The new bytes are just ordinary uninteresting data
  110. if (fields_.empty() || fields_.back().type != RdataFields::DATA) {
  111. fields_.push_back(RdataFields::FieldSpec(RdataFields::DATA, 0));
  112. }
  113. // We added this much data from last time
  114. fields_.back().len += getLength() - last_data_pos_;
  115. last_data_pos_ = getLength();
  116. }
  117. };
  118. }
  119. RdataFields::RdataFields(const Rdata& rdata) {
  120. RdataFieldComposer field_composer;
  121. rdata.toWire(field_composer);
  122. nfields_ = field_composer.getFields().size();
  123. data_length_ = field_composer.getLength();
  124. if (nfields_ > 0) {
  125. assert(data_length_ > 0);
  126. detail_ = new RdataFieldsDetail(field_composer.getFields(),
  127. static_cast<const uint8_t*>
  128. (field_composer.getData()),
  129. field_composer.getLength());
  130. data_ = &detail_->allocated_data_[0];
  131. fields_ = &detail_->allocated_fields_[0];
  132. } else {
  133. assert(data_length_ == 0);
  134. detail_ = NULL;
  135. data_ = NULL;
  136. fields_ = NULL;
  137. }
  138. }
  139. RdataFields::RdataFields(const void* fields, const unsigned int fields_length,
  140. const void* data, const size_t data_length) :
  141. fields_(static_cast<const FieldSpec*>(fields)),
  142. nfields_(fields_length / sizeof(*fields_)),
  143. data_(static_cast<const uint8_t*>(data)),
  144. data_length_(data_length),
  145. detail_(NULL)
  146. {
  147. if ((fields_ == NULL && nfields_ > 0) ||
  148. (fields_ != NULL && nfields_ == 0)) {
  149. isc_throw(InvalidParameter,
  150. "Inconsistent parameters for RdataFields: fields_length ("
  151. << fields_length << ") and fields conflict each other");
  152. }
  153. if ((data_ == NULL && data_length_ > 0) ||
  154. (data_ != NULL && data_length_ == 0)) {
  155. isc_throw(InvalidParameter,
  156. "Inconsistent parameters for RdataFields: data length ("
  157. << data_length_ << ") and data conflict each other");
  158. }
  159. size_t total_length = 0;
  160. for (unsigned int i = 0; i < nfields_; ++i) {
  161. total_length += fields_[i].len;
  162. }
  163. if (total_length != data_length_) {
  164. isc_throw(InvalidParameter,
  165. "Inconsistent parameters for RdataFields; "
  166. "fields len: " << total_length <<
  167. " data len: " << data_length_);
  168. }
  169. }
  170. RdataFields::~RdataFields() {
  171. delete detail_;
  172. }
  173. RdataFields::FieldSpec
  174. RdataFields::getFieldSpec(const unsigned int field_id) const {
  175. if (field_id >= nfields_) {
  176. isc_throw(OutOfRange, "Rdata field ID is out of range: " << field_id);
  177. }
  178. return (fields_[field_id]);
  179. }
  180. void
  181. RdataFields::toWire(AbstractMessageRenderer& renderer) const {
  182. size_t offset = 0;
  183. for (unsigned int i = 0; i < nfields_; ++i) {
  184. if (fields_[i].type == DATA) {
  185. renderer.writeData(data_ + offset, fields_[i].len);
  186. } else {
  187. // XXX: this is inefficient. Even if it's quite likely the
  188. // data is a valid wire representation of a name we parse
  189. // it to construct the Name object in the generic mode.
  190. // This should be improved in a future version.
  191. InputBuffer buffer(data_ + offset, fields_[i].len);
  192. renderer.writeName(Name(buffer),
  193. fields_[i].type == COMPRESSIBLE_NAME);
  194. }
  195. offset += fields_[i].len;
  196. }
  197. }
  198. void
  199. RdataFields::toWire(OutputBuffer& buffer) const {
  200. buffer.writeData(data_, data_length_);
  201. }
  202. } // end of namespace rdata
  203. } // end of namespace dns
  204. } // end of namespace isc