- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我的系统可以插入目标计算机上任意数量的以太网端口。我的目标是遍历每个设备,收集大约 20 个数据包,或者如果没有数据则跳过它,直到找到我要查找的数据,然后选择该设备作为我的“捕获设备”。看起来很简单。然而,我很快了解到,如果没有数据进入,libpcap 不会简单地超时。因此,我尝试使用 pcap_setnonblock 将我的捕获设备设置为非阻塞模式。但是,这会导致我阅读的目标端口完全崩溃。这是我的代码。愿意接受有关可能发生的事情的建议,或者甚至是更好的方法。谢谢
附言。 Type1 Type2 变量名不要敲,是用来混淆的。
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_t *fp;
struct pcap_pkthdr *header;
const u_char *pkt_data;
char errbuf[PCAP_ERRBUF_SIZE];
const int FIND_DEVICE_PACKET_LIMIT = 20;
#ifdef WIN32
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
#else
if (pcap_findalldevs(&alldevs, errbuf) == -1)
#endif
{
cerr << "Error in pcap_findalldevs_ex: " << errbuf << endl;
return;
}
// For each device, capture until timeout
// or until a matching packet is found.
for(d=alldevs; d; d=d->next)
{
if ((fp = pcap_open_live(d->name,
1514 /*snaplen*/,
1 /*flags, 1=promiscuous, 0=not promiscuous*/,
20 /*read timeout*/,
errbuf)
) == NULL)
{
cerr << endl << "Unable to open the adapter." << endl;
continue;
}
int res = 0;
int packetCounter = 0;
pcap_setnonblock(fp, true, errbuf);
// Capture at most FIND_DEVICE_PACKET_LIMIT packets
// to determine whether scanner is sending packets
// or no scanner found.
while ((res = pcap_next_ex(fp, &header, &pkt_data)) >=0) {
struct iphdr *iph = (struct iphdr *)(pkt_data + sizeof(struct ethhdr));
struct udphdr *udph = (struct udphdr*)(pkt_data + (iph->ihl*4) + sizeof(struct ethhdr));
u_int destPort = ntohs(udph->dest);
if (destPort==TYPE1_DATA_PORT || destPort==TYPE1_GPS_PORT) {
detectedScanners->type1 = true;
} else if (destPort==TYPE2_DATA_PORT || destPort==TYPE2_STATUS_PORT || destPort==TYPE2_NMEA_PORT) {
detectedScanners->type2 = true;
}
if (++packetCounter > FIND_DEVICE_PACKET_LIMIT) {
break;
}
}
if (detectedScanners->type1==true || detectedScanners->type2==true) {
*interface = d->name;
break;
}
}
最佳答案
在非阻塞模式下,如果没有数据包可读取,pcap_next_ex()
将返回 0,并且 NOT 不会返回任何数据包信息,因此,如果它返回 0,你应该 不要查看header
或packet_data
指向的任何内容。
即,做
while ((res = pcap_next_ex(fp, &header, &pkt_data)) >=0) {
if (res != 0) {
struct iphdr *iph = (struct iphdr *)(pkt_data + sizeof(struct ethhdr));
struct udphdr *udph = (struct udphdr*)(pkt_data + (iph->ihl*4) + sizeof(struct ethhdr));
u_int destPort = ntohs(udph->dest);
if (destPort==TYPE1_DATA_PORT || destPort==TYPE1_GPS_PORT) {
detectedScanners->type1 = true;
} else if (destPort==TYPE2_DATA_PORT || destPort==TYPE2_STATUS_PORT || destPort==TYPE2_NMEA_PORT) {
detectedScanners->type2 = true;
}
if (++packetCounter > FIND_DEVICE_PACKET_LIMIT) {
break;
}
}
}
但是请注意,您的程序将在该循环中不断旋转,消耗 CPU,永远。这意味着您永远不会错过第一台设备。
您可能真正想做的是:
pcap_t *
放入一个数组中,并使它们进入非阻塞模式;pcap_get_selectable_fd()
,在 Windows 上调用pcap_getevent()
,并将该调用的结果保存到一个数组中(你可以有并行数组,或者一个结构数组,其中包含pcap_t *
”和int
或HANDLE
作为成员);select()
中的所有poll()
上使用int
或pcap_get_selectable_fd()
,在 Windows 上的WaitForMultipleObjects()
中的所有HANDLE
上使用pcap_getevent()
和,当select()
/poll()
/WaitForMultipleObjects()
返回时,尝试使用pcap_t *
从每个的pcap_next_ex()
读取一个数据包,如果你得到一个数据包就处理它。<这样,您就可以并行扫描所有设备,而不会占用 CPU。
关于c++ - 查询接口(interface)以使用 libpcap 查找设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990299/
我的程序像这样使用 libpcap: while pcaket = pcap_next() { ... (modify the pcaket and do checksum) ... pcap_sen
我正在为一个流量非常大的网络编写监控程序(高清视频通过网络流式传输)。大多数数据包都非常大,我只想查看 header (仅限 IP 和 UDP/TCP)。当然,我想避免复制整个数据的开销。 libpc
我对原始套接字和 libcap 的使用感到有点困惑。任何人都可以简单地指出使用两者的优点。我读了几个链接,但是,它让我感到困惑。 最佳答案 libpcap 在不同的操作系统上使用不同的机制。在 Lin
我在项目中看到了这种代码: while (1) { l_numPkts = pcap_next_ex( m_pcapHandle, &header, &pkt_data); //do some
我想使用 libpcap 使用 gcc 选项 -m32 将 .c 文件编译为 32 位可执行文件机器是linux 64位 fedora 16 但是,我收到以下错误 [root@fdf source]#
我只想从我的系统中嗅探传出的“TCP-ACK”数据包。因此,我在 lib-pcap 程序中将过滤器表达式设置为: char filter_exp[] = "src host 172.16.0.1 an
我正在尝试使用交叉编译器 arm-linux-gcc 编译 libpcap。当我运行“make”时出现错误: ./pcap-linux.c:254:14: conflicting types for
我正在编写相当简单的pcap“实时”捕获引擎,但是pcap_dispatch的数据包处理回调实现应花费相对较长的时间进行处理。 pcap是否在单独的线程中运行每个“pcap_handler”回调?如果
我正在使用 lib-pcap 库来安装/卸载过滤器。 为了安装 pcap 过滤器,我使用pcap_compile 后接 pcap_setfilter,效果很好。 对于卸载过滤器,我使用 pcap_fr
pcap_next 是否显示已发送的数据包? 例如:如果我在 libpcap 监听数据包时 ping google.com,然后运行 pcap_next 两次,返回的值将都是数据包,还是其中一个为
我正在尝试使用 libpcap 打印确认号。我确实知道我会得到与我在 pcap 文件中看到的不同的确认号。我的问题是,在 pcap 文件中,编号为 10、11 和 12 的数据包具有不同的 ack 编
我是 libpcap 的新手。 我正在使用这个库来捕获数据包,下面是我编写的捕获数据包的代码。 我正在窃听的接口(interface)总是充满arp数据包,因此总是有数据包到达该接口(interfac
使用 pcap,我声明了 rtp 结构,当我尝试指向数据包的这个区域时,我发现根据我的声明,它以不同的方式工作。 我写的是: struct udphdr *udp; struct rtphdr *rt
我想拦截来自用户空间的网络流量,所以我使用了 libpcap API 来捕获 tcp/ip 数据包。现在我只是在注册的回调中捕获有关数据包 header 和有效负载的信息。我如何使用此回调来阻止流量或
有人可以给我指出一个好的起点来了解 libpcap 如何从网络驱动程序获取数据包吗?我打算用快速实现替换接收函数。 最佳答案 “接收函数”是: 标准驱动程序接收函数,与用于接收数据包并将其交给常规网络
我用库 libpcap(Ubuntu, c) 捕获了信标帧 我将 wlan 模式更改为通过以下函数调用进行监控和捕获 // 3000 is large enough number for test p
我正在使用 libpcap 捕获特定端口上的所有数据包。但是我需要分开并清楚地知道我的设备正在发送哪些数据包,以及它正在接收哪些数据包。实际上,我使用 pcap_close() 来捕获数据包,而且效果
我正在尝试用 C 解析一个 pcap 文件。我不想使用 libpcap。但出于某种原因,我无法做到。你知道我该怎么做吗?这是我的尝试: fseek(f,24,0); while(countcaplen
我遇到了以下情况:我用 pcap_open_live() 打开我的网络接口(interface)之一。然后我正在为 pcap 编译一个过滤器,只捕获指定的以太网类型(ether proto 0x123
我正在尝试编写一个 c++ 应用程序 (linux) 来捕获无线数据包以及相关的信号强度(以 dBm 为单位)。捕获部分很简单,但问题是我找不到任何关于如何获取每个数据包信号强度的文档。 它是标题的一
我是一名优秀的程序员,十分优秀!