Skip to content

Commit c6dbb15

Browse files
committed
Pcap*Writer: Fixed writing zero length packets.
1 parent ffc6ac5 commit c6dbb15

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/inet/common/packet/recorder/PcapWriter.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ void PcapWriter::writePacket(simtime_t stime, const Packet *packet, b frontOffse
123123
case 9: ph.ts_usec = (uint32_t)(stime.inUnit(SIMTIME_NS) - (uint32_t)1000000000 * stime.inUnit(SIMTIME_S)); break;
124124
default: throw cRuntimeError("Unsupported time precision (%d) in PcapWriter.", timePrecision);
125125
}
126-
auto data = packet->peekDataAt<BytesChunk>(frontOffset, packet->getDataLength() - frontOffset - backOffset);
127-
auto bytes = data->getBytes();
128-
for (size_t i = 0; i < bytes.size(); i++) {
129-
buf[i] = bytes[i];
126+
b capturedLength = packet->getDataLength() - frontOffset - backOffset;
127+
if (capturedLength != b(0)) {
128+
auto data = packet->peekDataAt<BytesChunk>(frontOffset, capturedLength);
129+
auto bytes = data->getBytes();
130+
for (size_t i = 0; i < bytes.size(); i++) {
131+
buf[i] = bytes[i];
132+
}
130133
}
131-
ph.orig_len = data->getChunkLength().get<B>();
134+
ph.orig_len = capturedLength.get<B>();
132135

133136
ph.incl_len = ph.orig_len > snaplen ? snaplen : ph.orig_len;
134137
fwrite(&ph, sizeof(ph), 1, dumpfile);

src/inet/common/packet/recorder/PcapngWriter.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,17 @@ void PcapngWriter::writePacket(simtime_t stime, const Packet *packet, b frontOff
232232
pbh.originalPacketLength = capturedLength.get<B>();
233233
fwrite(&pbh, sizeof(pbh), 1, dumpfile);
234234

235-
// packet data
236-
auto data = packet->peekDataAt<BytesChunk>(frontOffset, capturedLength);
237-
auto bytes = data->getBytes();
238-
fwrite(bytes.data(), bytes.size(), 1, dumpfile);
239-
240-
// packet padding
241-
char padding[] = { 0, 0, 0, 0 };
242-
int paddingLength = pad(capturedLength.get<B>());
243-
fwrite(padding, paddingLength, 1, dumpfile);
235+
if (capturedLength != b(0)) {
236+
// packet data
237+
auto data = packet->peekDataAt<BytesChunk>(frontOffset, capturedLength);
238+
auto bytes = data->getBytes();
239+
fwrite(bytes.data(), bytes.size(), 1, dumpfile);
240+
241+
// packet padding
242+
char padding[] = { 0, 0, 0, 0 };
243+
int paddingLength = pad(capturedLength.get<B>());
244+
fwrite(padding, paddingLength, 1, dumpfile);
245+
}
244246

245247
// direction option
246248
pcapng_option_header doh;

0 commit comments

Comments
 (0)