#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <asm/types.h>
#include <linux/netlink.h>

#define NETLINK_VFW 18
#define VFW_GROUP 0
#define MSG_SIZE NLMSG_SPACE(1024)

int main(void);

int main(void)
{
	int nl_sd;
	struct sockaddr_nl src_addr;
	struct nlmsghdr *nl_hdr;
	unsigned char buf[MSG_SIZE];
	int ret;
	
	memset(&src_addr, 0, sizeof(struct sockaddr_nl));
	memset(buf, 0, MSG_SIZE);
	
	nl_sd = socket(PF_NETLINK, SOCK_RAW, NETLINK_VFW);
	
	src_addr.nl_family = AF_NETLINK;
	src_addr.nl_pid = getpid();
	src_addr.nl_groups = VFW_GROUP;
	
	bind(nl_sd, (struct sockaddr *)&src_addr, sizeof(struct sockaddr));
	
	nl_hdr = (struct nlmsghdr *)buf;
	nl_hdr->nlmsg_len = MSG_SIZE;
	nl_hdr->nlmsg_pid = getpid();
	nl_hdr->nlmsg_flags = 0;
	strcpy(NLMSG_DATA(nl_hdr), "hello kernel");

	ret = send(nl_sd, buf, MSG_SIZE, 0);
	printf("send ret = %d\n", ret);
	if (ret == -1)
		return ret;
	
	while (1) {
		ret = recv(nl_sd, buf, MSG_SIZE, 0);
		printf("message from kernel = %s\n", (char *)NLMSG_DATA(nl_hdr));
	}
	
	return 0;
}
