]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
patch from div0 that adds a special "extResponse " type of packet that can be returne...
[xonotic/darkplaces.git] / netconn.c
index d848411d6c5193912060a3ddd254e44c2060bab0..02f6b5a9e718ed5492b054e786f9b8ba5a8d3c31 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -27,7 +27,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #define DPMASTER_PORT 27950
 
 // note this defaults on for dedicated servers, off for listen servers
-cvar_t sv_public = {0, "sv_public", "0", "advertises this server on the master server (so that players can find it in the server browser)"};
+cvar_t sv_public = {0, "sv_public", "0", "1: advertises this server on the master server (so that players can find it in the server browser); 0: allow direct queries only; -1: do not respond to direct queries; -2: do not allow anyone to connect"};
 static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "120", "how often to send heartbeat in seconds (only used if sv_public is 1)"};
 
 static cvar_t sv_masters [] =
@@ -126,6 +126,10 @@ cvar_t sv_netport = {0, "port", "26000", "server port for players to connect to"
 cvar_t net_address = {0, "net_address", "0.0.0.0", "network address to open ports on"};
 //cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]", "network address to open ipv6 ports on"};
 
+char net_extresponse[NET_EXTRESPONSE_MAX][1400];
+int net_extresponse_count = 0;
+int net_extresponse_last = 0;
+
 // ServerList interface
 serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
 serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
@@ -787,10 +791,15 @@ netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
        return conn;
 }
 
+void NetConn_ClearConnectFlood(lhnetaddress_t *peeraddress);
 void NetConn_Close(netconn_t *conn)
 {
        netconn_t *c;
        // remove connection from list
+
+       // allow the client to reconnect immediately
+       NetConn_ClearConnectFlood(&(conn->peeraddress));
+
        if (conn == netconn_list)
                netconn_list = conn->next;
        else
@@ -1087,14 +1096,14 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
                Cmd_ForwardStringToServer("new");
        if (cls.protocol == PROTOCOL_QUAKE)
        {
-               // write a keepalive (svc_nop) as it seems to greatly improve the
+               // write a keepalive (clc_nop) as it seems to greatly improve the
                // chances of connecting to a netquake server
                sizebuf_t msg;
                unsigned char buf[4];
                memset(&msg, 0, sizeof(msg));
                msg.data = buf;
                msg.maxsize = sizeof(buf);
-               MSG_WriteChar(&msg, svc_nop);
+               MSG_WriteChar(&msg, clc_nop);
                NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol);
        }
 }
@@ -1338,6 +1347,15 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        serverlist_querywaittime = realtime + 3;
                        return true;
                }
+               if (!strncmp(string, "extResponse ", 12))
+               {
+                       ++net_extresponse_count;
+                       if(net_extresponse_count > NET_EXTRESPONSE_MAX)
+                               net_extresponse_count = NET_EXTRESPONSE_MAX;
+                       net_extresponse_last = (net_extresponse_last + 1) % NET_EXTRESPONSE_MAX;
+                       dpsnprintf(net_extresponse[net_extresponse_last], sizeof(net_extresponse[net_extresponse_last]), "%s %s", addressstring2, string + 12);
+                       return true;
+               }
                if (!strncmp(string, "ping", 4))
                {
                        if (developer.integer >= 10)
@@ -1695,7 +1713,7 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                        client_t *cl = &svs.clients[i];
                        if (cl->active)
                        {
-                               int nameind, cleanind;
+                               int nameind, cleanind, pingvalue;
                                char curchar;
                                char cleanname [sizeof(cl->name)];
 
@@ -1713,9 +1731,14 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                        }
                                } while (curchar != '\0');
 
+                               pingvalue = (int)(cl->ping * 1000.0f);
+                               if(cl->netconnection)
+                                       pingvalue = bound(1, pingvalue, 9999);
+                               else
+                                       pingvalue = 0;
                                length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
                                                                        cl->frags,
-                                                                       (int)(cl->ping * 1000.0f),
+                                                                       pingvalue,
                                                                        cleanname);
                                if(length < 0)
                                        return false;
@@ -1753,6 +1776,7 @@ static qboolean NetConn_PreventConnectFlood(lhnetaddress_t *peeraddress)
                                // renew the ban on this address so it does not expire
                                // until the flood has subsided
                                sv.connectfloodaddresses[floodslotnum].lasttime = realtime;
+                               //Con_Printf("Flood detected!\n");
                                return true;
                        }
                        // the flood appears to have subsided, so allow this
