123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #include <config.h>
- #include <util/buffer.h>
- #include <asio.hpp>
- #include <asiolink/dummy_io_cb.h>
- #include <asiolink/tcp_endpoint.h>
- #include <asiolink/tcp_socket.h>
- #include <asiodns/tcp_server.h>
- #include <asiodns/logger.h>
- #include <boost/shared_array.hpp>
- #include <cassert>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <errno.h>
- using asio::io_service;
- using asio::buffer;
- using asio::const_buffer;
- using asio::ip::tcp;
- using namespace std;
- using namespace isc::dns;
- using namespace isc::util;
- using namespace isc::asiolink;
- namespace isc {
- namespace asiodns {
- TCPServer::TCPServer(io_service& io_service, int fd, int af,
- const DNSLookup* lookup,
- const DNSAnswer* answer) :
- io_(io_service), done_(false),
- lookup_callback_(lookup),
- answer_callback_(answer)
- {
- if (af != AF_INET && af != AF_INET6) {
- isc_throw(InvalidParameter, "Address family must be either AF_INET "
- "or AF_INET6, not " << af);
- }
- LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_FD_ADD_TCP).arg(fd);
- try {
- acceptor_.reset(new tcp::acceptor(io_service));
- acceptor_->assign(af == AF_INET6 ? tcp::v6() : tcp::v4(), fd);
- acceptor_->listen();
- } catch (const std::exception& exception) {
-
-
- isc_throw(IOError, exception.what());
- }
-
-
- tcp_recv_timeout_.reset(new size_t(5000));
- }
- namespace {
- void doTimeOut(boost::shared_ptr<asio::ip::tcp::socket> socket,
- const asio::error_code& error)
- {
- if (error != asio::error::operation_aborted) {
- socket->cancel();
- }
- }
- }
- void
- TCPServer::operator()(asio::error_code ec, size_t length) {
-
-
-
- boost::array<const_buffer,2> bufs;
- OutputBuffer lenbuf(TCP_MESSAGE_LENGTHSIZE);
- CORO_REENTER (this) {
- do {
-
- socket_.reset(new tcp::socket(acceptor_->get_io_service()));
-
-
- do {
- CORO_YIELD acceptor_->async_accept(*socket_, *this);
- if (ec) {
- using namespace asio::error;
- const asio::error_code::value_type err_val = ec.value();
-
-
-
-
-
- if (err_val == operation_aborted ||
- err_val == bad_descriptor) {
- return;
- }
-
-
-
-
-
- if (err_val != would_block && err_val != try_again &&
- err_val != connection_aborted &&
- err_val != interrupted) {
- LOG_ERROR(logger, ASIODNS_TCP_ACCEPT_FAIL).
- arg(ec.message());
- }
- }
- } while (ec);
-
-
-
-
- CORO_FORK io_.post(TCPServer(*this));
- } while (is_parent());
-
-
-
-
-
- data_.reset(new char[MAX_LENGTH]);
-
-
-
-
- if (*tcp_recv_timeout_ > 0) {
- timeout_.reset(new asio::deadline_timer(io_));
- timeout_->expires_from_now(
- boost::posix_time::milliseconds(*tcp_recv_timeout_));
- timeout_->async_wait(boost::bind(&doTimeOut, socket_,
- asio::placeholders::error));
- }
-
- CORO_YIELD async_read(*socket_, asio::buffer(data_.get(),
- TCP_MESSAGE_LENGTHSIZE), *this);
- if (ec) {
- LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_TCP_READLEN_FAIL).
- arg(ec.message());
- return;
- }
-
-
- CORO_YIELD {
- InputBuffer dnsbuffer(data_.get(), length);
- const uint16_t msglen = dnsbuffer.readUint16();
- async_read(*socket_, asio::buffer(data_.get(), msglen), *this);
- }
- if (ec) {
- LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_TCP_READDATA_FAIL).
- arg(ec.message());
- return;
- }
-
-
-
-
-
- peer_.reset(new TCPEndpoint(socket_->remote_endpoint(ec)));
- if (ec) {
- LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_TCP_GETREMOTE_FAIL).
- arg(ec.message());
- return;
- }
-
-
-
-
-
-
- iosock_.reset(new TCPSocket<DummyIOCallback>(*socket_));
- io_message_.reset(new IOMessage(data_.get(), length, *iosock_,
- *peer_));
-
-
- if (lookup_callback_ == NULL) {
- return;
- }
-
-
- respbuf_.reset(new OutputBuffer(0));
- query_message_.reset(new Message(Message::PARSE));
- answer_message_.reset(new Message(Message::RENDER));
-
-
-
-
-
- CORO_YIELD io_.post(AsyncLookup<TCPServer>(*this));
- assert(!ec);
-
-
- if (!done_) {
-
-
-
-
-
- socket_->close(ec);
- if (ec) {
- LOG_DEBUG(logger, 0, ASIODNS_TCP_CLOSE_FAIL).arg(ec.message());
- }
- return;
- }
-
-
- (*answer_callback_)(*io_message_, query_message_, answer_message_,
- respbuf_);
-
- lenbuf.writeUint16(respbuf_->getLength());
- bufs[0] = buffer(lenbuf.getData(), lenbuf.getLength());
- bufs[1] = buffer(respbuf_->getData(), respbuf_->getLength());
-
-
-
-
- CORO_YIELD async_write(*socket_, bufs, *this);
- if (ec) {
- LOG_DEBUG(logger, DBGLVL_TRACE_BASIC, ASIODNS_TCP_WRITE_FAIL).
- arg(ec.message());
- }
-
- timeout_->cancel();
-
-
- socket_->close(ec);
- if (ec) {
-
-
- LOG_DEBUG(logger, 0, ASIODNS_TCP_CLOSE_FAIL).arg(ec.message());
- }
- }
- }
- void
- TCPServer::asyncLookup() {
- (*lookup_callback_)(*io_message_, query_message_,
- answer_message_, respbuf_, this);
- }
- void TCPServer::stop() {
- asio::error_code ec;
-
-
- acceptor_->close(ec);
- if (ec) {
- LOG_ERROR(logger, ASIODNS_TCP_CLOSE_ACCEPTOR_FAIL).arg(ec.message());
- }
-
-
- if (socket_) {
- socket_->close(ec);
- if (ec) {
- LOG_ERROR(logger, ASIODNS_TCP_CLEANUP_CLOSE_FAIL).arg(ec.message());
- }
- }
- }
- void
- TCPServer::resume(const bool done) {
- done_ = done;
-
-
-
- io_.post(*this);
- }
- }
- }
|