hostapd的radius/eap server代码分析(1)-main

本文深入分析了hostapd的main函数,详细介绍了日志处理、命令行参数解析以及eapserver启动相关代码。通过理解这些核心组件的工作原理,读者可以更高效地配置和调试hostapd。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hostapd的radius/eap server代码分析(1)-main


NJZhuJinhua@csdn Apr.10,2010

http://blog.youkuaiyun.com/NJZhuJinhua/archive/2010/04/11/5473970.aspx
欢迎转载,转载请注明出处。

hostapd 的main函数位于hostapd/hostapd.c中。
函数开始是日志相关以及对命令行参数选项的处理。

[cpp]  view plain copy
  1. int main(int argc, char *argv[])  
  2. {  
  3.     struct hapd_interfaces interfaces;  
  4.     int ret = 1, k;  
  5.     size_t i, j;  
  6.     int c, debug = 0, daemonize = 0, tnc = 0;  
  7.     char *pid_file = NULL;  
  8.     hostapd_logger_register_cb(hostapd_logger_cb);  
  9.     for (;;) {  
  10.         c = getopt(argc, argv, "BdhKP:tv");  
  11.         switch (c) {  
  12.         case 'h':  
  13.             usage();   
  14.             break;  
  15.         case 'd':  
  16.             debug++;  
  17.             if (wpa_debug_level > 0)  
  18.                 wpa_debug_level--;  
  19.             break;  
  20.         case 'B':  
  21.             daemonize++;  
  22.             break;  
  23.         case 'K':  
  24.             wpa_debug_show_keys++;  
  25.             break;  
  26.         case 'P':  
  27.             os_free(pid_file);  
  28.             pid_file = os_rel2abs_path(optarg);  
  29.             break;  
  30.         case 't':  
  31.             wpa_debug_timestamp++;  
  32.             break;  
  33.         case 'v':  
  34.             show_version();  
  35.             exit(1);  
  36.             break;  
  37.         default:  
  38.             usage();  
  39.             break;  
  40.         }  


后续不用代码框了,csdn的博客中插入的代码还不知道怎么修改,
h:调用usage()函数,打印使用说明
static void usage(void)
{
    show_version();
    fprintf(stderr,
        "/n"
        "usage: hostapd [-hdBKtv] [-P <PID file>] "
        "<configuration file(s)>/n"
        "/n"
        "options:/n"
        "   -h   show this usage/n"
        "   -d   show more debug messages (-dd for even more)/n"
        "   -B   run daemon in the background/n"
        "   -P   PID file/n"
        "   -K   include key data in debug messages/n"
        "   -t   include timestamps in some debug messages/n"
        "   -v   show hostapd version/n");

    exit(1);
}


d:
        case 'd':
            debug++;
            if (wpa_debug_level > 0)
                wpa_debug_level--;
如果选项中每出现一个d则debug级别递增,同时日志级别数字降低。
在src/utils/wpa_debug.c中有定义: 
int wpa_debug_level = MSG_INFO;
int wpa_debug_show_keys = 0;
int wpa_debug_timestamp = 0;
这里enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR }; 其中MSG_INFO值为2,在该级别时正常情况下基本没有日志输出。
默认级别就是MSG_INFO,因而要想获得更多执行流程中的输出就需要增加debug级别。如果想把码流及各radius属性均打印出的话就将级别调为MSG_MSGDUMP=0吧。
【本系列分析中debug级别或日志级别越高指的是debug变量值越大,wpa_debug_level值越低。】


B:作为守护进程运行,
        case 'B':
            daemonize++;
            break;
    如果含有该选项则执行os_daemonize(pid_file)
    if (daemonize && os_daemonize(pid_file)) {
        perror("daemon");
        goto out;
    }
在utils/os_win32.c中该函数未实现。
在utils/os_unix.c中定义为
int os_daemonize(const char *pid_file)
{
#ifdef __uClinux__
    return -1;
#else /* __uClinux__ */
    if (os_daemon(0, 0)) {
        perror("daemon");
        return -1;
    }

    if (pid_file) {
        FILE *f = fopen(pid_file, "w");
        if (f) {
            fprintf(f, "%u/n", getpid());
            fclose(f);
        }
    }

    return -0;
#endif /* __uClinux__ */
}
其中os_daemon即为daemon系统调用。如果调用成功则获取进程id并写入pid_file指定的文件中。

K:
        case 'K':
            wpa_debug_show_keys++;
            break;
用于控制在调试时是否输出密钥相关key值。如需输出密钥还需要设置日志级别为MSG_DEBUG或以上
例如:
    wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
            challenge, len);
如果选项中没有K显式说明,则即使日志级别为MSG_DEBUG或MSG_MSGDUMP也不会输出的。




P:
        case 'P':
            os_free(pid_file);
            pid_file = os_rel2abs_path(optarg);
            break;
设定pid_file的值,os_rel2abs_path 将参数提供的相对路径转为绝对路径。







