hostapd_cli命令源码分析_hostapd_cli源码-程序员宅基地

技术标签: 无线网络  hostapd  Linux  c语言  linux  wireless  Linux C  源码  linux网络  

hostapd提供了控制命令hostapd_cli,使用方法可以查看usage。

这里写图片描述

源码中的main函数:

int main(int argc, char *argv[])
{
    int warning_displayed = 0;
    int c;
    int daemonize = 0;

    if (os_program_init())//不同操作体统平台下执行不同的环境初始化
        return -1;

    for (;;) {   //执行getopt的选择,h则显示usage
        c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
        if (c < 0)
            break;
        switch (c) {
        case 'a':
            action_file = optarg;
            break;
        case 'B':    //后台执行
            daemonize = 1;
            break;
        case 'G':
            ping_interval = atoi(optarg);
            break;
        case 'h':
            usage();
            return 0;
        case 'v':
            printf("%s\n", hostapd_cli_version);
            return 0;
        case 'i':    //选择控制的网络接口
            os_free(ctrl_ifname);
            ctrl_ifname = os_strdup(optarg);
            break;
        case 'p':
            ctrl_iface_dir = optarg;
            break;
        case 'P':
            pid_file = optarg;
            break;
        case 's':
            client_socket_dir = optarg;
            break;
        default:
            usage();
            return -1;
        }
    }

    interactive = (argc == optind) && (action_file == NULL);

    if (interactive) {
        printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
    }

    if (eloop_init())//创建和初始化epoll
        return -1;

    for (;;) {
        if (ctrl_ifname == NULL) {
            struct dirent *dent;
            DIR *dir = opendir(ctrl_iface_dir);
            if (dir) {
                while ((dent = readdir(dir))) {
                    if (os_strcmp(dent->d_name, ".") == 0
                        ||
                        os_strcmp(dent->d_name, "..") == 0)
                        continue;
                    printf("Selected interface '%s'\n",
                           dent->d_name);
                    ctrl_ifname = os_strdup(dent->d_name);
                    break;
                }
                closedir(dir);
            }
        }
        ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);//连接hostapd进程
        if (ctrl_conn) {
            if (warning_displayed)
                printf("Connection established.\n");
            break;
        }

        if (!interactive) {
            perror("Failed to connect to hostapd - "
                   "wpa_ctrl_open");
            return -1;
        }

        if (!warning_displayed) {
            printf("Could not connect to hostapd - re-trying\n");
            warning_displayed = 1;
        }
        os_sleep(1, 0);
        continue;
    }

    if (interactive || action_file) {
        if (wpa_ctrl_attach(ctrl_conn) == 0) {
            hostapd_cli_attached = 1;
            register_event_handler(ctrl_conn);
        } else {
            printf("Warning: Failed to attach to hostapd.\n");
            if (action_file)
                return -1;
        }
    }

    if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
        return -1;
//以下是控制操作的核心
    if (interactive)
        hostapd_cli_interactive();
    else if (action_file)
        hostapd_cli_action(ctrl_conn);
    else
        wpa_request(ctrl_conn, argc - optind, &argv[optind]);

    unregister_event_handler(ctrl_conn);
    os_free(ctrl_ifname);
    eloop_destroy();
    hostapd_cli_cleanup();
    return 0;
}

如果是交互状态则,执行交换式的命令行操作hostapd_cli_interactive,执行是文件操作,则执行hostapd_cli_action 函数,其它情况,执行wpa_request 函数。

hostapd_cli_interactive 函数操作如下:

static void hostapd_cli_interactive(void)
{
    printf("\nInteractive mode\n\n");

    eloop_register_signal_terminate(hostapd_cli_eloop_terminate, NULL);
    edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
          hostapd_cli_edit_completion_cb, NULL, NULL, NULL);
    eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);

    eloop_run();

    cli_txt_list_flush(&stations);
    edit_deinit(NULL, NULL);
    eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
}

主要是调用了edit_init 函数,关联了hostapd_cli_edit_cmd_cbhostapd_cli_edit_eof_cbhostapd_cli_edit_completion_cb三个操作函数:

edit_init(hostapd_cli_edit_cmd_cb, hostapd_cli_edit_eof_cb,
      hostapd_cli_edit_completion_cb, NULL, NULL, NULL); 

