如何在Windows本地WiFi中获取Wi-Fi信息元素

huangapple go评论68阅读模式
英文:

How to get Wi-Fi Information Elements in Windows Native Wifi

问题

Windows的Native Wifi文档提到,可以通过WLAN_BSS_ENTRY结构的ulIeOffset和ulIeSize成员来获取来自Beacon或Probe Response帧的802.11信息元素(IEs),这两个成员定义了包含IE数据块的内存区域。但是,我尝试使用这两个变量来检索IEs时未成功。

我通过GetAvailableNetworkList()获得了多个有效的WLAN_BSS_ENTRY结构(正确填充了802.11网络信息 - SSID、BSSID等),ulIeOffset和ulIeSize的值也是有意义的,但是当尝试访问该内存区域时出现了读取异常。

我的测试平台是Windows 10,包括截至本帖日期的所有更新,最新的Visual Studio Community版本,并且我编写了一个简单的控制台应用程序,用于打印所有WLAN_BSS_ENTRY字段(成功),以及IE数据块(不成功)。与扫描相关的C++代码如下所示。感谢任何见解。

// 检索扫描到的网络
dwResult = WlanGetNetworkBssList(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid,
        NULL, dot11_BSS_type_any, false, NULL, &pBssList);
if (dwResult == ERROR_SUCCESS) {
    for (DWORD i = 0; i < pBssList->dwNumberOfItems; i++) {
        const WLAN_BSS_ENTRY bssEntry = pBssList->wlanBssEntries[i];
        std::cout << "SSID: " << bssEntry.dot11Ssid.ucSSID << std::endl;
        std::cout << "RSSI: " << bssEntry.lRssi << " dBm" << std::endl;
        // 打印其他bssEntry字段,全部有效
        std::cout << "IE缓冲区偏移量: " << bssEntry.ulIeOffset << std::endl;
        std::cout << "IE缓冲区大小: " << bssEntry.ulIeSize << std::endl;
        // --> 以下IE检索不起作用(引发读取异常) <--
        const PBYTE ieBuffer = (const PBYTE)((PBYTE)(&bssEntry) + bssEntry.ulIeOffset);
        for (DWORD j = 0; j < bssEntry.ulIeSize; j++) {
           printf("%02X ", ieBuffer[j]);
        }
        std::cout << std::endl;
        std::cout << std::endl;
    }
    WlanFreeMemory(pBssList);
}

希望这能帮助你解决IE数据检索的问题。

英文:

The Windows Native Wifi documentation mentions 802.11 Information Elements (IEs) from Beacon or Probe Response frames are available through the WLAN_BSS_ENTRY structure, specifically, through its ulIeOffset and ulIeSize members, which define an area of memory that contains the IE data blob. I've been unsuccessful with retrieving IEs using these two variables.

I get multiple valid WLAN_BSS_ENTRY structures through GetAvailableNetworkList() (correctly populated with 802.11 network information - SSID, BSSID, etc.) - ulIeOffset and ulIeSize values make sense too, but I get read exception when trying to access that memory area.

My test platform is Windows 10 with all updates as of this post date, latest Visual Studio Community edition, and I wrote a simple console application that prints all WLAN_BSS_ENTRY fields (successful), and the IE data blob (unsuccessful). C++ code related to scan is shown below. Any insight appreciated.

// retrieve scanned networks
dwResult = WlanGetNetworkBssList(hClient, &amp;pIfList-&gt;InterfaceInfo[0].InterfaceGuid,
        NULL, dot11_BSS_type_any, false, NULL, &amp;pBssList);