@@ -1763,9 +1787,30 @@ static qboolean NetConn_PreventConnectFlood(lhnetaddress_t *peeraddress)
        // begin a new timeout on this address
        sv.connectfloodaddresses[bestfloodslotnum].address = noportpeeraddress;
        sv.connectfloodaddresses[bestfloodslotnum].lasttime = realtime;
+       //Con_Printf("Flood detection initiated!\n");
        return false;
 }
 
+void NetConn_ClearConnectFlood(lhnetaddress_t *peeraddress)
+{
+       int floodslotnum;
+       lhnetaddress_t noportpeeraddress;
+       // see if this is a connect flood
+       noportpeeraddress = *peeraddress;
+       LHNETADDRESS_SetPort(&noportpeeraddress, 0);
+       for (floodslotnum = 0;floodslotnum < MAX_CONNECTFLOODADDRESSES;floodslotnum++)
+       {
+               if (sv.connectfloodaddresses[floodslotnum].lasttime && LHNETADDRESS_Compare(&noportpeeraddress, &sv.connectfloodaddresses[floodslotnum].address) == 0)
+               {
+                       // this address matches an ongoing flood address
+                       // remove the ban
+                       sv.connectfloodaddresses[floodslotnum].address.addresstype = LHNETADDRESSTYPE_NONE;
+                       sv.connectfloodaddresses[floodslotnum].lasttime = 0;
+                       //Con_Printf("Flood cleared!\n");
+               }
+       }
+}
+
 extern void SV_SendServerinfo (client_t *client);
 static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
 {
@@ -1806,7 +1851,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        Com_HexDumpToConsole(data, length);
                }
 
-               if (length >= 12 && !memcmp(string, "getchallenge", 12))
+               if (length >= 12 && !memcmp(string, "getchallenge", 12) && sv_public.integer > -2)
                {
                        for (i = 0, best = 0, besttime = realtime;i < MAX_CHALLENGES;i++)
                        {
@@ -1828,7 +1873,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))
+               if (length > 8 && !memcmp(string, "connect\\", 8) && sv_public.integer > -2)
                {
                        string += 7;
                        length -= 7;
@@ -1866,7 +1911,9 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                                if (developer.integer >= 10)
                                                        Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", addressstring2);
                                                NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
+                                               SV_VM_Begin();
                                                SV_SendServerinfo(client);
+                                               SV_VM_End();
                                        }
                                        else
                                        {
@@ -1909,7 +1956,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
 
                        return true;
                }
-               if (length >= 7 && !memcmp(string, "getinfo", 7))
+               if (length >= 7 && !memcmp(string, "getinfo", 7) && sv_public.integer > -1)
                {
                        const char *challenge = NULL;
 
@@ -1925,7 +1972,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        }
                        return true;
                }
-               if (length >= 9 && !memcmp(string, "getstatus", 9))
+               if (length >= 9 && !memcmp(string, "getstatus", 9) && sv_public.integer > -1)
                {
                        const char *challenge = NULL;
 
@@ -2011,6 +2058,8 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
                        if (length < (int)strlen("QUAKE") + 1 + 1)
                                break;
+                       if(sv_public.integer <= -2)
+                               break;
 
                        if (memcmp(data, "QUAKE", strlen("QUAKE") + 1) != 0 || (int)data[strlen("QUAKE") + 1] != NET_PROTOCOL_VERSION)
                        {
@@ -2051,7 +2100,11 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                        // if client is already spawned, re-send the
                                        // serverinfo message as they'll need it to play
                                        if (client->spawned)
+                                       {
+                                               SV_VM_Begin();
                                                SV_SendServerinfo(client);
+                                               SV_VM_End();
+                                       }
                                        return true;
                                }
                        }
@@ -2104,6 +2157,8 @@ 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)
+                               break;
                        if (sv.active && !strcmp(MSG_ReadString(), "QUAKE"))
                        {
                                int numclients;
@@ -2133,6 +2188,8 @@ 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)
+                               break;
                        if (sv.active)
                        {
                                int playerNumber, activeNumber, clientNumber;
@@ -2164,6 +2221,8 @@ 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)
+                               break;
                        if (sv.active)
                        {
                                char *prevCvarName;
@@ -2341,7 +2400,7 @@ void NetConn_Heartbeat(int priority)
 
        // make advertising optional and don't advertise singleplayer games, and
        // only send a heartbeat as often as the admin wants
-       if (sv.active && sv_public.integer && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
+       if (sv.active && sv_public.integer > 0 && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
        {
                nextheartbeattime = realtime + sv_heartbeatperiod.value;
                for (masternum = 0;sv_masters[masternum].name;masternum++)