Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
02de760
Added heuristics file content detector that determines the content ba…
Dimi1010 Sep 12, 2025
d2b6339
Merge remote-tracking branch 'upstream/dev' into feature/heuristic-fi…
Dimi1010 Sep 12, 2025
685dd9f
Moved stream checkpoint outside format detector as it is not directly…
Dimi1010 Sep 12, 2025
40dee69
Added a new factory function `createReader` that uses the new heurist…
Dimi1010 Sep 12, 2025
f1e3e18
Add <algorithm> include.
Dimi1010 Sep 12, 2025
8da1790
Added unit tests.
Dimi1010 Sep 12, 2025
3ad51e2
Deprecated old factory function.
Dimi1010 Sep 12, 2025
15c2000
Add byte-swapped zstd magic number.
Dimi1010 Sep 12, 2025
17af8d4
Lint
Dimi1010 Sep 12, 2025
46418ec
Move enum closer to first usage.
Dimi1010 Sep 12, 2025
3d713ab
Added unit tests for file reader device factory.
Dimi1010 Sep 15, 2025
a2391ec
Revert indentation.
Dimi1010 Sep 15, 2025
ea328d7
Fixed StreamCheckpoint to restore original stream state.
Dimi1010 Sep 15, 2025
db86c3e
Merge branch 'dev' into feature/heuristic-file-selection
Dimi1010 Sep 15, 2025
4aed9bd
Merge branch 'dev' into feature/heuristic-file-selection
Dimi1010 Sep 20, 2025
a83ae2b
Moved isStreamSeekable helper to inside `CaptureFileFormatDetector`.
Dimi1010 Sep 20, 2025
916e872
Added pcap magic number for Alexey Kuznetzov's modified pcap format.
Dimi1010 Sep 20, 2025
022529f
Merge remote-tracking branch 'upstream/dev' into feature/heuristic-fi…
Dimi1010 Sep 26, 2025
169fcd2
Split the unit test into multiple smaller tests.
Dimi1010 Sep 26, 2025
db8c848
Merge branch 'dev' into feature/heuristic-file-selection
Dimi1010 Oct 2, 2025
3e74912
Merge remote-tracking branch 'upstream/dev' into feature/heuristic-fi…
Dimi1010 Oct 2, 2025
f1613c4
Added helper to indicate if ZstSupport is enabled for PcapNg devices.
Dimi1010 Oct 2, 2025
bc2bacd
Split pcap microsecond and nanosecond file heuristics tests.
Dimi1010 Oct 2, 2025
58ac45d
Skipping Zst test case if zst is not supported.
Dimi1010 Oct 2, 2025
3b4b5ad
Due to file heuristics returning PcapNG format on Zstd archive, if Zs…
Dimi1010 Oct 2, 2025
18379b4
Lint
Dimi1010 Oct 2, 2025
8a4f6f8
Added invalid device factory to pcap tag.
Dimi1010 Oct 2, 2025
7776e0e
Updated static zst archives to be actual archives.
Dimi1010 Oct 2, 2025
4f52f59
Centralized PTF test name width under a macro.
Dimi1010 Oct 3, 2025
88ebfff
Add Pcap++Test header files to test sources for IDE tooling.
Dimi1010 Oct 3, 2025
41fe188
Fixed test output formatting.
Dimi1010 Oct 3, 2025
c8ae4f8
Lint
Dimi1010 Oct 3, 2025
c7cab2b
Typo fix.
Dimi1010 Oct 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: '.*\.(pcap|pcapng|dat)|(PacketExamples|PcapExamples|expected_output|pcap_examples).*\.txt'
exclude: '.*\.(pcap|pcapng|dat)|(PacketExamples|PcapExamples|expected_output|pcap_examples).*\.(txt|zst|zstd)'
fail_fast: false
repos:
- repo: local
Expand Down
1 change: 1 addition & 0 deletions Pcap++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ target_link_libraries(
)

if(LIGHT_PCAPNG_ZSTD)
target_compile_definitions(Pcap++ PRIVATE -DPCPP_PCAPNG_ZSTD_SUPPORT)
target_link_libraries(Pcap++ PRIVATE light_pcapng)
endif()

