From 7d0e0f854888cb91e2601cf73a61a23c51d31b1f Mon Sep 17 00:00:00 2001 From: Matthias Klein Date: Thu, 23 Oct 2025 08:02:11 +0200 Subject: [PATCH 1/2] handle FIN flag in order of the sequence numbers Signed-off-by: Matthias Klein --- Packet++/header/TcpReassembly.h | 4 +- Packet++/src/TcpReassembly.cpp | 45 ++++++-- .../one_http_stream_fin2_output2.txt | 103 ------------------ Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp | 4 +- 4 files changed, 38 insertions(+), 118 deletions(-) delete mode 100644 Tests/Pcap++Test/PcapExamples/one_http_stream_fin2_output2.txt diff --git a/Packet++/header/TcpReassembly.h b/Packet++/header/TcpReassembly.h index f7ee20de50..ac6c48b459 100644 --- a/Packet++/header/TcpReassembly.h +++ b/Packet++/header/TcpReassembly.h @@ -404,9 +404,11 @@ namespace pcpp uint32_t sequence; size_t dataLength; uint8_t* data; + uint32_t flowKey; + bool isFin; std::chrono::time_point timestamp; - TcpFragment() : sequence(0), dataLength(0), data(nullptr) + TcpFragment() : sequence(0), dataLength(0), data(nullptr), flowKey(0), isFin(false) {} ~TcpFragment() { diff --git a/Packet++/src/TcpReassembly.cpp b/Packet++/src/TcpReassembly.cpp index 165d594c75..f1a6dc502f 100644 --- a/Packet++/src/TcpReassembly.cpp +++ b/Packet++/src/TcpReassembly.cpp @@ -268,10 +268,10 @@ namespace pcpp return Ignore_PacketOfClosedFlow; } - // handle FIN/RST packets that don't contain additional TCP data - if (isFinOrRst && tcpPayloadSize == 0) + // handle RST packets that don't contain additional TCP data + if (isRst && tcpPayloadSize == 0) { - PCPP_LOG_DEBUG("Got FIN or RST packet without data on side " << sideIndex); + PCPP_LOG_DEBUG("Got RST packet without data on side " << sideIndex); handleFinOrRst(tcpReassemblyData, sideIndex, flowKey, isRst); return FIN_RSTWithNoData; @@ -441,27 +441,36 @@ namespace pcpp { PCPP_LOG_DEBUG("Payload length is 0, doing nothing"); - // handle case where this packet is FIN or RST - if (isFinOrRst) + // handle case where this packet is RST + if (isRst) { handleFinOrRst(tcpReassemblyData, sideIndex, flowKey, isRst); status = FIN_RSTWithNoData; + return status; } - else + + if (!isFin) { status = Ignore_PacketWithNoData; + return status; } - - return status; } // create a new TcpFragment, copy the TCP data to it and add this packet to the the out-of-order packet list TcpFragment* newTcpFrag = new TcpFragment(); - newTcpFrag->data = new uint8_t[tcpPayloadSize]; + + if (tcpPayloadSize) + { + newTcpFrag->data = new uint8_t[tcpPayloadSize]; + memcpy(newTcpFrag->data, tcpLayer->getLayerPayload(), tcpPayloadSize); + } + newTcpFrag->dataLength = tcpPayloadSize; newTcpFrag->sequence = sequence; newTcpFrag->timestamp = currTime; - memcpy(newTcpFrag->data, tcpLayer->getLayerPayload(), tcpPayloadSize); + newTcpFrag->isFin = isFin; + newTcpFrag->flowKey = flowKey; + tcpReassemblyData->twoSides[sideIndex].tcpFragmentList.pushBack(newTcpFrag); PCPP_LOG_DEBUG("Found out-of-order packet and added a new TCP fragment with size " @@ -476,8 +485,8 @@ namespace pcpp checkOutOfOrderFragments(tcpReassemblyData, sideIndex, false); } - // handle case where this packet is FIN or RST - if (isFinOrRst) + // handle case where this packet is RST + if (isRst) { handleFinOrRst(tcpReassemblyData, sideIndex, flowKey, isRst); } @@ -580,6 +589,12 @@ namespace pcpp foundSomething = true; + if (curTcpFrag->isFin) + { + PCPP_LOG_DEBUG("handle saved FIN flag on sequence match"); + handleFinOrRst(tcpReassemblyData, sideIndex, curTcpFrag->flowKey, false); + } + continue; } @@ -616,6 +631,12 @@ namespace pcpp } foundSomething = true; + + if (curTcpFrag->isFin) + { + PCPP_LOG_DEBUG("handle saved FIN flag on lower sequence"); + handleFinOrRst(tcpReassemblyData, sideIndex, curTcpFrag->flowKey, false); + } } else { diff --git a/Tests/Pcap++Test/PcapExamples/one_http_stream_fin2_output2.txt b/Tests/Pcap++Test/PcapExamples/one_http_stream_fin2_output2.txt deleted file mode 100644 index 11cb049841..0000000000 --- a/Tests/Pcap++Test/PcapExamples/one_http_stream_fin2_output2.txt +++ /dev/null @@ -1,103 +0,0 @@ -GET /forward.html HTTP/1.1 -User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 -Referer: http://www.bowlsbybruno.com/ -Cookie: B=fdnulql8iqc6l&b=3&s=ps -Connection: Keep-Alive -Accept-Encoding: gzip -Accept-Language: en,* -Host: webhosting.yahoo.com - -HTTP/1.1 404 Not Found -Date: Tue, 26 Feb 2013 22:02:37 GMT -P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV" -X-Host: p9h3.geo.bf1.yahoo.com -X-INKT-URI: http://us.webhosting.yahoo.com/server-errors/not_found.html -X-INKT-SITE: http://us.webhosting.yahoo.com -Last-Modified: Mon, 13 Aug 2012 10:24:03 GMT -Accept-Ranges: bytes -Content-Length: 6484 -Cache-Control: private -Connection: close -Content-Type: text/html - -Yahoo! -
- - - - - -
YahooYahoo! - Help
-
- - - -
-Sorry, the page you requested was not found. -
-
- - -
- -<[1448 bytes missing]pt> - - - -
-
-
- -
-Address Book · Alerts · Auctions · Bill Pay · Bookmarks · Briefcase · Broadcast · Calendar · Chat · Classifieds · Clubs · Companion · Experts · Games · Greetings · Home Pages · Invites · Mail · Maps · Member Directory · Messenger · My Yahoo! · News · PayDirect · People Search · Personals · Photos · Shopping · Sports · Stock Quotes · TV · Travel · Weather · Yahooligans · Yellow Pages · more... -
-


