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);
58 // these two macros are to make the code more readable
59 #define sfunc net_landrivers[sock->landriver]
60 #define dfunc net_landrivers[net_landriverlevel]
62 static int net_landriverlevel;
64 /* statistic counters */
66 int packetsReSent = 0;
67 int packetsReceived = 0;
68 int receivedDuplicateCount = 0;
69 int shortPacketCount = 0;
72 static int myDriverLevel;
77 unsigned int sequence;
78 byte data[MAX_DATAGRAM];
83 char *StrAddr (struct qsockaddr *addr)
86 byte *p = (byte *)addr;
89 for (n = 0; n < 16; n++)
90 sprintf (buf + n * 2, "%02x", *p++);
97 unsigned long banAddr = 0x00000000;
98 unsigned long banMask = 0xffffffff;
100 void NET_Ban_f (void)
104 void (*print) (char *fmt, ...);
106 if (cmd_source == src_command)
110 Cmd_ForwardToServer ();
117 if (pr_global_struct->deathmatch)
119 print = SV_ClientPrintf;
125 if (((struct in_addr *)&banAddr)->s_addr)
127 strcpy(addrStr, inet_ntoa(*(struct in_addr *)&banAddr));
128 strcpy(maskStr, inet_ntoa(*(struct in_addr *)&banMask));
129 print("Banning %s [%s]\n", addrStr, maskStr);
132 print("Banning not active\n");
136 if (Q_strcasecmp(Cmd_Argv(1), "off") == 0)
137 banAddr = 0x00000000;
139 banAddr = inet_addr(Cmd_Argv(1));
140 banMask = 0xffffffff;
144 banAddr = inet_addr(Cmd_Argv(1));
145 banMask = inet_addr(Cmd_Argv(2));
149 print("BAN ip_address [mask]\n");
156 int Datagram_SendMessage (qsocket_t *sock, sizebuf_t *data)
158 unsigned int packetLen;
159 unsigned int dataLen;
163 if (data->cursize == 0)
164 Sys_Error("Datagram_SendMessage: zero length message\n");
166 if (data->cursize > NET_MAXMESSAGE)
167 Sys_Error("Datagram_SendMessage: message too big %u\n", data->cursize);
169 if (sock->canSend == false)
170 Sys_Error("SendMessage: called with canSend == false\n");
173 memcpy(sock->sendMessage, data->data, data->cursize);
174 sock->sendMessageLength = data->cursize;
176 if (data->cursize <= MAX_DATAGRAM)
178 dataLen = data->cursize;
183 dataLen = MAX_DATAGRAM;
186 packetLen = NET_HEADERSIZE + dataLen;
188 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
189 packetBuffer.sequence = BigLong(sock->sendSequence++);
190 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
192 sock->canSend = false;
194 if (sfunc.Write (sock->socket, (byte *)&packetBuffer, packetLen, &sock->addr) == -1)
197 sock->lastSendTime = net_time;
203 int SendMessageNext (qsocket_t *sock)
205 unsigned int packetLen;
206 unsigned int dataLen;
209 if (sock->sendMessageLength <= MAX_DATAGRAM)
211 dataLen = sock->sendMessageLength;
216 dataLen = MAX_DATAGRAM;
219 packetLen = NET_HEADERSIZE + dataLen;
221 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
222 packetBuffer.sequence = BigLong(sock->sendSequence++);
223 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
225 sock->sendNext = false;
227 if (sfunc.Write (sock->socket, (byte *)&packetBuffer, packetLen, &sock->addr) == -1)
230 sock->lastSendTime = net_time;
236 int ReSendMessage (qsocket_t *sock)
238 unsigned int packetLen;
239 unsigned int dataLen;
242 if (sock->sendMessageLength <= MAX_DATAGRAM)
244 dataLen = sock->sendMessageLength;
249 dataLen = MAX_DATAGRAM;
252 packetLen = NET_HEADERSIZE + dataLen;
254 packetBuffer.length = BigLong(packetLen | (NETFLAG_DATA | eom));
255 packetBuffer.sequence = BigLong(sock->sendSequence - 1);
256 memcpy (packetBuffer.data, sock->sendMessage, dataLen);
258 sock->sendNext = false;
260 if (sfunc.Write (sock->socket, (byte *)&packetBuffer, packetLen, &sock->addr) == -1)
263 sock->lastSendTime = net_time;
269 qboolean Datagram_CanSendMessage (qsocket_t *sock)
272 SendMessageNext (sock);
274 return sock->canSend;
278 qboolean Datagram_CanSendUnreliableMessage (qsocket_t *sock)
284 int Datagram_SendUnreliableMessage (qsocket_t *sock, sizebuf_t *data)
289 if (data->cursize == 0)
290 Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
292 if (data->cursize > MAX_DATAGRAM)
293 Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
296 packetLen = NET_HEADERSIZE + data->cursize;
298 packetBuffer.length = BigLong(packetLen | NETFLAG_UNRELIABLE);
299 packetBuffer.sequence = BigLong(sock->unreliableSendSequence++);
300 memcpy (packetBuffer.data, data->data, data->cursize);
302 if (sfunc.Write (sock->socket, (byte *)&packetBuffer, packetLen, &sock->addr) == -1)
310 int Datagram_GetMessage (qsocket_t *sock)
315 struct qsockaddr readaddr;
316 unsigned int sequence;
320 if ((net_time - sock->lastSendTime) > 1.0)
321 ReSendMessage (sock);
325 length = sfunc.Read (sock->socket, (byte *)&packetBuffer, NET_DATAGRAMSIZE, &readaddr);
327 // if ((rand() & 255) > 220)
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, (byte *)&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)
490 for (s = net_freeSockets; s; s = s->next)
495 for (s = net_activeSockets; s; s = s->next)
496 if (Q_strcasecmp(Cmd_Argv(1), s->address) == 0)
499 for (s = net_freeSockets; s; s = s->next)
500 if (Q_strcasecmp(Cmd_Argv(1), s->address) == 0)
509 static qboolean testInProgress = false;
510 static int testPollCount;
511 static int testDriver;
512 static int testSocket;
514 static void Test_Poll(void);
515 PollProcedure testPollProcedure = {NULL, 0.0, Test_Poll};
517 static void Test_Poll(void)
519 struct qsockaddr clientaddr;
529 net_landriverlevel = testDriver;
533 len = dfunc.Read (testSocket, net_message.data, net_message.maxsize, &clientaddr);
534 if (len < sizeof(int))
537 net_message.cursize = len;
540 control = BigLong(*((int *)net_message.data));
544 if ((control & (~NETFLAG_LENGTH_MASK)) != NETFLAG_CTL)
546 if ((control & NETFLAG_LENGTH_MASK) != len)
549 if (MSG_ReadByte() != CCREP_PLAYER_INFO)
550 Sys_Error("Unexpected repsonse to Player Info request\n");
552 playerNumber = MSG_ReadByte();
553 strcpy(name, MSG_ReadString());
554 colors = MSG_ReadLong();
555 frags = MSG_ReadLong();
556 connectTime = MSG_ReadLong();
557 strcpy(address, MSG_ReadString());
559 Con_Printf("%s\n frags:%3i colors:%u %u time:%u\n %s\n", name, frags, colors >> 4, colors & 0x0f, connectTime / 60, address);
565 SchedulePollProcedure(&testPollProcedure, 0.1);
569 dfunc.CloseSocket(testSocket);
570 testInProgress = false;
574 static void Test_f (void)
578 int max = MAX_SCOREBOARD;
579 struct qsockaddr sendaddr;
586 if (host && hostCacheCount)
588 for (n = 0; n < hostCacheCount; n++)
589 if (Q_strcasecmp (host, hostcache[n].name) == 0)
591 if (hostcache[n].driver != myDriverLevel)
593 net_landriverlevel = hostcache[n].ldriver;
594 max = hostcache[n].maxusers;
595 memcpy(&sendaddr, &hostcache[n].addr, sizeof(struct qsockaddr));
598 if (n < hostCacheCount)
602 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
604 if (!net_landrivers[net_landriverlevel].initialized)
607 // see if we can resolve the host name
608 if (dfunc.GetAddrFromName(host, &sendaddr) != -1)
611 if (net_landriverlevel == net_numlandrivers)
615 testSocket = dfunc.OpenSocket(0);
616 if (testSocket == -1)
619 testInProgress = true;
621 testDriver = net_landriverlevel;
623 for (n = 0; n < max; n++)
625 SZ_Clear(&net_message);
626 // save space for the header, filled in later
627 MSG_WriteLong(&net_message, 0);
628 MSG_WriteByte(&net_message, CCREQ_PLAYER_INFO);
629 MSG_WriteByte(&net_message, n);
630 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
631 dfunc.Write (testSocket, net_message.data, net_message.cursize, &sendaddr);
633 SZ_Clear(&net_message);
634 SchedulePollProcedure(&testPollProcedure, 0.1);
638 static qboolean test2InProgress = false;
639 static int test2Driver;
640 static int test2Socket;
642 static void Test2_Poll(void);
643 PollProcedure test2PollProcedure = {NULL, 0.0, Test2_Poll};
645 static void Test2_Poll(void)
647 struct qsockaddr clientaddr;
653 net_landriverlevel = test2Driver;
656 len = dfunc.Read (test2Socket, net_message.data, net_message.maxsize, &clientaddr);
657 if (len < sizeof(int))
660 net_message.cursize = len;
663 control = BigLong(*((int *)net_message.data));
667 if ((control & (~NETFLAG_LENGTH_MASK)) != NETFLAG_CTL)
669 if ((control & NETFLAG_LENGTH_MASK) != len)
672 if (MSG_ReadByte() != CCREP_RULE_INFO)
675 strcpy(name, MSG_ReadString());
678 strcpy(value, MSG_ReadString());
680 Con_Printf("%-16.16s %-16.16s\n", name, value);
682 SZ_Clear(&net_message);
683 // save space for the header, filled in later
684 MSG_WriteLong(&net_message, 0);
685 MSG_WriteByte(&net_message, CCREQ_RULE_INFO);
686 MSG_WriteString(&net_message, name);
687 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
688 dfunc.Write (test2Socket, net_message.data, net_message.cursize, &clientaddr);
689 SZ_Clear(&net_message);
692 SchedulePollProcedure(&test2PollProcedure, 0.05);
696 Con_Printf("Unexpected repsonse to Rule Info request\n");
698 dfunc.CloseSocket(test2Socket);
699 test2InProgress = false;
703 static void Test2_f (void)
707 struct qsockaddr sendaddr;
714 if (host && hostCacheCount)
716 for (n = 0; n < hostCacheCount; n++)
717 if (Q_strcasecmp (host, hostcache[n].name) == 0)
719 if (hostcache[n].driver != myDriverLevel)
721 net_landriverlevel = hostcache[n].ldriver;
722 memcpy(&sendaddr, &hostcache[n].addr, sizeof(struct qsockaddr));
725 if (n < hostCacheCount)
729 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
731 if (!net_landrivers[net_landriverlevel].initialized)
734 // see if we can resolve the host name
735 if (dfunc.GetAddrFromName(host, &sendaddr) != -1)
738 if (net_landriverlevel == net_numlandrivers)
742 test2Socket = dfunc.OpenSocket(0);
743 if (test2Socket == -1)
746 test2InProgress = true;
747 test2Driver = net_landriverlevel;
749 SZ_Clear(&net_message);
750 // save space for the header, filled in later
751 MSG_WriteLong(&net_message, 0);
752 MSG_WriteByte(&net_message, CCREQ_RULE_INFO);
753 MSG_WriteString(&net_message, "");
754 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
755 dfunc.Write (test2Socket, net_message.data, net_message.cursize, &sendaddr);
756 SZ_Clear(&net_message);
757 SchedulePollProcedure(&test2PollProcedure, 0.05);
761 int Datagram_Init (void)
766 myDriverLevel = net_driverlevel;
767 Cmd_AddCommand ("net_stats", NET_Stats_f);
769 if (COM_CheckParm("-nolan"))
772 for (i = 0; i < net_numlandrivers; i++)
774 csock = net_landrivers[i].Init ();
777 net_landrivers[i].initialized = true;
778 net_landrivers[i].controlSock = csock;
782 Cmd_AddCommand ("ban", NET_Ban_f);
784 Cmd_AddCommand ("test", Test_f);
785 Cmd_AddCommand ("test2", Test2_f);
791 void Datagram_Shutdown (void)
796 // shutdown the lan drivers
798 for (i = 0; i < net_numlandrivers; i++)
800 if (net_landrivers[i].initialized)
802 net_landrivers[i].Shutdown ();
803 net_landrivers[i].initialized = false;
809 void Datagram_Close (qsocket_t *sock)
811 sfunc.CloseSocket(sock->socket);
815 void Datagram_Listen (qboolean state)
819 for (i = 0; i < net_numlandrivers; i++)
820 if (net_landrivers[i].initialized)
821 net_landrivers[i].Listen (state);
825 static qsocket_t *_Datagram_CheckNewConnections (void)
827 struct qsockaddr clientaddr;
828 struct qsockaddr newaddr;
838 acceptsock = dfunc.CheckNewConnections();
839 if (acceptsock == -1)
842 SZ_Clear(&net_message);
844 len = dfunc.Read (acceptsock, net_message.data, net_message.maxsize, &clientaddr);
845 if (len < sizeof(int))
847 net_message.cursize = len;
850 control = BigLong(*((int *)net_message.data));
854 if ((control & (~NETFLAG_LENGTH_MASK)) != NETFLAG_CTL)
856 if ((control & NETFLAG_LENGTH_MASK) != len)
859 command = MSG_ReadByte();
860 if (command == CCREQ_SERVER_INFO)
862 if (strcmp(MSG_ReadString(), "QUAKE") != 0)
865 SZ_Clear(&net_message);
866 // save space for the header, filled in later
867 MSG_WriteLong(&net_message, 0);
868 MSG_WriteByte(&net_message, CCREP_SERVER_INFO);
869 dfunc.GetSocketAddr(acceptsock, &newaddr);
870 MSG_WriteString(&net_message, dfunc.AddrToString(&newaddr));
871 MSG_WriteString(&net_message, hostname.string);
872 MSG_WriteString(&net_message, sv.name);
873 MSG_WriteByte(&net_message, net_activeconnections);
874 MSG_WriteByte(&net_message, svs.maxclients);
875 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
876 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
877 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
878 SZ_Clear(&net_message);
882 if (command == CCREQ_PLAYER_INFO)
889 playerNumber = MSG_ReadByte();
891 for (clientNumber = 0, client = svs.clients; clientNumber < svs.maxclients; clientNumber++, client++)
896 if (activeNumber == playerNumber)
900 if (clientNumber == svs.maxclients)
903 SZ_Clear(&net_message);
904 // save space for the header, filled in later
905 MSG_WriteLong(&net_message, 0);
906 MSG_WriteByte(&net_message, CCREP_PLAYER_INFO);
907 MSG_WriteByte(&net_message, playerNumber);
908 MSG_WriteString(&net_message, client->name);
909 MSG_WriteLong(&net_message, client->colors);
910 MSG_WriteLong(&net_message, (int)client->edict->v.frags);
911 MSG_WriteLong(&net_message, (int)(net_time - client->netconnection->connecttime));
912 MSG_WriteString(&net_message, client->netconnection->address);
913 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
914 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
915 SZ_Clear(&net_message);
920 if (command == CCREQ_RULE_INFO)
925 // find the search start location
926 prevCvarName = MSG_ReadString();
929 var = Cvar_FindVar (prevCvarName);
937 // search for the next server cvar
940 if (var->flags & CVAR_NOTIFY)
947 SZ_Clear(&net_message);
948 // save space for the header, filled in later
949 MSG_WriteLong(&net_message, 0);
950 MSG_WriteByte(&net_message, CCREP_RULE_INFO);
953 MSG_WriteString(&net_message, var->name);
954 MSG_WriteString(&net_message, var->string);
956 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
957 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
958 SZ_Clear(&net_message);
963 if (command != CCREQ_CONNECT)
966 if (strcmp(MSG_ReadString(), "QUAKE") != 0)
969 if (MSG_ReadByte() != NET_PROTOCOL_VERSION)
971 SZ_Clear(&net_message);
972 // save space for the header, filled in later
973 MSG_WriteLong(&net_message, 0);
974 MSG_WriteByte(&net_message, CCREP_REJECT);
975 MSG_WriteString(&net_message, "Incompatible version.\n");
976 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
977 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
978 SZ_Clear(&net_message);
984 if (clientaddr.sa_family == AF_INET)
986 unsigned long testAddr;
987 testAddr = ((struct sockaddr_in *)&clientaddr)->sin_addr.s_addr;
988 if ((testAddr & banMask) == banAddr)
990 SZ_Clear(&net_message);
991 // save space for the header, filled in later
992 MSG_WriteLong(&net_message, 0);
993 MSG_WriteByte(&net_message, CCREP_REJECT);
994 MSG_WriteString(&net_message, "You have been banned.\n");
995 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
996 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
997 SZ_Clear(&net_message);
1003 // see if this guy is already connected
1004 for (s = net_activeSockets; s; s = s->next)
1006 if (s->driver != net_driverlevel)
1008 ret = dfunc.AddrCompare(&clientaddr, &s->addr);
1011 // is this a duplicate connection reqeust?
1012 if (ret == 0 && net_time - s->connecttime < 2.0)
1014 // yes, so send a duplicate reply
1015 SZ_Clear(&net_message);
1016 // save space for the header, filled in later
1017 MSG_WriteLong(&net_message, 0);
1018 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1019 dfunc.GetSocketAddr(s->socket, &newaddr);
1020 MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr));
1021 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1022 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1023 SZ_Clear(&net_message);
1026 // it's somebody coming back in from a crash/disconnect
1027 // so close the old qsocket and let their retry get them back in
1033 // allocate a QSocket
1034 sock = NET_NewQSocket ();
1037 // no room; try to let him know
1038 SZ_Clear(&net_message);
1039 // save space for the header, filled in later
1040 MSG_WriteLong(&net_message, 0);
1041 MSG_WriteByte(&net_message, CCREP_REJECT);
1042 MSG_WriteString(&net_message, "Server is full.\n");
1043 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1044 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1045 SZ_Clear(&net_message);
1049 // allocate a network socket
1050 newsock = dfunc.OpenSocket(0);
1053 NET_FreeQSocket(sock);
1057 // connect to the client
1058 if (dfunc.Connect (newsock, &clientaddr) == -1)
1060 dfunc.CloseSocket(newsock);
1061 NET_FreeQSocket(sock);
1065 // everything is allocated, just fill in the details
1066 sock->socket = newsock;
1067 sock->landriver = net_landriverlevel;
1068 sock->addr = clientaddr;
1069 strcpy(sock->address, dfunc.AddrToString(&clientaddr));
1071 // send him back the info about the server connection he has been allocated
1072 SZ_Clear(&net_message);
1073 // save space for the header, filled in later
1074 MSG_WriteLong(&net_message, 0);
1075 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1076 dfunc.GetSocketAddr(newsock, &newaddr);
1077 MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr));
1078 // MSG_WriteString(&net_message, dfunc.AddrToString(&newaddr));
1079 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1080 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1081 SZ_Clear(&net_message);
1086 qsocket_t *Datagram_CheckNewConnections (void)
1088 qsocket_t *ret = NULL;
1090 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1091 if (net_landrivers[net_landriverlevel].initialized)
1092 if ((ret = _Datagram_CheckNewConnections ()) != NULL)
1098 static void _Datagram_SearchForHosts (qboolean xmit)
1103 struct qsockaddr readaddr;
1104 struct qsockaddr myaddr;
1107 dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
1110 SZ_Clear(&net_message);
1111 // save space for the header, filled in later
1112 MSG_WriteLong(&net_message, 0);
1113 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1114 MSG_WriteString(&net_message, "QUAKE");
1115 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1116 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1117 dfunc.Broadcast(dfunc.controlSock, net_message.data, net_message.cursize);
1118 SZ_Clear(&net_message);
1121 while ((ret = dfunc.Read (dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0)
1123 if (ret < sizeof(int))
1125 net_message.cursize = ret;
1127 // don't answer our own query
1128 if (dfunc.AddrCompare(&readaddr, &myaddr) >= 0)
1131 // is the cache full?
1132 if (hostCacheCount == HOSTCACHESIZE)
1135 MSG_BeginReading ();
1136 control = BigLong(*((int *)net_message.data));
1140 if ((control & (~NETFLAG_LENGTH_MASK)) != NETFLAG_CTL)
1142 if ((control & NETFLAG_LENGTH_MASK) != ret)
1145 if (MSG_ReadByte() != CCREP_SERVER_INFO)
1148 dfunc.GetAddrFromName(MSG_ReadString(), &readaddr);
1149 // search the cache for this server
1150 for (n = 0; n < hostCacheCount; n++)
1151 if (dfunc.AddrCompare(&readaddr, &hostcache[n].addr) == 0)
1154 // is it already there?
1155 if (n < hostCacheCount)
1160 strcpy(hostcache[n].name, MSG_ReadString());
1161 strcpy(hostcache[n].map, MSG_ReadString());
1162 hostcache[n].users = MSG_ReadByte();
1163 hostcache[n].maxusers = MSG_ReadByte();
1164 if (MSG_ReadByte() != NET_PROTOCOL_VERSION)
1166 strcpy(hostcache[n].cname, hostcache[n].name);
1167 hostcache[n].cname[14] = 0;
1168 strcpy(hostcache[n].name, "*");
1169 strcat(hostcache[n].name, hostcache[n].cname);
1171 memcpy(&hostcache[n].addr, &readaddr, sizeof(struct qsockaddr));
1172 hostcache[n].driver = net_driverlevel;
1173 hostcache[n].ldriver = net_landriverlevel;
1174 strcpy(hostcache[n].cname, dfunc.AddrToString(&readaddr));
1176 // check for a name conflict
1177 for (i = 0; i < hostCacheCount; i++)
1181 if (Q_strcasecmp (hostcache[n].name, hostcache[i].name) == 0)
1183 i = strlen(hostcache[n].name);
1184 if (i < 15 && hostcache[n].name[i-1] > '8')
1186 hostcache[n].name[i] = '0';
1187 hostcache[n].name[i+1] = 0;
1190 hostcache[n].name[i-1]++;
1197 void Datagram_SearchForHosts (qboolean xmit)
1199 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1201 if (hostCacheCount == HOSTCACHESIZE)
1203 if (net_landrivers[net_landriverlevel].initialized)
1204 _Datagram_SearchForHosts (xmit);
1209 static qsocket_t *_Datagram_Connect (char *host)
1211 struct qsockaddr sendaddr;
1212 struct qsockaddr readaddr;
1221 // see if we can resolve the host name
1222 if (dfunc.GetAddrFromName(host, &sendaddr) == -1)
1225 newsock = dfunc.OpenSocket (0);
1229 sock = NET_NewQSocket ();
1232 sock->socket = newsock;
1233 sock->landriver = net_landriverlevel;
1235 // connect to the host
1236 if (dfunc.Connect (newsock, &sendaddr) == -1)
1239 // send the connection request
1240 Con_Printf("trying...\n"); SCR_UpdateScreen ();
1241 start_time = net_time;
1243 for (reps = 0; reps < 3; reps++)
1245 SZ_Clear(&net_message);
1246 // save space for the header, filled in later
1247 MSG_WriteLong(&net_message, 0);
1248 MSG_WriteByte(&net_message, CCREQ_CONNECT);
1249 MSG_WriteString(&net_message, "QUAKE");
1250 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1251 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1252 dfunc.Write (newsock, net_message.data, net_message.cursize, &sendaddr);
1253 SZ_Clear(&net_message);
1256 ret = dfunc.Read (newsock, net_message.data, net_message.maxsize, &readaddr);
1257 // if we got something, validate it
1260 // is it from the right place?
1261 if (sfunc.AddrCompare(&readaddr, &sendaddr) != 0)
1264 Con_DPrintf("wrong reply address\n");
1265 Con_DPrintf("Expected: %s\n", StrAddr (&sendaddr));
1266 Con_DPrintf("Received: %s\n", StrAddr (&readaddr));
1267 SCR_UpdateScreen ();
1273 if (ret < sizeof(int))
1279 net_message.cursize = ret;
1280 MSG_BeginReading ();
1282 control = BigLong(*((int *)net_message.data));
1289 if ((control & (~NETFLAG_LENGTH_MASK)) != NETFLAG_CTL)
1294 if ((control & NETFLAG_LENGTH_MASK) != ret)
1301 while (ret == 0 && (SetNetTime() - start_time) < 2.5);
1304 Con_Printf("still trying...\n"); SCR_UpdateScreen ();
1305 start_time = SetNetTime();
1310 reason = "No Response";
1311 Con_Printf("%s\n", reason);
1312 strcpy(m_return_reason, reason);
1318 reason = "Network Error";
1319 Con_Printf("%s\n", reason);
1320 strcpy(m_return_reason, reason);
1324 ret = MSG_ReadByte();
1325 if (ret == CCREP_REJECT)
1327 reason = MSG_ReadString();
1329 strncpy(m_return_reason, reason, 31);
1333 if (ret == CCREP_ACCEPT)
1335 memcpy(&sock->addr, &sendaddr, sizeof(struct qsockaddr));
1336 dfunc.SetSocketPort (&sock->addr, MSG_ReadLong());
1340 reason = "Bad Response";
1341 Con_Printf("%s\n", reason);
1342 strcpy(m_return_reason, reason);
1346 dfunc.GetNameFromAddr (&sendaddr, sock->address);
1348 Con_Printf ("Connection accepted\n");
1349 sock->lastMessageTime = SetNetTime();
1351 // switch the connection to the specified address
1352 if (dfunc.Connect (newsock, &sock->addr) == -1)
1354 reason = "Connect to Game failed";
1355 Con_Printf("%s\n", reason);
1356 strcpy(m_return_reason, reason);
1360 m_return_onerror = false;
1364 NET_FreeQSocket(sock);
1366 dfunc.CloseSocket(newsock);
1367 if (m_return_onerror)
1369 key_dest = key_menu;
1370 m_state = m_return_state;
1371 m_return_onerror = false;
1376 qsocket_t *Datagram_Connect (char *host)
1378 qsocket_t *ret = NULL;
1380 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1381 if (net_landrivers[net_landriverlevel].initialized)
1382 if ((ret = _Datagram_Connect (host)) != NULL)