]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
At O.Sezer's recommendation filematch.c has been changed to use
[xonotic/darkplaces.git] / netconn.c
index 3abc226d10886c1b2cc6279e98890ba94a6c93cb..369014891d05fd49fa3b6428373d0ca58973048d 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -75,6 +75,7 @@ static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4",
 static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"};
 static cvar_t net_slist_pause = {0, "net_slist_pause", "0", "when set to 1, the server list won't update until it is set back to 0"};
 static cvar_t net_slist_maxtries = {0, "net_slist_maxtries", "3", "how many times to ask the same server for information (more times gives better ping reports but takes longer)"};
+static cvar_t net_slist_favorites = {CVAR_SAVE, "net_slist_favorites", "", "contains a list of IP addresses and ports to always query explicitly"};
 
 static cvar_t gameversion = {0, "gameversion", "0", "version of game data (mod-specific), when client and server gameversion mismatch in the server browser the server is shown as incompatible"};
 static cvar_t rcon_restricted_password = {CVAR_PRIVATE, "rcon_restricted_password", "", "password to authenticate rcon commands in restricted mode"};
@@ -131,7 +132,7 @@ serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
 serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
 
 serverlist_infofield_t serverlist_sortbyfield;
-qboolean serverlist_sortdescending;
+int serverlist_sortflags;
 
 int serverlist_viewcount = 0;
 serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
@@ -171,6 +172,12 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
 {
        int result = 0; // > 0 if for numbers A > B and for text if A < B
 
+       if( serverlist_sortflags & SLSF_FAVORITESFIRST )
+       {
+               if(A->info.isfavorite != B->info.isfavorite)
+                       return A->info.isfavorite;
+       }
+
        switch( serverlist_sortbyfield ) {
                case SLIF_PING:
                        result = A->info.ping - B->info.ping;
@@ -211,18 +218,22 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
                case SLIF_QCSTATUS:
                        result = strcasecmp( B->info.qcstatus, A->info.qcstatus ); // not really THAT useful, though
                        break;
-               case SLIF_PLAYERS:
-                       result = strcasecmp( B->info.players, A->info.qcstatus ); // not really THAT useful, though
+               case SLIF_ISFAVORITE:
+                       result = !!B->info.isfavorite - !!A->info.isfavorite;
                        break;
                default:
                        Con_DPrint( "_ServerList_Entry_Compare: Bad serverlist_sortbyfield!\n" );
                        break;
        }
 
-       if( serverlist_sortdescending )
-               return result > 0;
        if (result != 0)
-               return result < 0;
+       {
+               if( serverlist_sortflags & SLSF_DESCENDING )
+                       return result > 0;
+               else
+                       return result < 0;
+       }
+
        // if the chosen sort key is identical, sort by index
        // (makes this a stable sort, so that later replies from servers won't
        //  shuffle the servers around when they have the same ping)
@@ -334,17 +345,32 @@ static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info
        if( *mask->info.players
                && !_ServerList_CompareStr( info->players, mask->tests[SLIF_PLAYERS], mask->info.players ) )
                return false;
+       if( !_ServerList_CompareInt( info->isfavorite, mask->tests[SLIF_ISFAVORITE], mask->info.isfavorite ))
+               return false;
        return true;
 }
 
 static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 {
        int start, end, mid;
+       const char *text;
 
        // reject incompatible servers
        if (entry->info.gameversion != gameversion.integer)
                return;
 
+       // refresh the "favorite" status
+       text = net_slist_favorites.string;
+       entry->info.isfavorite = false;
+       while(COM_ParseToken_Console(&text))
+       {
+               if(!strcmp(com_token, entry->info.cname))
+               {
+                       entry->info.isfavorite = true;
+                       break;
+               }
+       }
+
        // FIXME: change this to be more readable (...)
        // now check whether it passes through the masks
        for( start = 0 ; start < SERVERLIST_ANDMASKCOUNT && serverlist_andmasks[start].active; start++ )
@@ -1285,6 +1311,16 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address
                if (serverlist_consoleoutput)
                        Con_Printf("querying %s\n", addressstring);
                ++serverlist_cachecount;
+
+#if 0
+               // we should not NEED this part...
+               text = net_slist_favorites.string;
+               while(COM_ParseToken_Console(&text))
+               {
+                       if(!strcmp(com_token, addressstring))
+                               entry->isfavorite = 1;
+               }
+#endif
        }
        // if this is the first reply from this server, count it as having replied
        pingtime = (int)((realtime - entry->querytime) * 1000.0 + 0.5);
@@ -1326,7 +1362,7 @@ static void NetConn_ClientParsePacket_ServerList_UpdateCache(int n)
 }
 
 // returns true, if it's sensible to continue the processing
-static qboolean NetConn_ClientParsePacket_ServerList_PrepareQuery( int protocol, const char *ipstring ) {
+static qboolean NetConn_ClientParsePacket_ServerList_PrepareQuery( int protocol, const char *ipstring, qboolean isfavorite ) {
        int n;
        serverlist_entry_t *entry;
 
@@ -1349,6 +1385,8 @@ static qboolean NetConn_ClientParsePacket_ServerList_PrepareQuery( int protocol,
        entry->protocol =       protocol;
        //      store   the data        the engine cares about (address and     ping)
        strlcpy (entry->info.cname, ipstring, sizeof(entry->info.cname));
+
+       entry->info.isfavorite = isfavorite;
        
        // no, then reset the ping right away
        entry->info.ping = -1;
@@ -1530,7 +1568,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                if (serverlist_consoleoutput && developer_networking.integer)
                                        Con_Printf("Requesting info from DarkPlaces server %s\n", ipstring);
                                
-                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, ipstring ) ) {
+                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, ipstring, false ) ) {
                                        break;
                                }
 
@@ -1557,7 +1595,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                if (serverlist_consoleoutput && developer_networking.integer)
                                        Con_Printf("Requesting info from QuakeWorld server %s\n", ipstring);
                                
-                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, ipstring ) ) {
+                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, ipstring, false ) ) {
                                        break;
                                }
 
@@ -1623,7 +1661,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                return true;
 
                        info = &serverlist_cache[n].info;
-                       strlcpy(info->game, "QuakeWorld", sizeof(info->game));;
+                       strlcpy(info->game, "QuakeWorld", sizeof(info->game));
                        if ((s = SearchInfostring(string, "*gamedir"     )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0]  = 0;
                        if ((s = SearchInfostring(string, "map"          )) != 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;
@@ -1910,9 +1948,10 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
 // (div0) build the full response only if possible; better a getinfo response than no response at all if getstatus won't fit
 static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus)
 {
-       const char *qcstatus = NULL;
+       char qcstatus[256];
        unsigned int nb_clients = 0, nb_bots = 0, i;
        int length;
+       char teambuf[3];
 
        SV_VM_Begin();
 
@@ -1927,6 +1966,7 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                }
        }
 
+       *qcstatus = 0;
        if(prog->globaloffsets.worldstatus >= 0)
        {
                const char *str = PRVM_G_STRING(prog->globaloffsets.worldstatus);
@@ -1934,9 +1974,9 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                {
                        char *p;
                        const char *q;
-                       qcstatus = p = Mem_Alloc(tempmempool, strlen(str) + 1);
+                       p = qcstatus;
                        for(q = str; *q; ++q)
-                               if(*q != '\\')
+                               if(*q != '\\' && *q != '\n')
                                        *p++ = *q;
                        *p = 0;
                }
@@ -1953,13 +1993,10 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                                fullstatus ? "statusResponse" : "infoResponse",
                                                gamename, com_modname, gameversion.integer, svs.maxclients,
                                                nb_clients, nb_bots, sv.name, hostname.string, NET_PROTOCOL_VERSION,
-                                               qcstatus ? "\\qcstatus\\" : "", qcstatus ? qcstatus : "",
+                                               *qcstatus ? "\\qcstatus\\" : "", qcstatus,
                                                challenge ? "\\challenge\\" : "", challenge ? challenge : "",
                                                fullstatus ? "\n" : "");
 
-       if(qcstatus)
-               Mem_Free((char *)qcstatus);
-
        // Make sure it fits in the buffer
        if (length < 0)
                goto bad;
@@ -1997,6 +2034,7 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                                        break;
                                        }
                                } while (curchar != '\0');
+                               cleanname[cleanind] = 0; // cleanind is always a valid index even at this point
 
                                pingvalue = (int)(cl->ping * 1000.0f);
                                if(cl->netconnection)
@@ -2004,6 +2042,7 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                else
                                        pingvalue = 0;
 
+                               *qcstatus = 0;
                                if(prog->fieldoffsets.clientstatus >= 0)
                                {
                                        const char *str = PRVM_E_STRING(PRVM_EDICT_NUM(i + 1), prog->fieldoffsets.clientstatus);
@@ -2011,26 +2050,44 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                        {
                                                char *p;
                                                const char *q;
-                                               qcstatus = p = Mem_Alloc(tempmempool, strlen(str) + 1);
-                                               for(q = str; *q; ++q)
-                                                       if(*q != '\\' && *q != ' ')
+                                               p = qcstatus;
+                                               for(q = str; *q && p != qcstatus + sizeof(qcstatus) - 1; ++q)
+                                                       if(*q != '\\' && *q != '"' && !ISWHITESPACE(*q))
                                                                *p++ = *q;
                                                *p = 0;
                                        }
                                }
 
-                               if(qcstatus)
+                               if ((gamemode == GAME_NEXUIZ) && (teamplay.integer > 0))
                                {
-                                       length = dpsnprintf(ptr, left, "%s %d \"%s\"\n",
+                                       if(cl->frags == -666) // spectator
+                                               strlcpy(teambuf, " 0", sizeof(teambuf));
+                                       else if(cl->colors == 0x44) // red team
+                                               strlcpy(teambuf, " 1", sizeof(teambuf));
+                                       else if(cl->colors == 0xDD) // blue team
+                                               strlcpy(teambuf, " 2", sizeof(teambuf));
+                                       else if(cl->colors == 0xCC) // yellow team
+                                               strlcpy(teambuf, " 3", sizeof(teambuf));
+                                       else if(cl->colors == 0x99) // pink team
+                                               strlcpy(teambuf, " 4", sizeof(teambuf));
+                                       else
+                                               strlcpy(teambuf, " 0", sizeof(teambuf));
+                               }
+                               else
+                                       *teambuf = 0;
+
+                               // note: team number is inserted according to SoF2 protocol
+                               if(*qcstatus)
+                                       length = dpsnprintf(ptr, left, "%s %d%s \"%s\"\n",
                                                                                qcstatus,
                                                                                pingvalue,
+                                                                               teambuf,
                                                                                cleanname);
-                                       Mem_Free((char *)qcstatus);
-                               }
                                else
-                                       length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
+                                       length = dpsnprintf(ptr, left, "%d %d%s \"%s\"\n",
                                                                                cl->frags,
                                                                                pingvalue,
+                                                                               teambuf,
                                                                                cleanname);
 
                                if(length < 0)
@@ -2129,7 +2186,7 @@ const char *RCon_Authenticate(const char *password, const char *s, const char *e
                return NULL;
 
        for(text = s; text != endpos; ++text)
-               if(*text > 0 && (*text < ' ' || *text == ';'))
+               if((signed char) *text > 0 && ((signed char) *text < (signed char) ' ' || *text == ';'))
                        return NULL; // block possible exploits against the parser/alias expansion
 
        while(s != endpos)
@@ -2357,13 +2414,13 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        char *s = string + 5;
                        char *endpos = string + length + 1; // one behind the NUL, so adding strlen+1 will eventually reach it
                        char password[64];
-                       for (i = 0;*s > ' ';s++)
+                       for (i = 0;!ISWHITESPACE(*s);s++)
                                if (i < (int)sizeof(password) - 1)
                                        password[i++] = *s;
-                       if(*s <= ' ' && s != endpos) // skip leading ugly space
+                       if(ISWHITESPACE(*s) && s != endpos) // skip leading ugly space
                                ++s;
                        password[i] = 0;
-                       if (password[0] > ' ')
+                       if (!ISWHITESPACE(password[0]))
                        {
                                const char *userlevel = RCon_Authenticate(password, s, endpos);
                                if(userlevel)
@@ -2379,8 +2436,9 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                                s_ptr += l + 1;
                                        }
                                        Con_Printf("\n");
-                                       rcon_redirect = true;
-                                       rcon_redirect_bufferpos = 0;
+
+                                       if (!host_client || !host_client->netconnection || LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
+                                               Con_Rcon_Redirect_Init(mysocket, peeraddress);
                                        while(s != endpos)
                                        {
                                                size_t l = strlen(s);
@@ -2393,27 +2451,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                                }
                                                s += l + 1;
                                        }
-                                       rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
-                                       rcon_redirect = false;
-                                       // print resulting text to client
-                                       // if client is playing, send a reliable reply instead of
-                                       // a command packet
-                                       if (host_client)
-                                       {
-                                               // if the netconnection is loop, then this is the
-                                               // local player on a listen mode server, and it would
-                                               // result in duplicate printing to the console
-                                               // (not that the local player should be using rcon
-                                               //  when they have the console)
-                                               if (host_client->netconnection && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
-                                                       SV_ClientPrintf("%s", rcon_redirect_buffer);
-                                       }
-                                       else
-                                       {
-                                               // qw print command
-                                               dpsnprintf(response, sizeof(response), "\377\377\377\377n%s", rcon_redirect_buffer);
-                                               NetConn_WriteString(mysocket, response, peeraddress);
-                                       }
+                                       Con_Rcon_Redirect_End();
                                }
                                else
                                {
@@ -2699,6 +2737,8 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
        int masternum;
        lhnetaddress_t masteraddress;
        lhnetaddress_t broadcastaddress;
+       lhnetaddress_t serveraddress;
+       const char *text;
        char request[256];
 
        if (serverlist_cachecount >= SERVERLIST_TOTALSIZE)
@@ -2715,19 +2755,24 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                {
                        if (cl_sockets[i])
                        {
-                               // search LAN for Quake servers
-                               SZ_Clear(&net_message);
-                               // save space for the header, filled in later
-                               MSG_WriteLong(&net_message, 0);
-                               MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
-                               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));
-                               NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
-                               SZ_Clear(&net_message);
+                               int af = LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i]));
+
+                               if(LHNETADDRESS_GetAddressType(&broadcastaddress) == af)
+                               {
+                                       // search LAN for Quake servers
+                                       SZ_Clear(&net_message);
+                                       // save space for the header, filled in later
+                                       MSG_WriteLong(&net_message, 0);
+                                       MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
+                                       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));
+                                       NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
+                                       SZ_Clear(&net_message);
 
