Implement add/remove membership for Netlink sockets

Change-Id: Ib98f14b1d758ee7b4d464b359d4792c3ca7027b0
Test: with other b/169681573 changes
Bug: 169681573
This commit is contained in:
Tomasz Wasilczyk
2021-07-19 14:48:05 -07:00
parent abf7990228
commit 7b2bb9cc3c
2 changed files with 36 additions and 0 deletions

View File

@@ -162,6 +162,26 @@ pollfd Socket::preparePoll(short events) {
return {mFd.get(), events, 0};
}
bool Socket::addMembership(unsigned group) {
const auto res =
setsockopt(mFd.get(), SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
if (res < 0) {
PLOG(ERROR) << "Failed joining multicast group " << group;
return false;
}
return true;
}
bool Socket::dropMembership(unsigned group) {
const auto res =
setsockopt(mFd.get(), SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, &group, sizeof(group));
if (res < 0) {
PLOG(ERROR) << "Failed leaving multicast group " << group;
return false;
}
return true;
}
Socket::receive_iterator::receive_iterator(Socket& socket, bool end)
: mSocket(socket), mIsEnd(end) {
if (!end) receive();

View File

@@ -191,6 +191,22 @@ class Socket {
*/
pollfd preparePoll(short events = 0);
/**
* Join a multicast group.
*
* \param group Group ID (*not* a bitfield)
* \return whether the operation succeeded
*/
bool addMembership(unsigned group);
/**
* Leave a multicast group.
*
* \param group Group ID (*not* a bitfield)
* \return whether the operation succeeded
*/
bool dropMembership(unsigned group);
/**
* Live iterator continuously receiving messages from Netlink socket.
*