分类: LINUX
2013-05-07 09:56:55
内核态
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
DECLARE_MUTEX(receive_sem);
unsigned int i;
static struct sock *nlfd;
static void kernel_receive(struct sock *sk, int len)
{
struct sk_buff *send_skb;
int user_pid;
printk("receive a netlink \n");
do
{
struct sk_buff *skb;
if(down_trylock(&receive_sem))
return;
while((skb = skb_dequeue(&sk->receive_queue)) != NULL)
{
printk("skb->len = %x\n",skb->len);
for(i=0;i
printk("skb data are %x\n",skb->data[i]);
user_pid=skb->data[0];
// kfree_skb(skb);
send_skb=alloc_skb(102,GFP_ATOMIC);
for(i=0;i<100;i++)
send_skb->data[i]=0x66;
send_skb->len=100;
printk("skb->data[0] = %x\n",user_pid);
netlink_unicast(nlfd,send_skb,user_pid,MSG_DONTWAIT);
up(&receive_sem);
}
}while(nlfd && nlfd->receive_queue.qlen);
}
static int __init netlink_init(void)
{
// struct sk_buff *skb;
struct sk_buff *send_skb;
printk("22 netlink kernel start\n");
nlfd = netlink_kernel_create(NETLINK_USERSOCK,kernel_receive);
if(!nlfd)
{
printk("can not create a netlink socket\n");
return -1;
}
printk("nlfd=%x\n netlink_kernel_create success\n",nlfd);
/* skb=alloc_skb(10,GFP_ATOMIC);
skb->data[0]=0x55;
skb->data[1]=0x55;
netlink_unicast(nlfd,skb,0,MSG_DONTWAIT);*/
// if(skb)kfree_skb(skb);
/* printk("send skb to kernel\n");
send_skb=alloc_skb(102,GFP_ATOMIC);
for(i=0;i<100;i++)
send_skb->data[i]=0x66;
send_skb->len=100;
netlink_unicast(nlfd,send_skb,0,MSG_DONTWAIT);*/
return 0;
}
static void __exit netlink_fini(void)
{
if(nlfd)
{
sock_release(nlfd->socket);
}
printk("exit \n");
}
module_init(netlink_init);
module_exit(netlink_fini);
用户态****************************
#include
#include
#include
#include
#include
#include
#include
#include
#include "imp2.h"
unsigned int i;
static int skfd;
struct msg_to_kernel
{
struct nlmsghdr hdr;
};
struct u_packet_info
{
struct nlmsghdr hdr;
struct packet_info p_info;
};
static void sig_int(int signo)
{
struct sockaddr_nl kpeer;
struct msg_to_kernel message;
memset(&kpeer,0,sizeof(kpeer));
kpeer.nl_family=AF_NETLINK;
kpeer.nl_pid=0;
kpeer.nl_groups=0;
memset(&message,0,sizeof(message));
message.hdr.nlmsg_len=NLMSG_LENGTH(0);
message.hdr.nlmsg_flags=0;
message.hdr.nlmsg_type=IMP2_CLOSE;
message.hdr.nlmsg_pid=getpid();
sendto(skfd,&message,message.hdr.nlmsg_len,0,(struct sockaddr *)(&kpeer),sizeof(kpeer));
close(skfd);
exit(0);
}
main()
{
struct sockaddr_nl local;
struct sockaddr_nl kpeer;
struct msg_to_kernel message;
struct u_packet_info info;
struct in_addr addr;
int kpeerlen;
int rcvlen=0;
int sendlen=0;
char data[100]={0x00,0x01,0x02,0x03,0x56};
char re_data[10]={0x00,0x00,0x00,0x00};
unsigned int i;
printf(" test1\n");
skfd=socket(PF_NETLINK,SOCK_RAW,NETLINK_USERSOCK);
if(skfd<0)
{
printf("can not create a netlink socket\n");
exit(0);
}
memset(&local,0,sizeof(local));
local.nl_family=AF_NETLINK;
local.nl_pid=getpid();
local.nl_groups=0;
if(bind(skfd,(struct sockaddr *)&local,sizeof(local))!=0)
{
printf("bind() error\n");
return -1;
}
// signal(SIGINT,sig_int);
memset(&kpeer,0,sizeof(kpeer));
kpeer.nl_family=AF_NETLINK;
kpeer.nl_pid=getpid();
kpeer.nl_groups=0;
memset(&message,0,sizeof(message));
message.hdr.nlmsg_len=NLMSG_LENGTH(0);
message.hdr.nlmsg_flags=0;
message.hdr.nlmsg_type=IMP2_U_PID;
message.hdr.nlmsg_pid=local.nl_pid;
data[0]=local.nl_pid;
printf(" ap pid = %x\n",local.nl_pid);
for(i=1;i<100;i++)
data[i]=0x77;
sendto(skfd,&data,100,0,(struct sockaddr *)&kpeer,sizeof(kpeer));
while(1)
{
sleep(1);
printf(" receiving ....\n");
kpeerlen=sizeof(struct sockaddr_nl);
printf(" 1##\n");
rcvlen=recvfrom(skfd,&re_data,200,0,(struct sockaddr *)&kpeer,&kpeerlen);
printf("rcvlen = %d\n",rcvlen);
if(rcvlen)
{
printf("receive data\n");
for(i=0;i
}
}
return 0;
}