2121#include < iostream>
2222#include < ctime>
2323#include < fstream>
24+ #include < chrono>
2425#include " unistd.h"
2526#include " Packetcapture.h"
2627#include " Packetcapture_dp_ingress.h"
2728#include " Packetcapture_dp_egress.h"
2829#define ON_T 0
2930#define OFF_T 1
31+ #define BILLION 1000000000
32+ #define MILLION 1000000
33+ #define ONE_THOUSAND 1000
3034
3135
3236typedef int bpf_int32;
@@ -52,6 +56,14 @@ struct pcap_pkthdr {
5256Packetcapture::Packetcapture (const std::string name, const PacketcaptureJsonObject &conf)
5357 : TransparentCube(conf.getBase(), { packetcapture_code_ingress }, { packetcapture_code_egress }),
5458 PacketcaptureBase (name) {
59+
60+ gettimeofday (&ts, NULL ); // getting actual time
61+ std::uint64_t uptime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now ().time_since_epoch ()).count ();
62+ time_t sec_off = uptime / BILLION;
63+ ts.tv_sec -= sec_off;
64+ uptime -= sec_off * BILLION;
65+ ts.tv_usec -= uptime;
66+
5567 logger ()->info (" Creating Packetcapture instance" );
5668 setCapture (conf.getCapture ());
5769 setAnomimize (conf.getAnomimize ());
@@ -79,7 +91,6 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
7991
8092 PacketJsonObject pj;
8193 auto p = std::shared_ptr<Packet>(new Packet (*this , pj));
82- struct timeval tp;
8394 std::streamsize len;
8495
8596 if (dt == " " ) {
@@ -105,7 +116,17 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
105116 myFile.write (reinterpret_cast <const char *>(pcap_header), sizeof (*pcap_header));
106117 writeHeader = false ;
107118 }
108- gettimeofday (&tp, NULL );
119+ struct timeval tp;
120+ tp.tv_sec = ts.tv_sec ;
121+ tp.tv_usec = ts.tv_usec ;
122+ time_t sec_off = temp_offset/BILLION; /* from nanoseconds to seconds */
123+ temp_offset -= sec_off*BILLION;
124+ tp.tv_usec += temp_offset/ONE_THOUSAND; /* from nanoseconds to microseconds */
125+ tp.tv_sec += sec_off;
126+ sec_off = tp.tv_usec /MILLION; /* from microseconds to seconds */
127+ tp.tv_usec -= sec_off*MILLION;
128+ tp.tv_sec += sec_off;
129+
109130 p->setTimestampSeconds ((uint32_t ) tp.tv_sec );
110131 p->setTimestampMicroseconds ((uint32_t ) tp.tv_usec );
111132 p->setPacketlen ((uint32_t ) packet.size ());
@@ -140,13 +161,15 @@ void Packetcapture::packet_in(polycube::service::Direction direction,
140161
141162 switch (direction) {
142163 case polycube::service::Direction::INGRESS:
164+ temp_offset = get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::INGRESS).get (0x0 );
143165 if (getNetworkmode () == true ) {
144166 addPacket (packet); /* store the packet in the FIFO queue*/
145167 } else {
146168 writeDump (packet);
147169 }
148170 break ;
149171 case polycube::service::Direction::EGRESS:
172+ temp_offset = get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::EGRESS).get (0x0 );
150173 if (getNetworkmode () == true ) {
151174 addPacket (packet); /* store the packet in the FIFO queue*/
152175 } else {
0 commit comments