]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
split up net_slist into net_slist and net_slistqw, and modified menu accordingly...
[xonotic/darkplaces.git] / netconn.c
index ef47f7e6f3980119f6d38bc85cc7064bd04294da..4a6ea050cf48a564f778dd11a794b6bc651224d2 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -23,13 +23,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "lhnet.h"
 
-#define MASTER_PORT 27950
+#define QWMASTER_PORT 27000
+#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)"};
 static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "120", "how often to send heartbeat in seconds (only used if sv_public is 1)"};
 
-// 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", "", "user-chosen master server 1"},
@@ -43,6 +43,31 @@ static cvar_t sv_masters [] =
        {0, NULL, NULL, NULL}
 };
 
+static cvar_t sv_qwmasters [] =
+{
+       {CVAR_SAVE, "sv_qwmaster1", "", "user-chosen qwmaster server 1"},
+       {CVAR_SAVE, "sv_qwmaster2", "", "user-chosen qwmaster server 2"},
+       {CVAR_SAVE, "sv_qwmaster3", "", "user-chosen qwmaster server 3"},
+       {CVAR_SAVE, "sv_qwmaster4", "", "user-chosen qwmaster server 4"},
+       {0, "sv_qwmasterextra1", "satan.idsoftware.com", "default qwmaster server 1 (admin: idSoftware)"},
+       {0, "sv_qwmasterextra2", "192.246.40.37:27000", "id Limbo (admin: id Software)"},
+       {0, "sv_qwmasterextra3", "192.246.40.37:27002", "id CTF (admin: id Software)"},
+       {0, "sv_qwmasterextra4", "192.246.40.37:27003", "id TeamFortress (admin: id Software)"},
+       {0, "sv_qwmasterextra5", "192.246.40.37:27004", "id Miscilaneous (admin: id Software)"},
+       {0, "sv_qwmasterextra6", "192.246.40.37:27006", "id Deathmatch Only (admin: id Software)"},
+       {0, "sv_qwmasterextra7", "150.254.66.120:27000", "Poland's master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra8", "62.112.145.129:27000", "Ocrana master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra9", "master.edome.net", "edome master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra10", "qwmaster.barrysworld.com", "barrysworld master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra11", "qwmaster.ocrana.de:27000", "Ocrana2 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra12", "213.221.174.165:27000", "unknown1 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra13", "195.74.0.8", "unknown2 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra14", "192.246.40.37", "unknown3 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra15", "192.246.40.37:27006", "unknown4 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra16", "204.182.161.2", "unknown5 master server. (admin: unknown)"},
+       {0, NULL, NULL, NULL}
+};
+
 static double nextheartbeattime = 0;
 
 sizebuf_t net_message;
@@ -61,6 +86,8 @@ 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_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 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"};
+
 /* statistic counters */
 static int packetsSent = 0;
 static int packetsReSent = 0;
@@ -80,7 +107,11 @@ int serverquerycount = 0;
 int serverreplycount = 0;
 
 // this is only false if there are still servers left to query
-int serverlist_querysleep = true;
+static qboolean serverlist_querysleep = true;
+// this is pushed a second or two ahead of realtime whenever a master server
+// reply is received, to avoid issuing queries while master replies are still
+// flooding in (which would make a mess of the ping times)
+static double serverlist_querywaittime = 0;
 
 static unsigned char sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
 static unsigned char readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
@@ -273,6 +304,10 @@ static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 {
        int start, end, mid;
 
+       // reject incompatible servers
+       if (entry->info.gameversion != gameversion.integer)
+               return;
+
        // FIXME: change this to be more readable (...)
        // now check whether it passes through the masks
        for( start = 0 ; serverlist_andmasks[start].active && start < SERVERLIST_ANDMASKCOUNT ; start++ )
@@ -362,9 +397,10 @@ static void _ServerList_Test(void)
 }
 #endif
 
-void ServerList_QueryList(void)
+void ServerList_QueryList(qboolean querydp, qboolean queryqw)
 {
-       masterquerytime = realtime;
+       //masterquerytime = realtime;
+       masterquerytime = Sys_DoubleTime();
        masterquerycount = 0;
        masterreplycount = 0;
        serverquerycount = 0;
@@ -375,7 +411,7 @@ void ServerList_QueryList(void)
 
        //_ServerList_Test();
 
-       NetConn_QueryMasters();
+       NetConn_QueryMasters(querydp, queryqw);
 }
 
 // rest
@@ -432,113 +468,177 @@ int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnet
        return NetConn_Write(mysocket, string, (int)strlen(string), peeraddress);
 }
 
