- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用 libpcap 函数 pcap_next() 从其他主机捕获一些 tcp 数据包我检查了捕获的数据包的字节并注意到数据包的以太网和 ip header 失真,乱七八糟,有很多 0但 TCP header 没问题
这可能是什么原因?
代码:
pcap_t* create_pcap_handler()
{
pcap_t *handle; /* Session handle */
char *dev; /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
struct bpf_program fp; /* The compiled filter */
char filter_exp[] = "port 32000"; /* The filter expression */
bpf_u_int32 mask; /* Our netmask */
bpf_u_int32 net; /* Our IP subnet*/
/* Define the device */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
exit(2);
}
/* Find the properties for the device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
struct in_addr tmp;
tmp.s_addr=net;
char IPdotdec[20];
inet_ntop(AF_INET, (void *)&tmp, IPdotdec, 16);
printf("net is %s\n", IPdotdec);
tmp.s_addr=mask;
inet_ntop(AF_INET, (void *)&tmp, IPdotdec, 16);
printf("mask is %s\n", IPdotdec);
printf("dev is %s\n",dev);
handle = pcap_open_live(dev, BUFSIZ, 0, 0, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(2) ;
}
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, mask) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
exit(2);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
exit(2);
}
return handle;
}
和主要功能
int main()
{
pcap_t * pcap_handler=create_pcap_handler();
struct pcap_pkthdr pcap_header; /* The header that pcap gives us */
const u_char *pcap_packet; /* The actual packet */
pcap_packet = pcap_next(pcap_handler, &pcap_header);
if(pcap_packet !=NULL)
printf("capture one packet with length of %d\n", pcap_header.len);
pcap_close(pcap_handler);
return 0;
}
最佳答案
pcap_packet = pcap_next(pcap_handler, &pcap_header);
if(pcap_packet !=NULL)
printf("capture one packet with length of %d\n", pcap_header.len);
pcap_close(pcap_handler);
parse_pkt(pcap_packet,pcap_header.len);
那是行不通的。
当您关闭 pcap_handler
时,无法保证调用 pcap_next()
或 pcap_next_ex()
返回的任何指针 pcap_handler
将继续有效。
尝试
pcap_packet = pcap_next(pcap_handler, &pcap_header);
if(pcap_packet !=NULL)
printf("capture one packet with length of %d\n", pcap_header.len);
parse_pkt(pcap_packet,pcap_header.len);
pcap_close(pcap_handler);
相反。
关于c - 为什么 libpcap 函数捕获的数据包的以太网和 ip header 失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15436969/
在我们的服务出现一些预期的增长之后,突然间一些更新花费了非常长的时间,这些过去非常快,直到表达到大约 2MM 记录,现在它们每个需要大约 40-60 秒。 update table1 set fiel
我在服务中实现了一个传感器事件监听器,只要采样周期和最大报告延迟低于 1 秒,该监听器就可以正常工作,但一旦我将采样周期增加到超过 1 秒,传感器就根本不会更新。 我希望采样周期为 10 秒(可能是
我使用 Tkinter GUI 来启动测量和分析过程,基本上只需单击一个按钮即可开始。由于这些测量可能需要一段时间,我尝试添加一个进度条,即这个: http://tkinter.unpythonic.
我正在尝试使用套接字发送数据包,但出现错误。 invalid conversion from ‘omnetpp::cPacket*’ to ‘inet::Packet*’ [-fpermissive]
我刚刚发现 String#split 有以下奇怪的行为: "a\tb c\nd".split => ["a", "b", "c", "d"] "a\tb c\nd".split(' ') => ["a
您好,我正在尝试 ClojureScript,我正在使用 Klipse作为我的 REPL 差不多。这可能不是它的预期用途,但因为我没有做任何太复杂的事情,所以现在没问题。 我遇到的一个问题是尝试设置计
根据下面的数据,ClockKit 会生成一次 future 的 CLKComplicationTimelineEntry 项,但对于过去的时间点,会进行 24 次调用!这是为什么? 更多详情: 我注意
我有一个 MySQL 表,这个表有一个名为 datetime_utc 的 DATETIME 列。如您所料,它是 UTC 日期和时间。在我的 Bookshelf 模型中,我定义了一个虚拟 getter,
大家好,我是二哥呀! 昨天,一位球友问我能不能给他解释一下 @SpringBootApplication 注解是什么意思,还有 Spring Boot 的运行原理,于是我就带着他扒拉了一下这个注解的源
我是一名优秀的程序员,十分优秀!