]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
changed most Sys_Error calls to Con_Printf with appropriate error returns
[xonotic/darkplaces.git] / netconn.c
index 8a46404274cec9da057a9479404faadf36657e8d..870f694f2083d27055aa82bf8037f5994a132926 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -206,25 +206,34 @@ static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
 
 static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
 {
+       int i;
+       char bufferA[ 256 ], bufferB[ 256 ]; // should be more than enough
+       for (i = 0;i < sizeof(bufferA)-1 && A[i];i++)
+               bufferA[i] = (A[i] >= 'A' && A[i] <= 'Z') ? (A[i] + 'a' - 'A') : A[i];
+       bufferA[i] = 0;
+       for (i = 0;i < sizeof(bufferB)-1 && B[i];i++)
+               bufferB[i] = (B[i] >= 'A' && B[i] <= 'Z') ? (B[i] + 'a' - 'A') : B[i];
+       bufferB[i] = 0;
+
        // 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
+                       return *bufferB && !!strstr( bufferA, bufferB ); // we want a real bool
                case SLMO_NOTCONTAIN:
-                       return !*B || !strstr( A, B );
+                       return !*bufferB || !strstr( bufferA, bufferB );
                case SLMO_LESS:
-                       return strcmp( A, B ) < 0;
+                       return strcmp( bufferA, bufferB ) < 0;
                case SLMO_LESSEQUAL:
-                       return strcmp( A, B ) <= 0;
+                       return strcmp( bufferA, bufferB ) <= 0;
                case SLMO_EQUAL:
-                       return strcmp( A, B ) == 0;
+                       return strcmp( bufferA, bufferB ) == 0;
                case SLMO_GREATER:
-                       return strcmp( A, B ) > 0;
+                       return strcmp( bufferA, bufferB ) > 0;
                case SLMO_NOTEQUAL:
-                       return strcmp( A, B ) != 0;
+                       return strcmp( bufferA, bufferB ) != 0;
                case SLMO_GREATEREQUAL:
-                       return strcmp( A, B ) >= 0;
+                       return strcmp( bufferA, bufferB ) >= 0;
                default:
                        Con_DPrint( "_ServerList_CompareStr: Bad op!\n" );
                        return false;
@@ -431,13 +440,22 @@ int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
 
 //#ifdef DEBUG
        if (data->cursize == 0)
-               Sys_Error("Datagram_SendMessage: zero length message\n");
+       {
+               Con_Printf ("Datagram_SendMessage: zero length message\n");
+               return -1;
+       }
 
        if (data->cursize > (int)sizeof(conn->sendMessage))
-               Sys_Error("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
+       {
+               Con_Printf ("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
+               return -1;
+       }
 
        if (conn->canSend == false)
-               Sys_Error("SendMessage: called with canSend == false\n");
+       {
+               Con_Printf ("SendMessage: called with canSend == false\n");
+               return -1;
+       }
 //#endif
 
        memcpy(conn->sendMessage, data->data, data->cursize);
@@ -562,10 +580,16 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
 
 //#ifdef DEBUG
        if (data->cursize == 0)
-               Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
+       {
+               Con_Printf ("Datagram_SendUnreliableMessage: zero length message\n");
+               return -1;
+       }
 
        if (packetLen > (int)sizeof(sendbuffer))
-               Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
+       {
+               Con_Printf ("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
+               return -1;
+       }
 //#endif
 
        header = (void *)sendbuffer;
@@ -1224,7 +1248,7 @@ void NetConn_QueryQueueFrame(void)
        for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ )
        {
                serverlist_entry_t *entry = &serverlist_cache[ index ];
-               if( entry->query != SQS_QUERYING ) 
+               if( entry->query != SQS_QUERYING )
                {
                        continue;
                }
@@ -1235,7 +1259,7 @@ void NetConn_QueryQueueFrame(void)
                        continue;
                }
 
-               if( entry->querycounter != (unsigned) net_slist_maxtries.integer ) 
+               if( entry->querycounter != (unsigned) net_slist_maxtries.integer )
                {
                        lhnetaddress_t address;
                        int socket;
@@ -1253,8 +1277,8 @@ void NetConn_QueryQueueFrame(void)
                                Con_Printf("querying %25s (%i. try)\n", entry->info.cname, entry->querycounter);
 
                        queries++;
-               } 
-               else 
+               }
+               else
                {
                        entry->query = SQS_TIMEDOUT;
                }
@@ -1332,10 +1356,83 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
        buffer[i] = 0;
 }
 
+static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus)
+{
+       unsigned int nb_clients = 0, i;
+       int length;
+
+       // How many clients are there?
+       for (i = 0;i < (unsigned int)svs.maxclients;i++)
+               if (svs.clients[i].active)
+                       nb_clients++;
+
+       // TODO: we should add more information for the full status string
+       length = dpsnprintf(out_msg, out_size,
+                                               "\377\377\377\377%s\x0A"
+                                               "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
+                                               "\\clients\\%d\\mapname\\%s\\hostname\\%s""\\protocol\\%d"
+                                               "%s%s"
+                                               "%s",
+                                               fullstatus ? "statusResponse" : "infoResponse",
+                                               gamename, com_modname, svs.maxclients,
+                                               nb_clients, sv.name, hostname.string, NET_PROTOCOL_VERSION,
+                                               challenge ? "\\challenge\\" : "", challenge ? challenge : "",
+                                               fullstatus ? "\n" : "");
+
+       // Make sure it fits in the buffer
+       if (length < 0)
+               return false;
+
+       if (fullstatus)
+       {
+               char *ptr;
+               int left;
+
+               ptr = out_msg + length;
+               left = out_size - length;
+
+               for (i = 0;i < (unsigned int)svs.maxclients;i++)
+               {
+                       client_t *cl = &svs.clients[i];
+                       if (cl->active)
+                       {
+                               int nameind, cleanind;
+                               char curchar;
+                               char cleanname [sizeof(cl->name)];
+
+                               // Remove all characters '"' and '\' in the player name
+                               nameind = 0;
+                               cleanind = 0;
+                               do
+                               {
+                                       curchar = cl->name[nameind++];
+                                       if (curchar != '"' && curchar != '\\')
+                                       {
+                                               cleanname[cleanind++] = curchar;
+                                               if (cleanind == sizeof(cleanname) - 1)
+                                                       break;
+                                       }
+                               } while (curchar != '\0');
+
+                               length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
+                                                                       cl->frags,
+                                                                       (int)(cl->ping * 1000.0f),
+                                                                       cleanname);
+                               if(length < 0)
+                                       return false;
+                               left -= length;
+                               ptr += length;
+                       }
+               }
+       }
+
+       return true;
+}
+
 extern void SV_SendServerinfo (client_t *client);
 int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
 {
-       int i, n, ret, clientnum, responselength, best;
+       int i, ret, clientnum, best;
        double besttime;
        client_t *client;
        netconn_t *conn;
@@ -1466,19 +1563,12 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        if (length >= 7 && !memcmp(string, "getinfo", 7))
                        {
                                const char *challenge = NULL;
+
                                // If there was a challenge in the getinfo message
                                if (length > 8 && string[7] == ' ')
                                        challenge = string + 8;
-                               for (i = 0, n = 0;i < svs.maxclients;i++)
-                                       if (svs.clients[i].active)
-                                               n++;
-                               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 >= 0)
+
+                               if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), false))
                                {
                                        if (developer.integer)
                                                Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
@@ -1486,6 +1576,22 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                }
                                return true;
                        }
+                       if (length >= 9 && !memcmp(string, "getstatus", 9))
+                       {
+                               const char *challenge = NULL;
+
+                               // If there was a challenge in the getinfo message
+                               if (length > 10 && string[9] == ' ')
+                                       challenge = string + 10;
+
+                               if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), true))
+                               {
+                                       if (developer.integer)
+                                               Con_Printf("Sending reply to client %s - %s\n", addressstring2, response);
+                                       NetConn_WriteString(mysocket, response, peeraddress);
+                               }
+                               return true;
+                       }
                        /*
                        if (!strncmp(string, "ping", 4))
                        {