TCP/IP Socket 和 Unix Socket最大的區(qū)別就是unix-socket沒有port,使用文件handle作為傳輸對(duì)象,但是只能在同物理主機(jī)內(nèi)運(yùn)行,相對(duì)ip-socket速度也更快,有人驗(yàn)證過unix-socket要比ip-socket快31%。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
WPA_Supplicant連接方式簡(jiǎn)析

/third_party/wpa_supplicant
|-- CONTRIBUTIONS # 貢獻(xiàn)說明
|-- COPYING # 版權(quán)說明
|-- wpa_supplicant-2.9 # 輕量級(jí)系統(tǒng)的wpa_supplicant
| |-- hostapd # Wi-Fi熱點(diǎn)相關(guān)功能(Access Point)
| |-- hs20 # 熱點(diǎn)2.0(Hotspot2.0)相關(guān)功能
| |-- src # Wi-Fi熱點(diǎn)與Wi-Fi接入點(diǎn)共用的代碼
| |-- wpa_supplicant # Wi-Fi接入相關(guān)功能(Station)
| `-- wpa_supplicant_lib # OpenHarmony對(duì)Wi-Fi新開發(fā)的業(yè)務(wù)代碼
`-- wpa_supplicant-2.9_standard # 標(biāo)準(zhǔn)系統(tǒng)的wpa_supplicant
| |-- hostapd # Wi-Fi熱點(diǎn)相關(guān)功能(Access Point)
| |-- hs20 # 熱點(diǎn)2.0(Hotspot2.0)相關(guān)功能
| |-- src # Wi-Fi熱點(diǎn)與Wi-Fi接入點(diǎn)共用的代碼
| |-- wpa_supplicant # Wi-Fi接入相關(guān)功能(Station)
| `-- wpa_supplicant_lib # OpenHarmony對(duì)Wi-Fi新開發(fā)的業(yè)務(wù)代碼
- WPA_Supplicant支持的連接方式。
struct wpa_ctrl {
#ifdef CONFIG_CTRL_IFACE_UDP
int s;
#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
struct sockaddr_in6 local;
struct sockaddr_in6 dest;
#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
struct sockaddr_in local;
struct sockaddr_in dest;
#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
char *cookie;
char *remote_ifname;
char *remote_ip;
#endif /* CONFIG_CTRL_IFACE_UDP */
#ifdef CONFIG_CTRL_IFACE_UNIX
int s;
struct sockaddr_un local;
struct sockaddr_un dest;
#endif /* CONFIG_CTRL_IFACE_UNIX */
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
HANDLE pipe;
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
};
在以上的結(jié)構(gòu)體定義里我們可以看到一共定義了4個(gè)宏定義標(biāo)明4中wpa_supplicant
- CONFIG_CTRL_IFACE_UDP
- 使用ipv4 socket接口與client進(jìn)行連接和通訊,默認(rèn)使用localhost(127.0.0.1)
- CONFIG_CTRL_IFACE_UDP_IPV6
- 使用ipv6 socket接口與client進(jìn)行連接和通訊,默認(rèn)使用localhost(::1)
- CONFIG_CTRL_IFACE_UNIX
- 使用unix
socket接口與client進(jìn)行連接和通訊,是Linux和*BSD的默認(rèn)配置,默認(rèn)在linux系統(tǒng)使用/var/run/wpa_supplicant,在android系統(tǒng)使用/data/misc/wifi/sockets
- CONFIG_CTRL_IFACE_NAMED_PIPE
- 使用Windows Named Pipe模式與client進(jìn)行連接和通訊,是windows的默認(rèn)配置
TCP/IP Socket 和 Unix
Socket最大的區(qū)別就是unix-socket沒有port,使用文件handle作為傳輸對(duì)象,但是只能在同物理主機(jī)內(nèi)運(yùn)行,相對(duì)ip-socket速度也更快,有人驗(yàn)證過unix-socket要比ip-socket快31%。
WPA_Supplicant的控制接口。
# ps -A | grep wifi
497 ? 00:00:04 wifi_hal_servic
543 ? 00:00:00 wifi_host
629 ? 00:01:58 wifi_manager_se
# netstat -axp | grep wifi
unix 2 [ ACC ] STREAM LISTENING 2951369 25629/ohos.sample.w@25629ohos.sample.wifitest
unix 2 [ ACC ] STREAM LISTENING 18673 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
unix 2 [ ] DGRAM 18851 543/wifi_host
unix 2 [ ] DGRAM 18672 497/wifi_hal_servic
unix 2 [ ] DGRAM 19185 629/wifi_manager_se
# ps -A | grep wifi
497 ? 00:00:04 wifi_hal_servic
543 ? 00:00:00 wifi_host
629 ? 00:01:58 wifi_manager_se
779 ? 00:00:01 wifi_hal_servic
# netstat -axp | grep wifi
unix 2 [ ACC ] STREAM LISTENING 2951369 25629/ohos.sample.w@25629ohos.sample.wifitest
unix 2 [ ACC ] STREAM LISTENING 18673 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
unix 3 [ ] STREAM CONNECTED 3165678 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
unix 3 [ ] STREAM CONNECTED 3167635 629/wifi_manager_se
unix 2 [ ] DGRAM 18851 543/wifi_host
unix 2 [ ] DGRAM 18672 497/wifi_hal_servic
unix 3 [ ] STREAM CONNECTED 3167636 629/wifi_manager_se
unix 2 [ ] DGRAM 19185 629/wifi_manager_se
unix 3 [ ] STREAM CONNECTED 3167634 629/wifi_manager_se
unix 3 [ ] STREAM CONNECTED 3165679 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
unix 3 [ ] STREAM CONNECTED 3165020 629/wifi_manager_se
unix 3 [ ] STREAM CONNECTED 3167645 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
unix 3 [ ] STREAM CONNECTED 3165700 497/wifi_hal_servic/data/service/el1/public/wifi/unix_sock.sock
《簡(jiǎn)析OpenHarmony的WiFi能力》大概介紹過OpenHarmony里的WiFi架構(gòu),對(duì)應(yīng)上面的命令行可以看出,wifi在后臺(tái)啟動(dòng)了3個(gè)服務(wù)
- wifi_manager_service:wifi服務(wù)層,為wifi框架層服務(wù),對(duì)下通過wpa_hal與wpa_hal_service通信。
- wifi_host:wlan啟動(dòng)的設(shè)備服務(wù),對(duì)應(yīng)vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs。
- wifi_hal_service:wifi 硬件抽象服務(wù),對(duì)應(yīng)加載wpa_supplicant,與hdf進(jìn)行交互,對(duì)上提供wpa_client.so
- 客戶端wpa_supplicant提供兩種客戶端:
- wpa_cli:用于和wpa_supplicant交互;使用wpa_cli可以實(shí)現(xiàn)wifi的具體功能。
# wpa_cli -i wlan0 scan
wpa_ctrl_request cmd: GET_COOKIE
wpa_ctrl_request cmd: IFNAME
wpa_ctrl_request cmd: SCAN
OK
# wpa_cli -i wlan0 scan_result
wpa_ctrl_request cmd: GET_COOKIE
wpa_ctrl_request cmd: IFNAME
wpa_ctrl_request cmd: SCAN_RESULTS
bssid / frequency / signal level / flags / ssid / informationElements
6e:b1:58:65:63:67 5805 -39 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] [7 434e2024042395052300][61 a1070000000000000000000000000000000000000000][192 019b00faff]
6c:b1:58:75:63:67 5805 -39 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_6365 [7 434e2024042395052300][61 a1070000000000000000000000000000000000000000][192 019b00faff]
f4:84:8d:01:69:ac 5785 -51 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] Graphic [7 434e2024042395052300][61 9d050400000000000000000000000000000000000000][192 019b00faff]
f6:84:8d:21:69:ac 5785 -52 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] [7 434e2024042395052300][61 9d050400000000000000000000000000000000000000][192 019b00faff]
f2:45:21:15:5e:75 5745 -60 [WPA2-PSK+SAE-CCMP][SAE-H2E][ESS] iPhone 13 Pro Max [7 434e202401172801172c01173001173401173801173c011740011795011b99011b9d011ba1011ba5011b][61 95050400000000000000000000000000000000000000][192 019b000000]
f8:af:05:87:7a:40 5180 -74 [WPA2-PSK-CCMP][WPS][ESS] dist [61 240d0400000000000000000000000000000000000000][192 012a00faff]
f8:af:05:f7:7a:40 5180 -74 [WPA2-PSK-CCMP][WPS][ESS] [61 240d0000000000000000000000000000000000000000][192 012a00faff]
f8:af:05:87:7a:42 5180 -71 [WPA2-PSK-CCMP][WPS][ESS] [61 240d0000000000000000000000000000000000000000][192 012a00faff]
6c:b1:58:75:63:65 2462 -40 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_6365 [7 434e20010d1b][61 0b070000000000000000000000000000000000000000]
6e:b1:58:65:63:65 2462 -40 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] [7 434e20010d1b][61 0b070000000000000000000000000000000000000000]
f4:84:8d:01:69:aa 2437 -47 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] Graphic [7 434e20010d14][61 06070000000000000000000000000000000000000000]
f6:84:8d:01:69:aa 2437 -46 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] [7 434e20010d14][61 06070000000000000000000000000000000000000000]
對(duì)應(yīng)的主要功能:
- wpa_ctrl_open() : 連接WPAS的控制接口, 可以是WPAS的全局控制接口,也可以是為每一個(gè)無線網(wǎng)絡(luò)接口指定的控制接口。
- wpa_ctrl_close() : 關(guān)閉wpa_ctrl_open()打開的連接。
- wpa_ctrl_request() : 通過建立的連接向WPAS發(fā)送消息。
- wpa_ctrl_attach() : 使用wpa_ctrl_open()建立的連接, WPAS默認(rèn)不會(huì)向這些連接的client端發(fā)送event, 必須顯示調(diào)用wpa_ctrl_attach(),才能接收到消息。
- wpa_ctrl_detach() : 取消wpa_ctrl_attach()。
- wpa_ctrl_recv() : 接收WPAS端發(fā)來的event, 必須要先在打開的連接上調(diào)用wpa_ctrl_attach()才能接收到event, 當(dāng)無event可讀時(shí), 此調(diào)用會(huì)被block住。
- wpa_ctrl_pending() : 檢查是否有pending的event, 若有則可以調(diào)用wpa_ctrl_recv()來接收。
- wpa_ctrl_get_fd() : 獲取同WPAS的連接中, client端的fd, 獲取的fd可以用于select, epoll等, 但是不能直接用于收發(fā)消息, 必須使用wpa_ctrl_request()和wpa_ctrl_recv()。
- wpa_ctrl_cleanup() : 當(dāng)使用unix socekt 進(jìn)行連接時(shí),會(huì)建立socket文件, 若其carsh, 則會(huì)遺留這些文件, wpa_ctrl_cleanup()用于清理這些文件。
- hostapd_cli:用于和hostapd交互;(當(dāng)設(shè)置wlan為ap模式時(shí)候可以使用此功能)
- 服務(wù)端
- wpa_supplicant提供兩種服務(wù)端:
- wpa_supplicant:Wi-Fi接入相關(guān)功能,對(duì)應(yīng)wifi_hal_sta_interface.c啟動(dòng)或停止,此文件在wifi的services層,對(duì)應(yīng)之前的wifi_hal_service;這也解釋了為啥開wifi后會(huì)多出一個(gè)wifi_hal_service。
- hostapd:Wi-Fi熱點(diǎn)相關(guān)功能,對(duì)應(yīng)wifi_hal_ap_interface.c啟動(dòng)或停止,此文件在wifi的services層,對(duì)應(yīng)之前的wifi_hal_service。
# ls ./system/etc/wifi/
hostapd.conf p2p_supplicant.conf wpa_supplicant.conf
//熱點(diǎn)配置
# cat ./system/etc/wifi/hostapd.conf
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
interface=wlan0
ctrl_interface=udp
ssid=testap
hw_mode=g
channel=1
//點(diǎn)對(duì)點(diǎn)傳輸模式
# cat ./system/etc/wifi/p2p_supplicant.conf
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
ctrl_interface=udp
device_name=p2p_ohos
manufacturer=ohos
device_type=10-0050F204-5
config_methods=virtual_push_button physical_display keypad
p2p_listen_reg_class=81
p2p_listen_channel=1
p2p_oper_reg_class=81
p2p_oper_channel=1
p2p_go_intent=0
persistent_reconnect=1
serial_number=0123456789ABCDEF
p2p_ssid_postfix=-ohos
p2p_go_ht40=1
p2p_go_vht=1
update_config=1
//默認(rèn)的接入點(diǎn)模式
# cat ./system/etc/wifi/wpa_supplicant.conf
# Copyright (C) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
country=GB
ctrl_interface=udp
network={
}
此處多了個(gè)p2p的配置,其實(shí)就是wpa_supplicat啟動(dòng)時(shí)候使用的不同配置,也就是說wpa_supplicant可以作為接入點(diǎn)模式啟動(dòng)也可以作為p2p模式啟動(dòng)。
小結(jié)
以上就是對(duì)OpenHarmony里的wap_supplicant的大概分析,具體代碼分析可以自己搜索wap_supplicant的相關(guān)文章。
??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??
??51CTO 開源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??