Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258647
  • 博文数量: 86
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 640
  • 用 户 组: 普通用户
  • 注册时间: 2018-10-15 14:13
个人简介

搭建一个和linux开发者知识共享和学习的平台

文章分类

全部博文(86)

文章存档

2023年(24)

2022年(27)

2019年(8)

2018年(27)

分类: LINUX

2022-10-21 15:11:39

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
 




typedef unsigned char    UI_08;
typedef unsigned short  UI_16;
typedef unsigned int    UI_32;
typedef char            SI_08;
typedef short            SI_16;
typedef int              SI_32;




#define NETLINK_TEST 30
#define MSG_LEN 125
#define USER_PORT 100


struct sock *nlsock=NULL;
extern struct net init_net;


int send_umsg(char *buf, uint16_t len)
{
struct sk_buff *nl_skb;
struct nlmsghdr *nl_h;
int ret;

//创建sk_buff空间
nl_skb=nlmsg_new(len, GFP_ATOMIC);
if(!nl_skb)
{
printk("netlink alloc failed\n");
return -1;
}
//设置netlink消息头部
nl_h=nlmsg_put(nl_skb,0,0,NETLINK_TEST,len,0);
if(nl_h==NULL)
{
printk("nlmsg_put failed\n");
nlmsg_free(nl_skb);
return -1;
}
memcpy(nlmsg_data(nl_h),buf,len);
ret=netlink_unicast(nlsock,nl_skb,USER_PORT,MSG_DONTWAIT);

return ret;
}


static void recv_umsg(struct sk_buff *skb)
{
struct nlmsghdr *nl_h=NULL;
char *umsg=NULL;
char *kmsg="hello netlink users";

if(skb->len>=nlmsg_total_size(0))
{
nl_h=nlmsg_hdr(skb);
umsg=NLMSG_DATA(nl_h);
if(umsg)
{
printk("kernel recv from user:%s\n",umsg);
send_umsg(kmsg,strlen(kmsg));
}
}
}


struct netlink_kernel_cfg nl_cfg ={
.input=recv_umsg,
};


int nlink_demo_init(void)
{
nlsock=(struct sock *)netlink_kernel_create(&init_net,NETLINK_TEST,&nl_cfg);
if(nlsock==NULL)
{
printk("netlink_kernel_create error\n");
return -1;
}
printk("nlink demo init\n");
return 0;
}


void nlink_demo_exit(void)
{
if(nlsock)
{
netlink_kernel_release(nlsock);
nlsock=NULL;
}
printk("netlink demo exit\n");
}


module_init(nlink_demo_init);
module_exit(nlink_demo_exit);




MODULE_LICENSE("GPL");














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