Linux下netlink通信的实例代码

Linux下netlink通信的实例代码

 http://blog.csdn.net/love_life2011/article/details/7596190#comments
源代码共分三个文件:

内核模块:netlink-exam-kern.c

应用接收:netlink-exam-user-recv.c

应用发送:netlink-exam-user-send.c


内核模块源码:

[cpp]  view plain copy
  1. <span style="font-size:18px;">//kernel module: netlink-exam-kern.c  
  2. #include <linux/config.h>  
  3. #include <linux/module.h>  
  4. #include <linux/netlink.h>  
  5. #include <linux/sched.h>  
  6. #include <net/sock.h>  
  7. #include <linux/proc_fs.h>  
  8.   
  9. #define BUF_SIZE 16384  
  10. static struct sock *netlink_exam_sock;  
  11. static unsigned char buffer[BUF_SIZE];  
  12. static unsigned int buffer_tail = 0;  
  13. static int exit_flag = 0;  
  14. static DECLARE_COMPLETION(exit_completion);  
  15.   
  16. static void recv_handler(struct sock * sk, int length)  
  17. {  
  18.         wake_up(sk->sk_sleep);  
  19. }  
  20.   
  21. static int process_message_thread(void * data)  
  22. {  
  23.         struct sk_buff * skb = NULL;  
  24.         struct nlmsghdr * nlhdr = NULL;  
  25.         int len;  
  26.         DEFINE_WAIT(wait);  
  27.   
  28.         daemonize("mynetlink");  
  29.   
  30.         while (exit_flag == 0) {  
  31.                 prepare_to_wait(netlink_exam_sock->sk_sleep, &wait, TASK_INTERRUPTIBLE);  
  32.                 schedule();  
  33.                 finish_wait(netlink_exam_sock->sk_sleep, &wait);   
  34.   
  35.                 while ((skb = skb_dequeue(&netlink_exam_sock->sk_receive_queue))  
  36.                          != NULL) {  
  37.                         nlhdr = (struct nlmsghdr *)skb->data;  
  38.                         if (nlhdr->nlmsg_len < sizeof(struct nlmsghdr)) {  
  39.                                 printk("Corrupt netlink message.\n");  
  40.                                 continue;  
  41.                         }  
  42.                         len = nlhdr->nlmsg_len - NLMSG_LENGTH(0);  
  43.                         if (len + buffer_tail > BUF_SIZE) {  
  44.                                 printk("netlink buffer is full.\n");  
  45.                         }  
  46.                         else {  
  47.                                 memcpy(buffer + buffer_tail, NLMSG_DATA(nlhdr), len);  
  48.                                 buffer_tail += len;  
  49.                         }  
  50.                         nlhdr->nlmsg_pid = 0;  
  51.                         nlhdr->nlmsg_flags = 0;  
  52.                         NETLINK_CB(skb).pid = 0;  
  53.                         NETLINK_CB(skb).dst_pid = 0;  
  54.                         NETLINK_CB(skb).dst_group = 1;  
  55.                         netlink_broadcast(netlink_exam_sock, skb, 0, 1, GFP_KERNEL);  
  56.                 }  
  57.         }  
  58.         complete(&exit_completion);  
  59.         return 0;  
  60. }  
  61.   
  62. static int netlink_exam_readproc(char *page, char **start, off_t off,  
  63.                           int count, int *eof, void *data)  
  64. {  
  65.         int len;  
  66.   
  67.         if (off >= buffer_tail) {  
  68.                 * eof = 1;  
  69.                 return 0;  
  70.         }  
  71.         else {  
  72.                 len = count;  
  73.                 if (count > PAGE_SIZE) {  
  74.                         len = PAGE_SIZE;  
  75.                 }  
  76.                 if (len > buffer_tail - off) {  
  77.                         len = buffer_tail - off;  
  78.                 }  
  79.                 memcpy(page, buffer + off, len);  
  80.                 *start = page;  
  81.                 return len;  
  82.         }  
  83.   
  84. }  
  85.   
  86. static int __init netlink_exam_init(void)  
  87. {  
  88.         netlink_exam_sock = netlink_kernel_create(NETLINK_GENERIC, 0, recv_handler, THIS_MODULE);  
  89.         if (!netlink_exam_sock) {  
  90.                 printk("Fail to create netlink socket.\n");  
  91.                 return 1;  
  92.         }  
  93.         kernel_thread(process_message_thread, NULL, CLONE_KERNEL);  
  94.         create_proc_read_entry("netlink_exam_buffer", 0444, NULL, netlink_exam_readproc, 0);  
  95.         return 0;  
  96. }  
  97.   
  98. static void __exit netlink_exam_exit(void)  
  99. {  
  100.         exit_flag = 1;  
  101.         wake_up(netlink_exam_sock->sk_sleep);  
  102.         wait_for_completion(&exit_completion);  
  103.         sock_release(netlink_exam_sock->sk_socket);  
  104. }  
  105.   
  106. module_init(netlink_exam_init);  
  107. module_exit(netlink_exam_exit);  
  108. MODULE_LICENSE("GPL");  
  109.   
  110. </span>  


