1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <asiolink/asio_wrapper.h>
#include <tcp/tcp_listener.h>

using namespace isc::asiolink;
namespace ph = std::placeholders;

namespace isc {
namespace tcp {

TcpListener::TcpListener(const IOServicePtr& io_service,
                         const IOAddress& server_address,
                         const unsigned short server_port,
                         const TlsContextPtr& tls_context,
                         const IdleTimeout& idle_timeout,
                         const TcpConnectionFilterCallback& connection_filter)
    : io_service_(io_service), tls_context_(tls_context), acceptor_(),
      endpoint_(), connections_(), idle_timeout_(idle_timeout.value_),
      connection_filter_(connection_filter) {
    // Create the TCP or TLS acceptor.
    if (!tls_context) {
        acceptor_.reset(new TcpConnectionAcceptor(io_service));
    } else {
        acceptor_.reset(new TlsConnectionAcceptor(io_service));
    }

    // Try creating an endpoint. This may cause exceptions.
    try {
        endpoint_.reset(new TCPEndpoint(server_address, server_port));
    } catch (...) {
        isc_throw(TcpListenerError, "unable to create TCP endpoint for "
                  << server_address << ":" << server_port);
    }

    // Idle connection timeout is signed and must be greater than 0.
    if (idle_timeout_ <= 0) {
        isc_throw(TcpListenerError, "Invalid desired TCP idle connection"
                  " timeout " << idle_timeout_);
    }
}

TcpListener::~TcpListener() {
    stop();
}

const TCPEndpoint&
TcpListener::getEndpoint() const {
    return (*endpoint_);
}

void
TcpListener::start() {
    try {
        acceptor_->open(*endpoint_);
        acceptor_->setOption(TcpConnectionAcceptor::ReuseAddress(true));
        acceptor_->bind(*endpoint_);
        acceptor_->listen();

    } catch (const boost::system::system_error& ex) {
        stop();
        isc_throw(TcpListenerError, "unable to setup TCP acceptor for "
                  "listening for incoming TCP clients: " << ex.what());
    }

    accept();
}

void
TcpListener::stop() {
    connections_.stopAll();
    acceptor_->close();
}

void
TcpListener::accept() {
    TcpConnectionAcceptorCallback acceptor_callback =
        std::bind(&TcpListener::acceptHandler, this, ph::_1);

    TcpConnectionPtr conn = createConnection(acceptor_callback, connection_filter_);

    // Add this new connection to the pool.
    connections_.start(conn);
}

void
TcpListener::acceptHandler(const boost::system::error_code&) {
    // The new connection has arrived. Set the acceptor to continue
    // accepting new connections.
    accept();
}

TcpConnectionPtr
TcpListener::createConnection(const TcpConnectionAcceptorCallback&,
                              const TcpConnectionFilterCallback&) {
    isc_throw(NotImplemented, "TcpListener::createConnection:");
}

IOAddress
TcpListener::getLocalAddress() const {
    return (getEndpoint().getAddress());
}

uint16_t
TcpListener::getLocalPort() const {
    return (getEndpoint().getPort());
}

} // end of namespace isc::tcp
} // end of namespace isc