net.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. */
  6. #include "../common/debug.h"
  7. #include <iostream>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <signal.h>
  11. #include "../common/queue.h"
  12. #include "../common/timer.h"
  13. #include "../common/seperator.h"
  14. #include "net.h"
  15. #include "client.h"
  16. #include "LoginDatabase.h"
  17. #include "LWorld.h"
  18. #include "../common/packet_functions.h"
  19. #include "../common/EQStreamFactory.h"
  20. #include "../common/MiscFunctions.h"
  21. #include "../common/version.h"
  22. #include "../common/PacketStruct.h"
  23. #include "../common/DataBuffer.h"
  24. #include "../common/ConfigReader.h"
  25. #include "../common/Log.h"
  26. #ifdef WIN32
  27. #define snprintf _snprintf
  28. #define vsnprintf _vsnprintf
  29. #define strncasecmp _strnicmp
  30. #define strcasecmp _stricmp
  31. #include <conio.h>
  32. #else
  33. #include <stdlib.h>
  34. #include "../common/unix.h"
  35. #endif
  36. EQStreamFactory eqsf(LoginStream);
  37. map<int16,OpcodeManager*>EQOpcodeManager;
  38. //TCPServer eqns(5999);
  39. NetConnection net;
  40. ClientList client_list;
  41. LWorldList world_list;
  42. LoginDatabase database;
  43. ConfigReader configReader;
  44. map<int16, int16> EQOpcodeVersions;
  45. Timer statTimer(60000);
  46. volatile bool RunLoops = true;
  47. bool ReadLoginConfig();
  48. #ifdef PUBLICLOGIN
  49. char version[200], consoletitle[200];
  50. #endif
  51. #include "../common/timer.h"
  52. #include "../common/CRC16.h"
  53. #include <fstream>
  54. int main(int argc, char** argv){
  55. #ifdef _DEBUG
  56. _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  57. #endif
  58. if (signal(SIGINT, CatchSignal) == SIG_ERR) {
  59. cerr << "Could not set signal handler" << endl;
  60. }
  61. LogStart();
  62. LogParseConfigs();
  63. net.WelcomeHeader();
  64. srand(time(NULL));
  65. if(!net.ReadLoginConfig())
  66. return 1;
  67. const char* structList[] = { "CommonStructs.xml", "LoginStructs.xml" };
  68. for (int s = 0; s < sizeof(structList) / sizeof(const char*); s++)
  69. {
  70. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s..", structList[s]);
  71. if (configReader.processXML_Elements(structList[s]))
  72. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s completed..", structList[s]);
  73. else
  74. {
  75. LogWrite(INIT__ERROR, 0, "Init", "Loading Structs File %s FAILED!", structList[s]);
  76. return 1;
  77. }
  78. }
  79. LogWrite(INIT__INFO, 0, "Init", "Initialize World List..");
  80. world_list.Init();
  81. if(eqsf.listen_ip_address)
  82. LogWrite(INIT__INFO, 0, "Init", "Login server listening on %s port %i", eqsf.listen_ip_address, net.GetPort());
  83. else
  84. LogWrite(INIT__INFO, 0, "Init", "Login server listening on port %i", net.GetPort());
  85. /*}
  86. else {
  87. cout << "EQNetworkServer.Open() error" << endl;
  88. return 1;
  89. }*/
  90. if (!eqsf.Open(net.GetPort())) {
  91. LogWrite(INIT__ERROR, 0, "Init", "Failed to open port %i.", net.GetPort());
  92. return 1;
  93. }
  94. net.UpdateWindowTitle();
  95. EQStream* eqs;
  96. Timer* TimeoutTimer = new Timer(5000);
  97. TimeoutTimer->Start();
  98. while(RunLoops) {
  99. Timer::SetCurrentTime();
  100. while ((eqs = eqsf.Pop())) {
  101. struct in_addr in;
  102. in.s_addr = eqs->GetRemoteIP();
  103. LogWrite(LOGIN__INFO, 0, "Login", "New client from IP: %s on port %i", inet_ntoa(in), ntohs(eqs->GetRemotePort()));
  104. Client* client = new Client(eqs);
  105. eqs->SetClientVersion(0);
  106. client_list.Add(client);
  107. net.numclients++;
  108. net.UpdateWindowTitle();
  109. }
  110. if(TimeoutTimer->Check()){
  111. eqsf.CheckTimeout();
  112. }
  113. if(statTimer.Check()){
  114. world_list.UpdateWorldStats();
  115. database.RemoveOldWorldServerStats();
  116. database.FixBugReport();
  117. }
  118. client_list.Process();
  119. world_list.Process();
  120. #ifdef WIN32
  121. if(kbhit())
  122. {
  123. int hitkey = getch();
  124. net.HitKey(hitkey);
  125. }
  126. #endif
  127. Sleep(1);
  128. }
  129. //close
  130. //eqns.Close();
  131. eqsf.Close();
  132. world_list.Shutdown();
  133. return 0;
  134. }
  135. #ifdef WIN32
  136. void NetConnection::HitKey(int keyhit)
  137. {
  138. switch(keyhit)
  139. {
  140. case 'l':
  141. case 'L': {
  142. world_list.ListWorldsToConsole();
  143. break;
  144. }
  145. case 'v':
  146. case 'V':
  147. {
  148. printf("========Version Info=========\n");
  149. printf("%s %s\n", EQ2EMU_MODULE, CURRENT_VERSION);
  150. printf("Last Compiled on %s %s\n", COMPILE_DATE, COMPILE_TIME);
  151. printf("=============================\n\n");
  152. break;
  153. }
  154. case 'H':
  155. case 'h': {
  156. printf("===========Help=============\n");
  157. printf("Available Commands:\n");
  158. printf("l = Listing of World Servers\n");
  159. printf("v = Login Version\n");
  160. // printf("0 = Kick all connected world servers\n");
  161. printf("============================\n\n");
  162. break;
  163. }
  164. default:
  165. printf("Invalid Command.\n");
  166. break;
  167. }
  168. }
  169. #endif
  170. void CatchSignal(int sig_num) {
  171. cout << "Got signal " << sig_num << endl;
  172. RunLoops = false;
  173. }
  174. bool NetConnection::ReadLoginConfig() {
  175. char buf[201], type[201];
  176. int8 items[5] = {0, 0, 0, 0, 0};
  177. FILE *f;
  178. char* lsInfoFile = { "LoginServer.ini" };
  179. if (!(f = fopen (lsInfoFile, "r"))) {
  180. LogWrite(INIT__ERROR, 0, "Init", "File %s could not be found..", lsInfoFile);
  181. return false;
  182. }
  183. do {
  184. fgets (buf, 200, f);
  185. if (feof (f))
  186. {
  187. LogWrite(INIT__ERROR, 0, "Init", "[LoginConfig] block from %s missing..", lsInfoFile);
  188. fclose (f);
  189. return false;
  190. }
  191. }
  192. while (strncasecmp (buf, "[LoginConfig]\n", 14) != 0 && strncasecmp (buf, "[LoginConfig]\r\n", 15) != 0);
  193. while (!feof (f))
  194. {
  195. #ifdef WIN32
  196. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  197. #else
  198. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  199. #endif
  200. {
  201. items[0] = 1;
  202. LoginMode = Standalone;
  203. items[1] = 1;
  204. items[2] = 1;
  205. items[3] = 1;
  206. if (!strncasecmp(type, "[", 1)) {
  207. // new block after LoginConfig, skip
  208. break;
  209. }
  210. if (!strncasecmp(type, "serverport", 10)) {
  211. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  212. port = atoi(buf);
  213. }
  214. }
  215. if (!strncasecmp(type, "serverip", 8)) {
  216. eqsf.listen_ip_address = new char[sizeof(buf) + 1];
  217. strcpy(eqsf.listen_ip_address, buf);
  218. }
  219. if (!strncasecmp(type, "accountcreation", 15)) {
  220. if (Seperator::IsNumber(buf)) {
  221. allowAccountCreation = atoi(buf);
  222. }
  223. }
  224. }
  225. }
  226. fclose (f);
  227. if (!items[0] || (LoginMode == Slave && !(items[1] && items[2] && items[3])) || (items[4] && !(items[2] && items[3])))
  228. {
  229. LogWrite(INIT__ERROR, 0, "Init", "Incomplete %s..", lsInfoFile);
  230. return false;
  231. }
  232. LogWrite(INIT__INFO, 0, "Init", "%s loaded..", lsInfoFile);
  233. LogWrite(INIT__INFO, 0, "Init", "Database init begin..");
  234. //remove this when all database calls are using the new database class
  235. if (!database.Init()) {
  236. LogWrite(INIT__ERROR, 0, "Init", "Database init FAILED!");
  237. LogStop();
  238. return false;
  239. }
  240. LogWrite(INIT__INFO, 0, "Init", "Loading opcodes 2.0..");
  241. EQOpcodeVersions = database.GetVersions();
  242. map<int16,int16>::iterator version_itr2;
  243. int16 version1 = 0;
  244. for (version_itr2 = EQOpcodeVersions.begin(); version_itr2 != EQOpcodeVersions.end(); version_itr2++) {
  245. version1 = version_itr2->first;
  246. EQOpcodeManager[version1] = new RegularOpcodeManager();
  247. map<string, uint16> eq = database.GetOpcodes(version1);
  248. if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
  249. LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. void NetConnection::UpdateWindowTitle(char* iNewTitle) {
  256. #ifdef WIN32
  257. char tmp[500];
  258. if (iNewTitle) {
  259. snprintf(tmp, sizeof(tmp), "Login: %s", iNewTitle);
  260. }
  261. else {
  262. snprintf(tmp, sizeof(tmp), "%s, Version: %s: %i Server(s), %i Client(s) Connected", EQ2EMU_MODULE, CURRENT_VERSION, net.numservers, net.numclients);
  263. }
  264. SetConsoleTitle(tmp);
  265. #endif
  266. }
  267. void NetConnection::WelcomeHeader()
  268. {
  269. #ifdef _WIN32
  270. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  271. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  272. #endif
  273. printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
  274. #ifdef _WIN32
  275. SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
  276. #endif
  277. printf("\n\nCopyright (C) 2007-2020 EQ2Emulator. https://www.eq2emu.com \n\n");
  278. printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
  279. printf("it under the terms of the GNU General Public License as published by\n");
  280. printf("the Free Software Foundation, either version 3 of the License, or\n");
  281. printf("(at your option) any later version.\n\n");
  282. printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
  283. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  284. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  285. printf("GNU General Public License for more details.\n\n");
  286. #ifdef _WIN32
  287. SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
  288. #endif
  289. printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
  290. printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
  291. printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
  292. printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
  293. printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
  294. printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
  295. printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
  296. printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
  297. printf(" \\__/ \n\n");
  298. #ifdef _WIN32
  299. SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
  300. #endif
  301. printf(" Website : https://eq2emu.com \n");
  302. printf(" Wiki : http://eq2emu.com:3001/ \n");
  303. printf(" Git : http://git.eq2emu.com \n");
  304. printf(" Discord : https://discord.gg/j92Ay9H \n\n");
  305. #ifdef _WIN32
  306. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  307. #endif
  308. printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
  309. #ifdef _WIN32
  310. SetConsoleTextAttribute(console, FOREGROUND_WHITE);
  311. #endif
  312. fflush(stdout);
  313. }