最后的执行还是调用操作了wpa_request



static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
{
    const struct hostapd_cli_cmd *cmd, *match = NULL;
    int count;

    count = 0;
    cmd = hostapd_cli_commands;
    while (cmd->cmd) {
        if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
            match = cmd;
            if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
                /* we have an exact match */
                count = 1;
                break;
            }
            count++;
        }
        cmd++;
    }

    if (count > 1) {
        printf("Ambiguous command '%s'; possible commands:", argv[0]);
        cmd = hostapd_cli_commands;
        while (cmd->cmd) {
            if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
                0) {
                printf(" %s", cmd->cmd);
            }
            cmd++;
        }
        printf("\n");
    } else if (count == 0) {
        printf("Unknown command '%s'\n", argv[0]);
    } else {
        match->handler(ctrl, argc - 1, &argv[1]);
    }
}

其中结构体hostapd_cli_cmd是对应命令行不同命令的操作项目集合:

struct hostapd_cli_cmd {
    const char *cmd;
    int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
    char ** (*completion)(const char *str, int pos);
    const char *usage;
} 

最后执行的是操作函数handler()completion()

以下是最新稳定版hostapd提供的hostapd_cli操作条目和操作函数集合:


static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
    { "ping", hostapd_cli_cmd_ping, NULL,
      "= pings hostapd" },
    { "mib", hostapd_cli_cmd_mib, NULL,
      "= get MIB variables (dot1x, dot11, radius)" },
    { "relog", hostapd_cli_cmd_relog, NULL, NULL },
    { "status", hostapd_cli_cmd_status, NULL, NULL },
    { "sta", hostapd_cli_cmd_sta, NULL,
      "<addr> = get MIB variables for one station" },
    { "all_sta", hostapd_cli_cmd_all_sta, NULL,
       "= get MIB variables for all stations" },
    { "new_sta", hostapd_cli_cmd_new_sta, NULL,
      "<addr> = add a new station" },
    { "deauthenticate", hostapd_cli_cmd_deauthenticate,
      hostapd_complete_deauthenticate,
      "<addr> = deauthenticate a station" },
    { "disassociate", hostapd_cli_cmd_disassociate,
      hostapd_complete_disassociate,
      "<addr> = disassociate a station" },
#ifdef CONFIG_TAXONOMY
    { "signature", hostapd_cli_cmd_signature, NULL,
      "<addr> = get taxonomy signature for a station" },
#endif /* CONFIG_TAXONOMY */
#ifdef CONFIG_IEEE80211W
    { "sa_query", hostapd_cli_cmd_sa_query, NULL,
      "<addr> = send SA Query to a station" },
#endif /* CONFIG_IEEE80211W */
#ifdef CONFIG_WPS
    { "wps_pin", hostapd_cli_cmd_wps_pin, NULL,
      "<uuid> <pin> [timeout] [addr] = add WPS Enrollee PIN" },
    { "wps_check_pin", hostapd_cli_cmd_wps_check_pin, NULL,
      "<PIN> = verify PIN checksum" },
    { "wps_pbc", hostapd_cli_cmd_wps_pbc, NULL,
      "= indicate button pushed to initiate PBC" },
    { "wps_cancel", hostapd_cli_cmd_wps_cancel, NULL,
      "= cancel the pending WPS operation" },
#ifdef CONFIG_WPS_NFC
    { "wps_nfc_tag_read", hostapd_cli_cmd_wps_nfc_tag_read, NULL,
      "<hexdump> = report read NFC tag with WPS data" },
    { "wps_nfc_config_token", hostapd_cli_cmd_wps_nfc_config_token, NULL,
      "<WPS/NDEF> = build NFC configuration token" },
    { "wps_nfc_token", hostapd_cli_cmd_wps_nfc_token, NULL,
      "<WPS/NDEF/enable/disable> = manager NFC password token" },
    { "nfc_get_handover_sel", hostapd_cli_cmd_nfc_get_handover_sel, NULL,
      NULL },
#endif /* CONFIG_WPS_NFC */
    { "wps_ap_pin", hostapd_cli_cmd_wps_ap_pin, NULL,
      "<cmd> [params..] = enable/disable AP PIN" },
    { "wps_config", hostapd_cli_cmd_wps_config, NULL,
      "<SSID> <auth> <encr> <key> = configure AP" },
    { "wps_get_status", hostapd_cli_cmd_wps_get_status, NULL,
      "= show current WPS status" },
