Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
manglertest.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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
20#include <iostream.h>
21#include <signal.h>
22#ifdef _WINDOWS
23#include <process.h> // *MUST* be included before ANY Wnet/Wlib headers if _REENTRANT is defined
24#endif
25
26#include "mangler.h"
27#include "crc.h"
28
29#include <configfile.h>
30#include "threadfac.h"
31
32#include "endian.h"
33
34#include "xtime.h"
35#include <filed.h>
36#include <wstring.h>
37#include <wdebug.h>
38#include <udp.h>
39
40// ST - 2/1/01 12:46PM
41bool BigEndian = false;
42
43unsigned long ResolveIP(char *Server)
44{
45 char serverName[100];
46 struct hostent *serverStruct;
47 struct in_addr *serverNode;
48
49 if (Server == NULL)
50 {
51 ERRMSG("Can't resolve NULL");
52 return 0;
53 }
54
55 if (isdigit(Server[0]))
56 return ( ntohl(inet_addr(Server)) );
57
58 strcpy(serverName, Server);
59
60 serverStruct = gethostbyname(Server);
61 if (serverStruct == NULL)
62 {
63 ERRMSG("Can't resolve " << Server);
64 return 0;
65 }
66 serverNode = (struct in_addr *) serverStruct->h_addr;
67 return ( ntohl(serverNode->s_addr) );
68}
69
70void DisplayHelp(const char *prog)
71{
72 cout << "Usage: " << prog << " <config file>" << endl;
73 exit(0);
74}
75
76int main(int argc, char **argv)
77{
78 ConfigFile config;
79 FILE* conf;
80
81 if( argc <= 1 )
82 {
83 // No args - use a default config file
84 if ((conf = fopen("manglertest.cfg", "r")) == NULL) {
85 cout << "Cannot open mangler.cfg for reading." << endl;
86 DisplayHelp(argv[0]);
87 }
88 config.readFile(conf);
89 fclose(conf);
90 }
91 else if( argc == 2 && (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "?") == 0 ||
92 strcmp(argv[1], "-h") == 0) )
93 DisplayHelp(argv[0]);
94 else if( argc == 2 )
95 {
96 // Use a user-supplied config file
97 if ((conf = fopen(argv[1], "r")) == NULL) {
98 cout << "Cannot open " << argv[1] << " for reading." << endl;
99 DisplayHelp(argv[0]);
100 }
101 config.readFile(conf);
102 fclose(conf);
103 }
104
105 // ----- LOGGING -----
106 // Setup debugging & logging output
107 Wstring output_file("manglertest.log");
108 config.getString("LOGFILE", output_file);
109 Wstring backup_file;
110 backup_file = output_file;
111 backup_file += ".bak";
112 rename(output_file.get(),backup_file.get()); // save the old file
113 FileD output_device(output_file.get());
115 DBGMSG("DBG working...");
116 INFMSG("INF working...");
117 WRNMSG("WRN working...");
118
119
120 //
121 // See if our processor is big or little endian. Network order is big endian.
122 // ST - 2/1/01 12:11PM
123 //
124 if (htonl(0x12345678) == 0x12345678) {
125 BigEndian = true;
126 }
127
128
129 // ----- Initialize Winsock -----
130#ifdef _WINDOWS
131 WORD verReq = MAKEWORD(2, 2);
132 WSADATA wsadata;
133
134 int err = WSAStartup(verReq, &wsadata);
135 if (err != 0) {
136 ERRMSG("Winsock Init failed.");
137 return 1;
138 }
139
140 if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2)) {
141 ERRMSG("Winsock DLL is not 2.2");
142 WSACleanup();
143 ERRMSG("Winsock Init failed.");
144 return 1;
145 }
146 INFMSG("Winsock Init done.");
147#endif
148
149
150 // Set up a UDP listener
151 uint8 *buff=new uint8[1024];
152 int retval;
153 UDP udp;
154 int port = 4321;
155 config.getInt("MANGLERPORT", port);
156
157 int localport = 4444;
158 config.getInt("CLIENTPORT", localport);
159 retval = udp.Bind((uint32)0,(uint16)localport);
160 DBGMSG("Bind returned " << retval);
161
162 //-----------------------------------------------------------------------------------------
163 const int packet_size = sizeof(ManglerData);
164 INFMSG("sizeof(packet) == " << packet_size);
165
166 unsigned char buf[packet_size];
167 memset(buf, 0x44, packet_size); // init to something known for memory dumps :)
168 struct sockaddr_in addr;
169
170 int doBlitz = 0;
171 config.getInt("BLITZ", doBlitz);
172 if (doBlitz)
173 {
174 INFMSG("Requsting port blitz");
175 }
176
177 unsigned char *theAddr;
178 fd_set fdset;
179 unsigned long server_addr;
180 Wstring manglername = "localhost";
181 config.getString("MANGLERIP", manglername);
182 server_addr = ResolveIP(manglername.get());
183 if (!server_addr)
184 {
185 ERRMSG("Cannot resolve mangler server IP");
186 return 1;
187 }
188
189 ManglerData *packet = (ManglerData *)buf;
190 packet->NetCommandType = 12;
191 packet->packetID = 9999;
192 packet->BlitzMe = doBlitz;
193 packet->magic = htons((unsigned short)0xf00d);
194 Build_Packet_CRC(buf, packet_size);
195 DBGMSG("Writing to " << manglername.get() << ":" << port);
196 udp.Write(buf,packet_size,server_addr, 4321);
197
198 retval = udp.Wait(5, 0, fdset);
199 if (retval)
200 {
201 DBGMSG("Wait returned " << retval);
202 retval = udp.Read(buf, packet_size, &addr); // Wait until there is something on the socket
203 if (retval > 0)
204 {
205 theAddr = (unsigned char *)&(addr.sin_addr.s_addr);
206 if (retval != packet_size)
207 {
208 WRNMSG("Recieved mis-sized packet (" << retval << " bytes) from " << theAddr[0] << "." << theAddr[1] << "." << theAddr[2] << "." << theAddr[3] << ":" << addr.sin_port);
209 }
210 else
211 {
212 int packetCommand;
213 packetCommand = packet->NetCommandType;
214 if (!Passes_CRC_Check(buf, packet_size))
215 {
216 WRNMSG("CRC error!");
217 }
218 else if (packetCommand != 13)
219 {
220 WRNMSG("Returned packet had command type " << packetCommand);
221 }
222 else
223 {
224 int addr[4];
225 unsigned short retPort;
226 retPort = htons(packet->MyMangledPortNumber);
227 addr[0] = packet->MyMangledAddress[0];
228 addr[1] = packet->MyMangledAddress[1];
229 addr[2] = packet->MyMangledAddress[2];
230 addr[3] = packet->MyMangledAddress[3];
231 DBGMSG("Saw " << addr[0] << "." << addr[1] << "." << addr[2] << "." << addr[3] << ":" << retPort);
232 }
233 }
234 }
235 }
236 else
237 {
238 DBGMSG("Wait timed out");
239 }
240
241
242 return 0;
243}
244
245
#define NULL
Definition BaseType.h:92
#define INFMSG(X)
Definition wdebug.h:64
#define ERRMSG(X)
Definition wdebug.h:86
#define WRNMSG(X)
Definition wdebug.h:75
#define DBGMSG(X)
Definition wdebug.h:128
unsigned short uint16
Definition bittype.h:45
unsigned short WORD
Definition bittype.h:58
unsigned long uint32
Definition bittype.h:46
unsigned char uint8
Definition bittype.h:44
bool Passes_CRC_Check(unsigned char *buf, int len)
Definition crc.cpp:137
void Build_Packet_CRC(unsigned char *buf, int len)
Definition crc.cpp:80
bit8 getInt(IN Wstring &key, OUT sint32 &value)
bit8 readFile(IN FILE *config)
bit8 getString(IN Wstring &key, OUT Wstring &value)
Definition filed.h:25
static int setAllStreams(OutputDevice *device)
Definition wdebug.cpp:45
Definition udp.h:66
Int Bind(UnsignedInt IP, UnsignedShort port)
Definition udp.cpp:153
Int Write(const unsigned char *msg, UnsignedInt len, UnsignedInt IP, UnsignedShort port)
Definition udp.cpp:238
Int Read(unsigned char *msg, UnsignedInt len, sockaddr_in *from)
Definition udp.cpp:270
int Wait(sint32 sec, sint32 usec, fd_set &returnSet)
Definition udp.cpp:227
char * get(void)
Definition wstring.cpp:336
void main(void)
Definition test1.cpp:47
bool BigEndian
void DisplayHelp(const char *prog)
unsigned long ResolveIP(char *Server)
OutputDevice * output_device
Definition main.cpp:59
unsigned char MyMangledAddress[4]
unsigned short MyMangledPortNumber
unsigned char NetCommandType
unsigned short magic
unsigned char BlitzMe