mangler.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. ** Command & Conquer Generals(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 "endian.h"
  26. #include <configfile.h>
  27. #include "threadfac.h"
  28. #include "xtime.h"
  29. #include <filed.h>
  30. #include <wstring.h>
  31. #include <wdebug.h>
  32. #include <udp.h>
  33. void DisplayHelp(const char *prog)
  34. {
  35. cout << "Usage: " << prog << " <config file>" << endl;
  36. exit(0);
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. ConfigFile config;
  41. FILE* conf;
  42. if( argc <= 1 )
  43. {
  44. // No args - use a default config file
  45. if ((conf = fopen("mangler.cfg", "r")) == NULL) {
  46. cout << "Cannot open mangler.cfg for reading." << endl;
  47. DisplayHelp(argv[0]);
  48. }
  49. config.readFile(conf);
  50. fclose(conf);
  51. }
  52. else if( argc == 2 && (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "?") == 0 ||
  53. strcmp(argv[1], "-h") == 0) )
  54. DisplayHelp(argv[0]);
  55. else if( argc == 2 )
  56. {
  57. // Use a user-supplied config file
  58. if ((conf = fopen(argv[1], "r")) == NULL) {
  59. cout << "Cannot open " << argv[1] << " for reading." << endl;
  60. DisplayHelp(argv[0]);
  61. }
  62. config.readFile(conf);
  63. fclose(conf);
  64. }
  65. // ----- LOGGING -----
  66. // Setup debugging & logging output
  67. Wstring output_file("mangler.log");
  68. config.getString("LOGFILE", output_file);
  69. Wstring backup_file;
  70. backup_file = output_file;
  71. backup_file += ".bak";
  72. rename(output_file.get(),backup_file.get()); // save the old file
  73. FileD output_device(output_file.get());
  74. MsgManager::setAllStreams(&output_device);
  75. DBGMSG("DBG working...");
  76. INFMSG("INF working...");
  77. WRNMSG("WRN working...");
  78. if (htonl(0x12345678) == 0x12345678)
  79. {
  80. INFMSG("Host is network-byte-order");
  81. }
  82. else
  83. {
  84. INFMSG("Host is Intel-byte-order");
  85. }
  86. // ----- Initialize Winsock -----
  87. #ifdef _WINDOWS
  88. WORD verReq = MAKEWORD(2, 2);
  89. WSADATA wsadata;
  90. int err = WSAStartup(verReq, &wsadata);
  91. if (err != 0) {
  92. ERRMSG("Winsock Init failed.");
  93. return 1;
  94. }
  95. if ((LOBYTE(wsadata.wVersion) != 2) || (HIBYTE(wsadata.wVersion) !=2)) {
  96. ERRMSG("Winsock DLL is not 2.2");
  97. WSACleanup();
  98. ERRMSG("Winsock Init failed.");
  99. return 1;
  100. }
  101. INFMSG("Winsock Init done.");
  102. #endif
  103. // Set up a UDP listener
  104. uint8 *buff=new uint8[1024];
  105. int retval;
  106. UDP udp;
  107. UDP udp2;
  108. UDP udp3;
  109. UDP udp4;
  110. int port = 4321;
  111. config.getInt("PORT", port);
  112. uint8 blitz = 0;
  113. uint32 localIP = 0;
  114. Wstring hostIPStr = "";
  115. config.getString("IP", hostIPStr);
  116. if (hostIPStr.length())
  117. {
  118. INFMSG("Binding to "<<hostIPStr.get()<<":"<<port<<"-"<<(port+3));
  119. localIP = ntohl(inet_addr(hostIPStr.get()));
  120. }
  121. else
  122. {
  123. INFMSG("Binding to localhost:"<<port<<"-"<<(port+3));
  124. }
  125. retval = udp.Bind(localIP,(uint16)port);
  126. retval |= udp2.Bind(localIP,(uint16)port+1);
  127. retval |= udp3.Bind(localIP,(uint16)port+2);
  128. retval |= udp4.Bind(localIP,(uint16)port+3);
  129. if (retval != 0)
  130. {
  131. ERRMSG("Couldn't bind - error " << retval);
  132. exit(1);
  133. }
  134. unsigned char buf[1024];
  135. struct sockaddr_in addr;
  136. int packet_size = sizeof(ManglerData);
  137. INFMSG("sizeof(packet) == " << packet_size);
  138. unsigned char *theAddr;
  139. fd_set fdset;
  140. while (1)
  141. {
  142. retval = udp.Wait(15, 0, fdset);
  143. if (!retval)
  144. continue;
  145. //DBGMSG("Wait returned " << retval);
  146. retval = udp.Read(buf, packet_size, &addr); // Wait until there is something on the socket
  147. if (retval > 0)
  148. {
  149. ManglerData *packet = (ManglerData *)buf;
  150. theAddr = (unsigned char *)&(addr.sin_addr.s_addr);
  151. if (retval != packet_size)
  152. {
  153. WRNMSG("Recieved mis-sized packet (" << retval << " bytes) from " << theAddr[0] << "." << theAddr[1] << "." << theAddr[2] << "." << theAddr[3] << ":" << addr.sin_port);
  154. }
  155. else
  156. {
  157. if (!Passes_CRC_Check(buf, packet_size))
  158. {
  159. WRNMSG("Recieved a bad packet - good length!");
  160. continue;
  161. }
  162. packet->NetCommandType = 44;
  163. packet->MyMangledPortNumber = addr.sin_port; // not changing to host order, cause its in network byte order now, and the game will expect it to stay that way.
  164. packet->MyMangledAddress[0] = theAddr[0];
  165. packet->MyMangledAddress[1] = theAddr[1];
  166. packet->MyMangledAddress[2] = theAddr[2];
  167. packet->MyMangledAddress[3] = theAddr[3];
  168. blitz = packet->BlitzMe;
  169. INFMSG("Packet ID = " << packet->packetID);
  170. Build_Packet_CRC(buf, packet_size);
  171. udp.Write(buf,packet_size,ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port));
  172. INFMSG("Saw " << (int)theAddr[0] << "." << (int)theAddr[1] << "." << (int)theAddr[2] << "." << (int)theAddr[3] << ":" << ntohs(addr.sin_port) << ((blitz)?" Blitzed":"") );
  173. if (blitz)
  174. {
  175. udp2.Write(buf,packet_size,ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port)+1);
  176. udp3.Write(buf,packet_size,ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port)+2);
  177. udp4.Write(buf,packet_size,ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port)+3);
  178. }
  179. }
  180. }
  181. }
  182. return 0;
  183. }