2018-06-01 19:44:16 +02:00
|
|
|
/*
|
|
|
|
masterlist.c - multi-master list
|
|
|
|
Copyright (C) 2018 mittorn
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
#include "common.h"
|
|
|
|
#include "netchan.h"
|
2023-04-02 23:57:47 +02:00
|
|
|
#include "server.h"
|
2018-06-01 19:44:16 +02:00
|
|
|
|
|
|
|
typedef struct master_s
|
|
|
|
{
|
|
|
|
struct master_s *next;
|
2023-04-02 23:57:47 +02:00
|
|
|
qboolean sent; // TODO: get rid of this internal state
|
2018-06-01 19:44:16 +02:00
|
|
|
qboolean save;
|
|
|
|
string address;
|
2022-08-17 20:17:51 +02:00
|
|
|
netadr_t adr; // temporary, rewritten after each send
|
2023-04-02 23:57:47 +02:00
|
|
|
|
|
|
|
uint heartbeat_challenge;
|
|
|
|
double last_heartbeat;
|
2018-06-01 19:44:16 +02:00
|
|
|
} master_t;
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
static struct masterlist_s
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
|
|
|
master_t *list;
|
|
|
|
qboolean modified;
|
|
|
|
} ml;
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
static CVAR_DEFINE_AUTO( sv_verbose_heartbeats, "0", 0, "print every heartbeat to console" );
|
|
|
|
|
|
|
|
#define HEARTBEAT_SECONDS ((sv_nat.value > 0.0f) ? 60.0f : 300.0f) // 1 or 5 minutes
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_GetMasterHostByName
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
static net_gai_state_t NET_GetMasterHostByName( master_t *m )
|
|
|
|
{
|
|
|
|
net_gai_state_t res = NET_StringToAdrNB( m->address, &m->adr );
|
|
|
|
|
|
|
|
if( res == NET_EAI_OK )
|
|
|
|
return res;
|
|
|
|
|
|
|
|
m->adr.type = NA_UNUSED;
|
|
|
|
if( res == NET_EAI_NONAME )
|
|
|
|
Con_Reportf( "Can't resolve adr: %s\n", m->address );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:44:16 +02:00
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_SendToMasters
|
|
|
|
|
|
|
|
Send request to all masterservers list
|
|
|
|
return true if would block
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
qboolean NET_SendToMasters( netsrc_t sock, size_t len, const void *data )
|
|
|
|
{
|
|
|
|
master_t *list;
|
|
|
|
qboolean wait = false;
|
|
|
|
|
|
|
|
for( list = ml.list; list; list = list->next )
|
|
|
|
{
|
|
|
|
if( list->sent )
|
|
|
|
continue;
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
switch( NET_GetMasterHostByName( list ))
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
2023-04-02 23:57:47 +02:00
|
|
|
case NET_EAI_AGAIN:
|
|
|
|
list->sent = false;
|
|
|
|
wait = true;
|
|
|
|
break;
|
|
|
|
case NET_EAI_NONAME:
|
2018-06-01 19:44:16 +02:00
|
|
|
list->sent = true;
|
2023-04-02 23:57:47 +02:00
|
|
|
break;
|
|
|
|
case NET_EAI_OK:
|
|
|
|
list->sent = true;
|
|
|
|
NET_SendPacket( sock, len, data, list->adr );
|
|
|
|
break;
|
2018-06-01 19:44:16 +02:00
|
|
|
}
|
2023-04-02 23:57:47 +02:00
|
|
|
}
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
if( !wait )
|
|
|
|
{
|
|
|
|
// reset sent state
|
|
|
|
for( list = ml.list; list; list = list->next )
|
2018-06-01 19:44:16 +02:00
|
|
|
list->sent = false;
|
2023-04-02 23:57:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return wait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_AnnounceToMaster
|
|
|
|
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
static void NET_AnnounceToMaster( master_t *m )
|
|
|
|
{
|
|
|
|
sizebuf_t msg;
|
|
|
|
char buf[16];
|
|
|
|
|
|
|
|
m->heartbeat_challenge = COM_RandomLong( 0, INT_MAX );
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
MSG_Init( &msg, "Master Join", buf, sizeof( buf ));
|
|
|
|
MSG_WriteBytes( &msg, "q\xFF", 2 );
|
|
|
|
MSG_WriteDword( &msg, m->heartbeat_challenge );
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
NET_SendPacket( NS_SERVER, MSG_GetNumBytesWritten( &msg ), MSG_GetBuf( &msg ), m->adr );
|
|
|
|
|
|
|
|
if( sv_verbose_heartbeats.value )
|
|
|
|
{
|
|
|
|
Con_Printf( S_NOTE "sent heartbeat to %s (%s, 0x%x)\n",
|
|
|
|
m->address, NET_AdrToString( m->adr ), m->heartbeat_challenge );
|
2018-06-01 19:44:16 +02:00
|
|
|
}
|
2023-04-02 23:57:47 +02:00
|
|
|
}
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_AnnounceToMaster
|
|
|
|
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void NET_MasterClear( void )
|
|
|
|
{
|
|
|
|
master_t *m;
|
|
|
|
|
|
|
|
for( m = ml.list; m; m = m->next )
|
|
|
|
m->last_heartbeat = MAX_HEARTBEAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_MasterHeartbeat
|
|
|
|
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void NET_MasterHeartbeat( void )
|
|
|
|
{
|
|
|
|
master_t *m;
|
|
|
|
|
|
|
|
if(( !public_server.value && !sv_nat.value ) || svs.maxclients == 1 )
|
|
|
|
return; // only public servers send heartbeats
|
|
|
|
|
|
|
|
for( m = ml.list; m; m = m->next )
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
2023-04-02 23:57:47 +02:00
|
|
|
if( host.realtime - m->last_heartbeat < HEARTBEAT_SECONDS )
|
|
|
|
continue;
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
switch( NET_GetMasterHostByName( m ))
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
2023-04-02 23:57:47 +02:00
|
|
|
case NET_EAI_AGAIN:
|
|
|
|
m->last_heartbeat = MAX_HEARTBEAT; // retry on next frame
|
|
|
|
if( sv_verbose_heartbeats.value )
|
|
|
|
Con_Printf( S_NOTE "delay heartbeat to next frame until %s resolves\n", m->address );
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NET_EAI_NONAME:
|
|
|
|
m->last_heartbeat = host.realtime; // try to resolve again on next heartbeat
|
|
|
|
break;
|
|
|
|
case NET_EAI_OK:
|
|
|
|
m->last_heartbeat = host.realtime;
|
|
|
|
NET_AnnounceToMaster( m );
|
|
|
|
break;
|
2018-06-01 19:44:16 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 23:57:47 +02:00
|
|
|
}
|
2018-06-01 19:44:16 +02:00
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
/*
|
|
|
|
=================
|
|
|
|
NET_MasterShutdown
|
|
|
|
|
|
|
|
Informs all masters that this server is going down
|
|
|
|
(ignored by master servers in current implementation)
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void NET_MasterShutdown( void )
|
|
|
|
{
|
|
|
|
NET_Config( true, false ); // allow remote
|
|
|
|
while( NET_SendToMasters( NS_SERVER, 2, "\x62\x0A" ));
|
2018-06-01 19:44:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
|
2022-08-17 20:17:51 +02:00
|
|
|
/*
|
|
|
|
========================
|
2023-04-02 23:57:47 +02:00
|
|
|
NET_GetMasterFromAdr
|
2022-08-17 20:17:51 +02:00
|
|
|
|
|
|
|
========================
|
|
|
|
*/
|
2023-04-02 23:57:47 +02:00
|
|
|
static master_t *NET_GetMasterFromAdr( netadr_t adr )
|
2022-08-17 20:17:51 +02:00
|
|
|
{
|
|
|
|
master_t *master;
|
|
|
|
|
|
|
|
for( master = ml.list; master; master = master->next )
|
|
|
|
{
|
|
|
|
if( NET_CompareAdr( adr, master->adr ))
|
2023-04-02 23:57:47 +02:00
|
|
|
return master;
|
2022-08-17 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_GetMaster
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
qboolean NET_GetMaster( netadr_t from, uint *challenge, double *last_heartbeat )
|
|
|
|
{
|
|
|
|
master_t *m;
|
|
|
|
|
|
|
|
m = NET_GetMasterFromAdr( from );
|
|
|
|
|
|
|
|
if( m )
|
|
|
|
{
|
|
|
|
*challenge = m->heartbeat_challenge;
|
|
|
|
*last_heartbeat = m->last_heartbeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_IsMasterAdr
|
|
|
|
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
qboolean NET_IsMasterAdr( netadr_t adr )
|
|
|
|
{
|
|
|
|
return NET_GetMasterFromAdr( adr ) != NULL;
|
2022-08-17 20:17:51 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 19:44:16 +02:00
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_AddMaster
|
|
|
|
|
|
|
|
Add master to the list
|
|
|
|
========================
|
|
|
|
*/
|
2018-10-06 02:34:01 +02:00
|
|
|
static void NET_AddMaster( const char *addr, qboolean save )
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
|
|
|
master_t *master, *last;
|
|
|
|
|
|
|
|
for( last = ml.list; last && last->next; last = last->next )
|
|
|
|
{
|
|
|
|
if( !Q_strcmp( last->address, addr ) ) // already exists
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:04:50 +02:00
|
|
|
master = Mem_Malloc( host.mempool, sizeof( master_t ) );
|
2018-06-01 19:44:16 +02:00
|
|
|
Q_strncpy( master->address, addr, MAX_STRING );
|
|
|
|
master->sent = false;
|
|
|
|
master->save = save;
|
|
|
|
master->next = NULL;
|
2022-08-17 20:17:51 +02:00
|
|
|
master->adr.type = NA_UNUSED;
|
2018-06-01 19:44:16 +02:00
|
|
|
|
|
|
|
// link in
|
|
|
|
if( last )
|
|
|
|
last->next = master;
|
|
|
|
else
|
|
|
|
ml.list = master;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NET_AddMaster_f( void )
|
|
|
|
{
|
|
|
|
if( Cmd_Argc() != 2 )
|
|
|
|
{
|
|
|
|
Msg( S_USAGE "addmaster <address>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_AddMaster( Cmd_Argv( 1 ), true ); // save them into config
|
|
|
|
ml.modified = true; // save config
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_ClearMasters
|
|
|
|
|
|
|
|
Clear master list
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
static void NET_ClearMasters_f( void )
|
|
|
|
{
|
|
|
|
while( ml.list )
|
|
|
|
{
|
|
|
|
master_t *prev = ml.list;
|
|
|
|
ml.list = ml.list->next;
|
|
|
|
Mem_Free( prev );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_ListMasters_f
|
|
|
|
|
|
|
|
Display current master linked list
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
static void NET_ListMasters_f( void )
|
|
|
|
{
|
|
|
|
master_t *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Msg( "Master servers\n=============\n" );
|
|
|
|
|
|
|
|
|
|
|
|
for( i = 1, list = ml.list; list; i++, list = list->next )
|
|
|
|
{
|
2022-08-17 20:17:51 +02:00
|
|
|
Msg( "%d\t%s", i, list->address );
|
|
|
|
if( list->adr.type != NA_UNUSED )
|
|
|
|
Msg( "\t%s\n", NET_AdrToString( list->adr ));
|
|
|
|
else Msg( "\n" );
|
2018-06-01 19:44:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_LoadMasters
|
|
|
|
|
|
|
|
Load master server list from xashcomm.lst
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
static void NET_LoadMasters( void )
|
|
|
|
{
|
2019-07-13 22:25:03 +02:00
|
|
|
byte *afile;
|
|
|
|
char *pfile;
|
2018-06-01 19:44:16 +02:00
|
|
|
char token[MAX_TOKEN];
|
|
|
|
|
2019-07-13 22:25:03 +02:00
|
|
|
afile = FS_LoadFile( "xashcomm.lst", NULL, true );
|
2018-06-01 19:44:16 +02:00
|
|
|
|
|
|
|
if( !afile ) // file doesn't exist yet
|
|
|
|
{
|
2018-11-16 13:25:04 +01:00
|
|
|
Con_Reportf( "Cannot load xashcomm.lst\n" );
|
2018-06-01 19:44:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:25:03 +02:00
|
|
|
pfile = (char*)afile;
|
|
|
|
|
2018-06-01 19:44:16 +02:00
|
|
|
// format: master <addr>\n
|
2021-10-01 19:40:36 +02:00
|
|
|
while( ( pfile = COM_ParseFile( pfile, token, sizeof( token ) ) ) )
|
2018-06-01 19:44:16 +02:00
|
|
|
{
|
|
|
|
if( !Q_strcmp( token, "master" ) ) // load addr
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ) );
|
2018-06-01 19:44:16 +02:00
|
|
|
|
|
|
|
NET_AddMaster( token, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mem_Free( afile );
|
|
|
|
|
|
|
|
ml.modified = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_SaveMasters
|
|
|
|
|
|
|
|
Save master server list to xashcomm.lst, except for default
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void NET_SaveMasters( void )
|
|
|
|
{
|
|
|
|
file_t *f;
|
|
|
|
master_t *m;
|
|
|
|
|
|
|
|
if( !ml.modified )
|
|
|
|
{
|
2018-10-27 23:39:29 +02:00
|
|
|
Con_Reportf( "Master server list not changed\n" );
|
2018-06-01 19:44:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = FS_Open( "xashcomm.lst", "w", true );
|
|
|
|
|
|
|
|
if( !f )
|
|
|
|
{
|
2018-10-27 23:39:29 +02:00
|
|
|
Con_Reportf( S_ERROR "Couldn't write xashcomm.lst\n" );
|
2018-06-01 19:44:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( m = ml.list; m; m = m->next )
|
|
|
|
{
|
|
|
|
if( m->save )
|
|
|
|
FS_Printf( f, "master %s\n", m->address );
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Close( f );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
NET_InitMasters
|
|
|
|
|
|
|
|
Initialize master server list
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void NET_InitMasters( void )
|
|
|
|
{
|
|
|
|
Cmd_AddRestrictedCommand( "addmaster", NET_AddMaster_f, "add address to masterserver list" );
|
|
|
|
Cmd_AddRestrictedCommand( "clearmasters", NET_ClearMasters_f, "clear masterserver list" );
|
|
|
|
Cmd_AddCommand( "listmasters", NET_ListMasters_f, "list masterservers" );
|
|
|
|
|
2023-04-02 23:57:47 +02:00
|
|
|
Cvar_RegisterVariable( &sv_verbose_heartbeats );
|
|
|
|
|
2018-06-01 19:44:16 +02:00
|
|
|
// keep main master always there
|
|
|
|
NET_AddMaster( MASTERSERVER_ADR, false );
|
2023-10-22 18:34:07 +02:00
|
|
|
NET_AddMaster( MASTERSERVER_ADR_TEST, false );
|
2018-06-01 19:44:16 +02:00
|
|
|
NET_LoadMasters( );
|
|
|
|
}
|