2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2002 Mathieu Olivier
4 Copyright (C) 2003 Forest Hale
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #define MASTER_PORT 27950
28 cvar_t sv_public = {0, "sv_public", "0"};
29 static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "180"};
31 // FIXME: resolve DNS on masters whenever their value changes and cache it (to avoid major delays in active servers when they heartbeat)
32 static cvar_t sv_masters [] =
34 {CVAR_SAVE, "sv_master1", ""},
35 {CVAR_SAVE, "sv_master2", ""},
36 {CVAR_SAVE, "sv_master3", ""},
37 {CVAR_SAVE, "sv_master4", ""},
38 {0, "sv_masterextra1", "69.59.212.88"}, // ghdigital.com
39 {0, "sv_masterextra2", "66.169.205.13"}, // dpmaster.deathmask.net
43 static double nextheartbeattime = 0;
45 sizebuf_t net_message;
47 cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
48 cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10"};
49 cvar_t net_connecttimeout = {0, "net_connecttimeout","10"};
50 cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
51 cvar_t developer_networking = {0, "developer_networking", "0"};
53 cvar_t cl_netlocalping = {0, "cl_netlocalping","0"};
54 static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0"};
57 /* statistic counters */
58 static int packetsSent = 0;
59 static int packetsReSent = 0;
60 static int packetsReceived = 0;
61 static int receivedDuplicateCount = 0;
62 static int droppedDatagrams = 0;
64 static int unreliableMessagesSent = 0;
65 static int unreliableMessagesReceived = 0;
66 static int reliableMessagesSent = 0;
67 static int reliableMessagesReceived = 0;
69 double masterquerytime = -1000;
70 int masterquerycount = 0;
71 int masterreplycount = 0;
72 int serverquerycount = 0;
73 int serverreplycount = 0;
75 static qbyte sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
76 static qbyte readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
79 lhnetsocket_t *cl_sockets[16];
81 lhnetsocket_t *sv_sockets[16];
83 netconn_t *netconn_list = NULL;
84 mempool_t *netconn_mempool = NULL;
86 cvar_t cl_netport = {0, "cl_port", "0"};
87 cvar_t sv_netport = {0, "port", "26000"};
88 cvar_t net_address = {0, "net_address", "0.0.0.0"};
89 //cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]"};
91 // HostCache interface
92 hostcache_mask_t hostcache_currentmask;
93 hostcache_infofield_t hostcache_sortbyfield;
94 qboolean hostcache_sortdescending;
96 int hostcache_viewcount = 0;
97 hostcache_t *hostcache_viewset[HOSTCACHE_VIEWCACHESIZE];
99 int hostcache_cachecount;
100 hostcache_t hostcache_cache[HOSTCACHE_TOTALSIZE];
102 qboolean hostcache_consoleoutput;
105 static void _HostCache_PingTest(void)
108 for( i = 0 ; i < 50 ; i++ ) {
109 memset( &hostcache_cache[hostcache_cachecount], 0, sizeof( hostcache_t ) );
110 hostcache_cache[hostcache_cachecount].info.ping = rand() % 450;
111 hostcache_cache[hostcache_cachecount].finished = true;
112 sprintf( hostcache_cache[hostcache_cachecount].line1, "%i", hostcache_cache[hostcache_cachecount].info.ping );
113 _HostCache_Insert( &hostcache_cache[hostcache_cachecount] );
114 hostcache_cachecount++;
119 // helper function to insert a value into the viewset
120 // spare entries will be removed
121 static void _HostCache_ViewSet_InsertBefore( int index, hostcache_t *entry )
124 if( ++hostcache_viewcount > HOSTCACHE_VIEWCACHESIZE )
125 hostcache_viewcount = HOSTCACHE_VIEWCACHESIZE;
126 for( i = hostcache_viewcount - 1; i > index; i-- )
127 hostcache_viewset[i] = hostcache_viewset[i - 1];
128 hostcache_viewset[index] = entry;
131 // we suppose hostcache_viewcount to be valid, ie > 0
132 static void _HostCache_ViewSet_Remove( int index )
134 for( --hostcache_viewcount; index < hostcache_viewcount; index++ )
135 hostcache_viewset[index] = hostcache_viewset[index + 1];
138 // returns true if A should be inserted before B
139 static qboolean _HostCache_SortTest( hostcache_t *A, hostcache_t *B )
141 int result; // > 0 if for numbers A > B and for text if A < B
143 if( hostcache_sortbyfield == HCIF_PING )
144 result = A->info.ping - B->info.ping;
145 else if( hostcache_sortbyfield == HCIF_MAXPLAYERS )
146 result = A->info.maxplayers - B->info.maxplayers;
147 else if( hostcache_sortbyfield == HCIF_NUMPLAYERS )
148 result = A->info.numplayers - B->info.numplayers;
149 else if( hostcache_sortbyfield == HCIF_PROTOCOL )
150 result = A->info.protocol - B->info.protocol;
151 else if( hostcache_sortbyfield == HCIF_CNAME )
152 result = strcmp( B->info.cname, A->info.cname );
153 else if( hostcache_sortbyfield == HCIF_GAME )
154 result = strcmp( B->info.game, A->info.game );
155 else if( hostcache_sortbyfield == HCIF_MAP )
156 result = strcmp( B->info.map, A->info.map );
157 else if( hostcache_sortbyfield == HCIF_MOD )
158 result = strcmp( B->info.mod, A->info.mod );
159 else if( hostcache_sortbyfield == HCIF_NAME )
160 result = strcmp( B->info.name, A->info.name );
162 if( hostcache_sortdescending )
167 static qboolean _hc_testint( int A, hostcache_maskop_t op, int B )
176 case HCMO_GREATEREQUAL:
195 static qboolean _HostCache_TestMask( hostcache_info_t *info )
197 if( !_hc_testint( info->ping, hostcache_currentmask.pingtest, hostcache_currentmask.info.ping ) )
199 if( !_hc_testint( info->maxplayers, hostcache_currentmask.maxplayerstest, hostcache_currentmask.info.maxplayers ) )
201 if( !_hc_testint( info->numplayers, hostcache_currentmask.numplayerstest, hostcache_currentmask.info.numplayers ) )
203 if( !_hc_testint( info->protocol, hostcache_currentmask.protocoltest, hostcache_currentmask.info.protocol ))
205 if( *hostcache_currentmask.info.cname
206 && !strstr( info->cname, hostcache_currentmask.info.cname ) )
208 if( *hostcache_currentmask.info.game
209 && !strstr( info->game, hostcache_currentmask.info.game ) )
211 if( *hostcache_currentmask.info.mod
212 && !strstr( info->mod, hostcache_currentmask.info.mod ) )
214 if( *hostcache_currentmask.info.map
215 && !strstr( info->map, hostcache_currentmask.info.map ) )
217 if( *hostcache_currentmask.info.name
218 && !strstr( info->name, hostcache_currentmask.info.name ) )
223 static void _HostCache_Insert( hostcache_t *entry )
226 if( hostcache_viewcount == HOSTCACHE_VIEWCACHESIZE )
228 // now check whether it passes through mask
229 if( !_HostCache_TestMask( &entry->info ) )
232 if( !hostcache_viewcount ) {
233 _HostCache_ViewSet_InsertBefore( 0, entry );
236 // ok, insert it, we just need to find out where exactly:
239 // check whether to insert it as new first item
240 if( _HostCache_SortTest( entry, hostcache_viewset[0] ) ) {
241 _HostCache_ViewSet_InsertBefore( 0, entry );
243 } // check whether to insert it as new last item
244 else if( !_HostCache_SortTest( entry, hostcache_viewset[hostcache_viewcount - 1] ) ) {
245 _HostCache_ViewSet_InsertBefore( hostcache_viewcount, entry );
249 end = hostcache_viewcount - 1;
250 while( end > start + 1 )
252 mid = (start + end) / 2;
253 // test the item that lies in the middle between start and end
254 if( _HostCache_SortTest( entry, hostcache_viewset[mid] ) )
255 // the item has to be in the upper half
258 // the item has to be in the lower half
261 _HostCache_ViewSet_InsertBefore( start + 1, entry );
264 static void _HostCache_Remove( hostcache_t *entry )
267 for( i = 0; i < hostcache_viewcount; i++ )
269 if (hostcache_viewset[i] == entry)
271 _HostCache_ViewSet_Remove(i);
277 void HostCache_RebuildViewSet(void)
281 hostcache_viewcount = 0;
282 for( i = 0 ; i < hostcache_cachecount ; i++ )
283 if( hostcache_cache[i].finished )
284 _HostCache_Insert( &hostcache_cache[i] );
287 void HostCache_ResetMask(void)
289 memset( &hostcache_currentmask, 0, sizeof( hostcache_mask_t ) );
293 void HostCache_QueryList(void)
295 masterquerytime = realtime;
296 masterquerycount = 0;
297 masterreplycount = 0;
298 serverquerycount = 0;
299 serverreplycount = 0;
300 hostcache_cachecount = 0;
301 hostcache_viewcount = 0;
302 hostcache_consoleoutput = false;
303 NetConn_QueryMasters();
308 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
310 int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
312 if (cl_netpacketloss.integer)
313 for (i = 0;i < cl_numsockets;i++)
314 if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
316 if (developer_networking.integer && length != 0)
318 char addressstring[128], addressstring2[128];
319 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
322 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
323 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i from %s:\n", mysocket, addressstring, data, maxlength, peeraddress, length, addressstring2);
324 Com_HexDumpToConsole(data, length);
327 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i\n", mysocket, addressstring, data, maxlength, peeraddress, length);
332 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress)
336 if (cl_netpacketloss.integer)
337 for (i = 0;i < cl_numsockets;i++)
338 if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
340 ret = LHNET_Write(mysocket, data, length, peeraddress);
341 if (developer_networking.integer)
343 char addressstring[128], addressstring2[128];
344 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
345 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
346 Con_Printf("LHNET_Write(%p (%s), %p, %i, %p (%s)) = %i%s\n", mysocket, addressstring, data, length, peeraddress, addressstring2, length, ret == length ? "" : " (ERROR)");
347 Com_HexDumpToConsole(data, length);
352 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress)
354 // note this does not include the trailing NULL because we add that in the parser
355 return NetConn_Write(mysocket, string, strlen(string), peeraddress);
358 int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
360 unsigned int packetLen;
361 unsigned int dataLen;
363 unsigned int *header;
366 if (data->cursize == 0)
367 Sys_Error("Datagram_SendMessage: zero length message\n");
369 if (data->cursize > (int)sizeof(conn->sendMessage))
370 Sys_Error("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
372 if (conn->canSend == false)
373 Sys_Error("SendMessage: called with canSend == false\n");
376 memcpy(conn->sendMessage, data->data, data->cursize);
377 conn->sendMessageLength = data->cursize;
379 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
381 dataLen = conn->sendMessageLength;
386 dataLen = MAX_PACKETFRAGMENT;
390 packetLen = NET_HEADERSIZE + dataLen;
392 header = (void *)sendbuffer;
393 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
394 header[1] = BigLong(conn->sendSequence);
395 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
397 conn->sendSequence++;
398 conn->canSend = false;
400 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
403 conn->lastSendTime = realtime;
405 reliableMessagesSent++;
409 static void NetConn_SendMessageNext(netconn_t *conn)
411 unsigned int packetLen;
412 unsigned int dataLen;
414 unsigned int *header;
416 if (conn->sendMessageLength && !conn->canSend && conn->sendNext)
418 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
420 dataLen = conn->sendMessageLength;
425 dataLen = MAX_PACKETFRAGMENT;
429 packetLen = NET_HEADERSIZE + dataLen;
431 header = (void *)sendbuffer;
432 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
433 header[1] = BigLong(conn->sendSequence);
434 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
436 conn->sendSequence++;
437 conn->sendNext = false;
439 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
442 conn->lastSendTime = realtime;
447 static void NetConn_ReSendMessage(netconn_t *conn)
449 unsigned int packetLen;
450 unsigned int dataLen;
452 unsigned int *header;
454 if (conn->sendMessageLength && !conn->canSend && (realtime - conn->lastSendTime) > 1.0)
456 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
458 dataLen = conn->sendMessageLength;
463 dataLen = MAX_PACKETFRAGMENT;
467 packetLen = NET_HEADERSIZE + dataLen;
469 header = (void *)sendbuffer;
470 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
471 header[1] = BigLong(conn->sendSequence - 1);
472 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
474 conn->sendNext = false;
476 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
479 conn->lastSendTime = realtime;
484 qboolean NetConn_CanSendMessage(netconn_t *conn)
486 return conn->canSend;
489 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
494 packetLen = NET_HEADERSIZE + data->cursize;
497 if (data->cursize == 0)
498 Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
500 if (packetLen > (int)sizeof(sendbuffer))
501 Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
504 header = (void *)sendbuffer;
505 header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
506 header[1] = BigLong(conn->unreliableSendSequence);
507 memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
509 conn->unreliableSendSequence++;
511 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
515 unreliableMessagesSent++;
519 void NetConn_CloseClientPorts(void)
521 for (;cl_numsockets > 0;cl_numsockets--)
522 if (cl_sockets[cl_numsockets - 1])
523 LHNET_CloseSocket(cl_sockets[cl_numsockets - 1]);
526 void NetConn_OpenClientPort(const char *addressstring, int defaultport)
528 lhnetaddress_t address;
530 char addressstring2[1024];
531 if (LHNETADDRESS_FromString(&address, addressstring, defaultport))
533 if ((s = LHNET_OpenSocket_Connectionless(&address)))
535 cl_sockets[cl_numsockets++] = s;
536 LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
537 Con_Printf("Client opened a socket on address %s\n", addressstring2);
541 LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
542 Con_Printf("Client failed to open a socket on address %s\n", addressstring2);
546 Con_Printf("Client unable to parse address %s\n", addressstring);
549 void NetConn_OpenClientPorts(void)
552 NetConn_CloseClientPorts();
553 port = bound(0, cl_netport.integer, 65535);
554 if (cl_netport.integer != port)
555 Cvar_SetValueQuick(&cl_netport, port);
556 Con_Printf("Client using port %i\n", port);
557 NetConn_OpenClientPort("local:2", 0);
558 NetConn_OpenClientPort(net_address.string, port);
559 //NetConn_OpenClientPort(net_address_ipv6.string, port);
562 void NetConn_CloseServerPorts(void)
564 for (;sv_numsockets > 0;sv_numsockets--)
565 if (sv_sockets[sv_numsockets - 1])
566 LHNET_CloseSocket(sv_sockets[sv_numsockets - 1]);
569 void NetConn_OpenServerPort(const char *addressstring, int defaultport)
571 lhnetaddress_t address;
573 char addressstring2[1024];
574 if (LHNETADDRESS_FromString(&address, addressstring, defaultport))
576 if ((s = LHNET_OpenSocket_Connectionless(&address)))
578 sv_sockets[sv_numsockets++] = s;
579 LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
580 Con_Printf("Server listening on address %s\n", addressstring2);
584 LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
585 Con_Printf("Server failed to open socket on address %s\n", addressstring2);
589 Con_Printf("Server unable to parse address %s\n", addressstring);
592 void NetConn_OpenServerPorts(int opennetports)
595 NetConn_CloseServerPorts();
596 port = bound(0, sv_netport.integer, 65535);
599 Con_Printf("Server using port %i\n", port);
600 if (sv_netport.integer != port)
601 Cvar_SetValueQuick(&sv_netport, port);
602 if (cls.state != ca_dedicated)
603 NetConn_OpenServerPort("local:1", 0);
606 NetConn_OpenServerPort(net_address.string, port);
607 //NetConn_OpenServerPort(net_address_ipv6.string, port);
609 if (sv_numsockets == 0)
610 Host_Error("NetConn_OpenServerPorts: unable to open any ports!\n");
613 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address)
615 int i, a = LHNETADDRESS_GetAddressType(address);
616 for (i = 0;i < cl_numsockets;i++)
617 if (cl_sockets[i] && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])) == a)
618 return cl_sockets[i];
622 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address)
624 int i, a = LHNETADDRESS_GetAddressType(address);
625 for (i = 0;i < sv_numsockets;i++)
626 if (sv_sockets[i] && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(sv_sockets[i])) == a)
627 return sv_sockets[i];
631 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
634 conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
635 conn->mysocket = mysocket;
636 conn->peeraddress = *peeraddress;
637 conn->canSend = true;
638 conn->lastMessageTime = realtime;
639 // LordHavoc: (inspired by ProQuake) use a short connect timeout to
640 // reduce effectiveness of connection request floods
641 conn->timeout = realtime + net_connecttimeout.value;
642 LHNETADDRESS_ToString(&conn->peeraddress, conn->address, sizeof(conn->address), true);
643 conn->next = netconn_list;
648 void NetConn_Close(netconn_t *conn)
651 // remove connection from list
652 if (conn == netconn_list)
653 netconn_list = conn->next;
656 for (c = netconn_list;c;c = c->next)
660 c->next = conn->next;
664 // not found in list, we'll avoid crashing here...
672 static int clientport = -1;
673 static int clientport2 = -1;
674 static int hostport = -1;
675 static void NetConn_UpdateServerStuff(void)
677 if (cls.state != ca_dedicated)
679 if (clientport2 != cl_netport.integer)
681 clientport2 = cl_netport.integer;
682 if (cls.state == ca_connected)
683 Con_Print("Changing \"cl_port\" will not take effect until you reconnect.\n");
685 if (cls.state == ca_disconnected && clientport != clientport2)
687 clientport = clientport2;
688 NetConn_CloseClientPorts();
690 if (cl_numsockets == 0)
691 NetConn_OpenClientPorts();
694 if (hostport != sv_netport.integer)
696 hostport = sv_netport.integer;
698 Con_Print("Changing \"port\" will not take effect until \"map\" command is executed.\n");
702 int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
706 unsigned int sequence;
710 length = BigLong(((int *)data)[0]);
711 flags = length & ~NETFLAG_LENGTH_MASK;
712 length &= NETFLAG_LENGTH_MASK;
713 // control packets were already handled
714 if (!(flags & NETFLAG_CTL))
716 sequence = BigLong(((int *)data)[1]);
720 if (flags & NETFLAG_UNRELIABLE)
722 if (sequence >= conn->unreliableReceiveSequence)
724 if (sequence > conn->unreliableReceiveSequence)
726 count = sequence - conn->unreliableReceiveSequence;
727 droppedDatagrams += count;
728 Con_DPrintf("Dropped %u datagram(s)\n", count);
730 conn->unreliableReceiveSequence = sequence + 1;
731 conn->lastMessageTime = realtime;
732 conn->timeout = realtime + net_messagetimeout.value;
733 unreliableMessagesReceived++;
736 SZ_Clear(&net_message);
737 SZ_Write(&net_message, data, length);
743 Con_DPrint("Got a stale datagram\n");
746 else if (flags & NETFLAG_ACK)
748 if (sequence == (conn->sendSequence - 1))
750 if (sequence == conn->ackSequence)
753 if (conn->ackSequence != conn->sendSequence)
754 Con_DPrint("ack sequencing error\n");
755 conn->lastMessageTime = realtime;
756 conn->timeout = realtime + net_messagetimeout.value;
757 conn->sendMessageLength -= MAX_PACKETFRAGMENT;
758 if (conn->sendMessageLength > 0)
760 memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength);
761 conn->sendNext = true;
762 NetConn_SendMessageNext(conn);
766 conn->sendMessageLength = 0;
767 conn->canSend = true;
771 Con_DPrint("Duplicate ACK received\n");
774 Con_DPrint("Stale ACK received\n");
777 else if (flags & NETFLAG_DATA)
779 unsigned int temppacket[2];
780 temppacket[0] = BigLong(8 | NETFLAG_ACK);
781 temppacket[1] = BigLong(sequence);
782 NetConn_Write(conn->mysocket, (qbyte *)temppacket, 8, &conn->peeraddress);
783 if (sequence == conn->receiveSequence)
785 conn->lastMessageTime = realtime;
786 conn->timeout = realtime + net_messagetimeout.value;
787 conn->receiveSequence++;
788 memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
789 conn->receiveMessageLength += length;
790 if (flags & NETFLAG_EOM)
792 reliableMessagesReceived++;
793 length = conn->receiveMessageLength;
794 conn->receiveMessageLength = 0;
797 SZ_Clear(&net_message);
798 SZ_Write(&net_message, conn->receiveMessage, length);
805 receivedDuplicateCount++;
813 void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
815 cls.connect_trying = false;
816 M_Update_Return_Reason("");
817 // the connection request succeeded, stop current connection and set up a new connection
819 cls.netcon = NetConn_Open(mysocket, peeraddress);
820 Con_Printf("Connection accepted to %s\n", cls.netcon->address);
823 cls.demonum = -1; // not in the demo loop now
824 cls.state = ca_connected;
825 cls.signon = 0; // need all the signon messages before playing
827 SCR_BeginLoadingPlaque();
830 int NetConn_IsLocalGame(void)
832 if (cls.state == ca_connected && sv.active && cl.maxclients == 1)
837 int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
840 lhnetaddress_t svaddress;
842 char *string, addressstring2[128], cname[128], ipstring[32];
843 char stringbuf[16384];
845 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
847 // received a command string - strip off the packaging and put it
848 // into our string buffer with NULL termination
851 length = min(length, (int)sizeof(stringbuf) - 1);
852 memcpy(stringbuf, data, length);
853 stringbuf[length] = 0;
856 if (developer.integer)
858 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
859 Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2);
860 Com_HexDumpToConsole(data, length);
863 if (length > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
865 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
866 Con_Printf("\"%s\" received, sending connect request back to %s\n", string, addressstring2);
867 M_Update_Return_Reason("Got challenge response");
868 NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\challenge\\%s", string + 10), peeraddress);
871 if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying)
873 M_Update_Return_Reason("Accepted");
874 NetConn_ConnectionEstablished(mysocket, peeraddress);
877 if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying)
879 char rejectreason[32];
880 cls.connect_trying = false;
882 length = max(length - 7, (int)sizeof(rejectreason) - 1);
883 memcpy(rejectreason, string, length);
884 rejectreason[length] = 0;
885 M_Update_Return_Reason(rejectreason);
888 if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
890 hostcache_info_t *info;
895 // hostcache only uses text addresses
896 LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
897 // search the cache for this server and update it
898 for( n = 0; n < hostcache_cachecount; n++ )
899 if( !strcmp( cname, hostcache_cache[n].info.cname ) )
901 if( n == hostcache_cachecount )
904 info = &hostcache_cache[n].info;
905 if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(info->game, s, sizeof (info->game));else info->game[0] = 0;
906 if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0] = 0;
907 if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(info->map , s, sizeof (info->map ));else info->map[0] = 0;
908 if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(info->name, s, sizeof (info->name));else info->name[0] = 0;
909 if ((s = SearchInfostring(string, "protocol" )) != NULL) info->protocol = atoi(s);else info->protocol = -1;
910 if ((s = SearchInfostring(string, "clients" )) != NULL) info->numplayers = atoi(s);else info->numplayers = 0;
911 if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);else info->maxplayers = 0;
913 if (info->ping == 100000)
916 pingtime = (int)((realtime - hostcache_cache[n].querytime) * 1000.0);
917 pingtime = bound(0, pingtime, 9999);
919 info->ping = pingtime;
921 // legacy/old stuff move it to the menu ASAP
923 // build description strings for the things users care about
924 snprintf(hostcache_cache[n].line1, sizeof(hostcache_cache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
925 snprintf(hostcache_cache[n].line2, sizeof(hostcache_cache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", info->cname, info->game, info->mod, info->map);
926 // if ping is especially high, display it as such
929 // orange numbers (lower block)
930 for (i = 0;i < 5;i++)
931 if (hostcache_cache[n].line1[i] != ' ')
932 hostcache_cache[n].line1[i] += 128;
934 else if (pingtime >= 200)
936 // yellow numbers (in upper block)
937 for (i = 0;i < 5;i++)
938 if (hostcache_cache[n].line1[i] != ' ')
939 hostcache_cache[n].line1[i] -= 30;
941 // if not in the slist menu we should print the server to console
942 if( hostcache_consoleoutput )
943 Con_Printf("%s\n%s\n", hostcache_cache[n].line1, hostcache_cache[n].line2);
944 // and finally, update the view set
945 if( hostcache_cache[n].finished )
946 _HostCache_Remove( &hostcache_cache[n] );
947 _HostCache_Insert( &hostcache_cache[n] );
948 hostcache_cache[n].finished = true;
952 if (!strncmp(string, "getserversResponse\\", 19) && hostcache_cachecount < HOSTCACHE_TOTALSIZE)
954 // Extract the IP addresses
958 if (hostcache_consoleoutput)
959 Con_Print("received server list...\n");
960 while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
964 snprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
965 if (developer.integer)
966 Con_Printf("Requesting info from server %s\n", ipstring);
967 // ignore the rest of the message if the hostcache is full
968 if( hostcache_cachecount == HOSTCACHE_TOTALSIZE )
971 LHNETADDRESS_FromString(&svaddress, ipstring, 0);
972 NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
974 memset(&hostcache_cache[hostcache_cachecount], 0, sizeof(hostcache_cache[hostcache_cachecount]));
975 // store the data the engine cares about (address and ping)
976 strlcpy (hostcache_cache[hostcache_cachecount].info.cname, ipstring, sizeof (hostcache_cache[hostcache_cachecount].info.cname));
977 hostcache_cache[hostcache_cachecount].info.ping = 100000;
978 hostcache_cache[hostcache_cachecount].querytime = realtime;
979 // if not in the slist menu we should print the server to console
980 if (hostcache_consoleoutput)
981 Con_Printf("querying %s\n", ipstring);
983 ++hostcache_cachecount;
985 // move on to next address in packet
992 if (!strncmp(string, "ping", 4))
994 if (developer.integer)
995 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
996 NetConn_WriteString(mysocket, "\377\377\377\377ack", peeraddress);
999 if (!strncmp(string, "ack", 3))
1002 // we may not have liked the packet, but it was a command packet, so
1003 // we're done processing this packet now
1006 // netquake control packets, supported for compatibility only
1007 if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
1012 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1016 if (developer.integer)
1017 Con_Printf("Datagram_ParseConnectionless: received CCREP_ACCEPT from %s.\n", addressstring2);
1018 if (cls.connect_trying)
1020 lhnetaddress_t clientportaddress;
1021 clientportaddress = *peeraddress;
1024 unsigned int port = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
1027 LHNETADDRESS_SetPort(&clientportaddress, port);
1029 M_Update_Return_Reason("Accepted");
1030 NetConn_ConnectionEstablished(mysocket, &clientportaddress);
1034 if (developer.integer)
1035 Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
1036 cls.connect_trying = false;
1037 M_Update_Return_Reason(data);
1040 case CCREP_SERVER_INFO:
1041 if (developer.integer)
1042 Con_Printf("Datagram_ParseConnectionless: received CCREP_SERVER_INFO from %s.\n", addressstring2);
1043 if (cls.state != ca_dedicated)
1045 // LordHavoc: because the UDP driver reports 0.0.0.0:26000 as the address
1046 // string we just ignore it and keep the real address
1048 // hostcache only uses text addresses
1049 cname = UDP_AddrToString(readaddr);
1050 // search the cache for this server
1051 for (n = 0; n < hostCacheCount; n++)
1052 if (!strcmp(cname, hostcache[n].cname))
1055 if (n == hostCacheCount && hostCacheCount < HOSTCACHESIZE)
1058 memset(&hostcache[n], 0, sizeof(hostcache[n]));
1059 strlcpy (hostcache[n].name, MSG_ReadString(), sizeof (hostcache[n].name));
1060 strlcpy (hostcache[n].map, MSG_ReadString(), sizeof (hostcache[n].map));
1061 hostcache[n].users = MSG_ReadByte();
1062 hostcache[n].maxusers = MSG_ReadByte();
1064 if (c != NET_PROTOCOL_VERSION)
1066 strlcpy (hostcache[n].cname, hostcache[n].name, sizeof (hostcache[n].cname));
1067 strcpy(hostcache[n].name, "*");
1068 strlcat (hostcache[n].name, hostcache[n].cname, sizeof(hostcache[n].name));
1070 strlcpy (hostcache[n].cname, cname, sizeof (hostcache[n].cname));
1074 case CCREP_PLAYER_INFO:
1075 // we got a CCREP_PLAYER_INFO??
1076 //if (developer.integer)
1077 Con_Printf("Datagram_ParseConnectionless: received CCREP_PLAYER_INFO from %s.\n", addressstring2);
1079 case CCREP_RULE_INFO:
1080 // we got a CCREP_RULE_INFO??
1081 //if (developer.integer)
1082 Con_Printf("Datagram_ParseConnectionless: received CCREP_RULE_INFO from %s.\n", addressstring2);
1088 // we may not have liked the packet, but it was a valid control
1089 // packet, so we're done processing this packet now
1093 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)
1094 CL_ParseServerMessage();
1098 void NetConn_ClientFrame(void)
1101 lhnetaddress_t peeraddress;
1103 NetConn_UpdateServerStuff();
1104 if (cls.connect_trying && cls.connect_nextsendtime < realtime)
1106 if (cls.connect_remainingtries == 0)
1108 cls.connect_trying = false;
1109 M_Update_Return_Reason("Connect: Failed");
1112 cls.connect_nextsendtime = realtime + 1;
1113 cls.connect_remainingtries--;
1114 // try challenge first (newer server)
1115 NetConn_WriteString(cls.connect_mysocket, "\377\377\377\377getchallenge", &cls.connect_address);
1116 // then try netquake as a fallback (old server, or netquake)
1117 SZ_Clear(&net_message);
1118 // save space for the header, filled in later
1119 MSG_WriteLong(&net_message, 0);
1120 MSG_WriteByte(&net_message, CCREQ_CONNECT);
1121 MSG_WriteString(&net_message, "QUAKE");
1122 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1123 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1124 NetConn_Write(cls.connect_mysocket, net_message.data, net_message.cursize, &cls.connect_address);
1125 SZ_Clear(&net_message);
1127 for (i = 0;i < cl_numsockets;i++)
1128 while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
1129 NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
1130 if (cls.netcon && realtime > cls.netcon->timeout)
1132 Con_Print("Connection timed out\n");
1134 Host_ShutdownServer (false);
1136 for (conn = netconn_list;conn;conn = conn->next)
1137 NetConn_ReSendMessage(conn);
1140 #define MAX_CHALLENGES 128
1143 lhnetaddress_t address;
1147 challenge[MAX_CHALLENGES];
1149 static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
1153 for (i = 0;i < bufferlength - 1;i++)
1157 c = rand () % (127 - 33) + 33;
1158 } while (c == '\\' || c == ';' || c == '"' || c == '%' || c == '/');
1164 int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
1166 int i, n, ret, clientnum, responselength, best;
1170 char *s, *string, response[512], addressstring2[128], stringbuf[16384];
1174 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
1176 // received a command string - strip off the packaging and put it
1177 // into our string buffer with NULL termination
1180 length = min(length, (int)sizeof(stringbuf) - 1);
1181 memcpy(stringbuf, data, length);
1182 stringbuf[length] = 0;
1185 if (developer.integer)
1187 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1188 Con_Printf("NetConn_ServerParsePacket: %s sent us a command:\n", addressstring2);
1189 Com_HexDumpToConsole(data, length);
1192 if (length >= 12 && !memcmp(string, "getchallenge", 12))
1194 for (i = 0, best = 0, besttime = realtime;i < MAX_CHALLENGES;i++)
1196 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address))
1198 if (besttime > challenge[i].time)
1199 besttime = challenge[best = i].time;
1201 // if we did not find an exact match, choose the oldest and
1202 // update address and string
1203 if (i == MAX_CHALLENGES)
1206 challenge[i].address = *peeraddress;
1207 NetConn_BuildChallengeString(challenge[i].string, sizeof(challenge[i].string));
1209 challenge[i].time = realtime;
1210 // send the challenge
1211 NetConn_WriteString(mysocket, va("\377\377\377\377challenge %s", challenge[i].string), peeraddress);
1214 if (length > 8 && !memcmp(string, "connect\\", 8))
1218 if ((s = SearchInfostring(string, "challenge")))
1220 // validate the challenge
1221 for (i = 0;i < MAX_CHALLENGES;i++)
1222 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1224 if (i < MAX_CHALLENGES)
1226 // check engine protocol
1227 if (strcmp(SearchInfostring(string, "protocol"), "darkplaces 3"))
1229 if (developer.integer)
1230 Con_Printf("Datagram_ParseConnectionless: sending \"reject Wrong game protocol.\" to %s.\n", addressstring2);
1231 NetConn_WriteString(mysocket, "\377\377\377\377reject Wrong game protocol.", peeraddress);
1235 // see if this is a duplicate connection request
1236 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1237 if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
1239 if (clientnum < svs.maxclients)
1241 // duplicate connection request
1242 if (realtime - client->connecttime < 2.0)
1244 // client is still trying to connect,
1245 // so we send a duplicate reply
1246 if (developer.integer)
1247 Con_Printf("Datagram_ParseConnectionless: sending duplicate accept to %s.\n", addressstring2);
1248 NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
1250 // only kick if old connection seems dead
1251 if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
1253 // kick off connection and await retry
1254 client->deadsocket = true;
1259 // this is a new client, find a slot
1260 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1261 if (!client->active)
1263 if (clientnum < svs.maxclients)
1265 // prepare the client struct
1266 if ((conn = NetConn_Open(mysocket, peeraddress)))
1268 // allocated connection
1269 LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
1270 if (developer.integer)
1271 Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
1272 NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
1273 // now set up the client
1274 SV_ConnectClient(clientnum, conn);
1275 NetConn_Heartbeat(1);
1281 if (developer.integer)
1282 Con_Printf("Datagram_ParseConnectionless: sending \"reject Server is full.\" to %s.\n", addressstring2);
1283 NetConn_WriteString(mysocket, "\377\377\377\377reject Server is full.", peeraddress);
1291 if (length >= 7 && !memcmp(string, "getinfo", 7))
1293 const char *challenge = NULL;
1294 // If there was a challenge in the getinfo message
1295 if (length > 8 && string[7] == ' ')
1296 challenge = string + 8;
1297 for (i = 0, n = 0;i < svs.maxclients;i++)
1298 if (svs.clients[i].active)
1300 responselength = snprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
1301 "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
1302 "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d%s%s",
1303 gamename, com_modname, svs.maxclients, n,
1304 sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "");
1305 // does it fit in the buffer?
1306 if (responselength < (int)sizeof(response))
1308 if (developer.integer)
1309 Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
1310 NetConn_WriteString(mysocket, response, peeraddress);
1315 if (!strncmp(string, "ping", 4))
1317 if (developer.integer)
1318 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
1319 NetConn_WriteString(mysocket, "\377\377\377\377ack", peeraddress);
1322 if (!strncmp(string, "ack", 3))
1325 // we may not have liked the packet, but it was a command packet, so
1326 // we're done processing this packet now
1329 // LordHavoc: disabled netquake control packet support in server
1333 // netquake control packets, supported for compatibility only
1334 if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
1339 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1343 //if (developer.integer)
1344 Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
1345 if (length >= (int)strlen("QUAKE") + 1 + 1)
1347 if (memcmp(data, "QUAKE", strlen("QUAKE") + 1) != 0 || (int)data[strlen("QUAKE") + 1] != NET_PROTOCOL_VERSION)
1349 if (developer.integer)
1350 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Incompatible version.\" to %s.\n", addressstring2);
1351 SZ_Clear(&net_message);
1352 // save space for the header, filled in later
1353 MSG_WriteLong(&net_message, 0);
1354 MSG_WriteByte(&net_message, CCREP_REJECT);
1355 MSG_WriteString(&net_message, "Incompatible version.\n");
1356 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1357 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1358 SZ_Clear(&net_message);
1362 // see if this is a duplicate connection request
1363 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1364 if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
1366 if (clientnum < svs.maxclients)
1368 // duplicate connection request
1369 if (realtime - client->connecttime < 2.0)
1371 // client is still trying to connect,
1372 // so we send a duplicate reply
1373 if (developer.integer)
1374 Con_Printf("Datagram_ParseConnectionless: sending duplicate CCREP_ACCEPT to %s.\n", addressstring2);
1375 SZ_Clear(&net_message);
1376 // save space for the header, filled in later
1377 MSG_WriteLong(&net_message, 0);
1378 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1379 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(client->netconnection->mysocket)));
1380 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1381 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1382 SZ_Clear(&net_message);
1384 else if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
1386 // the old client hasn't sent us anything
1387 // in quite a while, so kick off and let
1388 // the retry take care of it...
1389 client->deadsocket = true;
1394 // this is a new client, find a slot
1395 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1396 if (!client->active)
1398 if (clientnum < svs.maxclients && (client->netconnection = conn = NetConn_Open(mysocket, peeraddress)) != NULL)
1400 // connect to the client
1401 // everything is allocated, just fill in the details
1402 strlcpy (conn->address, addressstring2, sizeof (conn->address));
1403 if (developer.integer)
1404 Con_Printf("Datagram_ParseConnectionless: sending CCREP_ACCEPT to %s.\n", addressstring2);
1405 // send back the info about the server connection
1406 SZ_Clear(&net_message);
1407 // save space for the header, filled in later
1408 MSG_WriteLong(&net_message, 0);
1409 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1410 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(conn->mysocket)));
1411 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1412 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1413 SZ_Clear(&net_message);
1414 // now set up the client struct
1415 SV_ConnectClient(clientnum, conn);
1416 NetConn_Heartbeat(1);
1420 //if (developer.integer)
1421 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Server is full.\" to %s.\n", addressstring2);
1422 // no room; try to let player know
1423 SZ_Clear(&net_message);
1424 // save space for the header, filled in later
1425 MSG_WriteLong(&net_message, 0);
1426 MSG_WriteByte(&net_message, CCREP_REJECT);
1427 MSG_WriteString(&net_message, "Server is full.\n");
1428 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1429 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1430 SZ_Clear(&net_message);
1437 case CCREQ_SERVER_INFO:
1438 if (developer.integer)
1439 Con_Printf("Datagram_ParseConnectionless: received CCREQ_SERVER_INFO from %s.\n", addressstring2);
1440 if (sv.active && !strcmp(MSG_ReadString(), "QUAKE"))
1442 if (developer.integer)
1443 Con_Printf("Datagram_ParseConnectionless: sending CCREP_SERVER_INFO to %s.\n", addressstring2);
1444 SZ_Clear(&net_message);
1445 // save space for the header, filled in later
1446 MSG_WriteLong(&net_message, 0);
1447 MSG_WriteByte(&net_message, CCREP_SERVER_INFO);
1448 UDP_GetSocketAddr(UDP_acceptSock, &newaddr);
1449 MSG_WriteString(&net_message, UDP_AddrToString(&newaddr));
1450 MSG_WriteString(&net_message, hostname.string);
1451 MSG_WriteString(&net_message, sv.name);
1452 MSG_WriteByte(&net_message, net_activeconnections);
1453 MSG_WriteByte(&net_message, svs.maxclients);
1454 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1455 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1456 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1457 SZ_Clear(&net_message);
1460 case CCREQ_PLAYER_INFO:
1461 if (developer.integer)
1462 Con_Printf("Datagram_ParseConnectionless: received CCREQ_PLAYER_INFO from %s.\n", addressstring2);
1465 int playerNumber, activeNumber, clientNumber;
1468 playerNumber = MSG_ReadByte();
1470 for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++)
1471 if (client->active && ++activeNumber == playerNumber)
1473 if (clientNumber != svs.maxclients)
1475 SZ_Clear(&net_message);
1476 // save space for the header, filled in later
1477 MSG_WriteLong(&net_message, 0);
1478 MSG_WriteByte(&net_message, CCREP_PLAYER_INFO);
1479 MSG_WriteByte(&net_message, playerNumber);
1480 MSG_WriteString(&net_message, client->name);
1481 MSG_WriteLong(&net_message, client->colors);
1482 MSG_WriteLong(&net_message, (int)client->edict->v->frags);
1483 MSG_WriteLong(&net_message, (int)(realtime - client->connecttime));
1484 MSG_WriteString(&net_message, client->netconnection ? client->netconnection->address : "botclient");
1485 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1486 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1487 SZ_Clear(&net_message);
1491 case CCREQ_RULE_INFO:
1492 if (developer.integer)
1493 Con_Printf("Datagram_ParseConnectionless: received CCREQ_RULE_INFO from %s.\n", addressstring2);
1499 // find the search start location
1500 prevCvarName = MSG_ReadString();
1501 var = Cvar_FindVarAfter(prevCvarName, CVAR_NOTIFY);
1503 // send the response
1504 SZ_Clear(&net_message);
1505 // save space for the header, filled in later
1506 MSG_WriteLong(&net_message, 0);
1507 MSG_WriteByte(&net_message, CCREP_RULE_INFO);
1510 MSG_WriteString(&net_message, var->name);
1511 MSG_WriteString(&net_message, var->string);
1513 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1514 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1515 SZ_Clear(&net_message);
1522 // we may not have liked the packet, but it was a valid control
1523 // packet, so we're done processing this packet now
1528 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1530 if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
1532 if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
1533 SV_ReadClientMessage();
1541 void NetConn_ServerFrame(void)
1544 lhnetaddress_t peeraddress;
1546 NetConn_UpdateServerStuff();
1547 for (i = 0;i < sv_numsockets;i++)
1548 while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
1549 NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress);
1550 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1552 // never timeout loopback connections
1553 if (host_client->netconnection && realtime > host_client->netconnection->timeout && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
1555 Con_Printf("Client \"%s\" connection timed out\n", host_client->name);
1556 SV_DropClient(false);
1559 for (conn = netconn_list;conn;conn = conn->next)
1560 NetConn_ReSendMessage(conn);
1563 void NetConn_QueryMasters(void)
1567 lhnetaddress_t masteraddress;
1570 if (hostcache_cachecount >= HOSTCACHE_TOTALSIZE)
1573 for (i = 0;i < cl_numsockets;i++)
1580 UDP_Broadcast(UDP_controlSock, "\377\377\377\377getinfo", 11);
1582 SZ_Clear(&net_message);
1583 // save space for the header, filled in later
1584 MSG_WriteLong(&net_message, 0);
1585 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1586 MSG_WriteString(&net_message, "QUAKE");
1587 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1588 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1589 UDP_Broadcast(UDP_controlSock, net_message.data, net_message.cursize);
1590 SZ_Clear(&net_message);
1594 // build the getservers
1595 snprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
1598 for (masternum = 0;sv_masters[masternum].name;masternum++)
1600 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
1603 NetConn_WriteString(cl_sockets[i], request, &masteraddress);
1608 if (!masterquerycount)
1610 Con_Print("Unable to query master servers, no suitable network sockets active.\n");
1611 M_Update_Return_Reason("No network");
1615 void NetConn_Heartbeat(int priority)
1617 lhnetaddress_t masteraddress;
1619 lhnetsocket_t *mysocket;
1621 // if it's a state change (client connected), limit next heartbeat to no
1622 // more than 30 sec in the future
1623 if (priority == 1 && nextheartbeattime > realtime + 30.0)
1624 nextheartbeattime = realtime + 30.0;
1626 // limit heartbeatperiod to 30 to 270 second range,
1627 // lower limit is to avoid abusing master servers with excess traffic,
1628 // upper limit is to avoid timing out on the master server (which uses
1630 if (sv_heartbeatperiod.value < 30)
1631 Cvar_SetValueQuick(&sv_heartbeatperiod, 30);
1632 if (sv_heartbeatperiod.value > 270)
1633 Cvar_SetValueQuick(&sv_heartbeatperiod, 270);
1635 // make advertising optional and don't advertise singleplayer games, and
1636 // only send a heartbeat as often as the admin wants
1637 if (sv.active && sv_public.integer && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
1639 nextheartbeattime = realtime + sv_heartbeatperiod.value;
1640 for (masternum = 0;sv_masters[masternum].name;masternum++)
1641 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && (mysocket = NetConn_ChooseServerSocketForAddress(&masteraddress)))
1642 NetConn_WriteString(mysocket, "\377\377\377\377heartbeat DarkPlaces\x0A", &masteraddress);
1646 int NetConn_SendToAll(sizebuf_t *data, double blocktime)
1649 qbyte sent[MAX_SCOREBOARD];
1651 memset(sent, 0, sizeof(sent));
1653 // simultaneously wait for the first CanSendMessage and send the message,
1654 // then wait for a second CanSendMessage (verifying it was received), or
1655 // the client drops and is no longer counted
1656 // the loop aborts when either it runs out of clients to send to, or a
1658 blocktime += Sys_DoubleTime();
1662 NetConn_ClientFrame();
1663 NetConn_ServerFrame();
1664 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1666 if (host_client->netconnection)
1668 if (NetConn_CanSendMessage(host_client->netconnection))
1671 NetConn_SendReliableMessage(host_client->netconnection, data);
1674 if (!NetConn_CanSendMessage(host_client->netconnection))
1679 while (count && Sys_DoubleTime() < blocktime);
1683 static void Net_Heartbeat_f(void)
1686 NetConn_Heartbeat(2);
1688 Con_Print("No server running, can not heartbeat to master server.\n");
1691 void PrintStats(netconn_t *conn)
1693 Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence);
1696 void Net_Stats_f(void)
1699 Con_Printf("unreliable messages sent = %i\n", unreliableMessagesSent);
1700 Con_Printf("unreliable messages recv = %i\n", unreliableMessagesReceived);
1701 Con_Printf("reliable messages sent = %i\n", reliableMessagesSent);
1702 Con_Printf("reliable messages received = %i\n", reliableMessagesReceived);
1703 Con_Printf("packetsSent = %i\n", packetsSent);
1704 Con_Printf("packetsReSent = %i\n", packetsReSent);
1705 Con_Printf("packetsReceived = %i\n", packetsReceived);
1706 Con_Printf("receivedDuplicateCount = %i\n", receivedDuplicateCount);
1707 Con_Printf("droppedDatagrams = %i\n", droppedDatagrams);
1708 Con_Print("connections =\n");
1709 for (conn = netconn_list;conn;conn = conn->next)
1713 void Net_Slist_f(void)
1715 HostCache_ResetMask();
1716 hostcache_sortbyfield = HCIF_PING;
1717 hostcache_sortdescending = false;
1718 if (m_state != m_slist) {
1719 Con_Print("Sending requests to master servers\n");
1720 HostCache_QueryList();
1721 hostcache_consoleoutput = true;
1722 Con_Print("Listening for replies...\n");
1724 HostCache_QueryList();
1727 void NetConn_Init(void)
1730 lhnetaddress_t tempaddress;
1731 netconn_mempool = Mem_AllocPool("Networking", 0, NULL);
1732 Cmd_AddCommand("net_stats", Net_Stats_f);
1733 Cmd_AddCommand("net_slist", Net_Slist_f);
1734 Cmd_AddCommand("heartbeat", Net_Heartbeat_f);
1735 Cvar_RegisterVariable(&net_messagetimeout);
1736 Cvar_RegisterVariable(&net_messagerejointimeout);
1737 Cvar_RegisterVariable(&net_connecttimeout);
1738 Cvar_RegisterVariable(&cl_netlocalping);
1739 Cvar_RegisterVariable(&cl_netpacketloss);
1740 Cvar_RegisterVariable(&hostname);
1741 Cvar_RegisterVariable(&developer_networking);
1742 Cvar_RegisterVariable(&cl_netport);
1743 Cvar_RegisterVariable(&sv_netport);
1744 Cvar_RegisterVariable(&net_address);
1745 //Cvar_RegisterVariable(&net_address_ipv6);
1746 Cvar_RegisterVariable(&sv_public);
1747 Cvar_RegisterVariable(&sv_heartbeatperiod);
1748 for (i = 0;sv_masters[i].name;i++)
1749 Cvar_RegisterVariable(&sv_masters[i]);
1750 // 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.
1751 if ((i = COM_CheckParm("-ip")) && i + 1 < com_argc)
1753 if (LHNETADDRESS_FromString(&tempaddress, com_argv[i + 1], 0) == 1)
1755 Con_Printf("-ip option used, setting net_address to \"%s\"\n");
1756 Cvar_SetQuick(&net_address, com_argv[i + 1]);
1759 Con_Printf("-ip option used, but unable to parse the address \"%s\"\n", com_argv[i + 1]);
1761 // COMMANDLINEOPTION: Server: -port <portnumber> sets the port to use for a server (default 26000, the same port as QUAKE itself), useful if you host multiple servers on your machine
1762 if (((i = COM_CheckParm("-port")) || (i = COM_CheckParm("-ipport")) || (i = COM_CheckParm("-udpport"))) && i + 1 < com_argc)
1764 i = atoi(com_argv[i + 1]);
1765 if (i >= 0 && i < 65536)
1767 Con_Printf("-port option used, setting port cvar to %i\n", i);
1768 Cvar_SetValueQuick(&sv_netport, i);
1771 Con_Printf("-port option used, but %i is not a valid port number\n", i);
1775 SZ_Alloc(&net_message, NET_MAXMESSAGE, "net_message");
1779 void NetConn_Shutdown(void)
1781 NetConn_CloseClientPorts();
1782 NetConn_CloseServerPorts();