ladybird/Libraries/LibIPC/MessageWindows.cpp
Aliaksandr Kalenik da6b928909 LibIPC+LibWeb: Introduce IPC::Attachment abstraction
Replace IPC::File / AutoCloseFileDescriptor / MessageFileType in
the IPC message pipeline with a new IPC::Attachment class. This
wraps a file descriptor transferred alongside IPC messages, and
provides a clean extension point for platform-specific transport
mechanisms (e.g., Mach ports on macOS) that will be introduced later.
2026-03-13 20:22:50 +01:00

81 lines
2.4 KiB
C++

/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2025, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteReader.h>
#include <LibCore/System.h>
#include <LibIPC/HandleType.h>
#include <LibIPC/Message.h>
#include <AK/Windows.h>
namespace IPC {
using MessageSizeType = u32;
MessageBuffer::MessageBuffer()
{
}
ErrorOr<void> MessageBuffer::extend_data_capacity(size_t capacity)
{
TRY(m_data.try_ensure_capacity(m_data.size() + capacity));
return {};
}
ErrorOr<void> MessageBuffer::append_data(u8 const* values, size_t count)
{
TRY(m_data.try_append(values, count));
return {};
}
ErrorOr<void> MessageBuffer::append_file_descriptor(int handle)
{
TRY(m_handle_offsets.try_append(m_data.size()));
if (Core::System::is_socket(handle)) {
auto type = HandleType::Socket;
TRY(m_data.try_append(to_underlying(type)));
// The handle will be duplicated and WSAPROTOCOL_INFO will be filled later in TransportSocketWindows::transfer.
// It can't be duplicated here because it requires peer process pid, which only TransportSocketWindows knows about.
WSAPROTOCOL_INFOW pi = {};
static_assert(sizeof(pi) >= sizeof(int));
ByteReader::store(reinterpret_cast<u8*>(&pi), handle);
TRY(m_data.try_append(reinterpret_cast<u8*>(&pi), sizeof(pi)));
} else {
auto type = HandleType::Generic;
TRY(m_data.try_append(to_underlying(type)));
// The handle will be overwritten by a duplicate handle later in TransportSocketWindows::transfer (for the same reason).
TRY(m_data.try_append(reinterpret_cast<u8*>(&handle), sizeof(handle)));
}
return {};
}
ErrorOr<void> MessageBuffer::append_attachment(Attachment attachment)
{
return append_file_descriptor(attachment.to_fd());
}
ErrorOr<void> MessageBuffer::extend(MessageBuffer&& buffer)
{
TRY(m_data.try_extend(move(buffer.m_data)));
TRY(m_attachments.try_extend(move(buffer.m_attachments)));
TRY(m_handle_offsets.try_extend(move(buffer.m_handle_offsets)));
return {};
}
ErrorOr<void> MessageBuffer::transfer_message(Transport& transport)
{
Checked<MessageSizeType> checked_message_size { m_data.size() };
if (checked_message_size.has_overflow())
return Error::from_string_literal("Message is too large for IPC encoding");
TRY(transport.transfer_message(m_data, m_handle_offsets));
return {};
}
}