diff --git a/automotive/can/1.0/default/libnl++/Socket.cpp b/automotive/can/1.0/default/libnl++/Socket.cpp index b0e67beee3..cc1d839434 100644 --- a/automotive/can/1.0/default/libnl++/Socket.cpp +++ b/automotive/can/1.0/default/libnl++/Socket.cpp @@ -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(); diff --git a/automotive/can/1.0/default/libnl++/include/libnl++/Socket.h b/automotive/can/1.0/default/libnl++/include/libnl++/Socket.h index 118b9f7b20..7ec0f7bdf9 100644 --- a/automotive/can/1.0/default/libnl++/include/libnl++/Socket.h +++ b/automotive/can/1.0/default/libnl++/include/libnl++/Socket.h @@ -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. *