- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 pcap 进行一些简单的数据包捕获,因此我创建了一个句柄来通过 eth0 进行监听。我的问题是代码末尾附近的 pcap_loop(handle, 10, myCallback, NULL);
行。我正在尝试使用 pcap_loop
。预期的输出应该是:
eth0
Activated!
1
2
3
...
10
Done processing packets!
当前输出缺少增量:
eth0
Activated!
Done processing packets!
目前它只是直接跳到“完成数据包处理!”我不知道为什么。即使它没有进入回调,它仍应等待数据包,因为 ;count' 参数(请参阅 pcap_loop 的文档)设置为 10。
#include <iostream>
#include <pcap.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void myCallback(u_char *useless, const struct pcap_pkthdr* hdr, const u_char*packet){
static int count = 1;
std::cout <<count<<std::endl;
count ++;
}
int main(){
char errbuf[PCAP_ERRBUF_SIZE];
char * devName;
char* net;
char* mask;
const u_char*packet;
struct in_addr addr;
struct pcap_pkthdr hdr;
bpf_u_int32 netp;
bpf_u_int32 maskp;
pcap_if_t *devs;
pcap_findalldevs(&devs, errbuf);
devName = pcap_lookupdev(errbuf);
std::cout <<devName<<std::endl;
int success = pcap_lookupnet(devName, &netp, &maskp, errbuf);
if(success<0){
exit(EXIT_FAILURE);
}
pcap_freealldevs(devs);
//Create a handle
pcap_t *handle = pcap_create(devName, errbuf);
pcap_set_promisc(handle, 1);
pcap_can_set_rfmon(handle);
//Activate the handle
if(pcap_activate(handle)){
std::cout <<"Activated!"<<std::endl;
}
else{
exit(EXIT_FAILURE);
}
pcap_loop(handle, 10, myCallback, NULL);
std::cout <<"Done processing packets!"<<std::endl;
//close handle
pcap_close(handle);
}
最佳答案
pcap_findalldevs(&devs, errbuf);
该调用没有做任何有用的事情,因为除了释放它之外,您没有对 devs
做任何事情。 (您也不会检查它是成功还是失败。)您最好将其删除,除非您需要知道可以捕获的所有设备是什么。
pcap_can_set_rfmon(handle);
这一切都没有做任何有用的事情,因为您没有检查它的返回值。如果您在 Wi-Fi 设备上捕获,并且想在监控模式下捕获,则在创建后的句柄上调用 pcap_set_rfmon()
- 而不是 pcap_can_set_rfmon()
在激活 handle 之前。
//Activate the handle
if(pcap_activate(handle)){
std::cout <<"Activated!"<<std::endl;
}
else{
exit(EXIT_FAILURE);
}
引用 pcap_activate()
手册页:
RETURN VALUE
pcap_activate() returns 0 on success without warnings, PCAP_WARN-
ING_PROMISC_NOTSUP on success on a device that doesn't support promis-
cuous mode if promiscuous mode was requested, PCAP_WARNING on success
with any other warning, PCAP_ERROR_ACTIVATED if the handle has already
been activated, PCAP_ERROR_NO_SUCH_DEVICE if the capture source speci-
fied when the handle was created doesn't exist, PCAP_ERROR_PERM_DENIED
if the process doesn't have permission to open the capture source,
PCAP_ERROR_RFMON_NOTSUP if monitor mode was specified but the capture
source doesn't support monitor mode, PCAP_ERROR_IFACE_NOT_UP if the
capture source is not up, and PCAP_ERROR if another error occurred. If
PCAP_WARNING or PCAP_ERROR is returned, pcap_geterr() or pcap_perror()
may be called with p as an argument to fetch or display a message
describing the warning or error. If PCAP_WARNING_PROMISC_NOTSUP,
PCAP_ERROR_NO_SUCH_DEVICE, or PCAP_ERROR_PERM_DENIED is returned,
pcap_geterr() or pcap_perror() may be called with p as an argument to
fetch or display an message giving additional details about the problem
that might be useful for debugging the problem if it's unexpected.
这意味着上面的代码是 100% 错误的 - 如果 pcap_activate()
返回一个非零值,它可能失败,如果它返回 0,它成功了。
如果返回值为负数,则为错误值,失败。如果它非零但为正,则为警告值;它已成功,但是,例如,它可能没有打开混杂模式,因为操作系统或设备可能不允许设置混杂模式。
所以你想要的是:
//Activate the handle
int status;
status = pcap_activate(handle);
if(status >= 0){
if(status == PCAP_WARNING){
// warning
std:cout << "Activated, with warning: " << pcap_geterror(handle) << std::endl;
}
else if (status != 0){
// warning
std:cout << "Activated, with warning: " << pcap_statustostr(status) << std::endl;
}
else{
// no warning
std::cout <<"Activated!"<<std::endl;
}
}
else{
if(status == PCAP_ERROR){
std:cout << "Failed to activate: " << pcap_geterror(handle) << std::endl;
}
else{
std:cout << "Failed to activate: " << pcap_statustostr(status) << std::endl;
}
exit(EXIT_FAILURE);
}
关于c++ - 'pcap_loop' 没有记录数据包,甚至没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095748/
使用 Python 的 rtmplib 绑定(bind)并遇到一些问题。 首先, 我有这样的东西: import librtmp conn = librtmp.RTMP(...) conn.con
基本上,我是在查看 Motorstorm 排行榜时在 PS3 上窃听数据包。排行榜以 XML 格式发送到我的 ps3,但只有在我获得授权后。那么有人可以告诉我这三个数据包之间发生了什么,以及我如何在浏
我正在努力了解 TCP,但解析大量 RFC 并没有帮助。我相信我了解连接和关闭握手,但我似乎无法找到任何总结实际数据流的内容。 在连接和关闭握手之间 TCP 数据包看起来像什么? (特别是标题) 最佳
我正在尝试通过 RCON 端口与我的 Minecraft 服务器通信。 虽然我不知道如何使用套接字和流的东西。四处寻找,我发现他们都有一些共同点。套接字、输入流和输出流。 我在我的代码中试过了,但返回
我正在 UDP 之上设计一个简单的协议(protocol),现在我意识到其他人可以将数据包发送到我正在监听的端口。这样的数据包对于我的应用程序来说显然是不正确的(我现在不担心安全问题) 是否有过滤这些
我目前有一个具有可自定义滴答率的游戏服务器,但在本示例中,我们建议服务器每秒仅滴答一次或 1hz。我想知道如果客户端发送速率比服务器快,因为我当前的设置似乎不起作用,那么处理传入数据包的最佳方法是什么
我无法理解网络字节顺序以及通过 UDP 发送和接收数据的顺序。我正在使用 C#。 我有一个结构保持: message.start_id = 0x7777CCCC; message.me
我正在为 USB 设备编写代码。假设 USB 主机开始控制读取传输以从设备读取一些数据,并且请求的数据量(设置数据包中的 wLength)是端点 0 最大数据包大小的倍数。那么在主机接收到所有数据后(
我有一台 Windows PC、Marvell 交换机、Netgear 交换机和一台 Ubuntu 机器连接在一起(通过 Netgear 交换机)。 我最近从 Windows PC 向 Marvell
在查看数据包字节码时,您将如何识别 dns 数据包。 IP header 的协议(protocol)字段会告诉后面有一个 UDP 帧,但是在 UDP 帧内没有协议(protocol)字段来指定接下来会
我有一个通过 udf 的 802.11 (wifi) 上各种类型的流量的 pcap。由于 MTU,udp(或更准确地说是 IP)对 wifi 数据包进行分段。我目前正在使用 SharpPcap 读取并
我正在开发的 Core Audio 应用程序上有此崩溃日志。我目前正在调试它,所以我的问题不是关于崩溃本身,而是关于 的含义“k”包 . 这是什么意思 ? 我已阅读 this , 和 this (关于
我在一台 VM Ubuntu 16.04 机器上的 100 个多播组上生成 UDP 数据包,并在另一台 VM Ubuntu 16.04 机器上订阅这些组。两者都在由 Hyper-V 管理器运行的 HP
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个回答) 6年前关闭。 我正在尝试创建一个简单的
我正在寻找使用 Java 来欺骗 UDP 数据包。是否有任何好的 Java 库可以让您创建自己的 RAW SOCKETS? 最佳答案 我会使用包装 libpcap 的 Java API . libpc
我在基于 Tyrus 的客户端和 tomcat Web 服务器之间使用没有压缩的 websocket。我在 tomcat 端看到消息传入和传出我的套接字,但如果我设置一个wireshark来观察它们传
我的应用程序在模拟器中运行时无法接收 UDP 数据包。 UDP 数据包由“localhost”上的以下 java 程序通过端口 49999 发送。 DatagramSocket clien
我正在开发一个 Google Glass 应用程序,它需要在工作线程中监听 UDP 数据包(与发送 UDP 数据包的现有系统集成)。我之前发布了一个问题(请参阅 here )并收到了一个答案,其中提供
我正在从客户端向服务器发送两个数据包。我遇到的问题是,在服务器上读取的数据使两个字符串对于发送的最长字符串具有相同的长度。例如: 如果字符串 1 为:1234 字符串 2 为:abcdefghi 服务
我知道这是不好的做法,但是可以执行以下操作吗? Send packet1 to UDP port 1 port 1 receives packet1 and sends it to port 2 po
我是一名优秀的程序员,十分优秀!