- 论坛徽章:
- 0
|
代码如下:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/smp_lock.h>
#include <linux/types.h>
#include <linux/net.h>
#include <net/sock.h>
#include <linux/in.h>
#include <asm/socket.h>
//#include <linux/net.h>
#include <linux/syscalls.h>
//#include <sys/socket.h>
#define PKT_LEN 1500
static struct socket *ksock;
static int sockfd;
mm_segment_t fs;
struct ifreq ifr;
int handle_packet(void *arg)
{
struct ifreq ifr;
struct sockaddr_ll saddr;
char buff[PKT_LEN];
int len = 0;
int saddr_len = sizeof(saddr);
int ret;
memset(&saddr, 0, sizeof(struct sockaddr_ll));
saddr.sll_ifindex = ifr.ifr_ifindex;
saddr.sll_family = PF_PACKET;
ret = sys_recvfrom(sockfd, buff, PKT_LEN, 0, (struct sockaddr *)&saddr, &saddr_len);
printk("recv len:%d\n", ret);
//sys_recv(sockfd, buff, PKT_LEN,0);
for ( ;; ) {
printk("handle packet ... \n");
//sleep(5);
break;
}
ret = sys_sendto(sockfd, buff, len, 0, (struct sockaddr *)&saddr, saddr_len);
printk("send len:%d\n", ret);
//sock_release(ksock);
//set_fs(fs);
return 0;
}
int socket_init(void)
{
pid_t pid;
int ret;
//sys_chdir("tmp"); // test sys_* func
ret = sock_create(PF_PACKET, SOCK_RAW, __constant_htons(ETH_P_ALL), &ksock);
if (ret < 0) return -1;
printk("socket create succeed\n");
fs = get_fs();
set_fs(KERNEL_DS);
//strcpy(ifr.ifr_name, ifname);
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");
ret = ksock->ops->ioctl(ksock, SIOGIFINDEX, (unsigned long)&ifr);
if (ret < 0) return -1;
printk("ifindex:%d\n", ifr.ifr_ifindex);
printk("socket set succeed\n");
sockfd = sock_map_fd(ksock);
//pid = kernel_thread(&handle_packet ,NULL,CLONE_KERNEL);
//if (pid < 0) return -1;
sock_release(ksock);
set_fs(fs);
return 0;
}
int init_module(void)
{
int ret = socket_init();
if (ret == 0)
printk("socket module succeed...\n");
else
printk("socket module failed...\n");
return 0;
}
void cleanup_module(void)
{
//sock_release(ksock);
//set_fs(fs);
printk("socket module exit...\n");
}
编译的时候,提示Warning: "sys_sendto" [/root/sock_kern.ko] undefined!
Warning: "sys_recvfrom" [/root/sock_kern.ko] undefined!
inmod时,提示insmod: error inserting 'sock_kern.ko': -1 Unknown symbol in module
是不是不能调用sys_sendto、sys_recvfrom函数,还是缺少什么 |
|