我在Linux上的C ++项目中使用libtins创建一个pcap转储。 我从代码中获得的pcap转储似乎无法被Wireshark,tcp转储或我用来尝试使用libtins读取代码的示例读取。
我需要我的程序生成可以被Wireshark读取的输出,以及可以通过项目中的代码读取它的方式读取的输出。
编辑:
相关代码:
bool packetHandler(const PDU &pdu) {
const IP &ip = pdu.rfind_pdu<IP>();
return true;
cout << ip.src_addr() << " -> " << ip.dst_addr() << endl;
return true;
}
int main(int argc, char** argv)
{
if(getuid() != 0)
{
printf("You must run this program as root!\n");
exit(1);
}
try
{
std::string pcap_path = "/tmp/test.pcap";
std::string device = "eth0";
printf("Filepath: %s\n", pcap_path.c_str());
PacketWriter writer(pcap_path, DataLinkType<EthernetII>());
std::vector<EthernetII> vec(1000, EthernetII(hwaddr(device)));
writer.write(vec.begin(), vec.end());
writer.write(vec[0]);
} catch(Tins::unknown_link_type e)
{
printf("ERROR:\t%s\n", e.what());
} catch(std::runtime_error e)
{
printf("ERROR:\t%s\n", e.what());
}
}
我也尝试过这段代码来读取pcap,但是它什么也没输出:
#include <tins/tins.h>
using namespace Tins;
using namespace std;
bool packetHandler(PDU &pdu)
{
// Find the IP layer
const IP &ip = pdu.rfind_pdu<IP>();
cout << ip.src_addr() << " -> " << ip.dst_addr() << endl;
return true;
}
int main() {
FileSniffer sniffer("/tmp/test.pcap");
sniffer.sniff_loop(packetHandler);
}
再次编辑..
正如您从Wireshark看到的那样,我得到的每个字段和数据的值都不正确,全为0。 https://i.imgur.com/wfCnaaA.png(很抱歉,我在此处没有10个声誉点,因此无法嵌入图片)。 我需要能够在Wireshark的正确字段中查看IP地址,数据等,但是我没有获得正确的数据。