用户接收源码:

[cpp]  view plain copy
  1. <span style="font-size:18px;">//application receiver: netlink-exam-user-recv.c  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <sys/socket.h>  
  5. #include <linux/netlink.h>  
  6.   
  7. #define MAX_MSGSIZE 1024  
  8.   
  9.   
  10. int main(void)  
  11. {  
  12.         struct sockaddr_nl saddr, daddr;  
  13.         struct nlmsghdr *nlhdr = NULL;  
  14.         struct msghdr msg;  
  15.         struct iovec iov;  
  16.         int sd;  
  17.         int ret = 1;  
  18.   
  19.         sd = socket(AF_NETLINK, SOCK_RAW,NETLINK_GENERIC);  
  20.         memset(&saddr, 0, sizeof(saddr));  
  21.         memset(&daddr, 0, sizeof(daddr));  
  22.   
  23.         saddr.nl_family = AF_NETLINK;        
  24.         saddr.nl_pid = getpid();  
  25.         saddr.nl_groups = 1;  
  26.         bind(sd, (struct sockaddr*)&saddr, sizeof(saddr));  
  27.   
  28.         nlhdr = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_MSGSIZE));  
  29.   
  30.         while (1) {  
  31.                 memset(nlhdr, 0, NLMSG_SPACE(MAX_MSGSIZE));  
  32.   
  33.                 iov.iov_base = (void *)nlhdr;  
  34.                 iov.iov_len = NLMSG_SPACE(MAX_MSGSIZE);  
  35.                 msg.msg_name = (void *)&daddr;  
  36.                 msg.msg_namelen = sizeof(daddr);  
  37.                 msg.msg_iov = &iov;  
  38.                 msg.msg_iovlen = 1;  
  39.   
  40.                 ret = recvmsg(sd, &msg, 0);  
  41.                 if (ret == 0) {  
  42.                         printf("Exit.\n");  
  43.                         exit(0);  
  44.                 }  
  45.                 else if (ret == -1) {  
  46.                         perror("recvmsg:");  
  47.                         exit(1);  
  48.                 }  
  49.                 printf("%s", NLMSG_DATA(nlhdr));  
  50.         }  
  51.    
  52.         close(sd);  
  53. }  
  54.   
  55. </span>  


 

用户发送源码:

