wifi配置工具iw源码解析

iw是一个基于nl80211接口的无线配置工具,用于替代原先基于wext接口的iwconfig。iw源码可以在网址 https://www.kernel.org/pub/software/network/iw/ 获取,或者使用git命令从http://git.kernel.org/?p=linux/kernel/git/jberg/iw.git. 中下载。

1、简单的nl80211程序

iw的源码主体在iw.c文件里,其他文件都是对iw相关的命令选项的实现。
在iw-4.9版本中,iw.c源代码有586行,并不是很多,如果去掉代码中的参数解析部分和命令选项匹配部分,就可以得到iw的最核心的代码,如下面的代码所示。

/**
 * 该程序使用nl80211命令从内核中读取wlan0接口信息,
 * 然后在回调函数中解析信息,打印出wlan0的接口类型。
 */
#include "netlink/netlink.h"
#include "netlink/genl/genl.h"
#include "netlink/genl/ctrl.h"
#include <net/if.h>

//从iw复制过来
#include "nl80211.h"

static int expected_id;

static int nl_callback(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 != expected_id)
    {
        // what is this??
        return NL_STOP;
    }

    struct genlmsghdr *gnlh = (struct genlmsghdr*) nlmsg_data(ret_hdr);

    nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0</
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值