From ec3815d3574b5425f83f104b0d5d17a21ed47b58 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Sun, 18 Feb 2024 01:08:31 +0100 Subject: [PATCH] posix: Support truncation in netlink recvMsg --- posix/subsystem/src/nl-socket.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/posix/subsystem/src/nl-socket.cpp b/posix/subsystem/src/nl-socket.cpp index 7ee5fcbdf..8df9491fa 100644 --- a/posix/subsystem/src/nl-socket.cpp +++ b/posix/subsystem/src/nl-socket.cpp @@ -123,19 +123,21 @@ struct OpenFile : File { while(_recvQueue.empty()) co_await _statusBell.async_wait(); - // TODO: Truncate packets (for SOCK_DGRAM) here. auto packet = &_recvQueue.front(); auto size = packet->buffer.size(); - assert(max_length >= size); - memcpy(data, packet->buffer.data(), size); - - struct sockaddr_nl sa; - memset(&sa, 0, sizeof(struct sockaddr_nl)); - sa.nl_family = AF_NETLINK; - sa.nl_pid = packet->senderPort; - sa.nl_groups = packet->group ? (1 << (packet->group - 1)) : 0; - memcpy(addr_ptr, &sa, sizeof(struct sockaddr_nl)); + auto chunk = std::min(packet->buffer.size() - packet->offset, max_length); + memcpy(data, packet->buffer.data() + packet->offset, chunk); + packet->offset += chunk; + + if(addr_ptr) { + struct sockaddr_nl sa; + memset(&sa, 0, sizeof(struct sockaddr_nl)); + sa.nl_family = AF_NETLINK; + sa.nl_pid = packet->senderPort; + sa.nl_groups = packet->group ? (1 << (packet->group - 1)) : 0; + memcpy(addr_ptr, &sa, sizeof(struct sockaddr_nl)); + } protocols::fs::CtrlBuilder ctrl{max_ctrl_length}; @@ -149,7 +151,8 @@ struct OpenFile : File { ctrl.write(creds); } - _recvQueue.pop_front(); + if(packet->offset == packet->buffer.size()) + _recvQueue.pop_front(); co_return RecvData{ctrl.buffer(), size, sizeof(struct sockaddr_nl), 0}; }