Kea 2.6.0
tcp_listener.cc
Go to the documentation of this file.
1// Copyright (C) 2022-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
9#include <tcp/tcp_listener.h>
10
11using namespace isc::asiolink;
12namespace ph = std::placeholders;
13
14namespace isc {
15namespace tcp {
16
18 const IOAddress& server_address,
19 const unsigned short server_port,
20 const TlsContextPtr& tls_context,
21 const IdleTimeout& idle_timeout,
22 const TcpConnectionFilterCallback& connection_filter)
23 : io_service_(io_service), tls_context_(tls_context), acceptor_(),
24 endpoint_(), connections_(), idle_timeout_(idle_timeout.value_),
25 connection_filter_(connection_filter) {
26 // Create the TCP or TLS acceptor.
27 if (!tls_context) {
28 acceptor_.reset(new TcpConnectionAcceptor(io_service));
29 } else {
30 acceptor_.reset(new TlsConnectionAcceptor(io_service));
31 }
32
33 // Try creating an endpoint. This may cause exceptions.
34 try {
35 endpoint_.reset(new TCPEndpoint(server_address, server_port));
36 } catch (...) {
37 isc_throw(TcpListenerError, "unable to create TCP endpoint for "
38 << server_address << ":" << server_port);
39 }
40
41 // Idle connection timeout is signed and must be greater than 0.
42 if (idle_timeout_ <= 0) {
43 isc_throw(TcpListenerError, "Invalid desired TCP idle connection"
44 " timeout " << idle_timeout_);
45 }
46}
47
49 stop();
50}
51
52const TCPEndpoint&
54 return (*endpoint_);
55}
56
57void
59 try {
60 acceptor_->open(*endpoint_);
62 acceptor_->bind(*endpoint_);
63 acceptor_->listen();
64
65 } catch (const boost::system::system_error& ex) {
66 stop();
67 isc_throw(TcpListenerError, "unable to setup TCP acceptor for "
68 "listening for incoming TCP clients: " << ex.what());
69 }
70
71 accept();
72}
73
74void
77 acceptor_->close();
78}
79
80void
82 TcpConnectionAcceptorCallback acceptor_callback =
83 std::bind(&TcpListener::acceptHandler, this, ph::_1);
84
85 TcpConnectionPtr conn = createConnection(acceptor_callback, connection_filter_);
86
87 // Add this new connection to the pool.
88 connections_.start(conn);
89}
90
91void
92TcpListener::acceptHandler(const boost::system::error_code&) {
93 // The new connection has arrived. Set the acceptor to continue
94 // accepting new connections.
95 accept();
96}
97
101 isc_throw(NotImplemented, "TcpListener::createConnection:");
102}
103
106 return (getEndpoint().getAddress());
107}
108
109uint16_t
111 return (getEndpoint().getPort());
112}
113
114} // end of namespace isc::tcp
115} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when a function is not implemented.
void stopAll()
Stops all connections and removes them from the pool.
void start(const TcpConnectionPtr &connection)
Start new connection.
A generic error raised by the TcpListener class.
Definition: tcp_listener.h:20
TcpListener(const asiolink::IOServicePtr &io_service, const asiolink::IOAddress &server_address, const unsigned short server_port, const asiolink::TlsContextPtr &tls_context, const IdleTimeout &idle_timeout, const TcpConnectionFilterCallback &connection_filter=0)
Constructor.
Definition: tcp_listener.cc:17
TcpConnectionPool connections_
Pool of active connections.
Definition: tcp_listener.h:152
void start()
Starts accepting new connections.
Definition: tcp_listener.cc:58
const asiolink::TCPEndpoint & getEndpoint() const
Returns reference to the current listener endpoint.
Definition: tcp_listener.cc:53
long idle_timeout_
Timeout after which idle connection is closed by the server.
Definition: tcp_listener.h:156
TcpConnectionAcceptorPtr acceptor_
Acceptor instance.
Definition: tcp_listener.h:145
uint16_t getLocalPort() const
Returns local port on which server is listening.
virtual ~TcpListener()
Virtual destructor.
Definition: tcp_listener.cc:48
asiolink::IOAddress getLocalAddress() const
Returns local address on which server is listening.
void accept()
Creates TcpConnection instance and adds it to the pool of active connections.
Definition: tcp_listener.cc:81
virtual TcpConnectionPtr createConnection(const TcpConnectionAcceptorCallback &acceptor_callback, const TcpConnectionFilterCallback &connection_filter)
Creates an instance of the TcpConnection.
Definition: tcp_listener.cc:99
boost::scoped_ptr< asiolink::TCPEndpoint > endpoint_
Pointer to the endpoint representing IP address and port on which the service is running.
Definition: tcp_listener.h:149
void acceptHandler(const boost::system::error_code &ec)
Callback invoked when the new connection is accepted.
Definition: tcp_listener.cc:92
TcpConnectionFilterCallback connection_filter_
Callback invoked during acceptance which may reject connections.
Definition: tcp_listener.h:160
void stop()
Stops all active connections and shuts down the service.
Definition: tcp_listener.cc:75
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::function< bool(const boost::asio::ip::tcp::endpoint &)> TcpConnectionFilterCallback
Type of the callback for filtering new connections by ip address.
asiolink::TLSAcceptor< TcpConnectionAcceptorCallback > TlsConnectionAcceptor
Type of the TLS acceptor used in this library.
boost::shared_ptr< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
asiolink::TCPAcceptor< TcpConnectionAcceptorCallback > TcpConnectionAcceptor
Type of the TCP acceptor used in this library.
std::function< void(const boost::system::error_code &)> TcpConnectionAcceptorCallback
Type of the callback for the TCP acceptor used in this library.
Defines the logger used by the top-level component of kea-lfc.
Idle connection timeout.
Definition: tcp_listener.h:31