data_source.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright (C) 2009 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. #ifndef __DATA_SOURCE_H
  16. #define __DATA_SOURCE_H
  17. #include <vector>
  18. #include <boost/shared_ptr.hpp>
  19. #include <dns/name.h>
  20. #include <dns/rrclass.h>
  21. namespace isc {
  22. namespace dns {
  23. class Name;
  24. class RRType;
  25. class RRset;
  26. class RRsetList;
  27. }
  28. namespace auth {
  29. class NameMatch;
  30. class Query;
  31. class Nsec3Param;
  32. class DataSrc;
  33. typedef boost::shared_ptr<DataSrc> DataSrcPtr;
  34. typedef boost::shared_ptr<const DataSrc> ConstDataSrcPtr;
  35. class AbstractDataSrc {
  36. ///
  37. /// \name Constructors, Assignment Operator and Destructor.
  38. ///
  39. /// Note: The copy constructor and the assignment operator are intentionally
  40. /// defined as private to make it explicit that this is a pure base class.
  41. private:
  42. AbstractDataSrc(const AbstractDataSrc& source);
  43. AbstractDataSrc& operator=(const AbstractDataSrc& source);
  44. protected:
  45. /// \brief The default constructor.
  46. ///
  47. /// This is intentionally defined as \c protected as this base class should
  48. /// never be instantiated (except as part of a derived class).
  49. AbstractDataSrc() {}
  50. public:
  51. /// \brief The destructor.
  52. virtual ~AbstractDataSrc() {};
  53. //@}
  54. enum Result {
  55. SUCCESS,
  56. ERROR,
  57. NOT_IMPLEMENTED
  58. };
  59. // These flags indicate conditions encountered while processing a query.
  60. //
  61. // REFERRAL: The node contains an NS record
  62. // CNAME_FOUND: The node contains a CNAME record
  63. // NAME_NOT_FOUND: The node does not exist in the data source.
  64. // TYPE_NOT_FOUND: The node does not contain the requested RRType
  65. // NO_SUCH_ZONE: The zone does not exist in this data source.
  66. enum QueryResponseFlags {
  67. REFERRAL = 0x01,
  68. CNAME_FOUND = 0x02,
  69. NAME_NOT_FOUND = 0x04,
  70. TYPE_NOT_FOUND = 0x08,
  71. NO_SUCH_ZONE = 0x10
  72. };
  73. // 'High-level' methods. These will be implemented by the
  74. // general DataSrc class, and SHOULD NOT be overwritten by subclasses.
  75. virtual void doQuery(Query& query) = 0;
  76. // XXX: High-level methods to be implemented later:
  77. // virtual void doUpdate(Update update) = 0;
  78. // virtual void doXfr(Query query) = 0;
  79. // 'Medium-level' methods. This will be implemented by the general
  80. // DataSrc class but MAY be overwritten by subclasses.
  81. virtual void findClosestEnclosure(NameMatch& match) const = 0;
  82. // Optional 'low-level' methods. These will have stub implementations
  83. // in the general DataSrc class but MAY be overwritten by subclasses
  84. virtual Result init() = 0;
  85. virtual Result close() = 0;
  86. // Mandatory 'low-level' methods: These will NOT be implemented by
  87. // the general DataSrc class; subclasses MUST implement them.
  88. virtual Result findRRset(const Query& q,
  89. const isc::dns::Name& qname,
  90. const isc::dns::RRClass& qclass,
  91. const isc::dns::RRType& qtype,
  92. isc::dns::RRsetList& target,
  93. uint32_t& flags,
  94. const isc::dns::Name* zonename) const = 0;
  95. virtual Result findExactRRset(const Query& q,
  96. const isc::dns::Name& qname,
  97. const isc::dns::RRClass& qclass,
  98. const isc::dns::RRType& qtype,
  99. isc::dns::RRsetList& target,
  100. uint32_t& flags,
  101. const isc::dns::Name* zonename) const = 0;
  102. // These will have dumb implementations in the general DataSrc
  103. // class, and SHOULD be overwritten by subclasses.
  104. virtual Result findAddrs(const Query& q,
  105. const isc::dns::Name& qname,
  106. const isc::dns::RRClass& qclass,
  107. isc::dns::RRsetList& target,
  108. uint32_t& flags,
  109. const isc::dns::Name* zonename) const = 0;
  110. virtual Result findReferral(const Query& q,
  111. const isc::dns::Name& qname,
  112. const isc::dns::RRClass& qclass,
  113. isc::dns::RRsetList& target,
  114. uint32_t& flags,
  115. const isc::dns::Name* zonename) const = 0;
  116. // This MUST be implemented by concrete data sources which support
  117. // DNSSEC, but is optional for others (e.g., the static data source).
  118. virtual Result findPreviousName(const Query& q,
  119. const isc::dns::Name& qname,
  120. isc::dns::Name& target,
  121. const isc::dns::Name* zonename) const = 0;
  122. // This MUST be implemented by concrete data sources which support
  123. // NSEC3, but is optional for others
  124. virtual Result findCoveringNSEC3(const Query& q,
  125. const std::string& hash,
  126. const isc::dns::Name& zonename,
  127. isc::dns::RRsetList& target) const = 0;
  128. };
  129. // Base class for a DNS Data Source
  130. class DataSrc : public AbstractDataSrc {
  131. ///
  132. /// \name Constructors, Assignment Operator and Destructor.
  133. ///
  134. /// Note: The copy constructor and the assignment operator are intentionally
  135. /// defined as private.
  136. private:
  137. DataSrc(const DataSrc& source);
  138. DataSrc& operator=(const DataSrc& source);
  139. public:
  140. DataSrc() : rrclass(isc::dns::RRClass::IN()) {}
  141. DataSrc(const isc::dns::RRClass& c) : rrclass(c) {}
  142. /// \brief The destructor.
  143. virtual ~DataSrc() {};
  144. //@}
  145. virtual void doQuery(Query& q);
  146. virtual void findClosestEnclosure(NameMatch& match) const = 0;
  147. const isc::dns::RRClass& getClass() const { return rrclass; }
  148. void setClass(isc::dns::RRClass& c) { rrclass = c; }
  149. void setClass(const isc::dns::RRClass& c) { rrclass = c; }
  150. Result init() { return NOT_IMPLEMENTED; }
  151. Result close() { return NOT_IMPLEMENTED; }
  152. virtual Result findRRset(const Query& q,
  153. const isc::dns::Name& qname,
  154. const isc::dns::RRClass& qclass,
  155. const isc::dns::RRType& qtype,
  156. isc::dns::RRsetList& target,
  157. uint32_t& flags,
  158. const isc::dns::Name* zonename) const = 0;
  159. virtual Result findExactRRset(const Query& q,
  160. const isc::dns::Name& qname,
  161. const isc::dns::RRClass& qclass,
  162. const isc::dns::RRType& qtype,
  163. isc::dns::RRsetList& target,
  164. uint32_t& flags,
  165. const isc::dns::Name* zonename) const = 0;
  166. virtual Result findAddrs(const Query& q,
  167. const isc::dns::Name& qname,
  168. const isc::dns::RRClass& qclass,
  169. isc::dns::RRsetList& target,
  170. uint32_t& flags,
  171. const isc::dns::Name* zonename) const;
  172. virtual Result findReferral(const Query& q,
  173. const isc::dns::Name& qname,
  174. const isc::dns::RRClass& qclass,
  175. isc::dns::RRsetList& target,
  176. uint32_t& flags,
  177. const isc::dns::Name* zonename) const;
  178. virtual Result findPreviousName(const Query& q,
  179. const isc::dns::Name& qname,
  180. isc::dns::Name& target,
  181. const isc::dns::Name* zonename) const = 0;
  182. virtual Result findCoveringNSEC3(const Query& q,
  183. const std::string& hash,
  184. const isc::dns::Name& zonename,
  185. isc::dns::RRsetList& target) const = 0;
  186. private:
  187. isc::dns::RRClass rrclass;
  188. };
  189. class MetaDataSrc : public DataSrc {
  190. ///
  191. /// \name Constructors, Assignment Operator and Destructor.
  192. ///
  193. /// Note: The copy constructor and the assignment operator are intentionally
  194. /// defined as private.
  195. //@{
  196. private:
  197. MetaDataSrc(const MetaDataSrc& source);
  198. MetaDataSrc& operator=(const MetaDataSrc& source);
  199. public:
  200. MetaDataSrc() : DataSrc(isc::dns::RRClass::ANY()) {}
  201. MetaDataSrc(const isc::dns::RRClass& c) : DataSrc(c) {}
  202. /// \brief The destructor.
  203. virtual ~MetaDataSrc() {}
  204. //@}
  205. void addDataSrc(ConstDataSrcPtr data_src);
  206. void findClosestEnclosure(NameMatch& match) const;
  207. // Actual queries for data should not be sent to a MetaDataSrc object,
  208. // so we return NOT_IMPLEMENTED if we receive any.
  209. //
  210. // The proper way to use the MetaDataSrc is to run findClosestEnclosure()
  211. // to get a pointer to the best concrete data source for the specified
  212. // zone, then send all queries directly to that data source.
  213. Result findRRset(const Query& q, const isc::dns::Name& qname,
  214. const isc::dns::RRClass& qclass,
  215. const isc::dns::RRType& qtype,
  216. isc::dns::RRsetList& target, uint32_t& flags,
  217. const isc::dns::Name* zonename) const
  218. {
  219. return (NOT_IMPLEMENTED);
  220. }
  221. Result findExactRRset(const Query& q, const isc::dns::Name& qname,
  222. const isc::dns::RRClass& qclass,
  223. const isc::dns::RRType& qtype,
  224. isc::dns::RRsetList& target, uint32_t& flags,
  225. const isc::dns::Name* zonename) const
  226. {
  227. return (NOT_IMPLEMENTED);
  228. }
  229. Result findAddrs(const Query& q, const isc::dns::Name& qname,
  230. const isc::dns::RRClass& qclass,
  231. isc::dns::RRsetList& target, uint32_t& flags,
  232. const isc::dns::Name* zonename) const
  233. {
  234. return (NOT_IMPLEMENTED);
  235. }
  236. Result findReferral(const Query& q, const isc::dns::Name& qname,
  237. const isc::dns::RRClass& qclass,
  238. isc::dns::RRsetList& target, uint32_t& flags,
  239. const isc::dns::Name* zonename) const
  240. {
  241. return (NOT_IMPLEMENTED);
  242. }
  243. virtual Result findPreviousName(const Query& q,
  244. const isc::dns::Name& qname,
  245. isc::dns::Name& target,
  246. const isc::dns::Name* zonename) const
  247. {
  248. return (NOT_IMPLEMENTED);
  249. }
  250. virtual Result findCoveringNSEC3(const Query& q,
  251. const std::string& qname,
  252. const isc::dns::Name& zonename,
  253. isc::dns::RRsetList& target) const
  254. {
  255. return (NOT_IMPLEMENTED);
  256. }
  257. private:
  258. std::vector<ConstDataSrcPtr> data_sources;
  259. };
  260. class NameMatch {
  261. public:
  262. NameMatch(const isc::dns::Name& qname) :
  263. closest_name_(NULL), best_source_(NULL), qname_(qname) {}
  264. ~NameMatch();
  265. void update(const DataSrc& new_source, const isc::dns::Name& container);
  266. const isc::dns::Name& qname() { return (qname_); }
  267. const isc::dns::Name* closestName() { return (closest_name_); }
  268. const DataSrc* bestDataSrc() { return (best_source_); }
  269. private:
  270. const isc::dns::Name* closest_name_;
  271. const DataSrc* best_source_;
  272. const isc::dns::Name qname_;
  273. };
  274. class Nsec3Param {
  275. public:
  276. Nsec3Param(uint8_t a, uint8_t f, uint16_t i, const std::vector<uint8_t>& s);
  277. const uint8_t algorithm_;
  278. const uint8_t flags_;
  279. const uint16_t iterations_;
  280. const std::vector<uint8_t> salt_;
  281. std::string getHash(const isc::dns::Name& name) const;
  282. };
  283. }
  284. }
  285. #endif
  286. // Local Variables:
  287. // mode: c++
  288. // End: