linux与应用层通信方式 之nf_sock_opt

  1. //内核部分代码。大家可以COPY ipt_sockopts

[cpp] view plain copy

  1. //#include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/init.h>
  5. #include "hello_sock_opt.h"
  6. #include <linux/kmod.h>
  7. #include <linux/module.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/netfilter/x_tables.h>
  10. #include <linux/netfilter_bridge/ebtables.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <asm/uaccess.h>
  15. #include <linux/smp.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/audit.h>
  18. #include <net/sock.h>
  19. char hello[10][20]; 
  20. static int hello_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len) 
  21. int ret=0; 
  22. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 
  23. return -EPERM; 
  24.         printk("%s: len is %d \n",__func__,len); 
  25. switch (cmd) { 
  26. case HELLO_LISA: 
  27. if (copy_from_user(hello[0], user,20) != 0) 
  28. return -EFAULT; 
  29. break; 
  30. case HELLO_MONA: 
  31. if (copy_from_user(hello[1], user,20) != 0) 
  32. return -EFAULT; 
  33. break; 
  34. default: 
  35.                 printk("hello_set_ctl:  unknown request %i\n", cmd); 
  36.                 ret = -EINVAL; 
  37.         } 
  38. return ret; 
  39. static int hello_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) 
  40. int ret=0; 
  41. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 
  42. return -EPERM; 
  43. switch (cmd) { 
  44. case HELLO_LISA: 
  45. if (copy_to_user(user,hello[0] , 20) != 0) 
  46.                 { 
  47. break; 
  48.                 } 
  49. break; 
  50. case HELLO_MONA: 
  51. if (copy_to_user(user,hello[1] , 20) != 0) 
  52. break; 
  53. else
  54. break; 
  55. default: 
  56.                 printk("hello_get_ctl:  unknown request %i\n", cmd); 
  57.                 ret = -EINVAL; 
  58.         } 
  59. return ret; 
  60. static struct nf_sockopt_ops   arpt_sockopts = { 
  61.         .pf             = PF_INET, 
  62.         .set_optmin     = HELLO_BASE_CTL, 
  63.         .set_optmax     = HELLO_SO_SET_MAX+1, 
  64.         .set            = hello_set_ctl, 
  65.         .get_optmin     = HELLO_BASE_CTL, 
  66.         .get_optmax     = HELLO_SO_SET_MAX+1, 
  67.         .get            = hello_get_ctl, 
  68.         .owner          = THIS_MODULE, 
  69. }; 
  70. static int simple_init(void) 
  71. int ret; 
  72. /* Register setsockopt */
  73.         ret = nf_register_sockopt(&arpt_sockopts); 
  74. if (ret < 0) 
  75.         { 
  76.                 printk("nf_register_sockopt error! \n"); 
  77. return -1; 
  78.         } 
  79.     printk("hello !!\n"); 
  80. return 0; 
  81. static void simple_cleanup(void) 
  82.         nf_unregister_sockopt(&arpt_sockopts); 
  83.         printk("Hello byebye\n"); 
  84. module_init(simple_init); 
  85. module_exit(simple_cleanup); 

[cpp] view plain copy

  1. /*
  2. 上下通信的通用头文件。
  3. */
  4. #define  HELLO_BASE_CTL  102400 //在系统中这个号不能有重复的。建议最好是取较大值。
  5. #define  HELLO_LISA      HELLO_BASE_CTL+1
  6. #define  HELLO_MONA      HELLO_BASE_CTL+2
  7. #define  HELLO_SO_SET_MAX  (HELLO_BASE_CTL+5)

[cpp] view plain copy

  1. #include <sys/socket.h>
  2.        #include <netinet/in.h>
  3.        #include <netinet/ip.h> /* superset of previous */
  4. #include <sys/types.h>          /* See NOTES */
  5. #include <sys/socket.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include "hello_sock_opt.h"
  11. int common_set_sock_opt(int id,void* msg,int len) 
  12. int sock; 
  13. int ret; 
  14.         sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 
  15. if(sock <=0){ 
  16.                 printf("socket error\n"); 
  17. return -1; 
  18.         } 
  19.         ret=setsockopt(sock,0,id,msg,len); 
  20.         close(sock); 
  21. return ret; 
  22. int common_get_sock_opt(int id,void* msg,int* len) 
  23. int sock; 
  24. int ret; 
  25.         sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 
  26. if(sock <=0){ 
  27.                 printf("socket error\n"); 
  28. return -1; 
  29.         } 
  30.         ret=getsockopt(sock,0,id,msg,len); 
  31.         close(sock); 
  32. return ret; 
  33. int __set_lisa(char* s) 
  34. return common_set_sock_opt(HELLO_LISA,s,strlen(s)); 
  35. int __get_lisa(char* s,int *len) 
  36. return common_get_sock_opt(HELLO_LISA,s,len); 
  37. void main(int argc,char* argv[]) 
  38. char ret[100]={0}; 
  39. int len=0; 
  40. int t; 
  41. if(argc ==2){ 
  42.                 printf("argv : %s :%s \n",argv[0],argv[1]); 
  43.                 t = __set_lisa(argv[1]);  //把参数配置到内核。
  44. if(t<0) 
  45.                         perror("__set_lisa:"); 
  46.                 __get_lisa(ret,&len);  //从内核读出信息。
  47. if(t<0) 
  48.                         perror("__GET_lisa:"); 
  49. else
  50.                 { 
  51.                         printf("ret %s; len %d\n",ret,len); 
  52.                 } 
  53.         } 
  54. }

转载于:https://www.cnblogs.com/yaxinsn/p/8376827.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值