rrset.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // $Id$
  15. #include <algorithm>
  16. #include <string>
  17. #include <vector>
  18. #include <boost/shared_ptr.hpp>
  19. #include <dns/buffer.h>
  20. #include <dns/messagerenderer.h>
  21. #include <dns/name.h>
  22. #include <dns/rrclass.h>
  23. #include <dns/rrtype.h>
  24. #include <dns/rrttl.h>
  25. #include <dns/rrset.h>
  26. using namespace std;
  27. using namespace isc::dns;
  28. using namespace isc::dns::rdata;
  29. namespace isc {
  30. namespace dns {
  31. void
  32. AbstractRRset::addRdata(const Rdata& rdata) {
  33. addRdata(createRdata(getType(), getClass(), rdata));
  34. }
  35. string
  36. AbstractRRset::toText() const {
  37. string s;
  38. RdataIteratorPtr it = getRdataIterator();
  39. it->first();
  40. if (it->isLast()) {
  41. isc_throw(EmptyRRset, "ToText() is attempted for an empty RRset");
  42. }
  43. do {
  44. s += getName().toText() + " " + getTTL().toText() + " " +
  45. getClass().toText() + " " + getType().toText() + " " +
  46. it->getCurrent().toText() + "\n";
  47. it->next();
  48. } while (!it->isLast());
  49. return (s);
  50. }
  51. namespace {
  52. template <typename T>
  53. inline unsigned int
  54. rrsetToWire(const AbstractRRset& rrset, T& output, const size_t limit) {
  55. unsigned int n = 0;
  56. RdataIteratorPtr it = rrset.getRdataIterator();
  57. it->first();
  58. if (it->isLast()) {
  59. isc_throw(EmptyRRset, "ToWire() is attempted for an empty RRset");
  60. }
  61. // sort the set of Rdata based on rrset-order and sortlist, and possible
  62. // other options. Details to be considered.
  63. do {
  64. const size_t pos0 = output.getLength();
  65. assert(pos0 < 65536);
  66. rrset.getName().toWire(output);
  67. rrset.getType().toWire(output);
  68. rrset.getClass().toWire(output);
  69. rrset.getTTL().toWire(output);
  70. const size_t pos = output.getLength();
  71. output.skip(sizeof(uint16_t)); // leave the space for RDLENGTH
  72. it->getCurrent().toWire(output);
  73. output.writeUint16At(output.getLength() - pos - sizeof(uint16_t), pos);
  74. if (limit > 0 && output.getLength() > limit) {
  75. // truncation is needed
  76. output.trim(output.getLength() - pos0);
  77. return (n);
  78. }
  79. it->next();
  80. ++n;
  81. } while (!it->isLast());
  82. return (n);
  83. }
  84. }
  85. unsigned int
  86. AbstractRRset::toWire(OutputBuffer& buffer) const {
  87. return (rrsetToWire<OutputBuffer>(*this, buffer, 0));
  88. }
  89. unsigned int
  90. AbstractRRset::toWire(MessageRenderer& renderer) const {
  91. const unsigned int rrs_written = rrsetToWire<MessageRenderer>(
  92. *this, renderer, renderer.getLengthLimit());
  93. if (getRdataCount() > rrs_written) {
  94. renderer.setTruncated();
  95. }
  96. return (rrs_written);
  97. }
  98. ostream&
  99. operator<<(ostream& os, const AbstractRRset& rrset) {
  100. os << rrset.toText();
  101. return (os);
  102. }
  103. /// \brief This encapsulates the actual implementation of the \c BasicRRset
  104. /// class. It's hidden from applications.
  105. class BasicRRsetImpl {
  106. public:
  107. BasicRRsetImpl(const Name& name, const RRClass& rrclass,
  108. const RRType& rrtype, const RRTTL& ttl) :
  109. name_(name), rrclass_(rrclass), rrtype_(rrtype), ttl_(ttl) {}
  110. Name name_;
  111. RRClass rrclass_;
  112. RRType rrtype_;
  113. RRTTL ttl_;
  114. // XXX: "list" is not a good name: It in fact isn't a list; more conceptual
  115. // name than a data structure name is generally better. But since this
  116. // is only used in the internal implementation we'll live with it.
  117. vector<ConstRdataPtr> rdatalist_;
  118. };
  119. BasicRRset::BasicRRset(const Name& name, const RRClass& rrclass,
  120. const RRType& rrtype, const RRTTL& ttl)
  121. {
  122. impl_ = new BasicRRsetImpl(name, rrclass, rrtype, ttl);
  123. }
  124. BasicRRset::~BasicRRset() {
  125. delete impl_;
  126. }
  127. void
  128. BasicRRset::addRdata(ConstRdataPtr rdata) {
  129. impl_->rdatalist_.push_back(rdata);
  130. }
  131. void
  132. BasicRRset::addRdata(const Rdata& rdata) {
  133. AbstractRRset::addRdata(rdata);
  134. }
  135. unsigned int
  136. BasicRRset::getRdataCount() const {
  137. return (impl_->rdatalist_.size());
  138. }
  139. const Name&
  140. BasicRRset::getName() const {
  141. return (impl_->name_);
  142. }
  143. const RRClass&
  144. BasicRRset::getClass() const {
  145. return (impl_->rrclass_);
  146. }
  147. const RRType&
  148. BasicRRset::getType() const {
  149. return (impl_->rrtype_);
  150. }
  151. const RRTTL&
  152. BasicRRset::getTTL() const {
  153. return (impl_->ttl_);
  154. }
  155. void
  156. BasicRRset::setName(const Name& name) {
  157. impl_->name_ = name;
  158. }
  159. void
  160. BasicRRset::setTTL(const RRTTL& ttl) {
  161. impl_->ttl_ = ttl;
  162. }
  163. string
  164. BasicRRset::toText() const {
  165. return (AbstractRRset::toText());
  166. }
  167. unsigned int
  168. BasicRRset::toWire(OutputBuffer& buffer) const {
  169. return (AbstractRRset::toWire(buffer));
  170. }
  171. unsigned int
  172. BasicRRset::toWire(MessageRenderer& renderer) const {
  173. return (AbstractRRset::toWire(renderer));
  174. }
  175. RRset::RRset(const Name& name, const RRClass& rrclass,
  176. const RRType& rrtype, const RRTTL& ttl) :
  177. BasicRRset(name, rrclass, rrtype, ttl)
  178. {
  179. rrsig_ = RRsetPtr();
  180. }
  181. RRset::~RRset() {}
  182. namespace {
  183. class BasicRdataIterator : public RdataIterator {
  184. private:
  185. BasicRdataIterator() {}
  186. public:
  187. BasicRdataIterator(const std::vector<rdata::ConstRdataPtr>& datavector) :
  188. datavector_(&datavector) {}
  189. ~BasicRdataIterator() {}
  190. virtual void first() { it_ = datavector_->begin(); }
  191. virtual void next() { ++it_; }
  192. virtual const rdata::Rdata& getCurrent() const { return (**it_); }
  193. virtual bool isLast() const { return (it_ == datavector_->end()); }
  194. private:
  195. const std::vector<rdata::ConstRdataPtr>* datavector_;
  196. std::vector<rdata::ConstRdataPtr>::const_iterator it_;
  197. };
  198. }
  199. RdataIteratorPtr
  200. BasicRRset::getRdataIterator() const {
  201. return (RdataIteratorPtr(new BasicRdataIterator(impl_->rdatalist_)));
  202. }
  203. }
  204. }