mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 20:37:34 +00:00
LibCore: Move Stream-based sockets into the Core
namespace
This commit is contained in:
parent
d43a7eae54
commit
a96339b72b
123 changed files with 1157 additions and 1100 deletions
|
@ -22,6 +22,7 @@ set(SOURCES
|
|||
Property.cpp
|
||||
SecretString.cpp
|
||||
SessionManagement.cpp
|
||||
Socket.cpp
|
||||
SOCKSProxyClient.cpp
|
||||
StandardPaths.cpp
|
||||
Stream.cpp
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/Object.h>
|
||||
#include <LibCore/SessionManagement.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibThreading/Mutex.h>
|
||||
#include <LibThreading/MutexProtected.h>
|
||||
#include <errno.h>
|
||||
|
@ -156,7 +157,7 @@ pid_t EventLoop::s_pid;
|
|||
class InspectorServerConnection : public Object {
|
||||
C_OBJECT(InspectorServerConnection)
|
||||
private:
|
||||
explicit InspectorServerConnection(NonnullOwnPtr<Stream::LocalSocket> socket)
|
||||
explicit InspectorServerConnection(NonnullOwnPtr<LocalSocket> socket)
|
||||
: m_socket(move(socket))
|
||||
, m_client_id(s_id_allocator.with_locked([](auto& allocator) {
|
||||
return allocator->allocate();
|
||||
|
@ -305,7 +306,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
NonnullOwnPtr<Stream::LocalSocket> m_socket;
|
||||
NonnullOwnPtr<LocalSocket> m_socket;
|
||||
WeakPtr<Object> m_inspected_object;
|
||||
int m_client_id { -1 };
|
||||
};
|
||||
|
@ -370,7 +371,7 @@ bool connect_to_inspector_server()
|
|||
return false;
|
||||
}
|
||||
auto inspector_server_path = maybe_path.value();
|
||||
auto maybe_socket = Stream::LocalSocket::connect(inspector_server_path, Stream::PreventSIGPIPE::Yes);
|
||||
auto maybe_socket = LocalSocket::connect(inspector_server_path, Socket::PreventSIGPIPE::Yes);
|
||||
if (maybe_socket.is_error()) {
|
||||
dbgln("connect_to_inspector_server: Failed to connect: {}", maybe_socket.error());
|
||||
return false;
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace Core {
|
|||
|
||||
class AnonymousBuffer;
|
||||
class ArgsParser;
|
||||
class BufferedSocketBase;
|
||||
class ChildEvent;
|
||||
class ConfigFile;
|
||||
class CustomEvent;
|
||||
|
@ -22,6 +23,7 @@ class Event;
|
|||
class EventLoop;
|
||||
class IODevice;
|
||||
class LocalServer;
|
||||
class LocalSocket;
|
||||
class MimeData;
|
||||
class NetworkJob;
|
||||
class NetworkResponse;
|
||||
|
@ -29,18 +31,19 @@ class Notifier;
|
|||
class Object;
|
||||
class ObjectClassRegistration;
|
||||
class ProcessStatisticsReader;
|
||||
class Socket;
|
||||
class SocketAddress;
|
||||
class TCPServer;
|
||||
class TCPSocket;
|
||||
class Timer;
|
||||
class TimerEvent;
|
||||
class UDPServer;
|
||||
class UDPSocket;
|
||||
|
||||
enum class TimerShouldFireWhenNotVisible;
|
||||
|
||||
namespace Stream {
|
||||
class File;
|
||||
class Socket;
|
||||
class BufferedSocketBase;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <LibCore/LocalServer.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/SessionManagement.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibCore/SystemServerTakeover.h>
|
||||
|
@ -113,7 +114,7 @@ bool LocalServer::listen(DeprecatedString const& address)
|
|||
return true;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::LocalSocket>> LocalServer::accept()
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalServer::accept()
|
||||
{
|
||||
VERIFY(m_listening);
|
||||
sockaddr_un un;
|
||||
|
@ -133,7 +134,7 @@ ErrorOr<NonnullOwnPtr<Stream::LocalSocket>> LocalServer::accept()
|
|||
(void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
|
||||
#endif
|
||||
|
||||
return Stream::LocalSocket::adopt_fd(accepted_fd, Stream::PreventSIGPIPE::Yes);
|
||||
return LocalSocket::adopt_fd(accepted_fd, Socket::PreventSIGPIPE::Yes);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,9 +21,9 @@ public:
|
|||
bool is_listening() const { return m_listening; }
|
||||
bool listen(DeprecatedString const& address);
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::LocalSocket>> accept();
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> accept();
|
||||
|
||||
Function<void(NonnullOwnPtr<Stream::LocalSocket>)> on_accept;
|
||||
Function<void(NonnullOwnPtr<LocalSocket>)> on_accept;
|
||||
Function<void(Error)> on_accept_error;
|
||||
|
||||
private:
|
||||
|
|
|
@ -16,7 +16,7 @@ NetworkJob::NetworkJob(AK::Stream& output_stream)
|
|||
{
|
||||
}
|
||||
|
||||
void NetworkJob::start(Core::Stream::Socket&)
|
||||
void NetworkJob::start(Core::Socket&)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
DetachFromSocket,
|
||||
CloseSocket,
|
||||
};
|
||||
virtual void start(Core::Stream::Socket&) = 0;
|
||||
virtual void start(Core::Socket&) = 0;
|
||||
virtual void shutdown(ShutdownMode) = 0;
|
||||
virtual void fail(Error error) { did_fail(error); }
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ StringView reply_response_name(Reply reply)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::Version version, Method method)
|
||||
ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Socket& socket, Core::SOCKSProxyClient::Version version, Method method)
|
||||
{
|
||||
Socks5VersionIdentifierAndMethodSelectionMessage message {
|
||||
.version_identifier = to_underlying(version),
|
||||
|
@ -120,7 +120,7 @@ ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Stream:
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::Version version, Core::SOCKSProxyClient::HostOrIPV4 target, int port, Core::SOCKSProxyClient::Command command)
|
||||
ErrorOr<Reply> send_connect_request_message(Core::Socket& socket, Core::SOCKSProxyClient::Version version, Core::SOCKSProxyClient::HostOrIPV4 target, int port, Core::SOCKSProxyClient::Command command)
|
||||
{
|
||||
AllocatingMemoryStream stream;
|
||||
|
||||
|
@ -216,7 +216,7 @@ ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::
|
|||
return Reply(response_header.status);
|
||||
}
|
||||
|
||||
ErrorOr<u8> send_username_password_authentication_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::UsernamePasswordAuthenticationData const& auth_data)
|
||||
ErrorOr<u8> send_username_password_authentication_message(Core::Socket& socket, Core::SOCKSProxyClient::UsernamePasswordAuthenticationData const& auth_data)
|
||||
{
|
||||
AllocatingMemoryStream stream;
|
||||
|
||||
|
@ -314,10 +314,10 @@ ErrorOr<NonnullOwnPtr<SOCKSProxyClient>> SOCKSProxyClient::connect(HostOrIPV4 co
|
|||
{
|
||||
auto underlying = TRY(server.visit(
|
||||
[&](u32 ipv4) {
|
||||
return Core::Stream::TCPSocket::connect({ IPv4Address(ipv4), static_cast<u16>(server_port) });
|
||||
return Core::TCPSocket::connect({ IPv4Address(ipv4), static_cast<u16>(server_port) });
|
||||
},
|
||||
[&](DeprecatedString const& hostname) {
|
||||
return Core::Stream::TCPSocket::connect(hostname, static_cast<u16>(server_port));
|
||||
return Core::TCPSocket::connect(hostname, static_cast<u16>(server_port));
|
||||
}));
|
||||
|
||||
auto socket = TRY(connect(*underlying, version, target, target_port, auth_data, command));
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <LibCore/Proxy.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
namespace Core {
|
||||
class SOCKSProxyClient final : public Stream::Socket {
|
||||
class SOCKSProxyClient final : public Socket {
|
||||
public:
|
||||
enum class Version : u8 {
|
||||
V4 = 0x04,
|
||||
|
|
410
Userland/Libraries/LibCore/Socket.cpp
Normal file
410
Userland/Libraries/LibCore/Socket.cpp
Normal file
|
@ -0,0 +1,410 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/System.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
|
||||
{
|
||||
int socket_domain;
|
||||
switch (domain) {
|
||||
case SocketDomain::Inet:
|
||||
socket_domain = AF_INET;
|
||||
break;
|
||||
case SocketDomain::Local:
|
||||
socket_domain = AF_LOCAL;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
// Let's have a safe default of CLOEXEC. :^)
|
||||
#ifdef SOCK_CLOEXEC
|
||||
return System::socket(socket_domain, socket_type | SOCK_CLOEXEC, 0);
|
||||
#else
|
||||
auto fd = TRY(System::socket(socket_domain, socket_type, 0));
|
||||
TRY(System::fcntl(fd, F_SETFD, FD_CLOEXEC));
|
||||
return fd;
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<IPv4Address> Socket::resolve_host(DeprecatedString const& host, SocketType type)
|
||||
{
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
struct addrinfo hints = {};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = socket_type;
|
||||
hints.ai_flags = 0;
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
auto const results = TRY(Core::System::getaddrinfo(host.characters(), nullptr, hints));
|
||||
|
||||
for (auto const& result : results.addresses()) {
|
||||
if (result.ai_family == AF_INET) {
|
||||
auto* socket_address = bit_cast<struct sockaddr_in*>(result.ai_addr);
|
||||
NetworkOrdered<u32> const network_ordered_address { socket_address->sin_addr.s_addr };
|
||||
return IPv4Address { network_ordered_address };
|
||||
}
|
||||
}
|
||||
|
||||
return Error::from_string_literal("Could not resolve to IPv4 address");
|
||||
}
|
||||
|
||||
ErrorOr<void> Socket::connect_local(int fd, DeprecatedString const& path)
|
||||
{
|
||||
auto address = SocketAddress::local(path);
|
||||
auto maybe_sockaddr = address.to_sockaddr_un();
|
||||
if (!maybe_sockaddr.has_value()) {
|
||||
dbgln("Core::Socket::connect_local: Could not obtain a sockaddr_un");
|
||||
return Error::from_errno(EINVAL);
|
||||
}
|
||||
|
||||
auto addr = maybe_sockaddr.release_value();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
}
|
||||
|
||||
ErrorOr<void> Socket::connect_inet(int fd, SocketAddress const& address)
|
||||
{
|
||||
auto addr = address.to_sockaddr_in();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
}
|
||||
|
||||
ErrorOr<Bytes> PosixSocketHelper::read(Bytes buffer, int flags)
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
ssize_t nread = TRY(System::recv(m_fd, buffer.data(), buffer.size(), flags));
|
||||
m_last_read_was_eof = nread == 0;
|
||||
|
||||
// If a socket read is EOF, then no more data can be read from it because
|
||||
// the protocol has disconnected. In this case, we can just disable the
|
||||
// notifier if we have one.
|
||||
if (m_last_read_was_eof && m_notifier)
|
||||
m_notifier->set_enabled(false);
|
||||
|
||||
return buffer.trim(nread);
|
||||
}
|
||||
|
||||
ErrorOr<size_t> PosixSocketHelper::write(ReadonlyBytes buffer, int flags)
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
return TRY(System::send(m_fd, buffer.data(), buffer.size(), flags));
|
||||
}
|
||||
|
||||
void PosixSocketHelper::close()
|
||||
{
|
||||
if (!is_open()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_notifier)
|
||||
m_notifier->set_enabled(false);
|
||||
|
||||
ErrorOr<void> result;
|
||||
do {
|
||||
result = System::close(m_fd);
|
||||
} while (result.is_error() && result.error().code() == EINTR);
|
||||
|
||||
VERIFY(!result.is_error());
|
||||
m_fd = -1;
|
||||
}
|
||||
|
||||
ErrorOr<bool> PosixSocketHelper::can_read_without_blocking(int timeout) const
|
||||
{
|
||||
struct pollfd the_fd = { .fd = m_fd, .events = POLLIN, .revents = 0 };
|
||||
|
||||
ErrorOr<int> result { 0 };
|
||||
do {
|
||||
result = Core::System::poll({ &the_fd, 1 }, timeout);
|
||||
} while (result.is_error() && result.error().code() == EINTR);
|
||||
|
||||
if (result.is_error())
|
||||
return result.release_error();
|
||||
|
||||
return (the_fd.revents & POLLIN) > 0;
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_blocking(bool enabled)
|
||||
{
|
||||
int value = enabled ? 0 : 1;
|
||||
return System::ioctl(m_fd, FIONBIO, &value);
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
||||
{
|
||||
int flags = TRY(System::fcntl(m_fd, F_GETFD));
|
||||
|
||||
if (enabled)
|
||||
flags |= FD_CLOEXEC;
|
||||
else
|
||||
flags &= ~FD_CLOEXEC;
|
||||
|
||||
TRY(System::fcntl(m_fd, F_SETFD, flags));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_receive_timeout(Time timeout)
|
||||
{
|
||||
auto timeout_spec = timeout.to_timespec();
|
||||
return System::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_spec, sizeof(timeout_spec));
|
||||
}
|
||||
|
||||
void PosixSocketHelper::setup_notifier()
|
||||
{
|
||||
if (!m_notifier)
|
||||
m_notifier = Core::Notifier::construct(m_fd, Core::Notifier::Read);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(DeprecatedString const& host, u16 port)
|
||||
{
|
||||
auto ip_address = TRY(resolve_host(host, SocketType::Stream));
|
||||
return connect(SocketAddress { ip_address, port });
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(SocketAddress const& address)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket()));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Stream));
|
||||
socket->m_helper.set_fd(fd);
|
||||
|
||||
TRY(connect_inet(fd, address));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::adopt_fd(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket()));
|
||||
socket->m_helper.set_fd(fd);
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<size_t> PosixSocketHelper::pending_bytes() const
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
int value;
|
||||
TRY(System::ioctl(m_fd, FIONREAD, &value));
|
||||
return static_cast<size_t>(value);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<UDPSocket>> UDPSocket::connect(DeprecatedString const& host, u16 port, Optional<Time> timeout)
|
||||
{
|
||||
auto ip_address = TRY(resolve_host(host, SocketType::Datagram));
|
||||
return connect(SocketAddress { ip_address, port }, timeout);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<UDPSocket>> UDPSocket::connect(SocketAddress const& address, Optional<Time> timeout)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) UDPSocket()));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Datagram));
|
||||
socket->m_helper.set_fd(fd);
|
||||
if (timeout.has_value()) {
|
||||
TRY(socket->m_helper.set_receive_timeout(timeout.value()));
|
||||
}
|
||||
|
||||
TRY(connect_inet(fd, address));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::connect(DeprecatedString const& path, PreventSIGPIPE prevent_sigpipe)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LocalSocket(prevent_sigpipe)));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Local, SocketType::Stream));
|
||||
socket->m_helper.set_fd(fd);
|
||||
|
||||
TRY(connect_local(fd, path));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::adopt_fd(int fd, PreventSIGPIPE prevent_sigpipe)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LocalSocket(prevent_sigpipe)));
|
||||
socket->m_helper.set_fd(fd);
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<int> LocalSocket::receive_fd(int flags)
|
||||
{
|
||||
#if defined(AK_OS_SERENITY)
|
||||
return Core::System::recvfd(m_helper.fd(), flags);
|
||||
#elif defined(AK_OS_LINUX) || defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD) || defined(AK_OS_OPENBSD)
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
char control[CMSG_SPACE(sizeof(int))];
|
||||
} cmsgu {};
|
||||
char c = 0;
|
||||
struct iovec iov {
|
||||
.iov_base = &c,
|
||||
.iov_len = 1,
|
||||
};
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgu.control;
|
||||
msg.msg_controllen = sizeof(cmsgu.control);
|
||||
TRY(Core::System::recvmsg(m_helper.fd(), &msg, 0));
|
||||
|
||||
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
|
||||
return Error::from_string_literal("Malformed message when receiving file descriptor");
|
||||
|
||||
VERIFY(cmsg->cmsg_level == SOL_SOCKET);
|
||||
VERIFY(cmsg->cmsg_type == SCM_RIGHTS);
|
||||
int fd = *((int*)CMSG_DATA(cmsg));
|
||||
|
||||
if (flags & O_CLOEXEC) {
|
||||
auto fd_flags = TRY(Core::System::fcntl(fd, F_GETFD));
|
||||
TRY(Core::System::fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC));
|
||||
}
|
||||
|
||||
return fd;
|
||||
#else
|
||||
(void)flags;
|
||||
return Error::from_string_literal("File descriptor passing not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<void> LocalSocket::send_fd(int fd)
|
||||
{
|
||||
#if defined(AK_OS_SERENITY)
|
||||
return Core::System::sendfd(m_helper.fd(), fd);
|
||||
#elif defined(AK_OS_LINUX) || defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD) || defined(AK_OS_OPENBSD)
|
||||
char c = 'F';
|
||||
struct iovec iov {
|
||||
.iov_base = &c,
|
||||
.iov_len = sizeof(c)
|
||||
};
|
||||
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
char control[CMSG_SPACE(sizeof(int))];
|
||||
} cmsgu {};
|
||||
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgu.control;
|
||||
msg.msg_controllen = sizeof(cmsgu.control);
|
||||
|
||||
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
|
||||
*((int*)CMSG_DATA(cmsg)) = fd;
|
||||
|
||||
TRY(Core::System::sendmsg(m_helper.fd(), &msg, 0));
|
||||
return {};
|
||||
#else
|
||||
(void)fd;
|
||||
return Error::from_string_literal("File descriptor passing not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> LocalSocket::peer_pid() const
|
||||
{
|
||||
#ifdef AK_OS_MACOS
|
||||
pid_t pid;
|
||||
socklen_t pid_size = sizeof(pid);
|
||||
#elif defined(AK_OS_FREEBSD)
|
||||
struct xucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#elif defined(AK_OS_OPENBSD)
|
||||
struct sockpeercred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#else
|
||||
struct ucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#endif
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size));
|
||||
return pid;
|
||||
#elif defined(AK_OS_FREEBSD)
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERCRED, &creds, &creds_size));
|
||||
return creds.cr_pid;
|
||||
#else
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size));
|
||||
return creds.pid;
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<Bytes> LocalSocket::read_without_waiting(Bytes buffer)
|
||||
{
|
||||
return m_helper.read(buffer, MSG_DONTWAIT);
|
||||
}
|
||||
|
||||
Optional<int> LocalSocket::fd() const
|
||||
{
|
||||
if (!is_open())
|
||||
return {};
|
||||
return m_helper.fd();
|
||||
}
|
||||
|
||||
ErrorOr<int> LocalSocket::release_fd()
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
auto fd = m_helper.fd();
|
||||
m_helper.set_fd(-1);
|
||||
return fd;
|
||||
}
|
||||
|
||||
}
|
508
Userland/Libraries/LibCore/Socket.h
Normal file
508
Userland/Libraries/LibCore/Socket.h
Normal file
|
@ -0,0 +1,508 @@
|
|||
/*
|
||||
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/BufferedStream.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/Stream.h>
|
||||
#include <AK/Time.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/SocketAddress.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
/// The Socket class is the base class for all concrete BSD-style socket
|
||||
/// classes. Sockets are non-seekable streams which can be read byte-wise.
|
||||
class Socket : public AK::Stream {
|
||||
public:
|
||||
Socket(Socket&&) = default;
|
||||
Socket& operator=(Socket&&) = default;
|
||||
|
||||
/// Checks how many bytes of data are currently available to read on the
|
||||
/// socket. For datagram-based socket, this is the size of the first
|
||||
/// datagram that can be read. Returns either the amount of bytes, or an
|
||||
/// errno in the case of failure.
|
||||
virtual ErrorOr<size_t> pending_bytes() const = 0;
|
||||
/// Returns whether there's any data that can be immediately read, or an
|
||||
/// errno on failure.
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const = 0;
|
||||
// Sets the blocking mode of the socket. If blocking mode is disabled, reads
|
||||
// will fail with EAGAIN when there's no data available to read, and writes
|
||||
// will fail with EAGAIN when the data cannot be written without blocking
|
||||
// (due to the send buffer being full, for example).
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) = 0;
|
||||
// Sets the close-on-exec mode of the socket. If close-on-exec mode is
|
||||
// enabled, then the socket will be automatically closed by the kernel when
|
||||
// an exec call happens.
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) = 0;
|
||||
|
||||
/// Disables any listening mechanisms that this socket uses.
|
||||
/// Can be called with 'false' when `on_ready_to_read` notifications are no longer needed.
|
||||
/// Conversely, set_notifications_enabled(true) will re-enable notifications.
|
||||
virtual void set_notifications_enabled(bool) { }
|
||||
|
||||
Function<void()> on_ready_to_read;
|
||||
|
||||
enum class PreventSIGPIPE {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
protected:
|
||||
enum class SocketDomain {
|
||||
Local,
|
||||
Inet,
|
||||
};
|
||||
|
||||
enum class SocketType {
|
||||
Stream,
|
||||
Datagram,
|
||||
};
|
||||
|
||||
Socket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: m_prevent_sigpipe(prevent_sigpipe == PreventSIGPIPE::Yes)
|
||||
{
|
||||
}
|
||||
|
||||
static ErrorOr<int> create_fd(SocketDomain, SocketType);
|
||||
// FIXME: This will need to be updated when IPv6 socket arrives. Perhaps a
|
||||
// base class for all address types is appropriate.
|
||||
static ErrorOr<IPv4Address> resolve_host(DeprecatedString const&, SocketType);
|
||||
|
||||
static ErrorOr<void> connect_local(int fd, DeprecatedString const& path);
|
||||
static ErrorOr<void> connect_inet(int fd, SocketAddress const&);
|
||||
|
||||
int default_flags() const
|
||||
{
|
||||
int flags = 0;
|
||||
if (m_prevent_sigpipe)
|
||||
flags |= MSG_NOSIGNAL;
|
||||
return flags;
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_prevent_sigpipe { false };
|
||||
};
|
||||
|
||||
/// A reusable socket maintains state about being connected in addition to
|
||||
/// normal Socket capabilities, and can be reconnected once disconnected.
|
||||
class ReusableSocket : public Socket {
|
||||
public:
|
||||
/// Returns whether the socket is currently connected.
|
||||
virtual bool is_connected() = 0;
|
||||
/// Reconnects the socket to the given host and port. Returns EALREADY if
|
||||
/// is_connected() is true.
|
||||
virtual ErrorOr<void> reconnect(DeprecatedString const& host, u16 port) = 0;
|
||||
/// Connects the socket to the given socket address (IP address + port).
|
||||
/// Returns EALREADY is_connected() is true.
|
||||
virtual ErrorOr<void> reconnect(SocketAddress const&) = 0;
|
||||
};
|
||||
|
||||
class PosixSocketHelper {
|
||||
AK_MAKE_NONCOPYABLE(PosixSocketHelper);
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
PosixSocketHelper(Badge<T>)
|
||||
requires(IsBaseOf<Socket, T>)
|
||||
{
|
||||
}
|
||||
|
||||
PosixSocketHelper(PosixSocketHelper&& other)
|
||||
{
|
||||
operator=(move(other));
|
||||
}
|
||||
|
||||
PosixSocketHelper& operator=(PosixSocketHelper&& other)
|
||||
{
|
||||
m_fd = exchange(other.m_fd, -1);
|
||||
m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
|
||||
m_notifier = move(other.m_notifier);
|
||||
return *this;
|
||||
}
|
||||
|
||||
int fd() const { return m_fd; }
|
||||
void set_fd(int fd) { m_fd = fd; }
|
||||
|
||||
ErrorOr<Bytes> read(Bytes, int flags);
|
||||
ErrorOr<size_t> write(ReadonlyBytes, int flags);
|
||||
|
||||
bool is_eof() const { return !is_open() || m_last_read_was_eof; }
|
||||
bool is_open() const { return m_fd != -1; }
|
||||
void close();
|
||||
|
||||
ErrorOr<size_t> pending_bytes() const;
|
||||
ErrorOr<bool> can_read_without_blocking(int timeout) const;
|
||||
|
||||
ErrorOr<void> set_blocking(bool enabled);
|
||||
ErrorOr<void> set_close_on_exec(bool enabled);
|
||||
ErrorOr<void> set_receive_timeout(Time timeout);
|
||||
|
||||
void setup_notifier();
|
||||
RefPtr<Core::Notifier> notifier() { return m_notifier; }
|
||||
|
||||
private:
|
||||
int m_fd { -1 };
|
||||
bool m_last_read_was_eof { false };
|
||||
RefPtr<Core::Notifier> m_notifier;
|
||||
};
|
||||
|
||||
class TCPSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(DeprecatedString const& host, u16 port);
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(SocketAddress const& address);
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> adopt_fd(int fd);
|
||||
|
||||
TCPSocket(TCPSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
TCPSocket& operator=(TCPSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); };
|
||||
virtual void close() override { m_helper.close(); };
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
|
||||
virtual ~TCPSocket() override { close(); }
|
||||
|
||||
private:
|
||||
TCPSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<TCPSocket> {} };
|
||||
};
|
||||
|
||||
class UDPSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(DeprecatedString const& host, u16 port, Optional<Time> timeout = {});
|
||||
static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(SocketAddress const& address, Optional<Time> timeout = {});
|
||||
|
||||
UDPSocket(UDPSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
UDPSocket& operator=(UDPSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override
|
||||
{
|
||||
auto pending_bytes = TRY(this->pending_bytes());
|
||||
if (pending_bytes > buffer.size()) {
|
||||
// With UDP datagrams, reading a datagram into a buffer that's
|
||||
// smaller than the datagram's size will cause the rest of the
|
||||
// datagram to be discarded. That's not very nice, so let's bail
|
||||
// early, telling the caller that he should allocate a bigger
|
||||
// buffer.
|
||||
return Error::from_errno(EMSGSIZE);
|
||||
}
|
||||
|
||||
return m_helper.read(buffer, default_flags());
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); }
|
||||
virtual void close() override { m_helper.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
|
||||
virtual ~UDPSocket() override { close(); }
|
||||
|
||||
private:
|
||||
UDPSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<UDPSocket> {} };
|
||||
};
|
||||
|
||||
class LocalSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LocalSocket>> connect(DeprecatedString const& path, PreventSIGPIPE = PreventSIGPIPE::No);
|
||||
static ErrorOr<NonnullOwnPtr<LocalSocket>> adopt_fd(int fd, PreventSIGPIPE = PreventSIGPIPE::No);
|
||||
|
||||
LocalSocket(LocalSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
LocalSocket& operator=(LocalSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); }
|
||||
virtual void close() override { m_helper.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
|
||||
ErrorOr<int> receive_fd(int flags);
|
||||
ErrorOr<void> send_fd(int fd);
|
||||
ErrorOr<pid_t> peer_pid() const;
|
||||
ErrorOr<Bytes> read_without_waiting(Bytes buffer);
|
||||
|
||||
/// Release the fd associated with this LocalSocket. After the fd is
|
||||
/// released, the socket will be considered "closed" and all operations done
|
||||
/// on it will fail with ENOTCONN. Fails with ENOTCONN if the socket is
|
||||
/// already closed.
|
||||
ErrorOr<int> release_fd();
|
||||
|
||||
Optional<int> fd() const;
|
||||
RefPtr<Core::Notifier> notifier() { return m_helper.notifier(); }
|
||||
|
||||
virtual ~LocalSocket() { close(); }
|
||||
|
||||
private:
|
||||
LocalSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<LocalSocket> {} };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept SocketLike = IsBaseOf<Socket, T>;
|
||||
|
||||
class BufferedSocketBase : public Socket {
|
||||
public:
|
||||
virtual ErrorOr<StringView> read_line(Bytes buffer) = 0;
|
||||
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
|
||||
virtual ErrorOr<bool> can_read_line() = 0;
|
||||
virtual size_t buffer_size() const = 0;
|
||||
};
|
||||
|
||||
template<SocketLike T>
|
||||
class BufferedSocket final : public BufferedSocketBase {
|
||||
friend BufferedHelper<T>;
|
||||
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BufferedSocket<T>>> create(NonnullOwnPtr<T> stream, size_t buffer_size = 16384)
|
||||
{
|
||||
return BufferedHelper<T>::template create_buffered<BufferedSocket>(move(stream), buffer_size);
|
||||
}
|
||||
|
||||
BufferedSocket(BufferedSocket&& other)
|
||||
: BufferedSocketBase(static_cast<BufferedSocketBase&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
BufferedSocket& operator=(BufferedSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
|
||||
setup_notifier();
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.stream().is_open(); }
|
||||
virtual void close() override { m_helper.stream().close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override
|
||||
{
|
||||
return TRY(m_helper.stream().pending_bytes()) + m_helper.buffered_data_size();
|
||||
}
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.buffered_data_size() > 0 || TRY(m_helper.stream().can_read_without_blocking(timeout)); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.stream().set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.stream().set_close_on_exec(enabled); }
|
||||
virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); }
|
||||
|
||||
virtual ErrorOr<StringView> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
|
||||
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
|
||||
template<size_t N>
|
||||
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
|
||||
virtual ErrorOr<bool> can_read_line() override { return m_helper.can_read_line(); }
|
||||
|
||||
virtual size_t buffer_size() const override { return m_helper.buffer_size(); }
|
||||
|
||||
virtual ~BufferedSocket() override = default;
|
||||
|
||||
private:
|
||||
BufferedSocket(NonnullOwnPtr<T> stream, CircularBuffer buffer)
|
||||
: m_helper(Badge<BufferedSocket<T>> {}, move(stream), move(buffer))
|
||||
{
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
m_helper.stream().on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
BufferedHelper<T> m_helper;
|
||||
};
|
||||
|
||||
using BufferedTCPSocket = BufferedSocket<TCPSocket>;
|
||||
using BufferedUDPSocket = BufferedSocket<UDPSocket>;
|
||||
using BufferedLocalSocket = BufferedSocket<LocalSocket>;
|
||||
|
||||
/// A BasicReusableSocket allows one to use one of the base Core::Stream classes
|
||||
/// as a ReusableSocket. It does not preserve any connection state or options,
|
||||
/// and instead just recreates the stream when reconnecting.
|
||||
template<SocketLike T>
|
||||
class BasicReusableSocket final : public ReusableSocket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(DeprecatedString const& host, u16 port)
|
||||
{
|
||||
return make<BasicReusableSocket<T>>(TRY(T::connect(host, port)));
|
||||
}
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(SocketAddress const& address)
|
||||
{
|
||||
return make<BasicReusableSocket<T>>(TRY(T::connect(address)));
|
||||
}
|
||||
|
||||
virtual bool is_connected() override
|
||||
{
|
||||
return m_socket.is_open();
|
||||
}
|
||||
|
||||
virtual ErrorOr<void> reconnect(DeprecatedString const& host, u16 port) override
|
||||
{
|
||||
if (is_connected())
|
||||
return Error::from_errno(EALREADY);
|
||||
|
||||
m_socket = TRY(T::connect(host, port));
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual ErrorOr<void> reconnect(SocketAddress const& address) override
|
||||
{
|
||||
if (is_connected())
|
||||
return Error::from_errno(EALREADY);
|
||||
|
||||
m_socket = TRY(T::connect(address));
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_socket.read(move(buffer)); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
|
||||
virtual bool is_eof() const override { return m_socket.is_eof(); }
|
||||
virtual bool is_open() const override { return m_socket.is_open(); }
|
||||
virtual void close() override { m_socket.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_socket.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_socket.can_read_without_blocking(timeout); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_socket.set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_socket.set_close_on_exec(enabled); }
|
||||
|
||||
private:
|
||||
BasicReusableSocket(NonnullOwnPtr<T> socket)
|
||||
: m_socket(move(socket))
|
||||
{
|
||||
}
|
||||
|
||||
NonnullOwnPtr<T> m_socket;
|
||||
};
|
||||
|
||||
using ReusableTCPSocket = BasicReusableSocket<TCPSocket>;
|
||||
using ReusableUDPSocket = BasicReusableSocket<UDPSocket>;
|
||||
|
||||
}
|
|
@ -192,401 +192,4 @@ ErrorOr<void> File::truncate(size_t length)
|
|||
return System::ftruncate(m_fd, length);
|
||||
}
|
||||
|
||||
ErrorOr<int> Socket::create_fd(SocketDomain domain, SocketType type)
|
||||
{
|
||||
int socket_domain;
|
||||
switch (domain) {
|
||||
case SocketDomain::Inet:
|
||||
socket_domain = AF_INET;
|
||||
break;
|
||||
case SocketDomain::Local:
|
||||
socket_domain = AF_LOCAL;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
// Let's have a safe default of CLOEXEC. :^)
|
||||
#ifdef SOCK_CLOEXEC
|
||||
return System::socket(socket_domain, socket_type | SOCK_CLOEXEC, 0);
|
||||
#else
|
||||
auto fd = TRY(System::socket(socket_domain, socket_type, 0));
|
||||
TRY(System::fcntl(fd, F_SETFD, FD_CLOEXEC));
|
||||
return fd;
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<IPv4Address> Socket::resolve_host(DeprecatedString const& host, SocketType type)
|
||||
{
|
||||
int socket_type;
|
||||
switch (type) {
|
||||
case SocketType::Stream:
|
||||
socket_type = SOCK_STREAM;
|
||||
break;
|
||||
case SocketType::Datagram:
|
||||
socket_type = SOCK_DGRAM;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
struct addrinfo hints = {};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = socket_type;
|
||||
hints.ai_flags = 0;
|
||||
hints.ai_protocol = 0;
|
||||
|
||||
auto const results = TRY(Core::System::getaddrinfo(host.characters(), nullptr, hints));
|
||||
|
||||
for (auto const& result : results.addresses()) {
|
||||
if (result.ai_family == AF_INET) {
|
||||
auto* socket_address = bit_cast<struct sockaddr_in*>(result.ai_addr);
|
||||
NetworkOrdered<u32> const network_ordered_address { socket_address->sin_addr.s_addr };
|
||||
return IPv4Address { network_ordered_address };
|
||||
}
|
||||
}
|
||||
|
||||
return Error::from_string_literal("Could not resolve to IPv4 address");
|
||||
}
|
||||
|
||||
ErrorOr<void> Socket::connect_local(int fd, DeprecatedString const& path)
|
||||
{
|
||||
auto address = SocketAddress::local(path);
|
||||
auto maybe_sockaddr = address.to_sockaddr_un();
|
||||
if (!maybe_sockaddr.has_value()) {
|
||||
dbgln("Core::Stream::Socket::connect_local: Could not obtain a sockaddr_un");
|
||||
return Error::from_errno(EINVAL);
|
||||
}
|
||||
|
||||
auto addr = maybe_sockaddr.release_value();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
}
|
||||
|
||||
ErrorOr<void> Socket::connect_inet(int fd, SocketAddress const& address)
|
||||
{
|
||||
auto addr = address.to_sockaddr_in();
|
||||
return System::connect(fd, bit_cast<struct sockaddr*>(&addr), sizeof(addr));
|
||||
}
|
||||
|
||||
ErrorOr<Bytes> PosixSocketHelper::read(Bytes buffer, int flags)
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
ssize_t nread = TRY(System::recv(m_fd, buffer.data(), buffer.size(), flags));
|
||||
m_last_read_was_eof = nread == 0;
|
||||
|
||||
// If a socket read is EOF, then no more data can be read from it because
|
||||
// the protocol has disconnected. In this case, we can just disable the
|
||||
// notifier if we have one.
|
||||
if (m_last_read_was_eof && m_notifier)
|
||||
m_notifier->set_enabled(false);
|
||||
|
||||
return buffer.trim(nread);
|
||||
}
|
||||
|
||||
ErrorOr<size_t> PosixSocketHelper::write(ReadonlyBytes buffer, int flags)
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
return TRY(System::send(m_fd, buffer.data(), buffer.size(), flags));
|
||||
}
|
||||
|
||||
void PosixSocketHelper::close()
|
||||
{
|
||||
if (!is_open()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_notifier)
|
||||
m_notifier->set_enabled(false);
|
||||
|
||||
ErrorOr<void> result;
|
||||
do {
|
||||
result = System::close(m_fd);
|
||||
} while (result.is_error() && result.error().code() == EINTR);
|
||||
|
||||
VERIFY(!result.is_error());
|
||||
m_fd = -1;
|
||||
}
|
||||
|
||||
ErrorOr<bool> PosixSocketHelper::can_read_without_blocking(int timeout) const
|
||||
{
|
||||
struct pollfd the_fd = { .fd = m_fd, .events = POLLIN, .revents = 0 };
|
||||
|
||||
ErrorOr<int> result { 0 };
|
||||
do {
|
||||
result = Core::System::poll({ &the_fd, 1 }, timeout);
|
||||
} while (result.is_error() && result.error().code() == EINTR);
|
||||
|
||||
if (result.is_error())
|
||||
return result.release_error();
|
||||
|
||||
return (the_fd.revents & POLLIN) > 0;
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_blocking(bool enabled)
|
||||
{
|
||||
int value = enabled ? 0 : 1;
|
||||
return System::ioctl(m_fd, FIONBIO, &value);
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_close_on_exec(bool enabled)
|
||||
{
|
||||
int flags = TRY(System::fcntl(m_fd, F_GETFD));
|
||||
|
||||
if (enabled)
|
||||
flags |= FD_CLOEXEC;
|
||||
else
|
||||
flags &= ~FD_CLOEXEC;
|
||||
|
||||
TRY(System::fcntl(m_fd, F_SETFD, flags));
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> PosixSocketHelper::set_receive_timeout(Time timeout)
|
||||
{
|
||||
auto timeout_spec = timeout.to_timespec();
|
||||
return System::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_spec, sizeof(timeout_spec));
|
||||
}
|
||||
|
||||
void PosixSocketHelper::setup_notifier()
|
||||
{
|
||||
if (!m_notifier)
|
||||
m_notifier = Core::Notifier::construct(m_fd, Core::Notifier::Read);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(DeprecatedString const& host, u16 port)
|
||||
{
|
||||
auto ip_address = TRY(resolve_host(host, SocketType::Stream));
|
||||
return connect(SocketAddress { ip_address, port });
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(SocketAddress const& address)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket()));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Stream));
|
||||
socket->m_helper.set_fd(fd);
|
||||
|
||||
TRY(connect_inet(fd, address));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::adopt_fd(int fd)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TCPSocket()));
|
||||
socket->m_helper.set_fd(fd);
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<size_t> PosixSocketHelper::pending_bytes() const
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
int value;
|
||||
TRY(System::ioctl(m_fd, FIONREAD, &value));
|
||||
return static_cast<size_t>(value);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<UDPSocket>> UDPSocket::connect(DeprecatedString const& host, u16 port, Optional<Time> timeout)
|
||||
{
|
||||
auto ip_address = TRY(resolve_host(host, SocketType::Datagram));
|
||||
return connect(SocketAddress { ip_address, port }, timeout);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<UDPSocket>> UDPSocket::connect(SocketAddress const& address, Optional<Time> timeout)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) UDPSocket()));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Datagram));
|
||||
socket->m_helper.set_fd(fd);
|
||||
if (timeout.has_value()) {
|
||||
TRY(socket->m_helper.set_receive_timeout(timeout.value()));
|
||||
}
|
||||
|
||||
TRY(connect_inet(fd, address));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::connect(DeprecatedString const& path, PreventSIGPIPE prevent_sigpipe)
|
||||
{
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LocalSocket(prevent_sigpipe)));
|
||||
|
||||
auto fd = TRY(create_fd(SocketDomain::Local, SocketType::Stream));
|
||||
socket->m_helper.set_fd(fd);
|
||||
|
||||
TRY(connect_local(fd, path));
|
||||
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<LocalSocket>> LocalSocket::adopt_fd(int fd, PreventSIGPIPE prevent_sigpipe)
|
||||
{
|
||||
if (fd < 0) {
|
||||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
auto socket = TRY(adopt_nonnull_own_or_enomem(new (nothrow) LocalSocket(prevent_sigpipe)));
|
||||
socket->m_helper.set_fd(fd);
|
||||
socket->setup_notifier();
|
||||
return socket;
|
||||
}
|
||||
|
||||
ErrorOr<int> LocalSocket::receive_fd(int flags)
|
||||
{
|
||||
#if defined(AK_OS_SERENITY)
|
||||
return Core::System::recvfd(m_helper.fd(), flags);
|
||||
#elif defined(AK_OS_LINUX) || defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD) || defined(AK_OS_OPENBSD)
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
char control[CMSG_SPACE(sizeof(int))];
|
||||
} cmsgu {};
|
||||
char c = 0;
|
||||
struct iovec iov {
|
||||
.iov_base = &c,
|
||||
.iov_len = 1,
|
||||
};
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgu.control;
|
||||
msg.msg_controllen = sizeof(cmsgu.control);
|
||||
TRY(Core::System::recvmsg(m_helper.fd(), &msg, 0));
|
||||
|
||||
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
|
||||
if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
|
||||
return Error::from_string_literal("Malformed message when receiving file descriptor");
|
||||
|
||||
VERIFY(cmsg->cmsg_level == SOL_SOCKET);
|
||||
VERIFY(cmsg->cmsg_type == SCM_RIGHTS);
|
||||
int fd = *((int*)CMSG_DATA(cmsg));
|
||||
|
||||
if (flags & O_CLOEXEC) {
|
||||
auto fd_flags = TRY(Core::System::fcntl(fd, F_GETFD));
|
||||
TRY(Core::System::fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC));
|
||||
}
|
||||
|
||||
return fd;
|
||||
#else
|
||||
(void)flags;
|
||||
return Error::from_string_literal("File descriptor passing not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<void> LocalSocket::send_fd(int fd)
|
||||
{
|
||||
#if defined(AK_OS_SERENITY)
|
||||
return Core::System::sendfd(m_helper.fd(), fd);
|
||||
#elif defined(AK_OS_LINUX) || defined(AK_OS_MACOS) || defined(AK_OS_FREEBSD) || defined(AK_OS_OPENBSD)
|
||||
char c = 'F';
|
||||
struct iovec iov {
|
||||
.iov_base = &c,
|
||||
.iov_len = sizeof(c)
|
||||
};
|
||||
|
||||
union {
|
||||
struct cmsghdr cmsghdr;
|
||||
char control[CMSG_SPACE(sizeof(int))];
|
||||
} cmsgu {};
|
||||
|
||||
struct msghdr msg = {};
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgu.control;
|
||||
msg.msg_controllen = sizeof(cmsgu.control);
|
||||
|
||||
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
|
||||
*((int*)CMSG_DATA(cmsg)) = fd;
|
||||
|
||||
TRY(Core::System::sendmsg(m_helper.fd(), &msg, 0));
|
||||
return {};
|
||||
#else
|
||||
(void)fd;
|
||||
return Error::from_string_literal("File descriptor passing not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<pid_t> LocalSocket::peer_pid() const
|
||||
{
|
||||
#ifdef AK_OS_MACOS
|
||||
pid_t pid;
|
||||
socklen_t pid_size = sizeof(pid);
|
||||
#elif defined(AK_OS_FREEBSD)
|
||||
struct xucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#elif defined(AK_OS_OPENBSD)
|
||||
struct sockpeercred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#else
|
||||
struct ucred creds = {};
|
||||
socklen_t creds_size = sizeof(creds);
|
||||
#endif
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERPID, &pid, &pid_size));
|
||||
return pid;
|
||||
#elif defined(AK_OS_FREEBSD)
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_LOCAL, LOCAL_PEERCRED, &creds, &creds_size));
|
||||
return creds.cr_pid;
|
||||
#else
|
||||
TRY(System::getsockopt(m_helper.fd(), SOL_SOCKET, SO_PEERCRED, &creds, &creds_size));
|
||||
return creds.pid;
|
||||
#endif
|
||||
}
|
||||
|
||||
ErrorOr<Bytes> LocalSocket::read_without_waiting(Bytes buffer)
|
||||
{
|
||||
return m_helper.read(buffer, MSG_DONTWAIT);
|
||||
}
|
||||
|
||||
Optional<int> LocalSocket::fd() const
|
||||
{
|
||||
if (!is_open())
|
||||
return {};
|
||||
return m_helper.fd();
|
||||
}
|
||||
|
||||
ErrorOr<int> LocalSocket::release_fd()
|
||||
{
|
||||
if (!is_open()) {
|
||||
return Error::from_errno(ENOTCONN);
|
||||
}
|
||||
|
||||
auto fd = m_helper.fd();
|
||||
m_helper.set_fd(-1);
|
||||
return fd;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,93 +28,6 @@
|
|||
|
||||
namespace Core::Stream {
|
||||
|
||||
enum class PreventSIGPIPE {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
/// The Socket class is the base class for all concrete BSD-style socket
|
||||
/// classes. Sockets are non-seekable streams which can be read byte-wise.
|
||||
class Socket : public AK::Stream {
|
||||
public:
|
||||
Socket(Socket&&) = default;
|
||||
Socket& operator=(Socket&&) = default;
|
||||
|
||||
/// Checks how many bytes of data are currently available to read on the
|
||||
/// socket. For datagram-based socket, this is the size of the first
|
||||
/// datagram that can be read. Returns either the amount of bytes, or an
|
||||
/// errno in the case of failure.
|
||||
virtual ErrorOr<size_t> pending_bytes() const = 0;
|
||||
/// Returns whether there's any data that can be immediately read, or an
|
||||
/// errno on failure.
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const = 0;
|
||||
// Sets the blocking mode of the socket. If blocking mode is disabled, reads
|
||||
// will fail with EAGAIN when there's no data available to read, and writes
|
||||
// will fail with EAGAIN when the data cannot be written without blocking
|
||||
// (due to the send buffer being full, for example).
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) = 0;
|
||||
// Sets the close-on-exec mode of the socket. If close-on-exec mode is
|
||||
// enabled, then the socket will be automatically closed by the kernel when
|
||||
// an exec call happens.
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) = 0;
|
||||
|
||||
/// Disables any listening mechanisms that this socket uses.
|
||||
/// Can be called with 'false' when `on_ready_to_read` notifications are no longer needed.
|
||||
/// Conversely, set_notifications_enabled(true) will re-enable notifications.
|
||||
virtual void set_notifications_enabled(bool) { }
|
||||
|
||||
Function<void()> on_ready_to_read;
|
||||
|
||||
protected:
|
||||
enum class SocketDomain {
|
||||
Local,
|
||||
Inet,
|
||||
};
|
||||
|
||||
enum class SocketType {
|
||||
Stream,
|
||||
Datagram,
|
||||
};
|
||||
|
||||
Socket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: m_prevent_sigpipe(prevent_sigpipe == PreventSIGPIPE::Yes)
|
||||
{
|
||||
}
|
||||
|
||||
static ErrorOr<int> create_fd(SocketDomain, SocketType);
|
||||
// FIXME: This will need to be updated when IPv6 socket arrives. Perhaps a
|
||||
// base class for all address types is appropriate.
|
||||
static ErrorOr<IPv4Address> resolve_host(DeprecatedString const&, SocketType);
|
||||
|
||||
static ErrorOr<void> connect_local(int fd, DeprecatedString const& path);
|
||||
static ErrorOr<void> connect_inet(int fd, SocketAddress const&);
|
||||
|
||||
int default_flags() const
|
||||
{
|
||||
int flags = 0;
|
||||
if (m_prevent_sigpipe)
|
||||
flags |= MSG_NOSIGNAL;
|
||||
return flags;
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_prevent_sigpipe { false };
|
||||
};
|
||||
|
||||
/// A reusable socket maintains state about being connected in addition to
|
||||
/// normal Socket capabilities, and can be reconnected once disconnected.
|
||||
class ReusableSocket : public Socket {
|
||||
public:
|
||||
/// Returns whether the socket is currently connected.
|
||||
virtual bool is_connected() = 0;
|
||||
/// Reconnects the socket to the given host and port. Returns EALREADY if
|
||||
/// is_connected() is true.
|
||||
virtual ErrorOr<void> reconnect(DeprecatedString const& host, u16 port) = 0;
|
||||
/// Connects the socket to the given socket address (IP address + port).
|
||||
/// Returns EALREADY is_connected() is true.
|
||||
virtual ErrorOr<void> reconnect(SocketAddress const&) = 0;
|
||||
};
|
||||
|
||||
// Concrete classes.
|
||||
|
||||
enum class OpenMode : unsigned {
|
||||
|
@ -204,407 +117,6 @@ private:
|
|||
ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
|
||||
};
|
||||
|
||||
class PosixSocketHelper {
|
||||
AK_MAKE_NONCOPYABLE(PosixSocketHelper);
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
PosixSocketHelper(Badge<T>)
|
||||
requires(IsBaseOf<Socket, T>)
|
||||
{
|
||||
}
|
||||
|
||||
PosixSocketHelper(PosixSocketHelper&& other)
|
||||
{
|
||||
operator=(move(other));
|
||||
}
|
||||
|
||||
PosixSocketHelper& operator=(PosixSocketHelper&& other)
|
||||
{
|
||||
m_fd = exchange(other.m_fd, -1);
|
||||
m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
|
||||
m_notifier = move(other.m_notifier);
|
||||
return *this;
|
||||
}
|
||||
|
||||
int fd() const { return m_fd; }
|
||||
void set_fd(int fd) { m_fd = fd; }
|
||||
|
||||
ErrorOr<Bytes> read(Bytes, int flags);
|
||||
ErrorOr<size_t> write(ReadonlyBytes, int flags);
|
||||
|
||||
bool is_eof() const { return !is_open() || m_last_read_was_eof; }
|
||||
bool is_open() const { return m_fd != -1; }
|
||||
void close();
|
||||
|
||||
ErrorOr<size_t> pending_bytes() const;
|
||||
ErrorOr<bool> can_read_without_blocking(int timeout) const;
|
||||
|
||||
ErrorOr<void> set_blocking(bool enabled);
|
||||
ErrorOr<void> set_close_on_exec(bool enabled);
|
||||
ErrorOr<void> set_receive_timeout(Time timeout);
|
||||
|
||||
void setup_notifier();
|
||||
RefPtr<Core::Notifier> notifier() { return m_notifier; }
|
||||
|
||||
private:
|
||||
int m_fd { -1 };
|
||||
bool m_last_read_was_eof { false };
|
||||
RefPtr<Core::Notifier> m_notifier;
|
||||
};
|
||||
|
||||
class TCPSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(DeprecatedString const& host, u16 port);
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(SocketAddress const& address);
|
||||
static ErrorOr<NonnullOwnPtr<TCPSocket>> adopt_fd(int fd);
|
||||
|
||||
TCPSocket(TCPSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
TCPSocket& operator=(TCPSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); };
|
||||
virtual void close() override { m_helper.close(); };
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
|
||||
virtual ~TCPSocket() override { close(); }
|
||||
|
||||
private:
|
||||
TCPSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<TCPSocket> {} };
|
||||
};
|
||||
|
||||
class UDPSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(DeprecatedString const& host, u16 port, Optional<Time> timeout = {});
|
||||
static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(SocketAddress const& address, Optional<Time> timeout = {});
|
||||
|
||||
UDPSocket(UDPSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
UDPSocket& operator=(UDPSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override
|
||||
{
|
||||
auto pending_bytes = TRY(this->pending_bytes());
|
||||
if (pending_bytes > buffer.size()) {
|
||||
// With UDP datagrams, reading a datagram into a buffer that's
|
||||
// smaller than the datagram's size will cause the rest of the
|
||||
// datagram to be discarded. That's not very nice, so let's bail
|
||||
// early, telling the caller that he should allocate a bigger
|
||||
// buffer.
|
||||
return Error::from_errno(EMSGSIZE);
|
||||
}
|
||||
|
||||
return m_helper.read(buffer, default_flags());
|
||||
}
|
||||
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); }
|
||||
virtual void close() override { m_helper.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
|
||||
virtual ~UDPSocket() override { close(); }
|
||||
|
||||
private:
|
||||
UDPSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<UDPSocket> {} };
|
||||
};
|
||||
|
||||
class LocalSocket final : public Socket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LocalSocket>> connect(DeprecatedString const& path, PreventSIGPIPE = PreventSIGPIPE::No);
|
||||
static ErrorOr<NonnullOwnPtr<LocalSocket>> adopt_fd(int fd, PreventSIGPIPE = PreventSIGPIPE::No);
|
||||
|
||||
LocalSocket(LocalSocket&& other)
|
||||
: Socket(static_cast<Socket&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
LocalSocket& operator=(LocalSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
if (is_open())
|
||||
setup_notifier();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer, default_flags()); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer, default_flags()); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.is_open(); }
|
||||
virtual void close() override { m_helper.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
|
||||
virtual void set_notifications_enabled(bool enabled) override
|
||||
{
|
||||
if (auto notifier = m_helper.notifier())
|
||||
notifier->set_enabled(enabled);
|
||||
}
|
||||
|
||||
ErrorOr<int> receive_fd(int flags);
|
||||
ErrorOr<void> send_fd(int fd);
|
||||
ErrorOr<pid_t> peer_pid() const;
|
||||
ErrorOr<Bytes> read_without_waiting(Bytes buffer);
|
||||
|
||||
/// Release the fd associated with this LocalSocket. After the fd is
|
||||
/// released, the socket will be considered "closed" and all operations done
|
||||
/// on it will fail with ENOTCONN. Fails with ENOTCONN if the socket is
|
||||
/// already closed.
|
||||
ErrorOr<int> release_fd();
|
||||
|
||||
Optional<int> fd() const;
|
||||
RefPtr<Core::Notifier> notifier() { return m_helper.notifier(); }
|
||||
|
||||
virtual ~LocalSocket() { close(); }
|
||||
|
||||
private:
|
||||
LocalSocket(PreventSIGPIPE prevent_sigpipe = PreventSIGPIPE::No)
|
||||
: Socket(prevent_sigpipe)
|
||||
{
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
VERIFY(is_open());
|
||||
|
||||
m_helper.setup_notifier();
|
||||
m_helper.notifier()->on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
PosixSocketHelper m_helper { Badge<LocalSocket> {} };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept SocketLike = IsBaseOf<Socket, T>;
|
||||
|
||||
class BufferedSocketBase : public Socket {
|
||||
public:
|
||||
virtual ErrorOr<StringView> read_line(Bytes buffer) = 0;
|
||||
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
|
||||
virtual ErrorOr<bool> can_read_line() = 0;
|
||||
virtual size_t buffer_size() const = 0;
|
||||
};
|
||||
|
||||
template<SocketLike T>
|
||||
class BufferedSocket final : public BufferedSocketBase {
|
||||
friend BufferedHelper<T>;
|
||||
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BufferedSocket<T>>> create(NonnullOwnPtr<T> stream, size_t buffer_size = 16384)
|
||||
{
|
||||
return BufferedHelper<T>::template create_buffered<BufferedSocket>(move(stream), buffer_size);
|
||||
}
|
||||
|
||||
BufferedSocket(BufferedSocket&& other)
|
||||
: BufferedSocketBase(static_cast<BufferedSocketBase&&>(other))
|
||||
, m_helper(move(other.m_helper))
|
||||
{
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
BufferedSocket& operator=(BufferedSocket&& other)
|
||||
{
|
||||
Socket::operator=(static_cast<Socket&&>(other));
|
||||
m_helper = move(other.m_helper);
|
||||
|
||||
setup_notifier();
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
|
||||
virtual bool is_eof() const override { return m_helper.is_eof(); }
|
||||
virtual bool is_open() const override { return m_helper.stream().is_open(); }
|
||||
virtual void close() override { m_helper.stream().close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override
|
||||
{
|
||||
return TRY(m_helper.stream().pending_bytes()) + m_helper.buffered_data_size();
|
||||
}
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.buffered_data_size() > 0 || TRY(m_helper.stream().can_read_without_blocking(timeout)); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.stream().set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.stream().set_close_on_exec(enabled); }
|
||||
virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); }
|
||||
|
||||
virtual ErrorOr<StringView> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
|
||||
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
|
||||
template<size_t N>
|
||||
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
|
||||
virtual ErrorOr<bool> can_read_line() override { return m_helper.can_read_line(); }
|
||||
|
||||
virtual size_t buffer_size() const override { return m_helper.buffer_size(); }
|
||||
|
||||
virtual ~BufferedSocket() override = default;
|
||||
|
||||
private:
|
||||
BufferedSocket(NonnullOwnPtr<T> stream, CircularBuffer buffer)
|
||||
: m_helper(Badge<BufferedSocket<T>> {}, move(stream), move(buffer))
|
||||
{
|
||||
setup_notifier();
|
||||
}
|
||||
|
||||
void setup_notifier()
|
||||
{
|
||||
m_helper.stream().on_ready_to_read = [this] {
|
||||
if (on_ready_to_read)
|
||||
on_ready_to_read();
|
||||
};
|
||||
}
|
||||
|
||||
BufferedHelper<T> m_helper;
|
||||
};
|
||||
|
||||
using BufferedFile = BufferedSeekable<File>;
|
||||
using BufferedTCPSocket = BufferedSocket<TCPSocket>;
|
||||
using BufferedUDPSocket = BufferedSocket<UDPSocket>;
|
||||
using BufferedLocalSocket = BufferedSocket<LocalSocket>;
|
||||
|
||||
/// A BasicReusableSocket allows one to use one of the base Core::Stream classes
|
||||
/// as a ReusableSocket. It does not preserve any connection state or options,
|
||||
/// and instead just recreates the stream when reconnecting.
|
||||
template<SocketLike T>
|
||||
class BasicReusableSocket final : public ReusableSocket {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(DeprecatedString const& host, u16 port)
|
||||
{
|
||||
return make<BasicReusableSocket<T>>(TRY(T::connect(host, port)));
|
||||
}
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(SocketAddress const& address)
|
||||
{
|
||||
return make<BasicReusableSocket<T>>(TRY(T::connect(address)));
|
||||
}
|
||||
|
||||
virtual bool is_connected() override
|
||||
{
|
||||
return m_socket.is_open();
|
||||
}
|
||||
|
||||
virtual ErrorOr<void> reconnect(DeprecatedString const& host, u16 port) override
|
||||
{
|
||||
if (is_connected())
|
||||
return Error::from_errno(EALREADY);
|
||||
|
||||
m_socket = TRY(T::connect(host, port));
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual ErrorOr<void> reconnect(SocketAddress const& address) override
|
||||
{
|
||||
if (is_connected())
|
||||
return Error::from_errno(EALREADY);
|
||||
|
||||
m_socket = TRY(T::connect(address));
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_socket.read(move(buffer)); }
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
|
||||
virtual bool is_eof() const override { return m_socket.is_eof(); }
|
||||
virtual bool is_open() const override { return m_socket.is_open(); }
|
||||
virtual void close() override { m_socket.close(); }
|
||||
virtual ErrorOr<size_t> pending_bytes() const override { return m_socket.pending_bytes(); }
|
||||
virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_socket.can_read_without_blocking(timeout); }
|
||||
virtual ErrorOr<void> set_blocking(bool enabled) override { return m_socket.set_blocking(enabled); }
|
||||
virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_socket.set_close_on_exec(enabled); }
|
||||
|
||||
private:
|
||||
BasicReusableSocket(NonnullOwnPtr<T> socket)
|
||||
: m_socket(move(socket))
|
||||
{
|
||||
}
|
||||
|
||||
NonnullOwnPtr<T> m_socket;
|
||||
};
|
||||
|
||||
using ReusableTCPSocket = BasicReusableSocket<TCPSocket>;
|
||||
using ReusableUDPSocket = BasicReusableSocket<UDPSocket>;
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include "SystemServerTakeover.h"
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/System.h>
|
||||
|
||||
namespace Core {
|
||||
|
@ -34,7 +35,7 @@ static void parse_sockets_from_system_server()
|
|||
unsetenv(socket_takeover);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Core::Stream::LocalSocket>> take_over_socket_from_system_server(DeprecatedString const& socket_path)
|
||||
ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(DeprecatedString const& socket_path)
|
||||
{
|
||||
if (!s_overtaken_sockets_parsed)
|
||||
parse_sockets_from_system_server();
|
||||
|
@ -57,7 +58,7 @@ ErrorOr<NonnullOwnPtr<Core::Stream::LocalSocket>> take_over_socket_from_system_s
|
|||
if (!S_ISSOCK(stat.st_mode))
|
||||
return Error::from_string_literal("The fd we got from SystemServer is not a socket");
|
||||
|
||||
auto socket = TRY(Core::Stream::LocalSocket::adopt_fd(fd));
|
||||
auto socket = TRY(Core::LocalSocket::adopt_fd(fd));
|
||||
// It had to be !CLOEXEC for obvious reasons, but we
|
||||
// don't need it to be !CLOEXEC anymore, so set the
|
||||
// CLOEXEC flag now.
|
||||
|
|
|
@ -10,6 +10,6 @@
|
|||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Core::Stream::LocalSocket>> take_over_socket_from_system_server(DeprecatedString const& socket_path = {});
|
||||
ErrorOr<NonnullOwnPtr<Core::LocalSocket>> take_over_socket_from_system_server(DeprecatedString const& socket_path = {});
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <AK/IPv4Address.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibCore/TCPServer.h>
|
||||
|
||||
|
@ -74,7 +75,7 @@ ErrorOr<void> TCPServer::set_blocking(bool blocking)
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::TCPSocket>> TCPServer::accept()
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPServer::accept()
|
||||
{
|
||||
VERIFY(m_listening);
|
||||
sockaddr_in in;
|
||||
|
@ -85,7 +86,7 @@ ErrorOr<NonnullOwnPtr<Stream::TCPSocket>> TCPServer::accept()
|
|||
int accepted_fd = TRY(Core::System::accept(m_fd, (sockaddr*)&in, &in_size));
|
||||
#endif
|
||||
|
||||
auto socket = TRY(Stream::TCPSocket::adopt_fd(accepted_fd));
|
||||
auto socket = TRY(TCPSocket::adopt_fd(accepted_fd));
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
// FIXME: Ideally, we should let the caller decide whether it wants the
|
||||
|
|
|
@ -29,7 +29,7 @@ public:
|
|||
ErrorOr<void> listen(IPv4Address const& address, u16 port, AllowAddressReuse = AllowAddressReuse::No);
|
||||
ErrorOr<void> set_blocking(bool blocking);
|
||||
|
||||
ErrorOr<NonnullOwnPtr<Stream::TCPSocket>> accept();
|
||||
ErrorOr<NonnullOwnPtr<TCPSocket>> accept();
|
||||
|
||||
Optional<IPv4Address> local_address() const;
|
||||
Optional<u16> local_port() const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue