manglertest.cpp 6.0 KB

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