gpt4 book ai didi

c - 如何使用 libnl 和 netlink 套接字以编程方式将设备连接到 AP?

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:54 26 4
gpt4 key购买 nike

我正在创建一个 C 库来管理我的嵌入式设备的许多外围设备。 S.O. used,是一个用yocto编译的Linux发行版。我正在尝试使用 netlink(使用 libnl 命令)实现一些功能,将我的设备连接到 wifi(知名)路由器。在this的帮助下社区,我开发了一个能够扫描该地区路由器的功能。你们中的一些人知道如何使用 libnl 命令将我的设备连接到路由器 wifi 吗?我开发了以下代码,尝试连接到名为“Validator_Test”(没有身份验证密码)的 AP。软件没有返回错误,但我的设备仍然与 AP 断开连接。你们中的一些人知道我的代码有什么问题吗?不幸的是,我没有找到此操作的任何示例或文档。

static int ap_conn() {

struct nl_msg *msg = nlmsg_alloc();

int if_index = if_nametoindex("wlan0"); // Use this wireless interface for scanning.

// Open socket to kernel.
struct nl_sock *socket = nl_socket_alloc(); // Allocate new netlink socket in memory.
genl_connect(socket); // Create file descriptor and bind socket.
int driver_id = genl_ctrl_resolve(socket, "nl80211"); // Find the nl80211 driver ID.

genlmsg_put(msg, 0, 0, driver_id, 0, (NLM_F_REQUEST | NLM_F_ACK), NL80211_CMD_CONNECT, 0);

nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index); // Add message attribute, which interface to use.

nla_put(msg, NL80211_ATTR_SSID, strlen("Validator_Test"), "Validator_Test");

nla_put(msg, NL80211_ATTR_MAC, strlen("00:1e:42:21:e4:e9"), "00:1e:42:21:e4:e9");

int ret = nl_send_auto_complete(socket, msg); // Send the message.
printf("NL80211_CMD_CONNECT sent %d bytes to the kernel.\n", ret);
ret = nl_recvmsgs_default(socket); // Retrieve the kernel's answer. callback_dump() prints SSIDs to stdout.
nlmsg_free(msg);

if (ret < 0) {
printf("ERROR: nl_recvmsgs_default() returned %d (%s).\n", ret, nl_geterror(-ret));
return ret;
}

nla_put_failure:
return -ENOSPC;

谢谢大家!

最佳答案

感谢您的代码。

根据你的代码,我在这里修改并做了测试;有用。源代码位于: https://github.com/neojou/nl80211/blob/master/test_connect/src/test_connect_nl80211.c

对此的一些建议:

  1. 确保测试环境正确

    在测试代码之前,也许你可以尝试使用 iw 来做测试。iw 是开源工具,它也使用 netlink。你可以输入“sudo iw wlan0 connect Validator_Test”然后使用 iwconfig 先查看它是否已连接。(假设你所说的AP没有安全设置)

  2. 你的源码和我的有两点不同

    (1) 不需要设置NL80211_ATTR_MAC

    (2) ret = nl_recvmsgs_default(套接字);

    不确定你的ap_conn()的返回值有没有判断, 但是当 nl_recvmsgs_default() 返回 0 时,在 ap_conn() 中返回 0 似乎更好。

关于c - 如何使用 libnl 和 netlink 套接字以编程方式将设备连接到 AP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com