query.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <exceptions/exceptions.h>
  17. namespace isc {
  18. namespace dns {
  19. class Message;
  20. class Name;
  21. class RRType;
  22. }
  23. namespace datasrc {
  24. class MemoryDataSrc;
  25. class Zone;
  26. }
  27. namespace auth {
  28. /// The \c Query class represents a standard DNS query that encapsulates
  29. /// processing logic to answer the query.
  30. ///
  31. /// Many of the design details for this class are still in flux.
  32. /// We'll revisit and update them as we add more functionality, for example:
  33. /// - memory_datasrc parameter of the constructor. It is a data source that
  34. /// uses in memory dedicated backend.
  35. /// - as a related point, we may have to pass the RR class of the query.
  36. /// in the initial implementation the RR class is an attribute of memory
  37. /// datasource and omitted. It's not clear if this assumption holds with
  38. /// generic data sources. On the other hand, it will help keep
  39. /// implementation simpler, and we might rather want to modify the design
  40. /// of the data source on this point.
  41. /// - return value of process(). rather than or in addition to setting the
  42. /// Rcode, we might use it as a return value of \c process().
  43. /// - we'll have to be able to specify whether DNSSEC is requested.
  44. /// It's an open question whether it should be in the constructor or via a
  45. /// separate attribute setter.
  46. /// - likewise, we'll eventually need to do per zone access control, for which
  47. /// we need querier's information such as its IP address.
  48. /// - memory_datasrc and response may better be parameters to process() instead
  49. /// of the constructor.
  50. ///
  51. /// <b>Note:</b> The class name is intentionally the same as the one used in
  52. /// the datasrc library. This is because the plan is to eventually merge
  53. /// the two classes. We could give it a different name such as "AuthQuery"
  54. /// to avoid possible ambiguity, but it may sound redundant in that it's
  55. /// obvious that this class is for authoritative queries.
  56. /// Since the interfaces are very different for now and it's less
  57. /// likely to misuse one of the classes instead of the other
  58. /// accidentally, and since it's considered a temporary development state,
  59. /// we keep this name at the moment.
  60. class Query {
  61. public:
  62. /// Constructor from query parameters.
  63. ///
  64. /// This constructor never throws an exception.
  65. ///
  66. /// \param memory_datasrc The memory datasource wherein the answer to the query is
  67. /// to be found.
  68. /// \param qname The query name
  69. /// \param qtype The RR type of the query
  70. /// \param response The response message to store the answer to the query.
  71. Query(const isc::datasrc::MemoryDataSrc& memory_datasrc,
  72. const isc::dns::Name& qname, const isc::dns::RRType& qtype,
  73. isc::dns::Message& response) :
  74. memory_datasrc_(memory_datasrc), qname_(qname), qtype_(qtype),
  75. response_(response)
  76. {}
  77. /// Process the query.
  78. ///
  79. /// This method first identifies the zone that best matches the query
  80. /// name (and in some cases RR type when the search is dependent on the
  81. /// type) and then searches the zone for an entry that best matches the
  82. /// query name.
  83. /// It then updates the response message accordingly; for example, a
  84. /// successful search would result in adding a corresponding RRset to
  85. /// the answer section of the response.
  86. ///
  87. /// If no matching zone is found in the memory datasource, the RCODE of
  88. /// SERVFAIL will be set in the response.
  89. /// <b>Note:</b> this is different from the error code that BIND 9 returns
  90. /// by default when it's configured as an authoritative-only server (and
  91. /// from the behavior of the BIND 10 datasrc library, which was implemented
  92. /// to be compatible with BIND 9).
  93. /// The difference comes from the fact that BIND 9 returns REFUSED as a
  94. /// result of access control check on the use of its cache.
  95. /// Since BIND 10's authoritative server doesn't have the notion of cache
  96. /// by design, it doesn't make sense to return REFUSED. On the other hand,
  97. /// providing compatible behavior may have its own benefit, so this point
  98. /// should be revisited later.
  99. ///
  100. /// This might throw BadZone or any of its specific subclasses, but that
  101. /// shouldn't happen in real-life (as BadZone means wrong data, it should
  102. /// have been rejected upon loading).
  103. void process() const;
  104. /// \short Bad zone data encountered.
  105. ///
  106. /// This is thrown when process encounteres misconfigured zone in a way
  107. /// it can't continue. This throws, not sets the Rcode, because such
  108. /// misconfigured zone should not be present in the data source and
  109. /// should have been rejected sooner.
  110. struct BadZone : public isc::Exception {
  111. BadZone(const char* file, size_t line, const char* what) :
  112. Exception(file, line, what)
  113. {}
  114. };
  115. /// \short Zone is missing its SOA record.
  116. ///
  117. /// We tried to add a SOA into the authoritative section, but the zone
  118. /// does not contain one.
  119. struct NoSOA : public BadZone {
  120. NoSOA(const char* file, size_t line, const char* what) :
  121. BadZone(file, line, what)
  122. {}
  123. };
  124. private:
  125. const isc::datasrc::MemoryDataSrc& memory_datasrc_;
  126. const isc::dns::Name& qname_;
  127. const isc::dns::RRType& qtype_;
  128. isc::dns::Message& response_;
  129. /**
  130. * \short Adds a SOA.
  131. *
  132. * Adds a SOA of the zone into the authority zone of response_.
  133. * Can throw NoSOA.
  134. */
  135. void putSOA(const isc::datasrc::Zone& zone) const;
  136. };
  137. }
  138. }
  139. // Local Variables:
  140. // mode: c++
  141. // End: