]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
strncpy -> {strlcpy,memcpy}. Replaced the "forceloop" boolean in "channel_t" by a...
[xonotic/darkplaces.git] / netconn.c
index 08ab2a0cf241240e1b2753386fb9fe94e4bf501d..3226f763b34c529ae022a43aa35a641d44ea6773 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -34,8 +34,8 @@ static cvar_t sv_masters [] =
        {CVAR_SAVE, "sv_master2", ""},
        {CVAR_SAVE, "sv_master3", ""},
        {CVAR_SAVE, "sv_master4", ""},
-       {0, "sv_masterextra1", "198.88.152.4"},
-       {0, "sv_masterextra2", "68.102.242.12"},
+       {0, "sv_masterextra1", "69.59.212.88"},
+       {0, "sv_masterextra2", "66.28.32.64"},
        {0, NULL, NULL}
 };
 
@@ -69,11 +69,17 @@ static int unreliableMessagesReceived = 0;
 static int reliableMessagesSent = 0;
 static int reliableMessagesReceived = 0;
 
+double masterquerytime = -1000;
+int masterquerycount = 0;
+int masterreplycount = 0;
+int serverquerycount = 0;
+int serverreplycount = 0;
+
 int hostCacheCount = 0;
 hostcache_t hostcache[HOSTCACHESIZE];
 
-static qbyte sendbuffer[NET_MAXMESSAGE];
-static qbyte readbuffer[NET_MAXMESSAGE];
+static qbyte sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
+static qbyte readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
 
 int cl_numsockets;
 lhnetsocket_t *cl_sockets[16];
@@ -84,12 +90,9 @@ netconn_t *netconn_list = NULL;
 mempool_t *netconn_mempool = NULL;
 
 cvar_t cl_netport = {0, "cl_port", "0"};
-cvar_t cl_netaddress = {0, "cl_netaddress", "0.0.0.0"};
-cvar_t cl_netaddress_ipv6 = {0, "cl_netaddress_ipv6", "[0:0:0:0:0:0:0:0]:0"};
-
 cvar_t sv_netport = {0, "port", "26000"};
-cvar_t sv_netaddress = {0, "sv_netaddress", "0.0.0.0"};
-cvar_t sv_netaddress_ipv6 = {0, "sv_netaddress_ipv6", "[0:0:0:0:0:0:0:0]:26000"};
+cvar_t net_address = {0, "net_address", "0.0.0.0"};
+//cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]"};
 
 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
 {
@@ -160,8 +163,8 @@ int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
        if (data->cursize == 0)
                Sys_Error("Datagram_SendMessage: zero length message\n");
 
-       if (data->cursize > NET_MAXMESSAGE)
-               Sys_Error("Datagram_SendMessage: message too big %u\n", data->cursize);
+       if (data->cursize > (int)sizeof(conn->sendMessage))
+               Sys_Error("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
 
        if (conn->canSend == false)
                Sys_Error("SendMessage: called with canSend == false\n");
@@ -186,7 +189,7 @@ int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
        header = (void *)sendbuffer;
        header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
        header[1] = BigLong(conn->sendSequence);
-       memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
        conn->sendSequence++;
        conn->canSend = false;
@@ -225,7 +228,7 @@ static void NetConn_SendMessageNext(netconn_t *conn)
                header = (void *)sendbuffer;
                header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
                header[1] = BigLong(conn->sendSequence);
-               memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
                conn->sendSequence++;
                conn->sendNext = false;
@@ -263,7 +266,7 @@ static void NetConn_ReSendMessage(netconn_t *conn)
                header = (void *)sendbuffer;
                header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
                header[1] = BigLong(conn->sendSequence - 1);
-               memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
+               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
                conn->sendNext = false;
 
@@ -285,20 +288,20 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
        int packetLen;
        int *header;
 
-#ifdef DEBUG
+       packetLen = NET_HEADERSIZE + data->cursize;
+
+//#ifdef DEBUG
        if (data->cursize == 0)
                Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
 
-       if (data->cursize > MAX_DATAGRAM)
+       if (packetLen > (int)sizeof(sendbuffer))
                Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
-#endif
-
-       packetLen = NET_HEADERSIZE + data->cursize;
+//#endif
 
        header = (void *)sendbuffer;
        header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
        header[1] = BigLong(conn->unreliableSendSequence);
-       memcpy(sendbuffer + 8, data->data, data->cursize);
+       memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
 
        conn->unreliableSendSequence++;
 
@@ -348,9 +351,9 @@ void NetConn_OpenClientPorts(void)
        if (cl_netport.integer != port)
                Cvar_SetValueQuick(&cl_netport, port);
        Con_Printf("Client using port %i\n", port);
-       NetConn_OpenClientPort("local", port);
-       NetConn_OpenClientPort(cl_netaddress.string, port);
-       NetConn_OpenClientPort(cl_netaddress_ipv6.string, port);
+       NetConn_OpenClientPort("local:2", 0);
+       NetConn_OpenClientPort(net_address.string, port);
+       //NetConn_OpenClientPort(net_address_ipv6.string, port);
 }
 
 void NetConn_CloseServerPorts(void)
@@ -394,11 +397,11 @@ void NetConn_OpenServerPorts(int opennetports)
        if (sv_netport.integer != port)
                Cvar_SetValueQuick(&sv_netport, port);
        if (cls.state != ca_dedicated)
-               NetConn_OpenServerPort("local", port);
+               NetConn_OpenServerPort("local:1", 0);
        if (opennetports)
        {
-               NetConn_OpenServerPort(sv_netaddress.string, port);
-               NetConn_OpenServerPort(sv_netaddress_ipv6.string, port);
+               NetConn_OpenServerPort(net_address.string, port);
+               //NetConn_OpenServerPort(net_address_ipv6.string, port);
        }
        if (sv_numsockets == 0)
                Host_Error("NetConn_OpenServerPorts: unable to open any ports!\n");
@@ -428,6 +431,11 @@ netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
        conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
        conn->mysocket = mysocket;
        conn->peeraddress = *peeraddress;
+       // updated by receiving "rate" command from client
+       conn->rate = NET_MINRATE;
+       // no limits for local player
+       if (LHNETADDRESS_GetAddressType(peeraddress) == LHNETADDRESSTYPE_LOOP)
+               conn->rate = 1000000000;
        conn->canSend = true;
        conn->connecttime = realtime;
        conn->lastMessageTime = realtime;
@@ -469,17 +477,17 @@ static int clientport2 = -1;
 static int hostport = -1;
 static void NetConn_UpdateServerStuff(void)
 {
-       if (clientport2 != cl_netport.integer)
-       {
-               clientport2 = cl_netport.integer;
-               if (cls.state == ca_connected)
-                       Con_Printf("Changing \"cl_port\" will not take effect until you reconnect.\n");
-       }
        if (cls.state != ca_dedicated)
        {
-               if (cls.state == ca_disconnected && clientport != cl_netport.integer)
+               if (clientport2 != cl_netport.integer)
+               {
+                       clientport2 = cl_netport.integer;
+                       if (cls.state == ca_connected)
+                               Con_Print("Changing \"cl_port\" will not take effect until you reconnect.\n");
+               }
+               if (cls.state == ca_disconnected && clientport != clientport2)
                {
-                       clientport = cl_netport.integer;
+                       clientport = clientport2;
                        NetConn_CloseClientPorts();
                }
                if (cl_numsockets == 0)
@@ -490,7 +498,7 @@ static void NetConn_UpdateServerStuff(void)
        {
                hostport = sv_netport.integer;
                if (sv.active)
-                       Con_Printf("Changing \"port\" will not take effect until \"map\" command is executed.\n");
+                       Con_Print("Changing \"port\" will not take effect until \"map\" command is executed.\n");
        }
 }
 
@@ -535,7 +543,7 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                        }
                                }
                                else
-                                       Con_DPrintf("Got a stale datagram\n");
+                                       Con_DPrint("Got a stale datagram\n");
                                return 1;
                        }
                        else if (flags & NETFLAG_ACK)
@@ -546,7 +554,7 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                        {
                                                conn->ackSequence++;
                                                if (conn->ackSequence != conn->sendSequence)
-                                                       Con_DPrintf("ack sequencing error\n");
+                                                       Con_DPrint("ack sequencing error\n");
                                                conn->lastMessageTime = realtime;
                                                conn->timeout = realtime + net_messagetimeout.value;
                                                conn->sendMessageLength -= MAX_PACKETFRAGMENT;
@@ -563,10 +571,10 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                                }
                                        }
                                        else
-                                               Con_DPrintf("Duplicate ACK received\n");
+                                               Con_DPrint("Duplicate ACK received\n");
                                }
                                else
-                                       Con_DPrintf("Stale ACK received\n");
+                                       Con_DPrint("Stale ACK received\n");
                                return 1;
                        }
                        else if (flags & NETFLAG_DATA)
@@ -621,15 +629,8 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
 
 int NetConn_IsLocalGame(void)
 {
-       int i;
-       if (cls.state == ca_connected && sv.active/* && LHNETADDRESS_GetAddressType(cl.netcon->peeraddress) == LHNETADDRESSTYPE_LOOP*/)
-       {
-               // make sure there are no other connected clients
-               for (i = 1;i < MAX_SCOREBOARD;i++)
-                       if (svs.connectedclients[i])
-                               return false;
+       if (cls.state == ca_connected && sv.active && cl.maxclients == 1)
                return true;
-       }
        return false;
 }
 
@@ -697,110 +698,129 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        string += 13;
                        // hostcache only uses text addresses
                        LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
-                       // search the cache for this server
-                       for (n = 0; n < hostCacheCount; n++)
-                               if (!strcmp(cname, hostcache[n].cname))
-                                       break;
-                       // add it or update it
-                       if (n == hostCacheCount)
-                       {
-                               // if cache is full replace highest ping server (the list is
-                               // kept sorted so this is always the last, and if this server
-                               // is good it will be sorted into an early part of the list)
-                               if (hostCacheCount >= HOSTCACHESIZE)
-                                       n = hostCacheCount - 1;
-                               else
-                                       hostCacheCount++;
-                       }
-                       if ((s = SearchInfostring(string, "gamename"     )) != NULL) strncpy(game, s, sizeof(game) - 1);else game[0] = 0;
-                       if ((s = SearchInfostring(string, "modname"      )) != NULL) strncpy(mod , s, sizeof(mod ) - 1);else mod[0] = 0;
-                       if ((s = SearchInfostring(string, "mapname"      )) != NULL) strncpy(map , s, sizeof(map ) - 1);else map[0] = 0;
-                       if ((s = SearchInfostring(string, "hostname"     )) != NULL) strncpy(name, s, sizeof(name) - 1);else name[0] = 0;
+                       if ((s = SearchInfostring(string, "gamename"     )) != NULL) strlcpy(game, s, sizeof (game));else game[0] = 0;
+                       if ((s = SearchInfostring(string, "modname"      )) != NULL) strlcpy(mod , s, sizeof (mod ));else mod[0]  = 0;
+                       if ((s = SearchInfostring(string, "mapname"      )) != NULL) strlcpy(map , s, sizeof (map ));else map[0]  = 0;
+                       if ((s = SearchInfostring(string, "hostname"     )) != NULL) strlcpy(name, s, sizeof (name));else name[0] = 0;
                        if ((s = SearchInfostring(string, "protocol"     )) != NULL) c = atoi(s);else c = -1;
                        if ((s = SearchInfostring(string, "clients"      )) != NULL) users = atoi(s);else users = 0;
                        if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) maxusers = atoi(s);else maxusers = 0;
-                       pingtime = 9999;
-                       for (i = 0;i < HOSTCACHESIZE;i++)
-                               if (!LHNETADDRESS_Compare(peeraddress, &pingcache[i].peeraddress))
-                                       pingtime = (int)(realtime - pingcache[i].senttime);
-                       pingtime = bound(0, pingtime, 9999);
-                       memset(&hostcache[n], 0, sizeof(hostcache[n]));
-                       // store the data the engine cares about (address and ping)
-                       strcpy(hostcache[n].cname, cname);
-                       hostcache[n].ping = pingtime;
-                       // build description strings for the things users care about
-                       snprintf(hostcache[n].line1, sizeof(hostcache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, c != NET_PROTOCOL_VERSION ? '*' : ' ', users, maxusers, name);
-                       snprintf(hostcache[n].line2, sizeof(hostcache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", cname, game, mod, map);
-                       // if ping is especially high, display it as such
-                       if (pingtime >= 300)
-                       {
-                               // orange numbers (lower block)
-                               for (i = 0;i < 5;i++)
-                                       if (hostcache[n].line1[i] != ' ')
-                                               hostcache[n].line1[i] += 128;
-                       }
-                       else if (pingtime >= 200)
-                       {
-                               // yellow numbers (in upper block)
-                               for (i = 0;i < 5;i++)
-                                       if (hostcache[n].line1[i] != ' ')
-                                               hostcache[n].line1[i] -= 30;
-                       }
-                       // if not in the slist menu we should print the server to console
-                       if (m_state != m_slist)
-                               Con_Printf("%s\n%s\n", hostcache[n].line1, hostcache[n].line2);
-                       // and finally, re-sort the list
-                       for (i = 0;i < hostCacheCount;i++)
+                       // search the cache for this server and update it
+                       for (n = 0; n < hostCacheCount; n++)
                        {
-                               for (j = i + 1;j < hostCacheCount;j++)
+                               if (!strcmp(cname, hostcache[n].cname))
                                {
-                                       //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
-                                       if (hostcache[i].ping > hostcache[j].ping)
+                                       if (hostcache[n].ping == 100000)
+                                               serverreplycount++;
+                                       pingtime = (int)((realtime - hostcache[n].querytime) * 1000.0);
+                                       pingtime = bound(0, pingtime, 9999);
+                                       // update the ping
+                                       hostcache[n].ping = pingtime;
+                                       // build description strings for the things users care about
+                                       snprintf(hostcache[n].line1, sizeof(hostcache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, c != NET_PROTOCOL_VERSION ? '*' : ' ', users, maxusers, name);
+                                       snprintf(hostcache[n].line2, sizeof(hostcache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", cname, game, mod, map);
+                                       // if ping is especially high, display it as such
+                                       if (pingtime >= 300)
                                        {
-                                               memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
-                                               memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
-                                               memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
+                                               // orange numbers (lower block)
+                                               for (i = 0;i < 5;i++)
+                                                       if (hostcache[n].line1[i] != ' ')
+                                                               hostcache[n].line1[i] += 128;
+                                       }
+                                       else if (pingtime >= 200)
+                                       {
+                                               // yellow numbers (in upper block)
+                                               for (i = 0;i < 5;i++)
+                                                       if (hostcache[n].line1[i] != ' ')
+                                                               hostcache[n].line1[i] -= 30;
+                                       }
+                                       // if not in the slist menu we should print the server to console
+                                       if (m_state != m_slist)
+                                               Con_Printf("%s\n%s\n", hostcache[n].line1, hostcache[n].line2);
+                                       // and finally, re-sort the list
+                                       for (i = 0;i < hostCacheCount;i++)
+                                       {
+                                               for (j = i + 1;j < hostCacheCount;j++)
+                                               {
+                                                       //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
+                                                       if (hostcache[i].ping > hostcache[j].ping)
+                                                       {
+                                                               memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
+                                                               memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
+                                                               memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
+                                                       }
+                                               }
                                        }
+                                       break;
                                }
                        }
                        return true;
                }
                if (!strncmp(string, "getserversResponse\\", 19) && hostCacheCount < HOSTCACHESIZE)
                {
-                       int i, best;
-                       double besttime;
+                       int i, n, j;
+                       hostcache_t temp;
                        // Extract the IP addresses
                        data += 18;
                        length -= 18;
+                       masterreplycount++;
+                       if (m_state != m_slist)
+                               Con_Print("received 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)
                        {
-                               sprintf(ipstring, "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
+                               snprintf (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);
                                LHNETADDRESS_FromString(&svaddress, ipstring, 0);
                                NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
-                               // replace oldest or matching entry in ping cache
-                               // we scan this later when getting a reply to see how long it took
-                               besttime = realtime;
-                               best = 0;
-                               for (i = 0;i < HOSTCACHESIZE;i++)
+
+
+                               // add to slist (hostCache)
+                               // search the cache for this server
+                               for (n = 0; n < hostCacheCount; n++)
+                                       if (!strcmp(ipstring, hostcache[n].cname))
+                                               break;
+                               // add it or update it
+                               if (n == hostCacheCount)
                                {
-                                       if (!LHNETADDRESS_Compare(&svaddress, &pingcache[i].peeraddress))
+                                       // if cache is full replace highest ping server (the list is
+                                       // kept sorted so this is always the last, and if this server
+                                       // is good it will be sorted into an early part of the list)
+                                       if (hostCacheCount >= HOSTCACHESIZE)
+                                               n = hostCacheCount - 1;
+                                       else
                                        {
-                                               best = i;
-                                               break;
+                                               serverquerycount++;
+                                               hostCacheCount++;
                                        }
-                                       if (besttime > pingcache[i].senttime)
+                               }
+                               memset(&hostcache[n], 0, sizeof(hostcache[n]));
+                               // store the data the engine cares about (address and ping)
+                               strlcpy (hostcache[n].cname, ipstring, sizeof (hostcache[n].cname));
+                               hostcache[n].ping = 100000;
+                               hostcache[n].querytime = realtime;
+                               // build description strings for the things users care about
+                               strlcpy (hostcache[n].line1, "?", sizeof (hostcache[n].line1));
+                               strlcpy (hostcache[n].line2, ipstring, sizeof (hostcache[n].line2));
+                               // if not in the slist menu we should print the server to console
+                               if (m_state != m_slist)
+                                       Con_Printf("querying %s\n", ipstring);
+                               // and finally, re-sort the list
+                               for (i = 0;i < hostCacheCount;i++)
+                               {
+                                       for (j = i + 1;j < hostCacheCount;j++)
                                        {
-                                               besttime = pingcache[i].senttime;
-                                               best = i;
-                                               // if ping cache isn't full yet we can skip out early
-                                               if (!besttime)
-                                                       break;
+                                               //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
+                                               if (hostcache[i].ping > hostcache[j].ping)
+                                               {
+                                                       memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
+                                                       memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
+                                                       memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
+                                               }
                                        }
                                }
-                               pingcache[best].peeraddress = svaddress;
-                               pingcache[best].senttime = realtime;
+
+
                                // move on to next address in packet
                                data += 7;
                                length -= 7;
@@ -852,7 +872,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        if (developer.integer)
                                Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
                        Con_Printf("%s\n", data);
-                       strncpy(m_return_reason, data, sizeof(m_return_reason) - 1);
+                       strlcpy (m_return_reason, data, sizeof (m_return_reason));
                        break;
 #if 0
                case CCREP_SERVER_INFO:
@@ -874,20 +894,18 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                {
                                        hostCacheCount++;
                                        memset(&hostcache[n], 0, sizeof(hostcache[n]));
-                                       strcpy(hostcache[n].name, MSG_ReadString());
-                                       strcpy(hostcache[n].map, MSG_ReadString());
+                                       strlcpy (hostcache[n].name, MSG_ReadString(), sizeof (hostcache[n].name));
+                                       strlcpy (hostcache[n].map, MSG_ReadString(), sizeof (hostcache[n].map));
                                        hostcache[n].users = MSG_ReadByte();
                                        hostcache[n].maxusers = MSG_ReadByte();
                                        c = MSG_ReadByte();
                                        if (c != NET_PROTOCOL_VERSION)
                                        {
-                                               strncpy(hostcache[n].cname, hostcache[n].name, sizeof(hostcache[n].cname) - 1);
-                                               hostcache[n].cname[sizeof(hostcache[n].cname) - 1] = 0;
+                                               strlcpy (hostcache[n].cname, hostcache[n].name, sizeof (hostcache[n].cname));
                                                strcpy(hostcache[n].name, "*");
-                                               strncat(hostcache[n].name, hostcache[n].cname, sizeof(hostcache[n].name) - 1);
-                                               hostcache[n].name[sizeof(hostcache[n].name) - 1] = 0;
+                                               strlcat (hostcache[n].name, hostcache[n].cname, sizeof(hostcache[n].name));
                                        }
-                                       strcpy(hostcache[n].cname, cname);
+                                       strlcpy (hostcache[n].cname, cname, sizeof (hostcache[n].cname));
                                }
                        }
                        break;
@@ -926,13 +944,26 @@ void NetConn_ClientFrame(void)
                if (cls.connect_remainingtries == 0)
                {
                        cls.connect_trying = false;
-                       Host_Error("Connect failed\n");
+                       if (m_state == m_slist)
+                               strcpy (m_return_reason, "Connect: Failed");
+                       else
+                               Con_Print("Connect failed\n");
                        return;
                }
                if (cls.connect_nextsendtime)
-                       Con_Printf("Still trying...\n");
+               {
+                       if (m_state == m_slist)
+                               strcpy (m_return_reason, "Connect: Still trying");
+                       else
+                               Con_Print("Still trying...\n");
+               }
                else
-                       Con_Printf("Trying...\n");
+               {
+                       if (m_state == m_slist)
+                               strcpy (m_return_reason, "Connect: Trying");
+                       else
+                               Con_Print("Trying...\n");
+               }
                cls.connect_nextsendtime = realtime + 1;
                cls.connect_remainingtries--;
                // try challenge first (newer server)
@@ -953,7 +984,7 @@ void NetConn_ClientFrame(void)
                        NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
        if (cls.netcon && realtime > cls.netcon->timeout)
        {
-               Con_Printf("Connection timed out\n");
+               Con_Print("Connection timed out\n");
                CL_Disconnect();
        }
        for (conn = netconn_list;conn;conn = conn->next)
@@ -987,7 +1018,7 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
 extern void SV_ConnectClient(int clientnum, netconn_t *netconnection);
 int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
 {
-       int i, n, ret, clientnum, responselength, best, clientcount;
+       int i, n, ret, clientnum, responselength, best;
        double besttime;
        client_t *client;
        netconn_t *conn;
@@ -1057,10 +1088,10 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                else
                                                {
                                                        // see if this is a duplicate connection request
-                                                       for (clientnum = 0;clientnum < MAX_SCOREBOARD;clientnum++)
-                                                               if ((client = svs.connectedclients[clientnum]) && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
+                                                       for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
+                                                               if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
                                                                        break;
-                                                       if (clientnum < MAX_SCOREBOARD)
+                                                       if (clientnum < svs.maxclients)
                                                        {
                                                                // duplicate connection request
                                                                if (realtime - client->netconnection->connecttime < 2.0)
@@ -1081,39 +1112,27 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                        else
                                                        {
                                                                // this is a new client, find a slot
-                                                               for (clientcount = 0, clientnum = 0;clientnum < MAX_SCOREBOARD;clientnum++)
-                                                                       if (svs.connectedclients[clientnum])
-                                                                               clientcount++;
-                                                               for (clientnum = 0;clientnum < MAX_SCOREBOARD;clientnum++)
-                                                                       if (!svs.connectedclients[clientnum])
+                                                               for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
+                                                                       if (!client->active)
                                                                                break;
-                                                               if (clientcount < max(1, sv_maxplayers.integer) && clientnum < MAX_SCOREBOARD)
+                                                               if (clientnum < svs.maxclients)
                                                                {
-                                                                       // allocate and prepare the client struct
-                                                                       if ((client = Mem_Alloc(sv_clients_mempool, sizeof(client_t))))
+                                                                       // prepare the client struct
+                                                                       if ((client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool)))
                                                                        {
-                                                                               if ((client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool)))
+                                                                               if ((conn = NetConn_Open(mysocket, peeraddress)))
                                                                                {
-                                                                                       if ((conn = NetConn_Open(mysocket, peeraddress)))
-                                                                                       {
-                                                                                               // allocated connection
-                                                                                               LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
-                                                                                               if (developer.integer)
-                                                                                                       Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
-                                                                                               NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
-                                                                                               // now set up the client struct
-                                                                                               svs.connectedclients[clientnum] = client;
-                                                                                               SV_ConnectClient(clientnum, conn);
-                                                                                               NetConn_Heartbeat(1);
-                                                                                       }
-                                                                                       else
-                                                                                       {
-                                                                                               EntityFrame4_FreeDatabase(client->entitydatabase4);
-                                                                                               Mem_Free(client);
-                                                                                       }
+                                                                                       // allocated connection
+                                                                                       LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
+                                                                                       if (developer.integer)
+                                                                                               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_ConnectClient(clientnum, conn);
+                                                                                       NetConn_Heartbeat(1);
                                                                                }
                                                                                else
-                                                                                       Mem_Free(client);
+                                                                                       EntityFrame4_FreeDatabase(client->entitydatabase4);
                                                                        }
                                                                }
                                                                else
@@ -1135,13 +1154,13 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                // If there was a challenge in the getinfo message
                                if (length > 8 && string[7] == ' ')
                                        challenge = string + 8;
-                               for (i = 0, n = 0;i < MAX_SCOREBOARD;i++)
-                                       if (svs.connectedclients[i])
+                               for (i = 0, n = 0;i < svs.maxclients;i++)
+                                       if (svs.clients[i].active)
                                                n++;
                                responselength = snprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
                                                        "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
                                                        "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d%s%s",
-                                                       gamename, com_modname, min(sv_maxplayers.integer, MAX_SCOREBOARD), n,
+                                                       gamename, com_modname, svs.maxclients, n,
                                                        sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "");
                                // does it fit in the buffer?
                                if (responselength < (int)sizeof(response))
@@ -1201,10 +1220,10 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                else
                                                {
                                                        // see if this is a duplicate connection request
-                                                       for (clientnum = 0;clientnum < MAX_SCOREBOARD;clientnum++)
-                                                               if ((client = svs.connectedclients[clientnum]) && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
+                                                       for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
+                                                               if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
                                                                        break;
-                                                       if (clientnum < MAX_SCOREBOARD)
+                                                       if (clientnum < svs.maxclients)
                                                        {
                                                                // duplicate connection request
                                                                if (realtime - client->netconnection->connecttime < 2.0)
@@ -1233,15 +1252,14 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                        else
                                                        {
                                                                // this is a new client, find a slot
-                                                               for (clientnum = 0;clientnum < MAX_SCOREBOARD;clientnum++)
-                                                                       if (!(client = svs.connectedclients[clientnum]))
+                                                               for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
+                                                                       if (!client->active)
                                                                                break;
-                                                               // WARNING: this is broken code
-                                                               if (clientnum < MAX_SCOREBOARD && (client->netconnection = conn = NetConn_Open(mysocket, peeraddress)) != NULL)
+                                                               if (clientnum < svs.maxclients && (client->netconnection = conn = NetConn_Open(mysocket, peeraddress)) != NULL)
                                                                {
                                                                        // connect to the client
                                                                        // everything is allocated, just fill in the details
-                                                                       strcpy(conn->address, addressstring2);
+                                                                       strlcpy (conn->address, addressstring2, sizeof (conn->address));
                                                                        if (developer.integer)
                                                                                Con_Printf("Datagram_ParseConnectionless: sending CCREP_ACCEPT to %s.\n", addressstring2);
                                                                        // send back the info about the server connection
@@ -1292,7 +1310,7 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                MSG_WriteString(&net_message, hostname.string);
                                                MSG_WriteString(&net_message, sv.name);
                                                MSG_WriteByte(&net_message, net_activeconnections);
-                                               MSG_WriteByte(&net_message, min(sv_maxplayers.integer, MAX_SCOREBOARD));
+                                               MSG_WriteByte(&net_message, svs.maxclients);
                                                MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
                                                *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
                                                NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
@@ -1367,25 +1385,14 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        }
                }
 #endif
-               for (i = 0;i < MAX_SCOREBOARD;i++)
+               for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
                {
-                       if ((host_client = svs.connectedclients[i]))
+                       if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
                        {
-                               if (host_client->netconnection)
-                               {
-                                       if (host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
-                                       {
-                                               sv_player = host_client->edict;
-                                               if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
-                                                       SV_ReadClientMessage();
-                                               return ret;
-                                       }
-                               }
-                               else
-                               {
-                                       Con_Printf("Removing client with no netconnection!\n");
-                                       SV_DropClient(true);
-                               }
+                               sv_player = host_client->edict;
+                               if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
+                                       SV_ReadClientMessage();
+                               return ret;
                        }
                }
        }
@@ -1401,9 +1408,10 @@ void NetConn_ServerFrame(void)
        for (i = 0;i < sv_numsockets;i++)
                while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
                        NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress);
-       for (i = 0;i < MAX_SCOREBOARD;i++)
+       for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
        {
-               if ((host_client = svs.connectedclients[i]) && realtime > host_client->netconnection->timeout)
+               // never timeout loopback connections
+               if (host_client->netconnection && realtime > host_client->netconnection->timeout && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(host_client->netconnection->mysocket)) != LHNETADDRESSTYPE_LOOP)
                {
                        Con_Printf("Client \"%s\" connection timed out\n", host_client->name);
                        sv_player = host_client->edict;
@@ -1450,10 +1458,20 @@ void NetConn_QueryMasters(void)
 
                        // search internet
                        for (masternum = 0;sv_masters[masternum].name;masternum++)
+                       {
                                if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
+                               {
+                                       masterquerycount++;
                                        NetConn_WriteString(cl_sockets[i], request, &masteraddress);
+                               }
+                       }
                }
        }
+       if (!masterquerycount)
+       {
+               Con_Print("Unable to query master servers, no suitable network sockets active.\n");
+               strcpy(m_return_reason, "No network");
+       }
 }
 
 void NetConn_Heartbeat(int priority)
@@ -1478,7 +1496,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 && (!cl.islocalgame || sv_maxplayers.integer >= 2) && (priority > 1 || realtime > nextheartbeattime))
+       if (sv.active && sv_public.integer && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
        {
                nextheartbeattime = realtime + sv_heartbeatperiod.value;
                for (masternum = 0;sv_masters[masternum].name;masternum++)
@@ -1505,9 +1523,9 @@ int NetConn_SendToAll(sizebuf_t *data, double blocktime)
                count = 0;
                NetConn_ClientFrame();
                NetConn_ServerFrame();
-               for (i = 0;i < MAX_SCOREBOARD;i++)
+               for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
                {
-                       if ((host_client = svs.connectedclients[i]))
+                       if (host_client->netconnection)
                        {
                                if (NetConn_CanSendMessage(host_client->netconnection))
                                {
@@ -1526,7 +1544,10 @@ int NetConn_SendToAll(sizebuf_t *data, double blocktime)
 
 static void Net_Heartbeat_f(void)
 {
-       NetConn_Heartbeat(2);
+       if (sv.active)
+               NetConn_Heartbeat(2);
+       else
+               Con_Print("No server running, can not heartbeat to master server.\n");
 }
 
 void PrintStats(netconn_t *conn)
@@ -1546,20 +1567,25 @@ void Net_Stats_f(void)
        Con_Printf("packetsReceived            = %i\n", packetsReceived);
        Con_Printf("receivedDuplicateCount     = %i\n", receivedDuplicateCount);
        Con_Printf("droppedDatagrams           = %i\n", droppedDatagrams);
-       Con_Printf("connections                =\n");
+       Con_Print("connections                =\n");
        for (conn = netconn_list;conn;conn = conn->next)
                PrintStats(conn);
 }
 
 void Net_Slist_f(void)
 {
+       masterquerytime = realtime;
+       masterquerycount = 0;
+       masterreplycount = 0;
+       serverquerycount = 0;
+       serverreplycount = 0;
        hostCacheCount = 0;
        memset(&pingcache, 0, sizeof(pingcache));
        if (m_state != m_slist)
-               Con_Printf("Sending requests to master servers\n");
+               Con_Print("Sending requests to master servers\n");
        NetConn_QueryMasters();
        if (m_state != m_slist)
-               Con_Printf("Listening for replies...\n");
+               Con_Print("Listening for replies...\n");
 }
 
 void NetConn_Init(void)
@@ -1582,11 +1608,9 @@ void NetConn_Init(void)
        Cvar_RegisterVariable(&hostname);
        Cvar_RegisterVariable(&developer_networking);
        Cvar_RegisterVariable(&cl_netport);
-       Cvar_RegisterVariable(&cl_netaddress);
-       Cvar_RegisterVariable(&cl_netaddress_ipv6);
        Cvar_RegisterVariable(&sv_netport);
-       Cvar_RegisterVariable(&sv_netaddress);
-       Cvar_RegisterVariable(&sv_netaddress_ipv6);
+       Cvar_RegisterVariable(&net_address);
+       //Cvar_RegisterVariable(&net_address_ipv6);
        Cvar_RegisterVariable(&sv_public);
        Cvar_RegisterVariable(&sv_heartbeatperiod);
        for (i = 0;sv_masters[i].name;i++)
@@ -1595,9 +1619,8 @@ void NetConn_Init(void)
        {
                if (LHNETADDRESS_FromString(&tempaddress, com_argv[i + 1], 0) == 1)
                {
-                       Con_Printf("-ip option used, setting cl_netaddress and sv_netaddress to address \"%s\"\n");
-                       Cvar_SetQuick(&cl_netaddress, com_argv[i + 1]);
-                       Cvar_SetQuick(&sv_netaddress, com_argv[i + 1]);
+                       Con_Printf("-ip option used, setting net_address to \"%s\"\n");
+                       Cvar_SetQuick(&net_address, com_argv[i + 1]);
                }
                else
                        Con_Printf("-ip option used, but unable to parse the address \"%s\"\n", com_argv[i + 1]);