1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 | // Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/unix_control_client.h>
#include <unistd.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/socket.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/un.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <errno.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
namespace isc {
namespace dhcp {
namespace test {
UnixControlClient::UnixControlClient() {
socket_fd_ = -1;
}
UnixControlClient::~UnixControlClient() {
disconnectFromServer();
}
/// @brief Closes the Control Channel socket
void UnixControlClient::disconnectFromServer() {
if (socket_fd_ >= 0) {
static_cast<void>(close(socket_fd_));
socket_fd_ = -1;
}
}
bool UnixControlClient::connectToServer(const std::string& socket_path) {
// Create UNIX socket
socket_fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
if (socket_fd_ < 0) {
const char* errmsg = strerror(errno);
ADD_FAILURE() << "Failed to open unix stream socket: " << errmsg;
return (false);
}
struct sockaddr_un srv_addr;
if (socket_path.size() > sizeof(srv_addr.sun_path) - 1) {
ADD_FAILURE() << "Socket path specified (" << socket_path
<< ") is larger than " << (sizeof(srv_addr.sun_path) - 1)
<< " allowed.";
disconnectFromServer();
return (false);
}
// Prepare socket address
memset(&srv_addr, 0, sizeof(srv_addr));
srv_addr.sun_family = AF_UNIX;
strncpy(srv_addr.sun_path, socket_path.c_str(),
sizeof(srv_addr.sun_path) - 1);
socklen_t len = sizeof(srv_addr);
// Connect to the specified UNIX socket
int status = connect(socket_fd_, (struct sockaddr*)&srv_addr, len);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
if (status == -1) {
const char* errmsg = strerror(errno);
ADD_FAILURE() << "Failed to connect unix socket: fd=" << socket_fd_
<< ", path=" << socket_path << " : " << errmsg;
disconnectFromServer();
return (false);
}
return (true);
}
bool UnixControlClient::sendCommand(const std::string& command) {
if (socket_fd_ < 0) {
ADD_FAILURE() << "send command with closed socket";
return (false);
}
// Send command
int bytes_sent = send(socket_fd_, command.c_str(), command.length(), 0);
if (bytes_sent < command.length()) {
const char* errmsg = strerror(errno);
ADD_FAILURE() << "Failed to send " << command.length()
<< " bytes, send() returned " << bytes_sent
<< " : " << errmsg;
return (false);
}
return (true);
}
bool UnixControlClient::getResponse(std::string& response,
const unsigned int timeout_sec) {
// Receive response
char buf[65536];
memset(buf, 0, sizeof(buf));
switch (selectCheck(timeout_sec)) {
case -1: {
const char* errmsg = strerror(errno);
ADD_FAILURE() << "getResponse - select failed: " << errmsg;
return (false);
}
case 0:
return (false);
default:
break;
}
int bytes_rcvd = recv(socket_fd_, buf, sizeof(buf), 0);
if (bytes_rcvd < 0) {
const char* errmsg = strerror(errno);
ADD_FAILURE() << "Failed to receive a response. recv() returned "
<< bytes_rcvd << " : " << errmsg;
return (false);
}
// Convert the response to a string
response = std::string(buf, bytes_rcvd);
return (true);
}
int UnixControlClient::selectCheck(const unsigned int timeout_sec) {
if (socket_fd_ < 0) {
ADD_FAILURE() << "select check with closed socket";
return -1;
}
if (socket_fd_ > 1023) {
ADD_FAILURE() << "select check with out of bound socket";
return -1;
}
int maxfd = 0;
fd_set read_fds;
FD_ZERO(&read_fds);
// Add this socket to listening set
FD_SET(socket_fd_, &read_fds);
maxfd = socket_fd_;
struct timeval select_timeout;
select_timeout.tv_sec = static_cast<time_t>(timeout_sec);
select_timeout.tv_usec = 0;
return (select(maxfd + 1, &read_fds, NULL, NULL, &select_timeout));
}
}
}
}
|