kernel socket 实例
来源:互联网 发布:优化特色课程 编辑:程序博客网 时间:2024/06/03 07:00
#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 sockaddrstatic 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");
转自
http://blog.chinaunix.net/uid-26748923-id-3548615.html
- kernel socket 实例
- kernel socket 实例
- socket kernel
- Socket Kernel Source Chapter03 socket
- Socket Kernel Module 01
- socket programming in kernel
- kernel panic解法实例
- linux kernel module 实例
- socket实例
- Socket实例
- socket 实例
- socket 实例
- socket 实例
- socket实例
- socket实例
- socket 实例
- socket实例
- socket 实例
- 程序员考试说明
- java反射详解
- 蓝牙(Bluetooth)---源码目录及设置应用源码分析
- 软件设计师考试说明
- insmod: error inserting 'sequans_usb.ko': -1 Unknown symbol in module
- kernel socket 实例
- SICP中parallel-execute
- shell 脚本
- ibitis简介及实例
- android调试工具MAT--安装
- JAVA反射机制
- ubuntu下搭建svn步骤
- 系统架构设计师考试说明
- 今天开始学模式识别与机器学习Pattern Recognition and Machine Learning 书,章节1.1,多项式曲线拟合(Polynomial Curve Fitting)