-int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
+int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol)
 {
-       unsigned int packetLen;
-       unsigned int dataLen;
-       unsigned int eom;
-       unsigned int *header;
-
-       // if a reliable message fragment has been lost, send it again
-       if (!conn->canSend && conn->sendMessageLength && (realtime - conn->lastSendTime) > 1.0)
+       if (protocol == PROTOCOL_QUAKEWORLD)
        {
-               if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+               int packetLen;
+               qboolean sendreliable;
+
+               // note that it is ok to send empty messages to the qw server,
+               // otherwise it won't respond to us at all
+
+               sendreliable = false;
+               // if the remote side dropped the last reliable message, resend it
+               if (conn->qw.incoming_acknowledged > conn->qw.last_reliable_sequence && conn->qw.incoming_reliable_acknowledged != conn->qw.reliable_sequence)
+                       sendreliable = true;
+               // if the reliable transmit buffer is empty, copy the current message out
+               if (!conn->sendMessageLength && conn->message.cursize)
                {
-                       dataLen = conn->sendMessageLength;
-                       eom = NETFLAG_EOM;
+                       memcpy (conn->sendMessage, conn->message.data, conn->message.cursize);
+                       conn->sendMessageLength = conn->message.cursize;
+                       SZ_Clear(&conn->message); // clear the message buffer
+                       conn->qw.reliable_sequence ^= 1;
+                       sendreliable = true;
                }
-               else
+               // outgoing unreliable packet number, and outgoing reliable packet number (0 or 1)
+               *((int *)(sendbuffer + 0)) = LittleLong((unsigned int)conn->qw.outgoing_sequence | ((unsigned int)sendreliable<<31));
+               // last received unreliable packet number, and last received reliable packet number (0 or 1)
+               *((int *)(sendbuffer + 4)) = LittleLong((unsigned int)conn->qw.incoming_sequence | ((unsigned int)conn->qw.incoming_reliable_sequence<<31));
+               packetLen = 8;
+               conn->qw.outgoing_sequence++;
+               // client sends qport in every packet
+               if (conn == cls.netcon)
                {
-                       dataLen = MAX_PACKETFRAGMENT;
-                       eom = 0;
+                       *((short *)(sendbuffer + 8)) = LittleShort(cls.qw_qport);
+                       packetLen += 2;
                }
-
-               packetLen = NET_HEADERSIZE + dataLen;
-
-               header = (unsigned int *)sendbuffer;
-               header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-               header[1] = BigLong(conn->sendSequence - 1);
-               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
-
-               if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
+               if (packetLen + (sendreliable ? conn->sendMessageLength : 0) > 1400)
                {
-                       conn->lastSendTime = realtime;
-                       packetsReSent++;
+                       Con_Printf ("NetConn_SendUnreliableMessage: reliable message too big %u\n", data->cursize);
+                       return -1;
                }
-       }
-
-       // if we have a new reliable message to send, do so
-       if (conn->canSend && conn->message.cursize)
-       {
-               if (conn->message.cursize > (int)sizeof(conn->sendMessage))
+               // add the reliable message if there is one
+               if (sendreliable)
                {
-                       Con_Printf("NetConn_SendUnreliableMessage: reliable message too big (%u > %u)\n", conn->message.cursize, sizeof(conn->sendMessage));
-                       conn->message.overflowed = true;
-                       return -1;
+                       memcpy(sendbuffer + packetLen, conn->sendMessage, conn->sendMessageLength);
+                       packetLen += conn->sendMessageLength;
+                       conn->qw.last_reliable_sequence = conn->qw.outgoing_sequence;
                }
-
-               if (developer_networking.integer && conn == cls.netcon)
+               // add the unreliable message if possible
+               if (packetLen + data->cursize <= 1400)
                {
-                       Con_Print("client sending reliable message to server:\n");
-                       SZ_HexDumpToConsole(&conn->message);
+                       memcpy(sendbuffer + packetLen, data->data, data->cursize);
+                       packetLen += data->cursize;
                }
 
-               memcpy(conn->sendMessage, conn->message.data, conn->message.cursize);
-               conn->sendMessageLength = conn->message.cursize;
-               SZ_Clear(&conn->message);
+               NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
+
+               packetsSent++;
+               unreliableMessagesSent++;
+
+               // delay later packets to obey rate limit
+               conn->qw.cleartime = max(conn->qw.cleartime, realtime) + packetLen * conn->qw.rate;
+
+               return 0;
+       }
+       else
+       {
+               unsigned int packetLen;
+               unsigned int dataLen;
+               unsigned int eom;
+               unsigned int *header;
 
-               if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+               // if a reliable message fragment has been lost, send it again
+               if (conn->sendMessageLength && (realtime - conn->lastSendTime) > 1.0)
                {
-                       dataLen = conn->sendMessageLength;
-                       eom = NETFLAG_EOM;
+                       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+                       {
+                               dataLen = conn->sendMessageLength;
+                               eom = NETFLAG_EOM;
+                       }
+                       else
+                       {
+                               dataLen = MAX_PACKETFRAGMENT;
+                               eom = 0;
+                       }
+
+                       packetLen = NET_HEADERSIZE + dataLen;
+
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
+                       header[1] = BigLong(conn->nq.sendSequence - 1);
+                       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
+
+                       if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
+                       {
+                               conn->lastSendTime = realtime;
+                               packetsReSent++;
+                       }
                }
-               else
+
+               // if we have a new reliable message to send, do so
+               if (!conn->sendMessageLength && conn->message.cursize)
                {
-                       dataLen = MAX_PACKETFRAGMENT;
-                       eom = 0;
-               }
+                       if (conn->message.cursize > (int)sizeof(conn->sendMessage))
+                       {
+                               Con_Printf("NetConn_SendUnreliableMessage: reliable message too big (%u > %u)\n", conn->message.cursize, sizeof(conn->sendMessage));
+                               conn->message.overflowed = true;
+                               return -1;
+                       }
+
+                       if (developer_networking.integer && conn == cls.netcon)
+                       {
+                               Con_Print("client sending reliable message to server:\n");
+                               SZ_HexDumpToConsole(&conn->message);
+                       }
 
-               packetLen = NET_HEADERSIZE + dataLen;
+                       memcpy(conn->sendMessage, conn->message.data, conn->message.cursize);
+                       conn->sendMessageLength = conn->message.cursize;
+                       SZ_Clear(&conn->message);
 
-               header = (unsigned int *)sendbuffer;
-               header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-               header[1] = BigLong(conn->sendSequence);
-               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
+                       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+                       {
+                               dataLen = conn->sendMessageLength;
+                               eom = NETFLAG_EOM;
+                       }
+                       else
+                       {
+                               dataLen = MAX_PACKETFRAGMENT;
+                               eom = 0;
+                       }
 
-               conn->sendSequence++;
-               conn->canSend = false;
+                       packetLen = NET_HEADERSIZE + dataLen;
 
-               NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
+                       header[1] = BigLong(conn->nq.sendSequence);
+                       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
-               conn->lastSendTime = realtime;
-               packetsSent++;
-               reliableMessagesSent++;
-       }
+                       conn->nq.sendSequence++;
 
-       // if we have an unreliable message to send, do so
-       if (data->cursize)
-       {
-               packetLen = NET_HEADERSIZE + data->cursize;
+                       NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
-               if (packetLen > (int)sizeof(sendbuffer))
-               {
-                       Con_Printf("NetConn_SendUnreliableMessage: message too big %u\n", data->cursize);
-                       return -1;
+                       conn->lastSendTime = realtime;
+                       packetsSent++;
+                       reliableMessagesSent++;
                }
 
-               header = (unsigned int *)sendbuffer;
-               header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
-               header[1] = BigLong(conn->unreliableSendSequence);
-               memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
+               // if we have an unreliable message to send, do so
+               if (data->cursize)
+               {
+                       packetLen = NET_HEADERSIZE + data->cursize;
 
-               conn->unreliableSendSequence++;
+                       if (packetLen > (int)sizeof(sendbuffer))
+                       {
+                               Con_Printf("NetConn_SendUnreliableMessage: message too big %u\n", data->cursize);
+                               return -1;
+                       }
 
-               NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
+                       header[1] = BigLong(conn->nq.unreliableSendSequence);
+                       memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
 
-               packetsSent++;
-               unreliableMessagesSent++;
+                       conn->nq.unreliableSendSequence++;
+
+                       NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
+
+                       packetsSent++;
+                       unreliableMessagesSent++;
+               }
+               return 0;
        }
-       return 0;
 }
 
 void NetConn_CloseClientPorts(void)
