我已经编写了一个pcap
使用pcap_open_live()
并逐步应用过滤器的程序(即重新编译pcap
过滤器并在初始 pcap_loop 之后再次设置过滤器),我想在pcap
我保存的一些文件上对其进行测试Wireshark
。
但是,当我运行程序时,我什至无法打印出我的数据包,除非我向 ; 提供一个空过滤器pcap_compile_filter
。
这只是lpcap
在保存的文件上使用的功能,还是我做错了什么?
这是一段代码供仔细阅读:
int main(int argc, char **argv)
{
char *dev = NULL; /* capture device name */
char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
pcap_t *handle; /* packet capture handle */
char filter_exp[] = "ip"; /* filter expression [3] */
struct bpf_program fp; /* compiled filter program (expression) */
bpf_u_int32 mask; /* subnet mask */
bpf_u_int32 net; /* ip */
int num_packets = -1; /* number of packets to capture -1 => capture forever! */
printf("Filter expression: %s\n", filter_exp);
// open capture device
handle = pcap_open_offline("heartbeats2", errbuf);
if (handle == NULL) {
fprintf(stderr, "pcap_open_offline failed: %s\n", errbuf);
exit(EXIT_FAILURE);
}
/* make sure we're capturing on an Ethernet device*/
if (pcap_datalink(handle) != DLT_EN10MB) {
fprintf(stderr, "%s is not an Ethernet\n", dev);
exit(EXIT_FAILURE);
}
/* compile the filter expression */
if (pcap_compile(handle, &fp, filter_exp, 0, 0) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n",
filter_exp, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
/* apply the compiled filter */
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n",
filter_exp, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
pcap_loop(handle, -1, gotPacket, NULL);
pcap_freecode(&fp);
pcap_close(handle);
printf("\nCapture complete.\n");
return(0);
}
got packet 函数只是打印出数据包的有效载荷;输出只是:
Filter expression: ip
Capture complete.