我正在编写自己的 C 库来管理 linux 中的 wlan。我基于 wpa_cli 接口,但我不明白,为什么他们使用两个 wpa_ctrl 结构:
static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *mon_conn;
当我仅使用 ctrl_conn 打开和附加时它也有效吗?
我正在编写自己的 C 库来管理 linux 中的 wlan。我基于 wpa_cli 接口,但我不明白,为什么他们使用两个 wpa_ctrl 结构:
static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *mon_conn;
当我仅使用 ctrl_conn 打开和附加时它也有效吗?
wpa_cli
有两种工作方式:交互式和非交互式
当您有提示时,您正在wpa_cli
交互使用,反之亦然。
这是交互模式:
$ wpa_cli -i wlan0
wpa_cli v2.1
Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi> and contributors
This software may be distributed under the terms of the BSD license.
See README for more details.
Interactive mode
> status
wpa_state=INACTIVE
address=98:fc:11:d1:89:68
uuid=0cb62eb3-776e-55d2-a4f9-983cdd3e48d2
并且是非交互模式:
$ wpa_cli -i wlan0 status
wpa_state=INACTIVE
address=98:fc:11:d1:89:68
uuid=0cb62eb3-776e-55d2-a4f9-983cdd3e48d2
似乎当您使用交互模式时,wpa_cli
同时使用ctrl_conn
和mon_conn
。ctrl_conn
仅用于发送命令,mon_conn
用于获取事件(即,它是通过 附加的事件wpa_ctrl_attach()
)。
而当你使用非交互模式时,wpa_cli
只使用ctrl_conn
因为没有返回事件。
如果您打算使用这些wpa_supplicant
事件(我希望您会这样做),我认为最好使用两个不同的连接,如wpa_ctrl_request()
有关 msg_cb 参数的评论中所述:
/**
* wpa_ctrl_request - Send a command to wpa_supplicant/hostapd
* @ctrl: Control interface data from wpa_ctrl_open()
* @cmd: Command; usually, ASCII text, e.g., "PING"
* @cmd_len: Length of the cmd in bytes
* @reply: Buffer for the response
* @reply_len: Reply buffer length
* @msg_cb: Callback function for unsolicited messages or %NULL if not used
* Returns: 0 on success, -1 on error (send or receive failed), -2 on timeout
*
* This function is used to send commands to wpa_supplicant/hostapd. Received
* response will be written to reply and reply_len is set to the actual length
* of the reply. This function will block for up to two seconds while waiting
* for the reply. If unsolicited messages are received, the blocking time may
* be longer.
*
* msg_cb can be used to register a callback function that will be called for
* unsolicited messages received while waiting for the command response. These
* messages may be received if wpa_ctrl_request() is called at the same time as
* wpa_supplicant/hostapd is sending such a message. This can happen only if
* the program has used wpa_ctrl_attach() to register itself as a monitor for
* event messages. Alternatively to msg_cb, programs can register two control
* interface connections and use one of them for commands and the other one for
* receiving event messages, in other words, call wpa_ctrl_attach() only for
* the control interface connection that will be used for event messages.
*/
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
char *reply, size_t *reply_len,
void (*msg_cb)(char *msg, size_t len));