9 #include <gtest/gtest.h>
12 #include <sys/socket.h>
41 const char* errmsg = strerror(errno);
42 ADD_FAILURE() <<
"Failed to open unix stream socket: " << errmsg;
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)
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);
63 int status = connect(
socket_fd_, (
struct sockaddr*)&srv_addr, len);
65 const char* errmsg = strerror(errno);
66 ADD_FAILURE() <<
"Failed to connect unix socket: fd=" <<
socket_fd_
67 <<
", path=" << socket_path <<
" : " << errmsg;
77 int bytes_sent = send(
socket_fd_, command.c_str(), command.length(), 0);
78 if (bytes_sent < command.length()) {
79 const char* errmsg = strerror(errno);
80 ADD_FAILURE() <<
"Failed to send " << command.length()
81 <<
" bytes, send() returned " << bytes_sent
90 const unsigned int timeout_sec) {
93 memset(buf, 0,
sizeof(buf));
96 const char* errmsg = strerror(errno);
97 ADD_FAILURE() <<
"getResponse - select failed: " << errmsg;
107 int bytes_rcvd = recv(
socket_fd_, buf,
sizeof(buf), 0);
108 if (bytes_rcvd < 0) {
109 const char* errmsg = strerror(errno);
110 ADD_FAILURE() <<
"Failed to receive a response. recv() returned "
111 << bytes_rcvd <<
" : " << errmsg;
116 response = std::string(buf, bytes_rcvd);
130 struct timeval select_timeout;
131 select_timeout.tv_sec =
static_cast<time_t
>(timeout_sec);
132 select_timeout.tv_usec = 0;
134 return (select(maxfd + 1, &read_fds, NULL, NULL, &select_timeout));
void disconnectFromServer()
Closes the Control Channel socket.
int selectCheck(const unsigned int timeout_sec)
Uses select to poll the Control Channel for data waiting.
~UnixControlClient()
Destructor.
bool connectToServer(const std::string &socket_path)
Connects to a Unix socket at the given path.
UnixControlClient()
Default constructor.
bool getResponse(std::string &response, const unsigned int timeout_sec=0)
Reads the response text from the open Control Channel.
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool sendCommand(const std::string &command)
Sends the given command across the open Control Channel.
int socket_fd_
Retains the fd of the open socket.