From cff8ee13e6a5eb526a25a5fc9cc0720a6b8f6d04 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 26 Oct 2024 19:37:26 +0300 Subject: [PATCH] engine: rcon refactoring. Use sizebuf_t to concatenate commands. Only call redirect when we're executing command. --- engine/client/cl_main.c | 44 +++++++++++++++++++-------------------- engine/server/sv_client.c | 18 +++++++--------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index f9b49df8..06358687 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -1506,9 +1506,9 @@ an unconnected command. */ static void CL_Rcon_f( void ) { - char message[1024]; - netadr_t to; - string command; + char message[1024]; + sizebuf_t msg; + netadr_t to; int i; if( !COM_CheckString( rcon_password.string )) @@ -1517,25 +1517,8 @@ static void CL_Rcon_f( void ) return; } - message[0] = (char)255; - message[1] = (char)255; - message[2] = (char)255; - message[3] = (char)255; - message[4] = 0; - NET_Config( true, false ); // allow remote - Q_strncat( message, C2S_RCON" ", sizeof( message )); - Q_strncat( message, rcon_password.string, sizeof( message )); - Q_strncat( message, " ", sizeof( message ) ); - - for( i = 1; i < Cmd_Argc(); i++ ) - { - Cmd_Escape( command, Cmd_Argv( i ), sizeof( command )); - Q_strncat( message, command, sizeof( message )); - Q_strncat( message, " ", sizeof( message )); - } - if( cls.state >= ca_connected ) { to = cls.netchan.remote_address; @@ -1549,10 +1532,27 @@ static void CL_Rcon_f( void ) } NET_StringToAdr( rcon_address.string, &to ); - if( to.port == 0 ) to.port = MSG_BigShort( PORT_SERVER ); + if( to.port == 0 ) + to.port = MSG_BigShort( PORT_SERVER ); } - NET_SendPacket( NS_CLIENT, Q_strlen( message ) + 1, message, to ); + MSG_Init( &msg, "RconMessage", message, sizeof( message )); + MSG_WriteLong( &msg, -1 ); + MSG_WriteStringf( &msg, C2S_RCON" %s ", rcon_password.string ); + MSG_SeekToBit( &msg, -8, SEEK_CUR ); + + for( i = 1; i < Cmd_Argc(); i++ ) + { + string command; + + Cmd_Escape( command, Cmd_Argv( i ), sizeof( command )); + MSG_WriteString( &msg, command ); + MSG_SeekToBit( &msg, -8, SEEK_CUR ); + MSG_WriteChar( &msg, ' ' ); + } + MSG_WriteByte( &msg, 0 ); + + NET_SendPacket( NS_CLIENT, MSG_GetNumBytesWritten( &msg ), MSG_GetData( &msg ), to ); } diff --git a/engine/server/sv_client.c b/engine/server/sv_client.c index f770a47d..6eeca401 100644 --- a/engine/server/sv_client.c +++ b/engine/server/sv_client.c @@ -714,20 +714,18 @@ static void SV_BeginRedirect( host_redirect_t *rd, netadr_t adr, rdtype_t target static void SV_FlushRedirect( netadr_t adr, int dest, char *buf ) { - if( sv.current_client && FBitSet( sv.current_client->flags, FCL_FAKECLIENT )) - return; - switch( dest ) { case RD_PACKET: Netchan_OutOfBandPrint( NS_SERVER, adr, A2C_PRINT"\n%s", buf ); break; case RD_CLIENT: - if( !sv.current_client ) return; // client not set + if( !sv.current_client || !FBitSet( sv.current_client->flags, FCL_FAKECLIENT )) + return; // client not set MSG_BeginServerCmd( &sv.current_client->netchan.message, svc_print ); MSG_WriteString( &sv.current_client->netchan.message, buf ); break; - case RD_NONE: + default: Con_Printf( S_ERROR "%s: %s: invalid destination\n", __func__, NET_AdrToString( adr )); break; } @@ -1109,10 +1107,7 @@ Redirect all printfs */ void SV_RemoteCommand( netadr_t from, sizebuf_t *msg ) { - static char outputbuf[2048]; const char *adr; - char remaining[1024]; - char *p = remaining; int i; if( !rcon_enable.value || !COM_CheckStringEmpty( rcon_password.string )) @@ -1125,7 +1120,9 @@ void SV_RemoteCommand( netadr_t from, sizebuf_t *msg ) if( Rcon_Validate( )) { - SV_BeginRedirect( &host.rd, from, RD_PACKET, outputbuf, sizeof( outputbuf ) - 16, SV_FlushRedirect ); + static char outputbuf[2048]; + char remaining[1024]; + char *p = remaining; remaining[0] = 0; for( i = 2; i < Cmd_Argc(); i++ ) @@ -1134,8 +1131,9 @@ void SV_RemoteCommand( netadr_t from, sizebuf_t *msg ) p += Q_strncpy( p, Cmd_Argv( i ), sizeof( remaining ) - ( p - remaining )); p += Q_strncpy( p, "\" ", sizeof( remaining ) - ( p - remaining )); } - Cmd_ExecuteString( remaining ); + SV_BeginRedirect( &host.rd, from, RD_PACKET, outputbuf, sizeof( outputbuf ) - 16, SV_FlushRedirect ); + Cmd_ExecuteString( remaining ); SV_EndRedirect( &host.rd ); } else Con_Printf( S_ERROR "Bad rcon_password.\n" );