Skip to content

Commit 0b83e76

Browse files
authored
Deduplication of pcap header read procedures. (#2082)
* Deduplicate reading of pcap header code. * Move toString to be free function. * Update tests + Lint * Fix needs swap being written to on empty file. * Enforce strict packing. * Lint * Try pre-checking the remaining bytes in stream before read to fix Mac CI fail. * Fixup checks * Add safe version of buffer comparison macro that accepts lengths for both buffers. * Update buffer comparisons. * Move error checking code to append mode block. * Add debug statements. * Explicitly set read pointer to start of file on append mode. * Lint * Revert writePcapHeader to be inside PcapFileWriterDevice. * Remove else.
1 parent 66fd711 commit 0b83e76

4 files changed

Lines changed: 214 additions & 202 deletions

File tree

Pcap++/header/PcapFileDevice.h

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -306,44 +306,13 @@ namespace pcpp
306306
void flush();
307307

308308
private:
309+
static bool writeHeader(std::ostream& outStream, FileTimestampPrecision precision, uint32_t snaplen,
310+
LinkLayerType linkType);
311+
309312
LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
310313
bool m_NeedsSwap = false;
311314
FileTimestampPrecision m_Precision = FileTimestampPrecision::Unknown;
312315
std::fstream m_PcapFile;
313-
314-
struct CheckHeaderResult
315-
{
316-
enum class Result
317-
{
318-
HeaderOk,
319-
HeaderError,
320-
HeaderNeeded
321-
};
322-
323-
Result result;
324-
std::string error;
325-
bool needsSwap = false;
326-
327-
static CheckHeaderResult fromOk(bool needsSwap)
328-
{
329-
return { Result::HeaderOk, "", needsSwap };
330-
}
331-
332-
static CheckHeaderResult fromError(const std::string& error)
333-
{
334-
return { Result::HeaderError, error };
335-
}
336-
337-
static CheckHeaderResult fromHeaderNeeded()
338-
{
339-
return { Result::HeaderNeeded };
340-
}
341-
};
342-
343-
static bool writeHeader(std::fstream& pcapFile, FileTimestampPrecision precision, uint32_t snaplen,
344-
LinkLayerType linkType);
345-
static CheckHeaderResult checkHeader(std::fstream& pcapFile, FileTimestampPrecision requestedPrecision,
346-
LinkLayerType requestedLinkType);
347316
};
348317

349318
/// @class PcapNgFileReaderDevice

0 commit comments

Comments
 (0)