@@ -670,7 +770,6 @@ netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
        conn = (netconn_t *)Mem_Alloc(netconn_mempool, sizeof(*conn));
        conn->mysocket = mysocket;
        conn->peeraddress = *peeraddress;
-       conn->canSend = true;
        conn->lastMessageTime = realtime;
        conn->message.data = conn->messagedata;
        conn->message.maxsize = sizeof(conn->messagedata);
@@ -738,15 +837,76 @@ void NetConn_UpdateSockets(void)
        }
 }
 
-static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length)
+static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length, protocolversion_t protocol)
 {
-       unsigned int count;
-       unsigned int flags;
-       unsigned int sequence;
-       int qlength;
+       if (length < 8)
+               return 0;
 
-       if (length >= 8)
+       if (protocol == PROTOCOL_QUAKEWORLD)
        {
+               int sequence, sequence_ack;
+               int reliable_ack, reliable_message;
+               int count;
+               int qport;
+
+               sequence = LittleLong(*((int *)(data + 0)));
+               sequence_ack = LittleLong(*((int *)(data + 4)));
+               data += 8;
+               length -= 8;
+
+               if (conn != cls.netcon)
+               {
+                       // server only
+                       if (length < 2)
+                               return 0;
+                       // TODO: use qport to identify that this client really is who they say they are?  (and elsewhere in the code to identify the connection without a port match?)
+                       qport = LittleShort(*((int *)(data + 8)));
+                       data += 2;
+                       length -= 2;
+               }
+
+               packetsReceived++;
+               reliable_message = (sequence >> 31) & 1;
+               reliable_ack = (sequence_ack >> 31) & 1;
+               sequence &= ~(1<<31);
+               sequence_ack &= ~(1<<31);
+               if (sequence <= conn->qw.incoming_sequence)
+               {
+                       Con_DPrint("Got a stale datagram\n");
+                       return 0;
+               }
+               count = sequence - (conn->qw.incoming_sequence + 1);
+               if (count > 0)
+               {
+                       droppedDatagrams += count;
+                       Con_DPrintf("Dropped %u datagram(s)\n", count);
+               }
+               if (reliable_ack == conn->qw.reliable_sequence)
+               {
+                       // received, now we will be able to send another reliable message
+                       conn->sendMessageLength = 0;
+                       reliableMessagesReceived++;
+               }
+               conn->qw.incoming_sequence = sequence;
+               conn->qw.incoming_acknowledged = sequence_ack;
+               conn->qw.incoming_reliable_acknowledged = reliable_ack;
+               if (reliable_message)
+                       conn->qw.incoming_reliable_sequence ^= 1;
+               conn->lastMessageTime = realtime;
+               conn->timeout = realtime + net_messagetimeout.value;
+               unreliableMessagesReceived++;
+               SZ_Clear(&net_message);
+               SZ_Write(&net_message, data, length);
+               MSG_BeginReading();
+               return 2;
+       }
+       else
+       {
+               unsigned int count;
+               unsigned int flags;
+               unsigned int sequence;
+               int qlength;
+
                qlength = (unsigned int)BigLong(((int *)data)[0]);
                flags = qlength & ~NETFLAG_LENGTH_MASK;
                qlength &= NETFLAG_LENGTH_MASK;
@@ -759,15 +919,15 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                        length -= 8;
                        if (flags & NETFLAG_UNRELIABLE)
                        {
-                               if (sequence >= conn->unreliableReceiveSequence)
+                               if (sequence >= conn->nq.unreliableReceiveSequence)
                                {
-                                       if (sequence > conn->unreliableReceiveSequence)
+                                       if (sequence > conn->nq.unreliableReceiveSequence)
                                        {
-                                               count = sequence - conn->unreliableReceiveSequence;
+                                               count = sequence - conn->nq.unreliableReceiveSequence;
                                                droppedDatagrams += count;
                                                Con_DPrintf("Dropped %u datagram(s)\n", count);
                                        }
-                                       conn->unreliableReceiveSequence = sequence + 1;
+                                       conn->nq.unreliableReceiveSequence = sequence + 1;
                                        conn->lastMessageTime = realtime;
                                        conn->timeout = realtime + net_messagetimeout.value;
                                        unreliableMessagesReceived++;
@@ -785,12 +945,12 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                        }
                        else if (flags & NETFLAG_ACK)
                        {
-                               if (sequence == (conn->sendSequence - 1))
+                               if (sequence == (conn->nq.sendSequence - 1))
                                {
-                                       if (sequence == conn->ackSequence)
+                                       if (sequence == conn->nq.ackSequence)
                                        {
-                                               conn->ackSequence++;
-                                               if (conn->ackSequence != conn->sendSequence)
+                                               conn->nq.ackSequence++;
+                                               if (conn->nq.ackSequence != conn->nq.sendSequence)
                                                        Con_DPrint("ack sequencing error\n");
                                                conn->lastMessageTime = realtime;
                                                conn->timeout = realtime + net_messagetimeout.value;
@@ -819,10 +979,10 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
 
                                                        header = (unsigned int *)sendbuffer;
                                                        header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-                                                       header[1] = BigLong(conn->sendSequence);
+                                                       header[1] = BigLong(conn->nq.sendSequence);
                                                        memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
-                                                       conn->sendSequence++;
+                                                       conn->nq.sendSequence++;
 
                                                        if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
                                                        {
@@ -831,10 +991,7 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                                                        }
                                                }
                                                else
-                                               {
                                                        conn->sendMessageLength = 0;
-                                                       conn->canSend = true;
-                                               }
                                        }
                                        else
                                                Con_DPrint("Duplicate ACK received\n");
@@ -849,11 +1006,11 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                                temppacket[0] = BigLong(8 | NETFLAG_ACK);
                                temppacket[1] = BigLong(sequence);
                                NetConn_Write(conn->mysocket, (unsigned char *)temppacket, 8, &conn->peeraddress);
-                               if (sequence == conn->receiveSequence)
+                               if (sequence == conn->nq.receiveSequence)
                                {
                                        conn->lastMessageTime = realtime;
                                        conn->timeout = realtime + net_messagetimeout.value;
-                                       conn->receiveSequence++;
+                                       conn->nq.receiveSequence++;
                                        if( conn->receiveMessageLength + length <= (int)sizeof( conn->receiveMessage ) ) {
                                                memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
                                                conn->receiveMessageLength += length;
@@ -886,7 +1043,7 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
        return 0;
 }
 
-void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
+void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress, protocolversion_t initialprotocol)
 {
        cls.connect_trying = false;
        M_Update_Return_Reason("");
@@ -903,6 +1060,9 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
        cls.demonum = -1;                       // not in the demo loop now
        cls.state = ca_connected;
        cls.signon = 0;                         // need all the signon messages before playing
+       cls.protocol = initialprotocol;
+       if (cls.protocol == PROTOCOL_QUAKEWORLD)
+               Cmd_ForwardStringToServer("new");
 }
 
 int NetConn_IsLocalGame(void)
@@ -914,11 +1074,15 @@ int NetConn_IsLocalGame(void)
 
 static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
 {
+       qboolean fromserver;
        int ret, c, control;
        const char *s;
        char *string, addressstring2[128], cname[128], ipstring[32];
        char stringbuf[16384];
 
+       // quakeworld ingame packet
+       fromserver = cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress);
+
        if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
        {
                // received a command string - strip off the packaging and put it
@@ -930,27 +1094,45 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                stringbuf[length] = 0;
                string = stringbuf;
 
-               if (developer.integer)
+               LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+
+               if (developer_networking.integer)
                {
-                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
                        Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2);
                        Com_HexDumpToConsole(data, length);
                }
 
                if (length > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
                {
+                       // darkplaces or quake3
                        char protocolnames[1400];
                        Protocol_Names(protocolnames, sizeof(protocolnames));
-                       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\\protocols\\%s\\challenge\\%s", protocolnames, string + 10), peeraddress);
                        return true;
                }
+               if (length > 1 && string[0] == 'c' && (string[1] == '-' || (string[1] >= '0' && string[1] <= '9')))
+               {
+                       // quakeworld
+                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+                       Con_Printf("\"%s\" received, sending QuakeWorld connect request back to %s\n", string, addressstring2);
+                       M_Update_Return_Reason("Got QuakeWorld challenge response");
+                       cls.qw_qport = qport.integer;
+                       NetConn_WriteString(mysocket, va("\377\377\377\377connect 28 %i %i \"%s\"\n", cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress);
+               }
                if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying)
                {
+                       // darkplaces or quake3
                        M_Update_Return_Reason("Accepted");
-                       NetConn_ConnectionEstablished(mysocket, peeraddress);
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_DARKPLACES3);
+                       return true;
+               }
+               if (length > 1 && string[0] == 'j' && cls.connect_trying)
+               {
+                       // quakeworld
+                       M_Update_Return_Reason("QuakeWorld Accepted");
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD);
                        return true;
                }
                if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying)
@@ -989,7 +1171,8 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                // store the data the engine cares about (address and ping)
                                strlcpy (serverlist_cache[serverlist_cachecount].info.cname, cname, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
                                serverlist_cache[serverlist_cachecount].info.ping = 100000;
-                               serverlist_cache[serverlist_cachecount].querytime = realtime;
+                               //serverlist_cache[serverlist_cachecount].querytime = realtime;
+                               serverlist_cache[serverlist_cachecount].querytime = Sys_DoubleTime();
                                // if not in the slist menu we should print the server to console
                                if (serverlist_consoleoutput) {
                                        Con_Printf("querying %s\n", ipstring);
@@ -1006,11 +1189,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        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;
 
                        if (info->ping == 100000)
                                        serverreplycount++;
 
-                       pingtime = (int)((realtime - serverlist_cache[n].querytime) * 1000.0);
+                       pingtime = (int)((Sys_DoubleTime() - serverlist_cache[n].querytime) * 1000.0 + 0.5);
                        pingtime = bound(0, pingtime, 9999);
                        // update the ping
                        info->ping = pingtime;
@@ -1018,8 +1202,8 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        // 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), "%c%c%5d%c%c%c%3u/%3u %-65.65s", STRING_COLOR_TAG, pingtime >= 300 ? '1' : (pingtime >= 200 ? '3' : '7'), (int)pingtime, STRING_COLOR_TAG, STRING_COLOR_DEFAULT + '0', 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);
+                       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", pingtime >= 300 ? '1' : (pingtime >= 200 ? '3' : '7'), (int)pingtime, ((info->numplayers > 0 && info->numplayers < info->maxplayers) ? (info->numplayers >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name);
+                       dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "^5%-21.21s %-19.19s ^%c%-17.17s^5 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '5', info->mod, info->map);
                        if( serverlist_cache[n].query == SQS_QUERIED ) {
                                ServerList_ViewList_Remove( &serverlist_cache[n] );
                        }
@@ -1039,14 +1223,14 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        length -= 18;
                        masterreplycount++;
                        if (serverlist_consoleoutput)
-                               Con_Print("received server list...\n");
+                               Con_Print("received DarkPlaces 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)
                        {
                                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);
+                               dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], data[5] * 256 + data[6]);
+                               if (serverlist_consoleoutput && developer_networking.integer)
+                                       Con_Printf("Requesting info from DarkPlaces server %s\n", ipstring);
                                // ignore the rest of the message if the serverlist is full
                                if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
                                        break;
@@ -1059,6 +1243,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                        serverquerycount++;
 
                                        memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
+                                       serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_DARKPLACES7;
                                        // 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;
@@ -1073,6 +1258,52 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        }
                        // begin or resume serverlist queries
                        serverlist_querysleep = false;
+                       serverlist_querywaittime = realtime + 3;
+                       return true;
+               }
+               if (!memcmp(string, "d\n", 2) && serverlist_cachecount < SERVERLIST_TOTALSIZE)
+               {
+                       // Extract the IP addresses
+                       data += 2;
+                       length -= 2;
+                       masterreplycount++;
+                       if (serverlist_consoleoutput)
+                               Con_Print("received QuakeWorld server list...\n");
+                       while (length >= 6 && (data[0] != 0xFF || data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF) && data[4] * 256 + data[5] != 0)
+                       {
+                               int n;
+
+                               dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[0], data[1], data[2], data[3], data[4] * 256 + data[5]);
+                               if (serverlist_consoleoutput && developer_networking.integer)
+                                       Con_Printf("Requesting info from QuakeWorld server %s\n", ipstring);
+                               // 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;
+                               if( n >= serverlist_cachecount )
+                               {
+                                       serverquerycount++;
+
+                                       memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
+                                       serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_QUAKEWORLD;
+                                       // 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].query = SQS_QUERYING;
+
+                                       ++serverlist_cachecount;
+                               }
+
+                               // move on to next address in packet
+                               data += 6;
+                               length -= 6;
+                       }
+                       // begin or resume serverlist queries
+                       serverlist_querysleep = false;
+                       serverlist_querywaittime = realtime + 3;
                        return true;
                }
                /*
@@ -1086,10 +1317,138 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                if (!strncmp(string, "ack", 3))
                        return true;
                */
