目前,我对网络很感兴趣,为了了解更多,我决定使用 PcapPlusPlus 库来捕获本地网络上的数据包。我的目标是了解数据包的结构、位置和不同标头(以太网、IP 等)的内容
我使用 cmake 和 vcpkg 在 C++20 中编译以管理我的依赖项,我在 Windows 上的 Visual Studio 2022 IDE 上开发。我能够编译和使用该库。但是,当我尝试从我的计算机检索不同的接口时,我得到一个空列表。
这是我用来列出接口的代码:
#include <iostream>
#include <PcapLiveDeviceList.h>
int main()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
if (devList.empty())
{
std::cout << "Empty device list" << std::endl;
return 1;
}
std::cout << "Network interfaces:" << std::endl;
for (std::vector<pcpp::PcapLiveDevice*>::const_iterator iter = devList.begin(); iter != devList.end(); iter++)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
}
return 0;
}
这是 CMakeLists:
cmake_minimum_required (VERSION 3.8)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project ("test_pcap")
file(GLOB SOURCES *.cpp)
message(${CMAKE_PREFIX_PATH})
add_definitions(-DWPCAP -DHAVE_REMOTE)
find_package(unofficial-pcapplusplus CONFIG REQUIRED)
add_executable (${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE
unofficial::pcapplusplus::pcappp
unofficial::pcapplusplus::packetpp
unofficial::pcapplusplus::commonpp
)
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
为了解决问题:
最后我运行了一个由 PcapPlusPlus (Arping) 提供的示例应用程序,它允许列出接口并且它有效。
我想我一定是缺少依赖项,但由于我没有错误,我不知道如何解决我的问题。