]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
add a "freeslots" field to the host cache
[xonotic/darkplaces.git] / netconn.c
index 0860c7ceb7d503fa881e08f74883a396059ac999..8513eafe2c62fd9b4b884cc3ef4a3f1b0e0cd069 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -76,7 +76,8 @@ cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED", "server message to show in
 cvar_t developer_networking = {0, "developer_networking", "0", "prints all received and sent packets (recommended only for debugging)"};
 
 cvar_t cl_netlocalping = {0, "cl_netlocalping","0", "lags local loopback connection by this much ping time (useful to play more fairly on your own server with people with higher pings)"};
-static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0", "drops this percentage of packets (incoming and outgoing), useful for testing network protocol robustness (effects failing to start, sounds failing to play, etc)"};
+static cvar_t cl_netpacketloss_send = {0, "cl_netpacketloss_send","0", "drops this percentage of outgoing packets, useful for testing network protocol robustness (jerky movement, prediction errors, etc)"};
+static cvar_t cl_netpacketloss_receive = {0, "cl_netpacketloss_receive","0", "drops this percentage of incoming packets, useful for testing network protocol robustness (jerky movement, effects failing to start, sounds failing to play, etc)"};
 static cvar_t net_slist_queriespersecond = {0, "net_slist_queriespersecond", "20", "how many server information requests to send per second"};
 static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4", "maximum number of server information requests to send each rendered frame (guards against low framerates causing problems)"};
 static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"};
@@ -184,6 +185,15 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
                case SLIF_NUMPLAYERS:
                        result = A->info.numplayers - B->info.numplayers;
                        break;
+               case SLIF_NUMBOTS:
+                       result = A->info.numbots - B->info.numbots;
+                       break;
+               case SLIF_NUMHUMANS:
+                       result = A->info.numhumans - B->info.numhumans;
+                       break;
+               case SLIF_FREESLOTS:
+                       result = A->info.freeslots - B->info.freeslots;
+                       break;
                case SLIF_PROTOCOL:
                        result = A->info.protocol - B->info.protocol;
                        break;
@@ -285,6 +295,12 @@ static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info
                return false;
        if( !_ServerList_CompareInt( info->numplayers, mask->tests[SLIF_NUMPLAYERS], mask->info.numplayers ) )
                return false;
+       if( !_ServerList_CompareInt( info->numbots, mask->tests[SLIF_NUMBOTS], mask->info.numbots ) )
+               return false;
+       if( !_ServerList_CompareInt( info->numhumans, mask->tests[SLIF_NUMHUMANS], mask->info.numhumans ) )
+               return false;
+       if( !_ServerList_CompareInt( info->freeslots, mask->tests[SLIF_FREESLOTS], mask->info.freeslots ) )
+               return false;
        if( !_ServerList_CompareInt( info->protocol, mask->tests[SLIF_PROTOCOL], mask->info.protocol ))
                return false;
        if( *mask->info.cname
@@ -382,8 +398,15 @@ void ServerList_RebuildViewList(void)
 
 void ServerList_ResetMasks(void)
 {
+       int i;
+
        memset( &serverlist_andmasks, 0, sizeof( serverlist_andmasks ) );
        memset( &serverlist_ormasks, 0, sizeof( serverlist_ormasks ) );
+       // numbots needs to be compared to -1 to always succeed
+       for(i = 0; i < SERVERLIST_ANDMASKCOUNT; ++i)
+               serverlist_andmasks[i].info.numbots = -1;
+       for(i = 0; i < SERVERLIST_ORMASKCOUNT; ++i)
+               serverlist_ormasks[i].info.numbots = -1;
 }
 
 #if 0
@@ -426,9 +449,9 @@ int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddres
        int i;
        if (length == 0)
                return 0;
-       if (cl_netpacketloss.integer)
+       if (cl_netpacketloss_receive.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_receive.integer)
                                return 0;
        if (developer_networking.integer)
        {
@@ -450,9 +473,9 @@ int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const l
 {
        int ret;
        int i;
-       if (cl_netpacketloss.integer)
+       if (cl_netpacketloss_send.integer)
                for (i = 0;i < cl_numsockets;i++)
-                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
+                       if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss_send.integer)
                                return length;
        ret = LHNET_Write(mysocket, data, length, peeraddress);
        if (developer_networking.integer)
@@ -487,7 +510,7 @@ qboolean NetConn_CanSend(netconn_t *conn)
        }
 }
 
-int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate)
+int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, qboolean quakesignon_suppressreliables)
 {
        int totallen = 0;
 
@@ -562,7 +585,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                packetsSent++;
                unreliableMessagesSent++;
 
-               totallen += packetLen + 18;
+               totallen += packetLen + 28;
        }
        else
        {
@@ -600,11 +623,11 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                                packetsReSent++;
                        }
 
-                       totallen += packetLen + 18;
+                       totallen += packetLen + 28;
                }
 
                // if we have a new reliable message to send, do so
