#include <asm/atomic.h>
#include <asm/checksum.h>
#include <asm/unaligned.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/types.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/sysctl.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <linux/route.h>
#include <linux/stddef.h>
#include <linux/mutex.h>
#include <linux/icmp.h>
#include <net/net_namespace.h>
#include <net/sock.h>
#include <net/route.h>
#include <net/inet_connection_sock.h>
#include <net/request_sock.h>
#define err(msg) printk(KERN_ERR "%s failed.\n", msg)
#define SA struct sockaddr
static struct task_struct *task;
static int threadfn(void *data)
{
struct socket *sock;
struct sockaddr_in addr;
if (sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock) < 0) {
err("sock_create_kern");
goto out;
}
sock->sk->sk_reuse = 1;
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(8888);
if (kernel_bind(sock, (SA *)&addr, sizeof(addr)) < 0) {
err("kernel_bind");
goto err;
}
if (kernel_listen(sock, 1024) < 0) {
err("kernel_listen");
goto err;
}
while (!kthread_should_stop()) {
struct socket *newsock;
struct inet_connection_sock *icsk = inet_csk(sock->sk);
wait_event_interruptible(*sock->sk->sk_sleep,
!reqsk_queue_empty(&icsk->icsk_accept_queue) || kthread_should_stop());
if (!reqsk_queue_empty(&icsk->icsk_accept_queue)) {
if (kernel_accept(sock, &newsock, 0) != 0) {
err("kernel_accept");
goto err;
}
printk(KERN_INFO "accept a new connection request.\n");
sock_release(newsock);
}
}
err:
sock_release(sock);
out:
return 0;
}
static int __init main_init(void)
{
task = kthread_run(threadfn, NULL, "listen thread");
return 0;
}
static void __exit main_exit(void)
{
kthread_stop(task);
}
module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");
|
阅读(1310) | 评论(0) | 转发(0) |