udp_server.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2011 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 UDP_SERVER_H
  15. #define 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 <asiolink/simple_callback.h>
  20. #include <asiodns/dns_answer.h>
  21. #include <asiodns/dns_lookup.h>
  22. #include <asiodns/dns_server.h>
  23. #include <coroutine.h>
  24. namespace isc {
  25. namespace asiodns {
  26. //
  27. // Asynchronous UDP server coroutine
  28. //
  29. ///
  30. /// \brief This class implements the coroutine to handle UDP
  31. /// DNS query event. As such, it is both a \c DNSServer and
  32. /// a \c coroutine
  33. ///
  34. class UDPServer : public virtual DNSServer, public virtual coroutine {
  35. public:
  36. /// \brief Constructor
  37. /// \param io_service the asio::io_service to work with
  38. /// \param fd the file descriptor of opened UDP socket
  39. /// \param af address family, either AF_INET or AF_INET6
  40. /// \param checkin the callbackprovider for non-DNS events
  41. /// \param lookup the callbackprovider for DNS lookup events
  42. /// \param answer the callbackprovider for DNS answer events
  43. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
  44. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  45. /// fd is not a valid descriptor.
  46. UDPServer(asio::io_service& io_service, int fd, int af,
  47. isc::asiolink::SimpleCallback* checkin = NULL,
  48. DNSLookup* lookup = NULL, DNSAnswer* answer = NULL);
  49. /// \brief The function operator
  50. void operator()(asio::error_code ec = asio::error_code(),
  51. size_t length = 0);
  52. /// \brief Calls the lookup callback
  53. void asyncLookup();
  54. /// \brief Stop the running server
  55. /// \note once the server stopped, it can't restart
  56. void stop();
  57. /// \brief Resume operation
  58. ///
  59. /// \param done Set this to true if the lookup action is done and
  60. /// we have an answer
  61. void resume(const bool done);
  62. /// \brief Clones the object
  63. ///
  64. /// \return a newly allocated copy of this object
  65. DNSServer* clone() {
  66. UDPServer* s = new UDPServer(*this);
  67. return (s);
  68. }
  69. private:
  70. enum { MAX_LENGTH = 4096 };
  71. /**
  72. * \brief Internal state and data.
  73. *
  74. * We use the pimple design pattern, but not because we need to hide
  75. * internal data. This class and whole header is for private use anyway.
  76. * It turned out that UDPServer is copied a lot, because it is a coroutine.
  77. * This way the overhead of copying is lower, we copy only one shared
  78. * pointer instead of about 10 of them.
  79. */
  80. struct Data;
  81. boost::shared_ptr<Data> data_;
  82. };
  83. } // namespace asiodns
  84. } // namespace isc
  85. #endif // UDP_SERVER_H