dns_client.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (C) 2013 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 <d2/dns_client.h>
  15. #include <d2/d2_log.h>
  16. #include <dns/messagerenderer.h>
  17. #include <limits>
  18. namespace isc {
  19. namespace d2 {
  20. namespace {
  21. // OutputBuffer objects are pre-allocated before data is written to them.
  22. // This is a default number of bytes for the buffers we create within
  23. // DNSClient class.
  24. const size_t DEFAULT_BUFFER_SIZE = 128;
  25. }
  26. using namespace isc::util;
  27. using namespace isc::asiolink;
  28. using namespace isc::asiodns;
  29. using namespace isc::dns;
  30. // This class provides the implementation for the DNSClient. This allows for
  31. // the separation of the DNSClient interface from the implementation details.
  32. // Currently, implementation uses IOFetch object to handle asynchronous
  33. // communication with the DNS. This design may be revisited in the future. If
  34. // implementation is changed, the DNSClient API will remain unchanged thanks
  35. // to this separation.
  36. class DNSClientImpl : public asiodns::IOFetch::Callback {
  37. public:
  38. // A buffer holding response from a DNS.
  39. util::OutputBufferPtr in_buf_;
  40. // A caller-supplied object holding a parsed response from DNS.
  41. D2UpdateMessagePtr response_;
  42. // A caller-supplied external callback which is invoked when DNS message
  43. // exchange is complete or interrupted.
  44. DNSClient::Callback* callback_;
  45. // A Transport Layer protocol used to communicate with a DNS.
  46. DNSClient::Protocol proto_;
  47. // Constructor and Destructor
  48. DNSClientImpl(D2UpdateMessagePtr& response_placeholder,
  49. DNSClient::Callback* callback,
  50. const DNSClient::Protocol proto);
  51. virtual ~DNSClientImpl();
  52. // This internal callback is called when the DNS update message exchange is
  53. // complete. It further invokes the external callback provided by a caller.
  54. // Before external callback is invoked, an object of the D2UpdateMessage
  55. // type, representing a response from the server is set.
  56. virtual void operator()(asiodns::IOFetch::Result result);
  57. // Starts asynchronous DNS Update.
  58. void doUpdate(asiolink::IOService& io_service,
  59. const asiolink::IOAddress& ns_addr,
  60. const uint16_t ns_port,
  61. D2UpdateMessage& update,
  62. const unsigned int wait);
  63. // This function maps the IO error to the DNSClient error.
  64. DNSClient::Status getStatus(const asiodns::IOFetch::Result);
  65. };
  66. DNSClientImpl::DNSClientImpl(D2UpdateMessagePtr& response_placeholder,
  67. DNSClient::Callback* callback,
  68. const DNSClient::Protocol proto)
  69. : in_buf_(new OutputBuffer(DEFAULT_BUFFER_SIZE)),
  70. response_(response_placeholder), callback_(callback), proto_(proto) {
  71. // @todo Currently we only support UDP. The support for TCP is planned for
  72. // the future release.
  73. if (proto_ == DNSClient::TCP) {
  74. isc_throw(isc::NotImplemented, "TCP is currently not supported as a"
  75. << " Transport protocol for DNS Updates; please use UDP");
  76. }
  77. // Given that we already eliminated the possibility that TCP is used, it
  78. // would be sufficient to check that (proto != DNSClient::UDP). But, once
  79. // support TCP is added the check above will disappear and the extra check
  80. // will be needed here anyway.
  81. // Note that cascaded check is used here instead of:
  82. // if (proto_ != DNSClient::TCP && proto_ != DNSClient::UDP)..
  83. // because some versions of GCC compiler complain that check above would
  84. // be always 'false' due to limited range of enumeration. In fact, it is
  85. // possible to pass out of range integral value through enum and it should
  86. // be caught here.
  87. if (proto_ != DNSClient::TCP) {
  88. if (proto_ != DNSClient::UDP) {
  89. isc_throw(isc::NotImplemented, "invalid transport protocol type '"
  90. << proto_ << "' specified for DNS Updates");
  91. }
  92. }
  93. if (!response_) {
  94. isc_throw(BadValue, "a pointer to an object to encapsulate the DNS"
  95. " server must be provided; found NULL value");
  96. }
  97. }
  98. DNSClientImpl::~DNSClientImpl() {
  99. }
  100. void
  101. DNSClientImpl::operator()(asiodns::IOFetch::Result result) {
  102. // Get the status from IO. If no success, we just call user's callback
  103. // and pass the status code.
  104. DNSClient::Status status = getStatus(result);
  105. if (status == DNSClient::SUCCESS) {
  106. InputBuffer response_buf(in_buf_->getData(), in_buf_->getLength());
  107. // Server's response may be corrupted. In such case, fromWire will
  108. // throw an exception. We want to catch this exception to return
  109. // appropriate status code to the caller and log this event.
  110. try {
  111. response_->fromWire(response_buf);
  112. } catch (const Exception& ex) {
  113. status = DNSClient::INVALID_RESPONSE;
  114. LOG_DEBUG(dctl_logger, DBGLVL_TRACE_DETAIL,
  115. DHCP_DDNS_INVALID_RESPONSE).arg(ex.what());
  116. }
  117. }
  118. // Once we are done with internal business, let's call a callback supplied
  119. // by a caller.
  120. if (callback_ != NULL) {
  121. (*callback_)(status);
  122. }
  123. }
  124. DNSClient::Status
  125. DNSClientImpl::getStatus(const asiodns::IOFetch::Result result) {
  126. switch (result) {
  127. case IOFetch::SUCCESS:
  128. return (DNSClient::SUCCESS);
  129. case IOFetch::TIME_OUT:
  130. return (DNSClient::TIMEOUT);
  131. case IOFetch::STOPPED:
  132. return (DNSClient::IO_STOPPED);
  133. default:
  134. ;
  135. }
  136. return (DNSClient::OTHER);
  137. }
  138. void
  139. DNSClientImpl::doUpdate(IOService& io_service,
  140. const IOAddress& ns_addr,
  141. const uint16_t ns_port,
  142. D2UpdateMessage& update,
  143. const unsigned int wait) {
  144. // A renderer is used by the toWire function which creates the on-wire data
  145. // from the DNS Update message. A renderer has its internal buffer where it
  146. // renders data by default. However, this buffer can't be directly accessed.
  147. // Fortunately, the renderer's API accepts user-supplied buffers. So, let's
  148. // create our own buffer and pass it to the renderer so as the message is
  149. // rendered to this buffer. Finally, we pass this buffer to IOFetch.
  150. dns::MessageRenderer renderer;
  151. OutputBufferPtr msg_buf(new OutputBuffer(DEFAULT_BUFFER_SIZE));
  152. renderer.setBuffer(msg_buf.get());
  153. // Render DNS Update message. This may throw a bunch of exceptions if
  154. // invalid message object is given.
  155. update.toWire(renderer);
  156. // IOFetch has all the mechanisms that we need to perform asynchronous
  157. // communication with the DNS server. The last but one argument points to
  158. // this object as a completion callback for the message exchange. As a
  159. // result operator()(Status) will be called.
  160. // Timeout value is explicitly cast to the int type to avoid warnings about
  161. // overflows when doing implicit cast. It should have been checked by the
  162. // caller that the unsigned timeout value will fit into int.
  163. IOFetch io_fetch(IOFetch::UDP, io_service, msg_buf, ns_addr, ns_port,
  164. in_buf_, this, static_cast<int>(wait));
  165. // Post the task to the task queue in the IO service. Caller will actually
  166. // run these tasks by executing IOService::run.
  167. io_service.post(io_fetch);
  168. }
  169. DNSClient::DNSClient(D2UpdateMessagePtr& response_placeholder,
  170. Callback* callback, const DNSClient::Protocol proto)
  171. : impl_(new DNSClientImpl(response_placeholder, callback, proto)) {
  172. }
  173. DNSClient::~DNSClient() {
  174. delete (impl_);
  175. }
  176. unsigned int
  177. DNSClient::getMaxTimeout() {
  178. static const unsigned int max_timeout = std::numeric_limits<int>::max();
  179. return (max_timeout);
  180. }
  181. void
  182. DNSClient::doUpdate(IOService&,
  183. const IOAddress&,
  184. const uint16_t,
  185. D2UpdateMessage&,
  186. const unsigned int,
  187. const dns::TSIGKey&) {
  188. isc_throw(isc::NotImplemented, "TSIG is currently not supported for"
  189. "DNS Update message");
  190. }
  191. void
  192. DNSClient::doUpdate(IOService& io_service,
  193. const IOAddress& ns_addr,
  194. const uint16_t ns_port,
  195. D2UpdateMessage& update,
  196. const unsigned int wait) {
  197. // The underlying implementation which we use to send DNS Updates uses
  198. // signed integers for timeout. If we want to avoid overflows we need to
  199. // respect this limitation here.
  200. if (wait > getMaxTimeout()) {
  201. isc_throw(isc::BadValue, "A timeout value for DNS Update request must"
  202. " not exceed " << getMaxTimeout()
  203. << ". Provided timeout value is '" << wait << "'");
  204. }
  205. impl_->doUpdate(io_service, ns_addr, ns_port, update, wait);
  206. }
  207. } // namespace d2
  208. } // namespace isc