I am also working on something that involves netlink. I pieced together a program that seems to work on my system (debian 3.2.0.4). It is a simple program that sends a msg first and starts listening updates coming from kernel. I believe you would have to change the send part and set necessary flags etc.
One rather important difference: I don't call nl_socket_alloc, instead, I create a regular socket. I am sure I have some mistake in there, you can be straightforward. I hope it helps:
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/cache.h>
#include <netlink/route/link.h>
//#include <>
#define NETLINK_USER 31
#define MAX_PAYLOAD 1024
using std::cout;
using std::endl;
int sequence_number = 0;
void sendNetLinkMsg(int fd) {
struct nlmsghdr *nh = 0;
struct sockaddr_nl sa;
struct iovec iov = { nh, nh->nlmsg_len };
struct msghdr msg;
msg = {&sa, sizeof(sa), &iov, 1, NULL, 0, 0};
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
nh->nlmsg_pid = getpid();
nh->nlmsg_seq = ++sequence_number;
nh->nlmsg_flags |= NLM_F_ACK;
sendmsg(fd, &msg, 0);
}
void recvNetLinkMsg(int fd, struct msghdr* msg, char* buf) {
int len;
struct nlmsghdr *nh;
len = recvmsg(fd, msg, 0);
for (nh = (struct nlmsghdr *) buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
if (nh->nlmsg_type == NLMSG_DONE)
break;
if (nh->nlmsg_type == NLMSG_ERROR) {
cout << "Error" << endl;
}
if (nh->nlmsg_type == RTM_DELROUTE)
cout << "Delete route" << endl;
else if (nh->nlmsg_type == RTM_NEWROUTE)
cout << "New route" << endl;
else {
cout << "Unknown msg: " << nh->nlmsg_type << endl;
}
}
}
int main() {
struct sockaddr_nl sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.nl_family = AF_NETLINK;
sockAddr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
bind(fd, (struct sockaddr *) &sockAddr, sizeof(sockAddr));
char buf[4096];
struct iovec iov = { buf, sizeof(buf) };
struct msghdr msg;
msg = {&sockAddr, sizeof(sockAddr), &iov, 1, NULL, 0, 0};
sendNetLinkMsg(fd);
while (true) {
recvNetLinkMsg(fd, &msg, buf);
}
return 0;
}