sync_udp_server.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2012 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 __SYNC_UDP_SERVER_H
  15. #define __SYNC_UDP_SERVER_H 1
  16. #ifndef ASIO_HPP
  17. #error "asio.hpp must be included before including this, see asiolink.h as to why"
  18. #endif
  19. #include "dns_answer.h"
  20. #include "dns_lookup.h"
  21. #include "dns_server.h"
  22. #include <dns/message.h>
  23. #include <asiolink/simple_callback.h>
  24. #include <util/buffer.h>
  25. #include <exceptions/exceptions.h>
  26. #include <boost/noncopyable.hpp>
  27. #include <stdint.h>
  28. namespace isc {
  29. namespace asiodns {
  30. /// \brief An UDP server that doesn't asynchronous lookup handlers.
  31. ///
  32. /// That means, the lookup handler must provide the answer right away.
  33. /// This allows for implementation with less overhead, compared with
  34. /// the UDPClass.
  35. class SyncUDPServer : public DNSServer, public boost::noncopyable {
  36. public:
  37. /// \brief Constructor
  38. /// \param io_service the asio::io_service to work with
  39. /// \param fd the file descriptor of opened UDP socket
  40. /// \param af address family, either AF_INET or AF_INET6
  41. /// \param checkin the callbackprovider for non-DNS events
  42. /// \param lookup the callbackprovider for DNS lookup events
  43. /// \param answer the callbackprovider for DNS answer events
  44. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
  45. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  46. /// fd is not a valid descriptor.
  47. SyncUDPServer(asio::io_service& io_service, const int fd, const int af,
  48. isc::asiolink::SimpleCallback* checkin = NULL,
  49. DNSLookup* lookup = NULL, DNSAnswer* answer = NULL);
  50. /// \brief Start the SyncUDPServer.
  51. ///
  52. /// This is the function operator to keep interface with other server
  53. /// classes. They need that because they're coroutines.
  54. virtual void operator()(asio::error_code ec = asio::error_code(),
  55. size_t length = 0);
  56. /// \brief Calls the lookup callback
  57. virtual void asyncLookup() {
  58. isc_throw(Unexpected,
  59. "SyncUDPServer doesn't support asyncLookup by design, use "
  60. "UDPServer if you need it.");
  61. }
  62. /// \brief Stop the running server
  63. /// \note once the server stopped, it can't restart
  64. virtual void stop();
  65. /// \brief Resume operation
  66. ///
  67. /// Note that unlike other servers, this one expects it to be called
  68. /// directly from the lookup callback. If it isn't, the server will
  69. /// throw an Unexpected exception (probably to the event loop, which
  70. /// would usually lead to termination of the program, but that's OK,
  71. /// as it would be serious programmer error).
  72. ///
  73. /// \param done Set this to true if the lookup action is done and
  74. /// we have an answer
  75. virtual void resume(const bool done);
  76. /// \brief Check if we have an answer
  77. ///
  78. /// \return true if we have an answer
  79. virtual bool hasAnswer();
  80. /// \brief Clones the object
  81. ///
  82. /// Since cloning is for the use of coroutines, the synchronous UDP server
  83. /// does not need to be cloned. Therefore supporting it would be needless
  84. /// work, and trying to clone it would be a programmer error anyway, this
  85. /// throws Unexpected.
  86. ///
  87. /// \return a newly allocated copy of this object
  88. virtual DNSServer* clone() {
  89. isc_throw(Unexpected, "SyncUDPServer can't be cloned.");
  90. }
  91. private:
  92. // Internal state & buffers. We don't use the PIMPL idiom, as this class
  93. // isn't usually used directly anyway.
  94. // Maximum size of incoming UDP packet
  95. static const size_t MAX_LENGTH = 4096;
  96. // Buffer for incoming data
  97. uint8_t data_[MAX_LENGTH];
  98. // The buffer to render the output to and send it.
  99. // If it was OK to have just a buffer, not the wrapper class,
  100. // we could reuse the data_
  101. isc::util::OutputBufferPtr output_buffer_;
  102. // Objects to hold the query message and the answer
  103. isc::dns::MessagePtr query_, answer_;
  104. // The socket used for the communication
  105. std::auto_ptr<asio::ip::udp::socket> socket_;
  106. // The event loop we use
  107. asio::io_service& io_;
  108. // Place the socket puts the sender of a packet when it is received
  109. asio::ip::udp::endpoint sender_;
  110. // Callbacks
  111. const asiolink::SimpleCallback* checkin_callback_;
  112. const DNSLookup* lookup_callback_;
  113. const DNSAnswer* answer_callback_;
  114. // Answers from the lookup callback (not sent directly, but signalled
  115. // through resume()
  116. bool resume_called_, done_;
  117. // This turns true when the server stops. Allows for not sending the
  118. // answer after we closed the socket.
  119. bool stopped_;
  120. // Auxiliary functions
  121. // Schedule next read on the socket. Just a wrapper around
  122. // socket_->async_read_from with the correct parameters.
  123. void scheduleRead();
  124. // Callback from the socket's read call (called when there's an error or
  125. // when a new packet comes).
  126. void handleRead(const asio::error_code& ec, const size_t length);
  127. };
  128. } // namespace asiodns
  129. } // namespace isc
  130. #endif // __SYNC_UDP_SERVER_H