+               // QuakeWorld compatibility
+               if (length >= 1 && string[0] == 'j' && cls.connect_trying)
+               {
+                       // accept message
+                       M_Update_Return_Reason("Accepted");
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD);
+                       return true;
+               }
+               if (length > 1 && string[0] == 'c' && string[1] >= '0' && string[1] <= '9' && cls.connect_trying)
+               {
+                       // challenge message
+                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+                       Con_Printf("challenge %s received, sending connect request back to %s\n", string + 1, addressstring2);
+                       M_Update_Return_Reason("Got challenge response");
+                       cls.qw_qport = qport.integer;
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ip", addressstring2);
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "name", cl_name.string);
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "topcolor", va("%i", (cl_color.integer >> 4) & 15));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "bottomcolor", va("%i", (cl_color.integer) & 15));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "rate", va("%i", cl_rate.integer));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "msg", "1");
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ver", engineversion);
+                       NetConn_WriteString(mysocket, va("\377\377\377\377connect %i %i %i \"%s\"\n", 28, cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress);
+                       return true;
+               }
+               if (length > 2 && !memcmp(string, "n\\", 2))
+               {
+                       serverlist_info_t *info;
+                       int n;
+                       double pingtime;
+
+                       // qw server status
+                       if (serverlist_consoleoutput && developer_networking.integer >= 2)
+                               Con_Printf("QW server status from server at %s:\n%s\n", addressstring2, string + 1);
+
+                       string += 1;
+                       // serverlist only uses text addresses
+                       LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
+                       // search the cache for this server and update it
+                       for( n = 0; n < serverlist_cachecount; n++ )
+                               if( !strcmp( cname, serverlist_cache[n].info.cname ) )
+                                       break;
+                       if( n == serverlist_cachecount ) {
+                               // LAN search doesnt require an answer from the master server so we wont
+                               // know the ping nor will it be initialized already...
+
+                               // find a slot
+                               if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+                                       return true;
+
+                               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, cname, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
+                               serverlist_cache[serverlist_cachecount].info.ping = 100000;
+                               //serverlist_cache[serverlist_cachecount].querytime = realtime;
+                               serverlist_cache[serverlist_cachecount].querytime = Sys_DoubleTime();
+                               // if not in the slist menu we should print the server to console
+                               if (serverlist_consoleoutput) {
+                                       Con_Printf("querying %s\n", ipstring);
+                               }
+
+                               ++serverlist_cachecount;
+                       }
+
+                       info = &serverlist_cache[n].info;
+                       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;
+                       info->protocol = 0;
+                       info->numplayers = 0; // updated below
+                       if ((s = SearchInfostring(string, "maxclients"   )) != NULL) info->maxplayers = atoi(s);else info->maxplayers  = 0;
+                       if ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);else info->gameversion = 0;
+
+                       // count active players on server
+                       // (we could gather more info, but we're just after the number)
+                       s = strchr(string, '\n');
+                       if (s)
+                       {
+                               s++;
+                               while (s < string + length)
+                               {
+                                       for (;s < string + length && *s != '\n';s++)
+                                               ;
+                                       if (s >= string + length)
+                                               break;
+                                       info->numplayers++;
+                                       s++;
+                               }
+                       }
+
+                       if (info->ping == 100000)
+                                       serverreplycount++;
+
+                       pingtime = (int)((Sys_DoubleTime() - serverlist_cache[n].querytime) * 1000.0 + 0.5);
+                       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), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", pingtime >= 300 ? '1' : (pingtime >= 200 ? '3' : '7'), (int)pingtime, ((info->numplayers > 0 && info->numplayers < info->maxplayers) ? (info->numplayers >= 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] );
+                       }
+                       // 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);
+                       // and finally, update the view set
+                       ServerList_ViewList_Insert( &serverlist_cache[n] );
+                       serverlist_cache[n].query = SQS_QUERIED;
+
+                       return true;
+               }
+               if (string[0] == 'n')
+               {
+                       // qw print command
+                       Con_Printf("QW print command from server at %s:\n%s\n", addressstring2, string + 1);
+               }
                // we may not have liked the packet, but it was a command packet, so
                // we're done processing this packet now
                return true;
        }
