Removing the --socket-mem eal parameter
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / pcapreader.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <pcap.h>
18 #include <cstring>
19 #include <linux/in.h>
20
21 #include "pcapreader.hpp"
22
23 int PcapReader::open(const string& file_path)
24 {
25         char err_str[PCAP_ERRBUF_SIZE];
26
27         if (m_handle) {
28                 m_error = "Pcap file already open";
29                 return -1;
30         }
31
32         m_handle = pcap_open_offline_with_tstamp_precision(file_path.c_str(),
33                                                            PCAP_TSTAMP_PRECISION_NANO,
34                                                            err_str);
35
36         if (!m_handle) {
37                 m_error = "Failed to open pcap file";
38                 return -1;
39         }
40
41         m_file_beg = ftell(pcap_file(m_handle));
42         fseek(pcap_file(m_handle), 0, SEEK_END);
43         m_file_end = ftell(pcap_file(m_handle));
44         fseek(pcap_file(m_handle), m_file_beg, SEEK_SET);
45
46         return 0;
47 }
48
49 int PcapReader::readOnce(PcapPkt *pkt, uint64_t pos)
50 {
51         return -1;
52 }
53
54 int PcapReader::read(PcapPkt *pkt)
55 {
56         if (!m_handle) {
57                 m_error = "No pcap file opened";
58         }
59
60         const uint8_t *buf = pcap_next(m_handle, &pkt->header);
61
62         if (buf) {
63                 memcpy(pkt->buf, buf, pkt->header.len);
64                 pktReadCount++;
65         }
66
67         return !!buf;
68 }
69
70 void PcapReader::close()
71 {
72         if (m_handle)
73                 pcap_close(m_handle);
74
75         m_handle = NULL;
76 }