Skip to content

Commit

Permalink
posix: Support truncation in netlink recvMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke committed Feb 18, 2024
1 parent 91224a7 commit ec3815d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions posix/subsystem/src/nl-socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -149,7 +151,8 @@ struct OpenFile : File {
ctrl.write<struct ucred>(creds);
}

_recvQueue.pop_front();
if(packet->offset == packet->buffer.size())
_recvQueue.pop_front();
co_return RecvData{ctrl.buffer(), size, sizeof(struct sockaddr_nl), 0};
}

Expand Down

0 comments on commit ec3815d

Please sign in to comment.