if (dwResult == ERROR_SUCCESS) {
    for (DWORD i = 0; i &lt; pBssList-&gt;dwNumberOfItems; i++) {
        const WLAN_BSS_ENTRY bssEntry = pBssList-&gt;wlanBssEntries[i];
        std::cout &lt;&lt; &quot;SSID: &quot; &lt;&lt; bssEntry.dot11Ssid.ucSSID &lt;&lt; std::endl;
        std::cout &lt;&lt; &quot;RSSI: &quot; &lt;&lt; bssEntry.lRssi &lt;&lt; &quot; dBm&quot; &lt;&lt; std::endl;
        // print bunch of other bssEntry fields, all valid
        std::cout &lt;&lt; &quot;IE buffer offset: &quot; &lt;&lt; bssEntry.ulIeOffset &lt;&lt; std::endl;
        std::cout &lt;&lt; &quot;IE buffer size: &quot; &lt;&lt; bssEntry.ulIeSize &lt;&lt; std::endl;
        // --&gt; IE retrieval below doesn&#39;t work (throws read exception) &lt;--
        const PBYTE ieBuffer = (const PBYTE)((PBYTE)(&amp;bssEntry) + bssEntry.ulIeOffset);
        for (DWORD j = 0; j &lt; bssEntry.ulIeSize; j++) {
           printf(&quot;%02X &quot;, ieBuffer[j]);
        }
        std::cout &lt;&lt; std::endl;
        std::cout &lt;&lt; std::endl;
    }
    WlanFreeMemory(pBssList);
}

答案1

得分: 1

问题在于这段代码:

const WLAN_BSS_ENTRY bssEntry = pBssList->wlanBssEntries[i];

它复制了WLAN_BSS_ENTRY结构,因此它的地址指向内存中的另一个位置。你想要保持所有内容都作为指针:

WlanGetNetworkBssList(hClient, &pIfList->InterfaceInfo[0].InterfaceGuid, NULL, dot11_BSS_type_any, false, NULL, &pBssList);
for (DWORD i = 0; i < pBssList->dwNumberOfItems; i++) {

    // 使用原始指针
    PWLAN_BSS_ENTRY bssEntry = &pBssList->wlanBssEntries[i];

    std::cout << "SSID: " << bssEntry->dot11Ssid.ucSSID << std::endl;
    std::cout << "RSSI: " << bssEntry->lRssi << " dBm" << std::endl;
    std::cout << "IE buffer offset: " << bssEntry->ulIeOffset << std::endl;
    std::cout << "IE buffer size: " << bssEntry->ulIeSize << std::endl;

    PBYTE ieBuffer = (PBYTE)bssEntry + bssEntry->ulIeOffset;
    for (DWORD j = 0; j < bssEntry->ulIeSize; j++) {
        printf("%02X ", ieBuffer[j]);
    }
    std::cout << std::endl;
    std::cout << std::endl;
}
WlanFreeMemory(pBssList);
英文:

The problem is this code:

const WLAN_BSS_ENTRY bssEntry = pBssList-&gt;wlanBssEntries[i];

makes a copy of the WLAN_BSS_ENTRY structure, so it's address points to another place in memory. You want to keep everything as pointers:

WlanGetNetworkBssList(hClient, &amp;pIfList-&gt;InterfaceInfo[0].InterfaceGuid, NULL, dot11_BSS_type_any, false, NULL, &amp;pBssList);
for (DWORD i = 0; i &lt; pBssList-&gt;dwNumberOfItems; i++) {

    // use the original pointer
	PWLAN_BSS_ENTRY bssEntry = &amp;pBssList-&gt;wlanBssEntries[i]

	std::cout &lt;&lt; &quot;SSID: &quot; &lt;&lt; bssEntry-&gt;dot11Ssid.ucSSID &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;RSSI: &quot; &lt;&lt; bssEntry-&gt;lRssi &lt;&lt; &quot; dBm&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;IE buffer offset: &quot; &lt;&lt; bssEntry-&gt;ulIeOffset &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;IE buffer size: &quot; &lt;&lt; bssEntry-&gt;ulIeSize &lt;&lt; std::endl;

	PBYTE ieBuffer = (PBYTE)bssEntry + bssEntry-&gt;ulIeOffset;
	for (DWORD j = 0; j &lt; bssEntry-&gt;ulIeSize; j++) {
		printf(&quot;%02X &quot;, ieBuffer[j]);
	}
	std::cout &lt;&lt; std::endl;
	std::cout &lt;&lt; std::endl;
}
WlanFreeMemory(pBssList);

huangapple
  • 本文由 发表于 2023年7月14日 07:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定