diff --git a/include/Void/Net/Socket.hpp b/include/Void/Net/Socket.hpp new file mode 100644 index 0000000..9d83e43 --- /dev/null +++ b/include/Void/Net/Socket.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace Void::Net +{ + class UdpSocket final + { + public: + UdpSocket() noexcept = default; + ~UdpSocket(); + UdpSocket(UdpSocket&& other) noexcept; + UdpSocket& operator=(UdpSocket&& other) noexcept; + UdpSocket(const UdpSocket&) = delete; + UdpSocket& operator=(const UdpSocket&) = delete; + + [[nodiscard]] static Void::Result::Result + connect(std::string_view host, std::uint16_t port); + [[nodiscard]] bool valid() const noexcept { return handle_ >= 0; } + [[nodiscard]] int native_handle() const noexcept { return handle_; } + void close() noexcept; + void shutdown() noexcept; + [[nodiscard]] Void::Result::Result + wait_readable(std::chrono::milliseconds timeout) const; + [[nodiscard]] Void::Result::Result + send(std::span bytes); + [[nodiscard]] Void::Result::Result + receive(std::span destination); + + private: + explicit UdpSocket(int handle) noexcept : handle_(handle) {} + int handle_{-1}; + }; +} diff --git a/include/Void/System/Process.hpp b/include/Void/System/Process.hpp index 4950855..5e9c3f5 100644 --- a/include/Void/System/Process.hpp +++ b/include/Void/System/Process.hpp @@ -63,4 +63,11 @@ namespace Void::Process shell(std::string_view command, std::chrono::milliseconds timeout = std::chrono::milliseconds{0}); + /// Replace the current process image while preserving its PID and inherited + /// environment. Returns only when the platform does not support re-exec or + /// exec fails. + [[nodiscard]] Void::Result::Result + reexec(const std::filesystem::path& executable, + const std::vector& arguments); + } // namespace Void::Process diff --git a/src/Net/Socket.cpp b/src/Net/Socket.cpp new file mode 100644 index 0000000..16f2e21 --- /dev/null +++ b/src/Net/Socket.cpp @@ -0,0 +1,142 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +namespace Void::Net +{ + UdpSocket::~UdpSocket() = default; + UdpSocket::UdpSocket(UdpSocket&&) noexcept = default; + UdpSocket& UdpSocket::operator=(UdpSocket&&) noexcept = default; + Void::Result::Result + UdpSocket::connect(std::string_view, std::uint16_t) + { + return Void::Result::fail("UdpSocket is not implemented on Windows"); + } + void UdpSocket::close() noexcept {} + void UdpSocket::shutdown() noexcept {} + Void::Result::Result + UdpSocket::wait_readable(std::chrono::milliseconds) const + { + return Void::Result::fail("UdpSocket is not implemented on Windows"); + } + Void::Result::Result + UdpSocket::send(std::span) + { + return Void::Result::fail("UdpSocket is not implemented on Windows"); + } + Void::Result::Result + UdpSocket::receive(std::span) + { + return Void::Result::fail("UdpSocket is not implemented on Windows"); + } +} +#else +#include +#include +#include +#include + +namespace Void::Net +{ + namespace + { + Void::Result::Failure socketError( + std::string_view operation) + { + return Void::Result::fail(std::format("{}: {}", operation, + std::strerror(errno))); + } + } + + UdpSocket::~UdpSocket() { close(); } + UdpSocket::UdpSocket(UdpSocket&& other) noexcept + : handle_(std::exchange(other.handle_, -1)) + { + } + UdpSocket& UdpSocket::operator=(UdpSocket&& other) noexcept + { + if (this != &other) { + close(); + handle_ = std::exchange(other.handle_, -1); + } + return *this; + } + + Void::Result::Result + UdpSocket::connect(std::string_view host, std::uint16_t port) + { + addrinfo hints{}; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; + addrinfo* addresses = nullptr; + const auto service = std::to_string(port); + const auto hostname = std::string(host); + const int resolved = ::getaddrinfo(hostname.c_str(), service.c_str(), + &hints, &addresses); + if (resolved != 0) return Void::Result::fail(std::format( + "getaddrinfo '{}': {}", hostname, gai_strerror(resolved))); + std::unique_ptr guard( + addresses, &freeaddrinfo); + for (auto* address = addresses; address; address = address->ai_next) { + const int handle = ::socket(address->ai_family, + address->ai_socktype, address->ai_protocol); + if (handle < 0) continue; + if (::connect(handle, address->ai_addr, address->ai_addrlen) == 0) + return UdpSocket(handle); + ::close(handle); + } + return socketError("UDP connect"); + } + + void UdpSocket::close() noexcept + { + if (handle_ >= 0) { + ::close(handle_); + handle_ = -1; + } + } + + void UdpSocket::shutdown() noexcept + { + if (handle_ >= 0) ::shutdown(handle_, SHUT_RDWR); + } + + Void::Result::Result + UdpSocket::wait_readable(std::chrono::milliseconds timeout) const + { + if (!valid()) return Void::Result::fail("UDP socket is closed"); + pollfd descriptor{.fd = handle_, .events = POLLIN, .revents = 0}; + const auto bounded = std::clamp(timeout.count(), 0, + std::numeric_limits::max()); + const int result = ::poll(&descriptor, 1, static_cast(bounded)); + if (result < 0 && errno != EINTR) return socketError("UDP poll"); + return result > 0 && (descriptor.revents & POLLIN) != 0; + } + + Void::Result::Result + UdpSocket::send(std::span bytes) + { + if (!valid()) return Void::Result::fail("UDP socket is closed"); + const auto result = ::send(handle_, bytes.data(), bytes.size(), 0); + if (result < 0) return socketError("UDP send"); + return static_cast(result); + } + + Void::Result::Result + UdpSocket::receive(std::span destination) + { + if (!valid()) return Void::Result::fail("UDP socket is closed"); + const auto result = ::recv(handle_, destination.data(), destination.size(), 0); + if (result < 0) return socketError("UDP receive"); + return static_cast(result); + } +} +#endif diff --git a/src/System/Process.cpp b/src/System/Process.cpp index c9c240b..d367714 100644 --- a/src/System/Process.cpp +++ b/src/System/Process.cpp @@ -14,7 +14,7 @@ namespace Void::Process std::chrono::milliseconds) { return Void::Result::fail("Process::run: not implemented on Windows"); - } +} Void::Result::Result run_streaming(const std::filesystem::path&, @@ -32,6 +32,13 @@ namespace Void::Process { return Void::Result::fail("Process::shell: not implemented on Windows"); } + + Void::Result::Result + reexec(const std::filesystem::path&, + const std::vector&) + { + return Void::Result::fail("Process::reexec: not implemented on Windows"); + } } // namespace Void::Process #else // POSIX @@ -316,6 +323,29 @@ namespace Void::Process return run("/bin/sh", {"-c", std::string{command}}, {}, timeout); } + Void::Result::Result + reexec(const std::filesystem::path& executable, + const std::vector& arguments) + { + if (executable.empty()) { + return Void::Result::fail("Process::reexec: executable is empty"); + } + std::vector owned; + owned.reserve(arguments.size() + 1); + owned.push_back(executable.string()); + owned.insert(owned.end(), arguments.begin(), arguments.end()); + std::vector argv; + argv.reserve(owned.size() + 1); + for (auto& argument : owned) { + argv.push_back(argument.data()); + } + argv.push_back(nullptr); + ::execv(executable.c_str(), argv.data()); + return Void::Result::fail(std::format( + "Process::reexec: execv '{}' failed: {}", executable.string(), + std::strerror(errno))); + } + } // namespace Void::Process #endif // _WIN32 diff --git a/tests/standalone/Net/Socket.cpp b/tests/standalone/Net/Socket.cpp new file mode 100644 index 0000000..04ee25c --- /dev/null +++ b/tests/standalone/Net/Socket.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +#include +#include + +Void::Testing::Test udp_socket_default_is_closed() +{ + Void::Net::UdpSocket socket; + Void::Testing::expect(!socket.valid()); + Void::Testing::expect(!socket.wait_readable(std::chrono::milliseconds{0})); +} + +Void::Testing::Test udp_socket_move_preserves_closed_state() +{ + Void::Net::UdpSocket source; + Void::Net::UdpSocket destination = std::move(source); + Void::Testing::expect(!source.valid()); + Void::Testing::expect(!destination.valid()); +}