#include
#include
#include
#include
#include
#include
#include
#include
static struct socket *sock;
static struct work_struct work;
static int udp_sendto(struct socket *sock, void * buff, size_t len,
unsigned flags, struct sockaddr *addr, int addr_len)
{
struct kvec vec;
struct msghdr msg;
vec.iov_base=buff;
vec.iov_len=len;
memset(&msg, 0x00, sizeof(msg));
msg.msg_name=addr;
msg.msg_namelen=addr_len;
msg.msg_flags = flags | MSG_DONTWAIT;
return kernel_sendmsg(sock, &msg, &vec, 1, len);
}
static char buffer[128];
static void sendmsg(void *dummy)
{
int n;
struct sockaddr_in addr;
memset(&addr, 0x00, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(1000);
addr.sin_addr.s_addr = in_aton("61.139.2.69");
n = udp_sendto(sock, buffer, 128, 0, (struct sockaddr *)&addr,
sizeof(addr));
printk("send %i bytes\n", n);
}
static int socket_init(void)
{
INIT_WORK(&work, sendmsg, NULL);
sock_create_kern(PF_INET, SOCK_DGRAM, 0, &sock);
schedule_work(&work);
return 0;
}
static void socket_exit(void)
{
sock_release(sock);
}
module_init(socket_init);
module_exit(socket_exit);
上面的例子2.6内核可以用,2.4的内核好像只能自己构造2,3,4层的头,然后用dev_qu
eue_xmit。
用sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)
创建socket结构.
用kernel_sendmsg(sock, &msg, &vec, x, xxx)发送数据包,
内核态的bsd socket和用户态的socket基本相同, 不过内核态用socket结构, 用户态用文件描述符, 内核的bind/connect/listen函数都用socket->ops->bind/connect/listen... 等, 发包和收包用kernel_sendmsg和kernel_recvmsg, 不过注意, kernel用kvec, 用户态用iovec, 不过他们可以相互转换. 用sourcenavigator看packetgen.c(一个内核发包模块)就明白了.
阅读(2941) | 评论(1) | 转发(1) |