From 9cbf5ab6a60de7da21054b7d0db2402c54b6dd35 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 1 Dec 2022 01:51:07 +0300 Subject: [PATCH] engine: client: streamline constructing master server scan request through common function --- engine/client/cl_game.c | 9 ++++++--- engine/client/cl_main.c | 42 +++++++++++++++++++++++++++++++--------- engine/client/client.h | 1 + engine/common/protocol.h | 3 +++ 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/engine/client/cl_game.c b/engine/client/cl_game.c index efa03828..827d7573 100644 --- a/engine/client/cl_game.c +++ b/engine/client/cl_game.c @@ -3401,9 +3401,12 @@ void GAME_EXPORT NetAPI_SendRequest( int context, int request, int flags, double if( request == NETAPI_REQUEST_SERVERLIST ) { - char fullquery[512] = "1\xFF" "0.0.0.0:0\0" "\\gamedir\\"; + char fullquery[512]; + size_t len; - // make sure what port is specified + len = CL_BuildMasterServerScanRequest( fullquery, sizeof( fullquery ), false ); + + // make sure that port is specified if( !nr->resp.remote_address.port ) nr->resp.remote_address.port = MSG_BigShort( PORT_MASTER ); @@ -3431,7 +3434,7 @@ void GAME_EXPORT NetAPI_CancelRequest( int context ) { net_request_t *nr; int i; - +; // find a specified request for( i = 0; i < MAX_REQUESTS; i++ ) { diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index 3984bb82..6f3da3bd 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -1584,7 +1584,32 @@ void CL_LocalServers_f( void ) Netchan_OutOfBandPrint( NS_CLIENT, adr, "info %i", PROTOCOL_VERSION ); } -#define MS_SCAN_REQUEST "1\xFF" "0.0.0.0:0\0" +/* +================= +CL_BuildMasterServerScanRequest +================= +*/ +size_t CL_BuildMasterServerScanRequest( char *buf, size_t size, qboolean nat ) +{ + size_t remaining; + char *info; + + if( unlikely( size < sizeof( MS_SCAN_REQUEST ))) + return 0; + + Q_strncpy( buf, MS_SCAN_REQUEST, size ); + + info = buf + sizeof( MS_SCAN_REQUEST ) - 1; + remaining = size - sizeof( MS_SCAN_REQUEST ); + + info[0] = 0; + + Info_SetValueForKey( info, "gamedir", GI->gamefolder, remaining ); + Info_SetValueForKey( info, "clver", XASH_VERSION, remaining ); // let master know about client version + Info_SetValueForKey( info, "nat", nat ? "1" : "0", remaining ); + + return sizeof( MS_SCAN_REQUEST ) + Q_strlen( info ); +} /* ================= @@ -1593,18 +1618,17 @@ CL_InternetServers_f */ void CL_InternetServers_f( void ) { - char fullquery[512] = MS_SCAN_REQUEST; - char *info = fullquery + sizeof( MS_SCAN_REQUEST ) - 1; - const size_t remaining = sizeof( fullquery ) - sizeof( MS_SCAN_REQUEST ); + char fullquery[512]; + size_t len; + qboolean nat = cl_nat->value != 0.0f; - NET_Config( true, true ); // allow remote + len = CL_BuildMasterServerScanRequest( fullquery, sizeof( fullquery ), nat ); Con_Printf( "Scanning for servers on the internet area...\n" ); - Info_SetValueForKey( info, "gamedir", GI->gamefolder, remaining ); - Info_SetValueForKey( info, "clver", XASH_VERSION, remaining ); // let master know about client version - Info_SetValueForKey( info, "nat", cl_nat->string, remaining ); - cls.internetservers_wait = NET_SendToMasters( NS_CLIENT, sizeof( MS_SCAN_REQUEST ) + Q_strlen( info ), fullquery ); + NET_Config( true, true ); // allow remote + + cls.internetservers_wait = NET_SendToMasters( NS_CLIENT, len, fullquery ); cls.internetservers_pending = true; if( !cls.internetservers_wait ) diff --git a/engine/client/client.h b/engine/client/client.h index 684150d1..1ed78ab2 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -754,6 +754,7 @@ int CL_IsDevOverviewMode( void ); void CL_PingServers_f( void ); void CL_SignonReply( void ); void CL_ClearState( void ); +size_t CL_BuildMasterServerScanRequest( char *buf, size_t size, qboolean nat ); // // cl_demo.c diff --git a/engine/common/protocol.h b/engine/common/protocol.h index ec0e9230..aba1fd5e 100644 --- a/engine/common/protocol.h +++ b/engine/common/protocol.h @@ -302,4 +302,7 @@ extern const char *clc_strings[clc_lastmsg+1]; #define MAX_LEGACY_TOTAL_CMDS 16 // 28 - 16 = 12 real legacy max backup #define MAX_LEGACY_BACKUP_CMDS 12 +// Master Server protocol +#define MS_SCAN_REQUEST "1\xFF" "0.0.0.0:0\0" // TODO: implement IP filter + #endif//NET_PROTOCOL_H