-               if (!conn->sendMessageLength && conn->message.cursize)
+               if (!conn->sendMessageLength && conn->message.cursize && !quakesignon_suppressreliables)
                {
                        if (conn->message.cursize > (int)sizeof(conn->sendMessage))
                        {
@@ -651,11 +674,11 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        packetsSent++;
                        reliableMessagesSent++;
 
-                       totallen += packetLen + 18;
+                       totallen += packetLen + 28;
                }
 
                // if we have an unreliable message to send, do so
-               //if (data->cursize)
+               if (data->cursize)
                {
                        packetLen = NET_HEADERSIZE + data->cursize;
 
@@ -679,8 +702,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        packetsSent++;
                        unreliableMessagesSent++;
 
-                       if (data->cursize)
-                               totallen += packetLen + 18;
+                       totallen += packetLen + 28;
                }
        }
 
@@ -694,6 +716,16 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
        return 0;
 }
 
+qboolean NetConn_HaveClientPorts(void)
+{
+       return !!cl_numsockets;
+}
+
+qboolean NetConn_HaveServerPorts(void)
+{
+       return !!sv_numsockets;
+}
+
 void NetConn_CloseClientPorts(void)
 {
        for (;cl_numsockets > 0;cl_numsockets--)
@@ -901,8 +933,6 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
        if (length < 8)
                return 0;
 
-       // TODO: add netgraph stuff rather than just packetloss counting...
-
        if (protocol == PROTOCOL_QUAKEWORLD)
        {
                int sequence, sequence_ack;
@@ -1150,7 +1180,7 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
        cls.signon = 0;                         // need all the signon messages before playing
        cls.protocol = initialprotocol;
        // reset move sequence numbering on this new connection
-       cls.movesequence = 0;
+       cls.movesequence = 1;
        cls.servermovesequence = 0;
        if (cls.protocol == PROTOCOL_QUAKEWORLD)
                Cmd_ForwardStringToServer("new");
@@ -1164,7 +1194,7 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
                msg.data = buf;
                msg.maxsize = sizeof(buf);
                MSG_WriteChar(&msg, clc_nop);
-               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol, 10000);
+               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol, 10000, false);
        }
 }
 
@@ -1217,7 +1247,7 @@ static void NetConn_ClientParsePacket_ServerList_UpdateCache(int n)
 {
        serverlist_info_t *info = &serverlist_cache[n].info;
        // update description strings for engine menu and console output
-       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numplayers > 0 && info->numplayers < info->maxplayers) ? (info->numplayers >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name);
+       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numhumans > 0 && info->numhumans < info->maxplayers) ? (info->numhumans >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name);
        dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "^4%-21.21s %-19.19s ^%c%-17.17s^4 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '4', info->mod, info->map);
        if (serverlist_cache[n].query == SQS_QUERIED)
                ServerList_ViewList_Remove(&serverlist_cache[n]);
@@ -1303,14 +1333,26 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                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 ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);else info->gameversion = 0;
+                       info->game[0] = 0;
+                       info->mod[0]  = 0;
+                       info->map[0]  = 0;
+                       info->name[0] = 0;
+                       info->protocol = -1;
+                       info->numplayers = 0;
+                       info->numbots = -1;
+                       info->maxplayers  = 0;
+                       info->gameversion = 0;
+                       if ((s = SearchInfostring(string, "gamename"     )) != NULL) strlcpy(info->game, s, sizeof (info->game));
+                       if ((s = SearchInfostring(string, "modname"      )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));
+                       if ((s = SearchInfostring(string, "mapname"      )) != NULL) strlcpy(info->map , s, sizeof (info->map ));
+                       if ((s = SearchInfostring(string, "hostname"     )) != NULL) strlcpy(info->name, s, sizeof (info->name));
+                       if ((s = SearchInfostring(string, "protocol"     )) != NULL) info->protocol = atoi(s);
+                       if ((s = SearchInfostring(string, "clients"      )) != NULL) info->numplayers = atoi(s);
+                       if ((s = SearchInfostring(string, "bots"         )) != NULL) info->numbots = atoi(s);
+                       if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);
+                       if ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);
+                       info->numhumans = info->numplayers - max(0, info->numbots);
+                       info->freeslots = info->maxplayers - info->numplayers;
 
                        NetConn_ClientParsePacket_ServerList_UpdateCache(n);
 
@@ -1697,7 +1739,7 @@ void NetConn_ClientFrame(void)
                while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
                        NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
        NetConn_QueryQueueFrame();
-       if (cls.netcon && realtime > cls.netcon->timeout)
+       if (cls.netcon && realtime > cls.netcon->timeout && !sv.active)
        {
                Con_Print("Connection timed out\n");
                CL_Disconnect();
@@ -1880,6 +1922,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
        double besttime;
        client_t *client;
        char *s, *string, response[1400], addressstring2[128], stringbuf[16384];
+       qboolean islocal = (LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP);
 
        if (!sv.active)
                return false;
@@ -1913,7 +1956,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        Com_HexDumpToConsole(data, length);
                }
 
