Kea: unix_control_socket.cc Source File

| Kea 3.2.0-git

unix_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 <cc/command_interpreter.h>
15#include <cc/json_feed.h>
16#include <config/client_connection.h>
17#include <config/timeouts.h>
18#include <config/unix_command_config.h>
19#include <netconf/unix_control_socket.h>
20
21using namespace std;
22using namespace isc::asiolink;
23using namespace isc::config;
24using namespace isc::data;
25
26namespace isc {
27namespace netconf {
28
29 template <> ControlSocketBasePtr
30 createControlSocket<CfgControlSocket::Type::UNIX>(CfgControlSocketPtr ctrl_sock) {
31 return (UnixControlSocketPtr(new UnixControlSocket(ctrl_sock)));
32}
33
34UnixControlSocket::UnixControlSocket(CfgControlSocketPtr ctrl_sock)
35 : ControlSocketBase(ctrl_sock) {
36}
37
38 ConstElementPtr
39UnixControlSocket::configGet(const string& /*service*/) {
40 return (sendCommand(createCommand("config-get")));
41}
42
43 ConstElementPtr
44UnixControlSocket::configTest(ElementPtr config,
45 const string& /*service*/) {
46 return (sendCommand(createCommand("config-test", config)));
47}
48
49 ConstElementPtr
50UnixControlSocket::configSet(ElementPtr config,
51 const string& /*service*/) {
52 return (sendCommand(createCommand("config-set", config)));
53}
54
55 ConstElementPtr
56UnixControlSocket::sendCommand(ConstElementPtr command) {
57 // We are using our own IO service because this method is synchronous.
58 IOServicePtr io_service(new IOService());
59 ClientConnection conn(io_service);
60 boost::system::error_code received_ec;
61 ConstJSONFeedPtr received_feed;
62
63 string name(getName());
64 name = isc::config::UnixCommandConfig::validatePath(name);
65
66 conn.start(ClientConnection::SocketPath(name),
67 ClientConnection::ControlCommand(command->toWire()),
68 [&io_service, &received_ec, &received_feed]
69 (const boost::system::error_code& ec, ConstJSONFeedPtr feed) {
70 // Capture error code and parsed data.
71 received_ec = ec;
72 received_feed = feed;
73 // Got the IO service so stop IO service. This causes to
74 // stop IO service when all handlers have been invoked.
75 io_service->stopWork();
76 },
77 ClientConnection::Timeout(TIMEOUT_DHCP_SERVER_FORWARD_COMMAND));
78
79 // Perform this synchronously.
80 io_service->run();
81
82 if (received_ec) {
83 // Got an error.
84 isc_throw(ControlSocketError, "communication error: "
85 << received_ec.message());
86 }
87
88 if (!received_feed) {
89 // Failed to get the answer.
90 isc_throw(ControlSocketError, "empty response");
91 }
92
93 try {
94 return (received_feed->toElement());
95 } catch (exception const& ex) {
96 isc_throw(ControlSocketError, "unparsable response: " << ex.what());
97 }
98}
99
100} // namespace netconf
101} // namespace isc
asio_wrapper.h
isc::asiolink::IOService
The IOService class is a wrapper for the ASIO io_context class.
Definition io_service.h:39
isc::config::ClientConnection
Represents client side connection over the unix domain socket.
Definition client_connection.h:70
isc::config::UnixCommandConfig::validatePath
static std::string validatePath(const std::string socket_path)
Validates a path against the supported path for unix control sockets.
Definition unix_command_config.cc:125
isc::netconf::ControlSocketBase::ControlSocketBase
ControlSocketBase(CfgControlSocketPtr ctrl_sock)
Constructor.
Definition control_socket.h:39
isc::netconf::ControlSocketBase::getName
const std::string getName() const
Returns the Unix socket name.
Definition control_socket.h:59
isc::netconf::ControlSocketError
Exception thrown when the error during communication.
Definition control_socket.h:20
isc::netconf::UnixControlSocket
Class for control socket communication over UNIX socket.
Definition unix_control_socket.h:23
isc::netconf::UnixControlSocket::configTest
data::ConstElementPtr configTest(data::ElementPtr config, const std::string &service) override final
Test configuration.
Definition unix_control_socket.cc:44
isc::netconf::UnixControlSocket::UnixControlSocket
UnixControlSocket(CfgControlSocketPtr ctrl_sock)
Constructor.
Definition unix_control_socket.cc:34
isc::netconf::UnixControlSocket::configGet
data::ConstElementPtr configGet(const std::string &service) override final
Get configuration.
Definition unix_control_socket.cc:39
isc::netconf::UnixControlSocket::configSet
data::ConstElementPtr configSet(data::ElementPtr config, const std::string &service) override final
Set configuration.
Definition unix_control_socket.cc:50
client_connection.h
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
io_service.h
json_feed.h
isc::asiolink
Definition addr_utilities.cc:170
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::ConstJSONFeedPtr
boost::shared_ptr< const JSONFeed > ConstJSONFeedPtr
Pointer to the const JSONFeed.
Definition json_feed.h:27
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::netconf
Definition control_socket.cc:20
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::UNIX >
ControlSocketBasePtr createControlSocket< CfgControlSocket::Type::UNIX >(CfgControlSocketPtr ctrl_sock)
Factory template specialization for unix control sockets.
Definition unix_control_socket.cc:30
isc::netconf::CfgControlSocketPtr
std::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
Definition netconf_config.h:143
isc::netconf::UnixControlSocketPtr
std::shared_ptr< UnixControlSocket > UnixControlSocketPtr
Type definition for the pointer to the UnixControlSocket.
Definition unix_control_socket.h:76
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::config::ClientConnection::ControlCommand
Encapsulates control command.
Definition client_connection.h:86
isc::config::ClientConnection::SocketPath
Encapsulates socket path.
Definition client_connection.h:78
isc::config::ClientConnection::Timeout
Encapsulates timeout value.
Definition client_connection.h:94
timeouts.h
unix_command_config.h
unix_control_socket.h
Contains declarations for UNIX control socket communication.