+       // quakeworld ingame packet
+       if (fromserver && cls.protocol == PROTOCOL_QUAKEWORLD && length >= 8 && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2)
+       {
+               ret = 0;
+               CL_ParseServerMessage();
+               return ret;
+       }
        // netquake control packets, supported for compatibility only
        if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
        {
@@ -1114,7 +1473,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                        LHNETADDRESS_SetPort(&clientportaddress, port);
                                }
                                M_Update_Return_Reason("Accepted");
-                               NetConn_ConnectionEstablished(mysocket, &clientportaddress);
+                               NetConn_ConnectionEstablished(mysocket, &clientportaddress, PROTOCOL_QUAKE);
                        }
                        break;
                case CCREP_REJECT:
@@ -1177,7 +1536,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                return true;
        }
        ret = 0;
-       if (length >= (int)NET_HEADERSIZE && cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress) && (ret = NetConn_ReceivedMessage(cls.netcon, data, length)) == 2)
+       if (fromserver && length >= (int)NET_HEADERSIZE && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2)
                CL_ParseServerMessage();
        return ret;
 }
@@ -1193,6 +1552,11 @@ void NetConn_QueryQueueFrame(void)
        if (serverlist_querysleep)
                return;
 
+       // apply a cool down time after master server replies,
+       // to avoid messing up the ping times on the servers
+       if (serverlist_querywaittime > realtime)
+               return;
+
        // each time querycounter reaches 1.0 issue a query
        querycounter += host_realframetime * net_slist_queriespersecond.value;
        maxqueries = (int)querycounter;
