From 5a8aa9ce8b31701919f0cd570d44bef17f6a3f52 Mon Sep 17 00:00:00 2001 From: asmodai Date: Sat, 10 Oct 2015 20:02:29 +0300 Subject: [PATCH] Implemented SV_GetChallenge for API. --- rehlds/engine/server.h | 8 +++- rehlds/engine/sv_main.cpp | 76 +++++++++---------------------- rehlds/public/rehlds/rehlds_api.h | 3 +- rehlds/rehlds/rehlds_api_impl.cpp | 3 +- 4 files changed, 33 insertions(+), 57 deletions(-) diff --git a/rehlds/engine/server.h b/rehlds/engine/server.h index 5eaa360..559ef22 100644 --- a/rehlds/engine/server.h +++ b/rehlds/engine/server.h @@ -40,6 +40,11 @@ #define MAX_NAME 32 #define MAX_LIGHTSTYLES 64 #define MAX_PACKET_ENTITIES 256 +#ifdef REHLDS_OPT_PEDANTIC +#define MAX_CHALLENGES 64 +#else +#define MAX_CHALLENGES 1024 +#endif #include "custom_int.h" #include "crc.h" @@ -573,7 +578,7 @@ extern int numipfilters; extern userfilter_t userfilters[32768]; extern int numuserfilters; -extern challenge_t g_rg_sv_challenges[1024]; +extern challenge_t g_rg_sv_challenges[MAX_CHALLENGES]; #ifdef HOOK_ENGINE @@ -647,6 +652,7 @@ int SV_FindEmptySlot(netadr_t *adr, int *pslot, client_t ** ppClient); void SV_ConnectClient(void); void SV_ConnectClient_internal(void); void SVC_Ping(void); +int SV_GetChallenge(netadr_t& adr); void SVC_GetChallenge(void); void SVC_ServiceChallenge(void); void SV_ResetModInfo(void); diff --git a/rehlds/engine/sv_main.cpp b/rehlds/engine/sv_main.cpp index ffef1ce..ef4feff 100644 --- a/rehlds/engine/sv_main.cpp +++ b/rehlds/engine/sv_main.cpp @@ -1831,7 +1831,7 @@ typedef struct challenge_s int time; } challenge_t; -challenge_t g_rg_sv_challenges[1024]; +challenge_t g_rg_sv_challenges[MAX_CHALLENGES]; #ifdef REHLDS_OPT_PEDANTIC int g_oldest_challenge = 0; #endif @@ -2418,25 +2418,17 @@ void SVC_Ping(void) NET_SendPacket(NS_SERVER, sizeof(data), data, net_from); } -/* ../engine/sv_main.c:3208 */ -void SVC_GetChallenge(void) +int SV_GetChallenge(netadr_t& adr) { int i; #ifndef REHLDS_OPT_PEDANTIC int oldest = 0; int oldestTime = INT_MAX; #endif - char data[1024]; - qboolean steam = FALSE; - - if (Cmd_Argc() == 2 && !Q_stricmp(Cmd_Argv(1), "steam")) - { - steam = TRUE; - } for (i = 0; i < MAX_CHALLENGES; i++) { - if (NET_CompareBaseAdr(net_from, g_rg_sv_challenges[i].adr)) + if (NET_CompareBaseAdr(adr, g_rg_sv_challenges[i].adr)) break; #ifndef REHLDS_OPT_PEDANTIC if (g_rg_sv_challenges[i].time < oldestTime) @@ -2446,7 +2438,7 @@ void SVC_GetChallenge(void) } #endif } - + if (i == MAX_CHALLENGES) { #ifdef REHLDS_OPT_PEDANTIC @@ -2464,32 +2456,39 @@ void SVC_GetChallenge(void) #else // REHLDS_FIXES g_rg_sv_challenges[i].challenge = (RandomLong(0, 36863) << 16) | (RandomLong(0, 65535)); #endif // REHLDS_FIXES - g_rg_sv_challenges[i].adr = net_from; + g_rg_sv_challenges[i].adr = adr; g_rg_sv_challenges[i].time = (int)realtime; } + + return g_rg_sv_challenges[i].challenge; +} + +/* ../engine/sv_main.c:3208 */ +void SVC_GetChallenge(void) +{ + char data[1024]; + qboolean steam = (Cmd_Argc() == 2 && !Q_stricmp(Cmd_Argv(1), "steam")); + int challenge = SV_GetChallenge(net_from); + if (steam) - Q_snprintf(data, sizeof(data), "\xFF\xFF\xFF\xFF%c00000000 %u 3 %I64i %d\n", S2C_CHALLENGE, g_rg_sv_challenges[i].challenge, Steam_GSGetSteamID(), Steam_GSBSecure()); + Q_snprintf(data, sizeof(data), "\xFF\xFF\xFF\xFF%c00000000 %u 3 %I64i %d\n", S2C_CHALLENGE, challenge, Steam_GSGetSteamID(), Steam_GSBSecure()); else { Con_DPrintf("Server requiring authentication\n"); - Q_snprintf(data, sizeof(data), "\xFF\xFF\xFF\xFF%c00000000 %u 2\n", S2C_CHALLENGE, g_rg_sv_challenges[i].challenge); + Q_snprintf(data, sizeof(data), "\xFF\xFF\xFF\xFF%c00000000 %u 2\n", S2C_CHALLENGE, challenge); } // Give 3-rd party plugins a chance to modify challenge response - g_RehldsHookchains.m_SVC_GetChallenge_mod.callChain(NULL, data, g_rg_sv_challenges[i].challenge); + g_RehldsHookchains.m_SVC_GetChallenge_mod.callChain(NULL, data, challenge); NET_SendPacket(NS_SERVER, Q_strlen(data) + 1, data, net_from); } /* ../engine/sv_main.c:3292 */ void SVC_ServiceChallenge(void) { - int i; -#ifndef REHLDS_OPT_PEDANTIC - int oldest = 0; - int oldestTime = INT_MAX; -#endif char data[128]; const char *type; + int challenge; if (Cmd_Argc() != 2) return; @@ -2501,40 +2500,9 @@ void SVC_ServiceChallenge(void) if (!type[0] || Q_stricmp(type, "rcon")) return; - for (i = 0; i < MAX_CHALLENGES; i++) - { - if (NET_CompareBaseAdr(net_from, g_rg_sv_challenges[i].adr)) - break; + challenge = SV_GetChallenge(net_from); -#ifndef REHLDS_OPT_PEDANTIC - if (g_rg_sv_challenges[i].time < oldestTime) - { - oldestTime = g_rg_sv_challenges[i].time; - oldest = i; - } -#endif - } - if (i == MAX_CHALLENGES) // no challenge for ip - { -#ifdef REHLDS_OPT_PEDANTIC - // oldest challenge is always next after last generated - i = g_oldest_challenge++; - - if(g_oldest_challenge >= MAX_CHALLENGES) - g_oldest_challenge = 0; -#else - i = oldest; -#endif - // generate new challenge number -#ifdef REHLDS_FIXES - g_rg_sv_challenges[i].challenge = (RandomLong(0, 0x7fff) << 16) | (RandomLong(0, 0xffff)); -#else // REHLDS_FIXES - g_rg_sv_challenges[i].challenge = (RandomLong(0, 36863) << 16) | (RandomLong(0, 65535)); -#endif // REHLDS_FIXES - g_rg_sv_challenges[i].adr = net_from; - g_rg_sv_challenges[i].time = (int)realtime; - } - Q_snprintf(data, sizeof(data), "%c%c%c%cchallenge %s %u\n", 255, 255, 255, 255, type, g_rg_sv_challenges[i].challenge); + Q_snprintf(data, sizeof(data), "%c%c%c%cchallenge %s %u\n", 255, 255, 255, 255, type, challenge); NET_SendPacket(NS_SERVER, Q_strlen(data) + 1, data, net_from); } diff --git a/rehlds/public/rehlds/rehlds_api.h b/rehlds/public/rehlds/rehlds_api.h index a172f50..675d84d 100644 --- a/rehlds/public/rehlds/rehlds_api.h +++ b/rehlds/public/rehlds/rehlds_api.h @@ -35,7 +35,7 @@ #include "model.h" #define REHLDS_API_VERSION_MAJOR 2 -#define REHLDS_API_VERSION_MINOR 1 +#define REHLDS_API_VERSION_MINOR 2 //Steam_NotifyClientConnect hook typedef IHookChain IRehldsHook_Steam_NotifyClientConnect; @@ -210,6 +210,7 @@ struct RehldsFuncs_t { void(*MSG_EndBitWriting)(sizebuf_t *buf); void*(*SZ_GetSpace)(sizebuf_t *buf, int length); cvar_t*(*GetCvarVars)(); + int (*SV_GetChallenge)(netadr_t& adr); }; class IRehldsApi { diff --git a/rehlds/rehlds/rehlds_api_impl.cpp b/rehlds/rehlds/rehlds_api_impl.cpp index bcc4cd5..5a3bd97 100644 --- a/rehlds/rehlds/rehlds_api_impl.cpp +++ b/rehlds/rehlds/rehlds_api_impl.cpp @@ -120,7 +120,8 @@ RehldsFuncs_t g_RehldsApiFuncs = &MSG_WriteBitVec3Coord_api, &MSG_EndBitWriting_api, &SZ_GetSpace, - &GetCvarVars_api + &GetCvarVars_api, + &SV_GetChallenge }; sizebuf_t* EXT_FUNC GetNetMessage_api()