WPA_Supplicant连接方式简析

系统 OpenHarmony
TCP/IP Socket 和 Unix Socket最大的区别就是unix-socket没有port,使用文件handle作为传输对象,但是只能在同物理主机内运行,相对ip-socket速度也更快,有人验证过unix-socket要比ip-socket快31%。

想了解更多关于开源的内容,请访问:

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

WPA_Supplicant连接方式简析

#创作者激励# #跟着小白一起学鸿蒙# 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新开发的业务代码
  1. 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的控制接口。

  • 关wifi时:
# 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
  • 开wifi时:
# 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
  1. 客户端wpa_supplicant提供两种客户端:
  2. 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()用于清理这些文件。
  1. hostapd_cli:用于和hostapd交互;(当设置wlan为ap模式时候可以使用此功能)
  2. 服务端
  • wpa_supplicant提供两种服务端:
  1. wpa_supplicant:Wi-Fi接入相关功能,对应wifi_hal_sta_interface.c启动或停止,此文件在wifi的services层,对应之前的wifi_hal_service;这也解释了为啥开wifi后会多出一个wifi_hal_service。
  2. 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​

责任编辑:jianghua 来源: 51CTO 开源基础软件社区
相关推荐

2010-01-20 14:25:56

函数调用

2011-07-12 14:04:58

2010-09-08 16:25:39

SIP协议栈

2010-09-10 09:52:44

开源协议栈

2009-06-08 21:25:29

Java声音技术

2010-09-13 16:58:13

2010-05-07 12:39:05

SQL Server负载均衡

2022-06-04 16:11:14

勒索软件加密Nokoyawa

2011-07-22 11:05:15

光纤通信

2010-06-21 14:05:08

Linux APM

2009-08-13 15:48:57

C#指针

2023-03-17 16:26:25

开源鸿蒙蓝牙能力

2011-07-20 10:20:04

2010-04-19 12:28:57

2010-04-12 15:39:37

无线接入技术应用

2013-12-05 10:13:25

iOS图标

2011-10-28 11:22:06

Android 4.0

2012-11-21 20:58:07

一体机专家集成系统PureSystems

2011-07-27 18:01:09

组策略

2013-09-25 09:55:05

交换机技术
点赞
收藏

51CTO技术栈公众号