From 9a006f4e2721c2e83925cb1983fd73cf0dc9df95 Mon Sep 17 00:00:00 2001 From: Fletcher Dunn Date: Wed, 29 Apr 2020 10:36:42 -0700 Subject: [PATCH] Added SteamNetConnectionInfo_t::m_eTransportKind Derive TransportPopulateConnectionInfo on all the transports to populate this field. The ICE transport (the one we actually care about the most) is unfortunately not fully plumbed up so that we know how we are relayed. Will plumb that through soon. --- include/steam/steamnetworkingtypes.h | 23 +++++++- .../steamnetworkingsockets_connections.cpp | 8 +++ .../steamnetworkingsockets_connections.h | 1 + .../steamnetworkingsockets_p2p_ice.cpp | 4 ++ .../clientlib/steamnetworkingsockets_udp.cpp | 52 ++++++++++++------- .../clientlib/steamnetworkingsockets_udp.h | 1 + 6 files changed, 70 insertions(+), 19 deletions(-) diff --git a/include/steam/steamnetworkingtypes.h b/include/steam/steamnetworkingtypes.h index 13e2546..c77215d 100644 --- a/include/steam/steamnetworkingtypes.h +++ b/include/steam/steamnetworkingtypes.h @@ -571,6 +571,21 @@ enum ESteamNetConnectionEnd k_ESteamNetConnectionEnd__Force32Bit = 0x7fffffff }; +/// Enumerate different kinds of transport that can be used +enum ESteamNetTransportKind +{ + k_ESteamNetTransport_Unknown = 0, + k_ESteamNetTransport_LoopbackBuffers = 1, // Internal buffers, not using OS network stack + k_ESteamNetTransport_LocalHost = 2, // Using OS network stack to talk to localhost address + k_ESteamNetTransport_UDP = 3, // Ordinary UDP connection, remote address is not a LAN address + k_ESteamNetTransport_UDPLan = 4, // Ordinary UDP connection, remote address is a LAN address + k_ESteamNetTransport_TURN = 5, // Relayed over TURN server + k_ESteamNetTransport_SDRP2P = 6, // P2P connection relayed over Steam Datagram Relay + k_ESteamNetTransport_SDRHostedServer = 7, // Connection to a server hosted in a known data center via Steam Datagram Relay + + k_ESteamNetTransport_Force32Bit = 0x7fffffff +}; + /// Max length, in bytes (including null terminator) of the reason string /// when a connection is closed. const int k_cchSteamNetworkingMaxConnectionCloseReason = 128; @@ -622,8 +637,14 @@ struct SteamNetConnectionInfo_t /// This string is used in various internal logging messages char m_szConnectionDescription[ k_cchSteamNetworkingMaxConnectionDescription ]; + /// What kind of transport is currently being used? + /// Note that this is potentially a dynamic property! Also, it may not + /// always be available, especially right as the connection starts, or + /// after the connection ends. + ESteamNetTransportKind m_eTransportKind; + /// Internal stuff, room to change API easily - uint32 reserved[64]; + uint32 reserved[63]; }; /// Quick connection state, pared down to something you could call diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp index 082cc57..e692ad4 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp @@ -1638,6 +1638,8 @@ void CConnectionTransport::GetDetailedConnectionStatus( SteamNetworkingDetailedC void CSteamNetworkConnectionBase::ConnectionPopulateInfo( SteamNetConnectionInfo_t &info ) const { + memset( &info, 0, sizeof(info) ); + info.m_eState = CollapseConnectionStateToAPIState( m_eConnectionState ); info.m_hListenSocket = m_pParentListenSocket ? m_pParentListenSocket->m_hListenSocketSelf : k_HSteamListenSocket_Invalid; NetAdrToSteamNetworkingIPAddr( info.m_addrRemote, m_netAdrRemote ); // FIXME this is in a weird place @@ -3245,6 +3247,12 @@ int CSteamNetworkConnectionPipe::SendEncryptedDataChunk( const void *pChunk, int return -1; } +void CSteamNetworkConnectionPipe::TransportPopulateConnectionInfo( SteamNetConnectionInfo_t &info ) const +{ + CConnectionTransport::TransportPopulateConnectionInfo( info ); + info.m_eTransportKind = k_ESteamNetTransport_LoopbackBuffers; +} + void CSteamNetworkConnectionPipe::ConnectionStateChanged( ESteamNetworkingConnectionState eOldState ) { CSteamNetworkConnectionBase::ConnectionStateChanged( eOldState ); diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.h b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.h index f04a562..2b72d70 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.h +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.h @@ -812,6 +812,7 @@ public: virtual void SendEndToEndConnectRequest( SteamNetworkingMicroseconds usecNow ) override; virtual void SendEndToEndStatsMsg( EStatsReplyRequest eRequest, SteamNetworkingMicroseconds usecNow, const char *pszReason ) override; virtual int SendEncryptedDataChunk( const void *pChunk, int cbChunk, SendPacketContext_t &ctx ) override; + virtual void TransportPopulateConnectionInfo( SteamNetConnectionInfo_t &info ) const override; private: diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p_ice.cpp b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p_ice.cpp index 36b6e35..d8c0d79 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p_ice.cpp +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_p2p_ice.cpp @@ -67,6 +67,10 @@ CConnectionTransportP2PICE::~CConnectionTransportP2PICE() void CConnectionTransportP2PICE::TransportPopulateConnectionInfo( SteamNetConnectionInfo_t &info ) const { + CConnectionTransport::TransportPopulateConnectionInfo( info ); + + // FIXME Need to rev the ice session interface version so that I get back this info! + info.m_eTransportKind = k_ESteamNetTransport_UDP; } void CConnectionTransportP2PICE::GetDetailedConnectionStatus( SteamNetworkingDetailedConnectionStatus &stats, SteamNetworkingMicroseconds usecNow ) diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp index 0f49fc7..2ea5003 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.cpp @@ -1093,7 +1093,23 @@ void CConnectionTransportUDP::TransportConnectionStateChanged( ESteamNetworkingC } } -#define ReportBadPacketIPv4( pszMsgType, /* fmt */ ... ) \ +void CConnectionTransportUDP::TransportPopulateConnectionInfo( SteamNetConnectionInfo_t &info ) const +{ + CConnectionTransport::TransportPopulateConnectionInfo( info ); + + if ( m_pSocket ) + { + const netadr_t &addr = m_pSocket->GetRemoteHostAddr(); + if ( addr.IsLoopback() ) + info.m_eTransportKind = k_ESteamNetTransport_LocalHost; + else if ( addr.IsReservedAdr() ) + info.m_eTransportKind = k_ESteamNetTransport_UDPLan; + else + info.m_eTransportKind = k_ESteamNetTransport_UDP; + } +} + +#define ReportBadPacketFromSocketPeer( pszMsgType, /* fmt */ ... ) \ ReportBadPacketFrom( m_pSocket->GetRemoteHostAddr(), pszMsgType, __VA_ARGS__ ) void CConnectionTransportUDP::PacketReceived( const void *pvPkt, int cbPkt, const netadr_t &adrFrom, CConnectionTransportUDP *pSelf ) @@ -1232,7 +1248,7 @@ void CConnectionTransportUDP::Received_Data( const uint8 *pPkt, int cbPkt, Steam if ( cbPkt < sizeof(UDPDataMsgHdr) ) { - ReportBadPacketIPv4( "DataPacket", "Packet of size %d is too small.", cbPkt ); + ReportBadPacketFromSocketPeer( "DataPacket", "Packet of size %d is too small.", cbPkt ); return; } @@ -1242,7 +1258,7 @@ void CConnectionTransportUDP::Received_Data( const uint8 *pPkt, int cbPkt, Steam { // Wrong session. It could be an old session, or it could be spoofed. - ReportBadPacketIPv4( "DataPacket", "Incorrect connection ID" ); + ReportBadPacketFromSocketPeer( "DataPacket", "Incorrect connection ID" ); if ( BCheckGlobalSpamReplyRateLimit( usecNow ) ) { SendNoConnection( LittleDWord( hdr->m_unToConnectionID ), 0 ); @@ -1295,18 +1311,18 @@ void CConnectionTransportUDP::Received_Data( const uint8 *pPkt, int cbPkt, Steam pIn = DeserializeVarInt( pIn, pPktEnd, cbStatsMsgIn ); if ( pIn == NULL ) { - ReportBadPacketIPv4( "DataPacket", "Failed to varint decode size of stats blob" ); + ReportBadPacketFromSocketPeer( "DataPacket", "Failed to varint decode size of stats blob" ); return; } if ( pIn + cbStatsMsgIn > pPktEnd ) { - ReportBadPacketIPv4( "DataPacket", "stats message size doesn't make sense. Stats message size %d, packet size %d", cbStatsMsgIn, cbPkt ); + ReportBadPacketFromSocketPeer( "DataPacket", "stats message size doesn't make sense. Stats message size %d, packet size %d", cbStatsMsgIn, cbPkt ); return; } if ( !msgStats.ParseFromArray( pIn, cbStatsMsgIn ) ) { - ReportBadPacketIPv4( "DataPacket", "protobuf failed to parse inline stats message" ); + ReportBadPacketFromSocketPeer( "DataPacket", "protobuf failed to parse inline stats message" ); return; } @@ -1342,7 +1358,7 @@ void CConnectionTransportUDP::Received_ChallengeReply( const CMsgSteamSockets_UD // We should only be getting this if we are the "client" if ( ListenSocket() ) { - ReportBadPacketIPv4( "ChallengeReply", "Shouldn't be receiving this unless on accepted connections, only connections initiated locally." ); + ReportBadPacketFromSocketPeer( "ChallengeReply", "Shouldn't be receiving this unless on accepted connections, only connections initiated locally." ); return; } @@ -1353,7 +1369,7 @@ void CConnectionTransportUDP::Received_ChallengeReply( const CMsgSteamSockets_UD // Check session ID to make sure they aren't spoofing. if ( msg.connection_id() != ConnectionIDLocal() ) { - ReportBadPacketIPv4( "ChallengeReply", "Incorrect connection ID. Message is stale or could be spoofed, ignoring." ); + ReportBadPacketFromSocketPeer( "ChallengeReply", "Incorrect connection ID. Message is stale or could be spoofed, ignoring." ); return; } if ( msg.protocol_version() < k_nMinRequiredProtocolVersion ) @@ -1428,14 +1444,14 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con // We should only be getting this if we are the "client" if ( ListenSocket() ) { - ReportBadPacketIPv4( "ConnectOK", "Shouldn't be receiving this unless on accepted connections, only connections initiated locally." ); + ReportBadPacketFromSocketPeer( "ConnectOK", "Shouldn't be receiving this unless on accepted connections, only connections initiated locally." ); return; } // Check connection ID to make sure they aren't spoofing and it's the same connection we think it is if ( msg.client_connection_id() != ConnectionIDLocal() ) { - ReportBadPacketIPv4( "ConnectOK", "Incorrect connection ID. Message is stale or could be spoofed, ignoring." ); + ReportBadPacketFromSocketPeer( "ConnectOK", "Incorrect connection ID. Message is stale or could be spoofed, ignoring." ); return; } @@ -1449,7 +1465,7 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con int r = SteamNetworkingIdentityFromSignedCert( identityRemote, msg.cert(), errMsg ); if ( r < 0 ) { - ReportBadPacketIPv4( "ConnectRequest", "Bad identity in cert. %s", errMsg ); + ReportBadPacketFromSocketPeer( "ConnectRequest", "Bad identity in cert. %s", errMsg ); return; } if ( r == 0 ) @@ -1459,7 +1475,7 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con r = SteamNetworkingIdentityFromProtobuf( identityRemote, msg, identity_string, legacy_identity_binary, legacy_server_steam_id, errMsg ); if ( r < 0 ) { - ReportBadPacketIPv4( "ConnectRequest", "Bad identity. %s", errMsg ); + ReportBadPacketFromSocketPeer( "ConnectRequest", "Bad identity. %s", errMsg ); return; } if ( r == 0 ) @@ -1484,7 +1500,7 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con if ( m_connection.m_connectionConfig.m_IP_AllowWithoutAuth.Get() == 0 ) { // Should we send an explicit rejection here? - ReportBadPacketIPv4( "ConnectOK", "Unauthenticated connections not allowed." ); + ReportBadPacketFromSocketPeer( "ConnectOK", "Unauthenticated connections not allowed." ); return; } @@ -1520,7 +1536,7 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con // Make sure they are still who we think they are if ( !m_connection.m_identityRemote.IsInvalid() && !( m_connection.m_identityRemote == identityRemote ) ) { - ReportBadPacketIPv4( "ConnectOK", "server_steam_id doesn't match who we expect to be connecting to!" ); + ReportBadPacketFromSocketPeer( "ConnectOK", "server_steam_id doesn't match who we expect to be connecting to!" ); return; } @@ -1579,7 +1595,7 @@ void CConnectionTransportUDP::Received_ConnectOK( const CMsgSteamSockets_UDP_Con if ( !m_connection.BRecvCryptoHandshake( msg.cert(), msg.crypt(), false ) ) { Assert( ConnectionState() == k_ESteamNetworkingConnectionState_ProblemDetectedLocally ); - ReportBadPacketIPv4( "ConnectOK", "Failed crypto init. %s", m_connection.m_szEndDebug ); + ReportBadPacketFromSocketPeer( "ConnectOK", "Failed crypto init. %s", m_connection.m_szEndDebug ); return; } @@ -1624,7 +1640,7 @@ void CConnectionTransportUDP::Received_NoConnection( const CMsgSteamSockets_UDP_ // Make sure it's an ack of something we would have sent if ( msg.to_connection_id() != ConnectionIDLocal() || msg.from_connection_id() != m_connection.m_unConnectionIDRemote ) { - ReportBadPacketIPv4( "NoConnection", "Old/incorrect connection ID. Message is for a stale connection, or is spoofed. Ignoring." ); + ReportBadPacketFromSocketPeer( "NoConnection", "Old/incorrect connection ID. Message is for a stale connection, or is spoofed. Ignoring." ); return; } @@ -1637,7 +1653,7 @@ void CConnectionTransportUDP::Received_ChallengeOrConnectRequest( const char *ps // If wrong connection ID, then check for sending a generic reply and bail if ( unPacketConnectionID != m_connection.m_unConnectionIDRemote ) { - ReportBadPacketIPv4( pszDebugPacketType, "Incorrect connection ID, when we do have a connection for this address. Could be spoofed, ignoring." ); + ReportBadPacketFromSocketPeer( pszDebugPacketType, "Incorrect connection ID, when we do have a connection for this address. Could be spoofed, ignoring." ); // Let's not send a reply in this case //if ( BCheckGlobalSpamReplyRateLimit( usecNow ) ) // SendNoConnection( unPacketConnectionID ); @@ -1669,7 +1685,7 @@ void CConnectionTransportUDP::Received_ChallengeOrConnectRequest( const char *ps if ( !ListenSocket() ) { // WAT? We initiated this connection, so why are they requesting to connect? - ReportBadPacketIPv4( pszDebugPacketType, "We are the 'client' who initiated the connection, so 'server' shouldn't be sending us this!" ); + ReportBadPacketFromSocketPeer( pszDebugPacketType, "We are the 'client' who initiated the connection, so 'server' shouldn't be sending us this!" ); return; } diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.h b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.h index 696fad1..a59758d 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.h +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_udp.h @@ -92,6 +92,7 @@ public: virtual void SendEndToEndConnectRequest( SteamNetworkingMicroseconds usecNow ) override; virtual void SendEndToEndStatsMsg( EStatsReplyRequest eRequest, SteamNetworkingMicroseconds usecNow, const char *pszReason ) override; virtual void TransportConnectionStateChanged( ESteamNetworkingConnectionState eOldState ) override; + virtual void TransportPopulateConnectionInfo( SteamNetConnectionInfo_t &info ) const; /// Interface used to talk to the remote host IBoundUDPSocket *m_pSocket;