Skip to content

Commit

Permalink
try abstract unix domain sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 23, 2024
1 parent 6ad1df7 commit d600da0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/common/ipc/unix_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

#include <cetl/cetl.hpp>

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -57,14 +59,20 @@ bool UnixSocketClient::connect_to_server()
}

sockaddr_un addr{};
addr.sun_family = AF_UNIX;
addr.sun_family = AF_UNIX;
const std::string abstract_socket_path = '\0' + socket_path_;
CETL_DEBUG_ASSERT(abstract_socket_path.size() <= sizeof(addr.sun_path), "");
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
::strncpy(addr.sun_path, socket_path_.c_str(), sizeof(addr.sun_path) - 1);
std::memcpy(addr.sun_path,
abstract_socket_path.c_str(),
std::min(sizeof(addr.sun_path), abstract_socket_path.size()));

if (const auto err = platform::posixSyscallError([this, &addr] {
if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] {
//
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return ::connect(client_fd_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr));
return ::connect(client_fd_,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<const sockaddr*>(&addr),
offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size());
}))
{
std::cerr << "Failed to connect to server: " << ::strerror(err) << "\n";
Expand Down
27 changes: 13 additions & 14 deletions src/common/ipc/unix_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include <cetl/rtti.hpp>
#include <libcyphal/executor.hpp>

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -95,10 +97,6 @@ UnixSocketServer::~UnixSocketServer()
//
return ::close(server_fd_);
});
platform::posixSyscallError([this] {
//
return ::unlink(socket_path_.c_str());
});
}
}

Expand All @@ -116,19 +114,20 @@ bool UnixSocketServer::start()
}

sockaddr_un addr{};
addr.sun_family = AF_UNIX;
addr.sun_family = AF_UNIX;
const std::string abstract_socket_path = '\0' + socket_path_;
CETL_DEBUG_ASSERT(abstract_socket_path.size() <= sizeof(addr.sun_path), "");
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
::strncpy(addr.sun_path, socket_path_.c_str(), sizeof(addr.sun_path) - 1);

platform::posixSyscallError([this] {
//
return ::unlink(socket_path_.c_str());
});
std::memcpy(addr.sun_path,
abstract_socket_path.c_str(),
std::min(sizeof(addr.sun_path), abstract_socket_path.size()));

if (const auto err = platform::posixSyscallError([this, &addr] {
if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] {
//
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return ::bind(server_fd_, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr));
return ::bind(server_fd_,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<const sockaddr*>(&addr),
offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size());
}))
{
std::cerr << "Failed to bind socket: " << ::strerror(err) << "\n";
Expand Down

0 comments on commit d600da0

Please sign in to comment.