Initial commit of Command & Conquer Renegade source code.

This commit is contained in:
LFeenanEA
2025-02-27 16:39:46 +00:00
parent 74ab8fa5e0
commit 58ed459113
4918 changed files with 1366710 additions and 0 deletions

136
Code/SControl/SControl.dsp Normal file
View File

@@ -0,0 +1,136 @@
# Microsoft Developer Studio Project File - Name="SControl" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Static Library" 0x0104
CFG=SControl - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "SControl.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "SControl.mak" CFG="SControl - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "SControl - Win32 Release" (based on "Win32 (x86) Static Library")
!MESSAGE "SControl - Win32 Debug" (based on "Win32 (x86) Static Library")
!MESSAGE "SControl - Win32 Profile" (based on "Win32 (x86) Static Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""$/Commando/Code/SControl", DLGEAAAA"
# PROP Scc_LocalPath "."
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "SControl - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD CPP /nologo /MT /W4 /WX /O2 /I "..\wwlib" /I "..\wwdebug" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo /out:"..\libs\Release\SControl.lib"
!ELSEIF "$(CFG)" == "SControl - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
# ADD CPP /nologo /MTd /W4 /WX /Gm /Zi /Od /I "..\wwlib" /I "..\wwdebug" /D "WWDEBUG" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo /out:"..\libs\Debug\SControl.lib"
!ELSEIF "$(CFG)" == "SControl - Win32 Profile"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Profile"
# PROP BASE Intermediate_Dir "Profile"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Profile"
# PROP Intermediate_Dir "Profile"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
# ADD CPP /nologo /MT /W4 /WX /O2 /I "..\wwlib" /I "..\wwdebug" /D "WWDEBUG" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo /out:"..\libs\Release\SControl.lib"
# ADD LIB32 /nologo /out:"..\libs\Profile\SControl.lib"
!ENDIF
# Begin Target
# Name "SControl - Win32 Release"
# Name "SControl - Win32 Debug"
# Name "SControl - Win32 Profile"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\servercontrol.cpp
# End Source File
# Begin Source File
SOURCE=.\servercontrol.h
# End Source File
# Begin Source File
SOURCE=.\servercontrolsocket.cpp
# End Source File
# Begin Source File
SOURCE=.\servercontrolsocket.h
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# End Group
# End Target
# End Project

View File

@@ -0,0 +1,563 @@
/*
** Command & Conquer Renegade(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***********************************************************************************************
*** 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 ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* $Archive:: /Commando/Code/SControl/servercontrol.cpp $*
* *
* $Author:: Bhayes $*
* *
* $Modtime:: 3/18/02 6:04p $*
* *
* $Revision:: 7 $*
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* *
* *
* *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include <stdlib.h>
#include "servercontrol.h"
#include "systimer.h"
/*
** Single instance of server control.
*/
ServerControlClass ServerControl;
/***********************************************************************************************
* ServerControlClass::ServerControlClass -- Class constructor *
* *
* *
* *
* INPUT: Nothing *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:00PM ST : Created *
*=============================================================================================*/
ServerControlClass::ServerControlClass(void)
{
AppRequestCallback = NULL;
AppResponseCallback = NULL;
LocalPort = 63999;
strcpy(Password, "");
Listening = false;
WelcomeMessage[0] = 0;
RemoteAdminAllowed = false;
}
/***********************************************************************************************
* ServerControlClass::ServerControlClass -- Class destructor *
* *
* *
* *
* INPUT: Nothing *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:01PM ST : Created *
*=============================================================================================*/
ServerControlClass::~ServerControlClass(void)
{
Stop_Listening();
}
/***********************************************************************************************
* ServerControlClass::Start_Listening -- Start listening for server control messages *
* *
* *
* *
* INPUT: Port to listen on *
* Password to authenticate controllers *
* App request callback *
* App response callback *
* Loopback - if set then only bind to the loopback address *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:01PM ST : Created *
*=============================================================================================*/
bool ServerControlClass::Start_Listening(unsigned short port, char *password, const char*(*app_request_callback)(char*), void(*app_response_callback)(char*), bool loopback, unsigned long ip)
{
LocalPort = port;
if (LocalPort != 0) {
strcpy(Password, password);
if (Comms.Open(LocalPort, loopback, ip)) {
Listening = true;
AppRequestCallback = app_request_callback;
AppResponseCallback = app_response_callback;
Comms.Set_Encryption_Key(password);
return(true);
}
}
return(false);
}
/***********************************************************************************************
* ServerControlClass::Stop_Listening -- Stop listening to control messages *
* *
* *
* *
* INPUT: Nothing *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:02PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Stop_Listening(void)
{
if (Listening) {
/*
** Throw off any connected controllers.
*/
RemoteControlStruct *control;
while (RemoteControllers.Count()) {
control = RemoteControllers[0];
if (control && control->Secure) {
Respond("** Server exiting - Connection closed! **\n", control->IP, control->Port);
}
Remove_Remote_Control(control->IP, control->Port);
}
Comms.Close();
Listening = false;
}
while (RemoteControllers.Count()) {
delete RemoteControllers[0];
RemoteControllers.Delete(0);
}
}
/***********************************************************************************************
* ServerControlClass::Service -- Service control messages *
* *
* *
* *
* INPUT: Nothing *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:03PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Service(void)
{
if (Listening) {
char buffer[1024];
int buffer_len = sizeof(buffer);
unsigned long address;
unsigned short port;
Comms.Service();
int bytes = Comms.Read(buffer, buffer_len, &address, &port);
if (bytes > 0 && bytes <= sizeof(ControlMessageStruct)) {
if (Is_Authenticated(address, port)) {
Reset_Timeout(address, port);
}
Parse_Message(buffer, bytes, address, port);
}
/*
** See if anyone timed out.
*/
RemoteControlStruct *control;
for (int i=0 ; i<RemoteControllers.Count() ; i++) {
control = RemoteControllers[i];
if (control && control->Secure) {
if (TIMEGETTIME() - control->Time > CONTROL_TIMEOUT) {
Respond("** Connection timed out - Bye! **\n", control->IP, control->Port);
Remove_Remote_Control(control->IP, control->Port);
break;
}
}
}
}
}
/***********************************************************************************************
* ServerControlClass::Parse_Message -- Parse server control message *
* *
* *
* *
* INPUT: Buffer containing message struct *
* Length of message *
* Address message came from *
* Port message came from *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:03PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Parse_Message(void *buffer, int len, unsigned long address, unsigned short port)
{
ControlMessageStruct *message = (ControlMessageStruct*) buffer;
assert(len <= sizeof(ControlMessageStruct));
assert(message != NULL);
/*
** This line prevents external control by only accepting messages from the loopback address.
*/
if (ntohl(address) == INADDR_LOOPBACK || RemoteAdminAllowed) {
/*
** Convert to upper case for parsing.
*/
char text[MAX_SERVER_CONTROL_MESSAGE_SIZE + 1];
memcpy(text, message->Message, MAX_SERVER_CONTROL_MESSAGE_SIZE);
text[MAX_SERVER_CONTROL_MESSAGE_SIZE] = 0;
strupr(text);
switch (message->Type) {
/*
** This is a request from a remote controller.
*/
case CONTROL_REQUEST:
if (strstr(text, "CONNECT")) {
/*
** Someone is requesting to talk to us.
*/
if (strlen(Password)) {
Respond("Password required:", address, port);
} else {
Add_Remote_Control(address, port);
}
break;
}
if (strstr(text, "BYE")) {
/*
** Remote controller is going away.
*/
if (Is_Authenticated(address, port)) {
Respond("Goodbye!\n", address, port);
Remove_Remote_Control(address, port);
}
break;
}
/*
** Check for password authentication.
*/
if (strlen(Password) && strstr(message->Message, Password)) {
char buildbuf[256];
strcpy(buildbuf, "Password accepted.\n");
strcat(buildbuf, WelcomeMessage);
strcat(buildbuf, "\n");
Respond(buildbuf, address, port);
Add_Remote_Control(address, port);
break;
}
/*
** If we know about this controller and we have got a valid password from him then pass his message to the game.
*/
if (AppRequestCallback && Is_Authenticated(address, port)) {
const char * response = AppRequestCallback(message->Message);
if (response && strlen(response)) {
Respond(response, address, port);
}
}
break;
/*
** This is a response to a control message we sent out.
*/
case CONTROL_RESPONSE:
if (AppResponseCallback) {
AppResponseCallback(message->Message);
}
break;
}
}
}
/***********************************************************************************************
* ServerControlClass::Add_Remote_Control -- Add a new remote controller *
* *
* *
* *
* INPUT: IP of remote controller *
* Port of remote controller *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:06PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Add_Remote_Control(unsigned long ip, unsigned short port)
{
RemoteControlStruct *control = Get_Controller(ip, port);
if (control == NULL) {
control = new RemoteControlStruct;
RemoteControllers.Add(control);
}
control->Time = TIMEGETTIME();
control->Secure = true;
control->IP = ip;
control->Port = port;
}
/***********************************************************************************************
* ServerControlClass::Remove_Remote_Control -- Remove a remote controller from our list *
* *
* *
* *
* INPUT: IP of remote controller *
* Port of remote controller *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:07PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Remove_Remote_Control(unsigned long ip, unsigned short port)
{
RemoteControlStruct *control;
for (int i=0 ; i<RemoteControllers.Count() ; i++) {
control = RemoteControllers[i];
if (control->IP == ip && control->Port == port) {
delete control;
RemoteControllers.Delete(i);
break;
}
}
}
/***********************************************************************************************
* ServerControlClass::Is_Authenticated -- Has this controller presented a valid password? *
* *
* *
* *
* INPUT: IP, port *
* *
* OUTPUT: True if controller knows password *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:07PM ST : Created *
*=============================================================================================*/
bool ServerControlClass::Is_Authenticated(unsigned long ip, unsigned short port)
{
RemoteControlStruct *control = Get_Controller(ip, port);
if (control && control->Secure && TIMEGETTIME() - control->Time < CONTROL_TIMEOUT) {
return(true);
}
return(false);
}
/***********************************************************************************************
* ServerControlClass::Get_Controller -- Find remote controller info based on ip and port *
* *
* *
* *
* INPUT: Ip, port *
* *
* OUTPUT: Ptr to remote control struct. Null if not found *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:08PM ST : Created *
*=============================================================================================*/
ServerControlClass::RemoteControlStruct *ServerControlClass::Get_Controller(unsigned long ip, unsigned short port)
{
RemoteControlStruct *control;
for (int i=0 ; i<RemoteControllers.Count() ; i++) {
control = RemoteControllers[i];
if (control->IP == ip && control->Port == port) {
return(control);
}
}
return(NULL);
}
/***********************************************************************************************
* ServerControlClass::Reset_Timeout -- Reset idle timeout for remote controller *
* *
* *
* *
* INPUT: Ip, port *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:09PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Reset_Timeout(unsigned long ip, unsigned short port)
{
RemoteControlStruct *control = Get_Controller(ip, port);
if (control && control->Secure && TIMEGETTIME() - control->Time < CONTROL_TIMEOUT) {
control->Time = TIMEGETTIME();
}
}
/***********************************************************************************************
* ServerControlClass::Send_Message -- Send control message to remote server *
* *
* *
* *
* INPUT: Message text *
* IP, port to send to *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:09PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Send_Message(char *text, unsigned long ip, unsigned short port)
{
ControlMessageStruct message;
message.Type = CONTROL_REQUEST;
strcpy(message.Message, text);
Comms.Write(&message, sizeof(message.Type) + strlen(text) + 1, &ip, port);
Comms.Service();
}
/***********************************************************************************************
* ServerControlClass::Respond -- Send a control request response to a remote controller *
* *
* *
* *
* INPUT: Text of response *
* IP to send to *
* port to send to *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:10PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Respond(const char *text, unsigned long ip, unsigned short port)
{
ControlMessageStruct message;
const char *outmsg = text;
while (strlen(outmsg)) {
message.Type = CONTROL_RESPONSE;
strncpy(message.Message, outmsg, sizeof(message.Message)-1);
message.Message[sizeof(message.Message)-1] = 0;
int outlen = 0;
if (strlen(outmsg) > sizeof(message.Message)-1) {
outlen = sizeof(message.Message)-1;
outmsg += (sizeof(message.Message)-1);
} else {
outlen = strlen(outmsg);
outmsg += outlen;
}
Comms.Write(&message, sizeof(message.Type) + outlen + 1, &ip, port);
Comms.Service();
}
}
/***********************************************************************************************
* ServerControlClass::Set_Welcome_Message -- Set the new connection welcome message *
* *
* *
* *
* INPUT: Text of message *
* *
* OUTPUT: Nothing *
* *
* WARNINGS: None *
* *
* HISTORY: *
* 11/16/2001 4:11PM ST : Created *
*=============================================================================================*/
void ServerControlClass::Set_Welcome_Message(char *message)
{
if (message) {
memset(WelcomeMessage, 0, sizeof(WelcomeMessage));
strncpy(WelcomeMessage, message, sizeof(WelcomeMessage)-1);
}
}

