【发布时间】:2020-07-15 06:51:15
【问题描述】:
你知道如何用winpcap dll创建空文件pcap吗?我在程序内存中缓冲过滤后的数据包,并希望在用户单击导出到 .pcap 文件时保存。
但是当使用 pcap_open_offline(const char *fname, char *errbuf) 时,只有当文件存在时才能打开文件。我之前尝试过 fopen 和其他函数来创建文件(也以二进制模式)但没有成功。
那么如何通过这种方式获取 pcap_dump_open(pcap_t *p, const char *fname) 的 pcap_t 句柄指针?
更新: 我尝试使用此代码
fileHandle = pcap_open_offline(pcap_file_path.c_str(), errbuf);
if (errbuf == nullptr) {
fprintf(stderr, "\nUnable to open the file %s.\n", pcap_file_path.c_str());
return 1;
}
if (fileHandle == nullptr) {
fprintf(stderr, "\nError to open file\n");//HERE IT FAILS
return 1;
}
dumpfile = pcap_dump_open(fileHandle, pcap_file_path.c_str());
if (dumpfile == NULL)
{
fprintf(stderr, "\nError opening output file\n");
return 1;
}
解决方案:(Creating a pcap file)
/*create fake handle*/
fileHandle = pcap_open_dead(DLT_EN10MB, 65535);
if (fileHandle == nullptr) {
fprintf(stderr, "\nError to open file\n");
return 1;
}
/* Open the dump file */
dumpfile = pcap_dump_open(fileHandle, file_path.c_str());
if (dumpfile == NULL)
{
fprintf(stderr, "\nError opening output file\n");
return 1;
}
【问题讨论】:
-
我不知道任何 pcap 功能,但也许您必须创建一个所需大小的文件,即使用 fopen/ofstream/etc 创建一个文件并写入一些字节,直到文件足够大。
-
pcap_t 句柄与接口或现有 保存文件相关联。因此,在您的情况下,您必须使用 pcap_open 或 pcap_create/pcap_activate 将您未来的转储与接口相关联。
-
omuffat:但是在我没有打开任何设备的情况下,如何使用pcap_open、pcap_create呢?如果离线,我想保存到文件中。当应用 pcap_open_offline(const char *fname, char *errbuf) 并且 fname 中指定的文件不存在时,它返回 NULL。