Kea  1.9.9-git
http_control_socket.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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 
13 #include <cc/command_interpreter.h>
14 #include <asiolink/asio_wrapper.h>
15 #include <asiolink/io_service.h>
16 #include <asiolink/tls_socket.h>
17 #include <http/client.h>
18 #include <http/post_request_json.h>
19 #include <http/response_json.h>
20 #include <config/timeouts.h>
21 
22 using namespace std;
23 using namespace isc::asiolink;
24 using namespace isc::config;
25 using namespace isc::data;
26 using namespace isc::http;
27 
28 namespace isc {
29 namespace netconf {
30 
31 template <> ControlSocketBasePtr
32 createControlSocket<CfgControlSocket::Type::HTTP>(CfgControlSocketPtr ctrl_sock) {
33  return (HttpControlSocketPtr(new HttpControlSocket(ctrl_sock)));
34 }
35 
36 HttpControlSocket::HttpControlSocket(CfgControlSocketPtr ctrl_sock)
37  : ControlSocketBase(ctrl_sock) {
38 }
39 
41 }
42 
44 HttpControlSocket::configGet(const string& service) {
45  if (service == "ca") {
46  return (sendCommand(createCommand("config-get")));
47  } else {
48  return (sendCommand(createCommand("config-get", service)));
49  }
50 }
51 
53 HttpControlSocket::configTest(ConstElementPtr config, const string& service) {
54  if (service == "ca") {
55  return (sendCommand(createCommand("config-test", config)));
56  } else {
57  return (sendCommand(createCommand("config-test", config, service)));
58  }
59 }
60 
62 HttpControlSocket::configSet(ConstElementPtr config, const string& service) {
63  if (service == "ca") {
64  return (sendCommand(createCommand("config-set", config)));
65  } else {
66  return (sendCommand(createCommand("config-set", config, service)));
67  }
68 }
69 
71 HttpControlSocket::sendCommand(ConstElementPtr command) {
72  PostHttpRequestJsonPtr request;
73  request.reset(new PostHttpRequestJson(HttpRequest::Method::HTTP_POST,
74  "/",
75  HttpVersion(1, 1)));
76  request->setBodyAsJson(command);
77  try {
78  request->finalize();
79  } catch (const std::exception& ex) {
80  isc_throw(ControlSocketError, "failed to create request: "
81  << ex.what());
82  }
83 
84  IOServicePtr io_service(new IOService());
85  HttpClient client(*io_service);
86  boost::system::error_code received_ec;
87  string receive_errmsg;
88  HttpResponseJsonPtr response(new HttpResponseJson());
89 
90  client.asyncSendRequest(getUrl(), TlsContextPtr(), request, response,
91  [&io_service, &received_ec, &receive_errmsg]
92  (const boost::system::error_code& ec,
93  const HttpResponsePtr&, const string& errmsg) {
94  // Capture error code and message.
95  received_ec = ec;
96  receive_errmsg = errmsg;
97  // Got the IO service so stop IO service.
98  // This causes to stop IO service when
99  // all handlers have been invoked.
100  io_service->stopWork();
101  },
103 
104  // Perform this synchronously.
105  io_service->run();
106 
107  if (received_ec) {
108  // Got an error code.
109  isc_throw(ControlSocketError, "communication error (code): "
110  << received_ec.message());
111  }
112 
113  if (!receive_errmsg.empty()) {
114  // Got an error message.
115  isc_throw(ControlSocketError, "communication error (message): "
116  << receive_errmsg);
117  }
118 
119  if (!response) {
120  // Failed to get the answer.
121  isc_throw(ControlSocketError, "empty response");
122  }
123 
124  try {
125  return (response->getBodyAsJson());
126  } catch (const std::exception& ex) {
127  isc_throw(ControlSocketError, "unparsable response: " << ex.what());
128  }
129 }
130 
131 } // namespace netconf
132 } // namespace isc
133 
Represents HTTP POST request with JSON body.
Contains declarations for HTTP control socket communication.
boost::shared_ptr< HttpControlSocket > HttpControlSocketPtr
Type definition for the pointer to the HttpControlSocket.
boost::shared_ptr< HttpResponseJson > HttpResponseJsonPtr
Pointer to the HttpResponseJson object.
Definition: response_json.h:24
boost::shared_ptr< ControlSocketBase > ControlSocketBasePtr
Type definition for the pointer to the ControlSocketBase.
HTTP request/response timeout value.
Definition: client.h:90
Base class for control socket communication.
HTTP protocol version.
Definition: http_types.h:14
STL namespace.
Class for control socket communication over HTTP socket.
virtual data::ConstElementPtr configTest(data::ConstElementPtr config, const std::string &service)
Test configuration.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< PostHttpRequestJson > PostHttpRequestJsonPtr
Pointer to PostHttpRequestJson.
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
virtual ~HttpControlSocket()
Destructor (does nothing).
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" }) ...
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
constexpr long TIMEOUT_AGENT_FORWARD_COMMAND
Timeout for the Control Agent to forward command to a Kea server, e.g.
Definition: timeouts.h:31
Represents HTTP response with JSON content.
Definition: response_json.h:34
HTTP client class.
Definition: client.h:87
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual data::ConstElementPtr configSet(data::ConstElementPtr config, const std::string &service)
Set configuration.
virtual data::ConstElementPtr configGet(const std::string &service)
Get configuration.
This file contains several functions and constants that are used for handling commands and responses ...
const isc::http::Url getUrl() const
Returns the HTTP server URL.
boost::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.