Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411437
  • 博文数量: 96
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 415
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-22 09:08
个人简介

最近的研究方向:Nginx

文章分类
文章存档

2017年(2)

2016年(59)

2015年(35)

我的朋友

分类: LINUX

2016-09-14 13:45:35

1.内核态 socket API

内核态socket编程的过程和用户态下的socket编程流程一样,但是接口不同。Kernel提供了一组内核态的socket API,基本上在用户态的sockt API在内核中都有对应的API。 在net/socket.c的最后部分可以看到如下导出符号:

点击(此处)折叠或打开

  1. EXPORT_SYMBOL(sock_create);
  2. EXPORT_SYMBOL(sock_create_kern);
  3. EXPORT_SYMBOL(sock_create_lite);
  4. EXPORT_SYMBOL(sock_map_fd);
  5. EXPORT_SYMBOL(sock_recvmsg);
  6. EXPORT_SYMBOL(sock_register);
  7. EXPORT_SYMBOL(sock_release);
  8. EXPORT_SYMBOL(sock_sendmsg);
  9. EXPORT_SYMBOL(sock_unregister);
  10. EXPORT_SYMBOL(sock_wake_async);
  11. EXPORT_SYMBOL(sockfd_lookup);
  12. EXPORT_SYMBOL(kernel_sendmsg);
  13. EXPORT_SYMBOL(kernel_recvmsg);
  14. EXPORT_SYMBOL(kernel_bind);
  15. EXPORT_SYMBOL(kernel_listen);
  16. EXPORT_SYMBOL(kernel_accept);
  17. EXPORT_SYMBOL(kernel_connect);
  18. EXPORT_SYMBOL(kernel_getsockname);
  19. EXPORT_SYMBOL(kernel_getpeername);
  20. EXPORT_SYMBOL(kernel_getsockopt);
  21. EXPORT_SYMBOL(kernel_setsockopt);
  22. EXPORT_SYMBOL(kernel_sendpage);
  23. EXPORT_SYMBOL(kernel_sock_ioctl);
  24. EXPORT_SYMBOL(kernel_sock_shutdown);

下面以两个内核模块间利用socket进行通信为例来讲述内核态下socket编程。

server内核模块功能是:建立套接字,绑定端口,监听端口,等待client的连接,并接收client发来的数据。

client内核模块功能是:建立套接字,和server建立连接,并向server发送数据。


2. server模块