Expand Down
20 changes: 20 additions & 0 deletions Pcap++/header/PcapFileDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,19 @@ namespace pcpp
/// it returns an instance of PcapFileReaderDevice
/// @param[in] fileName The file name to open
/// @return An instance of the reader to read the file. Notice you should free this instance when done using it
/// @deprecated Prefer `createReader` due to selection of reader based on file content instead of extension.
PCPP_DEPRECATED("Prefer `createReader` due to selection of reader based on file content instead of extension.")
static IFileReaderDevice* getReader(const std::string& fileName);

/// @brief Creates an instance of the reader best fit to read the file.
///
/// The factory function uses heuristics based on the file content to decide the reader.
/// If the file type is known at compile time, it is better to construct a concrete reader instance directly.
///
/// @param[in] fileName The path to the file to open.
/// @return A unique pointer to a reader instance or nullptr if the file is not supported.
/// @throws std::runtime_error If the file could not be opened.
static std::unique_ptr<IFileReaderDevice> createReader(const std::string& fileName);
};

/// @class IFileWriterDevice
Expand Down Expand Up @@ -313,6 +325,10 @@ namespace pcpp
PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);

public:
/// @brief A static method that checks if the device was built with zstd compression support
/// @return True if zstd compression is supported, false otherwise.
static bool isZstdSupported();

/// A constructor for this class that gets the pcap-ng full path file name to open. Notice that after calling
/// this constructor the file isn't opened yet, so reading packets will fail. For opening the file call open()
/// @param[in] fileName The full path of the file to read
Expand Down Expand Up @@ -397,6 +413,10 @@ namespace pcpp
PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);

public:
/// @brief A static method that checks if the device was built with zstd compression support.
/// @return True if zstd compression is supported, false otherwise.
static bool isZstdSupported();

/// A constructor for this class that gets the pcap-ng full path file name to open for writing or create. Notice
/// that after calling this constructor the file isn't opened yet, so writing packets will fail. For opening the
/// file call open()
Expand Down
273 changes: 240 additions & 33 deletions Pcap++/src/PcapFileDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "PcapFileDevice.h"
#define LOG_MODULE PcapLogModuleFileDevice

