Kea  2.5.3
tcp_listener.h
Go to the documentation of this file.
1 // Copyright (C) 2022 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 #ifndef TCP_LISTENER_H
8 #define TCP_LISTENER_H
9 
10 #include <asiolink/io_service.h>
11 #include <asiolink/io_address.h>
12 #include <asiolink/tcp_endpoint.h>
14 #include <boost/scoped_ptr.hpp>
15 
16 namespace isc {
17 namespace tcp {
18 
20 class TcpListenerError : public Exception {
21 public:
22  TcpListenerError(const char* file, size_t line, const char* what) :
23  isc::Exception(file, line, what) { };
24 };
25 
28 class TcpListener {
29 public:
31  struct IdleTimeout {
35  explicit IdleTimeout(long value)
36  : value_(value) {
37  }
38  long value_;
39  };
40 
59  TcpListener(asiolink::IOService& io_service,
60  const asiolink::IOAddress& server_address,
61  const unsigned short server_port,
62  const asiolink::TlsContextPtr& tls_context,
63  const IdleTimeout& idle_timeout,
64  const TcpConnectionFilterCallback& connection_filter = 0);
65 
67  virtual ~TcpListener();
68 
70  const asiolink::TCPEndpoint& getEndpoint() const;
71 
81  void start();
82 
84  void stop();
85 
88 
90  uint16_t getLocalPort() const;
91 
93  long getIdleTimeout() const {
94  return (idle_timeout_);
95  }
96 
104  size_t usedByRemoteIp(const asiolink::IOAddress& remote_ip,
105  size_t& total_connections) {
106  return (connections_.usedByRemoteIp(remote_ip, total_connections));
107  }
108 
109 protected:
110 
115  void accept();
116 
123  void acceptHandler(const boost::system::error_code& ec);
124 
135  const TcpConnectionAcceptorCallback& acceptor_callback,
136  const TcpConnectionFilterCallback& connection_filter);
137 
140 
143 
146 
149  boost::scoped_ptr<asiolink::TCPEndpoint> endpoint_;
150 
153 
157 
161 };
162 
164 typedef boost::shared_ptr<TcpListener> TcpListenerPtr;
165 
166 } // end of namespace isc::asiolink
167 } // end of namespace isc
168 
169 #endif // TCP_LISTENER_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Pool of active TCP connections.
size_t usedByRemoteIp(const asiolink::IOAddress &remote_ip, size_t &total_connections)
Returns the number of connections using a given remote IP address.
A generic error raised by the TcpListener class.
Definition: tcp_listener.h:20
TcpListenerError(const char *file, size_t line, const char *what)
Definition: tcp_listener.h:22
Implements a class that listens for, accepts, and manages TCP connections.
Definition: tcp_listener.h:28
asiolink::TlsContextPtr tls_context_
TLS context.
Definition: tcp_listener.h:142
TcpConnectionPool connections_
Pool of active connections.
Definition: tcp_listener.h:152
asiolink::IOService & io_service_
Reference to the IO service.
Definition: tcp_listener.h:139
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
TcpListener(asiolink::IOService &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
long getIdleTimeout() const
Returns the idle timeout (in milliseconds).
Definition: tcp_listener.h:93
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
size_t usedByRemoteIp(const asiolink::IOAddress &remote_ip, size_t &total_connections)
Returns the number of connections using a given remote IP address.
Definition: tcp_listener.h:104
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
std::function< bool(const boost::asio::ip::tcp::endpoint &)> TcpConnectionFilterCallback
Type of the callback for filtering new connections by ip address.
boost::shared_ptr< TcpConnectionAcceptor > TcpConnectionAcceptorPtr
Type of shared pointer to TCP acceptors.
boost::shared_ptr< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
boost::shared_ptr< TcpListener > TcpListenerPtr
Pointer to a TcpListener.
Definition: tcp_listener.h:164
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
long value_
Connection idle timeout value specified.
Definition: tcp_listener.h:38
IdleTimeout(long value)
Constructor.
Definition: tcp_listener.h:35