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 (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 (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 (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 (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 // LordHavoc: send from s->socket instead of acceptsock, this
1019 // way routers usually identify the connection correctly
1020 // (thanks to faded for provoking me to recite a lengthy
1021 // explanation of NAT nightmares, and realize this easy
1022 // workaround for quake)
1023 dfunc.Write (s->socket, net_message.data, net_message.cursize, &clientaddr);
1024 SZ_Clear(&net_message);
1027 // it's somebody coming back in from a crash/disconnect
1028 // so close the old qsocket and let their retry get them back in
1034 // allocate a QSocket
1035 sock = NET_NewQSocket ();
1038 // no room; try to let him know
1039 SZ_Clear(&net_message);
1040 // save space for the header, filled in later
1041 MSG_WriteLong(&net_message, 0);
1042 MSG_WriteByte(&net_message, CCREP_REJECT);
1043 MSG_WriteString(&net_message, "Server is full.\n");
1044 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1045 dfunc.Write (acceptsock, net_message.data, net_message.cursize, &clientaddr);
1046 SZ_Clear(&net_message);
1050 // allocate a network socket
1051 newsock = dfunc.OpenSocket(0);
1054 NET_FreeQSocket(sock);
1058 // connect to the client
1059 if (dfunc.Connect (newsock, &clientaddr) == -1)
1061 dfunc.CloseSocket(newsock);
1062 NET_FreeQSocket(sock);
1066 // everything is allocated, just fill in the details
1067 sock->socket = newsock;
1068 sock->landriver = net_landriverlevel;
1069 sock->addr = clientaddr;
1070 strcpy(sock->address, dfunc.AddrToString(&clientaddr));
1072 // send him back the info about the server connection he has been allocated
1073 SZ_Clear(&net_message);
1074 // save space for the header, filled in later
1075 MSG_WriteLong(&net_message, 0);
1076 MSG_WriteByte(&net_message, CCREP_ACCEPT);
1077 dfunc.GetSocketAddr(newsock, &newaddr);
1078 MSG_WriteLong(&net_message, dfunc.GetSocketPort(&newaddr));
1079 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1080 // LordHavoc: send from sock->socket instead of acceptsock, this way routers
1081 // usually identify the connection correctly (thanks to faded for provoking
1082 // me to recite a lengthy explanation of NAT nightmares, and realize this
1083 // easy workaround for quake)
1084 dfunc.Write (sock->socket, net_message.data, net_message.cursize, &clientaddr);
1085 SZ_Clear(&net_message);
1090 qsocket_t *Datagram_CheckNewConnections (void)
1092 qsocket_t *ret = NULL;
1094 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1095 if (net_landrivers[net_landriverlevel].initialized)
1096 if ((ret = _Datagram_CheckNewConnections ()) != NULL)
1102 static qboolean Datagram_HandleServerInfo (struct qsockaddr *readaddr)
1104 //struct qsockaddr myaddr;
1108 if (net_message.cursize < (int)sizeof(int))
1111 // don't answer our own query
1112 //dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
1113 //if (dfunc.AddrCompare(readaddr, &myaddr) >= 0)
1116 // is the cache full?
1117 if (hostCacheCount == HOSTCACHESIZE)
1120 MSG_BeginReading ();
1121 control = BigLong(*((int *)net_message.data));
1125 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
1127 if ((control & NETFLAG_LENGTH_MASK) != net_message.cursize)
1131 if (c != CCREP_SERVER_INFO)
1134 dfunc.GetAddrFromName(MSG_ReadString(), readaddr);
1135 // search the cache for this server
1136 for (n = 0; n < hostCacheCount; n++)
1137 if (dfunc.AddrCompare(readaddr, &hostcache[n].addr) == 0)
1140 // is it already there?
1141 if (n < hostCacheCount)
1146 strcpy(hostcache[n].name, MSG_ReadString());
1147 strcpy(hostcache[n].map, MSG_ReadString());
1148 hostcache[n].users = MSG_ReadByte();
1149 hostcache[n].maxusers = MSG_ReadByte();
1151 if (c != NET_PROTOCOL_VERSION)
1153 strcpy(hostcache[n].cname, hostcache[n].name);
1154 hostcache[n].cname[14] = 0;
1155 strcpy(hostcache[n].name, "*");
1156 strcat(hostcache[n].name, hostcache[n].cname);
1158 memcpy(&hostcache[n].addr, readaddr, sizeof(struct qsockaddr));
1159 hostcache[n].driver = net_driverlevel;
1160 hostcache[n].ldriver = net_landriverlevel;
1161 strcpy(hostcache[n].cname, dfunc.AddrToString(readaddr));
1163 // check for a name conflict
1164 for (i = 0; i < hostCacheCount; i++)
1168 if (strcasecmp (hostcache[n].name, hostcache[i].name) == 0)
1170 i = strlen(hostcache[n].name);
1171 if (i < 15 && hostcache[n].name[i-1] > '8')
1173 hostcache[n].name[i] = '0';
1174 hostcache[n].name[i+1] = 0;
1177 hostcache[n].name[i-1]++;
1186 static void _Datagram_SearchForHosts (qboolean xmit)
1189 struct qsockaddr readaddr;
1193 SZ_Clear(&net_message);
1194 // save space for the header, filled in later
1195 MSG_WriteLong(&net_message, 0);
1196 MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
1197 MSG_WriteString(&net_message, "QUAKE");
1198 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1199 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1200 dfunc.Broadcast(dfunc.controlSock, net_message.data, net_message.cursize);
1201 SZ_Clear(&net_message);
1204 while ((ret = dfunc.Read (dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0)
1206 net_message.cursize = ret;
1207 Datagram_HandleServerInfo (&readaddr);
1211 void Datagram_SearchForHosts (qboolean xmit)
1213 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1215 if (hostCacheCount == HOSTCACHESIZE)
1217 if (net_landrivers[net_landriverlevel].initialized)
1218 _Datagram_SearchForHosts (xmit);
1223 static qboolean _Datagram_SearchForInetHosts (const char *master)
1225 qboolean result = false;
1226 struct qsockaddr masteraddr;
1227 struct qsockaddr readaddr;
1232 if (dfunc.GetAddrFromName(master, &masteraddr) != -1)
1235 const char* port = strrchr (master, ':');
1237 portnum = atoi (port + 1);
1239 portnum = MASTER_PORT;
1240 Con_DPrintf("Datagram_SearchForInetHosts: sending %d byte message to master %s port %i\n", net_message.cursize, master, portnum);
1241 dfunc.SetSocketPort (&masteraddr, portnum);
1242 dfunc.Write (dfunc.controlSock, net_message.data, net_message.cursize, &masteraddr);
1246 while ((ret = dfunc.Read (dfunc.controlSock, net_message.data, net_message.maxsize, &readaddr)) > 0)
1248 net_message.cursize = ret;
1249 Con_DPrintf("Datagram_SearchForInetHosts: Read received %d byte message\n", net_message.cursize);
1250 if (Datagram_HandleServerInfo (&readaddr))
1253 Master_ParseServerList (&dfunc);
1260 qboolean Datagram_SearchForInetHosts (const char *master)
1262 qboolean result = false;
1263 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1265 if (hostCacheCount == HOSTCACHESIZE)
1267 if (net_landrivers[net_landriverlevel].initialized)
1268 if (_Datagram_SearchForInetHosts (master))
1276 static qsocket_t *_Datagram_Connect (const char *host)
1278 struct qsockaddr sendaddr;
1279 struct qsockaddr readaddr;
1288 // see if we can resolve the host name
1289 if (dfunc.GetAddrFromName(host, &sendaddr) == -1)
1292 newsock = dfunc.OpenSocket (cl_port.integer);
1296 sock = NET_NewQSocket ();
1299 sock->socket = newsock;
1300 sock->landriver = net_landriverlevel;
1302 // connect to the host
1303 if (dfunc.Connect (newsock, &sendaddr) == -1)
1306 // send the connection request
1307 Con_Printf("trying...\n");CL_UpdateScreen();CL_UpdateScreen();
1308 start_time = net_time;
1310 for (reps = 0; reps < 3; reps++)
1312 SZ_Clear(&net_message);
1313 // save space for the header, filled in later
1314 MSG_WriteLong(&net_message, 0);
1315 MSG_WriteByte(&net_message, CCREQ_CONNECT);
1316 MSG_WriteString(&net_message, "QUAKE");
1317 MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
1318 *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
1319 dfunc.Write (newsock, net_message.data, net_message.cursize, &sendaddr);
1320 SZ_Clear(&net_message);
1323 ret = dfunc.Read (newsock, net_message.data, net_message.maxsize, &readaddr);
1324 // if we got something, validate it
1327 // is it from the right place?
1328 if (sfunc.AddrCompare(&readaddr, &sendaddr) != 0)
1330 Con_DPrintf("wrong reply address\n");
1331 Con_DPrintf("Expected: %s\n", StrAddr (&sendaddr));
1332 Con_DPrintf("Received: %s\n", StrAddr (&readaddr));
1338 if (ret < (int)sizeof(int))
1344 net_message.cursize = ret;
1345 MSG_BeginReading ();
1347 control = BigLong(*((int *)net_message.data));
1354 if ((control & (~NETFLAG_LENGTH_MASK)) != (int)NETFLAG_CTL)
1359 if ((control & NETFLAG_LENGTH_MASK) != ret)
1366 while (ret == 0 && (SetNetTime() - start_time) < 2.5);
1369 Con_Printf("still trying...\n");CL_UpdateScreen();CL_UpdateScreen();
1370 start_time = SetNetTime();
1375 reason = "No Response";
1376 Con_Printf("%s\n", reason);
1377 strcpy(m_return_reason, reason);
1383 reason = "Network Error";
1384 Con_Printf("%s\n", reason);
1385 strcpy(m_return_reason, reason);
1389 ret = MSG_ReadByte();
1390 if (ret == CCREP_REJECT)
1392 reason = MSG_ReadString();
1393 Con_Printf("%s", reason);
1394 strncpy(m_return_reason, reason, 31);
1398 if (ret == CCREP_ACCEPT)
1400 memcpy(&sock->addr, &sendaddr, sizeof(struct qsockaddr));
1401 dfunc.SetSocketPort (&sock->addr, MSG_ReadLong());
1405 reason = "Bad Response";
1406 Con_Printf("%s\n", reason);
1407 strcpy(m_return_reason, reason);
1411 dfunc.GetNameFromAddr (&sendaddr, sock->address);
1413 Con_Printf ("Connection accepted to %s\n", sock->address);
1414 sock->lastMessageTime = SetNetTime();
1416 // switch the connection to the specified address
1417 if (dfunc.Connect (newsock, &sock->addr) == -1)
1419 reason = "Connect to Game failed";
1420 Con_Printf("%s\n", reason);
1421 strcpy(m_return_reason, reason);
1425 m_return_onerror = false;
1429 NET_FreeQSocket(sock);
1431 dfunc.CloseSocket(newsock);
1432 if (m_return_onerror)
1434 key_dest = key_menu;
1435 m_state = m_return_state;
1436 m_return_onerror = false;
1441 qsocket_t *Datagram_Connect (const char *host)
1443 qsocket_t *ret = NULL;
1445 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1446 if (net_landrivers[net_landriverlevel].initialized)
1447 if ((ret = _Datagram_Connect (host)) != NULL)
1452 static void _Datagram_Heartbeat (const char *master)
1454 struct qsockaddr masteraddr;
1458 if (dfunc.GetAddrFromName(master, &masteraddr) == -1)
1462 port = strrchr (master, ':');
1464 portnum = atoi (port + 1);
1466 portnum = MASTER_PORT;
1467 dfunc.SetSocketPort (&masteraddr, portnum);
1469 // FIXME: this is the only use of UDP_Send in the entire engine, add a dfunc.acceptSock to get rid of this function!
1470 dfunc.Send (net_message.data, net_message.cursize, &masteraddr);
1473 void Datagram_Heartbeat (const char *master)
1475 for (net_landriverlevel = 0; net_landriverlevel < net_numlandrivers; net_landriverlevel++)
1476 if (net_landrivers[net_landriverlevel].initialized)
1477 _Datagram_Heartbeat (master);