Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1106472
  • 博文数量: 143
  • 博客积分: 969
  • 博客等级: 准尉
  • 技术积分: 1765
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-30 12:09
文章分类

全部博文(143)

文章存档

2023年(4)

2021年(2)

2020年(4)

2019年(4)

2018年(33)

2017年(6)

2016年(13)

2014年(7)

2013年(23)

2012年(33)

2011年(14)

我的朋友

分类: LINUX

2023-08-04 19:00:24

内核态程序

点击(此处)折叠或打开

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/types.h>
  4. #include <net/sock.h>
  5. #include <linux/netlink.h>
  6. #include <linux/kthread.h>
  7. #include <linux/semaphore.h>

  8. #define NETLINK_TEST 30
  9. #define MSG_LEN 100
  10. #define USER_PORT 66
  11. #define GROUPID 5

  12. struct sock *nlsk;
  13. static struct task_struct *mythread = NULL; //内核线程对象
  14. extern struct net init_net;
  15. struct semaphore send_flag;
  16. struct semaphore read_flag;
  17. char *umsg = NULL;
  18. int send_usrmsg(char *pbuf, uint16_t len,int pid)
  19. {
  20.     struct sk_buff *nl_skb;
  21.     struct nlmsghdr *nlh; //消息头部
  22.     int ret=0;
  23.     //创建sk_buff
  24.     nl_skb = nlmsg_new(len, GFP_ATOMIC);
  25.     if(!nl_skb)
  26.     {
  27.         printk("netlink alloc failure\n");
  28.         return -1;
  29.     }
  30.     
  31.     /* 设置netlink消息头部 */
  32.     nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_TEST, len, 0);
  33.     if(nlh == NULL)
  34.     {
  35.         printk("nlmsg_put failaure \n");
  36.         nlmsg_free(nl_skb);
  37.         return -1;
  38.     }
  39.  
  40.     /* 拷贝数据发送 */
  41.     memcpy(nlmsg_data(nlh), pbuf, len);
  42.     //ret = netlink_broadcast(nlsk, nl_skb, 0,5, GFP_KERNEL); //发送多播消息到多播组5,这里我故意没有用1之类的“常见”值,目的就是为了证明我们上面提到的多播组号和多播组号掩码之间的对应关系
  43.     ret = netlink_unicast(nlsk, nl_skb, pid, 0); //阻塞模式
  44.     if(ret < 0){
  45.         printk("netlink_unicast error, len:%d, retlen:%d \n",len, ret);
  46.     }
  47.     
  48.     printk("send_usrmsg len:%d, retlen:%d \n",len, ret);
  49.     return ret;
  50. }

  51. int send_usrmsg_broadcast(char *pbuf, uint16_t len)
  52. {
  53.     struct sk_buff *nl_skb;
  54.     struct nlmsghdr *nlh; //消息头部
  55.     int ret=0;
  56.     //创建sk_buff
  57.     nl_skb = nlmsg_new(len, GFP_ATOMIC);
  58.     if(!nl_skb)
  59.     {
  60.         printk("netlink alloc failure\n");
  61.         return -1;
  62.     }
  63.     
  64.     /* 设置netlink消息头部 */
  65.     nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_TEST, len, 0);
  66.     if(nlh == NULL)
  67.     {
  68.         printk("nlmsg_put failaure \n");
  69.         nlmsg_free(nl_skb);
  70.         return -1;
  71.     }
  72.  
  73.     /* 拷贝数据发送 */
  74.     memcpy(nlmsg_data(nlh), pbuf, len);
  75.     ret = netlink_broadcast(nlsk, nl_skb, 0,GROUPID, GFP_KERNEL); //发送多播消息到多播组5,这里我故意没有用1之类的“常见”值,目的就是为了证明我们上面提到的多播组号和多播组号掩码之间的对应关系
  76.     if(ret < 0){
  77.         printk("netlink_broadcast error, len:%d, retlen:%d \n",len, ret);
  78.     }
  79.     
  80.     printk("send_usrmsg len:%d, retlen:%d \n",len, ret);
  81.     return ret;
  82. }

  83. static void netlink_rcv_msg(struct sk_buff *skb)
  84. {
  85.     struct nlmsghdr *nlh;
  86.     char *kmsg = "exit";
  87.     //从skb中获取data字段,并转换成nlh进行读取
  88.     nlh = nlmsg_hdr(skb);
  89.     //读取nlh后面的数据部分
  90.     umsg = NLMSG_DATA(nlh);
  91.     if(umsg){
  92.         printk("kernel recv from user: %s len:%d\n", umsg, (int)strlen(umsg));
  93.         printk("port id :%d\n",NETLINK_CB(skb).portid);
  94.         //send_usrmsg(kmsg, strlen(kmsg), NETLINK_CB(skb).portid); //给用户态发消息
  95.     }
  96.     up(&read_flag);
  97. }

  98. struct netlink_kernel_cfg cfg = {
  99.     .input = netlink_rcv_msg,
  100. };

  101. //每隔1分钟发送一条“I am from kernel!”消息,共发10个报文
  102. static int sending_thread(void *data)
  103. {
  104.      int i = 10;
  105.      struct completion cmpl;
  106.      char *kmsg = "I am from kernel";
  107.      int ret=0;
  108.      while((i--) > 0){        
  109.         init_completion(&cmpl);
  110.         wait_for_completion_timeout(&cmpl, 30 * HZ);
  111.         ret = send_usrmsg_broadcast(kmsg, strlen(kmsg)); //给用户态发消息
  112.         if(ret<0){
  113.             continue;
  114.         }
  115.         printk("sending thread wait recvdata!\n");
  116.         down(&read_flag);
  117.         printk("sending thread recv data :%s!\n", umsg);
  118.      }
  119.      printk("sending thread exited!");
  120.      return 0;
  121. }

  122. static int __init test_netlink_broad_init(void)
  123. {
  124.     nlsk = (struct sock *)netlink_kernel_create(&init_net,NETLINK_TEST, &cfg);
  125.     mythread = kthread_run(sending_thread,NULL,"thread_sender");
  126.     sema_init(&read_flag, 0);
  127.     return 0;
  128. }

  129. static void __exit test_netlink_broad_exit(void)
  130. {
  131.      // 注销netlink协议
  132.     if(nlsk)
  133.     {
  134.         netlink_kernel_release(nlsk);
  135.         nlsk = NULL;
  136.     }
  137.     printk("exit......\n");
  138. }

  139. MODULE_LICENSE("GPL");
  140. module_init(test_netlink_broad_init);
  141. module_exit(test_netlink_broad_exit);

