Kea: http_control_socket.cc Source File

| Kea 3.2.0-git

http_control_socket.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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
9
10#include <config.h>
11
12#include <asiolink/asio_wrapper.h>
13#include <asiolink/io_service.h>
14#include <asiolink/tls_socket.h>
15#include <cc/command_interpreter.h>
16#include <config/timeouts.h>
17#include <http/client.h>
18#include <http/post_request_json.h>
19#include <http/response_json.h>
20#include <netconf/http_control_socket.h>
21
22using namespace std;
23using namespace isc::asiolink;
24using namespace isc::config;
25using namespace isc::data;
26using namespace isc::http;
27
28namespace isc {
29namespace netconf {
30
31 template <> ControlSocketBasePtr
32 createControlSocket<CfgControlSocket::Type::HTTP>(CfgControlSocketPtr ctrl_sock) {
33 return (HttpControlSocketPtr(new HttpControlSocket(ctrl_sock)));
34}
35
36HttpControlSocket::HttpControlSocket(CfgControlSocketPtr ctrl_sock)
37 : ControlSocketBase(ctrl_sock) {
38}
39
40 ConstElementPtr
41HttpControlSocket::configGet(const string& service) {
42 return (sendCommand(createCommand("config-get", service)));
43}
44
45 ConstElementPtr
46HttpControlSocket::configTest(ElementPtr config, const string& service) {
47 return (sendCommand(createCommand("config-test", config, service)));
48}
49
50 ConstElementPtr
51HttpControlSocket::configSet(ElementPtr config, const string& service) {
52 return (sendCommand(createCommand("config-set", config, service)));
53}
54
55 ConstElementPtr
56HttpControlSocket::sendCommand(ConstElementPtr command) {
57 PostHttpRequestJsonPtr request;
58 request.reset(new PostHttpRequestJson(HttpRequest::Method::HTTP_POST,
59 "/",
60 HttpVersion(1, 1)));
61 request->setBodyAsJson(command);
62 try {
63 request->finalize();
64 } catch (exception const& ex) {
65 isc_throw(ControlSocketError, "failed to create request: "
66 << ex.what());
67 }
68
69 IOServicePtr io_service(new IOService());
70 HttpClient client(io_service, false);
71 boost::system::error_code received_ec;
72 string receive_errmsg;
73 HttpResponseJsonPtr response(new HttpResponseJson());
74
75 client.asyncSendRequest(getUrl(), TlsContextPtr(), request, response,
76 [&io_service, &received_ec, &receive_errmsg]
77 (const boost::system::error_code& ec,
78 const HttpResponsePtr&, const string& errmsg) {
79 // Capture error code and message.
80 received_ec = ec;
81 receive_errmsg = errmsg;
82 // Got the IO service so stop IO service.
83 // This causes to stop IO service when
84 // all handlers have been invoked.
85 io_service->stopWork();
86 },
87 HttpClient::RequestTimeout(TIMEOUT_DHCP_SERVER_FORWARD_COMMAND));
88
89 // Perform this synchronously.
90 io_service->run();
91
92 client.stop();
93 io_service->stopAndPoll();
94
95 if (received_ec) {
96 // Got an error code.
97 isc_throw(ControlSocketError, "communication error (code): "
98 << received_ec.message());
99 }
100
101 if (!receive_errmsg.empty()) {
102 // Got an error message.
103 isc_throw(ControlSocketError, "communication error (message): "
104 << receive_errmsg);
105 }
106
107 if (!response) {
108 // Failed to get the answer.
109 isc_throw(ControlSocketError, "empty response");
110 }
111
112 try {
113 auto response_list = response->getBodyAsJson();
114 // If there is no error the expected response is a list.
115 if (response_list->getType() == Element::list) {
116 // There must be at least one response.
117 if (response_list->empty()) {
118 isc_throw(ControlSocketError, "list of responses must not be empty");
119 }
120 return (response_list->get(0));
121 }
122 // If there is an error the expected response is a map.
123 return (response->getBodyAsJson());
124 } catch (exception const& ex) {
125 isc_throw(ControlSocketError, "unparsable response: " << ex.what());
126 }
127}
128
129} // namespace netconf
130} // namespace isc
asio_wrapper.h
Element::list
@ list
Definition data.h:159
isc::asiolink::IOService
The IOService class is a wrapper for the ASIO io_context class.
Definition io_service.h:39
isc::http::HttpClient
HTTP client class.
Definition lib/http/client.h:86
isc::http::HttpRequest::Method::HTTP_POST
@ HTTP_POST
Definition request.h:63
isc::http::HttpResponseJson
Represents HTTP response with JSON content.
Definition response_json.h:34
isc::http::PostHttpRequestJson
Represents HTTP POST request with JSON body.
Definition post_request_json.h:37
isc::netconf::ControlSocketBase::ControlSocketBase
ControlSocketBase(CfgControlSocketPtr ctrl_sock)
Constructor.
Definition control_socket.h:39
isc::netconf::ControlSocketBase::getUrl
const isc::http::Url getUrl() const
Returns the HTTP server URL.
Definition control_socket.h:66
isc::netconf::HttpControlSocket
Class for control socket communication over HTTP socket.
Definition http_control_socket.h:23
isc::netconf::HttpControlSocket::configTest
data::ConstElementPtr configTest(data::ElementPtr config, const std::string &service) override final
Test configuration.
Definition http_control_socket.cc:46
isc::netconf::HttpControlSocket::configSet
data::ConstElementPtr configSet(data::ElementPtr config, const std::string &service) override final
Set configuration.
Definition http_control_socket.cc:51
isc::netconf::HttpControlSocket::configGet
data::ConstElementPtr configGet(const std::string &service) override final
Get configuration.
Definition http_control_socket.cc:41
isc::netconf::HttpControlSocket::HttpControlSocket
HttpControlSocket(CfgControlSocketPtr ctrl_sock)
Constructor.
Definition http_control_socket.cc:36
command_interpreter.h
This file contains several functions and constants that are used for handling commands and responses ...
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
http_control_socket.h
Contains declarations for HTTP control socket communication.
io_service.h
client.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::TlsContextPtr
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Definition common_tls.h:37
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::config
Definition command_interpreter.cc:23
isc::config::createCommand
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" }...
Definition command_interpreter.cc:167
isc::config::TIMEOUT_DHCP_SERVER_FORWARD_COMMAND
constexpr long TIMEOUT_DHCP_SERVER_FORWARD_COMMAND
Timeout for the DHCP server to forward command to a Kea server, e.g.
Definition timeouts.h:27
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::http
Definition auth_config.h:19
isc::http::PostHttpRequestJsonPtr
boost::shared_ptr< PostHttpRequestJson > PostHttpRequestJsonPtr
Pointer to PostHttpRequestJson.
Definition post_request_json.h:29
isc::http::HttpResponseJsonPtr
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
Definition response_json.h:27
isc::http::HttpResponsePtr
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition response.h:81
isc::netconf
Definition control_socket.cc:20
isc::netconf::HttpControlSocketPtr
std::shared_ptr< HttpControlSocket > HttpControlSocketPtr
Type definition for the pointer to the HttpControlSocket.
Definition http_control_socket.h:76
isc::netconf::ControlSocketBasePtr
std::shared_ptr< ControlSocketBase > ControlSocketBasePtr
Type definition for the pointer to the ControlSocketBase.
Definition control_socket.h:106
isc::netconf::createControlSocket< CfgControlSocket::Type::HTTP >
ControlSocketBasePtr createControlSocket< CfgControlSocket::Type::HTTP >(CfgControlSocketPtr ctrl_sock)
Factory template specialization for http control sockets.
Definition http_control_socket.cc:32
isc::netconf::CfgControlSocketPtr
std::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
Definition netconf_config.h:143
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
post_request_json.h
response_json.h
isc::http::HttpVersion
HTTP protocol version.
Definition http_types.h:14
timeouts.h
tls_socket.h