9
3

net.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. if (!strncasecmp(type, "expansionflag", 13)) {
  225. if (Seperator::IsNumber(buf)) {
  226. expansionFlag = atoul(buf);
  227. }
  228. }
  229. if (!strncasecmp(type, "citiesflag", 10)) {
  230. if (Seperator::IsNumber(buf)) {
  231. citiesFlag = atoul(buf);
  232. }
  233. }
  234. if (!strncasecmp(type, "defaultsubscriptionlevel", 24)) {
  235. if (Seperator::IsNumber(buf)) {
  236. defaultSubscriptionLevel = atoul(buf);
  237. }
  238. }
  239. if (!strncasecmp(type, "enabledraces", 12)) {
  240. if (Seperator::IsNumber(buf)) {
  241. enabledRaces = atoul(buf);
  242. }
  243. }
  244. }
  245. }
  246. fclose (f);
  247. if (!items[0] || (LoginMode == Slave && !(items[1] && items[2] && items[3])) || (items[4] && !(items[2] && items[3])))
  248. {
  249. LogWrite(INIT__ERROR, 0, "Init", "Incomplete %s..", lsInfoFile);
  250. return false;
  251. }
  252. LogWrite(INIT__INFO, 0, "Init", "%s loaded..", lsInfoFile);
  253. LogWrite(INIT__INFO, 0, "Init", "Database init begin..");
  254. //remove this when all database calls are using the new database class
  255. if (!database.Init()) {
  256. LogWrite(INIT__ERROR, 0, "Init", "Database init FAILED!");
  257. LogStop();
  258. return false;
  259. }
  260. LogWrite(INIT__INFO, 0, "Init", "Loading opcodes 2.0..");
  261. EQOpcodeVersions = database.GetVersions();
  262. map<int16,int16>::iterator version_itr2;
  263. int16 version1 = 0;
  264. for (version_itr2 = EQOpcodeVersions.begin(); version_itr2 != EQOpcodeVersions.end(); version_itr2++) {
  265. version1 = version_itr2->first;
  266. EQOpcodeManager[version1] = new RegularOpcodeManager();
  267. map<string, uint16> eq = database.GetOpcodes(version1);
  268. if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
  269. LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. void NetConnection::UpdateWindowTitle(char* iNewTitle) {
  276. #ifdef WIN32
  277. char tmp[500];
  278. if (iNewTitle) {
  279. snprintf(tmp, sizeof(tmp), "Login: %s", iNewTitle);
  280. }
  281. else {
  282. snprintf(tmp, sizeof(tmp), "%s, Version: %s: %i Server(s), %i Client(s) Connected", EQ2EMU_MODULE, CURRENT_VERSION, net.numservers, net.numclients);
  283. }
  284. SetConsoleTitle(tmp);
  285. #endif
  286. }
  287. void NetConnection::WelcomeHeader()
  288. {
  289. #ifdef _WIN32
  290. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  291. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  292. #endif
  293. printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
  294. #ifdef _WIN32
  295. SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
  296. #endif
  297. printf("\n\nCopyright (C) 2007-2021 EQ2Emulator. https://www.eq2emu.com \n\n");
  298. printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
  299. printf("it under the terms of the GNU General Public License as published by\n");
  300. printf("the Free Software Foundation, either version 3 of the License, or\n");
  301. printf("(at your option) any later version.\n\n");
  302. printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
  303. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  304. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  305. printf("GNU General Public License for more details.\n\n");
  306. #ifdef _WIN32
  307. SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
  308. #endif
  309. printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
  310. printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
  311. printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
  312. printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
  313. printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
  314. printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
  315. printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
  316. printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
  317. printf(" \\__/ \n\n");
  318. #ifdef _WIN32
  319. SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
  320. #endif
  321. printf(" Website : https://eq2emu.com \n");
  322. printf(" Wiki : https://wiki.eq2emu.com \n");
  323. printf(" Git : https://git.eq2emu.com \n");
  324. printf(" Discord : https://discord.gg/5Cavm9NYQf \n\n");
  325. #ifdef _WIN32
  326. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  327. #endif
  328. printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
  329. #ifdef _WIN32
  330. SetConsoleTextAttribute(console, FOREGROUND_WHITE);
  331. #endif
  332. fflush(stdout);
  333. }