makefile 文件:

obj-m:=netlink_broad_kernel.o     

                                  

CURRENT_PATH:=$(shell pwd)   

LINUX_KERNEL:=$(shell uname -r) 

LINUX_KERNEL_PATH:=/usr/src/kernels/$(LINUX_KERNEL) 

#linux-headers-                    

all:

       make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules

clean:

       make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean


用户态程序


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/socket.h>
  4. #include <string.h>
  5. #include <linux/netlink.h>
  6. #include <stdint.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <assert.h>

  11. #define NETLINK_TEST 30
  12. #define MSG_LEN 100
  13. #define MAX_PLOAD 100
  14. #define USER_PORT 66
  15. #define GROUPID 5
  16. typedef struct _user_msg_info
  17. {
  18.     struct nlmsghdr hdr;
  19.     char msg[MSG_LEN];
  20. } user_msg_info;

  21. unsigned int netlink_group_mask(unsigned int group)
  22. {
  23.     return group ? 1 << (group - 1) : 0;
  24. }

  25. int main(int argc,char **argv)
  26. {
  27.     int sockfd;
  28.     struct sockaddr_nl saddr, daddr;
  29.     struct nlmsghdr *nlh;
  30.     user_msg_info u_info;
  31.     char *msg = "hello kernel, I am user process!";
  32.     socklen_t len;

  33.     //创建socket SOCK_RAW|SOCK_NONBLOCK
  34.     sockfd = socket(AF_NETLINK,SOCK_RAW, NETLINK_TEST);

  35.     //初始化地址
  36.     memset(&saddr,0,sizeof(saddr));
  37.     //地址赋值
  38.     saddr.nl_family = AF_NETLINK;
  39.     saddr.nl_pad = 0;
  40.     saddr.nl_pid = 0;// 表示我们要从内核接收多播消息。注意:该字段为0有双重意义,另一个意义是表示我们发送的数据的目的地址是内核。
  41.     saddr.nl_groups = netlink_group_mask(GROUPID); //组播ID

  42.     //地址与sockt绑定-bind
  43.     bind(sockfd,(struct sockaddr *)&saddr,sizeof(saddr));

  44.     //初始化目的地址
  45.     memset(&daddr, 0, sizeof(daddr));
  46.     daddr.nl_family = AF_NETLINK;
  47.     daddr.nl_pid = 0; // to kernel
  48.     daddr.nl_groups = 0;
  49.     //struct timeval tv= {10, 0};
  50.     struct timeval timeout;
  51.     timeout.tv_sec = 60;
  52.     timeout.tv_usec = 0;

  53.     setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval ));
  54.     
  55.      //初始化消息头
  56.     nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PLOAD));
  57.     memset(nlh, 0, sizeof(struct nlmsghdr));
  58.     nlh->nlmsg_len = NLMSG_SPACE(MAX_PLOAD);
  59.     nlh->nlmsg_flags = 0;
  60.     nlh->nlmsg_type = 0;
  61.     nlh->nlmsg_seq = 0;
  62.     nlh->nlmsg_pid = USER_PORT; //self port
  63.     //nlh->nlmsg_pid = getpid();//我们希望得到内核回应,所以得告诉内核我们ID号


  64.     while (1)
  65.     {
  66.         memset(&u_info, 0, sizeof(u_info));
  67.         len = sizeof(struct sockaddr_nl);

  68.         //接收消息
  69.         int ret = recvfrom(sockfd,&u_info,sizeof(user_msg_info),0,(struct sockaddr *)&daddr,&len);
  70.         if(ret < 0){
  71.             printf("recvfrom error, errno:%d \n",errno);
  72.             break;
  73.         }
  74.         
  75.         printf("recv from kernel:%s ret:%d\n",u_info.msg,ret);
  76.         if(strcmp("exit",u_info.msg) == 0){
  77.          printf("recv from kernel:%s exit .\n",u_info.msg);
  78.             break ;
  79.         }
  80.         
  81.          //设置消息内容
  82.         memcpy(NLMSG_DATA(nlh),msg,strlen(msg));
  83.         
  84.         //发送消息
  85.         ret = sendto(sockfd,nlh,nlh->nlmsg_len,0,(struct sockaddr *)&daddr,sizeof(struct sockaddr_nl));
  86.         if(ret < 0){
  87.             printf("sendto error, errno:%d \n", errno);
  88.             break;
  89.         }
  90.         printf("send to kernel :%s ret:%d nlmsg_len:%d \n",msg,ret, nlh->nlmsg_len);
  91.         
  92.     };
  93.     
  94.     close(sockfd);
  95.     return 0;
  96. }

编译命令:

Gcc -o netlink_broad_user netlink_ broad_user.c



阅读(381) | 评论(0) | 转发(0) |
0

上一篇:netlink(三)-用户态发起会话

下一篇:没有了

给主人留下些什么吧!~~