#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/netlink.h>
#define SA struct sockaddr
#define err(msg) perror(msg)
#define NETLINK_SINO 27
struct netlink_msg
{
struct nlmsghdr hdr;
char data[1024];
};
int main(int argc, char **argv)
{
struct sockaddr_nl saddr, daddr;
int fd;
struct netlink_msg msg;
if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_SINO)) == -1)
{
err("socket");
goto out;
}
memset(&msg, '\0', sizeof(msg));
msg.hdr.nlmsg_len = NLMSG_SPACE(10);
msg.hdr.nlmsg_pid = getpid();
memcpy(NLMSG_DATA(&msg.hdr), "abcdefghi", 9);
memset(&daddr, '\0', sizeof(daddr));
daddr.nl_family = AF_NETLINK;
if (sendto(fd, &msg, msg.hdr.nlmsg_len, 0, (SA *)&daddr, sizeof(daddr)) == -1)
{
err("sendto");
goto err;
}
char buff[1024];
int len;
memset(buff, '\0', sizeof(buff));
if ((len = recvfrom(fd, buff, sizeof(buff), 0, NULL, 0)) == -1)
{
err("recvfrom");
goto err;
}
char *data = NLMSG_DATA(buff);
write(STDOUT_FILENO, data, strlen(data));
return 0;
err:
close(fd);
out:
return -1;
}
|