From 86dd4c2ec3d838df33dc57a1b255ba5192fdbbd5 Mon Sep 17 00:00:00 2001 From: Syed Maroof Date: Wed, 18 Nov 2026 23:42:40 -0500 Subject: [PATCH] ICE: avoid heap allocation per TURN Data Indication The TURN Data Indication handler built a CUtlVector of every STUN attribute just to find the XOR-PEER-ADDRESS and DATA attributes, which allocates on every relayed packet. Walk the attributes directly instead, capturing the first match of each, and stop at the same early-out points ParseSTUNAttributes uses when called without a key (bad fingerprint, or any message-integrity attribute). The result is unchanged; the other vector-based call sites are untouched. --- .../steamnetworkingsockets_ice_client.cpp | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.cpp b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.cpp index e5a48147..f2a4ddb3 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.cpp +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_ice_client.cpp @@ -1633,12 +1633,48 @@ void CSteamNetworkingICESession::OnPacketReceived( const RecvPktInfo_t &info, IC ++TEST_ICE_ctr_data_ind_recv; - // TODO: avoid heap allocation per packet - CUtlVector vecAttrs; - ParseSTUNAttributes( info, nullptr, 0, &vecAttrs ); + // Find the first XOR-PEER-ADDRESS and DATA attributes without building a vector. + // The walk mirrors ParseSTUNAttributes( info, nullptr, 0, ... ): same decode, and it + // ends at the same early-out points, so the attributes found are the same. + STUNAttribute peerAttr, dataAttr; + const STUNAttribute *pPeerAttr = nullptr; + const STUNAttribute *pDataAttr = nullptr; + { + const uint32 * const pMessage = reinterpret_cast< const uint32* >( info.m_pPkt ); + const uint32 * const pMessageEnd = pMessage + info.m_cbPkt / 4; + const uint32 *pAttrPtr = &pMessage[5]; + while ( pAttrPtr < pMessageEnd && !( pPeerAttr && pDataAttr ) ) + { + STUNAttribute attr; + const uint32 * const pThisAttrPtr = pAttrPtr; + pAttrPtr = DecodeSTUNAttribute( pAttrPtr, pMessageEnd, &attr ); + if ( pAttrPtr == nullptr ) + break; + if ( !pPeerAttr && attr.m_nType == k_nTURN_Attr_XORPeerAddress ) + { + peerAttr = attr; + pPeerAttr = &peerAttr; + } + if ( !pDataAttr && attr.m_nType == k_nTURN_Attr_Data ) + { + dataAttr = attr; + pDataAttr = &dataAttr; + } + + // Same early-outs as ParseSTUNAttributes with no key: a bad fingerprint, or any + // message-integrity attribute (which cannot be verified here), ends the walk. + if ( attr.m_nType == k_nSTUN_Attr_Fingerprint ) + { + if ( attr.m_nLength != 4 || ( ntohl( attr.m_pData[0] ) ^ 0x5354554e ) != CRC32( reinterpret_cast( pMessage ), uint32( pThisAttrPtr - pMessage ) * 4 ) ) + break; + } + else if ( attr.m_nType == k_nSTUN_Attr_MessageIntegrity || attr.m_nType == k_nSTUN_Attr_MessageIntegrity_SHA256 ) + { + break; + } + } + } - const STUNAttribute *pPeerAttr = FindAttributeOfType( vecAttrs.Base(), vecAttrs.Count(), k_nTURN_Attr_XORPeerAddress ); - const STUNAttribute *pDataAttr = FindAttributeOfType( vecAttrs.Base(), vecAttrs.Count(), k_nTURN_Attr_Data ); if ( pPeerAttr == nullptr || pDataAttr == nullptr ) { SpewVerboseGroup( GlobalConfig::LogLevel_P2PRendezvous.Get(), "ICE: Dropping Data Indication from TURN: missing peer-address or data attribute.\n" );