【转】Hostapd工作流程分析   【转】Hostapd工作流程分析 

转自:http://blog.chinaunix.net/uid-30081165-id-5290531.html

  Hostapd是一个运行在用户态的守护进程,可以通过Hostapd来读取配置文件,通过nl802.11来控制底层的状态如RTS/CTS beacon帧间隔等等信息;也可以读取相关的信息。

  其代码框架如下图所示:hostapd_cli是基于文本的命令命令界面,GUI则是图形控制界面;event loop是一个死循环函数用于接收和处理各种事件信息。

【转】Hostapd工作流程分析
  【转】Hostapd工作流程分析 

下图是各种命令配置工具以及无线工作流程:

【转】Hostapd工作流程分析
  【转】Hostapd工作流程分析 

实际上可以看到无论是wpa_supplient iw还是hostapd工具都是通过调用libnl的相关方法来完成信息的配置预读取的。

         接下来分析hostaod的主函数:

int main(int argc, char *argv[])
{
    struct hapd_interfaces interfaces;
    int ret = 1;
    size_t i, j;
    int c, debug = 0;
    const char *log_file = NULL;
    const char *entropy_file = NULL;
    char **bss_config = NULL, **tmp_bss;
    size_t num_bss_configs = 0;
#ifdef CONFIG_DEBUG_LINUX_TRACING
    int enable_trace_dbg = 0;
#endif /* CONFIG_DEBUG_LINUX_TRACING */

    if (os_program_init())
        return -1;

    os_memset(&interfaces, 0, sizeof(interfaces));
    interfaces.reload_config = hostapd_reload_config;
    interfaces.config_read_cb = hostapd_config_read;
    interfaces.for_each_interface = hostapd_for_each_interface;
    interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
    interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
    interfaces.driver_init = hostapd_driver_init;
    interfaces.global_iface_path = NULL;
    interfaces.global_iface_name = NULL;
    interfaces.global_ctrl_sock = -1;

    wpa_supplicant_event = hostapd_wpa_event;
    //分析配置文件信息
    for (;;) {
        c = getopt(argc, argv, "b:Bde:f:hKP:Ttu:g:G:v::");
        if (c < 0)
            break;
        switch (c) {
        case 'h':
            usage();
            break;
        case 'd':
            debug++;
            if (wpa_debug_level > 0)
                wpa_debug_level--;
            break;
        case 'B':
            daemonize++;
            break;
        case 'e':
            entropy_file = optarg;
            break;
        case 'f':
            log_file = optarg;
            break;
        case 'K':
            wpa_debug_show_keys++;
            break;
        case 'P':
            os_free(pid_file);
            pid_file = os_rel2abs_path(optarg);
            break;
        case 't':
            wpa_debug_timestamp++;
            break;
#ifdef CONFIG_DEBUG_LINUX_TRACING
        case 'T':
            enable_trace_dbg = 1;
            break;
#endif /* CONFIG_DEBUG_LINUX_TRACING */
        case 'v':
            if (optarg)
                exit(!has_feature(optarg));
            show_version();
            exit(1);
            break;
        case 'g':
            if (hostapd_get_global_ctrl_iface(&interfaces, optarg))
                return -1;
            break;
        case 'G':
            if (hostapd_get_ctrl_iface_group(&interfaces, optarg))
                return -1;
            break;
        case 'b':
            tmp_bss = os_realloc_array(bss_config,
                num_bss_configs + 1,
                sizeof(char *));
            if (tmp_bss == NULL)
                goto out;
            bss_config = tmp_bss;
            bss_config[num_bss_configs++] = optarg;
            break;
#ifdef CONFIG_WPS
        case 'u':
            return gen_uuid(optarg);
#endif /* CONFIG_WPS */
        default:
            usage();
            break;
        }
    }

    if (optind == argc && interfaces.global_iface_path == NULL &&
        num_bss_configs == 0)
        usage();

    wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);

    if (log_file)
        wpa_debug_open_file(log_file);
#ifdef CONFIG_DEBUG_LINUX_TRACING
    if (enable_trace_dbg) {
        int tret = wpa_debug_open_linux_tracing();
        if (tret) {
            wpa_printf(MSG_ERROR, "Failed to enable trace logging");
            return -1;
        }
    }
#endif /* CONFIG_DEBUG_LINUX_TRACING */

    interfaces.count = argc - optind;
    if (interfaces.count || num_bss_configs) {
        interfaces.iface = os_calloc(interfaces.count + num_bss_configs,
            sizeof(struct hostapd_iface *));
        if (interfaces.iface == NULL) {
            wpa_printf(MSG_ERROR, "malloc failed");
            return -1;
        }
    }

    //初始化global context信息
    if (hostapd_global_init(&interfaces, entropy_file)) {
        wpa_printf(MSG_ERROR, "Failed to initilize global context");
        return -1;
    }

    /* Allocate and parse configuration for full interface files */
    for (i = 0; i < interfaces.count; i++) {
        interfaces.iface[i] = hostapd_interface_init(&interfaces,
            argv[optind + i],
            debug);
        if (!interfaces.iface[i]) {
            wpa_printf(MSG_ERROR, "Failed to initialize interface");
            goto out;
        }
    }

    /* Allocate and parse configuration for per-BSS files */
    for (i = 0; i < num_bss_configs; i++) {
        struct hostapd_iface *iface;
        char *fname;

        wpa_printf(MSG_INFO, "BSS config: %s", bss_config[i]);
        fname = os_strchr(bss_config[i], ':');
        if (fname == NULL) {
            wpa_printf(MSG_ERROR,
                "Invalid BSS config identifier '%s'",
                bss_config[i]);
            goto out;
        }
        *fname++ = '