Kea 2.7.5
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
76
77void
79 // First case: from no config to no config.
80 if (!config && http_listeners_.empty()) {
81 return;
82 }
83
84 // Second case: from config to no config.
85 if (!config && !http_listeners_.empty()) {
86 close(false);
87 return;
88 }
89
90 // Third case: no address or port change.
91 if (config && current_config_ &&
92 (config->getSocketAddress() == current_config_->getSocketAddress()) &&
93 (config->getSocketPort() == current_config_->getSocketPort())) {
94 // Check if TLS setup changed.
95 if ((config->getTrustAnchor() != current_config_->getTrustAnchor()) ||
96 (config->getCertFile() != current_config_->getCertFile()) ||
97 (config->getKeyFile() != current_config_->getKeyFile()) ||
98 (config->getCertRequired() != current_config_->getCertRequired())) {
100 // Overwrite the authentication setup and the emulation flag
101 // in the response creator config.
102 current_config_->setAuthConfig(config->getAuthConfig());
103 current_config_->setEmulateAgentResponse(config->getEmulateAgentResponse());
104 } else {
105 current_config_ = config;
106 }
107 return;
108 }
109
110 // Last case: from no config, or address or port change.
111 current_config_ = config;
112 IOAddress server_address = config->getSocketAddress();
113 uint16_t server_port = config->getSocketPort();
114 bool use_https = false;
115 TlsContextPtr tls_context;
116 if (!config->getCertFile().empty()) {
117 TlsContext::configure(tls_context,
118 TlsRole::SERVER,
119 config->getTrustAnchor(),
120 config->getCertFile(),
121 config->getKeyFile(),
122 config->getCertRequired());
123 use_https = true;
124 }
125
126 // Create response creator factory first. It will be used to
127 // generate response creators. Each response creator will be used
128 // to generate answer to specific request.
130
131 // Create HTTP listener. It will open up a TCP socket and be
132 // prepared to accept incoming connection.
133 HttpListenerPtr http_listener
135 server_address,
136 server_port,
137 tls_context,
138 rfc,
141
142 // Pass the use external socket flag.
143 http_listener->addExternalSockets(use_external_);
144
145 // Instruct the HTTP listener to actually open socket, install
146 // callback and start listening.
147 http_listener->start();
148
149 // The new listener is running so add it to the collection of
150 // active listeners. The next step will be to remove all other
151 // active listeners, but we do it inside the main process loop.
152 http_listeners_.push_back(http_listener);
153 active_ = 1;
154
155 // Ok, seems we're good to go.
157 .arg(use_https ? "HTTPS" : "HTTP")
158 .arg(server_address.toText())
159 .arg(server_port);
160}
161
162void
164 bool use_https = false;
165 ostringstream ep;
166 if (current_config_) {
167 use_https = !current_config_->getCertFile().empty();
168 ep << "bound to address " << current_config_->getSocketAddress()
169 << " port " << current_config_->getSocketPort();
170 }
172 .arg(use_https ? "HTTPS" : "HTTP")
173 .arg(ep.str());
174 current_config_.reset();
175 active_ = 0;
176 if (remove) {
178 }
179}
180
181void
183 // We expect only one active listener. If there are more (most likely 2),
184 // it means we have just reconfigured the server and need to shut down all
185 // listeners except the most recently added.
186 if (http_listeners_.size() > active_) {
187 // Stop no longer used listeners.
188 for (auto l = http_listeners_.begin();
189 l != http_listeners_.end() - active_;
190 ++l) {
191 (*l)->stop();
192 }
193 // We have stopped listeners but there may be some pending handlers
194 // related to these listeners. Need to invoke these handlers.
195 try {
196 io_service_->poll();
197 } catch (...) {
198 }
199 // Finally, we're ready to remove no longer used listeners.
200 http_listeners_.erase(http_listeners_.begin(),
201 http_listeners_.end() - active_);
202 }
203}
204
207 // Return the most recent listener or null.
208 return (http_listeners_.empty() ? ConstHttpListenerPtr() :
209 http_listeners_.back());
210}
211
214 static HttpCommandMgr http_cmd_mgr;
215 return (http_cmd_mgr);
216}
217
218HttpCommandMgr::HttpCommandMgr() : impl_(new HttpCommandMgrImpl()) {
219}
220
221void
223 impl_->io_service_ = io_service;
224}
225
226void
228 impl_->timeout_ = timeout;
229}
230
231void
233 impl_->idle_timeout_ = timeout;
234}
235
236void
238 impl_->use_external_ = use_external;
239}
240
241void
243 impl_->configure(config);
244}
245
246void
248 impl_->close(remove);
249}
250
251void
253 impl_->garbageCollectListeners();
254}
255
258 return (impl_->getHttpListener());
259}
260
261} // end of isc::config
262} // end of isc
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 http command manager.
void close(bool remove=true)
Close http control socket.
void configure(HttpCommandConfigPtr config)
Configure http 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