sender.c
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/socket.h>
#define MAX_PAYLOAD 1024 /* maximum payload size*/
struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;
struct str
{
int age;
char* name;
};
int main(int argc, char* argv[])
{
struct str* str = (struct str*)malloc(sizeof(struct str));
sock_fd = socket(PF_NETLINK, SOCK_RAW, 21);
memset(&msg, 0, sizeof(msg));
memset(&src_addr, 0, sizeof(src_addr));
//memset(&str,0,sizeof(str));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */
src_addr.nl_groups = 0; /* not in mcast groups */
bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */
nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
/* Fill the netlink message header */
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid(); /* self pid */
nlh->nlmsg_flags = 0;
/* Fill in the netlink message payload */
str->age = 24;
str->name = "zj";
printf("age=%d,name=%s,size=%d\n",str->age,str->name,sizeof(struct str));
memcpy(NLMSG_DATA(nlh),str,sizeof(struct str));
iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
printf(" Sending message. ...\n");
sendmsg(sock_fd, &msg, 0);
/* Read message from kernel */
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
printf(" Waiting message. ...\n");
recvmsg(sock_fd, &msg, 0);
printf(" Received message payload: %s\n",(char*)NLMSG_DATA(nlh));
/* Close Netlink Socket */
close(sock_fd);
}
net_link.c:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <net/sock.h>
#include <net/netlink.h>
#define NETLINK_TEST 21
struct sock *nl_sk = NULL;
EXPORT_SYMBOL_GPL(nl_sk);
struct str
{
int age;
char* name;
};
void nl_data_ready (struct sk_buff *__skb)
{
struct sk_buff *skb;
struct nlmsghdr *nlh;
u32 pid;
int rc;
int len = NLMSG_SPACE(1200);
char str[100];
printk("net_link: data is ready to read.\n");
skb = skb_get(__skb);
if (skb->len >= NLMSG_SPACE(0)) {
nlh = nlmsg_hdr(skb);
printk("net_link: recv age=%d.\n", ((struct str*)NLMSG_DATA(nlh))->age);
printk("net_link: recv name=%s.\n", ((struct str*)NLMSG_DATA(nlh))->name);
//memcpy(str,NLMSG_DATA(nlh), sizeof(str));
pid = nlh->nlmsg_pid; /*pid of sending process */
printk("net_link: pid is %d\n", pid);
kfree_skb(skb);
skb = alloc_skb(len, GFP_ATOMIC);
if (!skb){
printk(KERN_ERR "net_link: allocate failed.\n");
return;
}
nlh = nlmsg_put(skb,0,0,0,1200,0);
NETLINK_CB(skb).pid = 0; /* from kernel */
strcpy(str,"hello world\n");
memcpy(NLMSG_DATA(nlh), str, sizeof(str));
printk("net_link: going to send.\n");
rc = netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);
if (rc < 0) {
printk(KERN_ERR "net_link: can not unicast skb (%d)\n", rc);
}
printk("net_link: send is ok.\n");
}
return;
}
static int test_netlink(void)
{
nl_sk = netlink_kernel_create(&init_net, NETLINK_TEST, 0, nl_data_ready, NULL, THIS_MODULE);
if (!nl_sk) {
printk(KERN_ERR "net_link: Cannot create netlink socket.\n");
return -EIO;
}
printk("net_link: create socket ok.\n");
return 0;
}
int init_module()
{
test_netlink();
return 0;
}
void cleanup_module( )
{
if (nl_sk != NULL){
sock_release(nl_sk->sk_socket);
}
printk("net_link: remove ok.\n");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("iptaber@gmail.com");
结构题还不是很完善没有放到一个.h文件中,这个仅仅是test的嘛,将就点
|