linux nettlink套接字

用于处理用户空间和内核空间的通信机制

套接字地址结构函数:

struct sockaddr_nl

{

_kernel_sa_family_t           nl_family;    /af_netlink/

unsigned short                         nl_pad ;   

_u32                                  nl_pid;

_u32                                  nl_groups;                               /组播组掩码/

};


nl_family:始终为af_netlink

pad为0

pid值内核为0,在用户栈根据情况自动设置。


netlink消息报头。由结构nlmsghdr定义,长16字节。

nlmsg_len:

nlmsg_type:其中有四种基本netlik消息类型,noop,error,done,overrun。

nlmsg_flags;

nlmsg_seq;

nlmsg_pid;

属性头由nlattr定义

struct nlattr{

_u16 nla_len;

_u16 nla_type;};

nla_len;

nla_type:


通用netlink内核套接字由方法net_kernel_creat *net()创建,结构

static  int_net_init genl_pernet_init(struct net *net){

struct netlink_kernel_cfg cfg = {

input =genl_rcv,

cb_mutex =&genl_mutex,

flags =nl_cfg_f_nonroot_recv,

};

net->genl_sock =netlink_kernel_creatr(net,netlink_generic,&cfg);

}

创建通用netlink内核套接字自后,需要注册控制器簇(genl_ctrl)

static struct genl_family genl_ctrl={

id =genl_id_ctrl,

name="nlctrl"

cersion =0x2,

maxattr = ctrl_attr_max,

netnsok = true,}

创建和发送通用netlink消息,消息报头

struct genlmsghdr{

——u8 cmd;

——u8 version;

——u16 reserved;}


套接字监视接口

netlink_sock_diag

结构函数如下

索引协议号

sock_diag_handler

struct sock_diag_handler{

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章