#endif /* CONFIG_WPS */
    { "disassoc_imminent", hostapd_cli_cmd_disassoc_imminent, NULL, NULL },
    { "ess_disassoc", hostapd_cli_cmd_ess_disassoc, NULL, NULL },
    { "bss_tm_req", hostapd_cli_cmd_bss_tm_req, NULL, NULL },
    { "get_config", hostapd_cli_cmd_get_config, NULL,
      "= show current configuration" },
    { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
      "= show this usage help" },
    { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
      "[ifname] = show interfaces/select interface" },
#ifdef CONFIG_FST
    { "fst", hostapd_cli_cmd_fst, NULL, NULL },
#endif /* CONFIG_FST */
    { "raw", hostapd_cli_cmd_raw, NULL, NULL },
    { "level", hostapd_cli_cmd_level, NULL,
      "<debug level> = change debug level" },
    { "license", hostapd_cli_cmd_license, NULL,
      "= show full hostapd_cli license" },
    { "quit", hostapd_cli_cmd_quit, NULL,
      "= exit hostapd_cli" },
    { "set", hostapd_cli_cmd_set, NULL, NULL },
    { "get", hostapd_cli_cmd_get, NULL, NULL },
    { "set_qos_map_set", hostapd_cli_cmd_set_qos_map_set, NULL, NULL },
    { "send_qos_map_conf", hostapd_cli_cmd_send_qos_map_conf, NULL, NULL },
    { "chan_switch", hostapd_cli_cmd_chan_switch, NULL, NULL },
    { "hs20_wnm_notif", hostapd_cli_cmd_hs20_wnm_notif, NULL, NULL },
    { "hs20_deauth_req", hostapd_cli_cmd_hs20_deauth_req, NULL, NULL },
    { "vendor", hostapd_cli_cmd_vendor, NULL, NULL },
    { "enable", hostapd_cli_cmd_enable, NULL, NULL },
    { "reload", hostapd_cli_cmd_reload, NULL, NULL },
    { "disable", hostapd_cli_cmd_disable, NULL, NULL },
    { "erp_flush", hostapd_cli_cmd_erp_flush, NULL, NULL },
    { "log_level", hostapd_cli_cmd_log_level, NULL, NULL },
    { "pmksa", hostapd_cli_cmd_pmksa, NULL, NULL },
    { "pmksa_flush", hostapd_cli_cmd_pmksa_flush, NULL, NULL },
    { "set_neighbor", hostapd_cli_cmd_set_neighbor, NULL, NULL },
    { "remove_neighbor", hostapd_cli_cmd_remove_neighbor, NULL, NULL },
    { "req_lci", hostapd_cli_cmd_req_lci, NULL, NULL },
    { "req_range", hostapd_cli_cmd_req_range, NULL, NULL },
    { "driver_flags", hostapd_cli_cmd_driver_flags, NULL, NULL },
    { NULL, NULL, NULL, NULL }
};

可以通过查找相应的调用执行函数了解hostapt_cli相应操作的详细执行过程。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/viewsky11/article/details/55190707

智能推荐

c# 调用c++ lib静态库_c#调用lib-程序员宅基地

文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib

deepin/ubuntu安装苹方字体-程序员宅基地

文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang

html表单常见操作汇总_html表单的处理程序有那些-程序员宅基地

文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些

PHP设置谷歌验证器(Google Authenticator)实现操作二步验证_php otp 验证器-程序员宅基地

文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器

【Python】matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距-程序员宅基地

文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距

docker — 容器存储_docker 保存容器-程序员宅基地

文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器

随便推点

网络拓扑结构_网络拓扑csdn-程序员宅基地

文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn

JS重写Date函数,兼容IOS系统_date.prototype 将所有 ios-程序员宅基地

文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios

如何将EXCEL表导入plsql数据库中-程序员宅基地

文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql

Git常用命令速查手册-程序员宅基地

文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...

分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120-程序员宅基地

文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120

【C++缺省函数】 空类默认产生的6个类成员函数_空类默认产生哪些类成员函数-程序员宅基地

文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数

推荐文章

热门文章

相关标签