为什么我的程序在打开网络设备时遇到问题
Why is my program having problems opening a Network Device
我看了一下libtins和示例,我找到了ARP监视器示例。 示例的代码如下所示:
#include <tins/tins.h>
#include <map>
#include <iostream>
#include <functional>
using std::cout;
using std::endl;
using std::map;
using std::bind;
using namespace Tins;
class arp_monitor {
public:
void run(Sniffer& sniffer);
private:
bool callback(const PDU& pdu);
map<IPv4Address, HWAddress<6>> addresses;
};
void arp_monitor::run(Sniffer& sniffer) {
sniffer.sniff_loop(
bind(
&arp_monitor::callback,
this,
std::placeholders::_1
)
);
}
bool arp_monitor::callback(const PDU& pdu) {
// Retrieve the ARP layer
const ARP& arp = pdu.rfind_pdu<ARP>();
// Is it an ARP reply?
if (arp.opcode() == ARP::REPLY) {
// Let's check if there's already an entry for this address
auto iter = addresses.find(arp.sender_ip_addr());
if (iter == addresses.end()) {
// We haven't seen this address. Save it.
addresses.insert({ arp.sender_ip_addr(), arp.sender_hw_addr()});
cout << "[INFO] " << arp.sender_ip_addr() << " is at "
<< arp.sender_hw_addr() << std::endl;
}
else {
// We've seen this address. If it's not the same HW address, inform it
if (arp.sender_hw_addr() != iter->second) {
cout << "[WARNING] " << arp.sender_ip_addr() << " is at "
<< iter->second << " but also at " << arp.sender_hw_addr()
<< endl;
}
}
}
return true;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
cout << "Usage: " <<* argv << " <interface>" << endl;
return 1;
}
arp_monitor monitor;
// Sniffer configuration
SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter("arp");
try {
// Sniff on the provided interface in promiscuous mode
Sniffer sniffer(argv[1], config);
// Only capture arp packets
monitor.run(sniffer);
}
catch (std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
我在这里运行了这段代码:
myprogram.exe eth0
结果是:
错误:打开适配器时出错:系统找不到给定的设备。(二十(
以上词语的定义:
eth0:我的网络设备
libtins:高级、多平台C++网络数据包嗅探和制作库
ARP:地址解析协议
我只在Windows上一个名为"Shadow"的用户上运行它
用户目录的图像
根据 te OP 上传的图像,我只能得出结论,我对他在 Windows 上运行该程序的看法是正确的,所以我只从 libtins 的文档中复制:
为了在Windows上捕获数据包,您可以首先列出所有网络接口。您可以使用 NetworkInterface 类轻松完成此操作:
// First fetch all network interfaces
vector<NetworkInterface> interfaces = NetworkInterface::all();
// Now iterate them
for (const NetworkInterface& iface : interfaces) {
// First print the name (GUID)
cout << "Interface name: " << iface.name();
// Now print the friendly name, a wstring that will contain something like
// "Local Area Connection 2"
wcout << " (" << iface.friendly_name() << ")" << endl;
}
该小代码片段应提供如下所示的输出:
Interface name: {6527cc7d-c647-4986-ac10-7784dc1f2439} (Local Area Connection 1)
Interface name: {309d733f-79bb-41ef-aaec-8a7b83d2adcf} (Local Area Connection 2)
Interface name: {55ab969f-80df-4d51-8130-291d54a752a3} (Local Area Connection 3)
这可能足以让您识别出哪个是要使用的界面。您还可以求助于获取默认接口,这很可能是您想要使用的接口,或者显示每个接口的 IP 地址,直到您识别它们:
// Get the default interface (where the default gateway route is)
NetworkInterface iface = NetworkInterface::default_interface();
// Print the name and the IP address
cout << "Default interface: " << iface.name()
<< " (" << iface.addresses().ip_addr() << ")" << endl;
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 我的神经网络不起作用 [XOR 问题]
- 编译包含字符串的代码时遇到问题
- 指向设备对象成员的指针
- 为什么我的程序在打开网络设备时遇到问题
- 媒体基金会可以允许多个客户端同时访问单个网络摄像头设备吗?
- 安卓设备上的原生C++ WiFi P2P 网络是否可行?
- statvfs 可以在某些网络设备上阻止吗?如何处理这种情况?
- 在c++(WLANAPI)中获取Windows wifi托管网络设备的ip地址
- 如何获取本地网络上所有设备的名称和 ip
- 连接到网络的所有设备的列表
- 查找网络中的所有设备
- 如何知道何时设备从网络中删除使用Bonjour C库
- 是否可以使用c++ /OpenFrameworks将图像(或图像序列)作为硬件设备(网络摄像头)传递给Mac OSX ?
- 如何从Android设备检测本地网络上未知计算机上的c++程序
- 如何通过本地 LAN 使用网络别名自动访问嵌入式设备的 Web 服务器?
- 在C++中检测本地网络上的客户端-服务器连接设备
- 我在神经网络中实现反向传播时遇到了问题
- 哪种语言和库更适合高性能网络设备轮询服务器 (SNMP)
- 在苹果设备上运行的 Websocket 的网络性能不佳