16 #ifndef BM_BM_SIM_PCAP_FILE_H_
17 #define BM_BM_SIM_PCAP_FILE_H_
27 #include <unordered_map>
29 #include "packet_handler.h"
42 const pcap_pkthdr *pcap_header;
44 PcapPacket(
unsigned port,
const u_char *data,
const pcap_pkthdr *pcap_header)
47 pcap_header(pcap_header) { }
50 friend class PcapFileIn;
53 static std::string timevalToString(
const struct timeval *tv);
55 const char *getData()
const {
return reinterpret_cast<const char *
>(data); }
56 unsigned getLength()
const {
return static_cast<unsigned>(pcap_header->len); }
57 const struct timeval *getTime()
const {
return &pcap_header->ts; }
58 unsigned getPort()
const {
return port; }
66 PcapFileBase(
unsigned port, std::string filename);
72 class PcapFileIn :
public PcapFileBase {
74 PcapFileIn(
unsigned port, std::string filename);
75 virtual ~PcapFileIn();
78 std::unique_ptr<PcapPacket> current()
const;
89 pcap_pkthdr *current_header;
90 const u_char *current_data;
106 PcapFileIn(PcapFileIn
const& ) =
delete;
107 PcapFileIn& operator=(PcapFileIn
const&) =
delete;
112 public PcapFileBase {
115 PcapFileOut(
unsigned port, std::string filename);
116 virtual ~PcapFileOut();
117 void writePacket(
const char *data,
unsigned length);
121 pcap_dumper_t *dumper;
123 PcapFileOut(PcapFileOut
const& ) =
delete;
124 PcapFileOut& operator=(PcapFileOut
const&) =
delete;
130 class PcapFilesReader :
131 public PacketDispatcherIface {
140 PcapFilesReader(
bool respectTiming,
unsigned wait_time_in_seconds);
142 void addFile(
unsigned port, std::string file);
146 PacketDispatcherIface::ReturnCode set_packet_handler(
147 const PacketHandler &handler,
void *cookie);
150 std::vector<std::unique_ptr<PcapFileIn>> files;
151 unsigned nonEmptyFiles;
152 unsigned wait_time_in_seconds;
155 struct timeval startTime;
157 struct timeval firstPacketTime;
162 unsigned scheduledIndex;
166 void schedulePacket(
unsigned index,
const struct timeval *delay);
169 PcapFilesReader(PcapFilesReader
const& ) =
delete;
170 PcapFilesReader& operator=(PcapFilesReader
const&) =
delete;
173 PacketHandler handler;
179 class PcapFilesWriter :
public PacketReceiverIface {
183 void addFile(
unsigned port, std::string file);
184 void send_packet(
int port_num,
const char *buffer,
int len);
187 std::unordered_map<unsigned, std::unique_ptr<PcapFileOut>> files;
189 PcapFilesWriter(PcapFilesWriter
const& ) =
delete;
190 PcapFilesWriter& operator=(PcapFilesWriter
const&) =
delete;
195 #endif // BM_BM_SIM_PCAP_FILE_H_