tcp_server.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 __TCP_SERVER_H
  15. #define __TCP_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 <boost/shared_array.hpp>
  20. #include <boost/shared_ptr.hpp>
  21. #include <asiolink/asiolink.h>
  22. #include <coroutine.h>
  23. #include "dns_server.h"
  24. #include "dns_lookup.h"
  25. #include "dns_answer.h"
  26. namespace isc {
  27. namespace asiodns {
  28. /// \brief A TCP-specific \c DNSServer object.
  29. ///
  30. /// This class inherits from both \c DNSServer and from \c coroutine,
  31. /// defined in coroutine.h.
  32. class TCPServer : public virtual DNSServer, public virtual coroutine {
  33. public:
  34. /// \brief Constructor
  35. /// \param io_service the asio::io_service to work with
  36. /// \param fd the file descriptor of opened TCP socket
  37. /// \param af address family of the socket, either AF_INET or AF_INET6
  38. /// \param checkin the callbackprovider for non-DNS events
  39. /// \param lookup the callbackprovider for DNS lookup events
  40. /// \param answer the callbackprovider for DNS answer events
  41. /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6
  42. /// \throw isc::asiolink::IOError when a low-level error happens, like the
  43. /// fd is not a valid descriptor or it can't be listened on.
  44. TCPServer(asio::io_service& io_service, int fd, int af,
  45. const isc::asiolink::SimpleCallback* checkin = NULL,
  46. const DNSLookup* lookup = NULL, const DNSAnswer* answer = NULL);
  47. void operator()(asio::error_code ec = asio::error_code(),
  48. size_t length = 0);
  49. void asyncLookup();
  50. void stop();
  51. void resume(const bool done);
  52. DNSServer* clone() {
  53. TCPServer* s = new TCPServer(*this);
  54. return (s);
  55. }
  56. /// \brief Set the read timeout
  57. ///
  58. /// If the client does not send (all) query data within this
  59. /// timeframe, the connection is dropped
  60. ///
  61. /// \param timeout in milliseconds
  62. virtual void setTCPRecvTimeout(size_t timeout) {
  63. *tcp_recv_timeout_ = timeout;
  64. }
  65. private:
  66. enum { MAX_LENGTH = 65535 };
  67. static const size_t TCP_MESSAGE_LENGTHSIZE = 2;
  68. // The ASIO service object
  69. asio::io_service& io_;
  70. // Class member variables which are dynamic, and changes to which
  71. // need to accessible from both sides of a coroutine fork or from
  72. // outside of the coroutine (i.e., from an asynchronous I/O call),
  73. // should be declared here as pointers and allocated in the
  74. // constructor or in the coroutine. This allows state information
  75. // to persist when an individual copy of the coroutine falls out
  76. // scope while waiting for an event, *so long as* there is another
  77. // object that is referencing the same data. As a side-benefit, using
  78. // pointers also reduces copy overhead for coroutine objects.
  79. //
  80. // Note: Currently these objects are allocated by "new" in the
  81. // constructor, or in the function operator while processing a query.
  82. // Repeated allocations from the heap for every incoming query is
  83. // clearly a performance issue; this must be optimized in the future.
  84. // The plan is to have a structure pre-allocate several "server state"
  85. // objects which can be pulled off a free list and placed on an in-use
  86. // list whenever a query comes in. This will serve the dual purpose
  87. // of improving performance and guaranteeing that state information
  88. // will *not* be destroyed when any one instance of the coroutine
  89. // falls out of scope while waiting for an event.
  90. //
  91. // An ASIO acceptor object to handle new connections. Created in
  92. // the constructor.
  93. boost::shared_ptr<asio::ip::tcp::acceptor> acceptor_;
  94. // Socket used to for listen for queries. Created in the
  95. // constructor and stored in a shared_ptr because socket objects
  96. // are not copyable.
  97. boost::shared_ptr<asio::ip::tcp::socket> socket_;
  98. // The buffer into which the response is written
  99. boost::shared_ptr<isc::util::OutputBuffer> respbuf_;
  100. // \c IOMessage and \c Message objects to be passed to the
  101. // DNS lookup and answer providers
  102. boost::shared_ptr<isc::asiolink::IOMessage> io_message_;
  103. isc::dns::MessagePtr query_message_;
  104. isc::dns::MessagePtr answer_message_;
  105. // The buffer into which the query packet is written
  106. boost::shared_array<char>data_;
  107. // State information that is entirely internal to a given instance
  108. // of the coroutine can be declared here.
  109. size_t bytes_;
  110. bool done_;
  111. // Callback functions provided by the caller
  112. const isc::asiolink::SimpleCallback* checkin_callback_;
  113. const DNSLookup* lookup_callback_;
  114. const DNSAnswer* answer_callback_;
  115. boost::shared_ptr<isc::asiolink::IOEndpoint> peer_;
  116. boost::shared_ptr<isc::asiolink::IOSocket> iosock_;
  117. // Timer used to timeout on tcp connections
  118. // This is a shared pointer because we need to have something
  119. // that outlives the operator() call and is copyable (for CORO_FORK)
  120. // even though it is only set after fork
  121. boost::shared_ptr<asio::deadline_timer> timeout_;
  122. // Timeout value to use in the timer;
  123. // this, too, is a pointer, so that it can be updated whithout restarting
  124. // the server
  125. boost::shared_ptr<size_t> tcp_recv_timeout_;
  126. };
  127. } // namespace asiodns
  128. } // namespace isc
  129. #endif // __TCP_SERVER_H