rrset.cc 7.6 KB

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