diff --git a/src/fne/network/TrafficNetwork.cpp b/src/fne/network/TrafficNetwork.cpp index 7da2db2e..726a1467 100644 --- a/src/fne/network/TrafficNetwork.cpp +++ b/src/fne/network/TrafficNetwork.cpp @@ -863,6 +863,121 @@ void TrafficNetwork::processReplicatedPatchStatus(uint32_t peerId, json::object replicatePatchStatus(obj, peerId); } +/* Processes a packed subscriber operation proxy response received from an upstream parent. */ + +void TrafficNetwork::processSubscriberOperationProxyResponse(PeerNetwork* peer, const uint8_t* data, uint32_t length, uint32_t peerId) +{ + // validate the upstream peer, packet length, peer ID, and subfunction before parsing the response + const uint32_t packetLength = SUBSCRIBER_OPERATION_HEADER_LENGTH + SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH; + if (peer == nullptr || !peer->isEnabled() || !peer->isReplica() || data == nullptr || length != packetLength || peerId != peer->getPeerId()) { + LogWarning(LOG_MASTER, "Ignoring invalid subscriber operation proxy response"); + return; + } + + // validate the reserved bytes before parsing the response + const uint32_t offset = SUBSCRIBER_OPERATION_HEADER_LENGTH; + if (data[offset + 1U] != 0U || data[offset + 2U] != 0U || data[offset + 3U] != 0U) { + LogWarning(LOG_MASTER, "Ignoring subscriber operation proxy response with nonzero reserved bytes"); + return; + } + + // validate the result and response-found flag before looking up the pending transaction + const uint8_t result = data[offset]; + const uint8_t responseFound = data[offset + 16U]; + if (result > SUBSCRIBER_OPERATION_PROXY_NO_ROUTE || responseFound > 1U || (result == SUBSCRIBER_OPERATION_PROXY_FOUND) != (responseFound == 1U)) { + LogWarning(LOG_MASTER, "Ignoring malformed subscriber operation proxy response result"); + return; + } + + // match the response to a pending transaction by transaction ID and operation ID + const uint32_t transactionId = GET_UINT32(data, offset + 4U); + const uint64_t operationId = GET_UINT64(data, offset + 8U); + SubscriberOperationProxyPending pending{}; + + // scope is intentional + { + std::lock_guard lock(m_subscriberOperationProxyMutex); + auto it = m_subscriberOperationProxyPending.find(transactionId); + if (transactionId == 0U || it == m_subscriberOperationProxyPending.end() || it->second.operationId != operationId) { + LogWarning(LOG_MASTER, "Ignoring unknown subscriber operation proxy response, transactionId = %u", transactionId); + return; + } + + pending = it->second; + m_subscriberOperationProxyPending.erase(it); + } + + // forward the response to the downstream peer when a downstream transaction is pending + if (pending.downstreamTransactionId != 0U) { + uint8_t response[SUBSCRIBER_OPERATION_HEADER_LENGTH + SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH]; + ::memcpy(response, data, sizeof(response)); + + SET_UINT32(pending.downstreamTransactionId, response, offset + 4U); + + writePeer(pending.downstreamPeerId, m_peerId, { NET_FUNC::SUBSCRIBER_OP, NET_SUBFUNC::SUBSCRIBER_OP_PROXY_RESPONSE }, + response, sizeof(response), RTP_END_OF_CALL_SEQ, pending.downstreamStreamId); + return; + } + + // send the response to the downstream console when no downstream transaction is pending + PeerConnectionPtr connection = findPeer(pending.downstreamPeerId); + if (connection == nullptr || !connection->connected() || connection->peerClass() != PEER_CONN_CLASS_CONSOLE) { + return; + } + + json::object response = json::object(); + response["type"].set("operation"); + response["operationId"].set(operationId); + + const bool found = result == SUBSCRIBER_OPERATION_PROXY_FOUND; + response["found"].set(found); + + json::array operations = json::array(); + if (found) { + const SubscriberOperationStore::Protocol protocol = static_cast(data[offset + 17U]); + const SubscriberOperationStore::Kind kind = static_cast(data[offset + 18U]); + const SubscriberOperationStore::State state = static_cast(data[offset + 19U]); + response["protocol"].set(SubscriberOperationStore::protocolName(protocol)); + response["operation"].set(SubscriberOperationStore::kindName(kind)); + response["state"].set(SubscriberOperationStore::stateName(state)); + + const uint32_t targetId = GET_UINT32(data, offset + 20U) + const uint32_t operationPeerId = GET_UINT32(data, offset + 24U) + const uint32_t requestValue = GET_UINT32(data, offset + 28U) + const uint32_t resultValue = GET_UINT32(data, offset + 32U) + response["targetId"].set(targetId); + response["peerId"].set(operationPeerId); + response["requestValue"].set(requestValue); + response["resultValue"].set(resultValue); + + const uint64_t createdAt = GET_UINT64(data, offset + 36U); + const uint64_t completedAt = GET_UINT64(data, offset + 44U); + response["createdAt"].set(createdAt); + response["completedAt"].set(completedAt); + + json::object operation = response; + operation.erase("type"); + operation.erase("found"); + operations.push_back(json::value(operation)); + } else if (result == SUBSCRIBER_OPERATION_PROXY_NO_ROUTE) { + response["error"].set("no upstream route"); + } + + response["operations"].set(operations); + + const std::string payload = json::value(response).serialize(); + if (payload.length() + SUBSCRIBER_OPERATION_HEADER_LENGTH > DATA_PACKET_LENGTH) + return; + + // build the response packet and write it to the peer metadata + uint8_t buffer[DATA_PACKET_LENGTH]; + ::memset(buffer, 0x00U, sizeof(buffer)); + ::memcpy(buffer + SUBSCRIBER_OPERATION_HEADER_LENGTH, payload.data(), payload.length()); + + writePeerCommand(connection->id(), { NET_FUNC::SUBSCRIBER_OP, NET_SUBFUNC::SUBSCRIBER_OP_OPERATION }, + buffer, static_cast(payload.length()) + SUBSCRIBER_OPERATION_HEADER_LENGTH, pending.downstreamStreamId, false); +} + /* Helper to process an downstream peer In-Call Control message. */ void TrafficNetwork::processDownstreamInCallCtrl(network::NET_ICC::ENUM command, network::NET_SUBFUNC::ENUM subFunc, uint32_t dstId, @@ -1850,258 +1965,6 @@ std::vector TrafficNetwork::peerAffiliat return snapshot; } -/* Adds or updates one peer affiliation and publishes the aggregate upstream. */ - -bool TrafficNetwork::updatePeerAffiliation(uint32_t peerId, uint32_t srcId, uint32_t dstId) -{ - std::unordered_map aggregate; - - if (srcId == P25DEF::WUID_FNE) { - LogWarning(LOG_MASTER, "PEER %u ignoring affiliation for FNE source ID", peerId); - return false; - } - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr affiliations = getPeerAffiliations(peerId); - if (affiliations == nullptr) - return false; - - affiliations->groupUnaff(srcId); - affiliations->groupAff(srcId, dstId); - - aggregate = buildAggregatedAffiliations(); - } - - publishAggregatedAffiliations(aggregate); - return true; -} - -/* Removes one peer affiliation and publishes the aggregate upstream. */ - -bool TrafficNetwork::removePeerAffiliation(uint32_t peerId, uint32_t srcId) -{ - std::unordered_map aggregate; - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr affiliations = getPeerAffiliations(peerId); - if (affiliations == nullptr) - return false; - - affiliations->groupUnaff(srcId); - - aggregate = buildAggregatedAffiliations(); - } - - publishAggregatedAffiliations(aggregate); - return true; -} - -/* Replaces one peer's affiliations and publishes the aggregate upstream. */ - -bool TrafficNetwork::replacePeerAffiliations(uint32_t peerId, const std::unordered_map& affiliations) -{ - std::unordered_map aggregate; - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr peerAffiliations = getPeerAffiliations(peerId); - if (peerAffiliations == nullptr) - return false; - - peerAffiliations->replaceGroupAffiliations(affiliations); - - aggregate = buildAggregatedAffiliations(); - } - - publishAggregatedAffiliations(aggregate); - return true; -} - -/* Rebuilds the aggregate downstream affiliation snapshot. */ - -std::unordered_map TrafficNetwork::buildAggregatedAffiliations() -{ - std::vector peerSnapshot = peerAffiliationsSnapshot(); - std::sort(peerSnapshot.begin(), peerSnapshot.end(), [](const PeerAffiliationMapPair& lhs, const PeerAffiliationMapPair& rhs) { - return lhs.first < rhs.first; - }); - - // build the aggregate affiliation map from the peer snapshot - std::unordered_map aggregate; - for (const auto& peer : peerSnapshot) { - if (peer.second == nullptr) - continue; - - // iterate over the peer's group affiliations and merge into the aggregate map - const std::unordered_map affiliations = peer.second->grpAffTable(); - for (const auto& affiliation : affiliations) { - const auto result = aggregate.emplace(affiliation.first, affiliation.second); -/* - if (!result.second && result.first->second != affiliation.second) { - LogWarning(LOG_MASTER, "PEER %u affiliation conflict, srcId = %u, dstId = %u already represented by dstId = %u from a lower peer ID", - peer.first, affiliation.first, affiliation.second, result.first->second); - } -*/ - } - } - - m_globalAff->replaceGroupAffiliations(aggregate); - return aggregate; -} - -/* Publishes an aggregate downstream affiliation snapshot upstream. */ - -void TrafficNetwork::publishAggregatedAffiliations(const std::unordered_map& affiliations) -{ - for (auto& peer : m_host->m_peerNetworks) { - if (peer.second != nullptr && peer.second->isEnabled() && peer.second->isReplica()) - peer.second->announceAffiliationUpdate(affiliations); - } -} - -/* Adds one peer unit registration and publishes the aggregate upstream. */ - -bool TrafficNetwork::updatePeerUnitRegistration(uint32_t peerId, uint32_t srcId, uint32_t ssrc, bool log) -{ - std::vector aggregate; - - if (srcId == P25DEF::WUID_FNE) { - //LogWarning(LOG_MASTER, "PEER %u ignoring unit registration for FNE source ID", peerId); - return false; - } - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr registrations = getPeerAffiliations(peerId); - if (registrations == nullptr) - return false; - - registrations->unitReg(srcId, ssrc, log); - - const uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - m_unitRouteRegistry.update(srcId, peerId, ssrc, false, now); - - aggregate = buildAggregatedUnitRegistrations(); - } - - publishAggregatedUnitRegistrations(aggregate); - return true; -} - -/* Removes one peer unit registration and publishes the aggregate upstream. */ - -bool TrafficNetwork::removePeerUnitRegistration(uint32_t peerId, uint32_t srcId, bool log) -{ - std::vector aggregate; - std::unordered_map aggregateAffiliations; - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr registrations = getPeerAffiliations(peerId); - if (registrations == nullptr) - return false; - - registrations->unitDereg(srcId, false, log); - - m_unitRouteRegistry.erase(srcId, peerId); - - aggregate = buildAggregatedUnitRegistrations(); - aggregateAffiliations = buildAggregatedAffiliations(); - } - - publishAggregatedUnitRegistrations(aggregate); - publishAggregatedAffiliations(aggregateAffiliations); - return true; -} - -/* Replaces one peer's unit registrations and publishes the aggregate upstream. */ - -bool TrafficNetwork::replacePeerUnitRegistrations(uint32_t peerId, const std::vector& registrations, uint32_t ssrc) -{ - std::vector aggregate; - - // scope is intentional - { - std::lock_guard updateLock(m_affiliationUpdateMutex); - std::shared_ptr peerRegistrations = getPeerAffiliations(peerId); - if (peerRegistrations == nullptr) - return false; - - // build a map of source ID to SSRC for the new registrations - std::unordered_map registrationOrigins; - registrationOrigins.reserve(registrations.size()); - for (const uint32_t srcId : registrations) - registrationOrigins[srcId] = ssrc; - - peerRegistrations->replaceUnitRegistrations(registrationOrigins); - - const uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - m_unitRouteRegistry.replacePeer(peerId, registrations, ssrc, now); - - aggregate = buildAggregatedUnitRegistrations(); - } - - publishAggregatedUnitRegistrations(aggregate); - return true; -} - -/* Rebuilds the aggregate downstream unit registration snapshot. */ - -std::vector TrafficNetwork::buildAggregatedUnitRegistrations() -{ - std::vector peerSnapshot = peerAffiliationsSnapshot(); - std::sort(peerSnapshot.begin(), peerSnapshot.end(), [](const PeerAffiliationMapPair& lhs, const PeerAffiliationMapPair& rhs) { - return lhs.first < rhs.first; - }); - - std::unordered_map aggregateOrigins; - for (const auto& peer : peerSnapshot) { - if (peer.second == nullptr) - continue; - - // iterate over the peer's unit registrations and merge into the aggregate map - const std::vector registrations = peer.second->unitRegTable(); - for (const uint32_t srcId : registrations) { - const uint32_t ssrc = peer.second->getSSRCByUnitReg(srcId); - const auto result = aggregateOrigins.emplace(srcId, ssrc); -/* - if (!result.second && result.first->second != ssrc) { - LogWarning(LOG_MASTER, "PEER %u unit registration conflict, srcId = %u already represented by lower peer ID", peer.first, srcId); - } -*/ - } - } - - m_globalAff->replaceUnitRegistrations(aggregateOrigins); - - // build the aggregate list of unit registrations from the origins map - std::vector aggregate; - aggregate.reserve(aggregateOrigins.size()); - for (const auto& registration : aggregateOrigins) - aggregate.push_back(registration.first); - - std::sort(aggregate.begin(), aggregate.end()); - - return aggregate; -} - -/* Publishes an aggregate downstream unit registration snapshot upstream. */ - -void TrafficNetwork::publishAggregatedUnitRegistrations(const std::vector& registrations) -{ - for (auto& peer : m_host->m_peerNetworks) { - if (peer.second != nullptr && peer.second->isEnabled() && peer.second->isReplica()) - peer.second->announceUnitRegUpdate(registrations); - } -} - /* Helper to disconnect a downstream peer. */ void TrafficNetwork::disconnectPeer(uint32_t peerId, const PeerConnectionPtr& connection) diff --git a/src/fne/network/TrafficNetwork.h b/src/fne/network/TrafficNetwork.h index ef0e2da8..2fc02e9c 100644 --- a/src/fne/network/TrafficNetwork.h +++ b/src/fne/network/TrafficNetwork.h @@ -125,6 +125,22 @@ namespace network const int32_t REPEATER_AUTH_HASH_LEN = 32; const int32_t TRANSFER_PCKT_HDR_LEN = 11; + const uint32_t SUBSCRIBER_OPERATION_HEADER_LENGTH = 11U; + const uint32_t SUBSCRIBER_OPERATION_PROXY_REQUEST_LENGTH = 16U; + const uint32_t SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH = 52U; + const uint32_t SUBSCRIBER_OPERATION_PROXY_TIMEOUT_MS = 5000U; + const uint8_t SUBSCRIBER_OPERATION_PROXY_HOP_LIMIT = 16U; + + /** + * @brief Result codes for subscriber operation proxy responses. + * @ingroup fne_network + */ + enum SubscriberOperationProxyResult : uint8_t { + SUBSCRIBER_OPERATION_PROXY_FOUND = 0U, + SUBSCRIBER_OPERATION_PROXY_NOT_FOUND = 1U, + SUBSCRIBER_OPERATION_PROXY_NO_ROUTE = 2U + }; + // --------------------------------------------------------------------------- // Class Prototypes // --------------------------------------------------------------------------- @@ -278,26 +294,6 @@ namespace network */ SubscriberOperationStore& subscriberOperations() { return m_subscriberOperations; } - /** - * @brief Proxies a subscriber operation result query to the upstream replica parent. - * @param downstreamPeerId Console or downstream FNE peer ID awaiting the response. - * @param downstreamStreamId Stream ID supplied by the downstream requestor. - * @param downstreamTransactionId Proxy transaction ID supplied by a downstream FNE, or zero for a console. - * @param operationId Subscriber operation ID to query. - * @param hopLimit Number of additional upstream hops permitted. - * @returns True if the request was forwarded, otherwise false. - */ - bool proxySubscriberOperation(uint32_t downstreamPeerId, uint32_t downstreamStreamId, uint32_t downstreamTransactionId, - uint64_t operationId, uint8_t hopLimit); - /** - * @brief Processes a packed subscriber operation proxy response received from an upstream parent. - * @param peer Upstream peer network that supplied the response. - * @param data Packed response buffer. - * @param length Length of the packed response buffer. - * @param peerId Destination peer ID carried in the response. - */ - void processSubscriberOperationProxyResponse(PeerNetwork* peer, const uint8_t* data, uint32_t length, uint32_t peerId); - /** * @brief Sets the instances of the Radio ID, Radio Alias, Talkgroup ID, Peer List, and Crypto lookup tables. * @param ridLookup Radio ID Lookup Table Instance @@ -331,7 +327,14 @@ namespace network * @param obj Patch status JSON payload. */ void processReplicatedPatchStatus(uint32_t peerId, json::object obj); - + /** + * @brief Processes a packed subscriber operation proxy response received from an upstream parent. + * @param peer Upstream peer network that supplied the response. + * @param data Packed response buffer. + * @param length Length of the packed response buffer. + * @param peerId Destination peer ID carried in the response. + */ + void processSubscriberOperationProxyResponse(PeerNetwork* peer, const uint8_t* data, uint32_t length, uint32_t peerId); /** * @brief Helper to process an downstream peer In-Call Control message. * @param command In-Call Control Command. @@ -896,75 +899,7 @@ namespace network * @returns std::vector Snapshot of peer affiliation entries. */ std::vector peerAffiliationsSnapshot() const; - /** - * @brief Adds or updates one peer affiliation and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param srcId Source Radio ID. - * @param dstId Talkgroup ID. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool updatePeerAffiliation(uint32_t peerId, uint32_t srcId, uint32_t dstId); - /** - * @brief Removes one peer affiliation and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param srcId Source Radio ID. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool removePeerAffiliation(uint32_t peerId, uint32_t srcId); - /** - * @brief Replaces one peer's affiliations and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param affiliations Complete source-to-talkgroup affiliation snapshot. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool replacePeerAffiliations(uint32_t peerId, const std::unordered_map& affiliations); - /** - * @brief Rebuilds the aggregate downstream affiliation snapshot. - * @note The caller must hold m_affiliationUpdateMutex. - * @returns std::unordered_map Aggregate affiliation snapshot. - */ - std::unordered_map buildAggregatedAffiliations(); - /** - * @brief Publishes an aggregate downstream affiliation snapshot upstream. - * @param affiliations Complete aggregate source-to-talkgroup affiliation snapshot. - */ - void publishAggregatedAffiliations(const std::unordered_map& affiliations); - /** - * @brief Adds one peer unit registration and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param srcId Source Radio ID. - * @param ssrc Originating peer ID. - * @param log Flag indicating whether the registration should be logged. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool updatePeerUnitRegistration(uint32_t peerId, uint32_t srcId, uint32_t ssrc, bool log = true); - /** - * @brief Removes one peer unit registration and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param srcId Source Radio ID. - * @param log Flag indicating whether the deregistration should be logged. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool removePeerUnitRegistration(uint32_t peerId, uint32_t srcId, bool log = true); - /** - * @brief Replaces one peer's unit registrations and publishes the aggregate upstream. - * @param peerId Peer ID. - * @param registrations Complete source ID registration snapshot. - * @param ssrc Originating peer ID. - * @returns bool True, if the peer affiliation lookup exists, otherwise false. - */ - bool replacePeerUnitRegistrations(uint32_t peerId, const std::vector& registrations, uint32_t ssrc); - /** - * @brief Rebuilds the aggregate downstream unit registration snapshot. - * @note The caller must hold m_affiliationUpdateMutex. - * @returns std::vector Aggregate unit registration snapshot. - */ - std::vector buildAggregatedUnitRegistrations(); - /** - * @brief Publishes an aggregate downstream unit registration snapshot upstream. - * @param registrations Complete aggregate source ID registration snapshot. - */ - void publishAggregatedUnitRegistrations(const std::vector& registrations); + /** * @brief Helper to disconnect a downstream peer. * @param peerId Peer ID. @@ -1403,6 +1338,96 @@ namespace network */ void processKEKResponse(uint32_t srcId, p25::kmm::KeyItem* ki, uint8_t algId, uint8_t keyLength); + /* + ** NET_FUNC::ANNOUNCE Helper Functions (packet/Announce.cpp) + */ + + /** + * @brief Adds or updates one peer affiliation and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param srcId Source Radio ID. + * @param dstId Talkgroup ID. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool updatePeerAffiliation(uint32_t peerId, uint32_t srcId, uint32_t dstId); + /** + * @brief Removes one peer affiliation and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param srcId Source Radio ID. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool removePeerAffiliation(uint32_t peerId, uint32_t srcId); + /** + * @brief Replaces one peer's affiliations and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param affiliations Complete source-to-talkgroup affiliation snapshot. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool replacePeerAffiliations(uint32_t peerId, const std::unordered_map& affiliations); + /** + * @brief Rebuilds the aggregate downstream affiliation snapshot. + * @note The caller must hold m_affiliationUpdateMutex. + * @returns std::unordered_map Aggregate affiliation snapshot. + */ + std::unordered_map buildAggregatedAffiliations(); + /** + * @brief Publishes an aggregate downstream affiliation snapshot upstream. + * @param affiliations Complete aggregate source-to-talkgroup affiliation snapshot. + */ + void publishAggregatedAffiliations(const std::unordered_map& affiliations); + /** + * @brief Adds one peer unit registration and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param srcId Source Radio ID. + * @param ssrc Originating peer ID. + * @param log Flag indicating whether the registration should be logged. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool updatePeerUnitRegistration(uint32_t peerId, uint32_t srcId, uint32_t ssrc, bool log = true); + /** + * @brief Removes one peer unit registration and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param srcId Source Radio ID. + * @param log Flag indicating whether the deregistration should be logged. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool removePeerUnitRegistration(uint32_t peerId, uint32_t srcId, bool log = true); + /** + * @brief Replaces one peer's unit registrations and publishes the aggregate upstream. + * @param peerId Peer ID. + * @param registrations Complete source ID registration snapshot. + * @param ssrc Originating peer ID. + * @returns bool True, if the peer affiliation lookup exists, otherwise false. + */ + bool replacePeerUnitRegistrations(uint32_t peerId, const std::vector& registrations, uint32_t ssrc); + /** + * @brief Rebuilds the aggregate downstream unit registration snapshot. + * @note The caller must hold m_affiliationUpdateMutex. + * @returns std::vector Aggregate unit registration snapshot. + */ + std::vector buildAggregatedUnitRegistrations(); + /** + * @brief Publishes an aggregate downstream unit registration snapshot upstream. + * @param registrations Complete aggregate source ID registration snapshot. + */ + void publishAggregatedUnitRegistrations(const std::vector& registrations); + + /* + ** NET_FUNC::SUBSCRIBER_OP Helper Functions (packet/SubscriberOperation.cpp) + */ + + /** + * @brief Proxies a subscriber operation result query to the upstream replica parent. + * @param downstreamPeerId Console or downstream FNE peer ID awaiting the response. + * @param downstreamStreamId Stream ID supplied by the downstream requestor. + * @param downstreamTransactionId Proxy transaction ID supplied by a downstream FNE, or zero for a console. + * @param operationId Subscriber operation ID to query. + * @param hopLimit Number of additional upstream hops permitted. + * @returns True if the request was forwarded, otherwise false. + */ + bool proxySubscriberOperation(uint32_t downstreamPeerId, uint32_t downstreamStreamId, uint32_t downstreamTransactionId, + uint64_t operationId, uint8_t hopLimit); + /* ** Metrics Helpers */ diff --git a/src/fne/network/packet/Announce.cpp b/src/fne/network/packet/Announce.cpp index 816f3273..25bfd8b4 100644 --- a/src/fne/network/packet/Announce.cpp +++ b/src/fne/network/packet/Announce.cpp @@ -18,9 +18,262 @@ using namespace network; // Public Class Members // --------------------------------------------------------------------------- +/* Adds or updates one peer affiliation and publishes the aggregate upstream. */ + +bool TrafficNetwork::updatePeerAffiliation(uint32_t peerId, uint32_t srcId, uint32_t dstId) +{ + std::unordered_map aggregate; + + if (srcId == P25DEF::WUID_FNE) { + LogWarning(LOG_MASTER, "PEER %u ignoring affiliation for FNE source ID", peerId); + return false; + } + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr affiliations = getPeerAffiliations(peerId); + if (affiliations == nullptr) + return false; + + affiliations->groupUnaff(srcId); + affiliations->groupAff(srcId, dstId); + + aggregate = buildAggregatedAffiliations(); + } + + publishAggregatedAffiliations(aggregate); + return true; +} + +/* Removes one peer affiliation and publishes the aggregate upstream. */ + +bool TrafficNetwork::removePeerAffiliation(uint32_t peerId, uint32_t srcId) +{ + std::unordered_map aggregate; + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr affiliations = getPeerAffiliations(peerId); + if (affiliations == nullptr) + return false; + + affiliations->groupUnaff(srcId); + + aggregate = buildAggregatedAffiliations(); + } + + publishAggregatedAffiliations(aggregate); + return true; +} + +/* Replaces one peer's affiliations and publishes the aggregate upstream. */ + +bool TrafficNetwork::replacePeerAffiliations(uint32_t peerId, const std::unordered_map& affiliations) +{ + std::unordered_map aggregate; + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr peerAffiliations = getPeerAffiliations(peerId); + if (peerAffiliations == nullptr) + return false; + + peerAffiliations->replaceGroupAffiliations(affiliations); + + aggregate = buildAggregatedAffiliations(); + } + + publishAggregatedAffiliations(aggregate); + return true; +} + +/* Rebuilds the aggregate downstream affiliation snapshot. */ + +std::unordered_map TrafficNetwork::buildAggregatedAffiliations() +{ + std::vector peerSnapshot = peerAffiliationsSnapshot(); + std::sort(peerSnapshot.begin(), peerSnapshot.end(), [](const PeerAffiliationMapPair& lhs, const PeerAffiliationMapPair& rhs) { + return lhs.first < rhs.first; + }); + + // build the aggregate affiliation map from the peer snapshot + std::unordered_map aggregate; + for (const auto& peer : peerSnapshot) { + if (peer.second == nullptr) + continue; + + // iterate over the peer's group affiliations and merge into the aggregate map + const std::unordered_map affiliations = peer.second->grpAffTable(); + for (const auto& affiliation : affiliations) { + const auto result = aggregate.emplace(affiliation.first, affiliation.second); +/* + if (!result.second && result.first->second != affiliation.second) { + LogWarning(LOG_MASTER, "PEER %u affiliation conflict, srcId = %u, dstId = %u already represented by dstId = %u from a lower peer ID", + peer.first, affiliation.first, affiliation.second, result.first->second); + } +*/ + } + } + + m_globalAff->replaceGroupAffiliations(aggregate); + return aggregate; +} + +/* Publishes an aggregate downstream affiliation snapshot upstream. */ + +void TrafficNetwork::publishAggregatedAffiliations(const std::unordered_map& affiliations) +{ + for (auto& peer : m_host->m_peerNetworks) { + if (peer.second != nullptr && peer.second->isEnabled() && peer.second->isReplica()) + peer.second->announceAffiliationUpdate(affiliations); + } +} + +/* Adds one peer unit registration and publishes the aggregate upstream. */ + +bool TrafficNetwork::updatePeerUnitRegistration(uint32_t peerId, uint32_t srcId, uint32_t ssrc, bool log) +{ + std::vector aggregate; + + if (srcId == P25DEF::WUID_FNE) { + //LogWarning(LOG_MASTER, "PEER %u ignoring unit registration for FNE source ID", peerId); + return false; + } + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr registrations = getPeerAffiliations(peerId); + if (registrations == nullptr) + return false; + + registrations->unitReg(srcId, ssrc, log); + + const uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + m_unitRouteRegistry.update(srcId, peerId, ssrc, false, now); + + aggregate = buildAggregatedUnitRegistrations(); + } + + publishAggregatedUnitRegistrations(aggregate); + return true; +} + +/* Removes one peer unit registration and publishes the aggregate upstream. */ + +bool TrafficNetwork::removePeerUnitRegistration(uint32_t peerId, uint32_t srcId, bool log) +{ + std::vector aggregate; + std::unordered_map aggregateAffiliations; + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr registrations = getPeerAffiliations(peerId); + if (registrations == nullptr) + return false; + + registrations->unitDereg(srcId, false, log); + + m_unitRouteRegistry.erase(srcId, peerId); + + aggregate = buildAggregatedUnitRegistrations(); + aggregateAffiliations = buildAggregatedAffiliations(); + } + + publishAggregatedUnitRegistrations(aggregate); + publishAggregatedAffiliations(aggregateAffiliations); + return true; +} + +/* Replaces one peer's unit registrations and publishes the aggregate upstream. */ + +bool TrafficNetwork::replacePeerUnitRegistrations(uint32_t peerId, const std::vector& registrations, uint32_t ssrc) +{ + std::vector aggregate; + + // scope is intentional + { + std::lock_guard updateLock(m_affiliationUpdateMutex); + std::shared_ptr peerRegistrations = getPeerAffiliations(peerId); + if (peerRegistrations == nullptr) + return false; + + // build a map of source ID to SSRC for the new registrations + std::unordered_map registrationOrigins; + registrationOrigins.reserve(registrations.size()); + for (const uint32_t srcId : registrations) + registrationOrigins[srcId] = ssrc; + + peerRegistrations->replaceUnitRegistrations(registrationOrigins); + + const uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + m_unitRouteRegistry.replacePeer(peerId, registrations, ssrc, now); + + aggregate = buildAggregatedUnitRegistrations(); + } + + publishAggregatedUnitRegistrations(aggregate); + return true; +} + +/* Rebuilds the aggregate downstream unit registration snapshot. */ + +std::vector TrafficNetwork::buildAggregatedUnitRegistrations() +{ + std::vector peerSnapshot = peerAffiliationsSnapshot(); + std::sort(peerSnapshot.begin(), peerSnapshot.end(), [](const PeerAffiliationMapPair& lhs, const PeerAffiliationMapPair& rhs) { + return lhs.first < rhs.first; + }); + + std::unordered_map aggregateOrigins; + for (const auto& peer : peerSnapshot) { + if (peer.second == nullptr) + continue; + + // iterate over the peer's unit registrations and merge into the aggregate map + const std::vector registrations = peer.second->unitRegTable(); + for (const uint32_t srcId : registrations) { + const uint32_t ssrc = peer.second->getSSRCByUnitReg(srcId); + const auto result = aggregateOrigins.emplace(srcId, ssrc); +/* + if (!result.second && result.first->second != ssrc) { + LogWarning(LOG_MASTER, "PEER %u unit registration conflict, srcId = %u already represented by lower peer ID", peer.first, srcId); + } +*/ + } + } + + m_globalAff->replaceUnitRegistrations(aggregateOrigins); + + // build the aggregate list of unit registrations from the origins map + std::vector aggregate; + aggregate.reserve(aggregateOrigins.size()); + for (const auto& registration : aggregateOrigins) + aggregate.push_back(registration.first); + + std::sort(aggregate.begin(), aggregate.end()); + + return aggregate; +} + +/* Publishes an aggregate downstream unit registration snapshot upstream. */ + +void TrafficNetwork::publishAggregatedUnitRegistrations(const std::vector& registrations) +{ + for (auto& peer : m_host->m_peerNetworks) { + if (peer.second != nullptr && peer.second->isEnabled() && peer.second->isReplica()) + peer.second->announceUnitRegUpdate(registrations); + } +} + /* Handles NET_FUNC::ANNOUNCE packets. */ -void MetadataNetwork::PacketHandler::announce(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::announce(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)mdNetwork; diff --git a/src/fne/network/packet/GrantRequest.cpp b/src/fne/network/packet/GrantRequest.cpp index 791accb3..3c644235 100644 --- a/src/fne/network/packet/GrantRequest.cpp +++ b/src/fne/network/packet/GrantRequest.cpp @@ -24,7 +24,8 @@ using namespace network::callhandler; /* Handles NET_FUNC::GRANT_REQ packets. */ -void TrafficNetwork::PacketHandler::grantRequest(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::grantRequest(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { TrafficNetwork::PeerConnectionPtr connection = network->findPeer(peerId); if (peerId > 0 && connection != nullptr) { diff --git a/src/fne/network/packet/InCallControl.cpp b/src/fne/network/packet/InCallControl.cpp index fb664d1a..2d2d5a6f 100644 --- a/src/fne/network/packet/InCallControl.cpp +++ b/src/fne/network/packet/InCallControl.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::INCALL_CTRL packets. */ -void TrafficNetwork::PacketHandler::inCallControl(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::inCallControl(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { if (network->m_disallowInCallCtrl) return; diff --git a/src/fne/network/packet/KEKKeyRequest.cpp b/src/fne/network/packet/KEKKeyRequest.cpp index 63e91ae5..da2c36d6 100644 --- a/src/fne/network/packet/KEKKeyRequest.cpp +++ b/src/fne/network/packet/KEKKeyRequest.cpp @@ -22,7 +22,8 @@ using namespace network; /* Handles NET_FUNC::KEY_KEK_REQ packets. */ -void TrafficNetwork::PacketHandler::kekKeyRequest(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::kekKeyRequest(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { using namespace p25::defines; using namespace p25::kmm; diff --git a/src/fne/network/packet/KeyRequest.cpp b/src/fne/network/packet/KeyRequest.cpp index 09e29d85..da986242 100644 --- a/src/fne/network/packet/KeyRequest.cpp +++ b/src/fne/network/packet/KeyRequest.cpp @@ -22,7 +22,8 @@ using namespace network; /* Handles NET_FUNC::KEY_REQ packets. */ -void TrafficNetwork::PacketHandler::keyRequest(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::keyRequest(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { using namespace p25::defines; using namespace p25::kmm; diff --git a/src/fne/network/packet/KeysInventory.cpp b/src/fne/network/packet/KeysInventory.cpp index d661eaf6..7ccbbbfb 100644 --- a/src/fne/network/packet/KeysInventory.cpp +++ b/src/fne/network/packet/KeysInventory.cpp @@ -23,7 +23,8 @@ using namespace network; /* Handles NET_FUNC::KEYS_INVENTORY packets. */ -void MetadataNetwork::PacketHandler::keysInventory(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::keysInventory(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)ssrc; diff --git a/src/fne/network/packet/KeysUpdate.cpp b/src/fne/network/packet/KeysUpdate.cpp index 06537f05..97ece918 100644 --- a/src/fne/network/packet/KeysUpdate.cpp +++ b/src/fne/network/packet/KeysUpdate.cpp @@ -37,7 +37,8 @@ std::mutex s_keyUpdateCommitMutex; /* Handles NET_FUNC::KEYS_UPDATE packets. */ -void MetadataNetwork::PacketHandler::keysUpdate(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::keysUpdate(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)ssrc; diff --git a/src/fne/network/packet/NetworkTree.cpp b/src/fne/network/packet/NetworkTree.cpp index 4286a8d4..559eaef7 100644 --- a/src/fne/network/packet/NetworkTree.cpp +++ b/src/fne/network/packet/NetworkTree.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::NET_TREE packets. */ -void MetadataNetwork::PacketHandler::networkTree(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::networkTree(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)ssrc; diff --git a/src/fne/network/packet/Ping.cpp b/src/fne/network/packet/Ping.cpp index ae8bd8ad..02c06c2c 100644 --- a/src/fne/network/packet/Ping.cpp +++ b/src/fne/network/packet/Ping.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::PING packets. */ -void TrafficNetwork::PacketHandler::ping(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::ping(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { TrafficNetwork::PeerConnectionPtr connection = network->findPeer(peerId); if (peerId > 0 && connection != nullptr) { diff --git a/src/fne/network/packet/Protocol.cpp b/src/fne/network/packet/Protocol.cpp index 2378d253..5e96b77d 100644 --- a/src/fne/network/packet/Protocol.cpp +++ b/src/fne/network/packet/Protocol.cpp @@ -27,7 +27,8 @@ using namespace network::callhandler; /* Handles NET_FUNC::PROTOCOL packets. */ -void TrafficNetwork::PacketHandler::protocol(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::protocol(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { // process incoming message subfunction opcodes switch (req->fneHeader.getSubFunction()) { diff --git a/src/fne/network/packet/RadioAliasSync.cpp b/src/fne/network/packet/RadioAliasSync.cpp index 40b4a9a2..0bdc100c 100644 --- a/src/fne/network/packet/RadioAliasSync.cpp +++ b/src/fne/network/packet/RadioAliasSync.cpp @@ -22,7 +22,8 @@ using namespace network; /* Handles NET_FUNC::RADIO_ALIAS_SYNC packets. */ -void MetadataNetwork::PacketHandler::radioAliasSync(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::radioAliasSync(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)mdNetwork; diff --git a/src/fne/network/packet/RepeaterAuth.cpp b/src/fne/network/packet/RepeaterAuth.cpp index b6269766..5a96a9d6 100644 --- a/src/fne/network/packet/RepeaterAuth.cpp +++ b/src/fne/network/packet/RepeaterAuth.cpp @@ -21,7 +21,8 @@ using namespace network; /* Handles NET_FUNC::RPTK packets. */ -void TrafficNetwork::PacketHandler::repeaterAuth(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::repeaterAuth(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { // validate the incoming packet length for a repeater authentication packet if (req == nullptr || req->buffer == nullptr || !TrafficNetwork::validRepeaterAuthLength(req->length)) { diff --git a/src/fne/network/packet/RepeaterConfig.cpp b/src/fne/network/packet/RepeaterConfig.cpp index 7f6bd922..05d3e6c3 100644 --- a/src/fne/network/packet/RepeaterConfig.cpp +++ b/src/fne/network/packet/RepeaterConfig.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::RPTC packets. */ -void TrafficNetwork::PacketHandler::repeaterConfig(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::repeaterConfig(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { // validate the incoming packet length for a repeater configuration packet if (req == nullptr || req->buffer == nullptr || !TrafficNetwork::validRepeaterConfigLength(req->length)) { diff --git a/src/fne/network/packet/RepeaterDisconnect.cpp b/src/fne/network/packet/RepeaterDisconnect.cpp index bf0b9343..837ac6e8 100644 --- a/src/fne/network/packet/RepeaterDisconnect.cpp +++ b/src/fne/network/packet/RepeaterDisconnect.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::RPT_DISC packets. */ -void TrafficNetwork::PacketHandler::repeaterDisconnect(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::repeaterDisconnect(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { if (peerId > 0) { std::lock_guard peerGuard(TrafficNetwork::getPeerStateLock(peerId)); diff --git a/src/fne/network/packet/RepeaterLogin.cpp b/src/fne/network/packet/RepeaterLogin.cpp index 207d5566..8de066ac 100644 --- a/src/fne/network/packet/RepeaterLogin.cpp +++ b/src/fne/network/packet/RepeaterLogin.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::RPTL packets. */ -void TrafficNetwork::PacketHandler::repeaterLogin(TrafficNetwork* network, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) +void TrafficNetwork::PacketHandler::repeaterLogin(TrafficNetwork* network, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId, uint64_t now) { std::lock_guard peerGuard(TrafficNetwork::getPeerStateLock(peerId)); diff --git a/src/fne/network/packet/Replication.cpp b/src/fne/network/packet/Replication.cpp index 6e72fe4c..28a30556 100644 --- a/src/fne/network/packet/Replication.cpp +++ b/src/fne/network/packet/Replication.cpp @@ -20,7 +20,8 @@ using namespace network; /* Handles NET_FUNC::REPL packets. */ -void MetadataNetwork::PacketHandler::replication(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::replication(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)ssrc; diff --git a/src/fne/network/packet/SubscriberOperation.cpp b/src/fne/network/packet/SubscriberOperation.cpp index 7f3d06af..b3016c7c 100644 --- a/src/fne/network/packet/SubscriberOperation.cpp +++ b/src/fne/network/packet/SubscriberOperation.cpp @@ -3,6 +3,9 @@ * Digital Voice Modem - Converged FNE Software * GPLv2 Open Source. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright (C) 2026 Bryan Biedenkapp, N2PLL + * */ /** * @file SubscriberOperation.cpp @@ -30,22 +33,6 @@ using namespace network; #define SOP_P25 "p25" #define SOP_NXDN "nxdn" -const uint32_t SUBSCRIBER_OPERATION_HEADER_LENGTH = 11U; -const uint32_t SUBSCRIBER_OPERATION_PROXY_REQUEST_LENGTH = 16U; -const uint32_t SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH = 52U; -const uint32_t SUBSCRIBER_OPERATION_PROXY_TIMEOUT_MS = 5000U; -const uint8_t SUBSCRIBER_OPERATION_PROXY_HOP_LIMIT = 16U; - -/** - * @brief Result codes for subscriber operation proxy responses. - * @ingroup fne_network - */ -enum SubscriberOperationProxyResult : uint8_t { - SUBSCRIBER_OPERATION_PROXY_FOUND = 0U, - SUBSCRIBER_OPERATION_PROXY_NOT_FOUND = 1U, - SUBSCRIBER_OPERATION_PROXY_NO_ROUTE = 2U -}; - // --------------------------------------------------------------------------- // Global Functions // --------------------------------------------------------------------------- @@ -76,6 +63,10 @@ static void encodeProxyResponse(uint8_t* buffer, uint8_t result, uint32_t transa SET_UINT64(operation->completedAt, buffer, offset + 44U); // Completed At } +// --------------------------------------------------------------------------- +// Public Class Members +// --------------------------------------------------------------------------- + /* Proxies a subscriber operation result query to the upstream replica parent. */ bool TrafficNetwork::proxySubscriberOperation(uint32_t downstreamPeerId, uint32_t downstreamStreamId, uint32_t downstreamTransactionId, @@ -149,130 +140,10 @@ bool TrafficNetwork::proxySubscriberOperation(uint32_t downstreamPeerId, uint32_ return written; } -/* Processes a packed subscriber operation proxy response received from an upstream parent. */ - -void TrafficNetwork::processSubscriberOperationProxyResponse(PeerNetwork* peer, const uint8_t* data, uint32_t length, uint32_t peerId) -{ - // validate the upstream peer, packet length, peer ID, and subfunction before parsing the response - const uint32_t packetLength = SUBSCRIBER_OPERATION_HEADER_LENGTH + SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH; - if (peer == nullptr || !peer->isEnabled() || !peer->isReplica() || data == nullptr || length != packetLength || peerId != peer->getPeerId()) { - LogWarning(LOG_MASTER, "Ignoring invalid subscriber operation proxy response"); - return; - } - - // validate the reserved bytes before parsing the response - const uint32_t offset = SUBSCRIBER_OPERATION_HEADER_LENGTH; - if (data[offset + 1U] != 0U || data[offset + 2U] != 0U || data[offset + 3U] != 0U) { - LogWarning(LOG_MASTER, "Ignoring subscriber operation proxy response with nonzero reserved bytes"); - return; - } - - // validate the result and response-found flag before looking up the pending transaction - const uint8_t result = data[offset]; - const uint8_t responseFound = data[offset + 16U]; - if (result > SUBSCRIBER_OPERATION_PROXY_NO_ROUTE || responseFound > 1U || (result == SUBSCRIBER_OPERATION_PROXY_FOUND) != (responseFound == 1U)) { - LogWarning(LOG_MASTER, "Ignoring malformed subscriber operation proxy response result"); - return; - } - - // match the response to a pending transaction by transaction ID and operation ID - const uint32_t transactionId = GET_UINT32(data, offset + 4U); - const uint64_t operationId = GET_UINT64(data, offset + 8U); - SubscriberOperationProxyPending pending{}; - - // scope is intentional - { - std::lock_guard lock(m_subscriberOperationProxyMutex); - auto it = m_subscriberOperationProxyPending.find(transactionId); - if (transactionId == 0U || it == m_subscriberOperationProxyPending.end() || it->second.operationId != operationId) { - LogWarning(LOG_MASTER, "Ignoring unknown subscriber operation proxy response, transactionId = %u", transactionId); - return; - } - - pending = it->second; - m_subscriberOperationProxyPending.erase(it); - } - - // forward the response to the downstream peer when a downstream transaction is pending - if (pending.downstreamTransactionId != 0U) { - uint8_t response[SUBSCRIBER_OPERATION_HEADER_LENGTH + SUBSCRIBER_OPERATION_PROXY_RESPONSE_LENGTH]; - ::memcpy(response, data, sizeof(response)); - - SET_UINT32(pending.downstreamTransactionId, response, offset + 4U); - - writePeer(pending.downstreamPeerId, m_peerId, { NET_FUNC::SUBSCRIBER_OP, NET_SUBFUNC::SUBSCRIBER_OP_PROXY_RESPONSE }, - response, sizeof(response), RTP_END_OF_CALL_SEQ, pending.downstreamStreamId); - return; - } - - // send the response to the downstream console when no downstream transaction is pending - PeerConnectionPtr connection = findPeer(pending.downstreamPeerId); - if (connection == nullptr || !connection->connected() || connection->peerClass() != PEER_CONN_CLASS_CONSOLE) { - return; - } - - json::object response = json::object(); - response["type"].set("operation"); - response["operationId"].set(operationId); - - const bool found = result == SUBSCRIBER_OPERATION_PROXY_FOUND; - response["found"].set(found); - - json::array operations = json::array(); - if (found) { - const SubscriberOperationStore::Protocol protocol = static_cast(data[offset + 17U]); - const SubscriberOperationStore::Kind kind = static_cast(data[offset + 18U]); - const SubscriberOperationStore::State state = static_cast(data[offset + 19U]); - response["protocol"].set(SubscriberOperationStore::protocolName(protocol)); - response["operation"].set(SubscriberOperationStore::kindName(kind)); - response["state"].set(SubscriberOperationStore::stateName(state)); - - const uint32_t targetId = GET_UINT32(data, offset + 20U) - const uint32_t operationPeerId = GET_UINT32(data, offset + 24U) - const uint32_t requestValue = GET_UINT32(data, offset + 28U) - const uint32_t resultValue = GET_UINT32(data, offset + 32U) - response["targetId"].set(targetId); - response["peerId"].set(operationPeerId); - response["requestValue"].set(requestValue); - response["resultValue"].set(resultValue); - - const uint64_t createdAt = GET_UINT64(data, offset + 36U); - const uint64_t completedAt = GET_UINT64(data, offset + 44U); - response["createdAt"].set(createdAt); - response["completedAt"].set(completedAt); - - json::object operation = response; - operation.erase("type"); - operation.erase("found"); - operations.push_back(json::value(operation)); - } else if (result == SUBSCRIBER_OPERATION_PROXY_NO_ROUTE) { - response["error"].set("no upstream route"); - } - - response["operations"].set(operations); - - const std::string payload = json::value(response).serialize(); - if (payload.length() + SUBSCRIBER_OPERATION_HEADER_LENGTH > DATA_PACKET_LENGTH) - return; - - // build the response packet and write it to the peer metadata - uint8_t buffer[DATA_PACKET_LENGTH]; - ::memset(buffer, 0x00U, sizeof(buffer)); - ::memcpy(buffer + SUBSCRIBER_OPERATION_HEADER_LENGTH, payload.data(), payload.length()); - - writePeerCommand(connection->id(), { NET_FUNC::SUBSCRIBER_OP, NET_SUBFUNC::SUBSCRIBER_OP_OPERATION }, - buffer, static_cast(payload.length()) + SUBSCRIBER_OPERATION_HEADER_LENGTH, pending.downstreamStreamId, false); -} - -// --------------------------------------------------------------------------- -// Public Class Members -// --------------------------------------------------------------------------- - /* Handles NET_FUNC::SUBSCRIBER_OP packets. */ -void MetadataNetwork::PacketHandler::subscriberOperation(TrafficNetwork* network, - MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, - uint32_t streamId) +void MetadataNetwork::PacketHandler::subscriberOperation(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)ssrc; diff --git a/src/fne/network/packet/Transfer.cpp b/src/fne/network/packet/Transfer.cpp index 16cf7b46..3c8ef4cd 100644 --- a/src/fne/network/packet/Transfer.cpp +++ b/src/fne/network/packet/Transfer.cpp @@ -21,7 +21,8 @@ using namespace network; /* Handles NET_FUNC::TRANSFER packets. */ -void MetadataNetwork::PacketHandler::transfer(TrafficNetwork* network, MetadataNetwork* mdNetwork, NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) +void MetadataNetwork::PacketHandler::transfer(TrafficNetwork* network, MetadataNetwork* mdNetwork, + NetPacketRequest* req, uint32_t peerId, uint32_t ssrc, uint32_t streamId) { (void)mdNetwork; (void)ssrc;