- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在向 libnl 发送单个 SSID 和频率进行扫描,但我得到了多个扫描结果以及我请求的 SSID 和频率,但我需要单个扫描结果(仅适用于请求的 SSID),如何实现这一点。请帮助我,我也发送了我的代码。该代码将运行。
编译:gcc -g -o scan scantesthandler.c -L/usr/lib/i386-linux-gnu/libnl.so -lnl
使用调试日志运行:NLCB=debug ./scan
#include<assert.h>
#include<errno.h>
#include<ifaddrs.h>
#include<netdb.h>
#include<stddef.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/rtnetlink.h>
#include <netlink/netlink.h>
#include <netlink/msg.h>
#include <netlink/cache.h>
#include <netlink/socket.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <stdlib.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <netlink/route/link.h>
#include <linux/nl80211.h>
static int expectedId;
static int ifIndex;
struct wpa_scan_res
{
unsigned char bssid[6];
int freq;
};
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
{
int *ret = arg;
*ret = err->error;
return NL_SKIP;
}
static int finish_handler(struct nl_msg *msg, void *arg)
{
int *ret = arg;
*ret = 0;
return NL_SKIP;
}
static int ack_handler(struct nl_msg *msg, void *arg)
{
int *err = arg;
*err = 0;
return NL_STOP;
}
static int bss_info_handler(struct nl_msg *msg, void *arg)
{
printf("\nFunction: %s, Line: %d\n",__FUNCTION__,__LINE__);
struct nlattr *tb[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nlattr *bss[NL80211_BSS_MAX + 1];
static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
[NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
[NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
[NL80211_BSS_TSF] = { .type = NLA_U64 },
[NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
[NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
[NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
[NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
[NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
[NL80211_BSS_STATUS] = { .type = NLA_U32 },
[NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
[NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
};
struct wpa_scan_res *r = NULL;
r = (struct wpa_scan_res*)malloc(sizeof(struct wpa_scan_res));
nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
genlmsg_attrlen(gnlh, 0), NULL);
if (!tb[NL80211_ATTR_BSS])
return NL_SKIP;
if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
bss_policy))
return NL_SKIP;
if (bss[NL80211_BSS_BSSID])
memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),6);
if (bss[NL80211_BSS_FREQUENCY])
r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
printf("\nFrequency: %d ,BSSID: %2x:%2x:%2x:%2x:%2x:%2x",r->freq,r->bssid[0],r->bssid[1],r->bssid[2],r->bssid[3],r->bssid[4],r->bssid[5]);
return NL_SKIP;
}
static struct nl_msg* nl80211_scan_common(uint8_t cmd, int expectedId)
{
const char* ssid = "amitssid";
int ret;
struct nl_msg *msg;
int err;
size_t i;
int flags = 0,ifIndex;
msg = nlmsg_alloc();
if (!msg)
return NULL;
// setup the message
if(NULL==genlmsg_put(msg, 0, 0, expectedId, 0, flags, cmd, 0))
{
printf("\nError return genlMsg_put\n");
}
else
{
printf("\nSuccess genlMsg_put\n");
}
ifIndex = if_nametoindex("wlan1");
if(nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifIndex) < 0)
{
goto fail;
}
struct nl_msg *ssids = nlmsg_alloc();
if(nla_put(ssids, 1,strlen(ssid) ,ssid) <0)
{
nlmsg_free(ssids);
goto fail;
}
err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS,ssids);
nlmsg_free(ssids);
if (err < 0)
goto fail;
struct nl_msg *freqs = nlmsg_alloc();
if( nla_put_u32(freqs,1 ,2437) < 0) //amitssid
{
printf("\nnla_put_fail\n");
goto fail;
}
else
{
printf("\nnla_put_u32 pass\n");
}
//add message attributes
if(nla_put_nested(msg, NL80211_FREQUENCY_ATTR_FREQ,freqs) < 0)
{
printf("\nnla_put_nested failing:\n");
}
else
{
printf("\nnla_put_nested pass\n");
}
nlmsg_free(freqs);
if (err < 0)
goto fail;
return msg;
nla_put_failure:
printf("\nnla_put_failure\n");
nlmsg_free(msg);
return NULL;
fail:
nlmsg_free(msg);
return NULL;
}
int main(int argc, char** argv)
{
struct nl_msg *msg= NULL;
int ret = -1;
struct nl_cb *cb = NULL;
int err = -ENOMEM;
int returnvalue,getret;
int ifIndex, callbackret=-1;
struct nl_sock* sk = (void*)nl_handle_alloc();
if(sk == NULL)
{
printf("\nmemory error\n");
return;
}
cb = nl_cb_alloc(NL_CB_CUSTOM);
if(cb == NULL)
{
printf("\nfailed to allocate netlink callback\n");
}
enum nl80211_commands cmd;
if(genl_connect((void*)sk))
{
printf("\nConnected failed\n");
return;
}
//find the nl80211 driverID
expectedId = genl_ctrl_resolve((void*)sk, "nl80211");
if(expectedId < 0)
{
printf("\nnegative error code returned\n");
return;
}
else
{
printf("\ngenl_ctrl_resolve returned:%d\n",expectedId);
}
msg = nl80211_scan_common(NL80211_CMD_TRIGGER_SCAN, expectedId);
if (!msg)
{
printf("\nmsgbal:\n");
return -1;
}
err = nl_send_auto_complete((void*)sk, msg);
if (err < 0)
goto out;
else
{
printf("\nSent successfully\n");
}
err = 1;
nl_cb_err(cb,NL_CB_CUSTOM,error_handler,&err);
nl_cb_set(cb,NL_CB_FINISH,NL_CB_CUSTOM,finish_handler,&err);
nl_cb_set(cb,NL_CB_ACK,NL_CB_CUSTOM,ack_handler,&err);
callbackret = nl_cb_set(cb,NL_CB_VALID,NL_CB_CUSTOM,bss_info_handler,&err);
if(callbackret < 0)
{
printf("\n*************CallbackRet failed:***************** %d\n",callbackret);
}
else
{
printf("\n*************CallbackRet pass:***************** %d\n",callbackret);
}
returnvalue=nl_recvmsgs((void*)sk,cb);
printf("\n returnval:%d\n",returnvalue);
nlmsg_free(msg);
msg = NULL;
msg = nlmsg_alloc();
if (!msg)
return -1;
if(NULL==genlmsg_put(msg, 0, 0, expectedId, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0))
{
printf("\nError return genlMsg_put\n");
}
else
{
printf("\nSuccess genlMsg_put\n");
}
ifIndex = if_nametoindex("wlan1");
printf("\nGet Scaninterface returned :%d\n",ifIndex);
nla_put_u32(msg,NL80211_ATTR_IFINDEX,ifIndex);
err = nl_send_auto_complete((void*)sk,msg);
if(err < 0) goto out;
err = 1;
getret= nl_recvmsgs((void*)sk,cb);
printf("\nGt Scan resultreturn:%d\n",getret);
out:
nlmsg_free(msg);
return err;
nla_put_failure:
printf("\nnla_put_failure\n");
nlmsg_free(msg);
return err;
}
您只需将此代码复制并粘贴到您的系统上并运行即可。
最佳答案
据我所知(并且已经看到),扫描命令和结果取决于供应商的设备驱动程序。可以肯定的是,没有选项可以扫描特定的 ssid;相反,您可以做的是获取所有扫描结果并循环检查 ssid 是否在列表中(wpa_supplicant 使用此机制将网络配置与扫描结果进行匹配)。
现在对于频率,如果设备驱动程序具有该功能,则应该可以仅扫描特定 channel 。但通常扫描命令会扫描所有 channel 并返回 SSID(想想您的网络管理器;它显示为扫描命令找到的所有可用 SSID。这本质上是由设备驱动程序通过 cfg80211 发布的)。
关于c - 如何向libnl发送单 channel 扫描请求,并接收相应 channel 的单 channel 扫描完成响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760780/
我正在尝试配置 powertop-2.5,但是当我运行 ./configure 时,出现“配置:错误:需要 libnl 和 libnl-genl 但未找到”错误 我跑了 sudo apt-get in
我正在尝试使用 libnl 创建桥并更改它的标志。代码: #include #include #include #include #define BRIDGE_NAME "brr" int m
#include #include struct nl_sock *sock; sock = nl_socket_alloc(); 上面的代码总是编译失败,报如下错误: /home/micah/D
我正在尝试在 Ubuntu 下使用 libnl 3 ( http://www.infradead.org/~tgr/libnl/ ) 来使用 netlink 以便从 IPv6 邻居缓存中获取一些信息。
我正在尝试使用 netlink 套接字和通用消息类型来实现内核用户通信。到目前为止,我能够将消息从用户空间发送到内核,然后将消息发送回用户空间。问题是在我的用户空间程序中,我总是收到一个错误,即收到了
我正在创建一个 C 库来管理我的嵌入式设备的许多外围设备。 S.O.使用的是用yocto编译的linux dristro。我正在尝试使用 netlink(使用 libnl 命令)实现一些功能,将我的设
我开始使用 iwconfig/ioctl 编写代码来处理 wifi 卡,当我意识到它已被弃用并且大多数应用程序使用 nl80211 时。我开始阅读它的源代码,但没有文档,代码有点复杂。如何使用 nl8
最近,我正在研究iw 的源代码,试图了解libnl 是如何工作的。 由于我无法理解的原因,我在 iw.c 的某个点遇到了 nl_socket_set_cb() 的使用。网络上也没有文档。 我的第一个怀
我尝试扩展“iw”实用程序以允许它设置 802.11 争用窗口的最大和最小大小。但我总是收到“无效参数 (-22)”返回。 我编辑了 iw-3.15 源代码的 phy.c 并附加了 static in
我正在尝试将 Linux/Android C 应用程序迁移到 macOS/iOS。 我有这个问题,这个应用程序有时使用 libnl 库,在源代码中你可以看到包括如下内容: #include #inc
我正在创建一个 C 库来管理我的嵌入式设备的许多外围设备。 S.O. used,是一个用yocto编译的Linux发行版。我正在尝试使用 netlink(使用 libnl 命令)实现一些功能,将我的设
我有类似的问题: Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY) 但是 nl_recvmsgs_defaul
我想从 Linux 上的用户空间程序读取 Wi-Fi 信号强度。 由于 /proc/net/wireless 在系统上不可用,我发现 libnl应该用于获取信息。 与仅仅读取/proc中的值相比,通过
我必须安装 falcon-sensor rpm 包才能使 Crowdstrike 出现在服务器上,并且它需要 libn1 RPM 包作为依赖项。 我可以从https://rpmfind.net/获取它
我正在尝试编写一些基本的内核模块——使用 netlink 套接字(用户端的 libnl)的用户空间程序通信。用户空间程序向内核发送消息并期待回复。不幸的是,接收回复失败,返回值 -16 (EBUSY)
我想使用 netlink socket 和 taskstats 读取单个线程的 linux 内核统计信息。 我可以使用 python 包装器 ( https://github.com/facebook
我正在尝试使用 libnl-3-routing 添加路由(内核 2.6.32-57)。我使用文档: Netlink library Netlink routing 但是路由章节是空的...;) 问题是
我的目标是使用 libnl在 Android 应用程序中获取在最新网络扫描中接收到的信标帧列表并打印(显示)关联的元数据,例如 AP SSID、AP BSSID 等... 遵循基本教程和指南,方法似乎
我正在尝试编译编写为 here 的源代码,触发用户空间的 Wi-Fi 主动扫描。 它需要 netlink 库,所以我完成了 sudo apt-get install libnl-3-dev libnl
我正在尝试在 shell 命令工作时在 ubuntu 12.04 上进行网络模拟: tc qdisc add dev eth1 root handle 1:0 tbf rate 200kbit buf
我是一名优秀的程序员,十分优秀!