diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 6e123adce..21893df8d 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -145,7 +145,7 @@ static tic_t firstticstosend; // min of the nettics static tic_t tictoclear = 0; // optimize d_clearticcmd static tic_t maketic; -static INT16 consistancy[TICQUEUE]; +static INT16 consistancy[BACKUPTICS]; // Resynching shit! static UINT32 resynch_score[MAXNETNODES]; // "score" for kicking -- if this gets too high then cfail kick @@ -219,8 +219,8 @@ struct textcmdbuf_s static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS] = {NULL}; -ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; static ticcmd_t playercmds[MAXPLAYERS]; +ticcmd_t netcmds[BACKUPTICS][MAXPLAYERS]; static textcmdtic_t *textcmds[TEXTCMD_HASH_SIZE] = {NULL}; consvar_t cv_showjoinaddress = {"showjoinaddress", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; @@ -532,9 +532,9 @@ static void D_Clearticcmd(tic_t tic) D_FreeTextcmd(tic); for (i = 0; i < MAXPLAYERS; i++) - netcmds[tic%TICQUEUE][i].angleturn = 0; + netcmds[tic%BACKUPTICS][i].angleturn = 0; - DEBFILE(va("clear tic %5u (%2u)\n", tic, tic%TICQUEUE)); + DEBFILE(va("clear tic %5u (%2u)\n", tic, tic%BACKUPTICS)); } void D_ResetTiccmds(void) @@ -1087,6 +1087,8 @@ static void SV_SendResynch(INT32 node) { INT32 i, j; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (!nodeingame[node]) { // player left during resynch @@ -1145,6 +1147,7 @@ static void SV_SendResynch(INT32 node) static void CL_AcknowledgeResynch(resynch_pak *rsp) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); resynch_read_player(rsp); netbuffer->packettype = PT_RESYNCHGET; @@ -1539,6 +1542,7 @@ static inline void CL_DrawConnectionStatus(void) static boolean CL_AskFileList(INT32 firstfile) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); netbuffer->packettype = PT_TELLFILESNEEDED; netbuffer->u.filesneedednum = firstfile; @@ -1558,6 +1562,9 @@ static boolean CL_AskFileList(INT32 firstfile) static boolean CL_SendJoin(void) { UINT8 localplayers = 1; + + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (netgame) CONS_Printf(M_GetText("Sending join request...\n")); netbuffer->packettype = PT_CLIENTJOIN; @@ -1654,6 +1661,8 @@ static void SV_SendServerInfo(INT32 node, tic_t servertime) ? gametype : cv_kartgametypepreference.value; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_SERVERINFO; netbuffer->u.serverinfo._255 = 255; netbuffer->u.serverinfo.packetversion = PACKETVERSION; @@ -1761,6 +1770,8 @@ static void SV_SendServerInfo(INT32 node, tic_t servertime) static void SV_SendPlayerInfo(INT32 node) { UINT8 i; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_PLAYERINFO; for (i = 0; i < MSCOMPAT_MAXPLAYERS; i++) @@ -1832,6 +1843,8 @@ static boolean SV_SendServerConfig(INT32 node) UINT8 *p, *op; boolean waspacketsent; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_SERVERCFG; netbuffer->u.servercfg.version = VERSION; @@ -2034,6 +2047,8 @@ static void CL_LoadReceivedSavegame(boolean reloading) size_t length, decompressedlen; char tmpsave[264]; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + sprintf(tmpsave, "%s" PATHSEP TMPSAVENAME, srb2home); length = FIL_ReadFile(tmpsave, &save.buffer); @@ -2088,7 +2103,7 @@ static void CL_LoadReceivedSavegame(boolean reloading) save.p = NULL; if (unlink(tmpsave) == -1) CONS_Alert(CONS_ERROR, M_GetText("Can't delete %s\n"), tmpsave); - consistancy[gametic%TICQUEUE] = Consistancy(); + consistancy[gametic%BACKUPTICS] = Consistancy(); CON_ToggleOff(); #ifdef SATURNPAK @@ -2115,8 +2130,7 @@ static void CL_ReloadReceivedSavegame(void) CL_LoadReceivedSavegame(true); - if (neededtic < gametic) - neededtic = gametic; + neededtic = max(neededtic, gametic); maketic = neededtic; // we dont have P_ForceLocalAngle so were setting it manually here @@ -2151,6 +2165,8 @@ static void SendAskInfo(INT32 node) asktime = I_GetTime(); + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_ASKINFO; netbuffer->u.askinfo.version = VERSION; netbuffer->u.askinfo.time = (tic_t)LONG(asktime); @@ -4156,6 +4172,8 @@ static void Command_ResendGamestate(void) { SINT8 playernum; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (COM_Argc() == 1) { CONS_Printf(M_GetText("resendgamestate : resend the game state to a player\n")); @@ -4173,6 +4191,7 @@ static void Command_ResendGamestate(void) // Send a PT_WILLRESENDGAMESTATE packet to the client so they know what's going on netbuffer->packettype = PT_WILLRESENDGAMESTATE; + if (!HSendPacket(playernode[playernum], true, 0, 0)) { CONS_Alert(CONS_ERROR, M_GetText("A problem occured, please try again.\n")); @@ -4426,6 +4445,8 @@ static inline void SV_GenContext(void) // void D_QuitNetGame(void) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (!netgame || !netbuffer) return; @@ -4675,6 +4696,7 @@ static boolean SV_AddWaitingPlayers(void) #ifdef SATURNPAK static inline void SendSaturnInfo(INT32 node) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); netbuffer->packettype = PT_ISSATURN; HSendPacket(node, true, 0, 0); } @@ -4740,7 +4762,7 @@ void SV_StopServer(void) for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) localtextcmd[i][0] = 0; - for (i = firstticstosend; i < firstticstosend + TICQUEUE; i++) + for (i = firstticstosend; i < firstticstosend + BACKUPTICS; i++) D_Clearticcmd(i); consoleplayer = 0; @@ -4767,6 +4789,8 @@ void SV_StartSinglePlayerServer(void) static void SV_SendRefuse(INT32 node, const char *reason) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + strcpy(netbuffer->u.serverrefuse.reason, reason); netbuffer->packettype = PT_SERVERREFUSE; @@ -4807,6 +4831,8 @@ static void HandleConnect(SINT8 node) if (playernode[i] != UINT8_MAX) // We use this to count players because it is affected by SV_AddWaitingPlayers when more than one client joins on the same tic, unlike playeringame and D_NumPlayers. UINT8_MAX denotes no node for that player connectedplayers++; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (bannednode && bannednode[node].banid != SIZE_MAX) { const char *reason = NULL; @@ -4982,6 +5008,8 @@ static void HandleTimeout(SINT8 node) static void HandleServerInfo(SINT8 node) { char servername[MAXSERVERNAME]; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + // compute ping in ms const tic_t ticnow = I_GetTime(); const tic_t ticthen = (tic_t)LONG(netbuffer->u.serverinfo.time); @@ -5000,15 +5028,209 @@ static void HandleServerInfo(SINT8 node) memcpy(connectedservername, netbuffer->u.serverinfo.servername, MAXSERVERNAME); } - static void HandlePlayerInfo(SINT8 node) { (void)node; - + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + INT32 i; for (i = 0; i < MAXPLAYERS; i++) playerinfo[i] = netbuffer->u.playerinfo[i]; } + +// Helper function for packets that should only be sent by the server +// If it is NOT from the server, bail out and close the connection! +static boolean ServerOnly(SINT8 node) +{ + if (node == servernode) + return false; + + Net_CloseConnection(node); + return true; +} + +static void PT_TellFilesNeeded(SINT8 node) +{ + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + + if (server && serverrunning) + { + UINT8 *p; + INT32 firstfile = netbuffer->u.filesneedednum; + + netbuffer->packettype = PT_MOREFILESNEEDED; + netbuffer->u.filesneededcfg.first = firstfile; + netbuffer->u.filesneededcfg.more = 0; + + p = PutFileNeeded(firstfile); + + HSendPacket(node, false, 0, p - ((UINT8 *)&netbuffer->u)); + } + else // Shouldn't get this if you aren't the server...? + Net_CloseConnection(node); +} + +static void PT_MoreFilesNeeded(SINT8 node) +{ + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + + if (server && serverrunning) + { // But wait I thought I'm the server? + Net_CloseConnection(node); + return; + } + + if (ServerOnly(node)) + return; + + if (cl_mode == CL_ASKFULLFILELIST && netbuffer->u.filesneededcfg.first == fileneedednum) + { + D_ParseFileneeded(netbuffer->u.filesneededcfg.num, netbuffer->u.filesneededcfg.files, netbuffer->u.filesneededcfg.first); + if (!netbuffer->u.filesneededcfg.more) + cl_lastcheckedfilecount = UINT16_MAX; // Got the whole file list + } +} + +// Negative response of client join request +static void PT_ServerRefuse(SINT8 node) +{ + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + + if (server && serverrunning) + { // But wait I thought I'm the server? + Net_CloseConnection(node); + return; + } + + if (ServerOnly(node)) + return; + + if (cl_mode == CL_WAITJOINRESPONSE) + { + // Save the reason so it can be displayed after quitting the netgame + char *reason = strdup(netbuffer->u.serverrefuse.reason); + if (!reason) + I_Error("Out of memory!\n"); + + if (strstr(reason, "Maximum players reached")) + { + serverisfull = true; + //Special timeout for when refusing due to player cap. The client will wait 3 seconds between join requests when waiting for a slot, so we need this to be much longer + //We set it back to the value of cv_nettimeout.value in CL_Reset + connectiontimeout = NEWTICRATE*7; + cl_mode = CL_ASKJOIN; + free(reason); + return; + } + + D_QuitNetGame(); + CL_Reset(); + D_StartTitle(); + + if (reason[1] == '|') + { + M_StartMessage(va("You have been %sfrom the server\n\nReason:\n%s", + (reason[0] == 'B') ? "banned\n" : "temporarily\nkicked ", + reason+2), NULL, MM_NOTHING); + } + else + { + M_StartMessage(va(M_GetText("Server refuses connection\n\nReason:\n%s"), + reason), NULL, MM_NOTHING); + } + + free(reason); + + // Will be reset by caller. Signals refusal. + cl_mode = CL_ABORTED; + } +} + +// Positive response of client join request +static void PT_ServerCFG(SINT8 node) +{ + INT32 j; + UINT8 *scp; + + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + + if (server && serverrunning && node != servernode) + { // but wait I thought I'm the server? + Net_CloseConnection(node); + return; + } + + if (ServerOnly(node)) + return; + + /// \note how would this happen? and is it doing the right thing if it does? + if (!(cl_mode == CL_WAITJOINRESPONSE || cl_mode == CL_ASKJOIN)) + return; + + if (client) + { + maketic = gametic = neededtic = (tic_t)LONG(netbuffer->u.servercfg.gametic); + if ((gametype = netbuffer->u.servercfg.gametype) >= NUMGAMETYPES) + I_Error("Bad gametype in cliserv!"); + modifiedgame = netbuffer->u.servercfg.modifiedgame; + for (j = 0; j < MAXPLAYERS; j++) + adminplayers[j] = netbuffer->u.servercfg.adminplayers[j]; + memcpy(server_context, netbuffer->u.servercfg.server_context, 8); + } + +#ifdef HAVE_DISCORDRPC + discordInfo.maxPlayers = netbuffer->u.servercfg.maxplayer; + discordInfo.joinsAllowed = netbuffer->u.servercfg.allownewplayer; + discordInfo.everyoneCanInvite = netbuffer->u.servercfg.discordinvites; +#endif + + nodeingame[(UINT8)servernode] = true; + serverplayer = netbuffer->u.servercfg.serverplayer; + doomcom->numslots = SHORT(netbuffer->u.servercfg.totalslotnum); + mynode = netbuffer->u.servercfg.clientnode; + if (serverplayer >= 0) + playernode[(UINT8)serverplayer] = servernode; + + if (netgame) +#ifdef JOININGAME + CONS_Printf(M_GetText("Join accepted, waiting for complete game state...\n")); +#else + CONS_Printf(M_GetText("Join accepted, waiting for next level change...\n")); +#endif + DEBFILE(va("Server accept join gametic=%u mynode=%d\n", gametic, mynode)); + +#ifdef SATURNPAK + SendSaturnInfo(node); +#endif + + memset(playeringame, 0, sizeof(playeringame)); + + for (j = 0; j < MAXPLAYERS; j++) + { + if (netbuffer->u.servercfg.playerskins[j] == 0xFF + && netbuffer->u.servercfg.playercolor[j] == 0xFF) + continue; // not in game + + playeringame[j] = true; + SetPlayerSkinByNum(j, (INT32)netbuffer->u.servercfg.playerskins[j]); + players[j].skincolor = netbuffer->u.servercfg.playercolor[j]; + } + + scp = netbuffer->u.servercfg.varlengthinputs; + CV_LoadPlayerNames(&scp); + CV_LoadNetVars(&scp); +#ifdef JOININGAME + /// \note Wait. What if a Lua script uses some global custom variables synched with the NetVars hook? + /// Shouldn't them be downloaded even at intermission time? + /// Also, according to HandleConnect, the server will send the savegame even during intermission... + /// Sryder 2018-07-05: If we don't want to send the player config another way we need to send the gamestate + /// At almost any gamestate there could be joiners... So just always send gamestate? + cl_mode = ((server) ? CL_CONNECTED : CL_DOWNLOADSAVEGAME); +#else + cl_mode = CL_CONNECTED; +#endif +} + #endif #ifdef SATURNPAK @@ -5019,6 +5241,8 @@ static void PT_WillResendGamestate(void) if (server || cl_redownloadinggamestate) return; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + // Send back a PT_CANRECEIVEGAMESTATE packet to the server // so they know they can start sending the game state netbuffer->packettype = PT_CANRECEIVEGAMESTATE; @@ -5062,75 +5286,20 @@ static void HandlePacketFromAwayNode(SINT8 node) if (node != servernode) DEBFILE(va("Received packet from unknown host %d\n", node)); -// macro for packets that should only be sent by the server -// if it is NOT from the server, bail out and close the connection! -#define SERVERONLY \ - if (node != servernode) \ - { \ - Net_CloseConnection(node); \ - break; \ - } + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + switch (netbuffer->packettype) { case PT_ASKINFOVIAMS: -#if 0 - if (server && serverrunning) - { - INT32 clientnode; - if (ms_RoomId < 0) // ignore if we're not actually on the MS right now - { - Net_CloseConnection(node); // and yes, close connection - return; - } - clientnode = I_NetMakeNode(netbuffer->u.msaskinfo.clientaddr); - if (clientnode != -1) - { - SV_SendServerInfo(clientnode, (tic_t)LONG(netbuffer->u.msaskinfo.time)); - SV_SendPlayerInfo(clientnode); // Send extra info - Net_CloseConnection(clientnode); - // Don't close connection to MS... - } - else - Net_CloseConnection(node); // ...unless the IP address is not valid - } - else - Net_CloseConnection(node); // you're not supposed to get it, so ignore it -#else Net_CloseConnection(node); -#endif break; case PT_TELLFILESNEEDED: - if (server && serverrunning) - { - UINT8 *p; - INT32 firstfile = netbuffer->u.filesneedednum; - - netbuffer->packettype = PT_MOREFILESNEEDED; - netbuffer->u.filesneededcfg.first = firstfile; - netbuffer->u.filesneededcfg.more = 0; - - p = PutFileNeeded(firstfile); - - HSendPacket(node, false, 0, p - ((UINT8 *)&netbuffer->u)); - } - else // Shouldn't get this if you aren't the server...? - Net_CloseConnection(node); + PT_TellFilesNeeded(node); break; case PT_MOREFILESNEEDED: - if (server && serverrunning) - { // But wait I thought I'm the server? - Net_CloseConnection(node); - break; - } - SERVERONLY - if (cl_mode == CL_ASKFULLFILELIST && netbuffer->u.filesneededcfg.first == fileneedednum) - { - D_ParseFileneeded(netbuffer->u.filesneededcfg.num, netbuffer->u.filesneededcfg.files, netbuffer->u.filesneededcfg.first); - if (!netbuffer->u.filesneededcfg.more) - cl_lastcheckedfilecount = UINT16_MAX; // Got the whole file list - } + PT_MoreFilesNeeded(node); break; case PT_ASKINFO: @@ -5143,131 +5312,12 @@ static void HandlePacketFromAwayNode(SINT8 node) break; case PT_SERVERREFUSE: // Negative response of client join request - if (server && serverrunning) - { // But wait I thought I'm the server? - Net_CloseConnection(node); - break; - } - SERVERONLY - if (cl_mode == CL_WAITJOINRESPONSE) - { - // Save the reason so it can be displayed after quitting the netgame - char *reason = strdup(netbuffer->u.serverrefuse.reason); - if (!reason) - I_Error("Out of memory!\n"); - - if (strstr(reason, "Maximum players reached")) - { - serverisfull = true; - //Special timeout for when refusing due to player cap. The client will wait 3 seconds between join requests when waiting for a slot, so we need this to be much longer - //We set it back to the value of cv_nettimeout.value in CL_Reset - connectiontimeout = NEWTICRATE*7; - cl_mode = CL_ASKJOIN; - free(reason); - break; - } - - D_QuitNetGame(); - CL_Reset(); - D_StartTitle(); - - if (reason[1] == '|') - { - M_StartMessage(va("You have been %sfrom the server\n\nReason:\n%s", - (reason[0] == 'B') ? "banned\n" : "temporarily\nkicked ", - reason+2), NULL, MM_NOTHING); - } - else - { - M_StartMessage(va(M_GetText("Server refuses connection\n\nReason:\n%s"), - reason), NULL, MM_NOTHING); - } - - free(reason); - - // Will be reset by caller. Signals refusal. - cl_mode = CL_ABORTED; - } + PT_ServerRefuse(node); break; case PT_SERVERCFG: // Positive response of client join request - { - INT32 j; - UINT8 *scp; - - if (server && serverrunning && node != servernode) - { // but wait I thought I'm the server? - Net_CloseConnection(node); - break; - } - SERVERONLY - /// \note how would this happen? and is it doing the right thing if it does? - if (!(cl_mode == CL_WAITJOINRESPONSE || cl_mode == CL_ASKJOIN)) - break; - - if (client) - { - maketic = gametic = neededtic = (tic_t)LONG(netbuffer->u.servercfg.gametic); - if ((gametype = netbuffer->u.servercfg.gametype) >= NUMGAMETYPES) - I_Error("Bad gametype in cliserv!"); - modifiedgame = netbuffer->u.servercfg.modifiedgame; - for (j = 0; j < MAXPLAYERS; j++) - adminplayers[j] = netbuffer->u.servercfg.adminplayers[j]; - memcpy(server_context, netbuffer->u.servercfg.server_context, 8); - } - -#ifdef HAVE_DISCORDRPC - discordInfo.maxPlayers = netbuffer->u.servercfg.maxplayer; - discordInfo.joinsAllowed = netbuffer->u.servercfg.allownewplayer; - discordInfo.everyoneCanInvite = netbuffer->u.servercfg.discordinvites; -#endif - - nodeingame[(UINT8)servernode] = true; - serverplayer = netbuffer->u.servercfg.serverplayer; - doomcom->numslots = SHORT(netbuffer->u.servercfg.totalslotnum); - mynode = netbuffer->u.servercfg.clientnode; - if (serverplayer >= 0) - playernode[(UINT8)serverplayer] = servernode; - - if (netgame) -#ifdef JOININGAME - CONS_Printf(M_GetText("Join accepted, waiting for complete game state...\n")); -#else - CONS_Printf(M_GetText("Join accepted, waiting for next level change...\n")); -#endif - DEBFILE(va("Server accept join gametic=%u mynode=%d\n", gametic, mynode)); - -#ifdef SATURNPAK - SendSaturnInfo(node); -#endif - memset(playeringame, 0, sizeof(playeringame)); - for (j = 0; j < MAXPLAYERS; j++) - { - if (netbuffer->u.servercfg.playerskins[j] == 0xFF - && netbuffer->u.servercfg.playercolor[j] == 0xFF) - continue; // not in game - - playeringame[j] = true; - SetPlayerSkinByNum(j, (INT32)netbuffer->u.servercfg.playerskins[j]); - players[j].skincolor = netbuffer->u.servercfg.playercolor[j]; - } - - scp = netbuffer->u.servercfg.varlengthinputs; - CV_LoadPlayerNames(&scp); - CV_LoadNetVars(&scp); -#ifdef JOININGAME - /// \note Wait. What if a Lua script uses some global custom variables synched with the NetVars hook? - /// Shouldn't them be downloaded even at intermission time? - /// Also, according to HandleConnect, the server will send the savegame even during intermission... - /// Sryder 2018-07-05: If we don't want to send the player config another way we need to send the gamestate - /// At almost any gamestate there could be joiners... So just always send gamestate? - cl_mode = ((server) ? CL_CONNECTED : CL_DOWNLOADSAVEGAME); -#else - cl_mode = CL_CONNECTED; -#endif + PT_ServerCFG(node); break; - } - // Handled in d_netfil.c case PT_FILEFRAGMENT: if (server) @@ -5275,7 +5325,10 @@ static void HandlePacketFromAwayNode(SINT8 node) Net_CloseConnection(node); break; } - SERVERONLY + + if (ServerOnly(node)) + break; + Got_Filetxpak(); break; @@ -5297,10 +5350,10 @@ static void HandlePacketFromAwayNode(SINT8 node) case PT_CLIENTCMD: break; // This is not an "unknown packet" - - case PT_PLAYERINFO: + + case PT_PLAYERINFO: HandlePlayerInfo(node); - break; + break; case PT_SERVERTICS: // Do not remove my own server (we have just get a out of order packet) @@ -5314,7 +5367,6 @@ static void HandlePacketFromAwayNode(SINT8 node) break; // Ignore it } -#undef SERVERONLY } /** Checks ticcmd for "speed hacks" @@ -5326,9 +5378,9 @@ static void HandlePacketFromAwayNode(SINT8 node) */ static boolean CheckForSpeedHacks(UINT8 p) { - if (netcmds[maketic%TICQUEUE][p].forwardmove > MAXPLMOVE || netcmds[maketic%TICQUEUE][p].forwardmove < -MAXPLMOVE - || netcmds[maketic%TICQUEUE][p].sidemove > MAXPLMOVE || netcmds[maketic%TICQUEUE][p].sidemove < -MAXPLMOVE - || netcmds[maketic%TICQUEUE][p].driftturn > KART_FULLTURN || netcmds[maketic%TICQUEUE][p].driftturn < -KART_FULLTURN) + if (netcmds[maketic%BACKUPTICS][p].forwardmove > MAXPLMOVE || netcmds[maketic%BACKUPTICS][p].forwardmove < -MAXPLMOVE + || netcmds[maketic%BACKUPTICS][p].sidemove > MAXPLMOVE || netcmds[maketic%BACKUPTICS][p].sidemove < -MAXPLMOVE + || netcmds[maketic%BACKUPTICS][p].driftturn > KART_FULLTURN || netcmds[maketic%BACKUPTICS][p].driftturn < -KART_FULLTURN) { CONS_Alert(CONS_WARNING, M_GetText("Illegal movement value received from node %d\n"), playernode[p]); SendKick(p, KICK_MSG_CON_FAIL); @@ -5354,6 +5406,8 @@ static void HandlePacketFromPlayer(SINT8 node) txtpak = NULL; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (dedicated && node == 0) netconsole = 0; else @@ -5427,7 +5481,6 @@ static void HandlePacketFromPlayer(SINT8 node) || netbuffer->packettype == PT_NODEKEEPALIVEMIS) break; - // Copy ticcmd // store it in an internal buffer so the last packet takes precedence, which minimizes input lag G_MoveTiccmd(&playercmds[netconsole], &netbuffer->u.clientpak.cmd, 1); @@ -5484,8 +5537,8 @@ static void HandlePacketFromPlayer(SINT8 node) // Check player consistancy during the level if (gamestate == GS_LEVEL - && (realstart <= gametic && realstart + TICQUEUE - 1 > gametic) - && consistancy[realstart%TICQUEUE] != SHORT(netbuffer->u.clientpak.consistancy) + && (realstart <= gametic && realstart + BACKUPTICS - 1 > gametic) + && consistancy[realstart%BACKUPTICS] != SHORT(netbuffer->u.clientpak.consistancy) && (!UseSaturnSynch(node) || (!resendingsavegame[node] && savegameresendcooldown[node] <= I_GetTime() && !SV_ResendingSavegameToAnyone()))) { resendingsavegame[node] = false; // reset this before just in case @@ -5517,12 +5570,12 @@ static void HandlePacketFromPlayer(SINT8 node) { CONS_Printf(M_GetText("Synch failure for player %d (%s); expected %hd, got %hd\n"), netconsole+1, player_names[netconsole], - consistancy[realstart%TICQUEUE], + consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy)); } DEBFILE(va("Restoring player %d (synch failure) [%update] %d!=%d\n", - netconsole, realstart, consistancy[realstart%TICQUEUE], + netconsole, realstart, consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy))); break; } @@ -5530,7 +5583,7 @@ static void HandlePacketFromPlayer(SINT8 node) { SendKick(netconsole, KICK_MSG_CON_FAIL); DEBFILE(va("player %d kicked (synch failure) [%u] %d!=%d\n", - netconsole, realstart, consistancy[realstart%TICQUEUE], + netconsole, realstart, consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy))); break; } @@ -5540,8 +5593,8 @@ static void HandlePacketFromPlayer(SINT8 node) break; #else // Check player consistancy during the level - if (realstart <= gametic && realstart > gametic - TICQUEUE+1 && gamestate == GS_LEVEL - && consistancy[realstart%TICQUEUE] != SHORT(netbuffer->u.clientpak.consistancy)) + if (realstart <= gametic && realstart > gametic - BACKUPTICS+1 && gamestate == GS_LEVEL + && consistancy[realstart%BACKUPTICS] != SHORT(netbuffer->u.clientpak.consistancy)) { SV_RequireResynch(node); @@ -5550,10 +5603,10 @@ static void HandlePacketFromPlayer(SINT8 node) if (cv_blamecfail.value) CONS_Printf(M_GetText("Synch failure for player %d (%s); expected %hd, got %hd\n"), netconsole+1, player_names[netconsole], - consistancy[realstart%TICQUEUE], + consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy)); DEBFILE(va("Restoring player %d (synch failure) [%update] %d!=%d\n", - netconsole, realstart, consistancy[realstart%TICQUEUE], + netconsole, realstart, consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy))); break; } @@ -5561,7 +5614,7 @@ static void HandlePacketFromPlayer(SINT8 node) { SendKick(netconsole, KICK_MSG_CON_FAIL); DEBFILE(va("player %d kicked (synch failure) [%u] %d!=%d\n", - netconsole, realstart, consistancy[realstart%TICQUEUE], + netconsole, realstart, consistancy[realstart%BACKUPTICS], SHORT(netbuffer->u.clientpak.consistancy))); break; } @@ -5638,10 +5691,10 @@ static void HandlePacketFromPlayer(SINT8 node) // search a tic that have enougth space in the ticcmd while ((textcmd = D_GetExistingTextcmd(tic, netconsole)), (TotalTextCmdPerTic(tic) > j || netbuffer->u.textcmd[0] + (textcmd ? textcmd[0] : 0) > MAXTEXTCMD) - && tic < firstticstosend + TICQUEUE) + && tic < firstticstosend + BACKUPTICS) tic++; - if (tic >= firstticstosend + TICQUEUE) + if (tic >= firstticstosend + BACKUPTICS) { DEBFILE(va("GetPacket: Textcmd too long (max %s, used %s, mak %d, " "tosend %u, node %u, player %d)\n", sizeu1(j), sizeu2(TotalTextCmdPerTic(maketic)), @@ -5719,8 +5772,8 @@ static void HandlePacketFromPlayer(SINT8 node) txtpak = (UINT8 *)&netbuffer->u.serverpak.cmds[netbuffer->u.serverpak.numslots * netbuffer->u.serverpak.numtics]; - if (realend > gametic + BACKUPTICS) - realend = gametic + BACKUPTICS; + if (realend > gametic + CLIENTBACKUPTICS) + realend = gametic + CLIENTBACKUPTICS; cl_packetmissed = realstart > neededtic; if (realstart <= neededtic && realend > neededtic) @@ -5734,18 +5787,25 @@ static void HandlePacketFromPlayer(SINT8 node) D_Clearticcmd(i); // copy the tics - pak = G_ScpyTiccmd(netcmds[i%TICQUEUE], pak, + pak = G_ScpyTiccmd(netcmds[i%BACKUPTICS], pak, netbuffer->u.serverpak.numslots*sizeof (ticcmd_t)); // copy the textcmds numtxtpak = *txtpak++; + for (j = 0; j < numtxtpak; j++) { - INT32 k = *txtpak++; // playernum + INT32 playernum = *txtpak++; // playernum const size_t txtsize = txtpak[0]+1; + if (playernum < 0 || playernum >= MAXPLAYERS) + { + CONS_Alert(CONS_WARNING, "Got bogus NetXCmd packet targetting player %d\n", playernum); + return; + } + if (i >= gametic) // Don't copy old net commands - M_Memcpy(D_GetTextcmd(i, k), txtpak, txtsize); + M_Memcpy(D_GetTextcmd(i, playernum), txtpak, txtsize); txtpak += txtsize; } } @@ -5854,6 +5914,8 @@ static void GetPackets(void) { SINT8 node; // The packet sender + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + while (HGetPacket()) { node = (SINT8)doomcom->remotenode; @@ -5891,7 +5953,6 @@ static void GetPackets(void) /*if (netbuffer->packettype == PT_PLAYERINFO) continue; // We do nothing with PLAYERINFO, that's for the MS browser. Not quite true anymore :p*/ - // We also count unknown packets, hence "<=" if (netbuffer->packettype <= NUMPACKETTYPE) ++packetstat[netbuffer->packettype]; @@ -6069,8 +6130,8 @@ static INT16 Consistancy(void) // used during wipes to tell the server that a node is still connected static void CL_SendClientKeepAlive(void) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); netbuffer->packettype = PT_BASICKEEPALIVE; - HSendPacket(servernode, false, 0, 0); } @@ -6078,6 +6139,8 @@ static void SV_SendServerKeepAlive(void) { INT32 n; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + for (n = 1; n < MAXNETNODES; n++) { if (nodeingame[n]) @@ -6094,6 +6157,8 @@ static void CL_SendClientCmd(void) size_t packetsize = 0; boolean mis = false; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_CLIENTCMD; if (cl_packetmissed) @@ -6167,7 +6232,7 @@ static void CL_SendClientCmd(void) packetsize = sizeof (clientcmd_pak); G_MoveTiccmd(&netbuffer->u.clientpak.cmd, &localcmds[0][lagDelay], 1); - netbuffer->u.clientpak.consistancy = SHORT(consistancy[gametic % TICQUEUE]); + netbuffer->u.clientpak.consistancy = SHORT(consistancy[gametic % BACKUPTICS]); if (splitscreen || botingame) // Send a special packet with 2 cmd for splitscreen { @@ -6248,6 +6313,8 @@ static void SV_SendTics(void) UINT8 *bufpos; UINT8 *ntextcmd; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + // send to all client but not to me // for each node create a packet with x tics and send it // x is computed using supposedtics[n], max packet size and maketic @@ -6257,11 +6324,8 @@ static void SV_SendTics(void) continue; // assert supposedtics[n]>=nettics[n] - realfirsttic = supposedtics[n]; - lasttictosend = maketic; - - if (lasttictosend - nettics[n] >= BACKUPTICS) - lasttictosend = nettics[n] + BACKUPTICS-1; + realfirsttic = max(supposedtics[n], firstticstosend); + lasttictosend = min(maketic, nettics[n] + CLIENTBACKUPTICS); if (realfirsttic >= lasttictosend) { @@ -6270,16 +6334,14 @@ static void SV_SendTics(void) // packet detection work when we have received packet with firsttic > neededtic // (getpacket servertics case) DEBFILE(va("Nothing to send node %u mak=%u sup=%u net=%u \n", - n, lasttictosend, supposedtics[n], nettics[n])); + n, lasttictosend, supposedtics[n], nettics[n])); realfirsttic = nettics[n]; if (realfirsttic >= lasttictosend || (I_GetTime() + n)&3) // all tic are ok continue; DEBFILE(va("Sent %d anyway\n", realfirsttic)); } - - if (realfirsttic < firstticstosend) - realfirsttic = firstticstosend; + realfirsttic = max(realfirsttic, firstticstosend); // compute the length of the packet and cut it if too large packsize = BASESERVERTICSSIZE; @@ -6325,7 +6387,7 @@ static void SV_SendTics(void) for (i = realfirsttic; i < lasttictosend; i++) { - bufpos = G_DcpyTiccmd(bufpos, netcmds[i%TICQUEUE], doomcom->numslots * sizeof (ticcmd_t)); + bufpos = G_DcpyTiccmd(bufpos, netcmds[i%BACKUPTICS], doomcom->numslots * sizeof (ticcmd_t)); } // add textcmds @@ -6348,16 +6410,17 @@ static void SV_SendTics(void) } } } - packsize = bufpos - (UINT8 *)&(netbuffer->u); + packsize = bufpos - (UINT8 *)&(netbuffer->u); HSendPacket(n, false, 0, packsize); + // when tic are too large, only one tic is sent so don't go backward! if (lasttictosend-doomcom->extratics > realfirsttic) supposedtics[n] = lasttictosend-doomcom->extratics; else supposedtics[n] = lasttictosend; - if (supposedtics[n] < nettics[n]) supposedtics[n] = nettics[n]; + supposedtics[n] = max(supposedtics[n], nettics[n]); } // node 0 is me! supposedtics[0] = maketic; @@ -6414,16 +6477,16 @@ void SV_SpawnPlayer(INT32 playernum, INT32 x, INT32 y, angle_t angle) for (tic = server ? maketic : (neededtic - 1); tic >= gametic; tic--) { - if (numadjust++ == TICQUEUE) + if (numadjust++ == BACKUPTICS) { DEBFILE(va("SV_SpawnPlayer: All netcmds for player %d adjusted!\n", playernum)); // We already adjusted them all, waste of time doing the same thing over and over // This shouldn't happen normally though, either gametic was 0 (which is handled now anyway) - // or maketic >= gametic + TICQUEUE + // or maketic >= gametic + BACKUPTICS // -- Monster Iestyn 16/01/18 break; } - netcmds[tic%TICQUEUE][playernum].angleturn = (INT16)((angle>>16) | TICCMD_RECEIVED); + netcmds[tic%BACKUPTICS][playernum].angleturn = (INT16)((angle>>16) | TICCMD_RECEIVED); if (!tic) // failsafe for gametic == 0 -- Monster Iestyn 16/01/18 break; @@ -6433,7 +6496,7 @@ void SV_SpawnPlayer(INT32 playernum, INT32 x, INT32 y, angle_t angle) // create missed tic static void SV_Maketic(void) { - G_MoveTiccmd(netcmds[maketic % TICQUEUE], playercmds, MAXPLAYERS); + G_MoveTiccmd(netcmds[maketic % BACKUPTICS], playercmds, MAXPLAYERS); maketic++; } @@ -6503,7 +6566,7 @@ boolean TryRunTics(tic_t realtics) G_Ticker((gametic % NEWTICRATERATIO) == 0); ExtraDataTicker(); gametic++; - consistancy[gametic%TICQUEUE] = Consistancy(); + consistancy[gametic%BACKUPTICS] = Consistancy(); if (update_stats) { @@ -6536,7 +6599,7 @@ boolean TryRunTics(tic_t realtics) static INT32 pingtimeout[MAXPLAYERS]; -#define PINGKICK_TICQUEUE 2 +#define PINGKICK_BACKUPTICS 2 #define PINGKICK_LIMIT 1 static inline void PingUpdate(void) @@ -6547,10 +6610,12 @@ static inline void PingUpdate(void) memset(pingkick, 0, sizeof(pingkick)); + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + netbuffer->packettype = PT_PING; //check for ping limit breakage. - //if (cv_maxping.value) -- always check for TICQUEUE overrun + //if (cv_maxping.value) -- always check for BACKUPTICS overrun { for (i = 0; i < MAXPLAYERS; i++) { @@ -6560,10 +6625,10 @@ static inline void PingUpdate(void) continue; } - if ((maketic + 5) >= nettics[playernode[i]] + (TICQUEUE-(2*TICRATE))) + if ((maketic + 5) >= nettics[playernode[i]] + (BACKUPTICS-(2*TICRATE))) { - // Anyone who's gobbled most of the TICQUEUE and is likely to halt the server the next few times this runs has to die *right now*. (See also NetUpdate) - pingkick[i] = PINGKICK_TICQUEUE; + // Anyone who's gobbled most of the BACKUPTICS and is likely to halt the server the next few times this runs has to die *right now*. (See also NetUpdate) + pingkick[i] = PINGKICK_BACKUPTICS; } else if ((cv_maxping.value) && (realpingtable[i] / pingmeasurecount > (unsigned)cv_maxping.value)) @@ -6585,9 +6650,9 @@ static inline void PingUpdate(void) //kick lagging players... unless everyone but the server's ping sucks. //in that case, it is probably the server's fault. - // Always kick TICQUEUE-overrunners, too. + // Always kick BACKUPTICS-overrunners, too. { - UINT8 minimumkicklevel = (nonlaggers > 0) ? PINGKICK_LIMIT : PINGKICK_TICQUEUE; + UINT8 minimumkicklevel = (nonlaggers > 0) ? PINGKICK_LIMIT : PINGKICK_BACKUPTICS; for (i = 0; i < MAXPLAYERS; i++) { if (!playeringame[i] || pingkick[i] < minimumkicklevel) @@ -6916,7 +6981,7 @@ void NetUpdate(void) continue; if (nettics[i] < firstticstosend) firstticstosend = nettics[i]; - if (maketic + counts >= nettics[i] + (TICQUEUE - TICRATE)) + if (maketic + counts >= nettics[i] + (BACKUPTICS - TICRATE)) Net_ConnectionTimeout(i); } @@ -6931,8 +6996,8 @@ void NetUpdate(void) if (counts != -666) { // See also PingUpdate - if (maketic + counts >= firstticstosend + TICQUEUE) - counts = firstticstosend+TICQUEUE-maketic-1; + if (maketic + counts >= firstticstosend + BACKUPTICS) + counts = firstticstosend+BACKUPTICS-maketic-1; for (i = 0; i < counts; i++) SV_Maketic(); // Create missed tics and increment maketic diff --git a/src/d_clisrv.h b/src/d_clisrv.h index 7ba47cfd6..979e06693 100644 --- a/src/d_clisrv.h +++ b/src/d_clisrv.h @@ -42,8 +42,8 @@ applications may follow different packet versions. #define VANILLA_GT_MATCH 3 // Networking and tick handling related. -#define BACKUPTICS 32 -#define TICQUEUE 512 // more than enough for most timeouts.... +#define BACKUPTICS 512 // more than enough for most timeouts.... +#define CLIENTBACKUPTICS 32 #define MAXTEXTCMD 256 // No. of tics your controls can be delayed by. @@ -533,7 +533,6 @@ extern UINT32 serverlistultimatecount; extern INT32 mapchangepending; // Points inside doomcom -extern doomdata_t *netbuffer; extern consvar_t cv_stunserver; extern consvar_t cv_httpsource; extern consvar_t cv_kicktime; diff --git a/src/d_net.c b/src/d_net.c index 512ee0024..3a75a29a4 100644 --- a/src/d_net.c +++ b/src/d_net.c @@ -48,12 +48,6 @@ tic_t connectiontimeout = (10*TICRATE); /// \brief network packet doomcom_t *doomcom = NULL; -/// \brief network packet data, points inside doomcom -doomdata_t *netbuffer = NULL; -/// \brief hole punching packet, also points inside doomcom -#ifdef HOLEPUNCH -holepunch_t *holepunchpacket = NULL; -#endif #ifdef DEBUGFILE FILE *debugfile = NULL; // put some net info in a file during the game @@ -225,6 +219,8 @@ static boolean GetFreeAcknum(UINT8 *freeack) node_t *node = &nodes[doomcom->remotenode]; INT32 i, numfreeslot = 0; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + if (cmpack((UINT8)((node->remotefirstack + MAXACKTOSEND) % 256), node->nextacknum) < 0) { DEBFILE(va("too fast %d %d\n",node->remotefirstack,node->nextacknum)); @@ -294,6 +290,7 @@ static boolean Processackpak(void) INT32 i; boolean goodpacket = true; node_t *node = &nodes[doomcom->remotenode]; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); // Received an ack return, so remove the ack in the list if (netbuffer->ackreturn && cmpack(node->remotefirstack, netbuffer->ackreturn) < 0) @@ -303,7 +300,7 @@ static boolean Processackpak(void) // Search the ackbuffer and free it for (i = 0; i < MAXACKPACKETS; i++) { - if (ackpak[i].acknum && ackpak[i].destinationnode == node - nodes + if (ackpak[i].acknum && ackpak[i].destinationnode == doomcom->remotenode && cmpack(ackpak[i].acknum, netbuffer->ackreturn) <= 0) { RemoveAck(i); @@ -419,6 +416,7 @@ void Net_SendAcks(INT32 node) #ifdef NONET (void)node; #else + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); netbuffer->packettype = PT_NOTHING; M_Memcpy(netbuffer->u.textcmd, nodes[node].acktosend, MAXACKTOSEND); HSendPacket(node, false, 0, MAXACKTOSEND); @@ -430,6 +428,8 @@ static void GotAcks(void) { INT32 i, j; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + for (j = 0; j < MAXACKTOSEND; j++) { if (netbuffer->u.textcmd[j]) @@ -483,6 +483,8 @@ void Net_AckTicker(void) #ifndef NONET INT32 i; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + for (i = 0; i < MAXACKPACKETS; i++) { const INT32 nodei = ackpak[i].destinationnode; @@ -538,6 +540,7 @@ void Net_UnAcknowledgePacket(INT32 node) #ifdef NONET (void)node; #else + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); INT32 hm1 = (nodes[node].acktosend_head-1+MAXACKTOSEND) % MAXACKTOSEND; DEBFILE(va("UnAcknowledge node %d\n", node)); @@ -732,6 +735,7 @@ void Net_CloseConnection(INT32 node) // static UINT32 NetbufferChecksum(void) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); UINT32 c = 0x1234567; const INT32 l = doomcom->datalength - 4; const UINT8 *buf = (UINT8 *)netbuffer + 4; @@ -848,6 +852,8 @@ const char *Net_GetPacketName(UINT8 packettype) static void DebugPrintpacket(const char *header) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); + fprintf(debugfile, "%-12s (node %d,ack %d,ackret %d,size %d) type(%d) : %s\n", header, doomcom->remotenode, netbuffer->ack, netbuffer->ackreturn, doomcom->datalength, netbuffer->packettype, Net_GetPacketName(netbuffer->packettype)); @@ -1017,6 +1023,7 @@ void Command_Droprate(void) #ifndef NONET static boolean ShouldDropPacket(void) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); return (packetdropquantity[netbuffer->packettype]) || (packetdroprate != 0 && rand() < (((double)RAND_MAX) * (packetdroprate / 100.f))) || packetdroprate == 100; } @@ -1028,6 +1035,7 @@ static boolean ShouldDropPacket(void) // boolean HSendPacket(INT32 node, boolean reliable, UINT8 acknum, size_t packetlength) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); doomcom->datalength = (INT16)(packetlength + BASEPACKETSIZE); if (node == 0) // Packet is to go back to us @@ -1128,7 +1136,7 @@ boolean HSendPacket(INT32 node, boolean reliable, UINT8 acknum, size_t packetlen // boolean HGetPacket(void) { - //boolean nodejustjoined; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); // Get a packet from self if (rebound_tail != rebound_head) @@ -1216,6 +1224,18 @@ static void Internal_FreeNodenum(INT32 nodenum) (void)nodenum; } +static char *I_NetSplitAddress(char *host, char **port) +{ + boolean v4 = (host[0] != '['); + + host = strtok(host, v4 ? ":" : "[]"); + + if (port) + *port = strtok(NULL, ":"); + + return host; +} + SINT8 I_NetMakeNode(const char *hostname) { SINT8 newnode = -1; @@ -1223,20 +1243,14 @@ SINT8 I_NetMakeNode(const char *hostname) if (I_NetMakeNodewPort) { char *localhostname = strdup(hostname); - char *t = localhostname; - const char *port; + char *port; if (!localhostname) return newnode; + // retrieve portnum from address! - strtok(localhostname, ":"); - port = strtok(NULL, ":"); + hostname = I_NetSplitAddress(localhostname, &port); - // remove the port in the hostname as we've it already - while ((*t != ':') && (*t != '\0')) - t++; - *t = '\0'; - - newnode = I_NetMakeNodewPort(localhostname, port); + newnode = I_NetMakeNodewPort(hostname, port); free(localhostname); } return newnode; @@ -1328,11 +1342,6 @@ boolean D_CheckNetGame(void) if (doomcom->numnodes > MAXNETNODES) I_Error("Too many nodes (%d), max:%d", doomcom->numnodes, MAXNETNODES); - netbuffer = (doomdata_t *)(void *)&doomcom->data; -#ifdef HOLEPUNCH - holepunchpacket = (holepunch_t *)(void *)&doomcom->data; -#endif - #ifdef DEBUGFILE if (M_CheckParm("-debugfile")) { diff --git a/src/d_netfil.c b/src/d_netfil.c index 0650f3c34..07401eb46 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -140,6 +140,7 @@ UINT8 *PutFileNeeded(UINT16 firstfile) { size_t i; UINT8 count = 0; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); UINT8 *p_start = netbuffer->packettype == PT_MOREFILESNEEDED ? netbuffer->u.filesneededcfg.files : netbuffer->u.serverinfo.fileneeded; UINT8 *p = p_start; char wadfilename[MAX_WADPATH] = ""; @@ -321,6 +322,7 @@ boolean CL_SendRequestFile(void) #ifdef MORELEGACYDOWNLOADER boolean firstloop = true; #endif + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); #ifdef PARANOIA if (M_CheckParm("-nodownload")) @@ -449,9 +451,11 @@ tryagain: // returns false if a requested file was not found or cannot be sent boolean Got_RequestFilePak(INT32 node) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); char wad[MAX_WADPATH+1]; UINT8 *p = netbuffer->u.textcmd; UINT8 id; + while (p < netbuffer->u.textcmd + MAXTEXTCMD) // Don't allow hacked client to overflow { id = READUINT8(p); @@ -815,6 +819,7 @@ void SV_FileSendTicker(void) size_t size; filetx_t *f; INT32 packetsent, ram, i, j; + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); if (!filestosend) // No file to send return; @@ -943,6 +948,7 @@ void SV_FileSendTicker(void) void Got_Filetxpak(void) { + doomdata_t *netbuffer = DOOMCOM_DATA(doomcom); INT32 filenum = netbuffer->u.filetxpak.fileid; fileneeded_t *file = &fileneeded[filenum]; char *filename = file->filename; diff --git a/src/doomstat.h b/src/doomstat.h index 63c483a32..09102a772 100644 --- a/src/doomstat.h +++ b/src/doomstat.h @@ -576,7 +576,7 @@ extern consvar_t cv_downloading; // allow clients to downloading WADs. extern consvar_t cv_nettimeout; // SRB2Kart: Advanced server options menu extern consvar_t cv_jointimeout; extern consvar_t cv_maxping; -extern ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; +extern ticcmd_t netcmds[BACKUPTICS][MAXPLAYERS]; extern INT32 serverplayer; extern INT32 adminplayers[MAXPLAYERS]; diff --git a/src/g_game.c b/src/g_game.c index e046a4f6e..e59d788ac 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -1855,7 +1855,7 @@ void G_Ticker(boolean run) } } - buf = gametic % TICQUEUE; + buf = gametic % BACKUPTICS; if (!demo.playback) { diff --git a/src/http-mserv.c b/src/http-mserv.c index 2831e34e6..4cab52acf 100644 --- a/src/http-mserv.c +++ b/src/http-mserv.c @@ -220,7 +220,11 @@ HMS_connect (const char *format, ...) curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(curl, CURLOPT_IPRESOLVE, (long)CURL_IPRESOLVE_V4); + +#ifdef HAVE_IPV6 + if (!M_CheckParm("-ipv6")) +#endif + curl_easy_setopt(curl, CURLOPT_IPRESOLVE, (long)CURL_IPRESOLVE_V4); curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)cv_masterserver_timeout.value); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HMS_on_read); diff --git a/src/i_net.h b/src/i_net.h index 70d8f0384..bdbad182e 100644 --- a/src/i_net.h +++ b/src/i_net.h @@ -82,6 +82,8 @@ typedef struct char data[MAXPACKETLENGTH]; } ATTRPACK doomcom_t; +#define DOOMCOM_DATA(d) (doomdata_t *)&(d)->data + #ifdef HOLEPUNCH typedef struct { @@ -89,6 +91,8 @@ typedef struct INT32 addr; INT16 port; } ATTRPACK holepunch_t; + +#define HOLEPUNCH_DATA(d) (holepunch_t *)&(d)->data #endif #if defined(_MSC_VER) @@ -97,10 +101,6 @@ typedef struct extern doomcom_t *doomcom; -#ifdef HOLEPUNCH -extern holepunch_t *holepunchpacket; -#endif - /** \brief return packet in doomcom struct */ extern boolean (*I_NetGet)(void); diff --git a/src/i_tcp.c b/src/i_tcp.c index 8151abce0..37c08f6d5 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -371,8 +371,16 @@ static inline void I_UPnP_rem(const char *port, const char * servicetype) static const char *SOCK_AddrToStr(mysockaddr_t *sk) { - static char s[64]; // 255.255.255.255:65535 or IPv6:65535 + static char s[64]; // 255.255.255.255:65535 or + // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535 + #ifdef HAVE_NTOP +#ifdef HAVE_IPV6 + int v6 = (sk->any.sa_family == AF_INET6); +#else + int v6 = 0; +#endif + void *addr; int e = 0; // save error code so it can't be modified later code and avoid calling WSAGetLastError() more then once @@ -387,14 +395,20 @@ static const char *SOCK_AddrToStr(mysockaddr_t *sk) if(addr == NULL) sprintf(s, "No address"); - else if(inet_ntop(sk->any.sa_family, addr, s, sizeof (s)) == NULL) + else if(inet_ntop(sk->any.sa_family, addr, &s[v6], sizeof (s) - v6) == NULL) { e = errno; sprintf(s, "Unknown family type, error #%u: %s", e, strerror(e)); } #ifdef HAVE_IPV6 - else if(sk->any.sa_family == AF_INET6 && sk->ip6.sin6_port != 0) - strcat(s, va(":%d", ntohs(sk->ip6.sin6_port))); + else if(sk->any.sa_family == AF_INET6) + { + s[0] = '['; + strcat(s, "]"); + + if (sk->ip6.sin6_port != 0) + strcat(s, va(":%d", ntohs(sk->ip6.sin6_port))); + } #endif else if(sk->any.sa_family == AF_INET && sk->ip4.sin_port != 0) strcat(s, va(":%d", ntohs(sk->ip4.sin_port))); @@ -486,10 +500,30 @@ static time_t SOCK_GetUnbanTime(size_t ban) } #ifndef NONET + +#ifdef HAVE_IPV6 +static boolean SOCK_cmpipv6(mysockaddr_t *a, mysockaddr_t *b, UINT8 mask) +{ + UINT8 bitmask; + I_Assert(mask <= 128); + if (mask == 0) + mask = 128; + if (memcmp(&a->ip6.sin6_addr.s6_addr, &b->ip6.sin6_addr.s6_addr, mask / 8) != 0) + return false; + if (mask % 8 == 0) + return true; + bitmask = 255 << (mask % 8); + return (a->ip6.sin6_addr.s6_addr[mask / 8] & bitmask) == (b->ip6.sin6_addr.s6_addr[mask / 8] & bitmask); +} +#endif + static boolean SOCK_cmpaddr(mysockaddr_t *a, mysockaddr_t *b, UINT8 mask) { UINT32 bitmask = INADDR_NONE; + if (a->any.sa_family != b->any.sa_family) + return false; + if (mask && mask < 32) bitmask = htonl((UINT32)(-1) << (32 - mask)); @@ -498,13 +532,14 @@ static boolean SOCK_cmpaddr(mysockaddr_t *a, mysockaddr_t *b, UINT8 mask) && (b->ip4.sin_port == 0 || (a->ip4.sin_port == b->ip4.sin_port)); #ifdef HAVE_IPV6 else if (b->any.sa_family == AF_INET6) - return memcmp(&a->ip6.sin6_addr, &b->ip6.sin6_addr, sizeof(b->ip6.sin6_addr)) + return SOCK_cmpipv6(a, b, mask) && (b->ip6.sin6_port == 0 || (a->ip6.sin6_port == b->ip6.sin6_port)); #endif else return false; } + // This is a hack. For some reason, nodes aren't being freed properly. // This goes through and cleans up what nodes were supposed to be freed. /** \warning This function causes the file downloading to stop if someone joins. @@ -620,6 +655,7 @@ is_external_address (UINT32 p) static boolean hole_punch(ssize_t c) { + holepunch_t *holepunchpacket = HOLEPUNCH_DATA(doomcom); /* See ../doc/Holepunch-Protocol.txt */ if (cv_rendezvousserver.string[0] && @@ -659,6 +695,7 @@ static boolean SOCK_Get(void) fromlen = (socklen_t)sizeof(fromaddress); c = recvfrom(mysockets[n], (char *)&doomcom->data, MAXPACKETLENGTH, 0, (void *)&fromaddress, &fromlen); + if (c > 0) { #ifdef USE_STUN @@ -868,11 +905,11 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen unsigned long trueval = true; #endif mysockaddr_t straddr; - struct sockaddr_in sin; - socklen_t len = sizeof(sin); + socklen_t len = sizeof(straddr); if (s == (SOCKET_TYPE)ERRSOCKET) return (SOCKET_TYPE)ERRSOCKET; + #ifdef USE_WINSOCK { // Alam_GBC: disable the new UDP connection reset behavior for Win2k and up #ifdef USE_WINSOCK2 @@ -892,14 +929,12 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen } #endif - straddr.any = *addr; + memcpy(&straddr, addr, addrlen); I_OutputMsg("Binding to %s\n", SOCK_AddrToStr(&straddr)); if (family == AF_INET) { - mysockaddr_t tmpaddr; - tmpaddr.any = *addr ; - if (tmpaddr.ip4.sin_addr.s_addr == htonl(INADDR_ANY)) + if (straddr.ip4.sin_addr.s_addr == htonl(INADDR_ANY)) { opt = true; opts = (socklen_t)sizeof(opt); @@ -924,7 +959,7 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen #ifdef HAVE_IPV6 else if (family == AF_INET6) { - if (memcmp(addr, &in6addr_any, sizeof(in6addr_any)) == 0) //IN6_ARE_ADDR_EQUAL + if (memcmp(&straddr.ip6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) //IN6_ARE_ADDR_EQUAL { opt = true; opts = (socklen_t)sizeof(opt); @@ -939,7 +974,7 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen // make it IPv6 ony opt = true; opts = (socklen_t)sizeof(opt); - rc = setsockopt(s, SOL_SOCKET, IPV6_V6ONLY, (char *)&opt, opts); + rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&opt, opts); if (rc <= -1) { e = errno; @@ -969,7 +1004,7 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen inet_pton(AF_INET6, IPV6_MULTICAST_ADDRESS, &maddr.ipv6mr_multiaddr); maddr.ipv6mr_interface = 0; - rc = setsockopt(s, SOL_SOCKET, IPV6_JOIN_GROUP, (const char *)&maddr, sizeof(maddr)); + rc = setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, (const char *)&maddr, sizeof(maddr)); if (rc <= -1) { e = errno; @@ -1032,7 +1067,7 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen CONS_Printf(M_GetText("Network system buffer set to: %dKb\n"), opt>>10); } - rc = getsockname(s, (struct sockaddr *)&sin, &len); + rc = getsockname(s, &straddr.any, &len); if (rc != 0) { e = errno; @@ -1040,7 +1075,14 @@ static SOCKET_TYPE UDP_Bind(int family, struct sockaddr *addr, socklen_t addrlen I_OutputMsg("getsockname failed: #%u, %s\n", e, strerror(e)); } else - current_port = (UINT16)ntohs(sin.sin_port); + { + if (family == AF_INET) + current_port = (UINT16)ntohs(straddr.ip4.sin_port); +#ifdef HAVE_IPV6 + else if (family == AF_INET6) + current_port = (UINT16)ntohs(straddr.ip6.sin6_port); +#endif + } return s; } @@ -1121,6 +1163,7 @@ static boolean UDP_Socket(void) I_freeaddrinfo(ai); } } + #ifdef HAVE_IPV6 if (b_ipv6) { @@ -1319,12 +1362,13 @@ static boolean SOCK_GetAddr(struct sockaddr_in *sin, const char *address, const { struct my_addrinfo *ai = NULL, *runp, hints; int gaie; + size_t i; if (!port || !port[0]) port = DEFAULTPORT; memset (&hints, 0x00, sizeof (hints)); - hints.ai_flags = 0; + hints.ai_flags = AI_ADDRCONFIG; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; @@ -1343,10 +1387,19 @@ static boolean SOCK_GetAddr(struct sockaddr_in *sin, const char *address, const { while (runp != NULL) { - if (sendto(mysockets[0], NULL, 0, 0, runp->ai_addr, runp->ai_addrlen) == 0) - break; + // test ip address of server + for (i = 0; i < mysocketses; ++i) + { + if (runp->ai_addr->sa_family == myfamily[i]) + { + break; + } + } - runp = runp->ai_next; + if (i >= mysocketses) + runp = runp->ai_next; + else + break; } } @@ -1390,6 +1443,8 @@ static void rendezvous(int size) tic_t tic = I_GetTime(); + holepunch_t *holepunchpacket = HOLEPUNCH_DATA(doomcom); + if (tic != refreshtic) { if (SOCK_GetAddr(&rzv.ip4, host, (port ? port : "7777"), false)) @@ -1415,6 +1470,7 @@ static void rendezvous(int size) static void SOCK_RequestHolePunch(INT32 node) { mysockaddr_t * addr = &clientaddress[node]; + holepunch_t *holepunchpacket = HOLEPUNCH_DATA(doomcom); holepunchpacket->addr = addr->ip4.sin_addr.s_addr; holepunchpacket->port = addr->ip4.sin_port; @@ -1742,7 +1798,7 @@ boolean I_InitTcpNetwork(void) // server address only in ip if (serverhostname[0]) - { + { CONS_Printf("SERVERNAME: %s\n", serverhostname); COM_BufAddText("connect \""); COM_BufAddText(serverhostname); diff --git a/src/mserv.h b/src/mserv.h index 8142716ef..0b4e09bb8 100644 --- a/src/mserv.h +++ b/src/mserv.h @@ -30,7 +30,11 @@ typedef union typedef struct { msg_header_t header; +#ifndef HAVE_IPV6 char ip[16]; +#else + char ip[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"]; +#endif char port[8]; char contact[32]; char version[8]; // format is: x.yy.z (like 1.30.2 or 1.31) diff --git a/src/p_setup.c b/src/p_setup.c index 85e4d16ab..5cb6eafea 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -3065,7 +3065,7 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) if (!fromnetsave) // uglier hack { // to make a newly loaded level start on the second frame. - INT32 buf = gametic % TICQUEUE; + INT32 buf = gametic % BACKUPTICS; for (i = 0; i < MAXPLAYERS; i++) {