-               if (length >= 12 && !memcmp(string, "getchallenge", 12) && sv_public.integer > -2)
+               if (length >= 12 && !memcmp(string, "getchallenge", 12) && (islocal || sv_public.integer > -2))
                {
                        for (i = 0, best = 0, besttime = realtime;i < MAX_CHALLENGES;i++)
                        {
@@ -1935,7 +1978,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        NetConn_WriteString(mysocket, va("\377\377\377\377challenge %s", challenge[i].string), peeraddress);
                        return true;
                }
-               if (length > 8 && !memcmp(string, "connect\\", 8) && sv_public.integer > -2)
+               if (length > 8 && !memcmp(string, "connect\\", 8) && (islocal || sv_public.integer > -2))
                {
                        string += 7;
                        length -= 7;
@@ -2018,7 +2061,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
 
                        return true;
                }
-               if (length >= 7 && !memcmp(string, "getinfo", 7) && sv_public.integer > -1)
+               if (length >= 7 && !memcmp(string, "getinfo", 7) && (islocal || sv_public.integer > -1))
                {
                        const char *challenge = NULL;
 
@@ -2034,7 +2077,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        }
                        return true;
                }
-               if (length >= 9 && !memcmp(string, "getstatus", 9) && sv_public.integer > -1)
+               if (length >= 9 && !memcmp(string, "getstatus", 9) && (islocal || sv_public.integer > -1))
                {
                        const char *challenge = NULL;
 
@@ -2054,18 +2097,36 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                {
                        int i;
                        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++)
                                if (i < (int)sizeof(password) - 1)
                                        password[i++] = *s;
+                       if(*s <= ' ' && s != endpos) // skip leading ugly space
+                               ++s;
                        password[i] = 0;
                        if (password[0] > ' ' && !strcmp(rcon_password.string, password))
                        {
                                // looks like a legitimate rcon command with the correct password
-                               Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s);
+                               char *s_ptr = s;
+                               Con_Printf("server received rcon command from %s:\n", host_client ? host_client->name : addressstring2);
+                               while(s_ptr != endpos)
+                               {
+                                       size_t l = strlen(s_ptr);
+                                       if(l)
+                                               Con_Printf(" %s;", s_ptr);
+                                       s_ptr += l + 1;
+                               }
+                               Con_Printf("\n");
                                rcon_redirect = true;
                                rcon_redirect_bufferpos = 0;
-                               Cmd_ExecuteString(s, src_command);
+                               while(s != endpos)
+                               {
+                                       size_t l = strlen(s);
+                                       if(l)
+                                               Cmd_ExecuteString(s, src_command);
+                                       s += l + 1;
+                               }
                                rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
                                rcon_redirect = false;
                                // print resulting text to client
@@ -2108,7 +2169,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
        // protocol
        // (this protects more modern protocols against being used for
        //  Quake packet flood Denial Of Service attacks)
-       if (length >= 5 && (i = BigLong(*((int *)data))) && (i & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (i & NETFLAG_LENGTH_MASK) == length && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3))
+       if (length >= 5 && (i = BigLong(*((int *)data))) && (i & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (i & NETFLAG_LENGTH_MASK) == length && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3 || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3))
        {
                int c;
                int protocolnumber;
@@ -2124,7 +2185,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                case CCREQ_CONNECT:
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
-                       if(sv_public.integer <= -2)
+                       if(!islocal && sv_public.integer <= -2)
                                break;
 
                        protocolname = MSG_ReadString();
@@ -2225,7 +2286,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                case CCREQ_SERVER_INFO:
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_SERVER_INFO from %s.\n", addressstring2);
-                       if(sv_public.integer <= -1)
+                       if(!islocal && sv_public.integer <= -1)
                                break;
                        if (sv.active && !strcmp(MSG_ReadString(), "QUAKE"))
                        {
@@ -2256,7 +2317,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                case CCREQ_PLAYER_INFO:
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_PLAYER_INFO from %s.\n", addressstring2);
-                       if(sv_public.integer <= -1)
+                       if(!islocal && sv_public.integer <= -1)
                                break;
                        if (sv.active)
                        {
@@ -2289,7 +2350,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                case CCREQ_RULE_INFO:
                        if (developer.integer >= 10)
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_RULE_INFO from %s.\n", addressstring2);
-                       if(sv_public.integer <= -1)
+                       if(!islocal && sv_public.integer <= -1)
                                break;
                        if (sv.active)
                        {
@@ -2561,7 +2622,8 @@ void NetConn_Init(void)
        Cvar_RegisterVariable(&net_connecttimeout);
        Cvar_RegisterVariable(&net_connectfloodblockingtimeout);
        Cvar_RegisterVariable(&cl_netlocalping);
-       Cvar_RegisterVariable(&cl_netpacketloss);
+       Cvar_RegisterVariable(&cl_netpacketloss_send);
+       Cvar_RegisterVariable(&cl_netpacketloss_receive);
        Cvar_RegisterVariable(&hostname);
        Cvar_RegisterVariable(&developer_networking);
        Cvar_RegisterVariable(&cl_netport);