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

??想了解更多關于開源的內容,請訪問:??
??51CTO 開源基礎軟件社區??
??https://ost.51cto.com??
WPA_Supplicant連接方式簡析

/third_party/wpa_supplicant
|-- CONTRIBUTIONS # 貢獻說明
|-- COPYING # 版權說明
|-- wpa_supplicant-2.9 # 輕量級系統的wpa_supplicant
| |-- hostapd # Wi-Fi熱點相關功能(Access Point)
| |-- hs20 # 熱點2.0(Hotspot2.0)相關功能
| |-- src # Wi-Fi熱點與Wi-Fi接入點共用的代碼
| |-- wpa_supplicant # Wi-Fi接入相關功能(Station)
| `-- wpa_supplicant_lib # OpenHarmony對Wi-Fi新開發的業務代碼
`-- wpa_supplicant-2.9_standard # 標準系統的wpa_supplicant
| |-- hostapd # Wi-Fi熱點相關功能(Access Point)
| |-- hs20 # 熱點2.0(Hotspot2.0)相關功能
| |-- src # Wi-Fi熱點與Wi-Fi接入點共用的代碼
| |-- wpa_supplicant # Wi-Fi接入相關功能(Station)
| `-- wpa_supplicant_lib # OpenHarmony對Wi-Fi新開發的業務代碼
- 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 */
};
在以上的結構體定義里我們可以看到一共定義了4個宏定義標明4中wpa_supplicant
- CONFIG_CTRL_IFACE_UDP
- 使用ipv4 socket接口與client進行連接和通訊,默認使用localhost(127.0.0.1)
- CONFIG_CTRL_IFACE_UDP_IPV6
- 使用ipv6 socket接口與client進行連接和通訊,默認使用localhost(::1)
- CONFIG_CTRL_IFACE_UNIX
- 使用unix
socket接口與client進行連接和通訊,是Linux和*BSD的默認配置,默認在linux系統使用/var/run/wpa_supplicant,在android系統使用/data/misc/wifi/sockets
- CONFIG_CTRL_IFACE_NAMED_PIPE
- 使用Windows Named Pipe模式與client進行連接和通訊,是windows的默認配置
TCP/IP Socket 和 Unix
Socket最大的區別就是unix-socket沒有port,使用文件handle作為傳輸對象,但是只能在同物理主機內運行,相對ip-socket速度也更快,有人驗證過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
《簡析OpenHarmony的WiFi能力》大概介紹過OpenHarmony里的WiFi架構,對應上面的命令行可以看出,wifi在后臺啟動了3個服務
- wifi_manager_service:wifi服務層,為wifi框架層服務,對下通過wpa_hal與wpa_hal_service通信。
- wifi_host:wlan啟動的設備服務,對應vendor/hihope/rk3568/hdf_config/uhdf/device_info.hcs。
- wifi_hal_service:wifi 硬件抽象服務,對應加載wpa_supplicant,與hdf進行交互,對上提供wpa_client.so
- 客戶端wpa_supplicant提供兩種客戶端:
- wpa_cli:用于和wpa_supplicant交互;使用wpa_cli可以實現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]
對應的主要功能:
- wpa_ctrl_open() : 連接WPAS的控制接口, 可以是WPAS的全局控制接口,也可以是為每一個無線網絡接口指定的控制接口。
- wpa_ctrl_close() : 關閉wpa_ctrl_open()打開的連接。
- wpa_ctrl_request() : 通過建立的連接向WPAS發送消息。
- wpa_ctrl_attach() : 使用wpa_ctrl_open()建立的連接, WPAS默認不會向這些連接的client端發送event, 必須顯示調用wpa_ctrl_attach(),才能接收到消息。
- wpa_ctrl_detach() : 取消wpa_ctrl_attach()。
- wpa_ctrl_recv() : 接收WPAS端發來的event, 必須要先在打開的連接上調用wpa_ctrl_attach()才能接收到event, 當無event可讀時, 此調用會被block住。
- wpa_ctrl_pending() : 檢查是否有pending的event, 若有則可以調用wpa_ctrl_recv()來接收。
- wpa_ctrl_get_fd() : 獲取同WPAS的連接中, client端的fd, 獲取的fd可以用于select, epoll等, 但是不能直接用于收發消息, 必須使用wpa_ctrl_request()和wpa_ctrl_recv()。
- wpa_ctrl_cleanup() : 當使用unix socekt 進行連接時,會建立socket文件, 若其carsh, 則會遺留這些文件, wpa_ctrl_cleanup()用于清理這些文件。
- hostapd_cli:用于和hostapd交互;(當設置wlan為ap模式時候可以使用此功能)
- 服務端
- wpa_supplicant:Wi-Fi接入相關功能,對應wifi_hal_sta_interface.c啟動或停止,此文件在wifi的services層,對應之前的wifi_hal_service;這也解釋了為啥開wifi后會多出一個wifi_hal_service。
- hostapd:Wi-Fi熱點相關功能,對應wifi_hal_ap_interface.c啟動或停止,此文件在wifi的services層,對應之前的wifi_hal_service。
# ls ./system/etc/wifi/
hostapd.conf p2p_supplicant.conf wpa_supplicant.conf
//熱點配置
# 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
//點對點傳輸模式
# 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
//默認的接入點模式
# 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={
}
此處多了個p2p的配置,其實就是wpa_supplicat啟動時候使用的不同配置,也就是說wpa_supplicant可以作為接入點模式啟動也可以作為p2p模式啟動。
小結
以上就是對OpenHarmony里的wap_supplicant的大概分析,具體代碼分析可以自己搜索wap_supplicant的相關文章。
??想了解更多關于開源的內容,請訪問:??
??51CTO 開源基礎軟件社區??
??https://ost.51cto.com??