mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.89k stars 374 forks source link

get timestamp with wrong value while reading from pcap file #480

Closed nicholaswbsr closed 2 years ago

nicholaswbsr commented 2 years ago

Hi,

I want to get the timestamp from pcap file, thus I can know when the package is captured. But the timestamp seconds always show up the current time. For example I open pcap file via Wireshark then I can see the package captured with seconds: 1654494730 which represent around June 6, 2022 13:52. But while I run the code I got 1655275668, represent my current local time: June 15, 2022.

Here is my code snap:

    FileSniffer sniffer("../inputs/example.pcap");
    int8_t counter = 0;
    PDU * some_pdu = sniffer.next_packet();
    Packet packet = *some_pdu;
    auto sec = packet.timestamp().seconds();
    std::cout << sec;
nicholaswbsr commented 2 years ago

I fingered out the reason myself.

Change to the this then fixed.

    Packet some_pdu = sniffer.next_packet();
    auto sec = some_pdu.timestamp().seconds();
    std::cout << "next_packet: " << sec << std::endl;

It means only class Packet has the wrapper for timestamp not class PDU. So we must take Packet reference directly not construct from PDU.