【发布时间】:2019-09-04 17:54:06
【问题描述】:
我正在创建一个 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;
}
谢谢大家!
【问题讨论】: