#define MASTER_PORT 27950
-cvar_t sv_public = {0, "sv_public", "0"};
+cvar_t sv_public = {0, "sv_public", "1"};
static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "180"};
+// FIXME: resolve DNS on masters whenever their value changes and cache it (to avoid major delays in active servers when they heartbeat)
static cvar_t sv_masters [] =
{
{CVAR_SAVE, "sv_master1", ""},
{CVAR_SAVE, "sv_master2", ""},
{CVAR_SAVE, "sv_master3", ""},
{CVAR_SAVE, "sv_master4", ""},
- {0, "sv_masterextra1", "69.59.212.88"},
- {0, "sv_masterextra2", "66.28.32.64"},
+ {0, "sv_masterextra1", "69.59.212.88"}, // ghdigital.com
+ {0, "sv_masterextra2", "66.169.205.13"}, // dpmaster.deathmask.net
+ {0, "sv_masterextra3", "12.166.196.192"}, // blaze.mindphukd.org
{0, NULL, NULL}
};
static double nextheartbeattime = 0;
sizebuf_t net_message;
+static qbyte net_message_buf[NET_MAXMESSAGE];
cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10"};
cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
cvar_t developer_networking = {0, "developer_networking", "0"};
-cvar_t cl_fakelocalping_min = {0, "cl_fakelocalping_min","0"};
-cvar_t cl_fakelocalping_max = {0, "cl_fakelocalping_max","0"};
-static cvar_t cl_fakepacketloss_receive = {0, "cl_fakepacketloss_receive","0"};
-static cvar_t cl_fakepacketloss_send = {0, "cl_fakepacketloss_send","0"};
-static cvar_t sv_fakepacketloss_receive = {0, "sv_fakepacketloss_receive","0"};
-static cvar_t sv_fakepacketloss_send = {0, "sv_fakepacketloss_send","0"};
+cvar_t cl_netlocalping = {0, "cl_netlocalping","0"};
+static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0"};
/* statistic counters */
int serverquerycount = 0;
int serverreplycount = 0;
-int hostCacheCount = 0;
-hostcache_t hostcache[HOSTCACHESIZE];
-
static qbyte sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
static qbyte readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
cvar_t net_address = {0, "net_address", "0.0.0.0"};
//cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]"};
+// ServerList interface
+serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
+serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
+
+serverlist_infofield_t serverlist_sortbyfield;
+qboolean serverlist_sortdescending;
+
+int serverlist_viewcount = 0;
+serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
+
+int serverlist_cachecount;
+serverlist_entry_t serverlist_cache[SERVERLIST_TOTALSIZE];
+
+qboolean serverlist_consoleoutput;
+
+// helper function to insert a value into the viewset
+// spare entries will be removed
+static void _ServerList_ViewList_Helper_InsertBefore( int index, serverlist_entry_t *entry )
+{
+ int i;
+ if( serverlist_viewcount == SERVERLIST_VIEWLISTSIZE )
+ return;
+
+ for( i = serverlist_viewcount ; i > index ; i-- )
+ serverlist_viewlist[ i ] = serverlist_viewlist[ i - 1 ];
+
+ serverlist_viewlist[index] = entry;
+ serverlist_viewcount++;
+}
+
+// we suppose serverlist_viewcount to be valid, ie > 0
+static void _ServerList_ViewList_Helper_Remove( int index )
+{
+ serverlist_viewcount--;
+ for( ; index < serverlist_viewcount ; index++ )
+ serverlist_viewlist[index] = serverlist_viewlist[index + 1];
+}
+
+// returns true if A should be inserted before B
+static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_entry_t *B )
+{
+ int result = 0; // > 0 if for numbers A > B and for text if A < B
+
+ switch( serverlist_sortbyfield ) {
+ case SLIF_PING:
+ result = A->info.ping - B->info.ping;
+ break;
+ case SLIF_MAXPLAYERS:
+ result = A->info.maxplayers - B->info.maxplayers;
+ break;
+ case SLIF_NUMPLAYERS:
+ result = A->info.numplayers - B->info.numplayers;
+ break;
+ case SLIF_PROTOCOL:
+ result = A->info.protocol - B->info.protocol;
+ break;
+ case SLIF_CNAME:
+ result = strcmp( B->info.cname, A->info.cname );
+ break;
+ case SLIF_GAME:
+ result = strcmp( B->info.game, A->info.game );
+ break;
+ case SLIF_MAP:
+ result = strcmp( B->info.map, A->info.map );
+ break;
+ case SLIF_MOD:
+ result = strcmp( B->info.mod, A->info.mod );
+ break;
+ case SLIF_NAME:
+ result = strcmp( B->info.name, A->info.name );
+ break;
+ default:
+ Con_DPrint( "_ServerList_Entry_Compare: Bad serverlist_sortbyfield!\n" );
+ break;
+ }
+
+ if( serverlist_sortdescending )
+ return result > 0;
+ return result < 0;
+}
+
+static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
+{
+ // This should actually be done with some intermediate and end-of-function return
+ switch( op ) {
+ case SLMO_LESS:
+ return A < B;
+ case SLMO_LESSEQUAL:
+ return A <= B;
+ case SLMO_EQUAL:
+ return A == B;
+ case SLMO_GREATER:
+ return A > B;
+ case SLMO_NOTEQUAL:
+ return A != B;
+ case SLMO_GREATEREQUAL:
+ return A >= B;
+ default:
+ Con_DPrint( "_ServerList_CompareInt: Bad op!\n" );
+ return false;
+ }
+}
+
+static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
+{
+ // Same here, also using an intermediate & final return would be more appropriate
+ // A info B mask
+ switch( op ) {
+ case SLMO_CONTAINS:
+ return *B && !!strstr( A, B ); // we want a real bool
+ case SLMO_NOTCONTAIN:
+ return !*B || !strstr( A, B );
+ case SLMO_LESS:
+ return strcmp( A, B ) < 0;
+ case SLMO_LESSEQUAL:
+ return strcmp( A, B ) <= 0;
+ case SLMO_EQUAL:
+ return strcmp( A, B ) == 0;
+ case SLMO_GREATER:
+ return strcmp( A, B ) > 0;
+ case SLMO_NOTEQUAL:
+ return strcmp( A, B ) != 0;
+ case SLMO_GREATEREQUAL:
+ return strcmp( A, B ) >= 0;
+ default:
+ Con_DPrint( "_ServerList_CompareStr: Bad op!\n" );
+ return false;
+ }
+}
+
+static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info_t *info )
+{
+ if( !_ServerList_CompareInt( info->ping, mask->tests[SLIF_PING], mask->info.ping ) )
+ return false;
+ if( !_ServerList_CompareInt( info->maxplayers, mask->tests[SLIF_MAXPLAYERS], mask->info.maxplayers ) )
+ return false;
+ if( !_ServerList_CompareInt( info->numplayers, mask->tests[SLIF_NUMPLAYERS], mask->info.numplayers ) )
+ return false;
+ if( !_ServerList_CompareInt( info->protocol, mask->tests[SLIF_PROTOCOL], mask->info.protocol ))
+ return false;
+ if( *mask->info.cname
+ && !_ServerList_CompareStr( info->cname, mask->tests[SLIF_CNAME], mask->info.cname ) )
+ return false;
+ if( *mask->info.game
+ && !_ServerList_CompareStr( info->game, mask->tests[SLIF_GAME], mask->info.game ) )
+ return false;
+ if( *mask->info.mod
+ && !_ServerList_CompareStr( info->mod, mask->tests[SLIF_MOD], mask->info.mod ) )
+ return false;
+ if( *mask->info.map
+ && !_ServerList_CompareStr( info->map, mask->tests[SLIF_MAP], mask->info.map ) )
+ return false;
+ if( *mask->info.name
+ && !_ServerList_CompareStr( info->name, mask->tests[SLIF_NAME], mask->info.name ) )
+ return false;
+ return true;
+}
+
+static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
+{
+ int start, end, mid;
+
+ if( serverlist_viewcount == SERVERLIST_VIEWLISTSIZE )
+ return;
+
+ // now check whether it passes through the masks
+ for( start = 0 ; serverlist_andmasks[start].active && start < SERVERLIST_ANDMASKCOUNT ; start++ )
+ if( !_ServerList_Entry_Mask( &serverlist_andmasks[start], &entry->info ) )
+ return;
+
+ for( start = 0 ; serverlist_ormasks[start].active && start < SERVERLIST_ORMASKCOUNT ; start++ )
+ if( _ServerList_Entry_Mask( &serverlist_ormasks[start], &entry->info ) )
+ break;
+ if( start == SERVERLIST_ORMASKCOUNT || (start > 0 && !serverlist_ormasks[start].active) )
+ return;
+
+ if( !serverlist_viewcount ) {
+ _ServerList_ViewList_Helper_InsertBefore( 0, entry );
+ return;
+ }
+ // ok, insert it, we just need to find out where exactly:
+
+ // two special cases
+ // check whether to insert it as new first item
+ if( _ServerList_Entry_Compare( entry, serverlist_viewlist[0] ) ) {
+ _ServerList_ViewList_Helper_InsertBefore( 0, entry );
+ return;
+ } // check whether to insert it as new last item
+ else if( !_ServerList_Entry_Compare( entry, serverlist_viewlist[serverlist_viewcount - 1] ) ) {
+ _ServerList_ViewList_Helper_InsertBefore( serverlist_viewcount, entry );
+ return;
+ }
+ start = 0;
+ end = serverlist_viewcount - 1;
+ while( end > start + 1 )
+ {
+ mid = (start + end) / 2;
+ // test the item that lies in the middle between start and end
+ if( _ServerList_Entry_Compare( entry, serverlist_viewlist[mid] ) )
+ // the item has to be in the upper half
+ end = mid;
+ else
+ // the item has to be in the lower half
+ start = mid;
+ }
+ _ServerList_ViewList_Helper_InsertBefore( start + 1, entry );
+}
+
+static void ServerList_ViewList_Remove( serverlist_entry_t *entry )
+{
+ int i;
+ for( i = 0; i < serverlist_viewcount; i++ )
+ {
+ if (serverlist_viewlist[i] == entry)
+ {
+ _ServerList_ViewList_Helper_Remove(i);
+ break;
+ }
+ }
+}
+
+void ServerList_RebuildViewList(void)
+{
+ int i;
+
+ serverlist_viewcount = 0;
+ for( i = 0 ; i < serverlist_cachecount ; i++ )
+ if( serverlist_cache[i].finished )
+ ServerList_ViewList_Insert( &serverlist_cache[i] );
+}
+
+void ServerList_ResetMasks(void)
+{
+ memset( &serverlist_andmasks, 0, sizeof( serverlist_andmasks ) );
+ memset( &serverlist_ormasks, 0, sizeof( serverlist_ormasks ) );
+}
+
+#if 0
+static void _ServerList_Test(void)
+{
+ int i;
+ for( i = 0 ; i < 1024 ; i++ ) {
+ memset( &serverlist_cache[serverlist_cachecount], 0, sizeof( serverlist_t ) );
+ serverlist_cache[serverlist_cachecount].info.ping = rand() % 450 + 250;
+ dpsnprintf( serverlist_cache[serverlist_cachecount].info.name, 128, "Black's ServerList Test %i", i );
+ serverlist_cache[serverlist_cachecount].finished = true;
+ sprintf( serverlist_cache[serverlist_cachecount].line1, "%i %s", serverlist_cache[serverlist_cachecount].info.ping, serverlist_cache[serverlist_cachecount].info.name );
+ ServerList_ViewList_Insert( &serverlist_cache[serverlist_cachecount] );
+ serverlist_cachecount++;
+ }
+}
+#endif
+
+void ServerList_QueryList(void)
+{
+ masterquerytime = realtime;
+ masterquerycount = 0;
+ masterreplycount = 0;
+ serverquerycount = 0;
+ serverreplycount = 0;
+ serverlist_cachecount = 0;
+ serverlist_viewcount = 0;
+ serverlist_consoleoutput = false;
+ NetConn_QueryMasters();
+
+ //_ServerList_Test();
+}
+
+// rest
+
int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
{
int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
int i;
- if (cl_fakepacketloss_receive.integer)
- for (i = 0;i < cl_numsockets;i++)
- if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_receive.integer)
- return 0;
- if (sv_fakepacketloss_receive.integer)
+ if (length == 0)
+ return 0;
+ if (cl_netpacketloss.integer)
for (i = 0;i < cl_numsockets;i++)
- if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_receive.integer)
+ if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
return 0;
- if (developer_networking.integer && length != 0)
+ if (developer_networking.integer)
{
char addressstring[128], addressstring2[128];
LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
{
int ret;
int i;
- if (cl_fakepacketloss_send.integer)
+ if (cl_netpacketloss.integer)
for (i = 0;i < cl_numsockets;i++)
- if (cl_sockets[i] == mysocket && (rand() % 100) < cl_fakepacketloss_send.integer)
- return length;
- if (sv_fakepacketloss_send.integer)
- for (i = 0;i < cl_numsockets;i++)
- if (sv_sockets[i] == mysocket && (rand() % 100) < sv_fakepacketloss_send.integer)
+ if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
return length;
ret = LHNET_Write(mysocket, data, length, peeraddress);
if (developer_networking.integer)
conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
conn->mysocket = mysocket;
conn->peeraddress = *peeraddress;
- // updated by receiving "rate" command from client
- conn->rate = NET_MINRATE;
- // no limits for local player
- if (LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP)
- conn->rate = 1000000000;
conn->canSend = true;
- conn->connecttime = realtime;
conn->lastMessageTime = realtime;
// LordHavoc: (inspired by ProQuake) use a short connect timeout to
// reduce effectiveness of connection request floods
void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
{
+ cls.connect_trying = false;
+ M_Update_Return_Reason("");
+ // the connection request succeeded, stop current connection and set up a new connection
+ CL_Disconnect();
cls.netcon = NetConn_Open(mysocket, peeraddress);
Con_Printf("Connection accepted to %s\n", cls.netcon->address);
key_dest = key_game;
m_state = m_none;
- cls.connect_trying = false;
cls.demonum = -1; // not in the demo loop now
cls.state = ca_connected;
cls.signon = 0; // need all the signon messages before playing
- CL_ClearState();
- Host_Reconnect_f();
}
int NetConn_IsLocalGame(void)
return false;
}
-static struct
-{
- double senttime;
- lhnetaddress_t peeraddress;
-}
-pingcache[HOSTCACHESIZE];
-
int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
{
int ret, c, control;
{
LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
Con_Printf("\"%s\" received, sending connect request back to %s\n", string, addressstring2);
+ M_Update_Return_Reason("Got challenge response");
NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\challenge\\%s", string + 10), peeraddress);
return true;
}
if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying)
{
+ M_Update_Return_Reason("Accepted");
NetConn_ConnectionEstablished(mysocket, peeraddress);
return true;
}
if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying)
{
+ char rejectreason[32];
cls.connect_trying = false;
string += 7;
- length = max(length - 7, (int)sizeof(m_return_reason) - 1);
- memcpy(m_return_reason, string, length);
- m_return_reason[length] = 0;
- Con_Printf("%s\n", m_return_reason);
+ length = max(length - 7, (int)sizeof(rejectreason) - 1);
+ memcpy(rejectreason, string, length);
+ rejectreason[length] = 0;
+ M_Update_Return_Reason(rejectreason);
return true;
}
if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
{
- int i, j, c, n, users, maxusers;
- char game[32], mod[32], map[32], name[128];
+ serverlist_info_t *info;
+ int i, n;
double pingtime;
- hostcache_t temp;
+
string += 13;
- // hostcache only uses text addresses
+ // serverlist only uses text addresses
LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
- if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(game, s, sizeof (game));else game[0] = 0;
- if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(mod , s, sizeof (mod ));else mod[0] = 0;
- if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(map , s, sizeof (map ));else map[0] = 0;
- if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(name, s, sizeof (name));else name[0] = 0;
- if ((s = SearchInfostring(string, "protocol" )) != NULL) c = atoi(s);else c = -1;
- if ((s = SearchInfostring(string, "clients" )) != NULL) users = atoi(s);else users = 0;
- if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) maxusers = atoi(s);else maxusers = 0;
// search the cache for this server and update it
- for (n = 0; n < hostCacheCount; n++)
- {
- if (!strcmp(cname, hostcache[n].cname))
- {
- if (hostcache[n].ping == 100000)
- serverreplycount++;
- pingtime = (int)((realtime - hostcache[n].querytime) * 1000.0);
- pingtime = bound(0, pingtime, 9999);
- // update the ping
- hostcache[n].ping = pingtime;
- // build description strings for the things users care about
- snprintf(hostcache[n].line1, sizeof(hostcache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, c != NET_PROTOCOL_VERSION ? '*' : ' ', users, maxusers, name);
- snprintf(hostcache[n].line2, sizeof(hostcache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", cname, game, mod, map);
- // if ping is especially high, display it as such
- if (pingtime >= 300)
- {
- // orange numbers (lower block)
- for (i = 0;i < 5;i++)
- if (hostcache[n].line1[i] != ' ')
- hostcache[n].line1[i] += 128;
- }
- else if (pingtime >= 200)
- {
- // yellow numbers (in upper block)
- for (i = 0;i < 5;i++)
- if (hostcache[n].line1[i] != ' ')
- hostcache[n].line1[i] -= 30;
- }
- // if not in the slist menu we should print the server to console
- if (m_state != m_slist)
- Con_Printf("%s\n%s\n", hostcache[n].line1, hostcache[n].line2);
- // and finally, re-sort the list
- for (i = 0;i < hostCacheCount;i++)
- {
- for (j = i + 1;j < hostCacheCount;j++)
- {
- //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
- if (hostcache[i].ping > hostcache[j].ping)
- {
- memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
- memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
- memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
- }
- }
- }
+ for( n = 0; n < serverlist_cachecount; n++ )
+ if( !strcmp( cname, serverlist_cache[n].info.cname ) )
break;
- }
+ if( n == serverlist_cachecount )
+ return true;
+
+ info = &serverlist_cache[n].info;
+ if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(info->game, s, sizeof (info->game));else info->game[0] = 0;
+ if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0] = 0;
+ if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(info->map , s, sizeof (info->map ));else info->map[0] = 0;
+ if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(info->name, s, sizeof (info->name));else info->name[0] = 0;
+ if ((s = SearchInfostring(string, "protocol" )) != NULL) info->protocol = atoi(s);else info->protocol = -1;
+ if ((s = SearchInfostring(string, "clients" )) != NULL) info->numplayers = atoi(s);else info->numplayers = 0;
+ if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);else info->maxplayers = 0;
+
+ if (info->ping == 100000)
+ serverreplycount++;
+
+ pingtime = (int)((realtime - serverlist_cache[n].querytime) * 1000.0);
+ pingtime = bound(0, pingtime, 9999);
+ // update the ping
+ info->ping = pingtime;
+
+ // legacy/old stuff move it to the menu ASAP
+
+ // build description strings for the things users care about
+ dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
+ dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", info->cname, info->game, info->mod, info->map);
+ // if ping is especially high, display it as such
+ if (pingtime >= 300)
+ {
+ // orange numbers (lower block)
+ for (i = 0;i < 5;i++)
+ if (serverlist_cache[n].line1[i] != ' ')
+ serverlist_cache[n].line1[i] += 128;
+ }
+ else if (pingtime >= 200)
+ {
+ // yellow numbers (in upper block)
+ for (i = 0;i < 5;i++)
+ if (serverlist_cache[n].line1[i] != ' ')
+ serverlist_cache[n].line1[i] -= 30;
}
+ // and finally, update the view set
+ if( serverlist_cache[n].finished )
+ ServerList_ViewList_Remove( &serverlist_cache[n] );
+ // else if not in the slist menu we should print the server to console (if wanted)
+ else if( serverlist_consoleoutput )
+ Con_Printf("%s\n%s\n", serverlist_cache[n].line1, serverlist_cache[n].line2);
+ ServerList_ViewList_Insert( &serverlist_cache[n] );
+ serverlist_cache[n].finished = true;
+
return true;
}
- if (!strncmp(string, "getserversResponse\\", 19) && hostCacheCount < HOSTCACHESIZE)
+ if (!strncmp(string, "getserversResponse\\", 19) && serverlist_cachecount < SERVERLIST_TOTALSIZE)
{
- int i, n, j;
- hostcache_t temp;
// Extract the IP addresses
data += 18;
length -= 18;
masterreplycount++;
- if (m_state != m_slist)
+ if (serverlist_consoleoutput)
Con_Print("received server list...\n");
while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
{
- snprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
+ int n;
+
+ dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
if (developer.integer)
Con_Printf("Requesting info from server %s\n", ipstring);
- LHNETADDRESS_FromString(&svaddress, ipstring, 0);
- NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
-
-
- // add to slist (hostCache)
- // search the cache for this server
- for (n = 0; n < hostCacheCount; n++)
- if (!strcmp(ipstring, hostcache[n].cname))
+ // ignore the rest of the message if the serverlist is full
+ if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+ break;
+ // also ignore it if we have already queried it (other master server response)
+ for( n = 0 ; n < serverlist_cachecount ; n++ )
+ if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
break;
- // add it or update it
- if (n == hostCacheCount)
+ if( n >= serverlist_cachecount )
{
- // if cache is full replace highest ping server (the list is
- // kept sorted so this is always the last, and if this server
- // is good it will be sorted into an early part of the list)
- if (hostCacheCount >= HOSTCACHESIZE)
- n = hostCacheCount - 1;
- else
- {
- serverquerycount++;
- hostCacheCount++;
- }
- }
- memset(&hostcache[n], 0, sizeof(hostcache[n]));
- // store the data the engine cares about (address and ping)
- strlcpy (hostcache[n].cname, ipstring, sizeof (hostcache[n].cname));
- hostcache[n].ping = 100000;
- hostcache[n].querytime = realtime;
- // build description strings for the things users care about
- strlcpy (hostcache[n].line1, "?", sizeof (hostcache[n].line1));
- strlcpy (hostcache[n].line2, ipstring, sizeof (hostcache[n].line2));
- // if not in the slist menu we should print the server to console
- if (m_state != m_slist)
- Con_Printf("querying %s\n", ipstring);
- // and finally, re-sort the list
- for (i = 0;i < hostCacheCount;i++)
- {
- for (j = i + 1;j < hostCacheCount;j++)
- {
- //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
- if (hostcache[i].ping > hostcache[j].ping)
- {
- memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
- memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
- memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
- }
- }
- }
+ serverquerycount++;
+ LHNETADDRESS_FromString(&svaddress, ipstring, 0);
+ NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
+
+ memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
+ // store the data the engine cares about (address and ping)
+ strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
+ serverlist_cache[serverlist_cachecount].info.ping = 100000;
+ serverlist_cache[serverlist_cachecount].querytime = realtime;
+ // if not in the slist menu we should print the server to console
+ if (serverlist_consoleoutput)
+ Con_Printf("querying %s\n", ipstring);
+
+ ++serverlist_cachecount;
+ }
// move on to next address in packet
data += 7;
length -= 4;
LHNETADDRESS_SetPort(&clientportaddress, port);
}
+ M_Update_Return_Reason("Accepted");
NetConn_ConnectionEstablished(mysocket, &clientportaddress);
}
break;
case CCREP_REJECT:
if (developer.integer)
Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
- Con_Printf("%s\n", data);
- strlcpy (m_return_reason, data, sizeof (m_return_reason));
+ cls.connect_trying = false;
+ M_Update_Return_Reason(data);
break;
#if 0
case CCREP_SERVER_INFO:
// LordHavoc: because the UDP driver reports 0.0.0.0:26000 as the address
// string we just ignore it and keep the real address
MSG_ReadString();
- // hostcache only uses text addresses
+ // serverlist only uses text addresses
cname = UDP_AddrToString(readaddr);
// search the cache for this server
for (n = 0; n < hostCacheCount; n++)
- if (!strcmp(cname, hostcache[n].cname))
+ if (!strcmp(cname, serverlist[n].cname))
break;
// add it
- if (n == hostCacheCount && hostCacheCount < HOSTCACHESIZE)
+ if (n == hostCacheCount && hostCacheCount < SERVERLISTSIZE)
{
hostCacheCount++;
- memset(&hostcache[n], 0, sizeof(hostcache[n]));
- strlcpy (hostcache[n].name, MSG_ReadString(), sizeof (hostcache[n].name));
- strlcpy (hostcache[n].map, MSG_ReadString(), sizeof (hostcache[n].map));
- hostcache[n].users = MSG_ReadByte();
- hostcache[n].maxusers = MSG_ReadByte();
+ memset(&serverlist[n], 0, sizeof(serverlist[n]));
+ strlcpy (serverlist[n].name, MSG_ReadString(), sizeof (serverlist[n].name));
+ strlcpy (serverlist[n].map, MSG_ReadString(), sizeof (serverlist[n].map));
+ serverlist[n].users = MSG_ReadByte();
+ serverlist[n].maxusers = MSG_ReadByte();
c = MSG_ReadByte();
if (c != NET_PROTOCOL_VERSION)
{
- strlcpy (hostcache[n].cname, hostcache[n].name, sizeof (hostcache[n].cname));
- strcpy(hostcache[n].name, "*");
- strlcat (hostcache[n].name, hostcache[n].cname, sizeof(hostcache[n].name));
+ strlcpy (serverlist[n].cname, serverlist[n].name, sizeof (serverlist[n].cname));
+ strcpy(serverlist[n].name, "*");
+ strlcat (serverlist[n].name, serverlist[n].cname, sizeof(serverlist[n].name));
}
- strlcpy (hostcache[n].cname, cname, sizeof (hostcache[n].cname));
+ strlcpy (serverlist[n].cname, cname, sizeof (serverlist[n].cname));
}
}
break;
if (cls.connect_remainingtries == 0)
{
cls.connect_trying = false;
- if (m_state == m_slist)
- strcpy (m_return_reason, "Connect: Failed");
- else
- Con_Print("Connect failed\n");
+ M_Update_Return_Reason("Connect: Failed");
return;
}
- if (cls.connect_nextsendtime)
- {
- if (m_state == m_slist)
- strcpy (m_return_reason, "Connect: Still trying");
- else
- Con_Print("Still trying...\n");
- }
- else
- {
- if (m_state == m_slist)
- strcpy (m_return_reason, "Connect: Trying");
- else
- Con_Print("Trying...\n");
- }
cls.connect_nextsendtime = realtime + 1;
cls.connect_remainingtries--;
// try challenge first (newer server)
buffer[i] = 0;
}
-extern void SV_ConnectClient(int clientnum, netconn_t *netconnection);
int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
{
int i, n, ret, clientnum, responselength, best;
if (clientnum < svs.maxclients)
{
// duplicate connection request
- if (realtime - client->netconnection->connecttime < 2.0)
+ if (realtime - client->connecttime < 2.0)
{
// client is still trying to connect,
// so we send a duplicate reply
if (clientnum < svs.maxclients)
{
// prepare the client struct
- if ((client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool)))
+ if ((conn = NetConn_Open(mysocket, peeraddress)))
{
- if ((conn = NetConn_Open(mysocket, peeraddress)))
- {
- // allocated connection
- LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
- if (developer.integer)
- Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
- NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
- // now set up the client
- SV_ConnectClient(clientnum, conn);
- NetConn_Heartbeat(1);
- }
- else
- EntityFrame4_FreeDatabase(client->entitydatabase4);
+ // allocated connection
+ LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
+ if (developer.integer)
+ Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
+ NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
+ // now set up the client
+ SV_ConnectClient(clientnum, conn);
+ NetConn_Heartbeat(1);
}
}
else
for (i = 0, n = 0;i < svs.maxclients;i++)
if (svs.clients[i].active)
n++;
- responselength = snprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
+ responselength = dpsnprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
"\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
"\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d%s%s",
gamename, com_modname, svs.maxclients, n,
sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "");
// does it fit in the buffer?
- if (responselength < (int)sizeof(response))
+ if (responselength >= 0)
{
if (developer.integer)
Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
if (clientnum < svs.maxclients)
{
// duplicate connection request
- if (realtime - client->netconnection->connecttime < 2.0)
+ if (realtime - client->connecttime < 2.0)
{
// client is still trying to connect,
// so we send a duplicate reply
MSG_WriteString(&net_message, client->name);
MSG_WriteLong(&net_message, client->colors);
MSG_WriteLong(&net_message, (int)client->edict->v->frags);
- MSG_WriteLong(&net_message, (int)(realtime - client->netconnection->connecttime));
- MSG_WriteString(&net_message, client->netconnection->address);
+ MSG_WriteLong(&net_message, (int)(realtime - client->connecttime));
+ MSG_WriteString(&net_message, client->netconnection ? client->netconnection->address : "botclient");
*((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
SZ_Clear(&net_message);
{
if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
{
- sv_player = host_client->edict;
if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
SV_ReadClientMessage();
return ret;
for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
{
// never timeout loopback connections
- if (host_client->netconnection && realtime > host_client->netconnection->timeout && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(host_client->netconnection->mysocket)) != LHNETADDRESSTYPE_LOOP)
+ if (host_client->netconnection && realtime > host_client->netconnection->timeout && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
{
Con_Printf("Client \"%s\" connection timed out\n", host_client->name);
- sv_player = host_client->edict;
SV_DropClient(false);
}
}
int i;
int masternum;
lhnetaddress_t masteraddress;
+ lhnetaddress_t broadcastaddress;
char request[256];
- if (hostCacheCount >= HOSTCACHESIZE)
+ if (serverlist_cachecount >= SERVERLIST_TOTALSIZE)
return;
+ // 26000 is the default quake server port, servers on other ports will not
+ // be found
+ // note this is IPv4-only, I doubt there are IPv6-only LANs out there
+ LHNETADDRESS_FromString(&broadcastaddress, "255.255.255.255", 26000);
+
for (i = 0;i < cl_numsockets;i++)
{
if (cl_sockets[i])
{
-#if 0
- // search LAN
-#if 1
- UDP_Broadcast(UDP_controlSock, "\377\377\377\377getinfo", 11);
-#else
+ // search LAN for Quake servers
SZ_Clear(&net_message);
// save space for the header, filled in later
MSG_WriteLong(&net_message, 0);
MSG_WriteString(&net_message, "QUAKE");
MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
*((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
- UDP_Broadcast(UDP_controlSock, net_message.data, net_message.cursize);
+ NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
SZ_Clear(&net_message);
-#endif
-#endif
- // build the getservers
- snprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
+ // search LAN for DarkPlaces servers
+ NetConn_WriteString(cl_sockets[i], "\377\377\377\377getinfo", &broadcastaddress);
+
+ // build the getservers message to send to the master servers
+ dpsnprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
// search internet
for (masternum = 0;sv_masters[masternum].name;masternum++)
if (!masterquerycount)
{
Con_Print("Unable to query master servers, no suitable network sockets active.\n");
- strcpy(m_return_reason, "No network");
+ M_Update_Return_Reason("No network");
}
}
void Net_Slist_f(void)
{
- masterquerytime = realtime;
- masterquerycount = 0;
- masterreplycount = 0;
- serverquerycount = 0;
- serverreplycount = 0;
- hostCacheCount = 0;
- memset(&pingcache, 0, sizeof(pingcache));
- if (m_state != m_slist)
+ ServerList_ResetMasks();
+ serverlist_sortbyfield = SLIF_PING;
+ serverlist_sortdescending = false;
+ if (m_state != m_slist) {
Con_Print("Sending requests to master servers\n");
- NetConn_QueryMasters();
- if (m_state != m_slist)
+ ServerList_QueryList();
+ serverlist_consoleoutput = true;
Con_Print("Listening for replies...\n");
+ } else
+ ServerList_QueryList();
}
void NetConn_Init(void)
{
int i;
lhnetaddress_t tempaddress;
- netconn_mempool = Mem_AllocPool("Networking");
+ netconn_mempool = Mem_AllocPool("network connections", 0, NULL);
Cmd_AddCommand("net_stats", Net_Stats_f);
Cmd_AddCommand("net_slist", Net_Slist_f);
Cmd_AddCommand("heartbeat", Net_Heartbeat_f);
Cvar_RegisterVariable(&net_messagetimeout);
Cvar_RegisterVariable(&net_messagerejointimeout);
Cvar_RegisterVariable(&net_connecttimeout);
- Cvar_RegisterVariable(&cl_fakelocalping_min);
- Cvar_RegisterVariable(&cl_fakelocalping_max);
- Cvar_RegisterVariable(&cl_fakepacketloss_receive);
- Cvar_RegisterVariable(&cl_fakepacketloss_send);
- Cvar_RegisterVariable(&sv_fakepacketloss_receive);
- Cvar_RegisterVariable(&sv_fakepacketloss_send);
+ Cvar_RegisterVariable(&cl_netlocalping);
+ Cvar_RegisterVariable(&cl_netpacketloss);
Cvar_RegisterVariable(&hostname);
Cvar_RegisterVariable(&developer_networking);
Cvar_RegisterVariable(&cl_netport);
Cvar_RegisterVariable(&sv_heartbeatperiod);
for (i = 0;sv_masters[i].name;i++)
Cvar_RegisterVariable(&sv_masters[i]);
+// COMMANDLINEOPTION: Server: -ip <ipaddress> sets the ip address of this machine for purposes of networking (default 0.0.0.0 also known as INADDR_ANY), use only if you have multiple network adapters and need to choose one specifically.
if ((i = COM_CheckParm("-ip")) && i + 1 < com_argc)
{
if (LHNETADDRESS_FromString(&tempaddress, com_argv[i + 1], 0) == 1)
else
Con_Printf("-ip option used, but unable to parse the address \"%s\"\n", com_argv[i + 1]);
}
+// COMMANDLINEOPTION: Server: -port <portnumber> sets the port to use for a server (default 26000, the same port as QUAKE itself), useful if you host multiple servers on your machine
if (((i = COM_CheckParm("-port")) || (i = COM_CheckParm("-ipport")) || (i = COM_CheckParm("-udpport"))) && i + 1 < com_argc)
{
i = atoi(com_argv[i + 1]);
}
cl_numsockets = 0;
sv_numsockets = 0;
- memset(&pingcache, 0, sizeof(pingcache));
- SZ_Alloc(&net_message, NET_MAXMESSAGE, "net_message");
+ net_message.data = net_message_buf;
+ net_message.maxsize = sizeof(net_message_buf);
+ net_message.cursize = 0;
LHNET_Init();
}