@@ -1227,11 +1591,19 @@ void NetConn_QueryQueueFrame(void)
                        int socket;
 
                        LHNETADDRESS_FromString(&address, entry->info.cname, 0);
-                       for (socket = 0; socket < cl_numsockets ; socket++) {
-                               NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
+                       if (entry->protocol == PROTOCOL_QUAKEWORLD)
+                       {
+                               for (socket = 0; socket < cl_numsockets ; socket++)
+                                       NetConn_WriteString(cl_sockets[socket], "\377\377\377\377status\n", &address);
+                       }
+                       else
+                       {
+                               for (socket = 0; socket < cl_numsockets ; socket++)
+                                       NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
                        }
 
-                       entry->querytime = realtime;
+                       //entry->querytime = realtime;
+                       entry->querytime = Sys_DoubleTime();
                        entry->querycounter++;
 
                        // if not in the slist menu we should print the server to console
@@ -1264,7 +1636,7 @@ void NetConn_ClientFrame(void)
                        M_Update_Return_Reason("Connect: Failed");
                        return;
                }
-               // try challenge first (newer server)
+               // try challenge first (newer DP server or QW)
                NetConn_WriteString(cls.connect_mysocket, "\377\377\377\377getchallenge", &cls.connect_address);
                // then try netquake as a fallback (old server, or netquake)
                SZ_Clear(&net_message);
