Here is a very simple program sending NL80211_CMD_GET_INTERFACEand parsing the type of interface returned by the attribute NL80211_CMD_GET_INTERFACE.
Beware, there are very few errors here, you should not use any of this program as it is! Almost all of these features may fail.
#include "netlink/netlink.h"
#include "netlink/genl/genl.h"
#include "netlink/genl/ctrl.h"
#include <net/if.h>
#include "nl80211.h"
static int expectedId;
static int nlCallback(struct nl_msg* msg, void* arg)
{
struct nlmsghdr* ret_hdr = nlmsg_hdr(msg);
struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
if (ret_hdr->nlmsg_type != expectedId)
{
return NL_STOP;
}
struct genlmsghdr *gnlh = (struct genlmsghdr*) nlmsg_data(ret_hdr);
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (tb_msg[NL80211_ATTR_IFTYPE]) {
int type = nla_get_u32(tb_msg[NL80211_ATTR_IFTYPE]);
printf("Type: %d", type);
}
}
int main(int argc, char** argv)
{
int ret;
nl_sock* sk = nl_socket_alloc();
genl_connect(sk);
expectedId = genl_ctrl_resolve(sk, "nl80211");
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM,
nlCallback, NULL);
nl_msg* msg = nlmsg_alloc();
nl80211_commands cmd = NL80211_CMD_GET_INTERFACE;
int ifIndex = if_nametoindex("wlan0");
int flags = 0;
genlmsg_put(msg, 0, 0, expectedId, 0, flags, cmd, 0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifIndex);
ret = nl_send_auto_complete(sk, msg);
nl_recvmsgs_default(sk);
return 0;
nla_put_failure:
nlmsg_free(msg);
return 1;
}
source
share