From 1fda11039aa1e271d6b0db51dcdf3b850793c07d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 21 Jun 2026 10:11:34 +0300 Subject: [PATCH 1/3] Deduplicated SIP layer parsing logic. Use heuristic parser by default. The two parseSipLayer methods are functionally identical, except that the removed requires ports. Port numbers by themselves should not invalidate a parse and should only be used as hints to determine which parser to try first. --- Packet++/header/SipLayer.h | 12 ------------ Packet++/src/SipLayer.cpp | 24 ------------------------ Packet++/src/UdpLayer.cpp | 8 ++------ 3 files changed, 2 insertions(+), 42 deletions(-) diff --git a/Packet++/header/SipLayer.h b/Packet++/header/SipLayer.h index b67c871d98..a6b79f5b8d 100644 --- a/Packet++/header/SipLayer.h +++ b/Packet++/header/SipLayer.h @@ -114,18 +114,6 @@ namespace pcpp return port == 5060 || port == 5061; } - /// A static factory method that attempts to create a SIP layer from existing packet raw data - /// The method first checks whether the source or destination port matches the SIP protocol. - /// @param[in] data A pointer to the raw data - /// @param[in] dataLen Size of the data in bytes - /// @param[in] prevLayer A pointer to the previous layer - /// @param[in] packet A pointer to the Packet instance where layer will be stored in - /// @param[in] srcPort Source port number to check - /// @param[in] dstPort Dest port number to check - /// @return A newly allocated SIP layer of type request or response, or nullptr if parsing fails - static SipLayer* parseSipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, - uint16_t srcPort, uint16_t dstPort); - /// A static factory method that attempts to create a SIP layer from existing packet raw data /// This method does not check source or destination ports. Instead, it uses heuristics /// to determine whether the data represents a SIP request or response. diff --git a/Packet++/src/SipLayer.cpp b/Packet++/src/SipLayer.cpp index 9eb90e5203..184cc561ec 100644 --- a/Packet++/src/SipLayer.cpp +++ b/Packet++/src/SipLayer.cpp @@ -159,30 +159,6 @@ namespace pcpp } } - SipLayer* SipLayer::parseSipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, uint16_t srcPort, - uint16_t dstPort) - { - if (!(SipLayer::isSipPort(srcPort) || SipLayer::isSipPort(dstPort))) - { - return nullptr; - } - - if (SipRequestFirstLine::parseMethod(reinterpret_cast(data), dataLen) != - SipRequestLayer::SipMethodUnknown) - { - return new SipRequestLayer(data, dataLen, prevLayer, packet); - } - - if (SipResponseFirstLine::parseStatusCode(reinterpret_cast(data), dataLen) != - SipResponseLayer::SipStatusCodeUnknown && - !SipResponseFirstLine::parseVersion(reinterpret_cast(data), dataLen).empty()) - { - return new SipResponseLayer(data, dataLen, prevLayer, packet); - } - - return nullptr; - } - SipLayer* SipLayer::parseSipLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) { SipLayer::SipParseResult sipParseResult = detectSipMessageType(data, dataLen); diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index d82de04245..341ead6f7e 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -116,9 +116,7 @@ namespace pcpp } else if (SipLayer::isSipPort(portDst) || SipLayer::isSipPort(portSrc)) { - // Resolves the overload of parseSipLayer, without static_casting a function pointer. - auto* (*fac)(uint8_t*, size_t, Layer*, Packet*, uint16_t, uint16_t) = SipLayer::parseSipLayer; - tryConstructNextLayerFromFactoryWithFallback(fac, udpData, udpDataLen, portSrc, portDst); + tryConstructNextLayerFromFactoryWithFallback(SipLayer::parseSipLayer, udpData, udpDataLen); } else if ((RadiusLayer::isRadiusPort(portDst) || RadiusLayer::isRadiusPort(portSrc)) && RadiusLayer::isDataValid(udpData, udpDataLen)) @@ -173,9 +171,7 @@ namespace pcpp // Here, heuristics for all protocols should be invoked to determine the correct layer { - // Resolves the overload of parseSipLayer, without static_casting a function pointer. - auto* (*fac)(uint8_t*, size_t, Layer*, Packet*) = SipLayer::parseSipLayer; - tryConstructNextLayerFromFactoryWithFallback(fac, udpData, udpDataLen); + tryConstructNextLayerFromFactoryWithFallback(SipLayer::parseSipLayer, udpData, udpDataLen); } if (!hasNextLayer()) From 2b6049ddba1f273a7cb764e26a0ea5d0883b32cf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 21 Jun 2026 10:12:43 +0300 Subject: [PATCH 2/3] Update docs. --- Packet++/header/SipLayer.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Packet++/header/SipLayer.h b/Packet++/header/SipLayer.h index a6b79f5b8d..e1a99ff001 100644 --- a/Packet++/header/SipLayer.h +++ b/Packet++/header/SipLayer.h @@ -115,8 +115,10 @@ namespace pcpp } /// A static factory method that attempts to create a SIP layer from existing packet raw data - /// This method does not check source or destination ports. Instead, it uses heuristics - /// to determine whether the data represents a SIP request or response. + /// + /// The method uses heuristics to determine whether the data contains a SIP request or response message and + /// creates the relevant layer type. If no message type can be identified, the method returns nullptr. + /// /// @param[in] data A pointer to the raw data /// @param[in] dataLen Size of the data in bytes /// @param[in] prevLayer A pointer to the previous layer From 872d84645f676c57d602f476ac132d44076e9d81 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 21 Jun 2026 10:15:06 +0300 Subject: [PATCH 3/3] Lint --- Packet++/header/SipLayer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Packet++/header/SipLayer.h b/Packet++/header/SipLayer.h index e1a99ff001..7da753f9da 100644 --- a/Packet++/header/SipLayer.h +++ b/Packet++/header/SipLayer.h @@ -115,10 +115,10 @@ namespace pcpp } /// A static factory method that attempts to create a SIP layer from existing packet raw data - /// + /// /// The method uses heuristics to determine whether the data contains a SIP request or response message and /// creates the relevant layer type. If no message type can be identified, the method returns nullptr. - /// + /// /// @param[in] data A pointer to the raw data /// @param[in] dataLen Size of the data in bytes /// @param[in] prevLayer A pointer to the previous layer