Skip to content

Commit

Permalink
posix: Support partial reads in netlink recvMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke committed Feb 21, 2024
1 parent 91224a7 commit 3fa00d2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions posix/subsystem/src/nl-socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct Packet {

// The actual octet data that the packet consists of.
std::vector<char> buffer;

size_t offset = 0;
};

struct OpenFile : File {
Expand Down Expand Up @@ -127,15 +129,18 @@ struct OpenFile : File {
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 +154,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 3fa00d2

Please sign in to comment.