io_fetch.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef __IO_FETCH_H
  15. #define __IO_FETCH_H 1
  16. #include <config.h>
  17. #include <boost/shared_array.hpp>
  18. #include <boost/shared_ptr.hpp>
  19. #include <boost/date_time/posix_time/posix_time_types.hpp>
  20. #include <coroutine.h>
  21. #include <asio/error_code.hpp>
  22. #include <util/buffer.h>
  23. #include <dns/question.h>
  24. namespace asiolink {
  25. // Forward declarations
  26. class IOAddress;
  27. class IOFetchData;
  28. class IOService;
  29. /// \brief Upstream Fetch Processing
  30. ///
  31. /// IOFetch is the class used to send upstream fetches and to handle responses.
  32. ///
  33. /// \param E Endpoint type to use.
  34. class IOFetch : public coroutine {
  35. public:
  36. /// \brief Protocol to use on the fetch
  37. enum Protocol {
  38. UDP = 0,
  39. TCP = 1
  40. };
  41. /// \brief Origin of Asynchronous I/O Call
  42. ///
  43. /// Indicates what initiated an asynchronous I/O call and used in deciding
  44. /// what error message to output if the I/O fails.
  45. enum Origin {
  46. NONE = 0, ///< No asynchronous call outstanding
  47. OPEN = 1,
  48. SEND = 2,
  49. RECEIVE = 3,
  50. CLOSE = 4
  51. };
  52. /// \brief Result of Upstream Fetch
  53. ///
  54. /// Note that this applies to the status of I/Os in the fetch - a fetch
  55. /// that resulted in a packet being received from the server is a SUCCESS,
  56. /// even if the contents of the packet indicate that some error occurred.
  57. enum Result {
  58. SUCCESS = 0, ///< Success, fetch completed
  59. TIME_OUT = 1, ///< Failure, fetch timed out
  60. STOPPED = 2, ///< Control code, fetch has been stopped
  61. NOTSET = 3 ///< For testing, indicates value not set
  62. };
  63. // The next enum is a "trick" to allow constants to be defined in a class
  64. // declaration.
  65. /// \brief Integer Constants
  66. enum {
  67. STAGING_LENGTH = 8192 ///< Size of staging buffer
  68. };
  69. /// \brief I/O Fetch Callback
  70. ///
  71. /// Class of callback object for when the fetch itself has completed - an
  72. /// object of this class is passed to the IOFetch constructor and its
  73. /// operator() method called when the fetch completes.
  74. ///
  75. /// Note the difference between the two operator() methods:
  76. /// - IOFetch::operator() callback is called when an asynchronous I/O has
  77. /// completed.
  78. /// - IOFetch::Callback::operator() is called when an upstream fetch - which
  79. /// may have involved several asynchronous I/O operations - has completed.
  80. ///
  81. /// This is an abstract class.
  82. class Callback {
  83. public:
  84. /// \brief Default Constructor
  85. Callback()
  86. {}
  87. /// \brief Virtual Destructor
  88. virtual ~Callback()
  89. {}
  90. /// \brief Callback method
  91. ///
  92. /// This is the method called when the fetch completes.
  93. ///
  94. /// \param result Result of the fetch
  95. virtual void operator()(Result result) = 0;
  96. };
  97. /// \brief Constructor.
  98. ///
  99. /// Creates the object that will handle the upstream fetch.
  100. ///
  101. /// TODO: Need to randomise the source port
  102. ///
  103. /// \param protocol Fetch protocol, either IOFetch::TCP or IOFetch::UDP
  104. /// \param service I/O Service object to handle the asynchronous
  105. /// operations.
  106. /// \param question DNS question to send to the upstream server.
  107. /// \param buff Output buffer into which the response (in wire format)
  108. /// is written (if a response is received).
  109. /// \param cb Callback object containing the callback to be called
  110. /// when we terminate. The caller is responsible for managing this
  111. /// object and deleting it if necessary.
  112. /// \param address IP address of upstream server
  113. /// \param port Port to which to connect on the upstream server
  114. /// (default = 53)
  115. /// \param wait Timeout for the fetch (in ms). The default value of
  116. /// -1 indicates no timeout.
  117. IOFetch(Protocol protocol, IOService& service,
  118. const isc::dns::Question& question, const IOAddress& address,
  119. uint16_t port, isc::util::OutputBufferPtr& buff, Callback* cb,
  120. int wait = -1);
  121. /// \brief Return Current Protocol
  122. ///
  123. /// \return Protocol associated with this IOFetch object.
  124. Protocol getProtocol() const;
  125. /// \brief Coroutine entry point
  126. ///
  127. /// The operator() method is the method in which the coroutine code enters
  128. /// this object when an operation has been completed.
  129. ///
  130. /// \param ec Error code, the result of the last asynchronous I/O operation.
  131. /// \param length Amount of data received on the last asynchronous read
  132. void operator()(asio::error_code ec = asio::error_code(), size_t length = 0);
  133. /// \brief Terminate query
  134. ///
  135. /// This method can be called at any point. It terminates the current
  136. /// query with the specified reason.
  137. ///
  138. /// \param reason Reason for terminating the query
  139. void stop(Result reason = STOPPED);
  140. private:
  141. /// \brief Log I/O Failure
  142. ///
  143. /// Records an I/O failure to the log file
  144. ///
  145. /// \param ec ASIO error code
  146. void logIOFailure(asio::error_code ec);
  147. // Member variables. All data is in a structure pointed to by a shared
  148. // pointer. The IOFetch object is copied a number of times during its
  149. // life, and only requiring a pointer to be copied reduces overhead.
  150. boost::shared_ptr<IOFetchData> data_; ///< Private data
  151. };
  152. } // namespace asiolink
  153. #endif // __IO_FETCH_H