2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // This is enables a simple IP banning mechanism
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #define AF_INET 2 /* internet */
37 struct { unsigned char s_b1,s_b2,s_b3,s_b4; } S_un_b;
38 struct { unsigned short s_w1,s_w2; } S_un_w;
42 #define s_addr S_un.S_addr /* can be used for most tcp & ip code */
46 unsigned short sin_port;
47 struct in_addr sin_addr;
50 char *inet_ntoa(struct in_addr in);
51 unsigned long inet_addr(const char *cp);
57 #include "net_master.h"
59 cvar_t cl_port = {CVAR_SAVE, "cl_port", "0"};
61 // these two macros are to make the code more readable
62 #define sfunc net_landrivers[sock->landriver]
63 #define dfunc net_landrivers[net_landriverlevel]
65 static int net_landriverlevel;
67 /* statistic counters */
69 int packetsReSent = 0;
70 int packetsReceived = 0;
71 int receivedDuplicateCount = 0;
72 int shortPacketCount = 0;
75 static int myDriverLevel;
80 unsigned int sequence;
81 qbyte data[MAX_DATAGRAM];
86 char *StrAddr (struct qsockaddr *addr)
89 qbyte *p = (qbyte *)addr;
92 for (n = 0; n < 16; n++)
93 sprintf (buf + n * 2, "%02x", *p++);
100 unsigned long banAddr = 0x00000000;
101 unsigned long banMask = 0xffffffff;
103 void NET_Ban_f (void)
107 void (*print) (const char *fmt, ...);
109 if (cmd_source == src_command)
113 Cmd_ForwardToServer ();
120 if (pr_global_struct->deathmatch)
122 print = SV_ClientPrintf;
128 if (((struct in_addr *)&banAddr)->s_addr)
130 strcpy(addrStr, inet_ntoa(*(struct in_addr *)&banAddr));
131 strcpy(maskStr, inet_ntoa(*(struct in_addr *)&banMask));
132 print("Banning %s [%s]\n", addrStr, maskStr);
135 print("Banning not active\n");
139 if (Q_strcasecmp(Cmd_Argv(1), "off") == 0)
140 banAddr = 0x00000000;
142 banAddr = inet_addr(Cmd_Argv(1));
143 banMask = 0xffffffff;
147 banAddr = inet_addr(Cmd_Argv(1));
148 banMask = inet_addr(Cmd_Argv(2));
152 print("BAN ip_address [mask]\n");
159 int Datagram_SendMessage (qsocket_t *sock, sizebuf_t *data)
161 unsigned int packetLen;
162 unsigned int dataLen;
166 if (data->cursize == 0)
167 Sys_Error("Datagram_SendMessage: zero length message\n");
169 if (data->cursize > NET_MAXMESSAGE)
170 Sys_Error("Datagram_SendMessage: message too big %u\n", data->cursize);
172 if (sock->canSend == false)
173 Sys_Error("SendMessage: called with canSend == false\n");
176 memcpy(sock->sendMessage, data->data, data->cursize);
177 sock->sendMessageLength = data->cursize;
179 if (data->cursize <= MAX_DATAGRAM)
181 dataLen = data->cursize;
186 dataLen = MAX_DATAGRAM;
189 packetLen = NET_HEADERSIZE + dataLen;
191 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
192 packetBuffer.sequence = BigLong(sock->sendSequence++);
193 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
195 sock->canSend = false;
197 if (sfunc.Write (sock->socket, (qbyte *)&packetBuffer, packetLen, &sock->addr) == -1)
200 sock->lastSendTime = net_time;
206 int SendMessageNext (qsocket_t *sock)
208 unsigned int packetLen;
209 unsigned int dataLen;
212 if (sock->sendMessageLength <= MAX_DATAGRAM)
214 dataLen = sock->sendMessageLength;
219 dataLen = MAX_DATAGRAM;
222 packetLen = NET_HEADERSIZE + dataLen;
224 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
225 packetBuffer.sequence = BigLong(sock->sendSequence++);
226 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
228 sock->sendNext = false;
230 if (sfunc.Write (sock->socket, (qbyte *)&packetBuffer, packetLen, &sock->addr) == -1)
233 sock->lastSendTime = net_time;
239 int ReSendMessage (qsocket_t *sock)
241 unsigned int packetLen;
242 unsigned int dataLen;
245 if (sock->sendMessageLength <= MAX_DATAGRAM)
247 dataLen = sock->sendMessageLength;
252 dataLen = MAX_DATAGRAM;
255 packetLen = NET_HEADERSIZE + dataLen;
257 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
258 packetBuffer.sequence = BigLong(sock->sendSequence - 1);
259 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
261 sock->sendNext = false;
263 if (sfunc.Write (sock->socket, (qbyte *)&packetBuffer, packetLen, &sock->addr) == -1)
266 sock->lastSendTime = net_time;
272 qboolean Datagram_CanSendMessage (qsocket_t *sock)
275 SendMessageNext (sock);
277 return sock->canSend;
281 qboolean Datagram_CanSendUnreliableMessage (qsocket_t *sock)
287 int Datagram_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
292 if (data->cursize == 0)
293 Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
295 if (data->cursize > MAX_DATAGRAM)
296 Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
299 packetLen = NET_HEADERSIZE + data->cursize;
301 packetBuffer.length = BigLong(packetLen | NETFLAG_UNRELIABLE);
302 packetBuffer.sequence = BigLong(sock->unreliableSendSequence++);
303 memcpy (packetBuffer.data, data->data, data->cursize);
305 if (sfunc.Write (sock->socket, (qbyte *)&packetBuffer, packetLen, &sock->addr) == -1)
313 int Datagram_GetMessage (qsocket_t *sock)
318 struct qsockaddr readaddr;
319 unsigned int sequence;
323 if ((net_time - sock->lastSendTime) > 1.0)
324 ReSendMessage (sock);
328 length = sfunc.Read (sock->socket, (qbyte *)&packetBuffer, NET_DATAGRAMSIZE, &readaddr);
333 if ((int)length == -1)
335 Con_Printf("Read error\n");
339 if (sfunc.AddrCompare(&readaddr, &sock->addr) != 0)
342 Con_DPrintf("Forged packet received\n");
343 Con_DPrintf("Expected: %s\n", StrAddr (&sock->addr));
344 Con_DPrintf("Received: %s\n", StrAddr (&readaddr));
349 if (length < NET_HEADERSIZE)
355 length = BigLong(packetBuffer.length);
356 flags = length & (~NETFLAG_LENGTH_MASK);
357 length &= NETFLAG_LENGTH_MASK;
359 if (flags & NETFLAG_CTL)
362 sequence = BigLong(packetBuffer.sequence);
365 if (flags & NETFLAG_UNRELIABLE)
367 if (sequence < sock->unreliableReceiveSequence)
369 Con_DPrintf("Got a stale datagram\n");
373 if (sequence != sock->unreliableReceiveSequence)
375 count = sequence - sock->unreliableReceiveSequence;
376 droppedDatagrams += count;
377 Con_DPrintf("Dropped %u datagram(s)\n", count);
379 sock->unreliableReceiveSequence = sequence + 1;
381 length -= NET_HEADERSIZE;
383 SZ_Clear (&net_message);
384 SZ_Write (&net_message, packetBuffer.data, length);
390 if (flags & NETFLAG_ACK)
392 if (sequence != (sock->sendSequence - 1))
394 Con_DPrintf("Stale ACK received\n");
397 if (sequence == sock->ackSequence)
400 if (sock->ackSequence != sock->sendSequence)
401 Con_DPrintf("ack sequencing error\n");
405 Con_DPrintf("Duplicate ACK received\n");
408 sock->sendMessageLength -= MAX_DATAGRAM;
409 if (sock->sendMessageLength > 0)
411 memcpy(sock->sendMessage, sock->sendMessage+MAX_DATAGRAM, sock->sendMessageLength);
412 sock->sendNext = true;
416 sock->sendMessageLength = 0;
417 sock->canSend = true;
422 if (flags & NETFLAG_DATA)
424 packetBuffer.length = BigLong(NET_HEADERSIZE | NETFLAG_ACK);
425 packetBuffer.sequence = BigLong(sequence);
426 sfunc.Write (sock->socket, (qbyte *)&packetBuffer, NET_HEADERSIZE, &readaddr);
428 if (sequence != sock->receiveSequence)
430 receivedDuplicateCount++;
433 sock->receiveSequence++;
435 length -= NET_HEADERSIZE;
437 if (flags & NETFLAG_EOM)
439 SZ_Clear(&net_message);
440 SZ_Write(&net_message, sock->receiveMessage, sock->receiveMessageLength);
441 SZ_Write(&net_message, packetBuffer.data, length);
442 sock->receiveMessageLength = 0;
448 memcpy(sock->receiveMessage + sock->receiveMessageLength, packetBuffer.data, length);
449 sock->receiveMessageLength += length;
455 SendMessageNext (sock);
461 void PrintStats(qsocket_t *s)
463 Con_Printf("canSend = %4u \n", s->canSend);
464 Con_Printf("sendSeq = %4u ", s->sendSequence);
465 Con_Printf("recvSeq = %4u \n", s->receiveSequence);
469 void NET_Stats_f (void)
473 if (Cmd_Argc () == 1)
475 Con_Printf("unreliable messages sent = %i\n", unreliableMessagesSent);
476 Con_Printf("unreliable messages recv = %i\n", unreliableMessagesReceived);
477 Con_Printf("reliable messages sent = %i\n", messagesSent);
478 Con_Printf("reliable messages received = %i\n", messagesReceived);
479 Con_Printf("packetsSent = %i\n", packetsSent);
480 Con_Printf("packetsReSent = %i\n", packetsReSent);
481 Con_Printf("packetsReceived = %i\n", packetsReceived);
482 Con_Printf("receivedDuplicateCount = %i\n", receivedDuplicateCount);
483 Con_Printf("shortPacketCount = %i\n", shortPacketCount);
484 Con_Printf("droppedDatagrams = %i\n", droppedDatagrams);
486 else if (strcmp(Cmd_Argv(1), "*") == 0)
488 for (s = net_activeSockets; s; s = s->next)
493 for (s = net_activeSockets; s; s = s->next)
494 if (Q_strcasecmp(Cmd_Argv(1), s->address) == 0)
503 static qboolean testInProgress = false;
504 static int testPollCount;
505 static int testDriver;
506 static int testSocket;
508 static void Test_Poll(void);
509 PollProcedure testPollProcedure = {NULL, 0.0, Test_Poll};
511 static void Test_Poll(void)
513 struct qsockaddr clientaddr;
524 net_landriverlevel = testDriver;
528 len = dfunc.Read (testSocket, net_message.data, net_message.maxsize, &clientaddr);
529 if (len < (int)sizeof(int))
532 net_message.cursize = len;
535 control = BigLong(*((int *)net_message.data));
539 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
541 if ((control & NETFLAG_LENGTH_MASK) != len)
545 if (c != CCREP_PLAYER_INFO)
546 Sys_Error("Unexpected repsonse to Player Info request\n");
548 playerNumber = MSG_ReadByte();
549 strcpy(name, MSG_ReadString());
550 colors = MSG_ReadLong();
551 frags = MSG_ReadLong();
552 connectTime = MSG_ReadLong();
553 strcpy(address, MSG_ReadString());
555 Con_Printf("%s\n frags:%3i colors:%u %u time:%u\n %s\n", name, frags, colors >> 4, colors & 0x0f, connectTime / 60, address);
561 SchedulePollProcedure(&testPollProcedure, 0.1);
565 dfunc.CloseSocket(testSocket);
566 testInProgress = false;
570 static void Test_f (void)
573 int n, max = MAX_SCOREBOARD;
574 struct qsockaddr sendaddr;
581 if (host && hostCacheCount)
583 for (n = 0; n < hostCacheCount; n++)
584 if (Q_strcasecmp (host, hostcache[n].name) == 0)
586 if (hostcache[n].driver != myDriverLevel)
588 net_landriverlevel = hostcache[n].ldriver;
589 max = hostcache[n].maxusers;
590 memcpy(&sendaddr, &hostcache[n].addr, sizeof(struct qsockaddr));
593 if (n < hostCacheCount)
597 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
599 if (!net_landrivers[net_landriverlevel].initialized)
602 // see if we can resolve the host name
603 if (dfunc.GetAddrFromName(host, &sendaddr) != -1)
606 if (net_landriverlevel == net_numlandrivers)
610 testSocket = dfunc.OpenSocket(0);
611 if (testSocket == -1)
614 testInProgress = true;
616 testDriver = net_landriverlevel;
618 for (n = 0; n < max; n++)
620 SZ_Clear(&net_message);
621 // save space for the header, filled in later
622 MSG_WriteLong(&net_message, 0);
623 MSG_WriteByte(&net_message, CCREQ_PLAYER_INFO);
624 MSG_WriteByte(&net_message, n);
625 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
626 dfunc.Write (testSocket, net_message.data, net_message.cursize, &sendaddr);
628 SZ_Clear(&net_message);
629 SchedulePollProcedure(&testPollProcedure, 0.1);
633 static qboolean test2InProgress = false;
634 static int test2Driver;
635 static int test2Socket;
637 static void Test2_Poll(void);
638 PollProcedure test2PollProcedure = {NULL, 0.0, Test2_Poll};
640 static void Test2_Poll(void)
642 struct qsockaddr clientaddr;
649 net_landriverlevel = test2Driver;
652 len = dfunc.Read (test2Socket, net_message.data, net_message.maxsize, &clientaddr);
653 if (len < (int)sizeof(int))
656 net_message.cursize = len;
659 control = BigLong(*((int *)net_message.data));
663 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
665 if ((control & NETFLAG_LENGTH_MASK) != len)
669 if (c != CCREP_RULE_INFO)
672 strcpy(name, MSG_ReadString());
675 strcpy(value, MSG_ReadString());
677 Con_Printf("%-16.16s %-16.16s\n", name, value);
679 SZ_Clear(&net_message);
680 // save space for the header, filled in later
681 MSG_WriteLong(&net_message, 0);
682 MSG_WriteByte(&net_message, CCREQ_RULE_INFO);
683 MSG_WriteString(&net_message, name);
684 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
685 dfunc.Write (test2Socket, net_message.data, net_message.cursize, &clientaddr);
686 SZ_Clear(&net_message);
689 SchedulePollProcedure(&test2PollProcedure, 0.05);
693 Con_Printf("Unexpected repsonse to Rule Info request\n");
695 dfunc.CloseSocket(test2Socket);
696 test2InProgress = false;
700 static void Test2_f (void)
704 struct qsockaddr sendaddr;
711 if (host && hostCacheCount)
713 for (n = 0; n < hostCacheCount; n++)
714 if (Q_strcasecmp (host, hostcache[n].name) == 0)
716 if (hostcache[n].driver != myDriverLevel)
718 net_landriverlevel = hostcache[n].ldriver;
719 memcpy(&sendaddr, &hostcache[n].addr, sizeof(struct qsockaddr));
722 if (n < hostCacheCount)
726 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
728 if (!net_landrivers[net_landriverlevel].initialized)
731 // see if we can resolve the host name
732 if (dfunc.GetAddrFromName(host, &sendaddr) != -1)
735 if (net_landriverlevel == net_numlandrivers)
739 test2Socket = dfunc.OpenSocket(0);
740 if (test2Socket == -1)
743 test2InProgress = true;
744 test2Driver = net_landriverlevel;
746 SZ_Clear(&net_message);
747 // save space for the header, filled in later
748 MSG_WriteLong(&net_message, 0);
749 MSG_WriteByte(&net_message, CCREQ_RULE_INFO);
750 MSG_WriteString(&net_message, "");
751 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
752 dfunc.Write (test2Socket, net_message.data, net_message.cursize, &sendaddr);
753 SZ_Clear(&net_message);
754 SchedulePollProcedure(&test2PollProcedure, 0.05);
758 int Datagram_Init (void)
763 myDriverLevel = net_driverlevel;
764 Cmd_AddCommand ("net_stats", NET_Stats_f);
765 Cvar_RegisterVariable (&cl_port);
767 if (COM_CheckParm("-nolan"))
770 for (i = 0; i < net_numlandrivers; i++)
772 csock = net_landrivers[i].Init ();
775 net_landrivers[i].initialized = true;
776 net_landrivers[i].controlSock = csock;
780 Cmd_AddCommand ("ban", NET_Ban_f);
782 Cmd_AddCommand ("test", Test_f);
783 Cmd_AddCommand ("test2", Test2_f);
789 void Datagram_Shutdown (void)
794 // shutdown the lan drivers
796 for (i = 0; i < net_numlandrivers; i++)
798 if (net_landrivers[i].initialized)
800 net_landrivers[i].Shutdown ();
801 net_landrivers[i].initialized = false;
807 void Datagram_Close (qsocket_t *sock)
809 sfunc.CloseSocket(sock->socket);
813 void Datagram_Listen (qboolean state)
817 for (i = 0; i < net_numlandrivers; i++)
818 if (net_landrivers[i].initialized)
819 net_landrivers[i].Listen (state);
823 static qsocket_t *_Datagram_CheckNewConnections (void)
825 struct qsockaddr clientaddr;
826 struct qsockaddr newaddr;
837 acceptsock = dfunc.CheckNewConnections();
838 if (acceptsock == -1)
841 SZ_Clear(&net_message);
843 len = dfunc.Read (acceptsock, net_message.data, net_message.maxsize, &clientaddr);
844 if (len < (int)sizeof(int))
846 net_message.cursize = len;
849 control = BigLong(*((int *)net_message.data));
852 // Messages starting by 0xFFFFFFFF are master server messages
853 if ((unsigned int)control == 0xFFFFFFFF)
855 int responsesize = Master_HandleMessage();
856 if (responsesize > 0)
858 dfunc.Write(acceptsock, net_message.data, responsesize, &clientaddr);
859 SZ_Clear(&net_message);
863 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
865 if ((control & NETFLAG_LENGTH_MASK) != len)
868 command = MSG_ReadByte();
869 if (command == CCREQ_SERVER_INFO)
871 if (strcmp(MSG_ReadString(), "QUAKE") != 0)
874 Con_DPrintf("Datagram_CheckNewConnections: received CCREQ_SERVERINFO, replying.\n");
876 SZ_Clear(&net_message);
877 // save space for the header, filled in later
878 MSG_WriteLong(&net_message, 0);
879 MSG_WriteByte(&net_message, CCREP_SERVER_INFO);
880 dfunc.GetSocketAddr(acceptsock, &newaddr);
881 MSG_WriteString(&net_message, dfunc.AddrToString(&newaddr));
882 MSG_WriteString(&net_message, hostname.string);
883 MSG_WriteString(&net_message, sv.name);
884 MSG_WriteByte(&net_message, net_activeconnections);
885 MSG_WriteByte(&net_message, svs.maxclients);
886 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
887 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
888 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
889 SZ_Clear(&net_message);
893 if (command == CCREQ_PLAYER_INFO)
900 playerNumber = MSG_ReadByte();
902 for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++)
907 if (activeNumber == playerNumber)
911 if (clientNumber == svs.maxclients)
914 SZ_Clear(&net_message);
915 // save space for the header, filled in later
916 MSG_WriteLong(&net_message, 0);
917 MSG_WriteByte(&net_message, CCREP_PLAYER_INFO);
918 MSG_WriteByte(&net_message, playerNumber);
919 MSG_WriteString(&net_message, client->name);
920 MSG_WriteLong(&net_message, client->colors);
921 MSG_WriteLong(&net_message, (int)client->edict->v->frags);
922 MSG_WriteLong(&net_message, (int)(net_time - client->netconnection->connecttime));
923 MSG_WriteString(&net_message, client->netconnection->address);
924 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
925 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
926 SZ_Clear(&net_message);
931 if (command == CCREQ_RULE_INFO)
936 // find the search start location
937 prevCvarName = MSG_ReadString();
938 var = Cvar_FindVarAfter(prevCvarName, CVAR_NOTIFY);
942 SZ_Clear(&net_message);
943 // save space for the header, filled in later
944 MSG_WriteLong(&net_message, 0);
945 MSG_WriteByte(&net_message, CCREP_RULE_INFO);
948 MSG_WriteString(&net_message, var->name);
949 MSG_WriteString(&net_message, var->string);
951 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
952 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
953 SZ_Clear(&net_message);
958 if (command != CCREQ_CONNECT)
961 if (strcmp(MSG_ReadString(), "QUAKE") != 0)
965 if (c != NET_PROTOCOL_VERSION)
967 SZ_Clear(&net_message);
968 // save space for the header, filled in later
969 MSG_WriteLong(&net_message, 0);
970 MSG_WriteByte(&net_message, CCREP_REJECT);
971 MSG_WriteString(&net_message, "Incompatible version.\n");
972 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
973 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
974 SZ_Clear(&net_message);
980 if (clientaddr.sa_family == AF_INET)
982 unsigned long testAddr;
983 testAddr = ((struct sockaddr_in *)&clientaddr)->sin_addr.s_addr;
984 if ((testAddr & banMask) == banAddr)
986 SZ_Clear(&net_message);
987 // save space for the header, filled in later
988 MSG_WriteLong(&net_message, 0);
989 MSG_WriteByte(&net_message, CCREP_REJECT);
990 MSG_WriteString(&net_message, "You have been banned.\n");
991 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
992 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
993 SZ_Clear(&net_message);
999 // see if this guy is already connected
1000 for (s = net_activeSockets; s; s = s->next)
1002 if (s->driver != net_driverlevel)
1004 ret = dfunc.AddrCompare(&clientaddr, &s->addr);
1007 // is this a duplicate connection reqeust?
1008 if (ret == 0 && net_time - s->connecttime < 2.0)
1010 // yes, so send a duplicate reply
1011 SZ_Clear(&net_message);
1012 // save space for the header, filled in later
1013 MSG_WriteLong(&net_message, 0);
1014 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1015 dfunc.GetSocketAddr(s->socket, &newaddr);
1016 MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr));
1017 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1018 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1019 SZ_Clear(&net_message);
1022 // it's somebody coming back in from a crash/disconnect
1023 // so close the old qsocket and let their retry get them back in
1029 // allocate a QSocket
1030 sock = NET_NewQSocket ();
1033 // no room; try to let him know
1034 SZ_Clear(&net_message);
1035 // save space for the header, filled in later
1036 MSG_WriteLong(&net_message, 0);
1037 MSG_WriteByte(&net_message, CCREP_REJECT);
1038 MSG_WriteString(&net_message, "Server is full.\n");
1039 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1040 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1041 SZ_Clear(&net_message);
1045 // allocate a network socket
1046 newsock = dfunc.OpenSocket(0);
1049 NET_FreeQSocket(sock);
1053 // connect to the client
1054 if (dfunc.Connect (newsock, &clientaddr) == -1)
1056 dfunc.CloseSocket(newsock);
1057 NET_FreeQSocket(sock);
1061 // everything is allocated, just fill in the details
1062 sock->socket = newsock;
1063 sock->landriver = net_landriverlevel;
1064 sock->addr = clientaddr;
1065 strcpy(sock->address, dfunc.AddrToString(&clientaddr));
1067 // send him back the info about the server connection he has been allocated
1068 SZ_Clear(&net_message);
1069 // save space for the header, filled in later
1070 MSG_WriteLong(&net_message, 0);
1071 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1072 dfunc.GetSocketAddr(newsock, &newaddr);
1073 MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr));
1074 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1075 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1076 SZ_Clear(&net_message);
1081 qsocket_t *Datagram_CheckNewConnections (void)
1083 qsocket_t *ret = NULL;
1085 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1086 if (net_landrivers[net_landriverlevel].initialized)
1087 if ((ret = _Datagram_CheckNewConnections ()) != NULL)
1093 static qboolean Datagram_HandleServerInfo (struct qsockaddr *readaddr)
1095 struct qsockaddr myaddr;
1099 if (net_message.cursize < (int)sizeof(int))
1102 // don't answer our own query
1103 dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
1104 //if (dfunc.AddrCompare(readaddr, &myaddr) >= 0)
1107 // is the cache full?
1108 if (hostCacheCount == HOSTCACHESIZE)
1111 MSG_BeginReading ();
1112 control = BigLong(*((int *)net_message.data));
1116 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
1118 if ((control & NETFLAG_LENGTH_MASK) != net_message.cursize)
1122 if (c != CCREP_SERVER_INFO)
1125 dfunc.GetAddrFromName(MSG_ReadString(), readaddr);
1126 // search the cache for this server
1127 for (n = 0; n < hostCacheCount; n++)
1128 if (dfunc.AddrCompare(readaddr, &hostcache[n].addr) == 0)
1131 // is it already there?
1132 if (n < hostCacheCount)
1137 strcpy(hostcache[n].name, MSG_ReadString());
1138 strcpy(hostcache[n].map, MSG_ReadString());
1139 hostcache[n].users = MSG_ReadByte();
1140 hostcache[n].maxusers = MSG_ReadByte();
1142 if (c != NET_PROTOCOL_VERSION)
1144 strcpy(hostcache[n].cname, hostcache[n].name);
1145 hostcache[n].cname[14] = 0;
1146 strcpy(hostcache[n].name, "*");
1147 strcat(hostcache[n].name, hostcache[n].cname);
1149 memcpy(&hostcache[n].addr, readaddr, sizeof(struct qsockaddr));
1150 hostcache[n].driver = net_driverlevel;
1151 hostcache[n].ldriver = net_landriverlevel;
1152 strcpy(hostcache[n].cname, dfunc.AddrToString(readaddr));
1154 // check for a name conflict
1155 for (i = 0; i < hostCacheCount; i++)
1159 if (Q_strcasecmp (hostcache[n].name, hostcache[i].name) == 0)
1161 i = strlen(hostcache[n].name);
1162 if (i < 15 && hostcache[n].name[i-1] > '8')
1164 hostcache[n].name[i] = '0';
1165 hostcache[n].name[i+1] = 0;
1168 hostcache[n].name[i-1]++;
1177 static void _Datagram_SearchForHosts (qboolean xmit)
1180 struct qsockaddr readaddr;
1184 SZ_Clear(&net_message);
1185 // save space for the header, filled in later
1186 MSG_WriteLong(&net_message, 0);
1187 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1188 MSG_WriteString(&net_message, "QUAKE");
1189 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1190 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1191 dfunc.Broadcast(dfunc.controlSock, net_message.data, net_message.cursize);
1192 SZ_Clear(&net_message);
1195 while ((ret = dfunc.Read (dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0)
1197 net_message.cursize = ret;
1198 Datagram_HandleServerInfo (&readaddr);
1202 void Datagram_SearchForHosts (qboolean xmit)
1204 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1206 if (hostCacheCount == HOSTCACHESIZE)
1208 if (net_landrivers[net_landriverlevel].initialized)
1209 _Datagram_SearchForHosts (xmit);
1214 static qboolean _Datagram_SearchForInetHosts (const char *master)
1216 qboolean result = false;
1217 struct qsockaddr masteraddr;
1218 struct qsockaddr readaddr;
1223 if (dfunc.GetAddrFromName(master, &masteraddr) != -1)
1226 const char* port = strrchr (master, ':');
1228 portnum = atoi (port + 1);
1230 portnum = MASTER_PORT;
1231 Con_DPrintf("Datagram_SearchForInetHosts: sending %d byte message to master %s\n", net_message.cursize, master);
1232 dfunc.SetSocketPort (&masteraddr, portnum);
1233 dfunc.Send (net_message.data, net_message.cursize, &masteraddr);
1237 while ((ret = dfunc.Recv (net_message.data, net_message.maxsize, &readaddr)) > 0)
1239 net_message.cursize = ret;
1240 Con_DPrintf("Datagram_SearchForInetHosts: Recv received %d byte message\n", net_message.cursize);
1241 Master_ParseServerList (&dfunc);
1244 while ((ret = dfunc.Read (dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0)
1246 net_message.cursize = ret;
1247 Con_DPrintf("Datagram_SearchForInetHosts: Read received %d byte message\n", net_message.cursize);
1248 if (Datagram_HandleServerInfo (&readaddr))
1256 qboolean Datagram_SearchForInetHosts (const char *master)
1258 qboolean result = false;
1259 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1261 if (hostCacheCount == HOSTCACHESIZE)
1263 if (net_landrivers[net_landriverlevel].initialized)
1264 if (_Datagram_SearchForInetHosts (master))
1272 static qsocket_t *_Datagram_Connect (const char *host)
1274 struct qsockaddr sendaddr;
1275 struct qsockaddr readaddr;
1284 // see if we can resolve the host name
1285 if (dfunc.GetAddrFromName(host, &sendaddr) == -1)
1288 newsock = dfunc.OpenSocket (cl_port.integer);
1292 sock = NET_NewQSocket ();
1295 sock->socket = newsock;
1296 sock->landriver = net_landriverlevel;
1298 // connect to the host
1299 if (dfunc.Connect (newsock, &sendaddr) == -1)
1302 // send the connection request
1303 Con_Printf("trying...\n");CL_UpdateScreen();CL_UpdateScreen();
1304 start_time = net_time;
1306 for (reps = 0; reps < 3; reps++)
1308 SZ_Clear(&net_message);
1309 // save space for the header, filled in later
1310 MSG_WriteLong(&net_message, 0);
1311 MSG_WriteByte(&net_message, CCREQ_CONNECT);
1312 MSG_WriteString(&net_message, "QUAKE");
1313 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1314 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1315 dfunc.Write (newsock, net_message.data, net_message.cursize, &sendaddr);
1316 SZ_Clear(&net_message);
1319 ret = dfunc.Read (newsock, net_message.data, net_message.maxsize, &readaddr);
1320 // if we got something, validate it
1323 // is it from the right place?
1324 if (sfunc.AddrCompare(&readaddr, &sendaddr) != 0)
1326 Con_DPrintf("wrong reply address\n");
1327 Con_DPrintf("Expected: %s\n", StrAddr (&sendaddr));
1328 Con_DPrintf("Received: %s\n", StrAddr (&readaddr));
1334 if (ret < (int)sizeof(int))
1340 net_message.cursize = ret;
1341 MSG_BeginReading ();
1343 control = BigLong(*((int *)net_message.data));
1350 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
1355 if ((control & NETFLAG_LENGTH_MASK) != ret)
1362 while (ret == 0 && (SetNetTime() - start_time) < 2.5);
1365 Con_Printf("still trying...\n");CL_UpdateScreen();CL_UpdateScreen();
1366 start_time = SetNetTime();
1371 reason = "No Response";
1372 Con_Printf("%s\n", reason);
1373 strcpy(m_return_reason, reason);
1379 reason = "Network Error";
1380 Con_Printf("%s\n", reason);
1381 strcpy(m_return_reason, reason);
1385 ret = MSG_ReadByte();
1386 if (ret == CCREP_REJECT)
1388 reason = MSG_ReadString();
1389 Con_Printf("%s", reason);
1390 strncpy(m_return_reason, reason, 31);
1394 if (ret == CCREP_ACCEPT)
1396 memcpy(&sock->addr, &sendaddr, sizeof(struct qsockaddr));
1397 dfunc.SetSocketPort (&sock->addr, MSG_ReadLong());
1401 reason = "Bad Response";
1402 Con_Printf("%s\n", reason);
1403 strcpy(m_return_reason, reason);
1407 dfunc.GetNameFromAddr (&sendaddr, sock->address);
1409 Con_Printf ("Connection accepted to %s\n", sock->address);
1410 sock->lastMessageTime = SetNetTime();
1412 // switch the connection to the specified address
1413 if (dfunc.Connect (newsock, &sock->addr) == -1)
1415 reason = "Connect to Game failed";
1416 Con_Printf("%s\n", reason);
1417 strcpy(m_return_reason, reason);
1421 m_return_onerror = false;
1425 NET_FreeQSocket(sock);
1427 dfunc.CloseSocket(newsock);
1428 if (m_return_onerror)
1430 key_dest = key_menu;
1431 m_state = m_return_state;
1432 m_return_onerror = false;
1437 qsocket_t *Datagram_Connect (const char *host)
1439 qsocket_t *ret = NULL;
1441 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1442 if (net_landrivers[net_landriverlevel].initialized)
1443 if ((ret = _Datagram_Connect (host)) != NULL)
1448 static void _Datagram_Heartbeat (const char *master)
1450 struct qsockaddr masteraddr;
1454 if (dfunc.GetAddrFromName(master, &masteraddr) == -1)
1458 port = strrchr (master, ':');
1460 portnum = atoi (port + 1);
1462 portnum = MASTER_PORT;
1463 dfunc.SetSocketPort (&masteraddr, portnum);
1465 dfunc.Send (net_message.data, net_message.cursize, &masteraddr);
1468 void Datagram_Heartbeat (const char *master)
1470 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1471 if (net_landrivers[net_landriverlevel].initialized)
1472 _Datagram_Heartbeat (master);