return k;
}
-static int socket_recv_message(int fd, struct iovec *iov, bool peek) {
- uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred))];
+static int socket_recv_message(int fd, struct iovec *iov, uint32_t *_group, bool peek) {
+ uint8_t cred_buffer[CMSG_SPACE(sizeof(struct ucred)) +
+ CMSG_SPACE(sizeof(struct nl_pktinfo))];
struct msghdr msg = {
.msg_iov = iov,
.msg_iovlen = 1,
.msg_controllen = sizeof(cred_buffer),
};
struct cmsghdr *cmsg;
+ uint32_t group = 0;
bool auth = false;
int r;
struct ucred *ucred = (void *)CMSG_DATA(cmsg);
/* from the kernel */
- if (ucred->uid == 0 && ucred->pid == 0) {
+ if (ucred->uid == 0 && ucred->pid == 0)
auth = true;
- break;
- }
+ } else if (cmsg->cmsg_level == SOL_NETLINK &&
+ cmsg->cmsg_type == NETLINK_PKTINFO &&
+ cmsg->cmsg_len == CMSG_LEN(sizeof(struct nl_pktinfo))) {
+ struct nl_pktinfo *pktinfo = (void *)CMSG_DATA(cmsg);
+
+ /* multi-cast group */
+ group = pktinfo->group;
}
}
/* not from the kernel, ignore */
return 0;
+ if (group)
+ *_group = group;
+
return r;
}
int socket_read_message(sd_rtnl *rtnl) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *first = NULL;
struct iovec iov = {};
+ uint32_t group = 0;
bool multi_part = false, done = false;
struct nlmsghdr *new_msg;
size_t len;
assert(rtnl->rbuffer_allocated >= sizeof(struct nlmsghdr));
/* read nothing, just get the pending message size */
- r = socket_recv_message(rtnl->fd, &iov, true);
+ r = socket_recv_message(rtnl->fd, &iov, &group, true);
if (r <= 0)
return r;
else
iov.iov_len = rtnl->rbuffer_allocated;
/* read the pending message */
- r = socket_recv_message(rtnl->fd, &iov, false);
+ r = socket_recv_message(rtnl->fd, &iov, &group, false);
if (r <= 0)
return r;
else
#include <sys/socket.h>
#include <poll.h>
+#include "missing.h"
#include "macro.h"
#include "util.h"
#include "hashmap.h"
if (rtnl->fd < 0)
return -errno;
- if (setsockopt(rtnl->fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
+ r = setsockopt(rtnl->fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
+ if (r < 0)
+ return -errno;
+
+ r = setsockopt(rtnl->fd, SOL_NETLINK, NETLINK_PKTINFO, &one, sizeof(one));
+ if (r < 0)
return -errno;
va_start(ap, n_groups);