udp_server.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 addr the IP address to listen for queries on
  39. /// \param port the port to listen for queries on
  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. explicit UDPServer(asio::io_service& io_service,
  44. const asio::ip::address& addr, const uint16_t port,
  45. isc::asiolink::SimpleCallback* checkin = NULL,
  46. DNSLookup* lookup = NULL,
  47. DNSAnswer* answer = NULL);
  48. /// \brief Constructor
  49. /// \param io_service the asio::io_service to work with
  50. /// \param fd the file descriptor of opened UDP socket
  51. /// \param af address family, either AF_INET or AF_INET6
  52. /// \param checkin the callbackprovider for non-DNS events
  53. /// \param lookup the callbackprovider for DNS lookup events
  54. /// \param answer the callbackprovider for DNS answer events
  55. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
  56. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  57. /// fd is not a valid descriptor.
  58. UDPServer(asio::io_service& io_service, int fd, int af,
  59. isc::asiolink::SimpleCallback* checkin = NULL,
  60. DNSLookup* lookup = NULL, DNSAnswer* answer = NULL);
  61. /// \brief The function operator
  62. void operator()(asio::error_code ec = asio::error_code(),
  63. size_t length = 0);
  64. /// \brief Calls the lookup callback
  65. void asyncLookup();
  66. /// \brief Stop the running server
  67. /// \note once the server stopped, it can't restart
  68. void stop();
  69. /// \brief Resume operation
  70. ///
  71. /// \param done Set this to true if the lookup action is done and
  72. /// we have an answer
  73. void resume(const bool done);
  74. /// \brief Clones the object
  75. ///
  76. /// \return a newly allocated copy of this object
  77. DNSServer* clone() {
  78. UDPServer* s = new UDPServer(*this);
  79. return (s);
  80. }
  81. private:
  82. enum { MAX_LENGTH = 4096 };
  83. /**
  84. * \brief Internal state and data.
  85. *
  86. * We use the pimple design pattern, but not because we need to hide
  87. * internal data. This class and whole header is for private use anyway.
  88. * It turned out that UDPServer is copied a lot, because it is a coroutine.
  89. * This way the overhead of copying is lower, we copy only one shared
  90. * pointer instead of about 10 of them.
  91. */
  92. struct Data;
  93. boost::shared_ptr<Data> data_;
  94. };
  95. } // namespace asiodns
  96. } // namespace isc
  97. #endif // __UDP_SERVER_H