servercontrolsocket.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/SControl/servercontrolsocket.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 2/22/02 1:05p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma once
  36. #ifndef SERVERCONTROLSOCKET_H
  37. #define SERVERCONTROLSOCKET_H
  38. #include "assert.h"
  39. #include "vector.h"
  40. #include <winsock.h>
  41. #ifndef DebugString
  42. #include "wwdebug.h"
  43. #ifdef WWDEBUG_SAY
  44. #define DebugString WWDEBUG_SAY
  45. #endif
  46. #endif
  47. #ifdef WWASSERT
  48. #ifndef fw_assert
  49. #define fw_assert WWASSERT
  50. #endif //fw_assert
  51. #else //WWASSERT
  52. #define fw_assert assert
  53. #endif //WWASSERT
  54. #ifdef errno
  55. #undef errno
  56. #endif //errno
  57. #define errno (WSAGetLastError())
  58. #define LAST_ERROR errno
  59. #ifndef TIMER_SECOND
  60. #define TIMER_SECOND 1000
  61. #endif //TIMER_SECOND
  62. /*
  63. ** Length of winsocks internal buffer.
  64. */
  65. #define SERVER_CONTROL_SOCKET_BUFFER_SIZE 8192
  66. /*
  67. ** Length of our temporary receive buffer. Needs to be more that the max packet size which is about 550 bytes.
  68. */
  69. #define SERVER_CONTROL_RECEIVE_BUFFER_LEN 640
  70. /*
  71. ** Number of statically allocated packet buffers for the class
  72. */
  73. #define SERVER_CONTROL_MAX_STATIC_BUFFERS 32
  74. /*
  75. ** Class to manage low level comms for talking to Mangler Servers.
  76. **
  77. ** Can't use the Renegade packet comms since the packet format is different - Mangler servers expect C&C packet format.
  78. */
  79. class ServerControlSocketClass
  80. {
  81. public:
  82. /*
  83. ** Constructor, destructor.
  84. */
  85. ServerControlSocketClass(void);
  86. ~ServerControlSocketClass(void);
  87. /*
  88. ** Startup, shutdown.
  89. */
  90. bool Open(int port, bool loopback = false, unsigned long ip = 0);
  91. void Close(void);
  92. void Discard_In_Buffers(void);
  93. void Discard_Out_Buffers(void);
  94. void Set_Encryption_Key(char *key);
  95. /*
  96. ** Read, write.
  97. */
  98. int Peek(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
  99. int Read(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
  100. void Write(void *buffer, int buffer_len, void *address, unsigned short port = 0);
  101. /*
  102. ** Service.
  103. */
  104. void Service(void);
  105. /*
  106. ** Error handling.
  107. */
  108. void Clear_Socket_Error(void);
  109. private:
  110. /*
  111. ** The socket associated with this class.
  112. */
  113. SOCKET Socket;
  114. /*
  115. ** The port that this class listens on.
  116. */
  117. int Port;
  118. /*
  119. ** This struct contains the information needed for each incoming and outgoing packet.
  120. ** It acts as a temporary control for these packets.
  121. */
  122. struct WinsockBufferType {
  123. unsigned char Address[4]; // Address. IN_ADDR
  124. int BufferLen; // Length of data in buffer
  125. bool IsBroadcast; // Flag to broadcast this packet
  126. bool InUse; // Useage state of buffer
  127. bool IsAllocated; // false means statically allocated.
  128. unsigned short Port; // Override port. Send to this port if not 0. Save incoming port number.
  129. unsigned long CRC; // CRC of packet for extra sanity.
  130. unsigned char Buffer[SERVER_CONTROL_RECEIVE_BUFFER_LEN]; // Buffer to store packet in.
  131. };
  132. /*
  133. ** Packet buffer allocation.
  134. */
  135. void *Get_New_Out_Buffer(void);
  136. void *Get_New_In_Buffer(void);
  137. /*
  138. ** Packet CRCs.
  139. */
  140. void Add_CRC(unsigned long *crc, unsigned long val);
  141. virtual void Build_Packet_CRC(WinsockBufferType *packet);
  142. virtual bool Passes_CRC_Check(WinsockBufferType *packet);
  143. /*
  144. ** Encryption.
  145. */
  146. void Encrypt(unsigned char *packet, int size);
  147. void Decrypt(unsigned char *packet, int size);
  148. /*
  149. ** Array of buffers to temporarily store incoming and outgoing packets.
  150. */
  151. DynamicVectorClass<WinsockBufferType*> InBuffers;
  152. DynamicVectorClass<WinsockBufferType*> OutBuffers;
  153. /*
  154. ** Array of buffers that are always available for incoming packets.
  155. */
  156. WinsockBufferType StaticInBuffers[SERVER_CONTROL_MAX_STATIC_BUFFERS];
  157. WinsockBufferType StaticOutBuffers[SERVER_CONTROL_MAX_STATIC_BUFFERS];
  158. /*
  159. ** Pointers to allow circular use of the buffer arrays.
  160. */
  161. int InBufferArrayPos;
  162. int OutBufferArrayPos;
  163. /*
  164. ** Usage count for each array.
  165. */
  166. int InBuffersUsed;
  167. int OutBuffersUsed;
  168. /*
  169. ** Temporary receive buffer to use when querying Winsock for incoming packets.
  170. */
  171. unsigned char ReceiveBuffer[SERVER_CONTROL_RECEIVE_BUFFER_LEN];
  172. /*
  173. ** Encryption key, only 1st 8 bytes used. The rest is for safety.
  174. */
  175. char Key[12];
  176. };
  177. #endif //SERVERCONTROLSOCKET_H