編輯:關於 Android 程式設計
conf 檔是 wpa_supplicant 的設定檔,一般命名為 wpa_supplicant.conf。裡面存著兩類東西:wpa_supplicant 的運行參數,以及已保存的網路列表。
設定檔路徑透過啟動 wpa_supplicant 時的 -c 參數傳入,初始化過程中賦值給 wpa_s->confname,接著把檔案內容讀進 wpa_s->conf。
wpa_supplicant 的所有參數都定義在 struct wpa_config 中。
這個結構體是整個設定系統的核心,可以分成幾個區塊來理解。
// config 檔中,存儲所有 network 節點的鏈表
struct wpa_ssid *ssid;
// 按照 priority 排列的 network 節點
struct wpa_ssid **pssid;
int num_prio;
ssid 是依配置檔原始順序串起來的鏈表,pssid 則是依 priority 排序後的指標陣列。兩者指向同一批節點,只是排序依據不同——這個設計讓「按設定檔順序匹配」與「按優先級匹配」兩種需求都能滿足。
int eapol_version; // EAP 版本,WPAS 支援到 2
int ap_scan; // 1:wpas 負責掃描和選擇網路
// 0、2:驅動負責掃描和連接
char *bgscan; // 背景掃描與漫遊參數
int disable_scan_offload; // 設為 1 可停用驅動的 scheduled scan
int scan_cur_freq; // 只掃描當前頻率
unsigned int bss_max_count; // 記憶體中保留的 bss 最大個數
unsigned int bss_expiration_age; // bss 超期時間(秒)
unsigned int bss_expiration_scan_count; // 連續幾次掃描不到就移除
int filter_ssids; // 0:停用;1:啟用 SSID 過濾
int filter_rssi; // 0:停用;-n:濾掉 scan_res 中 < -n dBm 的 bss
ap_scan 這個參數特別關鍵,它決定「誰來挑網路」。設為 1 時由 wpa_supplicant 主導掃描與選網,設為 0 或 2 則把責任下放給驅動。
char *ctrl_interface; // 結構名稱,例如 wlan0
char *ctrl_interface_group; // 控制介面的群組(已棄用)
int fast_reauth; // EAP fast re-authentication,預設 1(啟用)
char *driver_param; // 透過 wpa_driver_ops::set_param() 傳遞
unsigned int dot11RSNAConfigPMKLifetime;
unsigned int dot11RSNAConfigPMKReauthThreshold;
unsigned int dot11RSNAConfigSATimeout;
int okc; // 是否啟用 opportunistic key caching
int pmf; // 是否啟用/要求 PMF,預設 0
// 對應 ieee80211w=1 或 2,pmf=1/2 可改動 enum mfp_options
int key_mgmt_offload;
pmf 對應的是管理幀保護(Management Frame Protection)。設為 1 是可選、2 是強制,這在企業環境下很重要。
u8 uuid[16];
char *device_name;
char *manufacturer; // 最長 64 個 ASCII 字元
char *model_name; // 最長 32 個 ASCII 字元
char *model_number;
char *serial_number;
u8 device_type[WPS_DEV_TYPE_LEN];
char *config_methods;
u8 os_version[4];
char country[2]; // ISO/IEC alpha2 國家碼
int wps_cred_processing;
struct wpabuf *wps_vendor_ext[MAX_WPS_VENDOR_EXT];
P2P 相關參數數量可觀,涵蓋 listen/operate 的信道與國家碼、GO intent、SSID 後綴、群組空閒超時等:
int p2p_listen_reg_class;
int p2p_listen_channel;
int p2p_oper_reg_class;
int p2p_oper_channel;
int p2p_go_intent;
char *p2p_ssid_postfix;
int persistent_reconnect;
int p2p_intra_bss;
int p2p_group_idle; // 超過該秒數未傳輸則關閉 group
unsigned int p2p_passphrase_len; // GO 產生的隨機金鑰長度(8~63)
int p2p_go_max_inactivity; // STA 不活躍逾時,預設 300 秒
int p2p_go_ht40;
int p2p_go_vht;
int p2p_no_group_iface;
int interworking; // 是否啟用 IEEE 802.11u
int access_network_type; // interworking=1 時生效,取值 0~15
int hs20; // Hotspot 2.0
int auto_interworking;
char *autoscan; // 自動掃描參數,格式為 :<module parameters>
char *ext_password_backend;
unsigned int sched_scan_interval;
int tdls_external_control;
char *wowlan_triggers; // Wake-on-WLAN 觸發條件
int disassoc_low_ack; // 因大量丟包而斷開的 station 個數
int dtim_period;
int beacon_int;
設定檔中每一個 network={ } 節點代表一個保存的網路。在手機的 Wi-Fi 掃描結果介面上直接點擊連接的網路,背後就是往這個檔裡寫入一筆。形式如下:
network={
ssid="ASUS"
psk="******"
key_mgmt=WPA-PSK
priority=6
}
這個節點存了網路名稱(ssid)、密碼(psk)、加密方式(key_mgmt)與優先級(priority)。
Wi-Fi 的加密方式有很多種——wpa_psk、wep、open、wapi、各種 EAP——每種加密方式的節點寫法都不一樣,具體可參考原始碼中的 wpa_supplicant.conf 範例檔。
每一個 network 節點解析完會生成一個 struct wpa_ssid,這個結構體定義了大量欄位,目的就是為了容納各種加密方式的差異。
struct wpa_ssid {
// 鏈表指標,鏈表頭存放在 struct wpa_config->ssid
struct wpa_ssid *next;
// 按照 priority 組成的鏈表,鏈表頭在 struct wpa_config->pssid
struct wpa_ssid *pnext;
// 每個 network 節點都會有一個 id
// 無論是透過 control interface 建立,還是從 config 檔讀取,都會賦予一個 id
int id;
// 初始 priority 為 0,當 select 該 bss 時 priority 會升高
// 收到掃描結果時,按 priority 由高到低匹配;
// priority 相同時,再依安全協議與信號強度匹配
// 注意:AP 採用 scan_ssid=1 且 ap_scan=2 時,
// 會改為按 config 檔中的順序(wpa_config->ssid)匹配
int priority;
u8 *ssid;
size_t ssid_len;
u8 bssid[ETH_ALEN]; // 若設定該值,關聯到該 bssid 的 AP 時會用到
int bssid_set;
u8 go_p2p_dev_addr[ETH_ALEN];
u8 psk[32]; // WPA pre-shared key,由 passphrase 經 SHA 生成
int psk_set;
char *passphrase; // 若含該項,連接時採用 ssid + passphrase(8~63 字元)
char *ext_psk;
int pairwise_cipher; // 允許的 pairwise 加密,WPA_CIPHER_*
int group_cipher; // 允許的 group 加密,WPA_CIPHER_*
int key_mgmt; // 允許的金鑰管理協議,WPA_KEY_MGMT_*
int proto; // 允許的協議,WPA_PROTO_*
int auth_alg; // 允許的認證算法,WPA_AUTH_ALG_*
int bg_scan_period; // 背景掃描間隔(秒),0:停用,-1:使用驅動預設值
// 當 BSS 設為隱藏時,用該值寫入 Probe Requests
// 若某些驅動不支援掃描隱藏網路,可設 ap_mode=2
int scan_ssid;
// EAP 相關
int eapol_flags;
struct eap_peer_config eap;
// WEP 相關,NUM_WEP_KEYS=4,MAX_WEP_KEY_LEN=16
u8 wep_key[NUM_WEP_KEYS][MAX_WEP_KEY_LEN];
size_t wep_key_len[NUM_WEP_KEYS];
int wep_tx_keyidx;
int proactive_key_caching; // 1:啟用;0:停用,與 PMK/PMKSA/RSN 有關
int mixed_cell; // 是否允許同一 SSID 內明文與加密並存,預設 0
int leap;
int non_leap;
unsigned int eap_workaround; // 跳過關聯失敗的 EAP server
// WPAS 模式
enum wpas_mode {
WPAS_MODE_INFRA = 0, // 一般 station 模式
WPAS_MODE_IBSS = 1, // IBSS 網路,不需要 AP
WPAS_MODE_AP = 2, // AP / softap
WPAS_MODE_P2P_GO = 3, // P2P GO
WPAS_MODE_P2P_GROUP_FORMATION = 4 // 不能透過 config 檔設定
} mode;
int disabled; // 0:可用;1:不可用(可透過 ctrl_iface 啟用)
// 2:該 network 包含 P2P 參數
int disabled_for_connect; // WPS 連接前暫時 disable,連完取消
int peerkey;
char *id_str;
enum mfp_options ieee80211w {
NO_MGMT_FRAME_PROTECTION = 0,
MGMT_FRAME_PROTECTION_OPTIONAL = 1,
MGMT_FRAME_PROTECTION_REQUIRED = 2
};
int frequency;
int ht40;
int vht;
int wpa_ptk_rekey; // PTK 生存時間(秒)
int *scan_freq; // 該 network 的掃描頻率
char *bgscan;
int ignore_broadcast_ssid; // 0:停用;1:beacon 中 ssid 長度為 0;
// 2:beacon 中 ssid 內容為 '0'
int *freq_list; // 掃描結果頻率不匹配則不 select 該 bss
u8 *p2p_client_list;
size_t num_p2p_clients;
int p2p_group; // 作為 P2P group 產生(config 檔不可設定)
int p2p_persistent_group;
int temporary; // 暫時網路,不予保存
int export_keys;
// HT 覆寫
int disable_ht;
int disable_ht40;
int disable_sgi; // Short Guard Interval
int disable_ldpc;
int ht40_intolerant;
int disable_max_amsdu; // 停用則 amsdu 為 3839B,啟用則 7935B,-1 表示不設定
int ampdu_factor; // 0-3
int ampdu_density; // 0-7
char *ht_mcs;
// VHT 覆寫
int disable_vht;
unsigned int vht_capa;
unsigned int vht_capa_mask;
// AP 模式
int ap_max_inactivity; // 偵測 STA 不活躍的超時,預設 300 秒
int dtim_period; // 每隔幾個 beacon 帶 tim 資訊,預設 2
int beacon_int; // Beacon 間隔,預設 100 TU
unsigned int auth_failures;
struct os_reltime disabled_until;
void *parent_cred;
int macsec_policy;
int update_identifier;
unsigned int wps_run;
};
這裡有個細節容易被忽略:priority 不是靜態的。每個 network 初始 priority 為 0,當該 bss 被 select 時 priority 會升高,後續掃描結果就優先往高 priority 的網路匹配。這解釋了為什麼手機會「記得」你常用的 Wi-Fi。
另外,DTIM 的意義值得說明一下:dtim_period 指的是每隔幾個 beacon 幀會在裡面攜帶 TIM 資訊,TIM 用來通知 station「AP 這裡有暫存的幀等著你來取」。這個值設小,station 喚醒更頻繁、功耗更高但延遲更低;設大則反之。
設定檔中可定義三種節點:
wpa_ssid 對應 network = { } 節點
wpa_cred 對應 cred = { } 節點
wpa_config_blob 對應 blob-base64- 參數
三者之中,network 節點的用途最廣泛,另外兩個在 Android 上比較少見。
設定檔採「項=值」的形式。network = { } 內部所有可設定的項與值,對應關係存在全域陣列 ssid_fields 中;其餘的全域設定項則存在 global_fields 裡。這兩個陣列是閱讀原始碼時最重要的索引表。
// name:config 檔路徑
// cfgp:之前的 config 資料
// 兩個全域陣列:ssid_fields 存放 network 節點所有可設定值,
// global_fields 存放 config 檔中所有全域可設定項
struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
{
// buf 存放 config 檔中一行的資料,最長不能超過 512
char buf[512], *pos;
// 建立一個空的 config 結構體,其中一些參數已預設好預設值
struct wpa_config *config = wpa_config_alloc_empty(NULL, NULL);
FILE *f = fopen(name, "r");
int id = 0;
// 循環取得檔案中的每一行
while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
// 偵測到 "network={"
if (os_strcmp(pos, "network={") == 0) {
// 進一步讀取網路節點的內容
struct wpa_ssid *ssid = wpa_config_read_network(f, &line, id++);
// 設定網路節點中一些參數的預設值
wpa_config_set_network_defaults(ssid);
// 取得每一行的內容,透過 '=' 匹配,前面是項名稱,後面是值
while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
// ssid_fields 全域陣列中定義了每一項的 name 與取值方式
wpa_config_set(ssid, pos, pos2, *line);
}
// 一個 network 讀完,對內部資訊做升級
// 1. 若同時設定了 passphrase 與 psk_set,用 SHA1 生成 WPA PSK
wpa_config_validate_network(ssid, *line);
}
// 將生成的 ssid 加入 config->pssid 鏈表
wpa_config_add_prio_network(config, ssid);
// cred={
else if (os_strcmp(pos, "cred={") == 0) {
cred = wpa_config_read_cred(f, &line, cred_id++);
}
// blob-base64-
else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
wpa_config_process_blob(config, f, &line, pos + 12);
}
// 其餘為全域參數,存在全域陣列 global_fields
else if (wpa_config_process_global(config, pos, line) < 0) {
// 處理失敗
}
}
}
整個流程的骨架是「逐行掃描 + 狀態分流」:遇到 network={ 就進入子迴圈讀到區塊結束,遇到 cred={ 或 blob-base64- 走各自的分支,其餘一律交給全域參數處理器。讀完一個 network 後的 wpa_config_validate_network 是關鍵一步——passphrase 到 PSK 的轉換就在這裡完成。
int wpa_config_write(const char *name, struct wpa_config *config)
{
FILE *f;
// 寫入全域項
wpa_config_write_global(f, config);
// 寫入 cred 節點
for (cred = config->cred; cred; cred = cred->next) {
wpa_config_write_cred(f, cred);
}
// 存儲 network 節點
for (ssid = config->ssid; ssid; ssid = ssid->next) {
if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
continue; // 不保存暫時網路
if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)
&& !ssid->psk_set && !ssid->passphrase)
continue; // 不保存無效網路
wpa_config_write_network(f, ssid);
}
// 寫入 blob 值
for (blob = config->blobs; blob; blob = blob->next) {
ret = wpa_config_write_blob(f, blob);
}
}
寫入時有兩條過濾規則值得留意:WPS 產生的網路與標記為 temporary 的網路不會落地,缺少 PSK 又沒有 passphrase 的無效網路同樣被跳過。這解釋了為什麼有些連過的 Wi-Fi 重開機後就不見了。
呼叫後回傳一個預先設定好 id 的 ssid 指標。
struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
{
// 遍歷取得 config->ssid 中最大的 id,加一作為新 id
// 建立一個空的 ssid 節點並加入 config->ssid 鏈表
ssid->id = id;
// 更新 pssid 鏈表
wpa_config_update_prio_list(config);
return ssid;
}
依 id 值從 config->ssid 鏈表中查找對應元素。
struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
{
struct wpa_ssid *ssid = config->ssid;
while (ssid) {
if (id == ssid->id)
break;
ssid = ssid->next;
}
return ssid;
}
int wpa_config_remove_network(struct wpa_config *config, int id)
{
// 先找出 id 匹配的 ssid 元素,並從鏈表中摘除
struct wpa_ssid *ssid = config->ssid;
while (ssid) {
if (id == ssid->id)
break;
ssid = ssid->next;
}
// 更新 pssid 列表
wpa_config_update_prio_list(config);
// 釋放空間
wpa_config_free_ssid(ssid);
}
注意:不論是新增還是刪除,wpa_config_update_prio_list 都必須跟著呼叫一次。因為 ssid 與 pssid 是同一批節點的兩種排序視圖,任何一邊變動都要同步重建優先級鏈表,否則選網邏輯會參照到過期資料。
這個函數由外部(framework 或 wpa_cli)呼叫,用來改寫某個 network 節點內的欄位值:
int wpa_config_set(struct wpa_ssid *ssid, const char *var,
const char *value, int line);
把這幾個介面串起來看,wpa_supplicant 的設定管理就是一個典型的「鏈表 + 排序視圖 + 欄位分派表」組合:wpa_config 持有鏈表、ssid_fields 決定每個欄位怎麼解析與序列化、pssid 提供優先級視圖。理解這三層,後續要追掃描、選網或重連的邏輯都會順手很多。
Android drawable 資源完整整理:從 bitmap 到 animation-list
Drawable 資源總覽 Android 的 drawable 資源種類很多,除了常見的 shape、selector、layer-list,還有 bitmap、cli
自定義 RefreshRecyclerView:下拉刷新與載入更多的完整實作
ListView 已經用了很多年,後來有了 RecyclerView,基本可以取代 ListView / GridView,還支援瀑布流模式,加上各種動畫效果,於是就試著
AndFix 熱修復框架原理與源碼深度解析
熱修復的核心訴求很簡單:應用發佈後發現嚴重 Bug,能不能不重新發版、不強迫用戶更新,就把問題修掉?AndFix 給出的答案是方法級別的替換——把有問題的方法整體換成補丁
Android 自定義 View 之 Path 詳解
假期將至,先祝大家玩得開心。今天要聊的主角是 Path。 Path 這個詞給人的第一聯想是那種彎彎曲曲的小徑,總讓人想起「曲徑通幽處」這類句子。而在 Android 繪圖