#include <cerrno>
#include <stdexcept>
#include <array>
#include <algorithm>
#include "PcapFileDevice.h"
#include "light_pcapng_ext.h"
#include "Logger.h"
Expand All @@ -14,6 +17,27 @@ namespace pcpp
{
namespace
{
bool checkNanoSupport()
{
#ifdef PCAP_TSTAMP_PRECISION_NANO
return true;
#else
PCPP_LOG_DEBUG(
"PcapPlusPlus was compiled without nano precision support which requires libpcap > 1.5.1. Please "
"recompile PcapPlusPlus with nano precision support to use this feature. Using default microsecond precision");
return false;
#endif
}

bool checkZstdSupport()
{
#ifdef PCPP_PCAPNG_ZSTD_SUPPORT
return true;
#else
return false;
#endif
}

/// @brief Converts a light_pcapng_t* to an opaque LightPcapNgHandle*.
/// @param pcapngHandle The light_pcapng_t* to convert.
/// @return An pointer to the opaque handle.
Expand All @@ -29,43 +53,195 @@ namespace pcpp
{
return reinterpret_cast<light_pcapng_t*>(pcapngHandle);
}
} // namespace

template <typename T, size_t N> constexpr size_t ARRAY_SIZE(T (&)[N])
{
return N;
}
struct pcap_file_header
{
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
int32_t thiszone;
uint32_t sigfigs;
uint32_t snaplen;
uint32_t linktype;
};

struct pcap_file_header
{
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
int32_t thiszone;
uint32_t sigfigs;
uint32_t snaplen;
uint32_t linktype;
};
struct packet_header
{
uint32_t tv_sec;
uint32_t tv_usec;
uint32_t caplen;
uint32_t len;
};

struct packet_header
{
uint32_t tv_sec;
uint32_t tv_usec;
uint32_t caplen;
uint32_t len;
};
class StreamPositionCheckpoint
{
public:
explicit StreamPositionCheckpoint(std::istream& stream)
: m_Stream(stream), m_State(stream.rdstate()), m_Pos(stream.tellg())
{}

static bool checkNanoSupport()
{
#if defined(PCAP_TSTAMP_PRECISION_NANO)
return true;
#else
PCPP_LOG_DEBUG(
"PcapPlusPlus was compiled without nano precision support which requires libpcap > 1.5.1. Please "
"recompile PcapPlusPlus with nano precision support to use this feature. Using default microsecond precision");
return false;
#endif
}
~StreamPositionCheckpoint()
{
m_Stream.seekg(m_Pos);
m_Stream.clear(m_State);
}

private:
std::istream& m_Stream;
std::ios_base::iostate m_State;
std::streampos m_Pos;
};

enum class CaptureFileFormat
{
Unknown,
Pcap,
PcapNG,
Snoop,
};
Comment on lines +95 to +101
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto: this can be an enum inside of CaptureFileFormatDetector

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose. It has internal linkage so it doesn't really matter.

But then we would end up with really long case names: CaptureFileFormatDetector::FileFormat::Pcap?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine? It's all internal anyway...


/// @brief Heuristic file format detector that scans the magic number of the file format header.
class CaptureFileFormatDetector
{
public:
/// @brief Checks a content stream for the magic number and determines the type.
/// @param content A content stream that contains the file content.
/// @return A CaptureFileFormat value with the detected content type.
CaptureFileFormat detectFormat(std::istream& content)
{
// Check if the stream supports seeking.
if (!isStreamSeekable(content))
{
throw std::runtime_error("Heuristic file format detection requires seekable stream");
}

if (isPcapFile(content))
return CaptureFileFormat::Pcap;

// PcapNG backend can support ZstdCompressed Pcap files, so we assume an archive is compressed PcapNG.
// If Zstd is not supported, we cannot read the file anyway, so we return Unknown.
if (isPcapNgFile(content) || (checkZstdSupport() && isZstdArchive(content)))
Copy link
Collaborator Author

@Dimi1010 Dimi1010 Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to return Unknown instead of PcapNG, if Zst is not supported in the binary.
This will in turn, return nullptr for reader device. I think that is better than returning a PcapNGDevice that can't be opened.

Maybe we can also do something similar for PcapReaderDevice's nanosecond format files, if the environment can't read them?

Any thoughts @seladb ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree that it's the correct behavior 👍

return CaptureFileFormat::PcapNG;

if (isSnoopFile(content))
return CaptureFileFormat::Snoop;

return CaptureFileFormat::Unknown;
}

private:
/// @brief Check if a stream is seekable.
/// @param stream The stream to check.
/// @return True if the stream supports seek operations, false otherwise.
bool isStreamSeekable(std::istream& stream)
{
auto pos = stream.tellg();
if (stream.fail())
{
stream.clear();
return false;
}

if (stream.seekg(pos).fail())
{
stream.clear();
return false;
}

return true;
}

bool isPcapFile(std::istream& content)
{
// Pcap magic numbers are taken from: https://github.com/the-tcpdump-group/libpcap/blob/master/sf-pcap.c
// There are some other reserved magic numbers but they are not supported by libpcap so we ignore them.
constexpr std::array<uint32_t, 6> pcapMagicNumbers = {
0xa1'b2'c3'd4, // regular pcap, microsecond-precision
0xd4'c3'b2'a1, // regular pcap, microsecond-precision (byte-swapped)
// Libpcap 1.5.0 and later support reading nanosecond-precision pcap files.
0xa1'b2'3c'4d, // regular pcap, nanosecond-precision
0x4d'3c'b2'a1, // regular pcap, nanosecond-precision (byte-swapped)
// Libpcap 0.9.1 and later support reading a modified pcap format that contains an extended header.
// Format reference: https://wiki.wireshark.org/Development/LibpcapFileFormat#modified-pcap
0xa1'b2'cd'34, // Alexey Kuznetzov's modified libpcap format
0x34'cd'b2'a1 // Alexey Kuznetzov's modified libpcap format (byte-swapped)
};

StreamPositionCheckpoint checkpoint(content);

pcap_file_header header;
content.read(reinterpret_cast<char*>(&header), sizeof(header));
if (content.gcount() != sizeof(header))
{
return false;
}

return std::find(pcapMagicNumbers.begin(), pcapMagicNumbers.end(), header.magic) !=
pcapMagicNumbers.end();
}

bool isPcapNgFile(std::istream& content)
{
constexpr std::array<uint32_t, 1> pcapMagicNumbers = {
0x0A'0D'0D'0A, // pcapng magic number (palindrome)
};

StreamPositionCheckpoint checkpoint(content);

uint32_t magic = 0;
content.read(reinterpret_cast<char*>(&magic), sizeof(magic));
if (content.gcount() != sizeof(magic))
{
return false;
}

return std::find(pcapMagicNumbers.begin(), pcapMagicNumbers.end(), magic) != pcapMagicNumbers.end();
}

bool isSnoopFile(std::istream& content)
{
constexpr std::array<uint64_t, 2> snoopMagicNumbers = {
0x73'6E'6F'6F'70'00'00'00, // snoop magic number, "snoop" in ASCII
0x00'00'00'70'6F'6F'6E'73 // snoop magic number, "snoop" in ASCII (byte-swapped)
};

StreamPositionCheckpoint checkpoint(content);

uint64_t magic = 0;
content.read(reinterpret_cast<char*>(&magic), sizeof(magic));
if (content.gcount() != sizeof(magic))
{
return false;
}

return std::find(snoopMagicNumbers.begin(), snoopMagicNumbers.end(), magic) != snoopMagicNumbers.end();
}

bool isZstdArchive(std::istream& content)
{
constexpr std::array<uint32_t, 2> zstdMagicNumbers = {
0x28'B5'2F'FD, // zstd archive magic number
0xFD'2F'B5'28, // zstd archive magic number (byte-swapped)
};

StreamPositionCheckpoint checkpoint(content);

uint32_t magic = 0;
content.read(reinterpret_cast<char*>(&magic), sizeof(magic));
if (content.gcount() != sizeof(magic))
{
return false;
}

return std::find(zstdMagicNumbers.begin(), zstdMagicNumbers.end(), magic) != zstdMagicNumbers.end();
}
};

template <typename T, size_t N> constexpr size_t ARRAY_SIZE(T (&)[N])
{
return N;
}
} // namespace

