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 // note this defaults on for dedicated servers, off for listen servers
29 cvar_t sv_public = {0, "sv_public", "0", "advertises this server on the master server (so that players can find it in the server browser)"};
30 static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "120", "how often to send heartbeat in seconds (only used if sv_public is 1)"};
32 // FIXME: resolve DNS on masters whenever their value changes and cache it (to avoid major delays in active servers when they heartbeat)
33 static cvar_t sv_masters [] =
35 {CVAR_SAVE, "sv_master1", "", "user-chosen master server 1"},
36 {CVAR_SAVE, "sv_master2", "", "user-chosen master server 2"},
37 {CVAR_SAVE, "sv_master3", "", "user-chosen master server 3"},
38 {CVAR_SAVE, "sv_master4", "", "user-chosen master server 4"},
39 {0, "sv_masterextra1", "ghdigital.com", "default master server 1 (admin: LordHavoc)"}, // admin: LordHavoc
40 {0, "sv_masterextra2", "dpmaster.deathmask.net", "default master server 2 (admin: Willis)"}, // admin: Willis
41 {0, "sv_masterextra3", "12.166.196.192", "default master server 3 (admin: Venim)"}, // admin: Venim
42 {0, "sv_masterextra4", "excalibur.nvg.ntnu.no", "default master server 4 (admin: tChr)"}, // admin: tChr
46 static double nextheartbeattime = 0;
48 sizebuf_t net_message;
49 static unsigned char net_message_buf[NET_MAXMESSAGE];
51 cvar_t net_messagetimeout = {0, "net_messagetimeout","300", "drops players who have not sent any packets for this many seconds"};
52 cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10", "give a player this much time in seconds to rejoin and continue playing (not losing frags and such)"};
53 cvar_t net_connecttimeout = {0, "net_connecttimeout","10", "after requesting a connection, the client must reply within this many seconds or be dropped (cuts down on connect floods)"};
54 cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED", "server message to show in server browser"};
55 cvar_t developer_networking = {0, "developer_networking", "0", "prints all received and sent packets (recommended only for debugging)"};
57 cvar_t cl_netlocalping = {0, "cl_netlocalping","0", "lags local loopback connection by this much ping time (useful to play more fairly on your own server with people with higher pings)"};
58 static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0", "drops this percentage of packets (incoming and outgoing), useful for testing network protocol robustness (effects failing to start, sounds failing to play, etc)"};
59 static cvar_t net_slist_queriespersecond = {0, "net_slist_queriespersecond", "20", "how many server information requests to send per second"};
60 static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4", "maximum number of server information requests to send each rendered frame (guards against low framerates causing problems)"};
61 static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"};
62 static cvar_t net_slist_maxtries = {0, "net_slist_maxtries", "3", "how many times to ask the same server for information (more times gives better ping reports but takes longer)"};
64 /* statistic counters */
65 static int packetsSent = 0;
66 static int packetsReSent = 0;
67 static int packetsReceived = 0;
68 static int receivedDuplicateCount = 0;
69 static int droppedDatagrams = 0;
71 static int unreliableMessagesSent = 0;
72 static int unreliableMessagesReceived = 0;
73 static int reliableMessagesSent = 0;
74 static int reliableMessagesReceived = 0;
76 double masterquerytime = -1000;
77 int masterquerycount = 0;
78 int masterreplycount = 0;
79 int serverquerycount = 0;
80 int serverreplycount = 0;
82 // this is only false if there are still servers left to query
83 int serverlist_querysleep = true;
85 static unsigned char sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
86 static unsigned char readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
89 lhnetsocket_t *cl_sockets[16];
91 lhnetsocket_t *sv_sockets[16];
93 netconn_t *netconn_list = NULL;
94 mempool_t *netconn_mempool = NULL;
96 cvar_t cl_netport = {0, "cl_port", "0", "forces client to use chosen port number if not 0"};
97 cvar_t sv_netport = {0, "port", "26000", "server port for players to connect to"};
98 cvar_t net_address = {0, "net_address", "0.0.0.0", "network address to open ports on"};
99 //cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]", "network address to open ipv6 ports on"};
101 // ServerList interface
102 serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
103 serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
105 serverlist_infofield_t serverlist_sortbyfield;
106 qboolean serverlist_sortdescending;
108 int serverlist_viewcount = 0;
109 serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
111 int serverlist_cachecount;
112 serverlist_entry_t serverlist_cache[SERVERLIST_TOTALSIZE];
114 qboolean serverlist_consoleoutput;
116 // helper function to insert a value into the viewset
117 // spare entries will be removed
118 static void _ServerList_ViewList_Helper_InsertBefore( int index, serverlist_entry_t *entry )
121 if( serverlist_viewcount < SERVERLIST_VIEWLISTSIZE ) {
122 i = serverlist_viewcount++;
124 i = SERVERLIST_VIEWLISTSIZE - 1;
127 for( ; i > index ; i-- )
128 serverlist_viewlist[ i ] = serverlist_viewlist[ i - 1 ];
130 serverlist_viewlist[index] = entry;
133 // we suppose serverlist_viewcount to be valid, ie > 0
134 static void _ServerList_ViewList_Helper_Remove( int index )
136 serverlist_viewcount--;
137 for( ; index < serverlist_viewcount ; index++ )
138 serverlist_viewlist[index] = serverlist_viewlist[index + 1];
141 // returns true if A should be inserted before B
142 static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_entry_t *B )
144 int result = 0; // > 0 if for numbers A > B and for text if A < B
146 switch( serverlist_sortbyfield ) {
148 result = A->info.ping - B->info.ping;
150 case SLIF_MAXPLAYERS:
151 result = A->info.maxplayers - B->info.maxplayers;
153 case SLIF_NUMPLAYERS:
154 result = A->info.numplayers - B->info.numplayers;
157 result = A->info.protocol - B->info.protocol;
160 result = strcmp( B->info.cname, A->info.cname );
163 result = strcmp( B->info.game, A->info.game );
166 result = strcmp( B->info.map, A->info.map );
169 result = strcmp( B->info.mod, A->info.mod );
172 result = strcmp( B->info.name, A->info.name );
175 Con_DPrint( "_ServerList_Entry_Compare: Bad serverlist_sortbyfield!\n" );
179 if( serverlist_sortdescending )
184 static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
186 // This should actually be done with some intermediate and end-of-function return
198 case SLMO_GREATEREQUAL:
200 case SLMO_NOTCONTAIN:
203 Con_DPrint( "_ServerList_CompareInt: Bad op!\n" );
208 static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
211 char bufferA[ 256 ], bufferB[ 256 ]; // should be more than enough
212 for (i = 0;i < (int)sizeof(bufferA)-1 && A[i];i++)
213 bufferA[i] = (A[i] >= 'A' && A[i] <= 'Z') ? (A[i] + 'a' - 'A') : A[i];
215 for (i = 0;i < (int)sizeof(bufferB)-1 && B[i];i++)
216 bufferB[i] = (B[i] >= 'A' && B[i] <= 'Z') ? (B[i] + 'a' - 'A') : B[i];
219 // Same here, also using an intermediate & final return would be more appropriate
223 return *bufferB && !!strstr( bufferA, bufferB ); // we want a real bool
224 case SLMO_NOTCONTAIN:
225 return !*bufferB || !strstr( bufferA, bufferB );
227 return strcmp( bufferA, bufferB ) < 0;
229 return strcmp( bufferA, bufferB ) <= 0;
231 return strcmp( bufferA, bufferB ) == 0;
233 return strcmp( bufferA, bufferB ) > 0;
235 return strcmp( bufferA, bufferB ) != 0;
236 case SLMO_GREATEREQUAL:
237 return strcmp( bufferA, bufferB ) >= 0;
239 Con_DPrint( "_ServerList_CompareStr: Bad op!\n" );
244 static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info_t *info )
246 if( !_ServerList_CompareInt( info->ping, mask->tests[SLIF_PING], mask->info.ping ) )
248 if( !_ServerList_CompareInt( info->maxplayers, mask->tests[SLIF_MAXPLAYERS], mask->info.maxplayers ) )
250 if( !_ServerList_CompareInt( info->numplayers, mask->tests[SLIF_NUMPLAYERS], mask->info.numplayers ) )
252 if( !_ServerList_CompareInt( info->protocol, mask->tests[SLIF_PROTOCOL], mask->info.protocol ))
254 if( *mask->info.cname
255 && !_ServerList_CompareStr( info->cname, mask->tests[SLIF_CNAME], mask->info.cname ) )
258 && !_ServerList_CompareStr( info->game, mask->tests[SLIF_GAME], mask->info.game ) )
261 && !_ServerList_CompareStr( info->mod, mask->tests[SLIF_MOD], mask->info.mod ) )
264 && !_ServerList_CompareStr( info->map, mask->tests[SLIF_MAP], mask->info.map ) )
267 && !_ServerList_CompareStr( info->name, mask->tests[SLIF_NAME], mask->info.name ) )
272 static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
276 // FIXME: change this to be more readable (...)
277 // now check whether it passes through the masks
278 for( start = 0 ; serverlist_andmasks[start].active && start < SERVERLIST_ANDMASKCOUNT ; start++ )
279 if( !_ServerList_Entry_Mask( &serverlist_andmasks[start], &entry->info ) )
282 for( start = 0 ; serverlist_ormasks[start].active && start < SERVERLIST_ORMASKCOUNT ; start++ )
283 if( _ServerList_Entry_Mask( &serverlist_ormasks[start], &entry->info ) )
285 if( start == SERVERLIST_ORMASKCOUNT || (start > 0 && !serverlist_ormasks[start].active) )
288 if( !serverlist_viewcount ) {
289 _ServerList_ViewList_Helper_InsertBefore( 0, entry );
292 // ok, insert it, we just need to find out where exactly:
295 // check whether to insert it as new first item
296 if( _ServerList_Entry_Compare( entry, serverlist_viewlist[0] ) ) {
297 _ServerList_ViewList_Helper_InsertBefore( 0, entry );
299 } // check whether to insert it as new last item
300 else if( !_ServerList_Entry_Compare( entry, serverlist_viewlist[serverlist_viewcount - 1] ) ) {
301 _ServerList_ViewList_Helper_InsertBefore( serverlist_viewcount, entry );
305 end = serverlist_viewcount - 1;
306 while( end > start + 1 )
308 mid = (start + end) / 2;
309 // test the item that lies in the middle between start and end
310 if( _ServerList_Entry_Compare( entry, serverlist_viewlist[mid] ) )
311 // the item has to be in the upper half
314 // the item has to be in the lower half
317 _ServerList_ViewList_Helper_InsertBefore( start + 1, entry );
320 static void ServerList_ViewList_Remove( serverlist_entry_t *entry )
323 for( i = 0; i < serverlist_viewcount; i++ )
325 if (serverlist_viewlist[i] == entry)
327 _ServerList_ViewList_Helper_Remove(i);
333 void ServerList_RebuildViewList(void)
337 serverlist_viewcount = 0;
338 for( i = 0 ; i < serverlist_cachecount ; i++ )
339 if( serverlist_cache[i].query == SQS_QUERIED )
340 ServerList_ViewList_Insert( &serverlist_cache[i] );
343 void ServerList_ResetMasks(void)
345 memset( &serverlist_andmasks, 0, sizeof( serverlist_andmasks ) );
346 memset( &serverlist_ormasks, 0, sizeof( serverlist_ormasks ) );
350 static void _ServerList_Test(void)
353 for( i = 0 ; i < 1024 ; i++ ) {
354 memset( &serverlist_cache[serverlist_cachecount], 0, sizeof( serverlist_entry_t ) );
355 serverlist_cache[serverlist_cachecount].info.ping = 1000 + 1024 - i;
356 dpsnprintf( serverlist_cache[serverlist_cachecount].info.name, sizeof(serverlist_cache[serverlist_cachecount].info.name), "Black's ServerList Test %i", i );
357 serverlist_cache[serverlist_cachecount].finished = true;
358 sprintf( serverlist_cache[serverlist_cachecount].line1, "%i %s", serverlist_cache[serverlist_cachecount].info.ping, serverlist_cache[serverlist_cachecount].info.name );
359 ServerList_ViewList_Insert( &serverlist_cache[serverlist_cachecount] );
360 serverlist_cachecount++;
365 void ServerList_QueryList(void)
367 masterquerytime = realtime;
368 masterquerycount = 0;
369 masterreplycount = 0;
370 serverquerycount = 0;
371 serverreplycount = 0;
372 serverlist_cachecount = 0;
373 serverlist_viewcount = 0;
374 serverlist_consoleoutput = false;
376 //_ServerList_Test();
378 NetConn_QueryMasters();
383 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress)
385 int length = LHNET_Read(mysocket, data, maxlength, peeraddress);
389 if (cl_netpacketloss.integer)
390 for (i = 0;i < cl_numsockets;i++)
391 if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
393 if (developer_networking.integer)
395 char addressstring[128], addressstring2[128];
396 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
399 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
400 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i from %s:\n", mysocket, addressstring, data, maxlength, peeraddress, length, addressstring2);
401 Com_HexDumpToConsole((unsigned char *)data, length);
404 Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i\n", mysocket, addressstring, data, maxlength, peeraddress, length);
409 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress)
413 if (cl_netpacketloss.integer)
414 for (i = 0;i < cl_numsockets;i++)
415 if (cl_sockets[i] == mysocket && (rand() % 100) < cl_netpacketloss.integer)
417 ret = LHNET_Write(mysocket, data, length, peeraddress);
418 if (developer_networking.integer)
420 char addressstring[128], addressstring2[128];
421 LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
422 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
423 Con_Printf("LHNET_Write(%p (%s), %p, %i, %p (%s)) = %i%s\n", mysocket, addressstring, data, length, peeraddress, addressstring2, length, ret == length ? "" : " (ERROR)");
424 Com_HexDumpToConsole((unsigned char *)data, length);
429 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress)
431 // note this does not include the trailing NULL because we add that in the parser
432 return NetConn_Write(mysocket, string, (int)strlen(string), peeraddress);
435 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
437 unsigned int packetLen;
438 unsigned int dataLen;
440 unsigned int *header;
442 // if a reliable message fragment has been lost, send it again
443 if (!conn->canSend && conn->sendMessageLength && (realtime - conn->lastSendTime) > 1.0)
445 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
447 dataLen = conn->sendMessageLength;
452 dataLen = MAX_PACKETFRAGMENT;
456 packetLen = NET_HEADERSIZE + dataLen;
458 header = (unsigned int *)sendbuffer;
459 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
460 header[1] = BigLong(conn->sendSequence - 1);
461 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
463 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
465 conn->lastSendTime = realtime;
470 // if we have a new reliable message to send, do so
471 if (conn->canSend && conn->message.cursize)
473 if (conn->message.cursize > (int)sizeof(conn->sendMessage))
475 Con_Printf("NetConn_SendUnreliableMessage: reliable message too big (%u > %u)\n", conn->message.cursize, sizeof(conn->sendMessage));
476 conn->message.overflowed = true;
480 if (developer_networking.integer && conn == cls.netcon)
482 Con_Print("client sending reliable message to server:\n");
483 SZ_HexDumpToConsole(&conn->message);
486 memcpy(conn->sendMessage, conn->message.data, conn->message.cursize);
487 conn->sendMessageLength = conn->message.cursize;
488 SZ_Clear(&conn->message);
490 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
492 dataLen = conn->sendMessageLength;
497 dataLen = MAX_PACKETFRAGMENT;
501 packetLen = NET_HEADERSIZE + dataLen;
503 header = (unsigned int *)sendbuffer;
504 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
505 header[1] = BigLong(conn->sendSequence);
506 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
508 conn->sendSequence++;
509 conn->canSend = false;
511 NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
513 conn->lastSendTime = realtime;
515 reliableMessagesSent++;
518 // if we have an unreliable message to send, do so
521 packetLen = NET_HEADERSIZE + data->cursize;
523 if (packetLen > (int)sizeof(sendbuffer))
525 Con_Printf("NetConn_SendUnreliableMessage: message too big %u\n", data->cursize);
529 header = (unsigned int *)sendbuffer;
530 header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
531 header[1] = BigLong(conn->unreliableSendSequence);
532 memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
534 conn->unreliableSendSequence++;
536 NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
539 unreliableMessagesSent++;
544 void NetConn_CloseClientPorts(void)
546 for (;cl_numsockets > 0;cl_numsockets--)
547 if (cl_sockets[cl_numsockets - 1])
548 LHNET_CloseSocket(cl_sockets[cl_numsockets - 1]);
551 void NetConn_OpenClientPort(const char *addressstring, int defaultport)
553 lhnetaddress_t address;
555 char addressstring2[1024];
556 if (LHNETADDRESS_FromString(&address, addressstring, defaultport))
558 if ((s = LHNET_OpenSocket_Connectionless(&address)))
560 cl_sockets[cl_numsockets++] = s;
561 LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
562 Con_Printf("Client opened a socket on address %s\n", addressstring2);
566 LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
567 Con_Printf("Client failed to open a socket on address %s\n", addressstring2);
571 Con_Printf("Client unable to parse address %s\n", addressstring);
574 void NetConn_OpenClientPorts(void)
577 NetConn_CloseClientPorts();
578 port = bound(0, cl_netport.integer, 65535);
579 if (cl_netport.integer != port)
580 Cvar_SetValueQuick(&cl_netport, port);
581 Con_Printf("Client using port %i\n", port);
582 NetConn_OpenClientPort("local:2", 0);
583 NetConn_OpenClientPort(net_address.string, port);
584 //NetConn_OpenClientPort(net_address_ipv6.string, port);
587 void NetConn_CloseServerPorts(void)
589 for (;sv_numsockets > 0;sv_numsockets--)
590 if (sv_sockets[sv_numsockets - 1])
591 LHNET_CloseSocket(sv_sockets[sv_numsockets - 1]);
594 void NetConn_OpenServerPort(const char *addressstring, int defaultport)
596 lhnetaddress_t address;
599 char addressstring2[1024];
601 for (port = defaultport; port <= defaultport + 100; port++)
603 if (LHNETADDRESS_FromString(&address, addressstring, port))
605 if ((s = LHNET_OpenSocket_Connectionless(&address)))
607 sv_sockets[sv_numsockets++] = s;
608 LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
609 Con_Printf("Server listening on address %s\n", addressstring2);
614 LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
615 Con_Printf("Server failed to open socket on address %s\n", addressstring2);
620 Con_Printf("Server unable to parse address %s\n", addressstring);
621 // if it cant parse one address, it wont be able to parse another for sure
627 void NetConn_OpenServerPorts(int opennetports)
630 NetConn_CloseServerPorts();
631 NetConn_UpdateSockets();
632 port = bound(0, sv_netport.integer, 65535);
635 Con_Printf("Server using port %i\n", port);
636 if (sv_netport.integer != port)
637 Cvar_SetValueQuick(&sv_netport, port);
638 if (cls.state != ca_dedicated)
639 NetConn_OpenServerPort("local:1", 0);
642 NetConn_OpenServerPort(net_address.string, port);
643 //NetConn_OpenServerPort(net_address_ipv6.string, port);
645 if (sv_numsockets == 0)
646 Host_Error("NetConn_OpenServerPorts: unable to open any ports!");
649 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address)
651 int i, a = LHNETADDRESS_GetAddressType(address);
652 for (i = 0;i < cl_numsockets;i++)
653 if (cl_sockets[i] && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])) == a)
654 return cl_sockets[i];
658 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address)
660 int i, a = LHNETADDRESS_GetAddressType(address);
661 for (i = 0;i < sv_numsockets;i++)
662 if (sv_sockets[i] && LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(sv_sockets[i])) == a)
663 return sv_sockets[i];
667 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
670 conn = (netconn_t *)Mem_Alloc(netconn_mempool, sizeof(*conn));
671 conn->mysocket = mysocket;
672 conn->peeraddress = *peeraddress;
673 conn->canSend = true;
674 conn->lastMessageTime = realtime;
675 conn->message.data = conn->messagedata;
676 conn->message.maxsize = sizeof(conn->messagedata);
677 conn->message.cursize = 0;
678 // LordHavoc: (inspired by ProQuake) use a short connect timeout to
679 // reduce effectiveness of connection request floods
680 conn->timeout = realtime + net_connecttimeout.value;
681 LHNETADDRESS_ToString(&conn->peeraddress, conn->address, sizeof(conn->address), true);
682 conn->next = netconn_list;
687 void NetConn_Close(netconn_t *conn)
690 // remove connection from list
691 if (conn == netconn_list)
692 netconn_list = conn->next;
695 for (c = netconn_list;c;c = c->next)
699 c->next = conn->next;
703 // not found in list, we'll avoid crashing here...
711 static int clientport = -1;
712 static int clientport2 = -1;
713 static int hostport = -1;
714 void NetConn_UpdateSockets(void)
716 if (cls.state != ca_dedicated)
718 if (clientport2 != cl_netport.integer)
720 clientport2 = cl_netport.integer;
721 if (cls.state == ca_connected)
722 Con_Print("Changing \"cl_port\" will not take effect until you reconnect.\n");
724 if (cls.state == ca_disconnected && clientport != clientport2)
726 clientport = clientport2;
727 NetConn_CloseClientPorts();
729 if (cl_numsockets == 0)
730 NetConn_OpenClientPorts();
733 if (hostport != sv_netport.integer)
735 hostport = sv_netport.integer;
737 Con_Print("Changing \"port\" will not take effect until \"map\" command is executed.\n");
741 static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length)
745 unsigned int sequence;
750 qlength = (unsigned int)BigLong(((int *)data)[0]);
751 flags = qlength & ~NETFLAG_LENGTH_MASK;
752 qlength &= NETFLAG_LENGTH_MASK;
753 // control packets were already handled
754 if (!(flags & NETFLAG_CTL) && qlength == length)
756 sequence = BigLong(((int *)data)[1]);
760 if (flags & NETFLAG_UNRELIABLE)
762 if (sequence >= conn->unreliableReceiveSequence)
764 if (sequence > conn->unreliableReceiveSequence)
766 count = sequence - conn->unreliableReceiveSequence;
767 droppedDatagrams += count;
768 Con_DPrintf("Dropped %u datagram(s)\n", count);
770 conn->unreliableReceiveSequence = sequence + 1;
771 conn->lastMessageTime = realtime;
772 conn->timeout = realtime + net_messagetimeout.value;
773 unreliableMessagesReceived++;
776 SZ_Clear(&net_message);
777 SZ_Write(&net_message, data, length);
783 Con_DPrint("Got a stale datagram\n");
786 else if (flags & NETFLAG_ACK)
788 if (sequence == (conn->sendSequence - 1))
790 if (sequence == conn->ackSequence)
793 if (conn->ackSequence != conn->sendSequence)
794 Con_DPrint("ack sequencing error\n");
795 conn->lastMessageTime = realtime;
796 conn->timeout = realtime + net_messagetimeout.value;
797 if (conn->sendMessageLength > MAX_PACKETFRAGMENT)
799 unsigned int packetLen;
800 unsigned int dataLen;
802 unsigned int *header;
804 conn->sendMessageLength -= MAX_PACKETFRAGMENT;
805 memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength);
807 if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
809 dataLen = conn->sendMessageLength;
814 dataLen = MAX_PACKETFRAGMENT;
818 packetLen = NET_HEADERSIZE + dataLen;
820 header = (unsigned int *)sendbuffer;
821 header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
822 header[1] = BigLong(conn->sendSequence);
823 memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
825 conn->sendSequence++;
827 if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
829 conn->lastSendTime = realtime;
835 conn->sendMessageLength = 0;
836 conn->canSend = true;
840 Con_DPrint("Duplicate ACK received\n");
843 Con_DPrint("Stale ACK received\n");
846 else if (flags & NETFLAG_DATA)
848 unsigned int temppacket[2];
849 temppacket[0] = BigLong(8 | NETFLAG_ACK);
850 temppacket[1] = BigLong(sequence);
851 NetConn_Write(conn->mysocket, (unsigned char *)temppacket, 8, &conn->peeraddress);
852 if (sequence == conn->receiveSequence)
854 conn->lastMessageTime = realtime;
855 conn->timeout = realtime + net_messagetimeout.value;
856 conn->receiveSequence++;
857 if( conn->receiveMessageLength + length <= (int)sizeof( conn->receiveMessage ) ) {
858 memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
859 conn->receiveMessageLength += length;
861 Con_Printf( "Reliable message (seq: %i) too big for message buffer!\n"
862 "Dropping the message!\n", sequence );
863 conn->receiveMessageLength = 0;
866 if (flags & NETFLAG_EOM)
868 reliableMessagesReceived++;
869 length = conn->receiveMessageLength;
870 conn->receiveMessageLength = 0;
873 SZ_Clear(&net_message);
874 SZ_Write(&net_message, conn->receiveMessage, length);
881 receivedDuplicateCount++;
889 void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
891 cls.connect_trying = false;
892 M_Update_Return_Reason("");
893 // the connection request succeeded, stop current connection and set up a new connection
895 // if we're connecting to a remote server, shut down any local server
896 if (LHNETADDRESS_GetAddressType(peeraddress) != LHNETADDRESSTYPE_LOOP && sv.active)
897 Host_ShutdownServer ();
898 // allocate a net connection to keep track of things
899 cls.netcon = NetConn_Open(mysocket, peeraddress);
900 Con_Printf("Connection accepted to %s\n", cls.netcon->address);
903 cls.demonum = -1; // not in the demo loop now
904 cls.state = ca_connected;
905 cls.signon = 0; // need all the signon messages before playing
908 int NetConn_IsLocalGame(void)
910 if (cls.state == ca_connected && sv.active && cl.maxclients == 1)
915 static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
919 char *string, addressstring2[128], cname[128], ipstring[32];
920 char stringbuf[16384];
922 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
924 // received a command string - strip off the packaging and put it
925 // into our string buffer with NULL termination
928 length = min(length, (int)sizeof(stringbuf) - 1);
929 memcpy(stringbuf, data, length);
930 stringbuf[length] = 0;
933 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
935 if (developer.integer)
937 Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2);
938 Com_HexDumpToConsole(data, length);
941 if (length > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
943 char protocolnames[1400];
944 Protocol_Names(protocolnames, sizeof(protocolnames));
945 Con_Printf("\"%s\" received, sending connect request back to %s\n", string, addressstring2);
946 M_Update_Return_Reason("Got challenge response");
947 NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\protocols\\%s\\challenge\\%s", protocolnames, string + 10), peeraddress);
950 if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying)
952 M_Update_Return_Reason("Accepted");
953 NetConn_ConnectionEstablished(mysocket, peeraddress);
956 if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying)
958 char rejectreason[32];
959 cls.connect_trying = false;
961 length = max(length - 7, (int)sizeof(rejectreason) - 1);
962 memcpy(rejectreason, string, length);
963 rejectreason[length] = 0;
964 M_Update_Return_Reason(rejectreason);
967 if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
969 serverlist_info_t *info;
974 // serverlist only uses text addresses
975 LHNETADDRESS_ToString(peeraddress, cname, sizeof(cname), true);
976 // search the cache for this server and update it
977 for( n = 0; n < serverlist_cachecount; n++ )
978 if( !strcmp( cname, serverlist_cache[n].info.cname ) )
980 if( n == serverlist_cachecount ) {
981 // LAN search doesnt require an answer from the master server so we wont
982 // know the ping nor will it be initialized already...
985 if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
988 memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
989 // store the data the engine cares about (address and ping)
990 strlcpy (serverlist_cache[serverlist_cachecount].info.cname, cname, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
991 serverlist_cache[serverlist_cachecount].info.ping = 100000;
992 serverlist_cache[serverlist_cachecount].querytime = realtime;
993 // if not in the slist menu we should print the server to console
994 if (serverlist_consoleoutput) {
995 Con_Printf("querying %s\n", ipstring);
998 ++serverlist_cachecount;
1001 info = &serverlist_cache[n].info;
1002 if ((s = SearchInfostring(string, "gamename" )) != NULL) strlcpy(info->game, s, sizeof (info->game));else info->game[0] = 0;
1003 if ((s = SearchInfostring(string, "modname" )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0] = 0;
1004 if ((s = SearchInfostring(string, "mapname" )) != NULL) strlcpy(info->map , s, sizeof (info->map ));else info->map[0] = 0;
1005 if ((s = SearchInfostring(string, "hostname" )) != NULL) strlcpy(info->name, s, sizeof (info->name));else info->name[0] = 0;
1006 if ((s = SearchInfostring(string, "protocol" )) != NULL) info->protocol = atoi(s);else info->protocol = -1;
1007 if ((s = SearchInfostring(string, "clients" )) != NULL) info->numplayers = atoi(s);else info->numplayers = 0;
1008 if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);else info->maxplayers = 0;
1010 if (info->ping == 100000)
1013 pingtime = (int)((realtime - serverlist_cache[n].querytime) * 1000.0);
1014 pingtime = bound(0, pingtime, 9999);
1016 info->ping = pingtime;
1018 // legacy/old stuff move it to the menu ASAP
1020 // build description strings for the things users care about
1021 dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "%c%c%5d%c%c%c%3u/%3u %-65.65s", STRING_COLOR_TAG, pingtime >= 300 ? '1' : (pingtime >= 200 ? '3' : '7'), (int)pingtime, STRING_COLOR_TAG, STRING_COLOR_DEFAULT + '0', info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
1022 dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", info->cname, info->game, info->mod, info->map);
1023 if( serverlist_cache[n].query == SQS_QUERIED ) {
1024 ServerList_ViewList_Remove( &serverlist_cache[n] );
1026 // if not in the slist menu we should print the server to console (if wanted)
1027 else if( serverlist_consoleoutput )
1028 Con_Printf("%s\n%s\n", serverlist_cache[n].line1, serverlist_cache[n].line2);
1029 // and finally, update the view set
1030 ServerList_ViewList_Insert( &serverlist_cache[n] );
1031 serverlist_cache[n].query = SQS_QUERIED;
1035 if (!strncmp(string, "getserversResponse\\", 19) && serverlist_cachecount < SERVERLIST_TOTALSIZE)
1037 // Extract the IP addresses
1041 if (serverlist_consoleoutput)
1042 Con_Print("received server list...\n");
1043 while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
1047 dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], (data[5] << 8) | data[6]);
1048 if (developer.integer)
1049 Con_Printf("Requesting info from server %s\n", ipstring);
1050 // ignore the rest of the message if the serverlist is full
1051 if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
1053 // also ignore it if we have already queried it (other master server response)
1054 for( n = 0 ; n < serverlist_cachecount ; n++ )
1055 if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
1057 if( n >= serverlist_cachecount )
1061 memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
1062 // store the data the engine cares about (address and ping)
1063 strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
1064 serverlist_cache[serverlist_cachecount].info.ping = 100000;
1065 serverlist_cache[serverlist_cachecount].query = SQS_QUERYING;
1067 ++serverlist_cachecount;
1070 // move on to next address in packet
1074 // begin or resume serverlist queries
1075 serverlist_querysleep = false;
1079 if (!strncmp(string, "ping", 4))
1081 if (developer.integer)
1082 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
1083 NetConn_WriteString(mysocket, "\377\377\377\377ack", peeraddress);
1086 if (!strncmp(string, "ack", 3))
1089 if (string[0] == 'n')
1092 Con_Printf("QW print command from server at %s:\n", addressstring2, string + 1);
1094 // we may not have liked the packet, but it was a command packet, so
1095 // we're done processing this packet now
1098 // netquake control packets, supported for compatibility only
1099 if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
1104 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1108 if (developer.integer)
1109 Con_Printf("Datagram_ParseConnectionless: received CCREP_ACCEPT from %s.\n", addressstring2);
1110 if (cls.connect_trying)
1112 lhnetaddress_t clientportaddress;
1113 clientportaddress = *peeraddress;
1116 unsigned int port = (data[0] << 0) | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
1119 LHNETADDRESS_SetPort(&clientportaddress, port);
1121 M_Update_Return_Reason("Accepted");
1122 NetConn_ConnectionEstablished(mysocket, &clientportaddress);
1126 if (developer.integer)
1127 Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
1128 cls.connect_trying = false;
1129 M_Update_Return_Reason((char *)data);
1132 case CCREP_SERVER_INFO:
1133 if (developer.integer)
1134 Con_Printf("Datagram_ParseConnectionless: received CCREP_SERVER_INFO from %s.\n", addressstring2);
1135 if (cls.state != ca_dedicated)
1137 // LordHavoc: because the UDP driver reports 0.0.0.0:26000 as the address
1138 // string we just ignore it and keep the real address
1140 // serverlist only uses text addresses
1141 cname = UDP_AddrToString(readaddr);
1142 // search the cache for this server
1143 for (n = 0; n < hostCacheCount; n++)
1144 if (!strcmp(cname, serverlist[n].cname))
1147 if (n == hostCacheCount && hostCacheCount < SERVERLISTSIZE)
1150 memset(&serverlist[n], 0, sizeof(serverlist[n]));
1151 strlcpy (serverlist[n].name, MSG_ReadString(), sizeof (serverlist[n].name));
1152 strlcpy (serverlist[n].map, MSG_ReadString(), sizeof (serverlist[n].map));
1153 serverlist[n].users = MSG_ReadByte();
1154 serverlist[n].maxusers = MSG_ReadByte();
1156 if (c != NET_PROTOCOL_VERSION)
1158 strlcpy (serverlist[n].cname, serverlist[n].name, sizeof (serverlist[n].cname));
1159 strcpy(serverlist[n].name, "*");
1160 strlcat (serverlist[n].name, serverlist[n].cname, sizeof(serverlist[n].name));
1162 strlcpy (serverlist[n].cname, cname, sizeof (serverlist[n].cname));
1166 case CCREP_PLAYER_INFO:
1167 // we got a CCREP_PLAYER_INFO??
1168 //if (developer.integer)
1169 Con_Printf("Datagram_ParseConnectionless: received CCREP_PLAYER_INFO from %s.\n", addressstring2);
1171 case CCREP_RULE_INFO:
1172 // we got a CCREP_RULE_INFO??
1173 //if (developer.integer)
1174 Con_Printf("Datagram_ParseConnectionless: received CCREP_RULE_INFO from %s.\n", addressstring2);
1180 // we may not have liked the packet, but it was a valid control
1181 // packet, so we're done processing this packet now
1185 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)
1186 CL_ParseServerMessage();
1190 void NetConn_QueryQueueFrame(void)
1196 static double querycounter = 0;
1198 if (serverlist_querysleep)
1201 // each time querycounter reaches 1.0 issue a query
1202 querycounter += host_realframetime * net_slist_queriespersecond.value;
1203 maxqueries = (int)querycounter;
1204 maxqueries = bound(0, maxqueries, net_slist_queriesperframe.integer);
1205 querycounter -= maxqueries;
1207 if( maxqueries == 0 ) {
1211 // scan serverlist and issue queries as needed
1212 serverlist_querysleep = true;
1214 timeouttime = realtime - net_slist_timeout.value;
1215 for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ )
1217 serverlist_entry_t *entry = &serverlist_cache[ index ];
1218 if( entry->query != SQS_QUERYING )
1223 serverlist_querysleep = false;
1224 if( entry->querycounter != 0 && entry->querytime > timeouttime )
1229 if( entry->querycounter != (unsigned) net_slist_maxtries.integer )
1231 lhnetaddress_t address;
1234 LHNETADDRESS_FromString(&address, entry->info.cname, 0);
1235 for (socket = 0; socket < cl_numsockets ; socket++) {
1236 NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
1239 entry->querytime = realtime;
1240 entry->querycounter++;
1242 // if not in the slist menu we should print the server to console
1243 if (serverlist_consoleoutput)
1244 Con_Printf("querying %25s (%i. try)\n", entry->info.cname, entry->querycounter);
1250 entry->query = SQS_TIMEDOUT;
1255 void NetConn_ClientFrame(void)
1258 lhnetaddress_t peeraddress;
1259 NetConn_UpdateSockets();
1260 if (cls.connect_trying && cls.connect_nextsendtime < realtime)
1262 if (cls.connect_remainingtries == 0)
1263 M_Update_Return_Reason("Connect: Waiting 10 seconds for reply");
1264 cls.connect_nextsendtime = realtime + 1;
1265 cls.connect_remainingtries--;
1266 if (cls.connect_remainingtries <= -10)
1268 cls.connect_trying = false;
1269 M_Update_Return_Reason("Connect: Failed");
1272 // try challenge first (newer server)
1273 NetConn_WriteString(cls.connect_mysocket, "\377\377\377\377getchallenge", &cls.connect_address);
1274 // then try netquake as a fallback (old server, or netquake)
1275 SZ_Clear(&net_message);
1276 // save space for the header, filled in later
1277 MSG_WriteLong(&net_message, 0);
1278 MSG_WriteByte(&net_message, CCREQ_CONNECT);
1279 MSG_WriteString(&net_message, "QUAKE");
1280 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1281 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1282 NetConn_Write(cls.connect_mysocket, net_message.data, net_message.cursize, &cls.connect_address);
1283 SZ_Clear(&net_message);
1285 for (i = 0;i < cl_numsockets;i++)
1286 while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
1287 NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
1288 NetConn_QueryQueueFrame();
1289 if (cls.netcon && realtime > cls.netcon->timeout)
1291 Con_Print("Connection timed out\n");
1293 Host_ShutdownServer ();
1297 #define MAX_CHALLENGES 128
1300 lhnetaddress_t address;
1304 challenge[MAX_CHALLENGES];
1306 static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
1310 for (i = 0;i < bufferlength - 1;i++)
1314 c = rand () % (127 - 33) + 33;
1315 } while (c == '\\' || c == ';' || c == '"' || c == '%' || c == '/');
1321 static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus)
1323 unsigned int nb_clients = 0, i;
1326 // How many clients are there?
1327 for (i = 0;i < (unsigned int)svs.maxclients;i++)
1328 if (svs.clients[i].active)
1331 // TODO: we should add more information for the full status string
1332 length = dpsnprintf(out_msg, out_size,
1333 "\377\377\377\377%s\x0A"
1334 "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
1335 "\\clients\\%d\\mapname\\%s\\hostname\\%s""\\protocol\\%d"
1338 fullstatus ? "statusResponse" : "infoResponse",
1339 gamename, com_modname, svs.maxclients,
1340 nb_clients, sv.name, hostname.string, NET_PROTOCOL_VERSION,
1341 challenge ? "\\challenge\\" : "", challenge ? challenge : "",
1342 fullstatus ? "\n" : "");
1344 // Make sure it fits in the buffer
1353 ptr = out_msg + length;
1354 left = (int)out_size - length;
1356 for (i = 0;i < (unsigned int)svs.maxclients;i++)
1358 client_t *cl = &svs.clients[i];
1361 int nameind, cleanind;
1363 char cleanname [sizeof(cl->name)];
1365 // Remove all characters '"' and '\' in the player name
1370 curchar = cl->name[nameind++];
1371 if (curchar != '"' && curchar != '\\')
1373 cleanname[cleanind++] = curchar;
1374 if (cleanind == sizeof(cleanname) - 1)
1377 } while (curchar != '\0');
1379 length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
1381 (int)(cl->ping * 1000.0f),
1394 extern void SV_SendServerinfo (client_t *client);
1395 static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
1397 int i, ret, clientnum, best;
1401 char *s, *string, response[1400], addressstring2[128], stringbuf[16384];
1403 // see if we can identify the sender as a local player
1404 // (this is necessary for rcon to send a reliable reply if the client is
1405 // actually on the server, not sending remotely)
1406 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1407 if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
1409 if (i == svs.maxclients)
1414 if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
1416 // received a command string - strip off the packaging and put it
1417 // into our string buffer with NULL termination
1420 length = min(length, (int)sizeof(stringbuf) - 1);
1421 memcpy(stringbuf, data, length);
1422 stringbuf[length] = 0;
1425 if (developer.integer)
1427 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1428 Con_Printf("NetConn_ServerParsePacket: %s sent us a command:\n", addressstring2);
1429 Com_HexDumpToConsole(data, length);
1432 if (length >= 12 && !memcmp(string, "getchallenge", 12))
1434 for (i = 0, best = 0, besttime = realtime;i < MAX_CHALLENGES;i++)
1436 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address))
1438 if (besttime > challenge[i].time)
1439 besttime = challenge[best = i].time;
1441 // if we did not find an exact match, choose the oldest and
1442 // update address and string
1443 if (i == MAX_CHALLENGES)
1446 challenge[i].address = *peeraddress;
1447 NetConn_BuildChallengeString(challenge[i].string, sizeof(challenge[i].string));
1449 challenge[i].time = realtime;
1450 // send the challenge
1451 NetConn_WriteString(mysocket, va("\377\377\377\377challenge %s", challenge[i].string), peeraddress);
1454 if (length > 8 && !memcmp(string, "connect\\", 8))
1458 if ((s = SearchInfostring(string, "challenge")))
1460 // validate the challenge
1461 for (i = 0;i < MAX_CHALLENGES;i++)
1462 if (!LHNETADDRESS_Compare(peeraddress, &challenge[i].address) && !strcmp(challenge[i].string, s))
1464 if (i < MAX_CHALLENGES)
1466 // check engine protocol
1467 if (strcmp(SearchInfostring(string, "protocol"), "darkplaces 3"))
1469 if (developer.integer)
1470 Con_Printf("Datagram_ParseConnectionless: sending \"reject Wrong game protocol.\" to %s.\n", addressstring2);
1471 NetConn_WriteString(mysocket, "\377\377\377\377reject Wrong game protocol.", peeraddress);
1475 // see if this is a duplicate connection request
1476 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1477 if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
1479 if (clientnum < svs.maxclients && realtime - client->connecttime < net_messagerejointimeout.value)
1481 // client is still trying to connect,
1482 // so we send a duplicate reply
1483 if (developer.integer)
1484 Con_Printf("Datagram_ParseConnectionless: sending duplicate accept to %s.\n", addressstring2);
1485 NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
1488 else if (clientnum < svs.maxclients)
1490 if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
1492 // client crashed and is coming back, keep their stuff intact
1493 SV_SendServerinfo(client);
1494 //host_client = client;
1495 //SV_DropClient (true);
1502 // this is a new client, find a slot
1503 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1504 if (!client->active)
1506 if (clientnum < svs.maxclients)
1508 // prepare the client struct
1509 if ((conn = NetConn_Open(mysocket, peeraddress)))
1511 // allocated connection
1512 LHNETADDRESS_ToString(peeraddress, conn->address, sizeof(conn->address), true);
1513 if (developer.integer)
1514 Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
1515 NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
1516 // now set up the client
1518 SV_ConnectClient(clientnum, conn);
1520 NetConn_Heartbeat(1);
1526 if (developer.integer)
1527 Con_Printf("Datagram_ParseConnectionless: sending \"reject Server is full.\" to %s.\n", addressstring2);
1528 NetConn_WriteString(mysocket, "\377\377\377\377reject Server is full.", peeraddress);
1536 if (length >= 7 && !memcmp(string, "getinfo", 7))
1538 const char *challenge = NULL;
1540 // If there was a challenge in the getinfo message
1541 if (length > 8 && string[7] == ' ')
1542 challenge = string + 8;
1544 if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), false))
1546 if (developer.integer)
1547 Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
1548 NetConn_WriteString(mysocket, response, peeraddress);
1552 if (length >= 9 && !memcmp(string, "getstatus", 9))
1554 const char *challenge = NULL;
1556 // If there was a challenge in the getinfo message
1557 if (length > 10 && string[9] == ' ')
1558 challenge = string + 10;
1560 if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), true))
1562 if (developer.integer)
1563 Con_Printf("Sending reply to client %s - %s\n", addressstring2, response);
1564 NetConn_WriteString(mysocket, response, peeraddress);
1568 if (length >= 5 && !memcmp(string, "rcon ", 5))
1571 char *s = string + 5;
1573 for (i = 0;*s > ' ';s++)
1574 if (i < (int)sizeof(password) - 1)
1577 if (password[0] > ' ' && !strcmp(rcon_password.string, password))
1579 // looks like a legitimate rcon command with the correct password
1580 Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s);
1581 rcon_redirect = true;
1582 rcon_redirect_bufferpos = 0;
1583 Cmd_ExecuteString(s, src_command);
1584 rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
1585 rcon_redirect = false;
1586 // print resulting text to client
1587 // if client is playing, send a reliable reply instead of
1591 // if the netconnection is loop, then this is the
1592 // local player on a listen mode server, and it would
1593 // result in duplicate printing to the console
1594 // (not that the local player should be using rcon
1595 // when they have the console)
1596 if (host_client->netconnection && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
1597 SV_ClientPrintf("%s", rcon_redirect_buffer);
1602 dpsnprintf(response, sizeof(response), "\377\377\377\377n%s", rcon_redirect_buffer);
1603 NetConn_WriteString(mysocket, response, peeraddress);
1609 if (!strncmp(string, "ping", 4))
1611 if (developer.integer)
1612 Con_Printf("Received ping from %s, sending ack\n", UDP_AddrToString(readaddr));
1613 NetConn_WriteString(mysocket, "\377\377\377\377ack", peeraddress);
1616 if (!strncmp(string, "ack", 3))
1619 // we may not have liked the packet, but it was a command packet, so
1620 // we're done processing this packet now
1623 // LordHavoc: disabled netquake control packet support in server
1627 // netquake control packets, supported for compatibility only
1628 if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
1633 LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
1637 //if (developer.integer)
1638 Con_Printf("Datagram_ParseConnectionless: received CCREQ_CONNECT from %s.\n", addressstring2);
1639 if (length >= (int)strlen("QUAKE") + 1 + 1)
1641 if (memcmp(data, "QUAKE", strlen("QUAKE") + 1) != 0 || (int)data[strlen("QUAKE") + 1] != NET_PROTOCOL_VERSION)
1643 if (developer.integer)
1644 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Incompatible version.\" to %s.\n", addressstring2);
1645 SZ_Clear(&net_message);
1646 // save space for the header, filled in later
1647 MSG_WriteLong(&net_message, 0);
1648 MSG_WriteByte(&net_message, CCREP_REJECT);
1649 MSG_WriteString(&net_message, "Incompatible version.\n");
1650 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1651 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1652 SZ_Clear(&net_message);
1656 // see if this is a duplicate connection request
1657 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1658 if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
1660 if (clientnum < svs.maxclients)
1662 // duplicate connection request
1663 if (realtime - client->connecttime < 2.0)
1665 // client is still trying to connect,
1666 // so we send a duplicate reply
1667 if (developer.integer)
1668 Con_Printf("Datagram_ParseConnectionless: sending duplicate CCREP_ACCEPT to %s.\n", addressstring2);
1669 SZ_Clear(&net_message);
1670 // save space for the header, filled in later
1671 MSG_WriteLong(&net_message, 0);
1672 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1673 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(client->netconnection->mysocket)));
1674 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1675 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1676 SZ_Clear(&net_message);
1679 else if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
1681 SV_SendServerinfo(client);
1682 // the old client hasn't sent us anything
1683 // in quite a while, so kick off and let
1684 // the retry take care of it...
1685 //host_client = client;
1686 //SV_DropClient (true);
1692 // this is a new client, find a slot
1693 for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
1694 if (!client->active)
1696 if (clientnum < svs.maxclients && (client->netconnection = conn = NetConn_Open(mysocket, peeraddress)) != NULL)
1698 // connect to the client
1699 // everything is allocated, just fill in the details
1700 strlcpy (conn->address, addressstring2, sizeof (conn->address));
1701 if (developer.integer)
1702 Con_Printf("Datagram_ParseConnectionless: sending CCREP_ACCEPT to %s.\n", addressstring2);
1703 // send back the info about the server connection
1704 SZ_Clear(&net_message);
1705 // save space for the header, filled in later
1706 MSG_WriteLong(&net_message, 0);
1707 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1708 MSG_WriteLong(&net_message, LHNETADDRESS_GetPort(LHNET_AddressFromSocket(conn->mysocket)));
1709 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1710 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1711 SZ_Clear(&net_message);
1712 // now set up the client struct
1714 SV_ConnectClient(clientnum, conn);
1716 NetConn_Heartbeat(1);
1720 //if (developer.integer)
1721 Con_Printf("Datagram_ParseConnectionless: sending CCREP_REJECT \"Server is full.\" to %s.\n", addressstring2);
1722 // no room; try to let player know
1723 SZ_Clear(&net_message);
1724 // save space for the header, filled in later
1725 MSG_WriteLong(&net_message, 0);
1726 MSG_WriteByte(&net_message, CCREP_REJECT);
1727 MSG_WriteString(&net_message, "Server is full.\n");
1728 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1729 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1730 SZ_Clear(&net_message);
1737 case CCREQ_SERVER_INFO:
1738 if (developer.integer)
1739 Con_Printf("Datagram_ParseConnectionless: received CCREQ_SERVER_INFO from %s.\n", addressstring2);
1740 if (sv.active && !strcmp(MSG_ReadString(), "QUAKE"))
1742 if (developer.integer)
1743 Con_Printf("Datagram_ParseConnectionless: sending CCREP_SERVER_INFO to %s.\n", addressstring2);
1744 SZ_Clear(&net_message);
1745 // save space for the header, filled in later
1746 MSG_WriteLong(&net_message, 0);
1747 MSG_WriteByte(&net_message, CCREP_SERVER_INFO);
1748 UDP_GetSocketAddr(UDP_acceptSock, &newaddr);
1749 MSG_WriteString(&net_message, UDP_AddrToString(&newaddr));
1750 MSG_WriteString(&net_message, hostname.string);
1751 MSG_WriteString(&net_message, sv.name);
1752 MSG_WriteByte(&net_message, net_activeconnections);
1753 MSG_WriteByte(&net_message, svs.maxclients);
1754 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1755 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1756 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1757 SZ_Clear(&net_message);
1760 case CCREQ_PLAYER_INFO:
1761 if (developer.integer)
1762 Con_Printf("Datagram_ParseConnectionless: received CCREQ_PLAYER_INFO from %s.\n", addressstring2);
1765 int playerNumber, activeNumber, clientNumber;
1768 playerNumber = MSG_ReadByte();
1770 for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++)
1771 if (client->active && ++activeNumber == playerNumber)
1773 if (clientNumber != svs.maxclients)
1775 SZ_Clear(&net_message);
1776 // save space for the header, filled in later
1777 MSG_WriteLong(&net_message, 0);
1778 MSG_WriteByte(&net_message, CCREP_PLAYER_INFO);
1779 MSG_WriteByte(&net_message, playerNumber);
1780 MSG_WriteString(&net_message, client->name);
1781 MSG_WriteLong(&net_message, client->colors);
1782 MSG_WriteLong(&net_message, (int)client->edict->fields.server->frags);
1783 MSG_WriteLong(&net_message, (int)(realtime - client->connecttime));
1784 MSG_WriteString(&net_message, client->netconnection ? client->netconnection->address : "botclient");
1785 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1786 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1787 SZ_Clear(&net_message);
1791 case CCREQ_RULE_INFO:
1792 if (developer.integer)
1793 Con_Printf("Datagram_ParseConnectionless: received CCREQ_RULE_INFO from %s.\n", addressstring2);
1799 // find the search start location
1800 prevCvarName = MSG_ReadString();
1801 var = Cvar_FindVarAfter(prevCvarName, CVAR_NOTIFY);
1803 // send the response
1804 SZ_Clear(&net_message);
1805 // save space for the header, filled in later
1806 MSG_WriteLong(&net_message, 0);
1807 MSG_WriteByte(&net_message, CCREP_RULE_INFO);
1810 MSG_WriteString(&net_message, var->name);
1811 MSG_WriteString(&net_message, var->string);
1813 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1814 NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
1815 SZ_Clear(&net_message);
1822 // we may not have liked the packet, but it was a valid control
1823 // packet, so we're done processing this packet now
1830 if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
1833 SV_ReadClientMessage();
1842 void NetConn_ServerFrame(void)
1845 lhnetaddress_t peeraddress;
1846 NetConn_UpdateSockets();
1847 for (i = 0;i < sv_numsockets;i++)
1848 while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
1849 NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress);
1850 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1852 // never timeout loopback connections
1853 if (host_client->netconnection && realtime > host_client->netconnection->timeout && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
1855 Con_Printf("Client \"%s\" connection timed out\n", host_client->name);
1856 SV_DropClient(false);
1861 void NetConn_QueryMasters(void)
1865 lhnetaddress_t masteraddress;
1866 lhnetaddress_t broadcastaddress;
1869 if (serverlist_cachecount >= SERVERLIST_TOTALSIZE)
1872 // 26000 is the default quake server port, servers on other ports will not
1874 // note this is IPv4-only, I doubt there are IPv6-only LANs out there
1875 LHNETADDRESS_FromString(&broadcastaddress, "255.255.255.255", 26000);
1877 for (i = 0;i < cl_numsockets;i++)
1881 // search LAN for Quake servers
1882 SZ_Clear(&net_message);
1883 // save space for the header, filled in later
1884 MSG_WriteLong(&net_message, 0);
1885 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1886 MSG_WriteString(&net_message, "QUAKE");
1887 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1888 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1889 NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
1890 SZ_Clear(&net_message);
1892 // search LAN for DarkPlaces servers
1893 NetConn_WriteString(cl_sockets[i], "\377\377\377\377getinfo", &broadcastaddress);
1895 // build the getservers message to send to the master servers
1896 dpsnprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
1899 for (masternum = 0;sv_masters[masternum].name;masternum++)
1901 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
1904 NetConn_WriteString(cl_sockets[i], request, &masteraddress);
1909 if (!masterquerycount)
1911 Con_Print("Unable to query master servers, no suitable network sockets active.\n");
1912 M_Update_Return_Reason("No network");
1916 void NetConn_Heartbeat(int priority)
1918 lhnetaddress_t masteraddress;
1920 lhnetsocket_t *mysocket;
1922 // if it's a state change (client connected), limit next heartbeat to no
1923 // more than 30 sec in the future
1924 if (priority == 1 && nextheartbeattime > realtime + 30.0)
1925 nextheartbeattime = realtime + 30.0;
1927 // limit heartbeatperiod to 30 to 270 second range,
1928 // lower limit is to avoid abusing master servers with excess traffic,
1929 // upper limit is to avoid timing out on the master server (which uses
1931 if (sv_heartbeatperiod.value < 30)
1932 Cvar_SetValueQuick(&sv_heartbeatperiod, 30);
1933 if (sv_heartbeatperiod.value > 270)
1934 Cvar_SetValueQuick(&sv_heartbeatperiod, 270);
1936 // make advertising optional and don't advertise singleplayer games, and
1937 // only send a heartbeat as often as the admin wants
1938 if (sv.active && sv_public.integer && svs.maxclients >= 2 && (priority > 1 || realtime > nextheartbeattime))
1940 nextheartbeattime = realtime + sv_heartbeatperiod.value;
1941 for (masternum = 0;sv_masters[masternum].name;masternum++)
1942 if (sv_masters[masternum].string && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, MASTER_PORT) && (mysocket = NetConn_ChooseServerSocketForAddress(&masteraddress)))
1943 NetConn_WriteString(mysocket, "\377\377\377\377heartbeat DarkPlaces\x0A", &masteraddress);
1947 static void Net_Heartbeat_f(void)
1950 NetConn_Heartbeat(2);
1952 Con_Print("No server running, can not heartbeat to master server.\n");
1955 void PrintStats(netconn_t *conn)
1957 Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence);
1960 void Net_Stats_f(void)
1963 Con_Printf("unreliable messages sent = %i\n", unreliableMessagesSent);
1964 Con_Printf("unreliable messages recv = %i\n", unreliableMessagesReceived);
1965 Con_Printf("reliable messages sent = %i\n", reliableMessagesSent);
1966 Con_Printf("reliable messages received = %i\n", reliableMessagesReceived);
1967 Con_Printf("packetsSent = %i\n", packetsSent);
1968 Con_Printf("packetsReSent = %i\n", packetsReSent);
1969 Con_Printf("packetsReceived = %i\n", packetsReceived);
1970 Con_Printf("receivedDuplicateCount = %i\n", receivedDuplicateCount);
1971 Con_Printf("droppedDatagrams = %i\n", droppedDatagrams);
1972 Con_Print("connections =\n");
1973 for (conn = netconn_list;conn;conn = conn->next)
1977 void Net_Slist_f(void)
1979 ServerList_ResetMasks();
1980 serverlist_sortbyfield = SLIF_PING;
1981 serverlist_sortdescending = false;
1982 if (m_state != m_slist) {
1983 Con_Print("Sending requests to master servers\n");
1984 ServerList_QueryList();
1985 serverlist_consoleoutput = true;
1986 Con_Print("Listening for replies...\n");
1988 ServerList_QueryList();
1991 void NetConn_Init(void)
1994 lhnetaddress_t tempaddress;
1995 netconn_mempool = Mem_AllocPool("network connections", 0, NULL);
1996 Cmd_AddCommand("net_stats", Net_Stats_f, "print network statistics");
1997 Cmd_AddCommand("net_slist", Net_Slist_f, "query master series and print all server information");
1998 Cmd_AddCommand("heartbeat", Net_Heartbeat_f, "send a heartbeat to the master server (updates your server information)");
1999 Cvar_RegisterVariable(&net_slist_queriespersecond);
2000 Cvar_RegisterVariable(&net_slist_queriesperframe);
2001 Cvar_RegisterVariable(&net_slist_timeout);
2002 Cvar_RegisterVariable(&net_slist_maxtries);
2003 Cvar_RegisterVariable(&net_messagetimeout);
2004 Cvar_RegisterVariable(&net_messagerejointimeout);
2005 Cvar_RegisterVariable(&net_connecttimeout);
2006 Cvar_RegisterVariable(&cl_netlocalping);
2007 Cvar_RegisterVariable(&cl_netpacketloss);
2008 Cvar_RegisterVariable(&hostname);
2009 Cvar_RegisterVariable(&developer_networking);
2010 Cvar_RegisterVariable(&cl_netport);
2011 Cvar_RegisterVariable(&sv_netport);
2012 Cvar_RegisterVariable(&net_address);
2013 //Cvar_RegisterVariable(&net_address_ipv6);
2014 Cvar_RegisterVariable(&sv_public);
2015 Cvar_RegisterVariable(&sv_heartbeatperiod);
2016 for (i = 0;sv_masters[i].name;i++)
2017 Cvar_RegisterVariable(&sv_masters[i]);
2018 // 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.
2019 if ((i = COM_CheckParm("-ip")) && i + 1 < com_argc)
2021 if (LHNETADDRESS_FromString(&tempaddress, com_argv[i + 1], 0) == 1)
2023 Con_Printf("-ip option used, setting net_address to \"%s\"\n");
2024 Cvar_SetQuick(&net_address, com_argv[i + 1]);
2027 Con_Printf("-ip option used, but unable to parse the address \"%s\"\n", com_argv[i + 1]);
2029 // 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
2030 if (((i = COM_CheckParm("-port")) || (i = COM_CheckParm("-ipport")) || (i = COM_CheckParm("-udpport"))) && i + 1 < com_argc)
2032 i = atoi(com_argv[i + 1]);
2033 if (i >= 0 && i < 65536)
2035 Con_Printf("-port option used, setting port cvar to %i\n", i);
2036 Cvar_SetValueQuick(&sv_netport, i);
2039 Con_Printf("-port option used, but %i is not a valid port number\n", i);
2043 net_message.data = net_message_buf;
2044 net_message.maxsize = sizeof(net_message_buf);
2045 net_message.cursize = 0;
2049 void NetConn_Shutdown(void)
2051 NetConn_CloseClientPorts();
2052 NetConn_CloseServerPorts();