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

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: C/C++

2014-12-26 18:11:50


点击(此处)折叠或打开

  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,int sock_fd){
  18.     struct sockaddr_nl sock_addr;
  19.     struct nlmsghdr *nlh = NULL;
  20.     struct msghdr msg;
  21.     struct iovec iov;
  22.     int ret;
  23.     
  24.     memset(&sock_addr,0,sizeof(sock_addr));
  25.     sock_addr.nl_family = AF_NETLINK;
  26.     sock_addr.nl_pid = 0;
  27.     sock_addr.nl_groups = netlink_group_mask(5);

  28.     ret = bind(sock_fd,(struct sockaddr *)&sock_addr,sizeof(sock_addr));
  29.     if(ret < 0){
  30.         printf("bind failed: %s\n",strerror(errno));
  31.         close(sock_fd);
  32.         return -1;
  33.     }
  34.     
  35.     nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
  36.     if(nlh == NULL){
  37.         perror("alloc mem failed\n");
  38.         return 1;
  39.     }

  40.     memset(nlh,0,MAX_PAYLOAD);
  41.     nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
  42.     nlh->nlmsg_pid = getpid();
  43.     nlh->nlmsg_type = NLMSG_NOOP;
  44.     nlh->nlmsg_flags = 0;

  45.     strcpy(NLMSG_DATA(nlh),data);

  46.     memset(&iov,0,sizeof(iov));
  47.     iov.iov_base = (void *)nlh;
  48.     iov.iov_len = nlh->nlmsg_len;

  49.     memset(&msg,0,sizeof(msg));
  50.     msg.msg_iov = &iov;
  51.     msg.msg_iovlen = 1;

  52.     sendmsg(sock_fd,&msg,0);
  53.     
  54.     free(nlh);
  55.     return 0;
  56. }

  57. int rec_from_kernel(char* data,int sock_fd){
  58.     struct sockaddr_nl sock_addr;
  59.     struct nlmsghdr *nlh = NULL;
  60.     struct msghdr msg;
  61.     struct iovec iov;
  62.     int ret;
  63.     
  64.     memset(&sock_addr,0,sizeof(sock_addr));
  65.     sock_addr.nl_family = AF_NETLINK;
  66.     sock_addr.nl_pid = 0;
  67.     sock_addr.nl_groups = netlink_group_mask(5);

  68.     ret = bind(sock_fd,(struct sockaddr *)&sock_addr,sizeof(sock_addr));
  69.     if(ret < 0){
  70.         printf("bind failed: %s\n",strerror(errno));
  71.         close(sock_fd);
  72.         return -1;
  73.     }
  74.     
  75.     nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
  76.     if(nlh == NULL){
  77.         perror("alloc mem failed\n");
  78.         return -1;
  79.     }

  80.     memset(nlh,0,MAX_PAYLOAD);
  81.     iov.iov_base = (void *)nlh;
  82.     iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD);

  83.     memset(&msg,0,sizeof(msg));
  84.     msg.msg_iov = &iov;
  85.     msg.msg_iovlen = 1;
  86.     recvmsg(sock_fd,&msg,0);
  87.     data = (char *)NLMSG_DATA(nlh);

  88.     free(nlh);
  89.     return 0;
  90. }

  91. int main(void){
  92.     int sock_fd = -1;
  93.     char* control_data = "userspace is ready for data";
  94.     char* mydata = NULL;
  95.     int a,b,c;
  96.     
  97.     sock_fd = socket(PF_NETLINK,SOCK_RAW,NETLINK_GENERIC);
  98.     if(sock_fd == -1){
  99.         perror("can't create netlink socket!\n");
  100.         return -1;
  101.     }
  102.     a = rec_from_kernel(mydata,sock_fd);
  103.     if(a == -1){
  104.         printf("rec from kernel failed\n");
  105.         return -1;
  106.     }
  107.     else{
  108.         printf("rec from kernel success,data is:%s\n",mydata);
  109.         b = send_to_kernel(mydata,sock_fd);
  110.         if(b == 0){
  111.             printf("send to kernel success\n");
  112.             return 0;
  113.         }
  114.         else{
  115.             printf("send to kernel failed\n");
  116.             return -1;
  117.         }
  118.     }
  119. }

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