Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1096640
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2010-06-17 11:02:22

#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");


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

chinaunix网友2010-06-20 10:35:55

你好!博客狠棒cheapMBT Shoes on sale! MBT Shoes are the combination of quality and fashion. MBT Shoes are perfect for lovers of the outdoors, hikers, mountain climbers, campers, and nature enthusiasts. MBT Lami Shoe are perfect for the rugged and the casual. In fact, they are perfect for just about anyone an