// ~~~~~~~~~~~~~~~~~~~
// IFileDevice members
Expand Down Expand Up @@ -131,6 +307,27 @@ namespace pcpp
return new PcapFileReaderDevice(fileName);
}

std::unique_ptr<IFileReaderDevice> IFileReaderDevice::createReader(const std::string& fileName)
{
std::ifstream fileContent(fileName, std::ios_base::binary);
if (fileContent.fail())
{
throw std::runtime_error("Could not open: " + fileName);
}

switch (CaptureFileFormatDetector().detectFormat(fileContent))
{
case CaptureFileFormat::Pcap:
return std::make_unique<PcapFileReaderDevice>(fileName);
case CaptureFileFormat::PcapNG:
return std::make_unique<PcapNgFileReaderDevice>(fileName);
case CaptureFileFormat::Snoop:
return std::make_unique<SnoopFileReaderDevice>(fileName);
default:
return nullptr;
}
}

uint64_t IFileReaderDevice::getFileSize() const
{
std::ifstream fileStream(m_FileName.c_str(), std::ifstream::ate | std::ifstream::binary);
Expand Down Expand Up @@ -536,6 +733,11 @@ namespace pcpp
// PcapNgFileReaderDevice members
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bool PcapNgFileReaderDevice::isZstdSupported()
{
return checkZstdSupport();
}

PcapNgFileReaderDevice::PcapNgFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName)
{
m_LightPcapNg = nullptr;
Expand Down Expand Up @@ -706,6 +908,11 @@ namespace pcpp
// PcapNgFileWriterDevice members
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bool PcapNgFileWriterDevice::isZstdSupported()
{
return checkZstdSupport();
}

PcapNgFileWriterDevice::PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel)
: IFileWriterDevice(fileName)
{
Expand Down
8 changes: 8 additions & 0 deletions Tests/Pcap++Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ add_executable(
Tests/XdpTests.cpp
)

# Added as source files for IDEs
target_sources(Pcap++Test PRIVATE
Common/GlobalTestArgs.h
Common/PcapFileNamesDef.h
Common/TestUtils.h
TestDefinition.h
)

target_link_libraries(Pcap++Test PUBLIC memplumber Pcap++ PcppTestFramework EndianPortable)

if(MSVC)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4561d5a474d6as465d8as41d1as6531863d1as65d1a36d1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4561d5a474d6as465d8as41d1as6531863d1as65d1a36d1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4561d5a474d6as465d8as41d1as6531863d1as65d1a36d1
Loading
Loading