View File

@@ -0,0 +1,191 @@
/*
** Command & Conquer Renegade(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***********************************************************************************************
*** 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 ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* $Archive:: /Commando/Code/SControl/servercontrol.h $*
* *
* $Author:: Bhayes $*
* *
* $Modtime:: 3/18/02 6:04p $*
* *
* $Revision:: 5 $*
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#pragma once
#ifndef SERVERCONTROL_H
#define SERVERCONTROL_H
#include "assert.h"
#include "vector.h"
#include "servercontrolsocket.h"
#ifndef DebugString
#include "wwdebug.h"
#ifdef WWDEBUG_SAY
#define DebugString WWDEBUG_SAY
#endif
#endif
#ifdef WWASSERT
#ifndef fw_assert
#define fw_assert WWASSERT
#endif //fw_assert
#else //WWASSERT
#define fw_assert assert
#endif //WWASSERT
/*
** Max size of packet this class can handle.
*/
#define MAX_SERVER_CONTROL_MESSAGE_SIZE 500
/*
** Time out non-responsive controllers after 1 minute.
*/
#define CONTROL_TIMEOUT 60*1000
/*
** This class is a simple 'out of band' authenticated server command/response system.
**
*/
class ServerControlClass
{
public:
/*
** Constructor, destructor.
*/
ServerControlClass(void);
~ServerControlClass(void);
/*
** Init, shutdown.
*/
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);
void Stop_Listening(void);
void Set_Welcome_Message(char *message);
void Allow_Remote_Admin(bool allow) {RemoteAdminAllowed = allow;}
/*
** Send/receive etc.
*/
void Send_Message(char *text, unsigned long ip, unsigned short port);
/*
** Service.
*/
void Service(void);
private:
void Parse_Message(void *buffer, int len, unsigned long address, unsigned short port);
void Add_Remote_Control(unsigned long ip, unsigned short port);
void Remove_Remote_Control(unsigned long ip, unsigned short port);
bool Is_Authenticated(unsigned long address, unsigned short port);
void Reset_Timeout(unsigned long address, unsigned short port);
void Respond(const char *message, unsigned long ip, unsigned short port);
/*
** Type of messages that can be sent.
*/
typedef enum tControlType {
CONTROL_REQUEST,
CONTROL_RESPONSE
} ControlType;
/*
** Format of control message.
*/
typedef struct tControlMessageStruct {
ControlType Type;
char Message[MAX_SERVER_CONTROL_MESSAGE_SIZE];
} ControlMessageStruct;
/*
** Instance of comms socket handler.
*/
ServerControlSocketClass Comms;
/*
** Port we are bound to.
*/
unsigned short LocalPort;
/*
** Are we listening for control messages?
*/
bool Listening;
/*
** Password.
*/
char Password[128];
/*
** Are we allowed to listen for control messages from a remote admin (i.e. not LOOPBACK address).
*/
bool RemoteAdminAllowed;
/*
** Welcome message sent to new controllers as the connect.
*/
char WelcomeMessage[1024];
/*
** Struct to hold info about remote controllers.
*/
typedef struct tRemoteControlStruct{
unsigned short Port;
unsigned long IP;
bool Secure;
unsigned long Time;
} RemoteControlStruct;
RemoteControlStruct *Get_Controller(unsigned long ip, unsigned short port);
/*
** List of remote controllers we know about.
*/
DynamicVectorClass<RemoteControlStruct*> RemoteControllers;
/*
** App callbacks for passing control messages.
*/
const char *(*AppRequestCallback)(char*);
void (*AppResponseCallback)(char*);
};
/*
** Single instance of local controller.
*/
extern ServerControlClass ServerControl;
#endif //SERVERCONTROL_H

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,212 @@
/*
** Command & Conquer Renegade(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***********************************************************************************************
*** 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 ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* $Archive:: /Commando/Code/SControl/servercontrolsocket.h $*
* *
* $Author:: Steve_t $*
* *
* $Modtime:: 2/22/02 1:05p $*
* *
* $Revision:: 2 $*
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#pragma once
#ifndef SERVERCONTROLSOCKET_H
#define SERVERCONTROLSOCKET_H
#include "assert.h"
#include "vector.h"
#include <winsock.h>
#ifndef DebugString
#include "wwdebug.h"
#ifdef WWDEBUG_SAY
#define DebugString WWDEBUG_SAY
#endif
#endif
#ifdef WWASSERT
#ifndef fw_assert
#define fw_assert WWASSERT
#endif //fw_assert
#else //WWASSERT
#define fw_assert assert
#endif //WWASSERT
#ifdef errno
#undef errno
#endif //errno
#define errno (WSAGetLastError())
#define LAST_ERROR errno
#ifndef TIMER_SECOND
#define TIMER_SECOND 1000
#endif //TIMER_SECOND
/*
** Length of winsocks internal buffer.
*/
#define SERVER_CONTROL_SOCKET_BUFFER_SIZE 8192
/*
** Length of our temporary receive buffer. Needs to be more that the max packet size which is about 550 bytes.
*/
#define SERVER_CONTROL_RECEIVE_BUFFER_LEN 640
/*
** Number of statically allocated packet buffers for the class
*/
#define SERVER_CONTROL_MAX_STATIC_BUFFERS 32
/*
** Class to manage low level comms for talking to Mangler Servers.
**
** Can't use the Renegade packet comms since the packet format is different - Mangler servers expect C&C packet format.
*/
class ServerControlSocketClass
{
public:
/*
** Constructor, destructor.
*/
ServerControlSocketClass(void);
~ServerControlSocketClass(void);
/*
** Startup, shutdown.
*/
bool Open(int port, bool loopback = false, unsigned long ip = 0);
void Close(void);
void Discard_In_Buffers(void);
void Discard_Out_Buffers(void);
void Set_Encryption_Key(char *key);
/*
** Read, write.
*/
int Peek(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
int Read(void *buffer, int buffer_len, void *address, unsigned short *port, int packetnum = 0);
void Write(void *buffer, int buffer_len, void *address, unsigned short port = 0);
/*
** Service.
*/
void Service(void);
/*
** Error handling.
*/
void Clear_Socket_Error(void);
private:
/*
** The socket associated with this class.
*/
SOCKET Socket;
/*
** The port that this class listens on.
*/
int Port;
/*
** This struct contains the information needed for each incoming and outgoing packet.
** It acts as a temporary control for these packets.
*/
struct WinsockBufferType {
unsigned char Address[4]; // Address. IN_ADDR
int BufferLen; // Length of data in buffer
bool IsBroadcast; // Flag to broadcast this packet
bool InUse; // Useage state of buffer
bool IsAllocated; // false means statically allocated.
unsigned short Port; // Override port. Send to this port if not 0. Save incoming port number.
unsigned long CRC; // CRC of packet for extra sanity.
unsigned char Buffer[SERVER_CONTROL_RECEIVE_BUFFER_LEN]; // Buffer to store packet in.
};
/*
** Packet buffer allocation.
*/
void *Get_New_Out_Buffer(void);
void *Get_New_In_Buffer(void);
/*
** Packet CRCs.
*/
void Add_CRC(unsigned long *crc, unsigned long val);
virtual void Build_Packet_CRC(WinsockBufferType *packet);
virtual bool Passes_CRC_Check(WinsockBufferType *packet);
/*
** Encryption.
*/
void Encrypt(unsigned char *packet, int size);
void Decrypt(unsigned char *packet, int size);
/*
** Array of buffers to temporarily store incoming and outgoing packets.
*/
DynamicVectorClass<WinsockBufferType*> InBuffers;
DynamicVectorClass<WinsockBufferType*> OutBuffers;
/*
** Array of buffers that are always available for incoming packets.
*/
WinsockBufferType StaticInBuffers[SERVER_CONTROL_MAX_STATIC_BUFFERS];
WinsockBufferType StaticOutBuffers[SERVER_CONTROL_MAX_STATIC_BUFFERS];
/*
** Pointers to allow circular use of the buffer arrays.
*/
int InBufferArrayPos;
int OutBufferArrayPos;
/*
** Usage count for each array.
*/
int InBuffersUsed;
int OutBuffersUsed;
/*
** Temporary receive buffer to use when querying Winsock for incoming packets.
*/
unsigned char ReceiveBuffer[SERVER_CONTROL_RECEIVE_BUFFER_LEN];
/*
** Encryption key, only 1st 8 bytes used. The rest is for safety.
*/
char Key[12];
};
#endif //SERVERCONTROLSOCKET_H