@@ -1326,12 +1698,12 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
        // 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"
+                                               "\\gamename\\%s\\modname\\%s\\gameversion\\%d\\sv_maxclients\\%d"
+                                               "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d"
                                                "%s%s"
                                                "%s",
                                                fullstatus ? "statusResponse" : "infoResponse",
-                                               gamename, com_modname, svs.maxclients,
+                                               gamename, com_modname, gameversion.integer, svs.maxclients,
                                                nb_clients, sv.name, hostname.string, NET_PROTOCOL_VERSION,
                                                challenge ? "\\challenge\\" : "", challenge ? challenge : "",
                                                fullstatus ? "\n" : "");
@@ -1393,7 +1765,16 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
        double besttime;
        client_t *client;
        netconn_t *conn;
-       char *s, *string, response[512], addressstring2[128], stringbuf[16384];
+       char *s, *string, response[1400], addressstring2[128], stringbuf[16384];
+
+       // see if we can identify the sender as a local player
+       // (this is necessary for rcon to send a reliable reply if the client is
+       //  actually on the server, not sending remotely)
+       for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
+               if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
+                       break;
+       if (i == svs.maxclients)
+               host_client = NULL;
 
        if (sv.active)
        {
@@ -1500,7 +1881,9 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                                                                        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_VM_Begin();
                                                                                SV_ConnectClient(clientnum, conn);
+                                                                               SV_VM_End();
                                                                                NetConn_Heartbeat(1);
                                                                        }
                                                                }
@@ -1549,6 +1932,46 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                }
                                return true;
                        }
