1.内核态 socket API
内核态socket编程的过程和用户态下的socket编程流程一样,但是接口不同。Kernel提供了一组内核态的socket API,基本上在用户态的sockt API在内核中都有对应的API。 在net/socket.c中可以看到如下导出符号:
EXPORT_SYMBOL(kernel_sendmsg);
EXPORT_SYMBOL(kernel_recvmsg);
EXPORT_SYMBOL(sock_create_kern);
EXPORT_SYMBOL(sock_release);
EXPORT_SYMBOL(kernel_bind);
EXPORT_SYMBOL(kernel_listen);
EXPORT_SYMBOL(kernel_accept);
EXPORT_SYMBOL(kernel_connect);
EXPORT_SYMBOL(kernel_getsockname);
EXPORT_SYMBOL(kernel_getpeername);
EXPORT_SYMBOL(kernel_getsockopt);
EXPORT_SYMBOL(kernel_setsockopt);
EXPORT_SYMBOL(kernel_sendpage);
EXPORT_SYMBOL(kernel_sock_ioctl);
EXPORT_SYMBOL(kernel_sock_shutdown);
下面以两个内核模块间利用socket进行通信为例来讲述内核态下socket编程。
server内核模块功能是:建立套接字,绑定端口,监听端口,等待client的连接,并接收client发来的数据。
client内核模块功能是:建立套接字,和server建立连接,并向server发送数据。
2. server模块
/*server.c*/
#include
#include
#include
#include
#include
#include
int myserver(void){
struct socket *sock,*client_sock;
struct sockaddr_in s_addr;
unsigned short portnum=0x8888;
int ret=0;
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(portnum);
s_addr.sin_addr.s_addr=htonl(INADDR_ANY);
sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
client_sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
/*create a socket*/
ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
if(ret){
printk("server:socket_create error!\n");
}
printk("server:socket_create ok!\n");
/*bind the socket*/
ret=sock->ops->bind(sock,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
if(ret<0){
printk("server: bind error\n");
return ret;
}
printk("server:bind ok!\n");
/*listen*/
ret=sock->ops->listen(sock,10);
if(ret<0){
printk("server: listen error\n");
return ret;
}
printk("server:listen ok!\n");
ret=sock->ops->accept(sock,client_sock,10);if(ret<0){
printk("server:accept error!\n");
return ret;
}
printk("server: accept ok, Connection Established\n");
/*kmalloc a receive buffer*/
char *recvbuf=NULL;
recvbuf=kmalloc(1024,GFP_KERNEL);
if(recvbuf==NULL){
printk("server: recvbuf kmalloc error!\n");
return -1;
}
memset(recvbuf, 0, sizeof(recvbuf));
/*receive message from client*/
struct kvec vec;
struct msghdr msg;
memset(&vec,0,sizeof(vec));
memset(&msg,0,sizeof(msg));
vec.iov_base=recvbuf;
vec.iov_len=1024;
ret=kernel_recvmsg(client_sock,&msg,&vec,1,1024,0); /*receive message*/
printk("receive message:\n %s\n",recvbuf);
/*release socket*/
sock_release(sock);
sock_release(client_sock);
return ret;
}
static int server_init(void){
printk("server init:\n");
return (myserver());
}
static void server_exit(void){
printk("good bye\n");
}
module_init(server_init);
module_exit(server_exit);
MODULE_LICENSE("GPL");
/*client.c*/
#include
#include
#include
#include
#include
#include
int myclient(void){
struct socket *sock;
struct sockaddr_in s_addr;
unsigned short portnum=0x8888;
int ret=0;
memset(&s_addr,0,sizeof(s_addr));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(portnum);
s_addr.sin_addr.s_addr=in_aton("192.168.209.134"); /*server ip is 192.168.209.134*/
sock=(struct socket *)kmalloc(sizeof(struct socket),GFP_KERNEL);
/*create a socket*/
ret=sock_create_kern(AF_INET, SOCK_STREAM,0,&sock);
if(ret<0){
printk("client:socket create error!\n");
return ret;
}
printk("client: socket create ok!\n");
/*connect server*/
ret=sock->ops->connect(sock,(struct sockaddr *)&s_addr, sizeof(s_addr),0);
if(ret!=0){
printk("client:connect error!\n");
return ret;
}
printk("client:connect ok!\n");
/*kmalloc sendbuf*/
char *sendbuf=NULL;
sendbuf=kmalloc(1024,GFP_KERNEL);
if(sendbuf==NULL){
printk("client: sendbuf kmalloc error!\n");
return -1;
}
memset(sendbuf,1,1024);
struct kvec vec;
struct msghdr msg;
vec.iov_base=sendbuf;
vec.iov_len=1024;
memset(&msg,0,sizeof(msg));
ret=kernel_sendmsg(sock,&msg,&vec,1,1024); /*send message */
if(ret<0){
printk("client: kernel_sendmsg error!\n");
return ret;
}else if(ret!=1024){
printk("client: ret!=1024");
}
printk("client:send ok!\n");
return ret;
}
static int client_init(void){
printk("client:init\n");
return (myclient());
}
static void client_exit(void){
printk("client exit!\n");
}
module_init(client_init);
module_exit(client_exit);
MODULE_LICENSE("GPL");
编译上面两个模块,首先在server主机上加载server模块,然后在client主机上加载client模块。使用dmesg查看输出信息。