tcp_server.h 5.5 KB

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