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 static cvar_t sv_masters [] =
33 {CVAR_SAVE, "sv_master1", ""},
34 {CVAR_SAVE, "sv_master2", ""},
35 {CVAR_SAVE, "sv_master3", ""},
36 {CVAR_SAVE, "sv_master4", ""},
37 {0, "sv_masterextra1", "198.88.152.4"},
38 {0, "sv_masterextra2", "68.102.242.12"},
42 static double nextheartbeattime = 0;
44 sizebuf_t net_message;
46 cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
47 cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
48 cvar_t developer_networking = {0, "developer_networking", "0"};
50 /* statistic counters */
51 static int packetsSent = 0;
52 static int packetsReSent = 0;
53 static int packetsReceived = 0;
54 static int receivedDuplicateCount = 0;
55 static int droppedDatagrams = 0;
57 static int unreliableMessagesSent = 0;
58 static int unreliableMessagesReceived = 0;
59 static int reliableMessagesSent = 0;
60 static int reliableMessagesReceived = 0;
62 int hostCacheCount = 0;
63 hostcache_t hostcache[HOSTCACHESIZE];
65 static qbyte sendbuffer[NET_MAXMESSAGE];
66 static qbyte readbuffer[NET_MAXMESSAGE];
69 lhnetsocket_t *cl_sockets[16];
71 lhnetsocket_t *sv_sockets[16];
73 netconn_t *netconn_list = NULL;
74 mempool_t *netconn_mempool = NULL;
76 cvar_t cl_netport = {0, "cl_port", "0"};
77 cvar_t cl_netaddress = {0, "cl_netaddress", "0.0.0.0"};
78 cvar_t cl_netaddress_ipv6 = {0, "cl_netaddress_ipv6", "[0:0:0:0:0:0:0:0]:0"};
80 cvar_t sv_netport = {0, "port", "26000"};
81 cvar_t sv_netaddress = {0, "sv_netaddress", "0.0.0.0"};
82 cvar_t sv_netaddress_ipv6 = {0, "sv_netaddress_ipv6", "[0:0:0:0:0:0:0:0]:26000"};
84 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
86 int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
87 if (developer_networking.integer && length != 0)
89 char addressstring[128], addressstring2[128];
90 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
93 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
94 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i from %s:\n", mysocket, addressstring, data, maxlength, peeraddress, length, addressstring2);
95 Com_HexDumpToConsole(data, length);
98 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i\n", mysocket, addressstring, data, maxlength, peeraddress, length);
103 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress)
105 int ret = LHNET_Write(mysocket, data, length, peeraddress);
106 if (developer_networking.integer && ret != 0)
108 char addressstring[128], addressstring2[128];
109 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
110 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
111 Con_Printf("LHNET_Write(%p (%s), %p, %i, %p (%s)) = %i%s\n", mysocket, addressstring, data, length, peeraddress, addressstring2, length, ret == length ? "" : " (ERROR)");
112 Com_HexDumpToConsole(data, length);
117 int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
119 unsigned int packetLen;
120 unsigned int dataLen;
122 unsigned int *header;
125 if (data->cursize == 0)
126 Sys_Error("Datagram_SendMessage: zero length message\n");
128 if (data->cursize > NET_MAXMESSAGE)
129 Sys_Error("Datagram_SendMessage: message too big %u\n", data->cursize);
131 if (conn->canSend == false)
132 Sys_Error("SendMessage: called with canSend == false\n");
135 memcpy(conn->sendMessage, data->data, data->cursize);
136 conn->sendMessageLength = data->cursize;
138 if (conn->sendMessageLength <= MAX_DATAGRAM)
140 dataLen = conn->sendMessageLength;
145 dataLen = MAX_DATAGRAM;
149 packetLen = NET_HEADERSIZE + dataLen;
151 header = (void *)sendbuffer;
152 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
153 header[1] = BigLong(conn->sendSequence);
154 memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
156 conn->sendSequence++;
157 conn->canSend = false;
159 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
162 conn->lastSendTime = realtime;
164 reliableMessagesSent++;
168 static void NetConn_SendMessageNext(netconn_t *conn)
170 unsigned int packetLen;
171 unsigned int dataLen;
173 unsigned int *header;
175 if (conn->sendMessageLength && !conn->canSend && conn->sendNext)
177 if (conn->sendMessageLength <= MAX_DATAGRAM)
179 dataLen = conn->sendMessageLength;
184 dataLen = MAX_DATAGRAM;
188 packetLen = NET_HEADERSIZE + dataLen;
190 header = (void *)sendbuffer;
191 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
192 header[1] = BigLong(conn->sendSequence);
193 memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
195 conn->sendSequence++;
196 conn->sendNext = false;
198 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
201 conn->lastSendTime = realtime;
206 static void NetConn_ReSendMessage(netconn_t *conn)
208 unsigned int packetLen;
209 unsigned int dataLen;
211 unsigned int *header;
213 if (conn->sendMessageLength && !conn->canSend && (realtime - conn->lastSendTime) > 1.0)
215 if (conn->sendMessageLength <= MAX_DATAGRAM)
217 dataLen = conn->sendMessageLength;
222 dataLen = MAX_DATAGRAM;
226 packetLen = NET_HEADERSIZE + dataLen;
228 header = (void *)sendbuffer;
229 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
230 header[1] = BigLong(conn->sendSequence - 1);
231 memcpy(sendbuffer + 8, conn->sendMessage, dataLen);
233 conn->sendNext = false;
235 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
238 conn->lastSendTime = realtime;
243 qboolean NetConn_CanSendMessage(netconn_t *conn)
245 return conn->canSend;
248 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
254 if (data->cursize == 0)
255 Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
257 if (data->cursize > MAX_DATAGRAM)
258 Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
261 packetLen = NET_HEADERSIZE + data->cursize;
263 header = (void *)sendbuffer;
264 header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
265 header[1] = BigLong(conn->unreliableSendSequence);
266 memcpy(sendbuffer + 8, data->data, data->cursize);
268 conn->unreliableSendSequence++;
270 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
274 unreliableMessagesSent++;
278 void NetConn_CloseClientPorts(void)
280 for (;cl_numsockets > 0;cl_numsockets--)
281 if (cl_sockets[cl_numsockets - 1])
282 LHNET_CloseSocket(cl_sockets[cl_numsockets - 1]);
285 void NetConn_OpenClientPorts(void)
288 lhnetaddress_t address;
289 NetConn_CloseClientPorts();
290 port = bound(0, cl_netport.integer, 65535);
291 if (cl_netport.integer != port)
292 Cvar_SetValueQuick(&cl_netport, port);
293 LHNETADDRESS_FromString(&address, "local", port);
294 cl_sockets[cl_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
295 LHNETADDRESS_FromString(&address, cl_netaddress.string, port);
296 cl_sockets[cl_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
297 LHNETADDRESS_FromString(&address, cl_netaddress_ipv6.string, port);
298 cl_sockets[cl_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
301 void NetConn_CloseServerPorts(void)
303 for (;sv_numsockets > 0;sv_numsockets--)
304 if (sv_sockets[sv_numsockets - 1])
305 LHNET_CloseSocket(sv_sockets[sv_numsockets - 1]);
308 void NetConn_OpenServerPorts(int opennetports)
311 lhnetaddress_t address;
312 NetConn_CloseServerPorts();
313 port = bound(0, sv_netport.integer, 65535);
316 if (sv_netport.integer != port)
317 Cvar_SetValueQuick(&sv_netport, port);
318 LHNETADDRESS_FromString(&address, "local", port);
319 sv_sockets[sv_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
322 LHNETADDRESS_FromString(&address, sv_netaddress.string, port);
323 sv_sockets[sv_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
324 LHNETADDRESS_FromString(&address, sv_netaddress_ipv6.string, port);
325 sv_sockets[sv_numsockets++] = LHNET_OpenSocket_Connectionless(&address);
329 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address)
331 int i, a = LHNETADDRESS_GetAddressType(address);
332 for (i = 0;i < cl_numsockets;i++)
333 if (LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])) == a)
334 return cl_sockets[i];
338 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address)
340 int i, a = LHNETADDRESS_GetAddressType(address);
341 for (i = 0;i < sv_numsockets;i++)
342 if (LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(sv_sockets[i])) == a)
343 return sv_sockets[i];
347 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
350 conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
351 conn->mysocket = mysocket;
352 conn->peeraddress = *peeraddress;
353 conn->canSend = true;
354 conn->connecttime = realtime;
355 conn->lastMessageTime = realtime;
356 LHNETADDRESS_ToString(&conn->peeraddress, conn->address, sizeof(conn->address), true);
357 conn->next = netconn_list;
362 void NetConn_Close(netconn_t *conn)
365 // remove connection from list
366 if (conn == netconn_list)
367 netconn_list = conn->next;
370 for (c = netconn_list;c;c = c->next)
374 c->next = conn->next;
378 // not found in list, we'll avoid crashing here...
386 static int clientport = -1;
387 static int clientport2 = -1;
388 static int hostport = -1;
389 static void NetConn_UpdateServerStuff(void)
391 if (clientport2 != cl_netport.integer)
393 clientport2 = cl_netport.integer;
394 if (cls.state == ca_connected)
395 Con_Printf("Changing \"cl_port\" will not take effect until you reconnect.\n");
397 if (cls.state != ca_dedicated)
399 if (cls.state == ca_disconnected && clientport != cl_netport.integer)
401 clientport = cl_netport.integer;
402 NetConn_CloseClientPorts();
404 if (cl_numsockets == 0)
405 NetConn_OpenClientPorts();
408 if (hostport != sv_netport.integer)
410 hostport = sv_netport.integer;
412 Con_Printf("Changing \"port\" will not take effect until \"map\" command is executed.\n");
416 int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
420 unsigned int sequence;
425 length = BigLong(((int *)data)[0]);
426 flags = length & ~NETFLAG_LENGTH_MASK;
427 length &= NETFLAG_LENGTH_MASK;
428 // control packets were already handled
429 if (!(flags & NETFLAG_CTL))
431 sequence = BigLong(((int *)data)[1]);
435 if (flags & NETFLAG_UNRELIABLE)
437 if (sequence >= conn->unreliableReceiveSequence)
439 if (sequence > conn->unreliableReceiveSequence)
441 count = sequence - conn->unreliableReceiveSequence;
442 droppedDatagrams += count;
443 Con_DPrintf("Dropped %u datagram(s)\n", count);
445 conn->unreliableReceiveSequence = sequence + 1;
446 unreliableMessagesReceived++;
447 SZ_Clear(&net_message);
448 SZ_Write(&net_message, data, length);
453 Con_DPrintf("Got a stale datagram\n");
456 else if (flags & NETFLAG_ACK)
458 if (sequence == (conn->sendSequence - 1))
460 if (sequence == conn->ackSequence)
463 if (conn->ackSequence != conn->sendSequence)
464 Con_DPrintf("ack sequencing error\n");
465 conn->sendMessageLength -= MAX_DATAGRAM;
466 if (conn->sendMessageLength > 0)
468 memcpy(conn->sendMessage, conn->sendMessage+MAX_DATAGRAM, conn->sendMessageLength);
469 conn->sendNext = true;
470 NetConn_SendMessageNext(conn);
474 conn->sendMessageLength = 0;
475 conn->canSend = true;
479 Con_DPrintf("Duplicate ACK received\n");
482 Con_DPrintf("Stale ACK received\n");
485 else if (flags & NETFLAG_DATA)
487 unsigned int temppacket[2];
488 temppacket[0] = BigLong(8 | NETFLAG_ACK);
489 temppacket[1] = BigLong(sequence);
490 NetConn_Write(conn->mysocket, (qbyte *)temppacket, 8, &conn->peeraddress);
491 if (sequence == conn->receiveSequence)
493 conn->receiveSequence++;
494 memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
495 conn->receiveMessageLength += length;
496 if (flags & NETFLAG_EOM)
498 reliableMessagesReceived++;
499 SZ_Clear(&net_message);
500 SZ_Write(&net_message, conn->receiveMessage, conn->receiveMessageLength);
501 conn->receiveMessageLength = 0;
507 receivedDuplicateCount++;
517 lhnetaddress_t peeraddress;
519 pingcache[HOSTCACHESIZE];
521 int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
524 lhnetaddress_t svaddress;
526 char *string, addressstring2[128], cname[128], ipstring[32];
528 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
532 string = (char *)data;
533 if (developer.integer)
535 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
536 Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2);
537 Com_HexDumpToConsole(data, length);
539 if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
541 int i, j, c, n, users, maxusers;
542 char game[32], mod[32], map[32], name[128];
546 // hostcache only uses text addresses
547 LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
548 // search the cache for this server
549 for (n = 0; n < hostCacheCount; n++)
550 if (!strcmp(cname, hostcache[n].cname))
552 // add it or update it
553 if (n == hostCacheCount)
555 // if cache is full replace highest ping server (the list is
556 // kept sorted so this is always the last, and if this server
557 // is good it will be sorted into an early part of the list)
558 if (hostCacheCount >= HOSTCACHESIZE)
559 n = hostCacheCount - 1;
563 if ((s = SearchInfostring(string, "gamename" )) != NULL) strncpy(game, s, sizeof(game) - 1);else game[0] = 0;
564 if ((s = SearchInfostring(string, "modname" )) != NULL) strncpy(mod , s, sizeof(mod ) - 1);else mod[0] = 0;
565 if ((s = SearchInfostring(string, "mapname" )) != NULL) strncpy(map , s, sizeof(map ) - 1);else map[0] = 0;
566 if ((s = SearchInfostring(string, "hostname" )) != NULL) strncpy(name, s, sizeof(name) - 1);else name[0] = 0;
567 if ((s = SearchInfostring(string, "protocol" )) != NULL) c = atoi(s);else c = -1;
568 if ((s = SearchInfostring(string, "clients" )) != NULL) users = atoi(s);else users = 0;
569 if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) maxusers = atoi(s);else maxusers = 0;
571 for (i = 0;i < HOSTCACHESIZE;i++)
572 if (!LHNETADDRESS_Compare(peeraddress, &pingcache[i].peeraddress))
573 pingtime = (int)(realtime - pingcache[i].senttime);
574 pingtime = bound(0, pingtime, 9999);
575 memset(&hostcache[n], 0, sizeof(hostcache[n]));
576 // store the data the engine cares about (address and ping)
577 strcpy(hostcache[n].cname, cname);
578 hostcache[n].ping = pingtime;
579 // build description strings for the things users care about
580 snprintf(hostcache[n].line1, sizeof(hostcache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, c != NET_PROTOCOL_VERSION ? '*' : ' ', users, maxusers, name);
581 snprintf(hostcache[n].line2, sizeof(hostcache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", cname, game, mod, map);
582 // if ping is especially high, display it as such
585 // orange numbers (lower block)
586 for (i = 0;i < 5;i++)
587 if (hostcache[n].line1[i] != ' ')
588 hostcache[n].line1[i] += 128;
590 else if (pingtime >= 200)
592 // yellow numbers (in upper block)
593 for (i = 0;i < 5;i++)
594 if (hostcache[n].line1[i] != ' ')
595 hostcache[n].line1[i] -= 30;
597 // if not in the slist menu we should print the server to console
598 if (m_state != m_slist)
599 Con_Printf("%s\n%s\n", hostcache[n].line1, hostcache[n].line2);
600 // and finally, re-sort the list
601 for (i = 0;i < hostCacheCount;i++)
603 for (j = i + 1;j < hostCacheCount;j++)
605 //if (strcmp(hostcache[j].name, hostcache[i].name) < 0)
606 if (hostcache[i].ping > hostcache[j].ping)
608 memcpy(&temp, &hostcache[j], sizeof(hostcache_t));
609 memcpy(&hostcache[j], &hostcache[i], sizeof(hostcache_t));
610 memcpy(&hostcache[i], &temp, sizeof(hostcache_t));
616 if (!strncmp(string, "getserversResponse\\", 19) && hostCacheCount < HOSTCACHESIZE)
620 // Extract the IP addresses
623 while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
625 sprintf(ipstring, "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
626 if (developer.integer)
627 Con_Printf("Requesting info from server %s\n", ipstring);
628 LHNETADDRESS_FromString(&svaddress, ipstring, 0);
629 NetConn_Write(mysocket, "\377\377\377\377getinfo", 11, &svaddress);
630 // replace oldest or matching entry in ping cache
631 // we scan this later when getting a reply to see how long it took
634 for (i = 0;i < HOSTCACHESIZE;i++)
636 if (!LHNETADDRESS_Compare(&svaddress, &pingcache[i].peeraddress))
641 if (besttime > pingcache[i].senttime)
643 besttime = pingcache[i].senttime;
645 // if ping cache isn't full yet we can skip out early
650 pingcache[best].peeraddress = svaddress;
651 pingcache[best].senttime = realtime;
652 // move on to next address in packet
659 if (!strncmp(string, "ping", 4))
661 if (developer.integer)
662 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
663 NetConn_Write(mysocket, "\377\377\377\377ack", 7, peeraddress);
666 if (!strncmp(string, "ack", 3))
669 // we may not have liked the packet, but it was a command packet, so
670 // we're done processing this packet now
673 if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
678 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
682 if (developer.integer)
683 Con_Printf("Datagram_ParseConnectionless: received CCREP_ACCEPT from %s.\n", addressstring2);
684 if (cls.connect_trying)
686 Con_Printf("Connection accepted to %s\n", addressstring2);
689 cls.netcon = NetConn_Open(mysocket, peeraddress);
690 cls.connect_trying = false;
691 cls.demonum = -1; // not in the demo loop now
692 cls.state = ca_connected;
693 cls.signon = 0; // need all the signon messages before playing
699 if (developer.integer)
700 Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
701 Con_Printf("%s\n", data);
702 strncpy(m_return_reason, data, sizeof(m_return_reason) - 1);
705 case CCREP_SERVER_INFO:
706 if (developer.integer)
707 Con_Printf("Datagram_ParseConnectionless: received CCREP_SERVER_INFO from %s.\n", addressstring2);
708 if (cls.state != ca_dedicated)
710 // LordHavoc: because the UDP driver reports 0.0.0.0:26000 as the address
711 // string we just ignore it and keep the real address
713 // hostcache only uses text addresses
714 cname = UDP_AddrToString(readaddr);
715 // search the cache for this server
716 for (n = 0; n < hostCacheCount; n++)
717 if (!strcmp(cname, hostcache[n].cname))
720 if (n == hostCacheCount && hostCacheCount < HOSTCACHESIZE)
723 memset(&hostcache[n], 0, sizeof(hostcache[n]));
724 strcpy(hostcache[n].name, MSG_ReadString());
725 strcpy(hostcache[n].map, MSG_ReadString());
726 hostcache[n].users = MSG_ReadByte();
727 hostcache[n].maxusers = MSG_ReadByte();
729 if (c != NET_PROTOCOL_VERSION)
731 strncpy(hostcache[n].cname, hostcache[n].name, sizeof(hostcache[n].cname) - 1);
732 hostcache[n].cname[sizeof(hostcache[n].cname) - 1] = 0;
733 strcpy(hostcache[n].name, "*");
734 strncat(hostcache[n].name, hostcache[n].cname, sizeof(hostcache[n].name) - 1);
735 hostcache[n].name[sizeof(hostcache[n].name) - 1] = 0;
737 strcpy(hostcache[n].cname, cname);
741 case CCREP_PLAYER_INFO:
742 // we got a CCREP_PLAYER_INFO??
743 //if (developer.integer)
744 Con_Printf("Datagram_ParseConnectionless: received CCREP_PLAYER_INFO from %s.\n", addressstring2);
746 case CCREP_RULE_INFO:
747 // we got a CCREP_RULE_INFO??
748 //if (developer.integer)
749 Con_Printf("Datagram_ParseConnectionless: received CCREP_RULE_INFO from %s.\n", addressstring2);
755 // we may not have liked the packet, but it was a valid control
756 // packet, so we're done processing this packet now
760 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)
761 CL_ParseServerMessage();
765 void NetConn_ClientFrame(void)
768 lhnetaddress_t peeraddress;
770 NetConn_UpdateServerStuff();
771 if (cls.connect_trying && cls.connect_nextsendtime < realtime)
773 if (cls.connect_remainingtries == 0)
775 cls.connect_trying = false;
776 Host_Error("Connect failed\n");
779 if (cls.connect_nextsendtime)
780 Con_Printf("Still trying...\n");
782 Con_Printf("Trying...\n");
783 cls.connect_nextsendtime = realtime + 1;
784 cls.connect_remainingtries--;
785 SZ_Clear(&net_message);
786 // save space for the header, filled in later
787 MSG_WriteLong(&net_message, 0);
788 MSG_WriteByte(&net_message, CCREQ_CONNECT);
789 MSG_WriteString(&net_message, "QUAKE");
790 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
791 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
792 NetConn_Write(cls.connect_mysocket, net_message.data, net_message.cursize, &cls.connect_address);
793 SZ_Clear(&net_message);
795 for (i = 0;i < cl_numsockets;i++)
796 while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
797 NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
798 if (cls.netcon && realtime > cls.netcon->lastMessageTime + net_messagetimeout.value)
800 Con_Printf("Connection timed out\n");
803 for (conn = netconn_list;conn;conn = conn->next)
804 NetConn_ReSendMessage(conn);
807 extern void SV_ConnectClient(int clientnum, netconn_t *netconnection);
808 int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
810 int i, c, n, ret, clientnum, control, responselength;
813 char *string, response[512], addressstring2[128];
817 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
821 string = (char *)data;
823 if (developer.integer)
825 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
826 Con_Printf("NetConn_ServerParsePacket: %s sent us a command:\n", addressstring2);
827 Com_HexDumpToConsole(data, length);
830 if (length >= 7 && !memcmp(string, "getinfo", 7))
832 const char *challenge = NULL;
833 // If there was a challenge in the getinfo message
834 if (length > 8 && string[7] == ' ')
835 challenge = string + 8;
836 for (i = 0, n = 0;i < svs.maxclients;i++)
837 if (svs.clients[i].active)
839 responselength = snprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
840 "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
841 "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d%s%s",
842 gamename, com_modname, svs.maxclients, n,
843 sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "");
844 // does it fit in the buffer?
845 if (responselength < (int)sizeof(response))
847 if (developer.integer)
848 Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
849 NetConn_Write(mysocket, response, responselength, peeraddress);
854 if (!strncmp(string, "ping", 4))
856 if (developer.integer)
857 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
858 NetConn_Write(mysocket, "\377\377\377\377ack", 7, peeraddress);
861 if (!strncmp(string, "ack", 3))
864 // we may not have liked the packet, but it was a command packet, so
865 // we're done processing this packet now
870 control = BigLong(*((int *)data));
871 if ((control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
876 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
881 //if (developer.integer)
882 Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
883 if (length >= (int)strlen("QUAKE") + 1 + 1)
885 if (memcmp(data, "QUAKE", strlen("QUAKE") + 1) != 0 || (int)data[strlen("QUAKE") + 1] != NET_PROTOCOL_VERSION)
887 //if (developer.integer)
888 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Incompatible version.\" to %s.\n", addressstring2);
890 NetConn_Write(mysocket, "\377\377\377\377reject Incompatible version.", 32, peeraddress);
892 SZ_Clear(&net_message);
893 // save space for the header, filled in later
894 MSG_WriteLong(&net_message, 0);
895 MSG_WriteByte(&net_message, CCREP_REJECT);
896 MSG_WriteString(&net_message, "Incompatible version.\n");
897 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
898 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
899 SZ_Clear(&net_message);
904 // see if this client is already connected
905 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
906 if (client->active && (ret = LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress)) == 0)//>= 0)
908 if (clientnum < svs.maxclients)
910 // is this a duplicate connection request?
911 if (ret == 0 && realtime - client->netconnection->connecttime < 2.0)
913 //if (developer.integer)
914 Con_Printf("Datagram_ParseConnectionless: sending duplicate CCREP_ACCEPT to %s.\n", addressstring2);
915 // yes, so send a duplicate reply
917 NetConn_Write(mysocket, "\377\377\377\377accept", 10, peeraddress);
919 SZ_Clear(&net_message);
920 // save space for the header, filled in later
921 MSG_WriteLong(&net_message, 0);
922 MSG_WriteByte(&net_message, CCREP_ACCEPT);
923 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(client->netconnection->mysocket)));
924 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
925 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
926 SZ_Clear(&net_message);
931 //if (developer.integer)
932 Con_Printf("Datagram_ParseConnectionless: removing crashed/disconnected client #%i \"%s\" (address %s).\n", clientnum, client->name, client->netconnection->address);
933 // it's somebody coming back from a
934 // crash/disconnect so kill old connection
935 // asap and let them retry to get in
936 client->deadsocket = true;
941 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
944 if (clientnum < svs.maxclients && (client->netconnection = conn = NetConn_Open(mysocket, peeraddress)) != NULL)
946 // connect to the client
947 // everything is allocated, just fill in the details
948 strcpy(conn->address, addressstring2);
949 //if (developer.integer)
950 Con_Printf("Datagram_ParseConnectionless: sending CCREP_ACCEPT to %s.\n", addressstring2);
952 NetConn_Write(mysocket, "\377\377\377\377accept", 10, peeraddress);
954 // send back the info about the server connection
955 SZ_Clear(&net_message);
956 // save space for the header, filled in later
957 MSG_WriteLong(&net_message, 0);
958 MSG_WriteByte(&net_message, CCREP_ACCEPT);
959 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(conn->mysocket)));
960 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
961 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
962 SZ_Clear(&net_message);
964 // now set up the client struct
965 SV_ConnectClient(clientnum, conn);
966 NetConn_Heartbeat(1);
970 //if (developer.integer)
971 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Server is full.\" to %s.\n", addressstring2);
972 // no room; try to let player know
974 NetConn_Write(mysocket, "\377\377\377\377reject Server is full.", 26, peeraddress);
976 SZ_Clear(&net_message);
977 // save space for the header, filled in later
978 MSG_WriteLong(&net_message, 0);
979 MSG_WriteByte(&net_message, CCREP_REJECT);
980 MSG_WriteString(&net_message, "Server is full.\n");
981 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
982 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
983 SZ_Clear(&net_message);
991 case CCREQ_SERVER_INFO:
992 if (developer.integer)
993 Con_Printf("Datagram_ParseConnectionless: received CCREQ_SERVER_INFO from %s.\n", addressstring2);
994 if (sv.active && !strcmp(MSG_ReadString(), "QUAKE"))
996 if (developer.integer)
997 Con_Printf("Datagram_ParseConnectionless: sending CCREP_SERVER_INFO to %s.\n", addressstring2);
998 SZ_Clear(&net_message);
999 // save space for the header, filled in later
1000 MSG_WriteLong(&net_message, 0);
1001 MSG_WriteByte(&net_message, CCREP_SERVER_INFO);
1002 UDP_GetSocketAddr(UDP_acceptSock, &newaddr);
1003 MSG_WriteString(&net_message, UDP_AddrToString(&newaddr));
1004 MSG_WriteString(&net_message, hostname.string);
1005 MSG_WriteString(&net_message, sv.name);
1006 MSG_WriteByte(&net_message, net_activeconnections);
1007 MSG_WriteByte(&net_message, svs.maxclients);
1008 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1009 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1010 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1011 SZ_Clear(&net_message);
1014 case CCREQ_PLAYER_INFO:
1015 if (developer.integer)
1016 Con_Printf("Datagram_ParseConnectionless: received CCREQ_PLAYER_INFO from %s.\n", addressstring2);
1019 int playerNumber, activeNumber, clientNumber;
1022 playerNumber = MSG_ReadByte();
1024 for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++)
1025 if (client->active && ++activeNumber == playerNumber)
1027 if (clientNumber != svs.maxclients)
1029 SZ_Clear(&net_message);
1030 // save space for the header, filled in later
1031 MSG_WriteLong(&net_message, 0);
1032 MSG_WriteByte(&net_message, CCREP_PLAYER_INFO);
1033 MSG_WriteByte(&net_message, playerNumber);
1034 MSG_WriteString(&net_message, client->name);
1035 MSG_WriteLong(&net_message, client->colors);
1036 MSG_WriteLong(&net_message, (int)client->edict->v->frags);
1037 MSG_WriteLong(&net_message, (int)(realtime - client->netconnection->connecttime));
1038 MSG_WriteString(&net_message, client->netconnection->address);
1039 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1040 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1041 SZ_Clear(&net_message);
1045 case CCREQ_RULE_INFO:
1046 if (developer.integer)
1047 Con_Printf("Datagram_ParseConnectionless: received CCREQ_RULE_INFO from %s.\n", addressstring2);
1053 // find the search start location
1054 prevCvarName = MSG_ReadString();
1055 var = Cvar_FindVarAfter(prevCvarName, CVAR_NOTIFY);
1057 // send the response
1058 SZ_Clear(&net_message);
1059 // save space for the header, filled in later
1060 MSG_WriteLong(&net_message, 0);
1061 MSG_WriteByte(&net_message, CCREP_RULE_INFO);
1064 MSG_WriteString(&net_message, var->name);
1065 MSG_WriteString(&net_message, var->string);
1067 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1068 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1069 SZ_Clear(&net_message);
1077 // we may not have liked the packet, but it was a valid control
1078 // packet, so we're done processing this packet now
1082 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1084 if (host_client->active && host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
1086 sv_player = host_client->edict;
1087 if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
1088 SV_ReadClientMessage();
1096 void NetConn_ServerFrame(void)
1099 lhnetaddress_t peeraddress;
1101 NetConn_UpdateServerStuff();
1102 for (i = 0;i < sv_numsockets;i++)
1103 while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
1104 NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress);
1105 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++)
1107 if (host_client->active && realtime > host_client->netconnection->lastMessageTime + net_messagetimeout.value)
1109 Con_Printf("Client \"%s\" connection timed out\n", host_client->name);
1110 sv_player = host_client->edict;
1111 SV_DropClient(false);
1114 for (conn = netconn_list;conn;conn = conn->next)
1115 NetConn_ReSendMessage(conn);
1118 void NetConn_QueryMasters(void)
1123 lhnetaddress_t masteraddress;
1126 if (hostCacheCount >= HOSTCACHESIZE)
1129 for (i = 0;i < cl_numsockets;i++)
1136 UDP_Broadcast(UDP_controlSock, "\377\377\377\377getinfo", 11);
1138 SZ_Clear(&net_message);
1139 // save space for the header, filled in later
1140 MSG_WriteLong(&net_message, 0);
1141 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1142 MSG_WriteString(&net_message, "QUAKE");
1143 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1144 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1145 UDP_Broadcast(UDP_controlSock, net_message.data, net_message.cursize);
1146 SZ_Clear(&net_message);
1150 // build the getservers
1151 requestlen = snprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
1154 for (masternum = 0;sv_masters[masternum].name;masternum++)
1155 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
1156 NetConn_Write(cl_sockets[i], request, requestlen, &masteraddress);
1161 void NetConn_Heartbeat(int priority)
1163 lhnetaddress_t masteraddress;
1165 char *request = "\377\377\377\377heartbeat DarkPlaces\x0A";
1166 int requestlen = strlen(request);
1167 lhnetsocket_t *mysocket;
1169 // if it's a state change (client connected), limit next heartbeat to no
1170 // more than 30 sec in the future
1171 if (priority == 1 && nextheartbeattime > realtime + 30.0)
1172 nextheartbeattime = realtime + 30.0;
1174 // limit heartbeatperiod to 30 to 270 second range,
1175 // lower limit is to avoid abusing master servers with excess traffic,
1176 // upper limit is to avoid timing out on the master server (which uses
1178 if (sv_heartbeatperiod.value < 30)
1179 Cvar_SetValueQuick(&sv_heartbeatperiod, 30);
1180 if (sv_heartbeatperiod.value > 270)
1181 Cvar_SetValueQuick(&sv_heartbeatperiod, 270);
1183 // make advertising optional and don't advertise singleplayer games, and
1184 // only send a heartbeat as often as the admin wants
1185 if (sv.active && sv_public.integer && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
1187 nextheartbeattime = realtime + sv_heartbeatperiod.value;
1188 for (masternum = 0;sv_masters[masternum].name;masternum++)
1189 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && (mysocket = NetConn_ChooseServerSocketForAddress(&masteraddress)))
1190 NetConn_Write(mysocket, request, requestlen, &masteraddress);
1194 int NetConn_SendToAll(sizebuf_t *data, double blocktime)
1197 qbyte state[MAX_SCOREBOARD];
1199 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1202 // simultaneously wait for the first CanSendMessage and send the message,
1203 // then wait for a second CanSendMessage (verifying it was received)
1204 blocktime += Sys_DoubleTime();
1208 NetConn_ClientFrame();
1209 NetConn_ServerFrame();
1210 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1212 if (host_client->active && state[i] < 2)
1215 // need to send to this one
1216 if (NetConn_CanSendMessage(host_client->netconnection))
1218 if (state[i] == 0 && NetConn_SendReliableMessage(host_client->netconnection, data) == -1)
1219 state[i] = 2; // connection lost
1225 while (count && Sys_DoubleTime() < blocktime);
1229 static void MaxPlayers_f(void)
1233 if (Cmd_Argc() != 2)
1235 Con_Printf("\"maxplayers\" is \"%u\"\n", svs.maxclients);
1241 Con_Printf("maxplayers can not be changed while a server is running.\n");
1245 n = atoi(Cmd_Argv(1));
1246 n = bound(1, n, MAX_SCOREBOARD);
1247 if (svs.maxclients != n)
1248 Con_Printf("\"maxplayers\" set to \"%u\"\n", n);
1250 SV_SetMaxClients(n);
1253 static void Net_Heartbeat_f(void)
1255 NetConn_Heartbeat(2);
1258 void PrintStats(netconn_t *conn)
1260 Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence);
1263 void Net_Stats_f(void)
1266 Con_Printf("unreliable messages sent = %i\n", unreliableMessagesSent);
1267 Con_Printf("unreliable messages recv = %i\n", unreliableMessagesReceived);
1268 Con_Printf("reliable messages sent = %i\n", reliableMessagesSent);
1269 Con_Printf("reliable messages received = %i\n", reliableMessagesReceived);
1270 Con_Printf("packetsSent = %i\n", packetsSent);
1271 Con_Printf("packetsReSent = %i\n", packetsReSent);
1272 Con_Printf("packetsReceived = %i\n", packetsReceived);
1273 Con_Printf("receivedDuplicateCount = %i\n", receivedDuplicateCount);
1274 Con_Printf("droppedDatagrams = %i\n", droppedDatagrams);
1275 Con_Printf("connections =\n");
1276 for (conn = netconn_list;conn;conn = conn->next)
1280 void Net_Slist_f(void)
1283 memset(&pingcache, 0, sizeof(pingcache));
1284 if (m_state != m_slist)
1285 Con_Printf("Sending requests to master servers\n");
1286 NetConn_QueryMasters();
1287 if (m_state != m_slist)
1288 Con_Printf("Listening for replies...\n");
1291 void NetConn_Init(void)
1294 netconn_mempool = Mem_AllocPool("Networking");
1295 Cmd_AddCommand("net_stats", Net_Stats_f);
1296 Cmd_AddCommand("net_slist", Net_Slist_f);
1297 Cmd_AddCommand("maxplayers", MaxPlayers_f);
1298 Cmd_AddCommand("heartbeat", Net_Heartbeat_f);
1299 Cvar_RegisterVariable(&net_messagetimeout);
1300 Cvar_RegisterVariable(&hostname);
1301 Cvar_RegisterVariable(&developer_networking);
1302 Cvar_RegisterVariable(&cl_netport);
1303 Cvar_RegisterVariable(&cl_netaddress);
1304 Cvar_RegisterVariable(&cl_netaddress_ipv6);
1305 Cvar_RegisterVariable(&sv_netport);
1306 Cvar_RegisterVariable(&sv_netaddress);
1307 Cvar_RegisterVariable(&sv_netaddress_ipv6);
1308 Cvar_RegisterVariable(&sv_public);
1309 Cvar_RegisterVariable(&sv_heartbeatperiod);
1310 for (masternum = 0;sv_masters[masternum].name;masternum++)
1311 Cvar_RegisterVariable(&sv_masters[masternum]);
1314 memset(&pingcache, 0, sizeof(pingcache));
1315 SZ_Alloc(&net_message, NET_MAXMESSAGE, "net_message");
1319 void NetConn_Shutdown(void)
1321 NetConn_CloseClientPorts();
1322 NetConn_CloseServerPorts();