servercontrol.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/servercontrol.h $*
  25. * *
  26. * $Author:: Bhayes $*
  27. * *
  28. * $Modtime:: 3/18/02 6:04p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #pragma once
  36. #ifndef SERVERCONTROL_H
  37. #define SERVERCONTROL_H
  38. #include "assert.h"
  39. #include "vector.h"
  40. #include "servercontrolsocket.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. /*
  55. ** Max size of packet this class can handle.
  56. */
  57. #define MAX_SERVER_CONTROL_MESSAGE_SIZE 500
  58. /*
  59. ** Time out non-responsive controllers after 1 minute.
  60. */
  61. #define CONTROL_TIMEOUT 60*1000
  62. /*
  63. ** This class is a simple 'out of band' authenticated server command/response system.
  64. **
  65. */
  66. class ServerControlClass
  67. {
  68. public:
  69. /*
  70. ** Constructor, destructor.
  71. */
  72. ServerControlClass(void);
  73. ~ServerControlClass(void);
  74. /*
  75. ** Init, shutdown.
  76. */
  77. bool Start_Listening(unsigned short port, char *password, const char*(*app_request_callback)(char*), void(*app_response_callback)(char*), bool loopback = false, unsigned long ip = 0);
  78. void Stop_Listening(void);
  79. void Set_Welcome_Message(char *message);
  80. void Allow_Remote_Admin(bool allow) {RemoteAdminAllowed = allow;}
  81. /*
  82. ** Send/receive etc.
  83. */
  84. void Send_Message(char *text, unsigned long ip, unsigned short port);
  85. /*
  86. ** Service.
  87. */
  88. void Service(void);
  89. private:
  90. void Parse_Message(void *buffer, int len, unsigned long address, unsigned short port);
  91. void Add_Remote_Control(unsigned long ip, unsigned short port);
  92. void Remove_Remote_Control(unsigned long ip, unsigned short port);
  93. bool Is_Authenticated(unsigned long address, unsigned short port);
  94. void Reset_Timeout(unsigned long address, unsigned short port);
  95. void Respond(const char *message, unsigned long ip, unsigned short port);
  96. /*
  97. ** Type of messages that can be sent.
  98. */
  99. typedef enum tControlType {
  100. CONTROL_REQUEST,
  101. CONTROL_RESPONSE
  102. } ControlType;
  103. /*
  104. ** Format of control message.
  105. */
  106. typedef struct tControlMessageStruct {
  107. ControlType Type;
  108. char Message[MAX_SERVER_CONTROL_MESSAGE_SIZE];
  109. } ControlMessageStruct;
  110. /*
  111. ** Instance of comms socket handler.
  112. */
  113. ServerControlSocketClass Comms;
  114. /*
  115. ** Port we are bound to.
  116. */
  117. unsigned short LocalPort;
  118. /*
  119. ** Are we listening for control messages?
  120. */
  121. bool Listening;
  122. /*
  123. ** Password.
  124. */
  125. char Password[128];
  126. /*
  127. ** Are we allowed to listen for control messages from a remote admin (i.e. not LOOPBACK address).
  128. */
  129. bool RemoteAdminAllowed;
  130. /*
  131. ** Welcome message sent to new controllers as the connect.
  132. */
  133. char WelcomeMessage[1024];
  134. /*
  135. ** Struct to hold info about remote controllers.
  136. */
  137. typedef struct tRemoteControlStruct{
  138. unsigned short Port;
  139. unsigned long IP;
  140. bool Secure;
  141. unsigned long Time;
  142. } RemoteControlStruct;
  143. RemoteControlStruct *Get_Controller(unsigned long ip, unsigned short port);
  144. /*
  145. ** List of remote controllers we know about.
  146. */
  147. DynamicVectorClass<RemoteControlStruct*> RemoteControllers;
  148. /*
  149. ** App callbacks for passing control messages.
  150. */
  151. const char *(*AppRequestCallback)(char*);
  152. void (*AppResponseCallback)(char*);
  153. };
  154. /*
  155. ** Single instance of local controller.
  156. */
  157. extern ServerControlClass ServerControl;
  158. #endif //SERVERCONTROL_H