点击(此处)折叠或打开

  1. /*server.c*/
  2. #include<linux/in.h>
  3. #include<linux/inet.h>
  4. #include<linux/socket.h>
  5. #include<net/sock.h>
  6.   
  7. #include<linux/init.h>
  8. #include<linux/module.h>
  9.   
  10. int myserver(void){
  11.   
  12.         struct socket *sock,*client_sock;
  13.         struct sockaddr_in s_addr;
  14.         unsigned short portnum=0x8888;
  15.         int ret=0;
  16.   
  17.         memset(&s_addr,0,sizeof(s_addr));
  18.         s_addr.sin_family=AF_INET;
  19.         s_addr.sin_port=htons(portnum);
  20.         s_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  21.   
  22.   
  23.         sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
  24.         client_sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
  25.   
  26.         /*create a socket*/
  27.         ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
  28.         if(ret){
  29.                 printk("server:socket_create error!\n");
  30.         }
  31.         printk("server:socket_create ok!\n");
  32.   
  33.         /*bind the socket*/
  34.         ret=sock->ops->bind(sock,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
  35.         if(ret<0){
  36.                 printk("server: bind error\n");
  37.                 return ret;
  38.         }
  39.         printk("server:bind ok!\n");
  40.   
  41.         /*listen*/
  42.         ret=sock->ops->listen(sock,10);
  43.         if(ret<0){
  44.                 printk("server: listen error\n");
  45.                 return ret;
  46.         }
  47.         printk("server:listen ok!\n");
  48.   
  49.         ret=sock->ops->accept(sock,client_sock,10);if(ret<0){
  50.                 printk("server:accept error!\n");
  51.                 return ret;
  52.         }
  53.   
  54.         printk("server: accept ok, Connection Established\n");
  55.   
  56.         /*kmalloc a receive buffer*/
  57.         char *recvbuf=NULL;
  58.         recvbuf=kmalloc(1024,GFP_KERNEL);
  59.         if(recvbuf==NULL){
  60.                 printk("server: recvbuf kmalloc error!\n");
  61.                 return -1;
  62.         }
  63.         memset(recvbuf, 0, sizeof(recvbuf));
  64.   
  65.         /*receive message from client*/
  66.         struct kvec vec;
  67.         struct msghdr msg;
  68.         memset(&vec,0,sizeof(vec));
  69.         memset(&msg,0,sizeof(msg));
  70.         vec.iov_base=recvbuf;
  71.         vec.iov_len=1024;
  72.         ret=kernel_recvmsg(client_sock,&msg,&vec,1,1024,0); /*receive message*/
  73.         printk("receive message:\n %s\n",recvbuf);
  74.           
  75.     /*release socket*/
  76.     sock_release(sock);
  77.         sock_release(client_sock);
  78.         return ret;
  79. }
  80.   
  81. static int server_init(void){
  82.         printk("server init:\n");
  83.         return (myserver());
  84. }
  85.   
  86. static void server_exit(void){
  87.         printk("good bye\n");
  88. }
  89.   
  90. module_init(server_init);
  91. module_exit(server_exit);
  92.   
  93. MODULE_LICENSE("GPL");
3.client模块

点击(此处)折叠或打开

  1. /*client.c*/
  2. #include<linux/in.h>
  3. #include<linux/inet.h>
  4. #include<linux/socket.h>
  5. #include<net/sock.h>
  6.   
  7. #include<linux/init.h>
  8. #include<linux/module.h>
  9.   
  10. int myclient(void){
  11.         struct socket *sock;
  12.         struct sockaddr_in s_addr;
  13.         unsigned short portnum=0x8888;
  14.         int ret=0;
  15.   
  16.         memset(&s_addr,0,sizeof(s_addr));
  17.         s_addr.sin_family=AF_INET;
  18.         s_addr.sin_port=htons(portnum);
  19.        
  20.         s_addr.sin_addr.s_addr=in_aton("192.168.209.134"); /*server ip is 192.168.209.134*/
  21.         sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
  22.   
  23.         /*create a socket*/
  24.         ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
  25.         if(ret<0){
  26.                 printk("client:socket create error!\n");
  27.                 return ret;
  28.         }
  29.         printk("client: socket create ok!\n");
  30.   
  31.         /*connect server*/
  32.         ret=sock->ops->connect(sock,(struct sockaddr *)&s_addr, sizeof(s_addr),0);
  33.         if(ret!=0){
  34.                 printk("client:connect error!\n");
  35.                 return ret;
  36.         }
  37.         printk("client:connect ok!\n");
  38.   
  39.         /*kmalloc sendbuf*/
  40.         char *sendbuf=NULL;
  41.         sendbuf=kmalloc(1024,GFP_KERNEL);
  42.         if(sendbuf==NULL){
  43.                 printk("client: sendbuf kmalloc error!\n");
  44.                 return -1;
  45.         }
  46.         memset(sendbuf,1,1024);
  47.           
  48.     struct kvec vec;
  49.         struct msghdr msg;
  50.   
  51.         vec.iov_base=sendbuf;
  52.         vec.iov_len=1024;
  53.   
  54.         memset(&msg,0,sizeof(msg));
  55.   
  56.         ret=kernel_sendmsg(sock,&msg,&vec,1,1024); /*send message */
  57.         if(ret<0){
  58.                 printk("client: kernel_sendmsg error!\n");
  59.                 return ret;
  60.         }else if(ret!=1024){
  61.                 printk("client: ret!=1024");
  62.         }
  63.         printk("client:send ok!\n");
  64.   
  65.         return ret;
  66. }
  67.   
  68. static int client_init(void){
  69.         printk("client:init\n");
  70.         return (myclient());
  71. }
  72.   
  73. static void client_exit(void){
  74.         printk("client exit!\n");
  75. }
  76.   
  77. module_init(client_init);
  78. module_exit(client_exit);
  79. MODULE_LICENSE("GPL");

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