-                               // search LAN for DarkPlaces servers
-                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377getstatus", &broadcastaddress);
+                                       // search LAN for DarkPlaces servers
+                                       NetConn_WriteString(cl_sockets[i], "\377\377\377\377getstatus", &broadcastaddress);
+                               }
 
                                // build the getservers message to send to the dpmaster master servers
                                dpsnprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
@@ -2735,12 +2780,22 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                                // search internet
                                for (masternum = 0;sv_masters[masternum].name;masternum++)
                                {
-                                       if (sv_masters[masternum].string && sv_masters[masternum].string[0] && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
+                                       if (sv_masters[masternum].string && sv_masters[masternum].string[0] && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
                                        {
                                                masterquerycount++;
                                                NetConn_WriteString(cl_sockets[i], request, &masteraddress);
                                        }
                                }
+
+                               // search favorite servers
+                               text = net_slist_favorites.string;
+                               while(COM_ParseToken_Console(&text))
+                               {
+                                       if(LHNETADDRESS_FromString(&serveraddress, com_token, 26000) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
+                                       {
+                                               NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, com_token, true );
+                                       }
+                               }
                        }
                }
        }
@@ -2752,12 +2807,17 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                {
                        if (cl_sockets[i])
                        {
-                               // search LAN for QuakeWorld servers
-                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &broadcastaddress);
+                               int af = LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i]));
 
-                               // build the getservers message to send to the qwmaster master servers
-                               // note this has no -1 prefix, and the trailing nul byte is sent
-                               dpsnprintf(request, sizeof(request), "c\n");
+                               if(LHNETADDRESS_GetAddressType(&broadcastaddress) == af)
+                               {
+                                       // search LAN for QuakeWorld servers
+                                       NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &broadcastaddress);
+
+                                       // build the getservers message to send to the qwmaster master servers
+                                       // note this has no -1 prefix, and the trailing nul byte is sent
+                                       dpsnprintf(request, sizeof(request), "c\n");
+                               }
 
                                // search internet
                                for (masternum = 0;sv_qwmasters[masternum].name;masternum++)
@@ -2774,6 +2834,20 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                                                NetConn_Write(cl_sockets[i], request, (int)strlen(request) + 1, &masteraddress);
                                        }
                                }
+
+                               // search favorite servers
+                               text = net_slist_favorites.string;
+                               while(COM_ParseToken_Console(&text))
+                               {
+                                       if(LHNETADDRESS_FromString(&serveraddress, com_token, 26000) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
+                                       {
+                                               // writing AND querying to catch replies for both
+                                               // protocols (in case DP has been queried above, this
+                                               // would only try the DP protocol otherwise)
+                                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &serveraddress);
+                                               NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, com_token, true );
+                                       }
+                               }
                        }
                }
        }
@@ -2862,7 +2936,7 @@ void Net_Slist_f(void)
 {
        ServerList_ResetMasks();
        serverlist_sortbyfield = SLIF_PING;
-       serverlist_sortdescending = false;
+       serverlist_sortflags = 0;
     if (m_state != m_slist) {
                Con_Print("Sending requests to master servers\n");
                ServerList_QueryList(true, true, false, true);
@@ -2875,7 +2949,7 @@ void Net_SlistQW_f(void)
 {
        ServerList_ResetMasks();
        serverlist_sortbyfield = SLIF_PING;
-       serverlist_sortdescending = false;
+       serverlist_sortflags = 0;
     if (m_state != m_slist) {
                Con_Print("Sending requests to master servers\n");
                ServerList_QueryList(true, false, true, true);
@@ -2901,6 +2975,7 @@ void NetConn_Init(void)
        Cvar_RegisterVariable(&net_slist_queriesperframe);
        Cvar_RegisterVariable(&net_slist_timeout);
        Cvar_RegisterVariable(&net_slist_maxtries);
+       Cvar_RegisterVariable(&net_slist_favorites);
        Cvar_RegisterVariable(&net_slist_pause);
        Cvar_RegisterVariable(&net_messagetimeout);
        Cvar_RegisterVariable(&net_connecttimeout);