+                       if (length >= 5 && !memcmp(string, "rcon ", 5))
+                       {
+                               int i;
+                               char *s = string + 5;
+                               char password[64];
+                               for (i = 0;*s > ' ';s++)
+                                       if (i < (int)sizeof(password) - 1)
+                                               password[i++] = *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);
+                                       rcon_redirect = true;
+                                       rcon_redirect_bufferpos = 0;
+                                       Cmd_ExecuteString(s, src_command);
+                                       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);
+                                       }
+                               }
+                               return true;
+                       }
                        /*
                        if (!strncmp(string, "ping", 4))
                        {
@@ -1654,7 +2077,9 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                                                        NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
                                                                        SZ_Clear(&net_message);
                                                                        // now set up the client struct
+                                                                       SV_VM_Begin();
                                                                        SV_ConnectClient(clientnum, conn);
+                                                                       SV_VM_End();
                                                                        NetConn_Heartbeat(1);
                                                                }
                                                                else
@@ -1767,12 +2192,13 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        }
                }
 #endif
-               for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
+               if (host_client)
                {
-                       if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
+                       if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length, sv.protocol)) == 2)
                        {
-                               if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
-                                       SV_ReadClientMessage();
+                               SV_VM_Begin();
+                               SV_ReadClientMessage();
+                               SV_VM_End();
                                return ret;
                        }
                }
@@ -1794,12 +2220,14 @@ void NetConn_ServerFrame(void)
                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_VM_Begin();
                        SV_DropClient(false);
+                       SV_VM_End();
                }
        }
 }
 
-void NetConn_QueryMasters(void)
+void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
 {
        int i;
        int masternum;
@@ -1815,34 +2243,61 @@ void NetConn_QueryMasters(void)
        // 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 (querydp)
+       {
+               for (i = 0;i < cl_numsockets;i++)
+               {
+                       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);
+
+                               // search LAN for DarkPlaces servers
+                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377getinfo", &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);
+
+                               // search internet
+                               for (masternum = 0;sv_masters[masternum].name;masternum++)
+                               {
+                                       if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
+                                       {
+                                               masterquerycount++;
+                                               NetConn_WriteString(cl_sockets[i], request, &masteraddress);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       // only query QuakeWorld servers when the user wants to
+       if (queryqw)
        {
-               if (cl_sockets[i])
+               for (i = 0;i < cl_numsockets;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);
-
-                       // 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 (cl_sockets[i])
                        {
-                               if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == 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");
+
+                               // search internet
+                               for (masternum = 0;sv_qwmasters[masternum].name;masternum++)
                                {
-                                       masterquerycount++;
-                                       NetConn_WriteString(cl_sockets[i], request, &masteraddress);
+                                       if (sv_qwmasters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_qwmasters[masternum].string, QWMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
+                                       {
+                                               masterquerycount++;
+                                               NetConn_Write(cl_sockets[i], request, 7, &masteraddress);
+                                       }
                                }
                        }
                }
@@ -1880,7 +2335,7 @@ void NetConn_Heartbeat(int priority)
        {
                nextheartbeattime = realtime + sv_heartbeatperiod.value;
                for (masternum = 0;sv_masters[masternum].name;masternum++)
-                       if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && (mysocket = NetConn_ChooseServerSocketForAddress(&masteraddress)))
+                       if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && (mysocket = NetConn_ChooseServerSocketForAddress(&masteraddress)))
                                NetConn_WriteString(mysocket, "\377\377\377\377heartbeat DarkPlaces\x0A", &masteraddress);
        }
 }
@@ -1895,7 +2350,10 @@ static void Net_Heartbeat_f(void)
 
 void PrintStats(netconn_t *conn)
 {
-       Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence);
+       if ((cls.state == ca_connected && cls.protocol == PROTOCOL_QUAKEWORLD) || (sv.active && sv.protocol == PROTOCOL_QUAKEWORLD))
+               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->qw.outgoing_sequence, conn->qw.incoming_sequence);
+       else
+               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->nq.sendSequence, conn->nq.receiveSequence);
 }
 
 void Net_Stats_f(void)
@@ -1922,11 +2380,25 @@ void Net_Slist_f(void)
        serverlist_sortdescending = false;
     if (m_state != m_slist) {
                Con_Print("Sending requests to master servers\n");
-               ServerList_QueryList();
+               ServerList_QueryList(true, false);
+               serverlist_consoleoutput = true;
+               Con_Print("Listening for replies...\n");
+       } else
+               ServerList_QueryList(true, false);
+}
+
+void Net_SlistQW_f(void)
+{
+       ServerList_ResetMasks();
+       serverlist_sortbyfield = SLIF_PING;
+       serverlist_sortdescending = false;
+    if (m_state != m_slist) {
+               Con_Print("Sending requests to master servers\n");
+               ServerList_QueryList(false, true);
                serverlist_consoleoutput = true;
                Con_Print("Listening for replies...\n");
        } else
-               ServerList_QueryList();
+               ServerList_QueryList(false, true);
 }
 
 void NetConn_Init(void)
@@ -1935,7 +2407,8 @@ void NetConn_Init(void)
        lhnetaddress_t tempaddress;
        netconn_mempool = Mem_AllocPool("network connections", 0, NULL);
        Cmd_AddCommand("net_stats", Net_Stats_f, "print network statistics");
-       Cmd_AddCommand("net_slist", Net_Slist_f, "query master series and print all server information");
+       Cmd_AddCommand("net_slist", Net_Slist_f, "query dp master servers and print all server information");
+       Cmd_AddCommand("net_slistqw", Net_SlistQW_f, "query qw master servers and print all server information");
        Cmd_AddCommand("heartbeat", Net_Heartbeat_f, "send a heartbeat to the master server (updates your server information)");
        Cvar_RegisterVariable(&net_slist_queriespersecond);
        Cvar_RegisterVariable(&net_slist_queriesperframe);
@@ -1956,6 +2429,7 @@ void NetConn_Init(void)
        Cvar_RegisterVariable(&sv_heartbeatperiod);
        for (i = 0;sv_masters[i].name;i++)
                Cvar_RegisterVariable(&sv_masters[i]);
+       Cvar_RegisterVariable(&gameversion);
 // 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)
        {