2024-01-02 20:14:18 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-03 02:55:11 +02:00
|
|
|
#include <LibIPC/Decoder.h>
|
2026-01-21 22:07:21 +01:00
|
|
|
#include <LibIPC/Limits.h>
|
2024-01-02 20:14:18 -05:00
|
|
|
#include <LibIPC/Message.h>
|
|
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
2024-01-02 20:27:29 -05:00
|
|
|
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 fd)
|
|
|
|
|
{
|
|
|
|
|
auto auto_fd = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AutoCloseFileDescriptor(fd)));
|
|
|
|
|
TRY(m_fds.try_append(move(auto_fd)));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 09:47:15 -04:00
|
|
|
ErrorOr<void> MessageBuffer::extend(MessageBuffer&& buffer)
|
|
|
|
|
{
|
|
|
|
|
TRY(m_data.try_extend(move(buffer.m_data)));
|
|
|
|
|
TRY(m_fds.try_extend(move(buffer.m_fds)));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 15:47:33 -06:00
|
|
|
ErrorOr<void> MessageBuffer::transfer_message(Transport& transport)
|
2024-01-02 20:14:18 -05:00
|
|
|
{
|
2026-01-21 22:07:21 +01:00
|
|
|
// These VERIFYs catch bugs where we try to send messages that exceed IPC limits.
|
|
|
|
|
// If we hit these, we have a bug in our encoding code.
|
|
|
|
|
VERIFY(m_data.size() <= MAX_MESSAGE_PAYLOAD_SIZE);
|
|
|
|
|
VERIFY(m_fds.size() <= MAX_MESSAGE_FD_COUNT);
|
2024-01-02 20:14:18 -05:00
|
|
|
|
2025-04-08 04:55:50 +02:00
|
|
|
transport.post_message(m_data, m_fds);
|
2024-01-02 20:14:18 -05:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|