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

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: LINUX

2015-01-04 17:24:06

需首先更改/usr/include/linux/netlink.h添加自定义的NETLINK type                                            

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/socket.h>
  4. #include <linux/in.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <asm/types.h>
  11. #include <linux/netlink.h>
  12. #include <linux/socket.h>

  13. #define MAX_PAYLOAD 4096

  14. unsigned int netlink_group_mask(unsigned int group){
  15.     return group ? 1<<(group-1) : 0;
  16. }

  17. int send_to_kernel(char *data){
  18.     int sock_fd = -1;
  19.     struct sockaddr_nl sock_addr;
  20.     struct nlmsghdr *nlh = NULL;
  21.     struct msghdr msg;
  22.     struct iovec iov;
  23.     int ret;
  24.     int len = strlen(data);
  25.     char *tempdata = (char*)malloc(len*4);
  26.     strcpy(tempdata,data);

  27.     sock_fd = socket(PF_NETLINK,SOCK_RAW,NETLINK_TEST);
  28.     if(sock_fd == -1){
  29.         perror("can't create netlink socket!\n");
  30.         return -1;
  31.     }
  32.     
  33.     memset(&sock_addr,0,sizeof(sock_addr));
  34.     sock_addr.nl_family = AF_NETLINK;
  35.     sock_addr.nl_pid = 0;
  36.     sock_addr.nl_groups = netlink_group_mask(5);

  37.     ret = bind(sock_fd,(struct sockaddr *)&sock_addr,sizeof(sock_addr));
  38.     if(ret < 0){
  39.         printf("bind failed: %s\n",strerror(errno));
  40.         close(sock_fd);
  41.         return -1;
  42.     }
  43.     
  44.     nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
  45.     if(nlh == NULL){
  46.         perror("alloc mem failed\n");
  47.         return 1;
  48.     }

  49.     memset(nlh,0,MAX_PAYLOAD);
  50.     nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
  51.     nlh->nlmsg_pid = getpid();
  52.     nlh->nlmsg_type = NLMSG_NOOP;
  53.     nlh->nlmsg_flags = 0;
  54.     
  55.     strcpy(NLMSG_DATA(nlh),tempdata);

  56.     memset(&iov,0,sizeof(iov));
  57.     iov.iov_base = (void *)nlh;
  58.     iov.iov_len = nlh->nlmsg_len;

  59.     memset(&msg,0,sizeof(msg));
  60.     msg.msg_iov = &iov;
  61.     msg.msg_iovlen = 1;

  62.     sendmsg(sock_fd,&msg,0);
  63.     
  64.     free(nlh);
  65.     free(tempdata);
  66.     return 0;
  67. }

  68. int rec_from_kernel(char** data){
  69.     int sock_fd = -1;
  70.     struct sockaddr_nl sock_addr;
  71.     struct nlmsghdr *nlh = NULL;
  72.     struct msghdr msg;
  73.     struct iovec iov;
  74.     int ret,rec;
  75.     
  76.     sock_fd = socket(PF_NETLINK,SOCK_RAW,NETLINK_TEST);
  77.     if(sock_fd == -1){
  78.         perror("can't create netlink socket!\n");
  79.         return -1;
  80.     }
  81.     
  82.     memset(&sock_addr,0,sizeof(sock_addr));
  83.     sock_addr.nl_family = AF_NETLINK;
  84.     sock_addr.nl_pid = 0;
  85.     sock_addr.nl_groups = netlink_group_mask(5);

  86.     ret = bind(sock_fd,(struct sockaddr *)&sock_addr,sizeof(sock_addr));
  87.     if(ret < 0){
  88.         printf("bind failed: %s\n",strerror(errno));
  89.         close(sock_fd);
  90.         return -1;
  91.     }
  92.     
  93.     nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
  94.     if(nlh == NULL){
  95.         perror("alloc mem failed\n");
  96.         return -1;
  97.     }

  98.     memset(nlh,0,MAX_PAYLOAD);
  99.     iov.iov_base = (void *)nlh;
  100.     iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);

  101.     memset(&msg,0,sizeof(msg));
  102.     msg.msg_iov = &iov;
  103.     msg.msg_iovlen = 1;

  104.     rec = recvmsg(sock_fd,&msg,0);
  105.     
  106.     *data = (char *)NLMSG_DATA(nlh);

  107.     free(nlh);
  108.     close(sock_fd);
  109.     return 0;
  110. }

  111. int main(void){
  112.     char* control_data = "userspace is ready for data";
  113.     char* mydata = NULL;
  114.     int a,b,c;
  115.     
  116.     while(1)
  117.     {
  118.     a = rec_from_kernel(&mydata);
  119.     if(a == -1){
  120.         printf("rec from kernel failed\n");
  121.         return -1;
  122.     }
  123.     else{
  124.         printf("rec from kernel success,data is:%s\n",mydata);
  125.         b = send_to_kernel(mydata);
  126.         if(b == 0){
  127.             printf("send to kernel success\n");
  128.             //return 0;
  129.             mydata = NULL;
  130.         }
  131.         else{
  132.             printf("send to kernel failed\n");
  133.             return -1;
  134.             mydata = NULL;
  135.         }
  136.     }
  137.     }

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