hostapd的radius/eap server代码分析(2)-hostapd配置

公良鸿风
2023-12-01


NJZhuJinhua@csdn Apr.10,2010

http://blog.csdn.net/njzhujinhua
欢迎转载,转载请联系 jinhua1982@gmail.com 并注明出处。

 


本节将hostapd基本配置及初始化,下节将eap相关的基本配置及初始化
【1】
首先是main函数最开始定义的hapd_interface变量
int main(int argc, char *argv[])
{
 struct hapd_interfaces interfaces;
其中hapd_interfaces 定义于hostapd/hostapd.c,该类型仅由main函数使用,
struct hapd_interfaces {
 size_t count;
 struct hostapd_iface **iface;
};
用于维护所有的interface信息。
对于count个interface分别由hostapd_init进行分配内存及初始化,并紧接着由hostapd_setup_interface进行设置
 /* Initialize interfaces */
 for (i = 0; i < interfaces.count; i++) {
  wpa_printf(MSG_ERROR, "Configuration file: %s",
      argv[optind + i]);
  interfaces.iface[i] = hostapd_init(argv[optind + i]);
  if (!interfaces.iface[i])
   goto out;
  for (k = 0; k < debug; k++) {
   if (interfaces.iface[i]->bss[0]->conf->
       logger_stdout_level > 0)
    interfaces.iface[i]->bss[0]->conf->
     logger_stdout_level--;
  }

  ret = hostapd_setup_interface(interfaces.iface[i]);
  if (ret)
   goto out;

  for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
   if (interfaces.iface[i]->bss[0]->conf->tnc)
    tnc++;
  }
 }
初始化时用该interface对应的配置文件作为参数。

[2] hostapd_iface :定义于hostapd/hostapd.h
定义每个接口拥有的配置信息
/**
 * struct hostapd_iface - hostapd per-interface data structure
 */
struct hostapd_iface {
 char *config_fname;
 struct hostapd_config *conf;

 size_t num_bss;
 struct hostapd_data **bss;

 int num_ap; /* number of entries in ap_list */
 struct ap_info *ap_list; /* AP info list head */
 struct ap_info *ap_hash[STA_HASH_SIZE];
 struct ap_info *ap_iter_list;

 struct hostapd_hw_modes *hw_features;
 int num_hw_features;
 struct hostapd_hw_modes *current_mode;
 /* Rates that are currently used (i.e., filtered copy of
  * current_mode->channels */
 int num_rates;
 struct hostapd_rate_data *current_rates;

 u16 hw_flags;

 /* Number of associated Non-ERP stations (i.e., stations using 802.11b
  * in 802.11g BSS) */
 int num_sta_non_erp;

 /* Number of associated stations that do not support Short Slot Time */
 int num_sta_no_short_slot_time;

 /* Number of associated stations that do not support Short Preamble */
 int num_sta_no_short_preamble;

 int olbc; /* Overlapping Legacy BSS Condition */

 /* Number of HT associated stations that do not support greenfield */
 int num_sta_ht_no_gf;

 /* Number of associated non-HT stations */
 int num_sta_no_ht;

 /* Number of HT associated stations 20 MHz */
 int num_sta_ht_20mhz;

 /* Overlapping BSS information */
 int olbc_ht;

#ifdef CONFIG_IEEE80211N
 u16 ht_op_mode;
#endif /* CONFIG_IEEE80211N */
};

看hostapd_init对hostapd_iface进行初始化的代码:
static struct hostapd_iface * hostapd_init(const char *config_file)
{
 struct hostapd_iface *hapd_iface = NULL;
 struct hostapd_config *conf = NULL;
 struct hostapd_data *hapd;
 size_t i;

 hapd_iface = os_zalloc(sizeof(*hapd_iface));
 if (hapd_iface == NULL)
  goto fail;

 hapd_iface->config_fname = os_strdup(config_file);
 if (hapd_iface->config_fname == NULL)
  goto fail;

 conf = hostapd_config_read(hapd_iface->config_fname);
 if (conf == NULL)
  goto fail;
 hapd_iface->conf = conf;

 hapd_iface->num_bss = conf->num_bss;
 hapd_iface->bss = os_zalloc(conf->num_bss *
        sizeof(struct hostapd_data *));
 if (hapd_iface->bss == NULL)
  goto fail;

 for (i = 0; i < conf->num_bss; i++) {
  hapd = hapd_iface->bss[i] =
   hostapd_alloc_bss_data(hapd_iface, conf,
            &conf->bss[i]);
  if (hapd == NULL)
   goto fail;
 }

 return hapd_iface;

fail:
 if (conf)
  hostapd_config_free(conf);
 if (hapd_iface) {
  for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
   hapd = hapd_iface->bss[i];
   if (hapd && hapd->ssl_ctx)
    tls_deinit(hapd->ssl_ctx);
  }

  os_free(hapd_iface->config_fname);
  os_free(hapd_iface->bss);
  os_free(hapd_iface);
 }
 return NULL;
}

hostapd_init内以该interface的配置文件作为参数调用hostapd_config_read。并将读取到的配置信息赋给成员变量struct hostapd_config *conf.
根据配置文件中bss的配置个数conf->num_bss的值通过调用hostapd_alloc_bss_data分配空间及相关设置,并对hapd_iface->bss[i]进行赋值,
 for (i = 0; i < conf->num_bss; i++) {
  hapd = hapd_iface->bss[i] =
   hostapd_alloc_bss_data(hapd_iface, conf,
            &conf->bss[i]);
  if (hapd == NULL)
   goto fail;
 }
其中hostapd_alloc_bss_data的定义为
static struct hostapd_data *
hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
         struct hostapd_config *conf,
         struct hostapd_bss_config *bss)
{
 struct hostapd_data *hapd;

 hapd = os_zalloc(sizeof(*hapd));
 if (hapd == NULL)
  return NULL;

 hapd->iconf = conf;
 hapd->conf = bss;
 hapd->iface = hapd_iface;

 if (hapd->conf->individual_wep_key_len > 0) {
  /* use key0 in individual key and key1 in broadcast key */
  hapd->default_wep_key_idx = 1;
 }

// TLS 及 EAP_SERVER 相关代码,此处暂时略

 hapd->driver = hapd->iconf->driver;

 return hapd;

#if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
fail:
#endif /* TODO: cleanup allocated resources(?) */
 os_free(hapd);
 return NULL;
}
三个参数分别为本interface信息,本interface的配置信息,本interface配置信息中第i个bss的配置部分。
其中代码
 hapd->iconf = conf;
 hapd->conf = bss;
 hapd->iface = hapd_iface;
使得hostapd_data中均有指向这几个配置的指针了。
既有:
hapd_iface->bss[i]->iconf == hapd_iface->conf
hapd_iface->bss[i]->conf == &hapd_iface->conf->bss[i]
hapd_iface->bss[i]->iface == hapd_iface

至此,
struct hostapd_data,
struct hostapd_iface,
struct hostapd_config,
struct hostapd_bss_config
四个基本hostapd配置结构互相指的关系应该搞清楚了吧?

 

 

 类似资料: