我正在this question and answer上学习netlink的例子。
但是,我在源代码中没有看到一种connection identifier。可以这样说:
内核
my_nl_sock = netlink_kernel_create(&init_net, NETLINK_USERSOCK, 0,
my_nl_rcv_msg, NULL, THIS_MODULE);用户空间
nls = nl_socket_alloc();
ret = nl_connect(nls, NETLINK_USERSOCK);
ret = nl_send_simple(nls, MY_MSG_TYPE, 0, msg, sizeof(msg));NETLINK_USERSOCK和MY_MSG_TYPE似乎不是connection identifier。
在这种情况下,netlink如何知道哪些数据来自哪个用户空间应用程序或内核模块,以及数据应该来自哪个用户空间应用程序或内核模块?
在我看来,netlink从用户空间应用程序或内核模块接收数据并进行广播。每个连接到netlink的应用程序或模块都会检查消息类型,如果数据被指定为“me”。
我想得对吗?
发布于 2015-06-18 14:51:21
首先,我建议阅读一些文档,例如howto
要进行通信,您必须使用支持的操作注册一个家庭。它可以通过以下功能来完成
int genl_register_family( struct genl_family *family)
int genl_register_ops( struct genl_family * family, struct genl_ops *ops)家庭定义的一个例子:
/*
* Attributes (variables): the index in this enum is used as a reference for the type,
* userspace application has to indicate the corresponding type
*/
enum {
CTRL_ATT_R_UNSPEC = 0,
CTRL_ATT_CNT_SESSIONS,
__CTRL_ATT_R_MAX
};
#define CTRL_ATT_R_MAX ( __CTRL_ATT_R_MAX - 1 )
#define CTRL_FAMILY "your-family"
#define CTRL_PROTO_VERSION 1
/* Family definition */
static struct genl_family ctrl_bin_gnl_family = {
.id = GENL_ID_GENERATE, // genetlink should generate an id
.hdrsize = 0,
.name = CTRL_FAMILY, // the name of this family, used by userspace application
.version = CTRL_PROTO_VERSION, // version number
.maxattr = CTRL_ATT_R_MAX, // max number of attr
};操作定义的示例:
struct genl_ops ctrl_info = {
.cmd = CTRL_CMD_INFO,
.flags = 0,
.policy = 0, // you can use policy if you need
.doit = 0, // set this callback if this op does some interval stuff
.dumpit = __info, // set this callback if this op dump data
};之后,你可以在你的用户空间应用程序,你的家庭和操作沟通。连接:
struct nl_sock * _nl = nl_socket_alloc();
int ret = genl_connect(nl);
// test if fail
int gid = genl_ctrl_resolve( nl, CTRL_FAMILY );
// test if fail发送信息操作
struct nl_msg * msg = msg_alloc(
CTRL_CMD_INFO,
NLM_F_DUMP
);
int ret = nl_send_auto(_nl, msg );
// test if fail
// wait for the ack
// read a reply https://stackoverflow.com/questions/30905830
复制相似问题