[cpp]  view plain copy
  1. <span style="font-size:18px;">//application sender: netlink-exam-user-send.c  
  2. #include <stdio.h>  
  3. #include <sys/types.h>  
  4. #include <sys/socket.h>  
  5. #include <linux/netlink.h>  
  6.   
  7. #define MAX_MSGSIZE 1024  
  8.   
  9.   
  10. int main(int argc, char * argv[])  
  11. {  
  12.         FILE * fp;  
  13.         struct sockaddr_nl saddr, daddr;  
  14.         struct nlmsghdr *nlhdr = NULL;  
  15.         struct msghdr msg;  
  16.         struct iovec iov;  
  17.         int sd;  
  18.         char text_line[MAX_MSGSIZE];  
  19.         int ret = -1;  
  20.   
  21.         if (argc < 2) {  
  22.                 printf("Usage: %s atextfilename\n", argv[0]);  
  23.                 exit(1);  
  24.         }  
  25.   
  26.         if ((fp = fopen(argv[1], "r")) == NULL) {  
  27.                 printf("File %s dosen't exist.\n");  
  28.                 exit(1);  
  29.         }  
  30.   
  31.         sd = socket(AF_NETLINK, SOCK_RAW,NETLINK_GENERIC);  
  32.         memset(&saddr, 0, sizeof(saddr));  
  33.         memset(&daddr, 0, sizeof(daddr));  
  34.   
  35.         saddr.nl_family = AF_NETLINK;        
  36.         saddr.nl_pid = getpid();  
  37.         saddr.nl_groups = 0;  
  38.         bind(sd, (struct sockaddr*)&saddr, sizeof(saddr));  
  39.   
  40.         daddr.nl_family = AF_NETLINK;  
  41.         daddr.nl_pid = 0;  
  42.         daddr.nl_groups = 0;  
  43.   
  44.         nlhdr = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_MSGSIZE));  
  45.   
  46.         while (fgets(text_line, MAX_MSGSIZE, fp)) {  
  47.                 memcpy(NLMSG_DATA(nlhdr), text_line, strlen(text_line));  
  48.                 memset(&msg, 0 ,sizeof(struct msghdr));  
  49.   
  50.                 nlhdr->nlmsg_len = NLMSG_LENGTH(strlen(text_line));  
  51.                 nlhdr->nlmsg_pid = getpid();  /* self pid */  
  52.                 nlhdr->nlmsg_flags = 0;  
  53.   
  54.                 iov.iov_base = (void *)nlhdr;  
  55.                 iov.iov_len = nlhdr->nlmsg_len;  
  56.                 msg.msg_name = (void *)&daddr;  
  57.                 msg.msg_namelen = sizeof(daddr);  
  58.                 msg.msg_iov = &iov;  
  59.                 msg.msg_iovlen = 1;  
  60.                 ret = sendmsg(sd, &msg, 0);  
  61.                 if (ret == -1) {  
  62.                         perror("sendmsg error:");  
  63.                 }  
  64.         }  
  65.    
  66.         close(sd);  
  67. }  
  68.   
  69. </span>  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、imp2源码 整个源码包含三个文件:imp2_k.c, imp2_u.c和imp2.h. 其中imp2_k.c为内核模块的源代码,imp2_u.c为应用程序,即测试代码,imp2.h为两个源文件都需要引用的头文件。其整体的功能是:注册一种新的netlink协议,并注册一个新的NF hook函数。当有ping包发往当前主机或者经过当前主机转发时,内核向用户发送ping包的源IP和目的IP。各个文件的简单分析见下文。 1. imp2.h 该文件主要是定义了一种新的Netlink协议类型NL_IMP2(31)。新的协议类型的选值不能和当前内核中已经定义的netlink协议类型重复。定义了基于该协议类型的消息类型,内核根据接收到消息的不同类型,进行不同的处理:IMP2_U_PID和IMP2_CLOSE分别为请求和关闭。IMP2_K_MSG代表内核空间发送的消息。 该头文件的源码如下: 2. imp2_k.c 该程序为内核模块程序。其完成的功能如下: (1)创建一种新的Netlink协议NL_IMP2,并注册该协议的回调函数kernel_receive。但用户空间通过建立且协议类型为NL_IMP2的socket套接字并调用sendto,sendmsg函数发送数据时,传送到内核空间的数据由kernel_receive进行处理。该函数主要是记录用户进程的ID,用于随后发送数据的时候指定目的。 (2)在Netfilter的hook点NF_IP_PRE_ROUTING注册hook函数get_icmp,对经过该hook点的ping包进行处理。get_icmp首先判断是否是ping包,如果不是,直接Accept。如果是,则记录该包的源IP和目的IP,然后调用send_to_user,将记录的信息发送给kernel_recieve函数中记录的用户进程ID。 该文件的源码如下: 3. imp2_u.c 该程序为用户空间的测试程序。该程序包括以下功能: (1)生成NL_IMP2协议的socket.然后通过调用sendto发送IMP2_U_PID类型的请求信息给内核。然后等待接受内核发回的信息。记住:仅当有ping包经过内核的NF时,内核才会向用户进程发送信息。 (2)当用户进程通过Ctrl+C来结束该程序时,调用信号处理函数sig_int,向内核发送IMP2_CLOSE的消息,结束socket。 该文件的源码如下: 二、编译和测试 1. 整个源文件编译的Makefile如下: all: gcc -O2 -DMODULE -D__KERNEL__ -W -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o imp2_k.o imp2_k.c gcc imp2_u.c -o imp2_u install: insmod imp2_k.o uninstall: rmmod imp2_k clean: rm -f imp2_k.o imp2_u 2. 加载内核模块,并执行测试程序。 #make install #./imp2_u 当没有ping包时,终端一直处于等待输出状态。通过另一台主机(192.168.1.100)向当前主机(192.168.1.101)发送ping包,则终端已经有输出: #./imp2_u [root@localhost imp2]# ./imp2_u src: 192.168.1.100, dest: 192.168.1.101 src: 192.168.1.100, dest: 192.168.1.101 src: 192.168.1.100, dest: 192.168.1.101 src: 192.168.1.100, dest: 192.168.1.101

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值