通用netlink的基本使用(1) NETLINK_GENERIC

本文详细介绍了NETLINK_GENERIC在内核和应用部分的基础使用,包括如何在内核模块中设置和接收消息,以及应用程序如何通过netlink套接字与内核交互进行数据传递。
摘要由CSDN通过智能技术生成

内核部分

#include <net/genetlink.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

int ctl_test_function(struct sk_buff *skb, struct genl_info *info)
{
    printk("get process pid=%d  cmd=%d pid=%d type=%d\n",info->snd_pid,info->genlhdr->cmd,info->nlhdr->nlmsg_pid,info->nlhdr->nlmsg_type);
    return 0;
}

struct genl_family test_ctl = {
    .id=1023, //消息类型
    .name="testnet",
    .version=0x02,
};

struct genl_ops test_ctl_ops={
    .cmd = 100,//自己先设置一个命令
    .doit = ctl_test_function,
};

static int __init testnlk_init(void)
{
    if(genl_register_family(&test_ctl) != 0)
    {
        printk("register faimly error\n");
        return -1;
    }

    if(genl_register_ops(&test_ctl,&test_ctl_ops) !=  0)
    {
        printk("Register ops error\n");
        goto out;
    }

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值