Copyright © 2002 Yahoo! Inc. All rights reserved.
NOTICE: We collect personal information on this site. To learn more about how we use your information, see our Yahoo Privacy Policy
- - - - - -1 diff --git a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp index 2fefcb5897..f9c2c56b6c 100644 --- a/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp +++ b/Tests/Pcap++Test/Tests/TcpReassemblyTests.cpp @@ -721,13 +721,13 @@ PTF_TEST_CASE(TestTcpReassemblyWithFIN_RST) tcpReassemblyTest(packetStream, tcpReassemblyResults, true, false); PTF_ASSERT_EQUAL(stats.size(), 1); - PTF_ASSERT_EQUAL(stats.begin()->second.numOfDataPackets, 5); + PTF_ASSERT_EQUAL(stats.begin()->second.numOfDataPackets, 6); PTF_ASSERT_EQUAL(stats.begin()->second.numOfMessagesFromSide[0], 1); PTF_ASSERT_EQUAL(stats.begin()->second.numOfMessagesFromSide[1], 1); PTF_ASSERT_TRUE(stats.begin()->second.connectionsStarted); PTF_ASSERT_TRUE(stats.begin()->second.connectionsEnded); PTF_ASSERT_FALSE(stats.begin()->second.connectionsEndedManually); - expectedReassemblyData = readFileIntoString(std::string("PcapExamples/one_http_stream_fin2_output2.txt")); + expectedReassemblyData = readFileIntoString(std::string("PcapExamples/one_http_stream_fin2_output.txt")); PTF_ASSERT_EQUAL(expectedReassemblyData, stats.begin()->second.reassembledData); } // TestTcpReassemblyWithFIN_RST From 8fb159bed9801da3dd675d553220d36ac90d235d Mon Sep 17 00:00:00 2001 From: Matthias Klein Date: Mon, 27 Oct 2025 10:24:08 +0100 Subject: [PATCH 2/2] ExamplesTest: X509Toolkit: test_pcap_extract_to_file: adjust TLS messages count after TcpReassembly changes Note: the 31 extracted certificates still have exactly the same content. Signed-off-by: Matthias Klein --- Tests/ExamplesTest/expected_output/x509_pcap_extract.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/ExamplesTest/expected_output/x509_pcap_extract.txt b/Tests/ExamplesTest/expected_output/x509_pcap_extract.txt index ef30cc1edc..5a8d04a260 100644 --- a/Tests/ExamplesTest/expected_output/x509_pcap_extract.txt +++ b/Tests/ExamplesTest/expected_output/x509_pcap_extract.txt @@ -1,7 +1,7 @@ Packet count: 7451 -TLS messages: 7229 +TLS messages: 7216 TLS Flows: 334 -TLS handshake messages: 1086 +TLS handshake messages: 1085 Certificates parsed: 31 Certificates failed parsing: 0 Incomplete Certificates: 104