Kea 2.7.5
unix_control_client.cc
Go to the documentation of this file.
1// Copyright (C) 2015-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
9#include <gtest/gtest.h>
11#include <unistd.h>
12#include <sys/socket.h>
13#include <sys/un.h>
14#include <errno.h>
15#include <string.h>
16
17namespace isc {
18namespace dhcp {
19namespace test {
20
24
28
31 if (socket_fd_ >= 0) {
32 static_cast<void>(close(socket_fd_));
33 socket_fd_ = -1;
34 }
35}
36
37bool UnixControlClient::connectToServer(const std::string& socket_path) {
38 // Create UNIX socket
39 socket_fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
40 if (socket_fd_ < 0) {
41 const char* errmsg = strerror(errno);
42 ADD_FAILURE() << "Failed to open unix stream socket: " << errmsg;
43 return (false);
44 }
45
46 struct sockaddr_un srv_addr;
47 if (socket_path.size() > sizeof(srv_addr.sun_path) - 1) {
48 ADD_FAILURE() << "Socket path specified (" << socket_path
49 << ") is larger than " << (sizeof(srv_addr.sun_path) - 1)
50 << " allowed.";
52 return (false);
53 }
54
55 // Prepare socket address
56 memset(&srv_addr, 0, sizeof(srv_addr));
57 srv_addr.sun_family = AF_UNIX;
58 strncpy(srv_addr.sun_path, socket_path.c_str(),
59 sizeof(srv_addr.sun_path) - 1);
60 socklen_t len = sizeof(srv_addr);
61
62 // Connect to the specified UNIX socket
63 int status = connect(socket_fd_, (struct sockaddr*)&srv_addr, len);
64 if (status == -1) {
65 const char* errmsg = strerror(errno);
66 ADD_FAILURE() << "Failed to connect unix socket: fd=" << socket_fd_
67 << ", path=" << socket_path << " : " << errmsg;
69 return (false);
70 }
71
72 return (true);
73}
74
75bool UnixControlClient::sendCommand(const std::string& command) {
76 if (socket_fd_ < 0) {
77 ADD_FAILURE() << "send command with closed socket";
78 return (false);
79 }
80 // Send command
81 int bytes_sent = send(socket_fd_, command.c_str(), command.length(), 0);
82 if (bytes_sent < command.length()) {
83 const char* errmsg = strerror(errno);
84 ADD_FAILURE() << "Failed to send " << command.length()
85 << " bytes, send() returned " << bytes_sent
86 << " : " << errmsg;
87 return (false);
88 }
89
90 return (true);
91}
92
93bool UnixControlClient::getResponse(std::string& response,
94 const unsigned int timeout_sec) {
95 // Receive response
96 char buf[65536];
97 memset(buf, 0, sizeof(buf));
98 switch (selectCheck(timeout_sec)) {
99 case -1: {
100 const char* errmsg = strerror(errno);
101 ADD_FAILURE() << "getResponse - select failed: " << errmsg;
102 return (false);
103 }
104 case 0:
105 return (false);
106
107 default:
108 break;
109 }
110
111 int bytes_rcvd = recv(socket_fd_, buf, sizeof(buf), 0);
112 if (bytes_rcvd < 0) {
113 const char* errmsg = strerror(errno);
114 ADD_FAILURE() << "Failed to receive a response. recv() returned "
115 << bytes_rcvd << " : " << errmsg;
116 return (false);
117 }
118
119 // Convert the response to a string
120 response = std::string(buf, bytes_rcvd);
121 return (true);
122}
123
124int UnixControlClient::selectCheck(const unsigned int timeout_sec) {
125 if (socket_fd_ < 0) {
126 ADD_FAILURE() << "select check with closed socket";
127 return -1;
128 }
129 if (socket_fd_ > 1023) {
130 ADD_FAILURE() << "select check with out of bound socket";
131 return -1;
132 }
133 int maxfd = 0;
134
135 fd_set read_fds;
136 FD_ZERO(&read_fds);
137
138 // Add this socket to listening set
139 FD_SET(socket_fd_, &read_fds);
140 maxfd = socket_fd_;
141
142 struct timeval select_timeout;
143 select_timeout.tv_sec = static_cast<time_t>(timeout_sec);
144 select_timeout.tv_usec = 0;
145
146 return (select(maxfd + 1, &read_fds, NULL, NULL, &select_timeout));
147}
148
149}
150}
151}
void disconnectFromServer()
Closes the Control Channel socket.
int socket_fd_
Retains the fd of the open socket.
bool getResponse(std::string &response, const unsigned int timeout_sec=0)
Reads the response text from the open Control Channel.
int selectCheck(const unsigned int timeout_sec)
Uses select to poll the Control Channel for data waiting.
bool sendCommand(const std::string &command)
Sends the given command across the open Control Channel.
bool connectToServer(const std::string &socket_path)
Connects to a Unix socket at the given path.
Defines the logger used by the top-level component of kea-lfc.