diff --git a/src/fne/network/callhandler/packetdata/P25PacketData.cpp b/src/fne/network/callhandler/packetdata/P25PacketData.cpp index 37f93be4..59d5d4c6 100644 --- a/src/fne/network/callhandler/packetdata/P25PacketData.cpp +++ b/src/fne/network/callhandler/packetdata/P25PacketData.cpp @@ -204,6 +204,34 @@ bool P25PacketData::processFrame(const uint8_t* data, uint32_t len, uint32_t pee LogInfoEx(LOG_NET, P25_PDU_STR ", received block %u, peerId = %u, len = %u", currentBlock, peerId, blockLength); + // a header block that differs from the header already held for this peer opens a new PDU + // transaction; the held transaction (e.g. a response header whose announced data block never + // arrived) can never complete, and keeping it would reject every later header from the peer + // as a conflicting duplicate, so discard it and start over with this header; data blocks held + // without a header are kept, they may simply have arrived ahead of this header + if (currentBlock == 0U && totalBlocks <= P25_MAX_PDU_BLOCKS) { + auto heldHeader = status->receivedBlocks.find(0U); + if (heldHeader != status->receivedBlocks.end() && + ::memcmp(heldHeader->second, data + 24U, blockLength) != 0) { + LogWarning(LOG_P25, P25_PDU_STR ", discarding incomplete PDU, peer = %u, streamId = %u, blocks = %u of %u", + peerId, status->streamId, status->dataBlockCnt, status->totalBlocks); + + m_status.erase(peerId); + delete status; + + m_status.lock(true); + status = new RxStatus(); + status->callStartTime = pktTime; + status->lastPacket = pktTime; + status->streamId = streamId; + status->peerId = peerId; + status->totalBlocks = totalBlocks; + m_status.unlock(); + + m_status.insert(peerId, status); + } + } + // store the received block auto existing = status->receivedBlocks.find(currentBlock); if (existing != status->receivedBlocks.end()) { diff --git a/tests/fne/FNENetwork_Tests.cpp b/tests/fne/FNENetwork_Tests.cpp index a9cfb7bf..78dfda91 100644 --- a/tests/fne/FNENetwork_Tests.cpp +++ b/tests/fne/FNENetwork_Tests.cpp @@ -13,6 +13,10 @@ #include "fne/FNETestHooks.h" #include "fne/HostFNE.h" #include "fne/network/callhandler/TagAnalogData.h" +#include "fne/network/callhandler/TagP25Data.h" +#include "fne/network/callhandler/packetdata/P25PacketData.h" +#include "common/p25/P25Defines.h" +#include "common/p25/data/DataHeader.h" #include "fne/network/UnitRouteRegistry.h" #include "common/analog/AnalogDefines.h" #include "common/BitManipulation.h" @@ -24,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1749,3 +1754,105 @@ TEST_CASE("FNE metadata dispatcher rejects truncated log transfers", "[fne][hook 1001U, std::vector(10U))); REQUIRE(FNETestHooks::peerState(harness.traffic, 1001U) == network::NET_STAT_RUNNING); } + +namespace { + /** + * @brief Builds a network PDU frame carrying one encoded CAI block, as a peer forwards it. + * @param blocksToFollow Blocks to follow announced by the PDU header. + * @param currentBlock Index of the carried block (0 is the header block). + * @param[in] block Encoded CAI block (P25_PDU_FEC_LENGTH_BYTES). + * @returns std::vector Network PDU frame. + */ + std::vector p25PDUNetworkFrame(uint8_t blocksToFollow, uint8_t currentBlock, const uint8_t* block) + { + using namespace p25::defines; + + std::vector frame(24U + P25_PDU_FEC_LENGTH_BYTES, 0x00U); + SET_UINT24(P25_PDU_FEC_LENGTH_BYTES, frame.data(), 8U); + frame[20U] = blocksToFollow; + frame[21U] = currentBlock; + frame[22U] = DUID::PDU; + ::memcpy(frame.data() + 24U, block, P25_PDU_FEC_LENGTH_BYTES); + return frame; + } + + /** + * @brief Encodes an inbound P25 response header block. + * @param ackClass Response class. + * @param ackType Response type. + * @param blocksToFollow Blocks to follow announced by the header. + * @param[out] block Buffer receiving the encoded header block (P25_PDU_FEC_LENGTH_BYTES). + */ + void p25ResponseHeader(uint8_t ackClass, uint8_t ackType, uint8_t blocksToFollow, uint8_t* block) + { + using namespace p25::defines; + + p25::data::DataHeader header; + header.setFormat(PDUFormatType::RSP); + header.setMFId(MFG_STANDARD); + header.setOutbound(false); + header.setResponseClass(ackClass); + header.setResponseType(ackType); + header.setResponseStatus(3U); + header.setLLId(9051U); + header.setBlocksToFollow(blocksToFollow); + ::memset(block, 0x00U, P25_PDU_FEC_LENGTH_BYTES); + header.encode(block); + } +} + +TEST_CASE("FNE P25 PDU assembly recovers from a response missing its data block", + "[fne][p25][packet-data][pdu]") +{ + using namespace p25::defines; + + constexpr uint32_t PEER_ID = 71002U; + constexpr uint32_t STREAM_ID = 4242U; + + FNEHarness harness; + network::callhandler::packetdata::P25PacketData* packetData = harness.traffic.p25TrafficHandler()->packetData(); + REQUIRE(packetData != nullptr); + + uint8_t sackHeader[P25_PDU_FEC_LENGTH_BYTES]; + p25ResponseHeader(PDUAckClass::ACK_RETRY, PDUAckType::ACK, 1U, sackHeader); + uint8_t ackHeader[P25_PDU_FEC_LENGTH_BYTES]; + p25ResponseHeader(PDUAckClass::ACK, PDUAckType::ACK, 0U, ackHeader); + + // a peer that forwards only the header of a selective retry response leaves the + // announced data block outstanding + std::vector frame = p25PDUNetworkFrame(1U, 0U, sackHeader); + REQUIRE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 1U); + + // a header announcing more blocks than the receiver supports does not replace the + // held transaction + uint8_t longHeader[P25_PDU_FEC_LENGTH_BYTES]; + p25ResponseHeader(PDUAckClass::ACK_RETRY, PDUAckType::ACK, uint8_t(P25_MAX_PDU_BLOCKS), longHeader); + frame = p25PDUNetworkFrame(uint8_t(P25_MAX_PDU_BLOCKS), 0U, longHeader); + CHECK_FALSE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 1U); + + // the next header from the same peer opens a new transaction and must be processed, + // not rejected as a conflicting duplicate of the stale header + frame = p25PDUNetworkFrame(0U, 0U, ackHeader); + CHECK(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 0U); + + // a complete selective retry response (header and its data block) leaves no state behind + uint8_t retryBlock[P25_PDU_FEC_LENGTH_BYTES]; + ::memset(retryBlock, 0x00U, P25_PDU_FEC_LENGTH_BYTES); + frame = p25PDUNetworkFrame(1U, 0U, sackHeader); + REQUIRE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + frame = p25PDUNetworkFrame(1U, 1U, retryBlock); + REQUIRE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 0U); + + // a data block that arrives ahead of its own header is kept, and the transaction + // completes when the header follows + frame = p25PDUNetworkFrame(1U, 1U, retryBlock); + REQUIRE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 1U); + frame = p25PDUNetworkFrame(1U, 0U, sackHeader); + REQUIRE(packetData->processFrame(frame.data(), uint32_t(frame.size()), PEER_ID, 0U, STREAM_ID)); + CHECK(FNETestHooks::p25HeldPDUBlocks(harness.traffic, PEER_ID) == 0U); +} diff --git a/tests/fne/FNETestHooks.cpp b/tests/fne/FNETestHooks.cpp index fe0aa2db..d2458f39 100644 --- a/tests/fne/FNETestHooks.cpp +++ b/tests/fne/FNETestHooks.cpp @@ -864,6 +864,21 @@ bool FNETestHooks::processP25PDUResponse(TrafficNetwork& network, uint32_t llId, 1U, 0U, 1U); } +/** Gets the number of PDU blocks the FNE packet-data receiver holds for a peer. */ + +uint32_t FNETestHooks::p25HeldPDUBlocks(TrafficNetwork& network, uint32_t peerId) +{ + if (network.m_tagP25 == nullptr) + return 0U; + + auto* packetData = network.m_tagP25->packetData(); + auto it = packetData->m_status.find(peerId); + if (it == packetData->m_status.end() || it->second == nullptr) + return 0U; + + return (uint32_t)it->second->receivedBlocks.size(); +} + /** * @brief Passes a complete Version-0 DLI datagram through the network receive task. * @param network The TrafficNetwork instance. diff --git a/tests/fne/FNETestHooks.h b/tests/fne/FNETestHooks.h index 1984f0f8..83de7936 100644 --- a/tests/fne/FNETestHooks.h +++ b/tests/fne/FNETestHooks.h @@ -572,6 +572,13 @@ public: */ static bool processP25PDUResponse(network::TrafficNetwork& network, uint32_t llId, uint8_t responseStatus); + /** + * @brief Gets the number of PDU blocks the FNE packet-data receiver holds for a peer. + * @param network The TrafficNetwork that owns the packet-data receiver. + * @param peerId Peer ID. + * @return Number of blocks held for the peer; 0 when no receive state exists. + */ + static uint32_t p25HeldPDUBlocks(network::TrafficNetwork& network, uint32_t peerId); /** * @brief Passes a complete Version-0 DLI datagram through the network receive task. * @param network The TrafficNetwork that owns the OTAR service.