t:
        case 't':
            wpa_debug_timestamp++;
            break;
该项作用为在日志中包含时间戳信息。
wpa_debug_timestamp的定义参见选项d时的说明,默认值为0,即不包含。

带时间戳效果为
1270995804.000000: EAP: EAP entering state RECEIVED
1270995804.000000: EAP: parseEapResp: rxResp=1 respId=77 respMethod=21 respVendor=0 respVendorMethod=0
1270995804.000000: EAP: EAP entering state INTEGRITY_CHECK
1270995804.000000: EAP: EAP entering state METHOD_RESPONSE
1270995804.000000: SSL: Received packet(len=176) - Flags 0x00
1270995804.000000: SSL: Received packet: Flags 0x0 Message Length 0




v
        case 'v':
            show_version();
            exit(1);
            break;
显式版本号并退出。
static void show_version(void)
{
    fprintf(stderr,
        "hostapd v" VERSION_STR "/n"
        "User space daemon for IEEE 802.11 AP management,/n"
        "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator/n"
        "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
        "and contributors/n");
}
#define VERSION_STR "0.6.10"


了解上述选项的设定,在随后eap server的分离中可以直接设定时间戳,日志级别等。

下一节介绍hostapd启动时eap server相关的代码。


elro -ffunction-sections -fdata-sections" /home/ubuntu/tina-v821-release/prebuilt/hostbuilt/make4.1/bin/make -C /home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd AR="riscv32-linux-musl-ar" AS="riscv32-linux-musl-gcc -c -Os -pipe -fno-caller-saves -fno-plt -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Wformat -Werror=format-security -DPIC -fpic -D_FORTIFY_SOURCE=2 -Wl,-z,now -Wl,-z,relro -ffunction-sections -fdata-sections" LD=riscv32-linux-musl-ld NM="riscv32-linux-musl-nm" CC="riscv32-linux-musl-gcc" GCC="riscv32-linux-musl-gcc" CXX="riscv32-linux-musl-g++" RANLIB="riscv32-linux-musl-ranlib" STRIP=riscv32-linux-musl-strip OBJCOPY=riscv32-linux-musl-objcopy OBJDUMP=riscv32-linux-musl-objdump SIZE=riscv32-linux-musl-size CONFIG_ACS=y CONFIG_DRIVER_NL80211=y CONFIG_IEEE80211N= CONFIG_IEEE80211AC= CONFIG_IEEE80211AX= CONFIG_DRIVER_WEXT= CONFIG_WEP=y LIBS="-L/home/ubuntu/tina-v821-release/prebuilt/rootfsbuilt/riscv/nds32le-linux-musl-v5d//sysroot/usr/lib -DPIC -fpic -specs=/home/ubuntu/tina-v821-release/openwrt/openwrt/include/hardened-ld-pie.specs -znow -zrelro -Wl,--gc-sections -fuse-linker-plugin -lubox -lubus -lm -lnl-tiny" LIBS_c="" AR="riscv32-linux-musl-ar" BCHECK= hostapd hostapd_cli make[4]: Entering directory '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd' In file included from main.c:26: /home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/src/ap/hostapd.h:84:21: error: field 'ubus' has incomplete type 84 | struct ubus_object ubus; | ^~~~ Makefile:1302: recipe for target 'main.o' failed make[4]: *** [main.o] Error 1 make[4]: Leaving directory '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/hostapd' Makefile:708: recipe for target '/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/.built' failed make[3]: *** [/home/ubuntu/tina-v821-release/out/v821/avaota_f1/openwrt/build_dir/target/hostapd-full-internal/hostapd-2020-06-08-5a8b3662/.built] Error 2 make[3]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt/package/network/services/hostapd' time: package/network/services/hostapd/full-internal/compile#0.71#0.22#0.79#1751822626.32#1751822627.10 ERROR: package/network/services/hostapd failed to build (build variant: full-internal). More error defails please refer to: /home/ubuntu/tina-v821-release/openwrt/openwrt/build_log/package/network/services/hostapd/full-internal/compile.txt package/Makefile:114: recipe for target 'package/network/services/hostapd/compile' failed make[2]: *** [package/network/services/hostapd/compile] Error 1 make[2]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' package/Makefile:110: recipe for target '/home/ubuntu/tina-v821-release/openwrt/openwrt/staging_dir/target/stamp/.package_compile' failed make[1]: *** [/home/ubuntu/tina-v821-release/openwrt/openwrt/staging_dir/target/stamp/.package_compile] Error 2 make[1]: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' /home/ubuntu/tina-v821-release/openwrt/openwrt/include/toplevel.mk:238: recipe for target 'world' failed make: *** [world] Error 2 make: Leaving directory '/home/ubuntu/tina-v821-release/openwrt/openwrt' INFO: build_openwrt_rootfs failed 编译tianlinux的时候报错如上,帮我看看怎么办?
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值