用 libpcap 合并两个 pcap 文件 [英] Merging two pcap files with libpcap

查看:194
本文介绍了用 libpcap 合并两个 pcap 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道如何读取 pcap 文件并获取其中的数据包.但是如何将数据包写入新的 pcap 文件?我需要这个来将两个 pcap 文件合并为一个.

I already know how to read a pcap file and get the packets it have.B ut how can I write the packets into a new pcap file? I need this to merge two pcap files into one.

推荐答案

根据我的评论,libpcap/WinPcap 是一个库,而不是一个程序,因此要使用 libpcap/WinPcap 合并捕获文件,您必须编写您自己的代码进行合并,使用 libpcap/WinPcap 读取输入文件并写入输出文件.

As per my comment, libpcap/WinPcap is a library, not a program, so to use libpcap/WinPcap to merge capture files, you'd have to write your own code to do the merging, using libpcap/WinPcap to read the input files and write the output files.

您可以使用现有工具(例如 tracemerge 或 Wireshark 的 mergecap)来合并捕获.

You could use an existing tool, such as tracemerge or Wireshark's mergecap, to merge the captures.

假设目标是按时间戳合并两个文件的数据包,那么,如果您想编写自己的代码,您应该:

Assuming the goal is to merge two files' packets by time stamp, then, if you wanted to write your own code, you'd:

  • 尝试打开这两个文件,如果不能打开则失败;
  • 如果这两个文件具有不同的链路层标头类型或快照长度,则失败(您必须编写一个 pcap-ng 文件来处理该问题,而 libpcap/WinPcap 尚不支持);
  • 如果文件具有相同的链路层标头类型和快照长度,则使用 pcap_t 之一打开输出文件(哪个无关紧要;所有 pcap_t 是告诉 pcap_dump_open() 使用什么链路层头类型和快照长度);
  • attempt to open the two files, and fail if you can't;
  • if the two files have different link-layer header types or snapshot lengths, fail (you'd have to write a pcap-ng file to handle that, and libpcap/WinPcap don't support that yet);
  • if the files have the same link-layer header types and snapshot lengths, open an output file using one of the pcap_ts (it doesn't matter which one; all the pcap_t does is tell pcap_dump_open() what link-layer header type and snapshot length to use);

并有一个循环:

  • 如果没有从第一个文件中读取数据包,并且第一个文件仍然打开,则从中读取一个数据包 - 如果得到 EOF,则关闭第一个文件;
  • 如果没有从第二个文件中读取数据包,而第二个文件仍处于打开状态,则从中读取一个数据包 - 如果获得 EOF,则关闭第二个文件;
  • 如果你有两个数据包,写出一个带有旧时间戳的数据包,并将该数据包标记为不再存在,这样你就可以从它来自的文件中读取另一个数据包;
  • 如果您只有一个数据包,请将其写出并标记为不再存在,这样您就可以从它来自的文件中读取另一个数据包;
  • 如果你没有数据包,你就完成了——退出循环;

然后,当您退出循环时,关闭转储文件.到那时,你就完成了.

and then, when you exit the loop, close the dump file. At that point, you're done.

这篇关于用 libpcap 合并两个 pcap 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