2929#define ON_T 0
3030#define OFF_T 1
3131#define BILLION 1000000000
32- #define MILLION 1000000
33- #define ONE_THOUSAND 1000
34-
3532
3633typedef int bpf_int32;
3734typedef u_int bpf_u_int32;
@@ -70,16 +67,22 @@ Packetcapture::Packetcapture(const std::string name, const PacketcaptureJsonObje
7067 * 'timeval struct ts' rapresents the system start time in epoch calculated as
7168 * actual time - system uptime
7269 *
73- * See line 138 of this file for more details
70+ * See line 141 or 307 of this file for more details
7471 */
7572
7673
77- gettimeofday (&ts, NULL ); // getting actual time
78- std::uint64_t uptime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now ().time_since_epoch ()).count ();
79- time_t sec_off = uptime / BILLION;
80- ts.tv_sec -= sec_off;
81- uptime -= sec_off * BILLION;
82- ts.tv_usec -= uptime;
74+ timeP = std::chrono::high_resolution_clock::now (); // getting actual time
75+
76+ std::chrono::nanoseconds uptime (0u );
77+ double uptime_seconds;
78+ if (std::ifstream (" /proc/uptime" , std::ios::in) >> uptime_seconds)
79+ {
80+ uptime = std::chrono::nanoseconds (
81+ static_cast <unsigned long long >(uptime_seconds*BILLION)
82+ );
83+ }
84+
85+ timeP -= uptime;
8386
8487 logger ()->info (" Creating Packetcapture instance" );
8588 setCapture (conf.getCapture ());
@@ -136,20 +139,13 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
136139
137140 /*
138141 * Here the packet capture time offset must be added to the system boot time stored in epoch format.
139- * See line 61 of this file to see system boot time stored in epoch algorithm
142+ * See line 58 of this file to see system boot time stored in epoch algorithm
140143 */
141144
142145 struct timeval tp;
143- tp.tv_sec = ts.tv_sec ;
144- tp.tv_usec = ts.tv_usec ;
145- time_t sec_off = temp_offset/BILLION; /* from nanoseconds to seconds */
146- temp_offset -= sec_off*BILLION;
147- tp.tv_usec += temp_offset/ONE_THOUSAND; /* from nanoseconds to microseconds */
148- tp.tv_sec += sec_off;
149- sec_off = tp.tv_usec /MILLION; /* from microseconds to seconds */
150- tp.tv_usec -= sec_off*MILLION;
151- tp.tv_sec += sec_off;
152-
146+ std::chrono::system_clock::duration tp_dur = (timeP + temp_offset).time_since_epoch ();
147+ to_timeval (tp_dur, tp);
148+
153149 p->setTimestampSeconds ((uint32_t ) tp.tv_sec );
154150 p->setTimestampMicroseconds ((uint32_t ) tp.tv_usec );
155151 p->setPacketlen ((uint32_t ) packet.size ());
@@ -181,18 +177,18 @@ void Packetcapture::packet_in(polycube::service::Direction direction,
181177 const std::vector<uint8_t > &packet) {
182178
183179 Tins::EthernetII pkt (&packet[0 ], packet.size ());
184-
180+
185181 switch (direction) {
186182 case polycube::service::Direction::INGRESS:
187- temp_offset = get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::INGRESS).get (0x0 );
183+ temp_offset = std::chrono::nanoseconds ( static_cast < unsigned long long >( get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::INGRESS).get (0x0 )));
188184 if (getNetworkmode () == true ) {
189185 addPacket (packet); /* store the packet in the FIFO queue*/
190186 } else {
191187 writeDump (packet);
192188 }
193189 break ;
194190 case polycube::service::Direction::EGRESS:
195- temp_offset = get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::EGRESS).get (0x0 );
191+ temp_offset = std::chrono::nanoseconds ( static_cast < unsigned long long >( get_array_table<uint64_t >(" packet_timestamp" , 0 , ProgramType::EGRESS).get (0x0 )) );
196192 if (getNetworkmode () == true ) {
197193 addPacket (packet); /* store the packet in the FIFO queue*/
198194 } else {
@@ -306,8 +302,16 @@ std::shared_ptr<Packet> Packetcapture::getPacket() {
306302void Packetcapture::addPacket (const std::vector<uint8_t > &packet) {
307303 PacketJsonObject pj;
308304 auto p = std::shared_ptr<Packet>(new Packet (*this , pj));
305+
306+ /*
307+ * Here the packet capture time offset must be added to the system boot time stored in epoch format.
308+ * See line 58 of this file to see system boot time stored in epoch algorithm
309+ */
310+
309311 struct timeval tp;
310- gettimeofday (&tp, NULL );
312+ std::chrono::system_clock::duration tp_dur = (timeP + temp_offset).time_since_epoch ();
313+ to_timeval (tp_dur, tp);
314+
311315 p->setTimestampSeconds ((uint32_t ) tp.tv_sec );
312316 p->setTimestampMicroseconds ((uint32_t ) tp.tv_usec );
313317 p->setPacketlen ((uint32_t ) packet.size ());
@@ -403,3 +407,10 @@ void Packetcapture::replaceGlobalheader(const GlobalheaderJsonObject &conf) {
403407void Packetcapture::delGlobalheader () {
404408 throw std::runtime_error (" Packetcapture::delGlobalheader: method not implemented" );
405409}
410+
411+ void Packetcapture::to_timeval (std::chrono::system_clock::duration& d, struct timeval & tv) {
412+ std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d);
413+
414+ tv.tv_sec = sec.count ();
415+ tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - sec).count ();
416+ }
0 commit comments