Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
7175409
Added initial framework for Parser configuration.
Dimi1010 May 21, 2025
a37a63f
Added nullptr guard and a helper method fetching required rules.
Dimi1010 May 21, 2025
319ca9c
Renamed methods to tryGetPortRule and getPortRule.
Dimi1010 May 21, 2025
a54c67d
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 17, 2025
3c26962
Rework the port mapper.
Dimi1010 Jul 17, 2025
938c74e
Moved some functions to cpp file.
Dimi1010 Jul 17, 2025
bc94131
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 17, 2025
94084f8
Fixed parser memory leak false positive.
Dimi1010 Jul 17, 2025
c7b79bf
Moved default factories to cpp file.
Dimi1010 Jul 17, 2025
7252620
Convert HTTP implementation to use PortMapper.
Dimi1010 Jul 17, 2025
9652a06
Lint
Dimi1010 Jul 17, 2025
1682e4b
Added helper method to reset the default config/
Dimi1010 Jul 17, 2025
89ca885
Added explicit cast to uint32_t in hash function.
Dimi1010 Jul 17, 2025
216accc
Added documentation.
Dimi1010 Jul 17, 2025
deb3e2e
Added method returning a match matrix instead of a single port.
Dimi1010 Jul 18, 2025
855e7df
Added port mappings from TCP Layer.
Dimi1010 Jul 18, 2025
e5b9f9c
Fixed SIP mappings.
Dimi1010 Jul 18, 2025
78071f3
Fixed recursion error.
Dimi1010 Jul 18, 2025
ea10b97
Lint
Dimi1010 Jul 18, 2025
a6ba392
Fixed warn / update execution order.
Dimi1010 Jul 18, 2025
e3c1876
Add port mappings from UDP Layer.
Dimi1010 Jul 18, 2025
8f4f3b5
Lint
Dimi1010 Jul 18, 2025
f5ff15b
Added member methods to PortPair to create a new pair from only one s…
Dimi1010 Jul 18, 2025
5d2df59
Extended the mapping to work with protocol families.
Dimi1010 Jul 20, 2025
5a60efa
Updated port mapping for GTPv2 to GTP
Dimi1010 Jul 20, 2025
e77dc46
Added utility function for quering port pair matrix.
Dimi1010 Jul 20, 2025
90bb410
Added todo.
Dimi1010 Jul 20, 2025
18f02a2
Merged SIP port rules to SIP family.
Dimi1010 Jul 20, 2025
08f67cd
Updated UDPLayer to utilize the dynamic protocol mapper.
Dimi1010 Jul 20, 2025
d6cdddd
Fixed SomeIP test
Dimi1010 Jul 20, 2025
d134241
Lint
Dimi1010 Jul 20, 2025
a890e74
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 20, 2025
800789f
Fix docs.
Dimi1010 Jul 20, 2025
a23b41b
Fix explicit ctor.
Dimi1010 Jul 20, 2025
6fe00cd
Docs fix.
Dimi1010 Jul 20, 2025
25bc58a
Changed getProtocolByPortPair to only check exact matches.
Dimi1010 Jul 20, 2025
27e7942
todo change
Dimi1010 Jul 20, 2025
21c3b95
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 22, 2025
ef5ca85
Fixed leftover parameter.
Dimi1010 Jul 22, 2025
067364d
Expanded PortPair to allow port 0 as a valid port.
Dimi1010 Jul 22, 2025
0dc3573
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 24, 2025
a89eadb
Fixed PortPair equalify operator.
Dimi1010 Jul 24, 2025
bd3ca9a
Added inequality operator.
Dimi1010 Jul 24, 2025
1dd6bbe
Enabled WakeOnLan port 0 mapping.
Dimi1010 Jul 24, 2025
6da2493
Changed comparePort to static helper.
Dimi1010 Jul 24, 2025
04a6a94
Merge remote-tracking branch 'upstream/dev' into feature/parser-config
Dimi1010 Jul 26, 2025
4be73ef
Fixed withAnySrc and withAnyDst not respecting port set flags.
Dimi1010 Jul 26, 2025
96297b5
Added constructor that takes anyport + anyport.
Dimi1010 Jul 26, 2025
63ab500
Merge branch 'dev' into feature/parser-config
Dimi1010 Aug 10, 2025
d828452
Fix parseNextLayer params in DoIP layer.
Dimi1010 Aug 10, 2025
c0b6ebd
Added DoIP mappings to default mapper.
Dimi1010 Aug 10, 2025
be81263
Added dedicated benchmark target.
Dimi1010 Aug 10, 2025
ff12269
Disabled mix of hash function.
Dimi1010 Aug 10, 2025
f8a3834
Added second port pair to benchmark.
Dimi1010 Aug 10, 2025
61fd2f8
Merge branch 'dev' into feature/parser-config
Dimi1010 Aug 14, 2025
da22e56
Renamed to getMatchMatrix.
Dimi1010 Aug 14, 2025
6d73cca
Removed portmapper namespace.
Dimi1010 Aug 14, 2025
280f6b4
Fixed mappings to FTPControl and FTPData.
Dimi1010 Aug 14, 2025
299db80
Merge branch 'dev' into feature/parser-config
Dimi1010 Sep 27, 2025
15ed36f
Converted `parseNextLayer` to static dispatch. Added protected `doPar…
Dimi1010 Sep 27, 2025
2a5fe39
Lint.
Dimi1010 Sep 27, 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
23 changes: 23 additions & 0 deletions Benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

include(FetchContent)

if(NOT (
(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL CMAKE_SYSTEM_PROCESSOR)
AND (NOT MINGW)
AND (NOT CMAKE_OSX_ARCHITECTURES OR (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL CMAKE_OSX_ARCHITECTURES))
)
)
message(WARNING "Google Benchmark backend is not supported for cross-compilation")
return()
endif()

# Fetch Google Benchmark
FetchContent_Declare(benchmark GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG v1.9.0)

# Disable testing and installation for Google Benchmark
set(BENCHMARK_ENABLE_TESTING OFF)
set(BENCHMARK_ENABLE_INSTALL OFF)
FetchContent_MakeAvailable(benchmark)

# Add PcapPlusPlus benchmarks
add_subdirectory(Packet++Bench)
99 changes: 99 additions & 0 deletions Benchmarks/Packet++Bench/Benchmarks/PortMapperBench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <vector>
#include <benchmark\benchmark.h>

#include "ParserConfig.h"

namespace pcpp_bench
{
using namespace pcpp;

namespace
{
void PortMapperStaticLookupSingle(benchmark::State& state)
{
ParserConfiguration& config = ParserConfiguration::getDefault();
PortMapper& portMapper = config.portMapper;

size_t totalLookups = 0;
for (auto _ : state)
{
PortPair portPair(80, 443);
auto protocol = portMapper.getProtocolByPortPair(portPair);
benchmark::DoNotOptimize(protocol);
totalLookups++;
}

state.SetItemsProcessed(totalLookups);
}
BENCHMARK(PortMapperStaticLookupSingle);

void PortMapperStaticLookupMatrix(benchmark::State& state)
{
ParserConfiguration& config = ParserConfiguration::getDefault();
PortMapper& portMapper = config.portMapper;

size_t totalLookups = 0;
for (auto _ : state)
{
PortPair portPair(80, 443);
auto matrix = portMapper.getMatchMatrix(portPair);
benchmark::DoNotOptimize(matrix);
totalLookups++;
}
state.SetItemsProcessed(totalLookups);
}
BENCHMARK(PortMapperStaticLookupMatrix);

void PortMapperDynamicLookupSingle(benchmark::State& state)
{
ParserConfiguration& config = ParserConfiguration::getDefault();
PortMapper& portMapper = config.portMapper;
size_t totalLookups = 0;

// Generate random port pairs for dynamic lookups
std::vector<PortPair> inputPorts{
PortPair{ 80, 65440 },
PortPair{ 64000, 64002 },
};

for (auto _ : state)
{
// Simulate dynamic port pairs by cycling through a predefined set
PortPair const& portPair = inputPorts[totalLookups % inputPorts.size()];

auto protocol = portMapper.getProtocolByPortPair(portPair);
benchmark::DoNotOptimize(protocol);
totalLookups++;
}

state.SetItemsProcessed(totalLookups);
}
BENCHMARK(PortMapperDynamicLookupSingle);

void PortMapperDynamicLookupMatrix(benchmark::State& state)
{
ParserConfiguration& config = ParserConfiguration::getDefault();
PortMapper& portMapper = config.portMapper;
size_t totalLookups = 0;

// Generate random port pairs for dynamic lookups
std::vector<PortPair> inputPorts{
PortPair{ 80, 65440 },
PortPair{ 64000, 64002 },
};

for (auto _ : state)
{
// Simulate dynamic port pairs by cycling through a predefined set
PortPair const& portPair = inputPorts[totalLookups % inputPorts.size()];

auto matrix = portMapper.getMatchMatrix(portPair);
benchmark::DoNotOptimize(matrix);
totalLookups++;
}

state.SetItemsProcessed(totalLookups);
}
BENCHMARK(PortMapperDynamicLookupMatrix);
} // namespace
} // namespace pcpp_bench
8 changes: 8 additions & 0 deletions Benchmarks/Packet++Bench/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.14)

add_executable(Packet++Bench
"main.cpp"
"Benchmarks/PortMapperBench.cpp"
)

target_link_libraries(Packet++Bench PUBLIC PcapPlusPlus::Pcap++ benchmark::benchmark)
12 changes: 12 additions & 0 deletions Benchmarks/Packet++Bench/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#include <benchmark\benchmark.h>

int main(int argc, char** argv)
{
// Initialize the benchmark library
benchmark::Initialize(&argc, argv);

// Run all benchmarks
benchmark::RunSpecifiedBenchmarks();
return 0;
}
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ cmake_dependent_option(
OFF
)
option(PCAPPP_BUILD_TESTS "Build Tests" ${PCAPPP_MAIN_PROJECT})
option(PCAPPP_BUILD_BENCHMARK "Build Benchmark Targets" OFF)
option(PCAPPP_BUILD_COVERAGE "Generate Coverage Report" OFF)
option(PCAPPP_BUILD_FUZZERS "Build Fuzzers binaries" OFF)
option(PCAPPP_BUILD_REPRODUCIBLE "Build a reproducible version" OFF)
Expand Down Expand Up @@ -358,6 +359,10 @@ if(PCAPPP_BUILD_TESTS OR PCAPPP_BUILD_FUZZERS OR PCAPPP_BUILD_EXAMPLES)
add_subdirectory(Tests)
endif()

if(PCAPPP_BUILD_BENCHMARK)
add_subdirectory(Benchmarks)
endif()

if(PCAPPP_INSTALL)
# Generate PKG-Config for non WIN32 system
if(NOT WIN32)
Expand Down
2 changes: 2 additions & 0 deletions Packet++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_library(
src/Packet.cpp
src/PacketTrailerLayer.cpp
src/PacketUtils.cpp
src/ParserConfig.cpp
src/PayloadLayer.cpp
src/PemCodec.cpp
src/PPPoELayer.cpp
Expand Down Expand Up @@ -118,6 +119,7 @@ set(
header/Packet.h
header/PacketTrailerLayer.h
header/PacketUtils.h
header/ParserConfig.h
header/PayloadLayer.h
header/PemCodec.h
header/PPPoELayer.h
Expand Down
9 changes: 5 additions & 4 deletions Packet++/header/ArpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ namespace pcpp

// implement abstract methods

/// Does nothing for this layer (ArpLayer is always last)
void parseNextLayer() override
{}

/// @return The size of @ref arphdr
size_t getHeaderLen() const override
{
Expand Down Expand Up @@ -280,6 +276,11 @@ namespace pcpp
{
return canReinterpretAs<arphdr>(data, dataLen);
}

protected:
/// Does nothing for this layer (ArpLayer is always last)
void doParseNextLayer(ParserConfiguration const& config) override
{}
};

} // namespace pcpp
8 changes: 4 additions & 4 deletions Packet++/header/BgpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ namespace pcpp
/// @return The size of the BGP message
size_t getHeaderLen() const override;

/// Multiple BGP messages can reside in a single packet, and the only layer that can come after a BGP message
/// is another BGP message. This method checks for remaining data and parses it as another BGP layer
void parseNextLayer() override;

std::string toString() const override;

OsiModelLayer getOsiModelLayer() const override
Expand All @@ -107,6 +103,10 @@ namespace pcpp
: Layer(data, dataLen, prevLayer, packet, BGP)
{}

/// Multiple BGP messages can reside in a single packet, and the only layer that can come after a BGP message
/// is another BGP message. This method checks for remaining data and parses it as another BGP layer
void doParseNextLayer(ParserConfiguration const& config) override;

bgp_common_header* getBasicHeader() const
{
return reinterpret_cast<bgp_common_header*>(m_Data);
Expand Down
7 changes: 4 additions & 3 deletions Packet++/header/CiscoHdlcLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,17 @@ namespace pcpp
/// Calculate the Next Protocol when possible
void computeCalculateFields() override;

/// Parses the next layer. Currently, supports IPv4 and IPv6
void parseNextLayer() override;

std::string toString() const override;

OsiModelLayer getOsiModelLayer() const override
{
return OsiModelDataLinkLayer;
}

protected:
/// Parses the next layer. Currently, supports IPv4 and IPv6
void doParseNextLayer(ParserConfiguration const& config) override;

private:
#pragma pack(push, 1)
struct cisco_hdlc_header
Expand Down
7 changes: 4 additions & 3 deletions Packet++/header/CotpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ namespace pcpp
void computeCalculateFields() override
{}

/// Currently parses the rest of the packet as a S7COMM or generic payload (PayloadLayer)
void parseNextLayer() override;

/// A static method that takes a byte array and detects whether it is a COTP
/// @param[in] data A byte array
/// @param[in] dataSize The byte array size (in bytes)
Expand All @@ -91,6 +88,10 @@ namespace pcpp
return OsiModelTransportLayer;
}

protected:
/// Currently parses the rest of the packet as a S7COMM or generic payload (PayloadLayer)
void doParseNextLayer(ParserConfiguration const& config) override;

private:
cotphdr* getCotpHeader() const
{
Expand Down
9 changes: 5 additions & 4 deletions Packet++/header/DhcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,6 @@ namespace pcpp

// implement abstract methods

/// Does nothing for this layer (DhcpLayer is always last)
void parseNextLayer() override
{}

/// @return The size of @ref dhcp_header + size of options
size_t getHeaderLen() const override
{
Expand All @@ -759,6 +755,11 @@ namespace pcpp
return OsiModelApplicationLayer;
}

protected:
/// Does nothing for this layer (DhcpLayer is always last)
void doParseNextLayer(ParserConfiguration const& config) override
{}

private:
uint8_t* getOptionsBasePtr() const
{
Expand Down
9 changes: 5 additions & 4 deletions Packet++/header/DhcpV6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ namespace pcpp

// implement abstract methods

/// Does nothing for this layer (DhcpV6Layer is always last)
void parseNextLayer() override
{}

/// @return The size of @ref dhcpv6_header + size of options
size_t getHeaderLen() const override
{
Expand All @@ -369,6 +365,11 @@ namespace pcpp
return OsiModelApplicationLayer;
}

protected:
/// Does nothing for this layer (DhcpV6Layer is always last)
void doParseNextLayer(ParserConfiguration const& config) override
{}

private:
uint8_t* getOptionsBasePtr() const
{
Expand Down
8 changes: 4 additions & 4 deletions Packet++/header/DnsLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,6 @@ namespace pcpp

// implement abstract methods

/// Does nothing for this layer (DnsLayer is always last)
void parseNextLayer() override
{}

/// @return The size of the DNS data in the packet including he DNS header and size of all queries, answers,
/// authorities and additional records
size_t getHeaderLen() const override
Expand Down Expand Up @@ -388,6 +384,10 @@ namespace pcpp
DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, size_t offsetAdjustment);
explicit DnsLayer(size_t offsetAdjustment);

/// Does nothing for this layer (DnsLayer is always last)
void doParseNextLayer(ParserConfiguration const& config) override
{}

private:
IDnsResource* m_ResourceList;
DnsQuery* m_FirstQuery;
Expand Down
7 changes: 4 additions & 3 deletions Packet++/header/DoIpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,6 @@ namespace pcpp

// implement abstract methods

/// parse UDS layer
void parseNextLayer() override;

/// @return The size of @ref doiphdr + attached fields length
size_t getHeaderLen() const override
{
Expand All @@ -639,6 +636,10 @@ namespace pcpp
return OsiModelApplicationLayer;
}

protected:
/// parse UDS layer
void doParseNextLayer(ParserConfiguration const& config) override;

private:
void setPayloadType(DoIpPayloadTypes payloadType);

Expand Down
7 changes: 4 additions & 3 deletions Packet++/header/EthDot3Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ namespace pcpp

// implement abstract methods

/// Parses next layer
void parseNextLayer() override;

/// @return Size of ether_dot3_header
size_t getHeaderLen() const override
{
Expand All @@ -118,6 +115,10 @@ namespace pcpp
/// @param[in] dataLen The length of the byte stream
/// @return True if the data is valid and can represent an IEEE 802.3 Eth packet
static bool isDataValid(const uint8_t* data, size_t dataLen);

protected:
/// Parses next layer
void doParseNextLayer(ParserConfiguration const& config) override;
};

} // namespace pcpp
Loading
Loading