rrset.cc 6.2 KB

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