Kea 2.7.3
http_command_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 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>
8
10#include <config/config_log.h>
14#include <config/timeouts.h>
15#include <sstream>
16#include <vector>
17
18using namespace isc;
19using namespace isc::asiolink;
20using namespace isc::config;
21using namespace isc::http;
22using namespace std;
23
24namespace isc {
25namespace config {
26
74
75void
77 // First case: from no config to no config.
78 if (!config && http_listeners_.empty()) {
79 return;
80 }
81
82 // Second case: from config to no config.
83 if (!config && !http_listeners_.empty()) {
84 close(false);
85 return;
86 }
87
88 // Third case: no address or port change.
89 if (config && current_config_ &&
90 (config->getSocketAddress() == current_config_->getSocketAddress()) &&
91 (config->getSocketPort() == current_config_->getSocketPort())) {
92 // Check if TLS setup changed.
93 if ((config->getTrustAnchor() != current_config_->getTrustAnchor()) ||
94 (config->getCertFile() != current_config_->getCertFile()) ||
95 (config->getKeyFile() != current_config_->getKeyFile()) ||
96 (config->getCertRequired() != current_config_->getCertRequired())) {
98 // Overwrite the authentication setup and the emulation flag
99 // in the response creator config.
100 current_config_->setAuthConfig(config->getAuthConfig());
101 current_config_->setEmulateAgentResponse(config->getEmulateAgentResponse());
102 } else {
103 current_config_ = config;
104 }
105 return;
106 }
107
108 // Last case: from no config, or address or port change.
109 current_config_ = config;
110 IOAddress server_address = config->getSocketAddress();
111 uint16_t server_port = config->getSocketPort();
112 bool use_https = false;
113 TlsContextPtr tls_context;
114 if (!config->getCertFile().empty()) {
115 TlsContext::configure(tls_context,
116 TlsRole::SERVER,
117 config->getTrustAnchor(),
118 config->getCertFile(),
119 config->getKeyFile(),
120 config->getCertRequired());
121 use_https = true;
122 }
123
124 // Create response creator factory first. It will be used to
125 // generate response creators. Each response creator will be used
126 // to generate answer to specific request.
128
129 // Create HTTP listener. It will open up a TCP socket and be
130 // prepared to accept incoming connection.
131 HttpListenerPtr http_listener
133 server_address,
134 server_port,
135 tls_context,
136 rfc,
139
140 // Pass the use external socket flag.
141 http_listener->addExternalSockets(use_external_);
142
143 // Instruct the HTTP listener to actually open socket, install
144 // callback and start listening.
145 http_listener->start();
146
147 // The new listener is running so add it to the collection of
148 // active listeners. The next step will be to remove all other
149 // active listeners, but we do it inside the main process loop.
150 http_listeners_.push_back(http_listener);
151 active_ = 1;
152
153 // Ok, seems we're good to go.
155 .arg(use_https ? "HTTPS" : "HTTP")
156 .arg(server_address.toText())
157 .arg(server_port);
158}
159
160void
162 bool use_https = false;
163 ostringstream ep;
164 if (current_config_) {
165 use_https = !current_config_->getCertFile().empty();
166 ep << " bound to address " << current_config_->getSocketAddress()
167 << " port " << current_config_->getSocketPort();
168 }
170 .arg(use_https ? "HTTPS" : "HTTP")
171 .arg(ep.str());
172 current_config_.reset();
173 active_ = 0;
174 if (remove) {
176 }
177}
178
179void
181 // We expect only one active listener. If there are more (most likely 2),
182 // it means we have just reconfigured the server and need to shut down all
183 // listeners except the most recently added.
184 if (http_listeners_.size() > active_) {
185 // Stop no longer used listeners.
186 for (auto l = http_listeners_.begin();
187 l != http_listeners_.end() - active_;
188 ++l) {
189 (*l)->stop();
190 }
191 // We have stopped listeners but there may be some pending handlers
192 // related to these listeners. Need to invoke these handlers.
193 try {
194 io_service_->poll();
195 } catch (...) {
196 }
197 // Finally, we're ready to remove no longer used listeners.
198 http_listeners_.erase(http_listeners_.begin(),
199 http_listeners_.end() - active_);
200 }
201}
202
205 // Return the most recent listener or null.
206 return (http_listeners_.empty() ? ConstHttpListenerPtr() :
207 http_listeners_.back());
208}
209
212 static HttpCommandMgr http_cmd_mgr;
213 return (http_cmd_mgr);
214}
215
216HttpCommandMgr::HttpCommandMgr()
217 : HookedCommandMgr(), impl_(new HttpCommandMgrImpl()) {
218}
219
220void
222 impl_->io_service_ = io_service;
223}
224
225void
227 impl_->timeout_ = timeout;
228}
229
230void
232 impl_->idle_timeout_ = timeout;
233}
234
235void
237 impl_->use_external_ = use_external;
238}
239
240void
242 impl_->configure(config);
243}
244
245void
247 impl_->close(remove);
248}
249
250void
252 impl_->garbageCollectListeners();
253}
254
257 return (impl_->getHttpListener());
258}
259
260} // end of isc::config
261} // end of isc
Command Manager which can delegate commands to a hook library.
Implementation of the HttpCommandMgr.
vector< HttpListenerPtr > http_listeners_
Active listeners.
bool use_external_
Use external sockets flag.
HttpCommandConfigPtr current_config_
Current config.
void close(bool remove)
Close control socket.
void configure(HttpCommandConfigPtr config)
Configure control socket from configuration.
ConstHttpListenerPtr getHttpListener() const
Returns a const pointer to the HTTP listener.
size_t active_
Number of active listeners (0 or 1).
void garbageCollectListeners()
Removes listeners which are no longer in use.
long idle_timeout_
Idle connection timeout.
IOServicePtr io_service_
Pointer to the IO service.
long timeout_
Connection timeout.
HTTP Commands Manager implementation for the Kea servers.
void setIdleConnectionTimeout(const long timeout)
Override default idle connection timeout.
isc::http::ConstHttpListenerPtr getHttpListener() const
Returns a const pointer to the HTTP listener.
void setConnectionTimeout(const long timeout)
Override default connection timeout.
void addExternalSockets(bool use_external=true)
Use external sockets flag.
static HttpCommandMgr & instance()
HttpCommandMgr is a singleton class.
void garbageCollectListeners()
Removes listeners which are no longer in use.
void setIOService(const asiolink::IOServicePtr &io_service)
Sets IO service to be used by the command manager.
void close(bool remove=true)
Close control socket.
void configure(HttpCommandConfigPtr config)
Configure control socket from configuration.
HTTP response creator factory for HTTP control socket.
HTTP listener.
Definition listener.h:52
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
constexpr long TIMEOUT_AGENT_IDLE_CONNECTION_TIMEOUT
Timeout for the idle connection to be closed.
Definition timeouts.h:24
const isc::log::MessageID HTTP_COMMAND_MGR_IGNORED_TLS_SETUP_CHANGES
const isc::log::MessageID HTTP_COMMAND_MGR_SERVICE_STARTED
const isc::log::MessageID HTTP_COMMAND_MGR_SERVICE_STOPPING
boost::shared_ptr< HttpCommandConfig > HttpCommandConfigPtr
Pointer to a HttpCommandConfig object.
isc::log::Logger command_logger("commands")
Command processing Logger.
Definition config_log.h:21
constexpr long TIMEOUT_AGENT_RECEIVE_COMMAND
Timeout for the Control Agent to receive command over the RESTful interface.
Definition timeouts.h:21
boost::shared_ptr< const HttpListener > ConstHttpListenerPtr
Pointer to the const HttpListener.
Definition listener.h:153
boost::shared_ptr< HttpListener > HttpListenerPtr
Pointer to the HttpListener.
Definition listener.h:150
boost::shared_ptr< HttpResponseCreatorFactory > HttpResponseCreatorFactoryPtr
Pointer to the HttpResponseCreatorFactory.
Defines the logger used by the top-level component of kea-lfc.
Idle connection timeout.
Definition listener.h:67
HTTP request timeout value.
Definition listener.h:56