forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcap_file.cpp
394 lines (319 loc) · 10.1 KB
/
pcap_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <bm/bm_sim/pcap_file.h>
#include <bm/bm_sim/logger.h>
#include <cassert>
#include <chrono>
#include <thread>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cstring>
namespace bm {
namespace {
// TODO(antonin): remove or good enough?
void pcap_fatal_error(const std::string &message) {
Logger::get()->critical(message);
exit(1);
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// This method should be in some separate C library
// Converts a timeval into a string.
// static
std::string
PcapPacket::timevalToString(const struct timeval *tv) {
std::stringstream result;
result << std::to_string(tv->tv_sec) << ":"
<< std::setfill('0') << std::setw(6) << std::to_string(tv->tv_usec);
return result.str();
}
////////////////////////////////////////////////////////////////////////////////
PcapFileBase::PcapFileBase(unsigned port, std::string filename)
: port(port),
filename(filename) {}
////////////////////////////////////////////////////////////////////////////////
// A stream-like abstraction of a PCAP (Packet capture) file.
PcapFileIn::PcapFileIn(unsigned port, std::string filename)
: PcapFileBase(port, filename),
pcap(nullptr),
current_header(nullptr),
current_data(nullptr) {
reset();
}
void
PcapFileIn::deallocate() {
if (pcap != nullptr)
pcap_close(pcap);
pcap = nullptr;
current_header = nullptr;
current_data = nullptr;
}
PcapFileIn::~PcapFileIn() {
deallocate();
}
void
PcapFileIn::open() {
char errbuf[PCAP_ERRBUF_SIZE];
pcap = pcap_open_offline(filename.c_str(), errbuf);
if (pcap == nullptr)
pcap_fatal_error(errbuf);
state = State::Opened;
}
void
PcapFileIn::reset() {
deallocate();
state = State::Uninitialized;
open();
}
bool
PcapFileIn::moveNext() {
switch (state) {
case State::AtEnd:
return false;
case State::Opened:
case State::Reading:
return advance();
case State::Uninitialized:
default:
pcap_fatal_error("Unexpected state");
return false; // unreachable
}
}
bool
PcapFileIn::advance() {
int pcap_status = pcap_next_ex(pcap, ¤t_header, ¤t_data);
if (pcap_status == 1) { // packet read without problems
if (current_header->caplen != current_header->len) {
pcap_fatal_error(
std::string("Incompletely captured packet in ") + filename);
}
state = State::Reading;
return true;
} else if (pcap_status == -2) { // end of file
state = State::AtEnd;
return false;
} else if (pcap_status == -1) {
pcap_fatal_error(
std::string("Error while reading packet from ") + filename);
} else {
// other status impossible, even 0 (which is only for live capture)
assert(0 && "Invalid pcap status");
}
// unreachable
return false;
}
std::unique_ptr<PcapPacket>
PcapFileIn::current() const {
switch (state) {
case State::Reading:
{
PcapPacket *packet = new PcapPacket(port, current_data, current_header);
return std::unique_ptr<PcapPacket>(packet);
}
case State::Opened:
pcap_fatal_error("Must call moveNext() before calling current()");
break;
case State::AtEnd:
pcap_fatal_error("Cannot read past end-of-file");
break;
case State::Uninitialized:
/* fallthrough */
default:
pcap_fatal_error("Unexpected state");
}
return nullptr; // unreachable
}
bool PcapFileIn::atEOF() const {
return state == State::AtEnd;
}
////////////////////////////////////////////////////////////////////////////////
PcapFilesReader::PcapFilesReader(bool respectTiming,
unsigned wait_time_in_seconds)
: nonEmptyFiles(0),
wait_time_in_seconds(wait_time_in_seconds),
respectTiming(respectTiming),
started(false) {
timerclear(&zero);
}
void
PcapFilesReader::addFile(unsigned index, std::string file) {
auto f = std::unique_ptr<PcapFileIn>(new PcapFileIn(index, file));
files.push_back(std::move(f));
}
void
PcapFilesReader::scan() {
while (true) {
if (nonEmptyFiles == 0) {
BMLOG_DEBUG("Pcap reader: end of all input files");
// notifyAllEnd();
return;
}
int earliest_index = -1;
const struct timeval *earliest_time = nullptr;
for (unsigned int i = 0; i < files.size(); i++) {
auto file = files.at(i).get();
if (file->atEOF()) continue;
std::unique_ptr<PcapPacket> p = file->current();
if (earliest_index == -1) {
earliest_index = i;
earliest_time = p->getTime();
} else {
const struct timeval *time = p->getTime();
if (timercmp(time, earliest_time, <)) {
earliest_index = i;
earliest_time = time;
}
}
}
assert(earliest_index >= 0 && earliest_time != nullptr);
struct timeval delay;
BMLOG_DEBUG("Pcap reader: first packet to send {}",
PcapPacket::timevalToString(earliest_time));
if (respectTiming) {
struct timeval delta;
timersub(earliest_time, &firstPacketTime, &delta);
struct timeval now;
gettimeofday(&now, nullptr);
struct timeval elapsed;
timersub(&now, &startTime, &elapsed);
timersub(&delta, &elapsed, &delay);
BMLOG_DEBUG("Pcap reader: delta {}, elapsed {}, delay {}",
PcapPacket::timevalToString(&delta),
PcapPacket::timevalToString(&elapsed),
PcapPacket::timevalToString(&delay));
} else {
timerclear(&delay);
}
schedulePacket((unsigned)earliest_index, &delay);
}
}
void PcapFilesReader::timerFired() {
auto file = files.at(scheduledIndex).get();
std::unique_ptr<PcapPacket> packet = file->current();
if (handler != nullptr)
handler(packet->getPort(), packet->getData(), packet->getLength(), cookie);
else
pcap_fatal_error("No packet handler set when sending packet");
bool success = file->moveNext();
if (!success)
nonEmptyFiles--;
}
void PcapFilesReader::schedulePacket(unsigned index, const struct timeval *at) {
scheduledIndex = index;
int compare = timercmp(at, &zero, <=);
if (compare) {
// packet should already have been sent
timerFired();
} else {
auto duration = std::chrono::seconds(at->tv_sec) +
std::chrono::microseconds(at->tv_usec);
BMLOG_DEBUG("Pcap reader: sleep for {}", duration.count());
std::this_thread::sleep_for(duration);
timerFired();
}
}
void PcapFilesReader::start() {
BMLOG_DEBUG("Pcap reader: starting PcapFilesReader {}",
std::to_string(files.size()));
// Give the switch some time to initialize
if (wait_time_in_seconds > 0) {
BMLOG_DEBUG("Pcap reader: waiting for {} seconds",
std::to_string(wait_time_in_seconds));
auto duration = std::chrono::seconds(wait_time_in_seconds);
std::this_thread::sleep_for(duration);
}
// Find out time of first packet
const struct timeval *firstTime = nullptr;
// Linear scan; we could use a priority queue, but probably this
// is more efficient for a small number of files.
for (auto &file : files) {
bool success = file->moveNext();
if (success) {
nonEmptyFiles++;
std::unique_ptr<PcapPacket> p = file->current();
const struct timeval *ptime = p->getTime();
if (firstTime == nullptr)
firstTime = ptime;
else if (timercmp(ptime, firstTime, <))
firstTime = ptime;
}
}
if (nonEmptyFiles > 0) {
assert(firstTime != nullptr);
firstPacketTime = *firstTime;
BMLOG_DEBUG("Pcap reader: first packet time {}",
PcapPacket::timevalToString(firstTime));
}
if (started)
pcap_fatal_error("Reader already started");
started = true;
gettimeofday(&startTime, nullptr);
scan();
}
PacketDispatcherIface::ReturnCode
PcapFilesReader::set_packet_handler(const PacketHandler &hnd, void *ck) {
assert(hnd);
assert(ck);
handler = hnd;
cookie = ck;
return ReturnCode::SUCCESS;
}
////////////////////////////////////////////////////////////////////////////////
PcapFileOut::PcapFileOut(unsigned port, std::string filename)
: PcapFileBase(port, filename) {
pcap = pcap_open_dead(0, 0);
if (pcap == nullptr)
pcap_fatal_error(
std::string("Could not open file ") + filename + " for writing");
dumper = pcap_dump_open(pcap, filename.c_str());
if (dumper == nullptr)
pcap_fatal_error(
std::string("Could not open file ") + filename + " for writing");
}
void
PcapFileOut::writePacket(const char *data, unsigned length) {
struct pcap_pkthdr pkt_header;
memset(&pkt_header, 0, sizeof(pkt_header));
gettimeofday(&pkt_header.ts, NULL);
pkt_header.caplen = length;
pkt_header.len = length;
pcap_dump(reinterpret_cast<unsigned char *>(dumper), &pkt_header,
reinterpret_cast<const unsigned char *>(data));
pcap_dump_flush(dumper);
}
PcapFileOut::~PcapFileOut() {
pcap_dump_close(dumper);
pcap_close(pcap);
}
////////////////////////////////////////////////////////////////////////////////
PcapFilesWriter::PcapFilesWriter() {}
void
PcapFilesWriter::addFile(unsigned port, std::string file) {
auto f = std::unique_ptr<PcapFileOut>(new PcapFileOut(port, file));
files.emplace(port, std::move(f));
}
void
PcapFilesWriter::send_packet(int port_num, const char *buffer, int len) {
unsigned idx = port_num;
if (files.find(port_num) == files.end())
// The behavior of the bmi_* library seems to be to ignore
// packets sent to inexistent interfaces, so we replicate it here.
return;
auto file = files.at(idx).get();
file->writePacket(buffer, len);
}
} // namespace bm