tcp_server.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. private:
  57. enum { MAX_LENGTH = 65535 };
  58. static const size_t TCP_MESSAGE_LENGTHSIZE = 2;
  59. // The ASIO service object
  60. asio::io_service& io_;
  61. // Class member variables which are dynamic, and changes to which
  62. // need to accessible from both sides of a coroutine fork or from
  63. // outside of the coroutine (i.e., from an asynchronous I/O call),
  64. // should be declared here as pointers and allocated in the
  65. // constructor or in the coroutine. This allows state information
  66. // to persist when an individual copy of the coroutine falls out
  67. // scope while waiting for an event, *so long as* there is another
  68. // object that is referencing the same data. As a side-benefit, using
  69. // pointers also reduces copy overhead for coroutine objects.
  70. //
  71. // Note: Currently these objects are allocated by "new" in the
  72. // constructor, or in the function operator while processing a query.
  73. // Repeated allocations from the heap for every incoming query is
  74. // clearly a performance issue; this must be optimized in the future.
  75. // The plan is to have a structure pre-allocate several "server state"
  76. // objects which can be pulled off a free list and placed on an in-use
  77. // list whenever a query comes in. This will serve the dual purpose
  78. // of improving performance and guaranteeing that state information
  79. // will *not* be destroyed when any one instance of the coroutine
  80. // falls out of scope while waiting for an event.
  81. //
  82. // An ASIO acceptor object to handle new connections. Created in
  83. // the constructor.
  84. boost::shared_ptr<asio::ip::tcp::acceptor> acceptor_;
  85. // Socket used to for listen for queries. Created in the
  86. // constructor and stored in a shared_ptr because socket objects
  87. // are not copyable.
  88. boost::shared_ptr<asio::ip::tcp::socket> socket_;
  89. // The buffer into which the response is written
  90. boost::shared_ptr<isc::util::OutputBuffer> respbuf_;
  91. // \c IOMessage and \c Message objects to be passed to the
  92. // DNS lookup and answer providers
  93. boost::shared_ptr<isc::asiolink::IOMessage> io_message_;
  94. isc::dns::MessagePtr query_message_;
  95. isc::dns::MessagePtr answer_message_;
  96. // The buffer into which the query packet is written
  97. boost::shared_array<char>data_;
  98. // State information that is entirely internal to a given instance
  99. // of the coroutine can be declared here.
  100. size_t bytes_;
  101. bool done_;
  102. // Callback functions provided by the caller
  103. const isc::asiolink::SimpleCallback* checkin_callback_;
  104. const DNSLookup* lookup_callback_;
  105. const DNSAnswer* answer_callback_;
  106. boost::shared_ptr<isc::asiolink::IOEndpoint> peer_;
  107. boost::shared_ptr<isc::asiolink::IOSocket> iosock_;
  108. };
  109. } // namespace asiodns
  110. } // namespace isc
  111. #endif // __TCP_SERVER_H