Chinaunix首页 | 论坛 | 博客
  • 博客访问: 77457
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: LINUX

2015-01-04 16:42:52

需首先修改/usr/src/linux内核版本/include/uapi/linux/netlink.h添加NETLINK_TEST类型                                       

点击(此处)折叠或打开

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/skbuff.h>
  4. #include <linux/init.h>
  5. #include <linux/ip.h>
  6. #include <linux/types.h>
  7. #include <linux/sched.h>
  8. #include <net/sock.h>
  9. #include <net/netlink.h>
  10. #include <linux/kthread.h>

  11. #define MAX_MSGSIZE 4096

  12. MODULE_LICENSE("GPL");
  13. MODULE_AUTHOR("WHO");

  14. struct sock *nl_sk = NULL;
  15. static struct task_struct *mythread = NULL;

  16. void sendnlmsg(char *message)
  17. {
  18.     struct sk_buff *skb;
  19.     struct nlmsghdr *nlh;
  20.     int slen = 0;

  21.     if(!message || !nl_sk){
  22.         return;
  23.     }

  24.     skb = nlmsg_new(MAX_MSGSIZE, GFP_KERNEL);
  25.     if(!skb){
  26.         printk(KERN_ERR "my_net_link: alloc_skb Error./n");
  27.         return;
  28.     }

  29.     slen = strlen(message)+1;

  30.     nlh = nlmsg_put(skb, 0, 0, 0, MAX_MSGSIZE, 0);

  31.     NETLINK_CB(skb).portid = 0;
  32.     NETLINK_CB(skb).dst_group = 5;

  33.     memcpy(NLMSG_DATA(nlh), message, slen);

  34.     netlink_broadcast(nl_sk, skb, 0,5, GFP_KERNEL);
  35.     printk("send OK!\n");
  36.     return;
  37. }

  38. static void recnldata (struct sk_buff *__skb, char *mydata)
  39. {
  40.     struct nlmsghdr *nlh = NULL;
  41.     struct sk_buff *skb;
  42.     skb = skb_get(__skb);
  43.     
  44.     if(skb->len >= NLMSG_SPACE(0))
  45.     {
  46.           nlh = (struct nlmsghdr *)skb->data;
  47.           mydata = (char*)NLMSG_DATA(nlh);
  48.           printk("%s:received netlink message payload: %s \n",__FUNCTION__,mydata);
  49.           kfree_skb(skb);
  50.     }
  51.     printk("recvied finished!\n");
  52. }

  53. static int sending_thread(void *data)
  54. {
  55.      int i = 10;
  56.      struct completion cmpl;
  57.      while(i--){
  58.             init_completion(&cmpl);
  59.             wait_for_completion_timeout(&cmpl, 1 * HZ);
  60.             sendnlmsg("hello userspace!");
  61.      }
  62.      printk("sending thread exited!");
  63.      return 0;
  64. }

  65. static int __init myinit_module(void)
  66. {
  67.     struct netlink_kernel_cfg netlink_kerncfg = {
  68.            .input = recnldata,
  69.     };
  70.     printk("my netlink in\n");
  71.     nl_sk = netlink_kernel_create(&init_net,NETLINK_TEST,&netlink_kerncfg);

  72.     if(!nl_sk){
  73.         printk(KERN_ERR "my_net_link: create netlink socket error.\n");
  74.         return 1;
  75.     }

  76.     printk("my netlink: create netlink socket ok.\n");
  77.     mythread = kthread_run(sending_thread,NULL,"thread_sender");
  78.     return 0;
  79. }

  80. static void __exit mycleanup_module(void)
  81. {
  82.     if(nl_sk != NULL){
  83.         sock_release(nl_sk->sk_socket);
  84.     }
  85. printk("my netlink out!\n");
  86. }

  87. module_init(myinit_module);
  88. module_exit(mycleanup_module);

阅读(2111) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~