代码在这里。
struct netlink_data * netlink_init(struct netlink_config *cfg)
{
struct netlink_data *netlink;
struct sockaddr_nl local;
netlink = os_zalloc(sizeof(*netlink));
if (netlink == NULL)
return NULL;
netlink->sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (netlink->sock < 0) {
wpa_printf(MSG_ERROR, "netlink: Failed to open netlink "
"socket: %s", strerror(errno));
netlink_deinit(netlink);
return NULL;
}
os_memset(&local, 0, sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_groups = RTMGRP_LINK;
if (bind(netlink->sock, (struct sockaddr *) &local, sizeof(local)) < 0)
{
wpa_printf(MSG_ERROR, "netlink: Failed to bind netlink "
"socket: %s", strerror(errno));
netlink_deinit(netlink);
return NULL;
}
eloop_register_read_sock(netlink->sock, netlink_receive, netlink,
NULL);
netlink->cfg = cfg;
return netlink;
}
回调函数是 netlink_receive 。