打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
netlink简单介绍 (linux

~~~~MARK~~~~~~~:

看了几遍关于netlink的文章:

Kernel Korner - Why and How to Use Netlink Socket:

http://www.linuxjournal.com/article/7356

对应翻译:

http://bbs.chinaunix.net/thread-2029813-1-1.html

相当不错,感谢!

由于netlink的内容一直在变,这些帖子也是有些年头了,所以具体的调用方法与当前的linux kernel有些不匹配,配合linux-3.14.2的code:

netlink_kernel.c

  1. #include <linux/kernel.h>  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/sched.h>  
  5. #include <net/sock.h>  
  6. #include <linux/netlink.h>  
  7.   
  8. #define NETLINK_TEST 17  
  9.   
  10. static struct sock *generic_nlsk;  
  11.   
  12. static void nl_process_data(struct sk_buff *skb)  
  13. {  
  14.     //printk("nl_recv_data in\n");  
  15.   
  16.     struct nlmsghdr* nlh = NULL;  
  17.     void* data = NULL;  
  18.     printk("skb->len %u\n", skb->len);  
  19.     if (skb->len >= nlmsg_total_size(0)) {  
  20.         nlh = nlmsg_hdr(skb);  
  21.   
  22.         //printk("nlh->nlmsg_len %u, nlh->nlmsg_flags %u, nlh->nlmsg_pid %u\n", nlh->nlmsg_len, nlh->nlmsg_flags, nlh->nlmsg_pid);  
  23.         data = NLMSG_DATA(nlh);  
  24.         if (data) {  
  25.             printk("data: %s\n", (char*)data);  
  26.         }  
  27.   
  28.         //printk("NETLINK_CB(skb).portid %u\n", NETLINK_CB(skb).portid);  
  29.         //netlink_unicast(skb->sk, skb, NETLINK_CB(skb).portid, MSG_DONTWAIT);  
  30.     }  
  31.     return;  
  32. }  
  33.   
  34. static int netlink_k_init(void)  
  35. {  
  36.     struct netlink_kernel_cfg cfg = {  
  37.         .input = nl_process_data,  
  38.     };  
  39.   
  40.     generic_nlsk = netlink_kernel_create(&init_net, NETLINK_TEST, &cfg);  
  41.     if (!generic_nlsk) {  
  42.         printk("create NETLINK_GENERIC fail!\n");  
  43.         return -1;  
  44.     }  
  45.     printk("create NETLINK_GENERIC done\n");  
  46.   
  47.     return 0;  
  48. }  
  49.   
  50. static int netlink_init(void)  
  51. {  
  52.     printk("netlink_k init\n");  
  53.     netlink_k_init();  
  54.     return 0;  
  55. }  
  56. static void netlink_exit(void)  
  57. {  
  58.     if (generic_nlsk) {  
  59.         netlink_kernel_release(generic_nlsk);  
  60.     }  
  61.     printk("netlink exit\n");  
  62. }  
  63. MODULE_LICENSE("GPL");  
  64. MODULE_AUTHOR("chuchen");  
  65.   
  66. module_init(netlink_init);  
  67. module_exit(netlink_exit);  

user.c参考以上链接的帖子就可以了。

主要的变动:

1 create API

2 struct nlmsghdr定义

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用户空间和内核空间通讯之【Netlink 下】
Linux 内核态与用户态通信 netlink
linux netlink通信机制
Netlink编程-用户主动发起会话
用户空间和内核空间通讯之Netlink
netlink socket 编程之 why & how - 内核源码 - ChinaUn...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服