9
3

net.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. srand(time(NULL));
  62. if(!net.ReadLoginConfig())
  63. return 1;
  64. const char* structList[] = { "CommonStructs.xml", "LoginStructs.xml" };
  65. for (int s = 0; s < sizeof(structList) / sizeof(const char*); s++)
  66. {
  67. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s..", structList[s]);
  68. if (configReader.processXML_Elements(structList[s]))
  69. LogWrite(INIT__INFO, 0, "Init", "Loading Structs File %s completed..", structList[s]);
  70. else
  71. {
  72. LogWrite(INIT__ERROR, 0, "Init", "Loading Structs File %s FAILED!", structList[s]);
  73. return 1;
  74. }
  75. }
  76. LogWrite(INIT__INFO, 0, "Init", "Initialize World List..");
  77. world_list.Init();
  78. if(eqsf.listen_ip_address)
  79. LogWrite(INIT__INFO, 0, "Init", "Login server listening on %s port %i", eqsf.listen_ip_address, net.GetPort());
  80. else
  81. LogWrite(INIT__INFO, 0, "Init", "Login server listening on port %i", net.GetPort());
  82. /*}
  83. else {
  84. cout << "EQNetworkServer.Open() error" << endl;
  85. return 1;
  86. }*/
  87. if (!eqsf.Open(net.GetPort())) {
  88. LogWrite(INIT__ERROR, 0, "Init", "Failed to open port %i.", net.GetPort());
  89. return 1;
  90. }
  91. net.UpdateWindowTitle();
  92. EQStream* eqs;
  93. Timer* TimeoutTimer = new Timer(5000);
  94. TimeoutTimer->Start();
  95. while(RunLoops) {
  96. Timer::SetCurrentTime();
  97. while ((eqs = eqsf.Pop())) {
  98. struct in_addr in;
  99. in.s_addr = eqs->GetRemoteIP();
  100. LogWrite(LOGIN__INFO, 0, "Login", "New client from IP: %s on port %i", inet_ntoa(in), ntohs(eqs->GetRemotePort()));
  101. Client* client = new Client(eqs);
  102. eqs->SetClientVersion(0);
  103. client_list.Add(client);
  104. net.numclients++;
  105. net.UpdateWindowTitle();
  106. }
  107. if(TimeoutTimer->Check()){
  108. eqsf.CheckTimeout();
  109. }
  110. if(statTimer.Check()){
  111. world_list.UpdateWorldStats();
  112. database.RemoveOldWorldServerStats();
  113. database.FixBugReport();
  114. }
  115. client_list.Process();
  116. world_list.Process();
  117. #ifdef WIN32
  118. if(kbhit())
  119. {
  120. int hitkey = getch();
  121. net.HitKey(hitkey);
  122. }
  123. #endif
  124. Sleep(1);
  125. }
  126. //close
  127. //eqns.Close();
  128. eqsf.Close();
  129. world_list.Shutdown();
  130. return 0;
  131. }
  132. #ifdef WIN32
  133. void NetConnection::HitKey(int keyhit)
  134. {
  135. switch(keyhit)
  136. {
  137. case 'l':
  138. case 'L': {
  139. world_list.ListWorldsToConsole();
  140. break;
  141. }
  142. case '1':
  143. world_list.SendWorldChanged(1, true);
  144. break;
  145. case '2':
  146. world_list.SendWorldChanged(1, false);
  147. break;
  148. case 'H':
  149. case 'h': {
  150. printf("============================\n");
  151. printf("Available Commands:\n");
  152. printf("l = Listing of World Servers\n");
  153. printf("v = Login Version\n");
  154. // printf("0 = Kick all connected world servers\n");
  155. printf("============================\n");
  156. break;
  157. }
  158. default:
  159. printf("Invalid Command.\n");
  160. break;
  161. }
  162. }
  163. #endif
  164. void CatchSignal(int sig_num) {
  165. cout << "Got signal " << sig_num << endl;
  166. RunLoops = false;
  167. }
  168. bool NetConnection::ReadLoginConfig() {
  169. char buf[201], type[201];
  170. int8 items[5] = {0, 0, 0, 0, 0};
  171. FILE *f;
  172. char* lsInfoFile = { "LoginServer.ini" };
  173. if (!(f = fopen (lsInfoFile, "r"))) {
  174. LogWrite(INIT__ERROR, 0, "Init", "File %s could not be found..", lsInfoFile);
  175. return false;
  176. }
  177. do {
  178. fgets (buf, 200, f);
  179. if (feof (f))
  180. {
  181. LogWrite(INIT__ERROR, 0, "Init", "[LoginConfig] block from %s missing..", lsInfoFile);
  182. fclose (f);
  183. return false;
  184. }
  185. }
  186. while (strncasecmp (buf, "[LoginConfig]\n", 14) != 0 && strncasecmp (buf, "[LoginConfig]\r\n", 15) != 0);
  187. while (!feof (f))
  188. {
  189. #ifdef WIN32
  190. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  191. #else
  192. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  193. #endif
  194. {
  195. items[0] = 1;
  196. LoginMode = Standalone;
  197. items[1] = 1;
  198. items[2] = 1;
  199. items[3] = 1;
  200. if (!strncasecmp(type, "[", 1)) {
  201. // new block after LoginConfig, skip
  202. break;
  203. }
  204. if (!strncasecmp(type, "serverport", 10)) {
  205. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  206. port = atoi(buf);
  207. }
  208. }
  209. if (!strncasecmp(type, "serverip", 8)) {
  210. eqsf.listen_ip_address = new char[sizeof(buf) + 1];
  211. strcpy(eqsf.listen_ip_address, buf);
  212. }
  213. if (!strncasecmp(type, "accountcreation", 15)) {
  214. if (Seperator::IsNumber(buf)) {
  215. allowAccountCreation = atoi(buf);
  216. }
  217. }
  218. }
  219. }
  220. fclose (f);
  221. if (!items[0] || (LoginMode == Slave && !(items[1] && items[2] && items[3])) || (items[4] && !(items[2] && items[3])))
  222. {
  223. LogWrite(INIT__ERROR, 0, "Init", "Incomplete %s..", lsInfoFile);
  224. return false;
  225. }
  226. LogWrite(INIT__INFO, 0, "Init", "%s loaded..", lsInfoFile);
  227. LogWrite(INIT__INFO, 0, "Init", "Database init begin..");
  228. //remove this when all database calls are using the new database class
  229. if (!database.Init()) {
  230. LogWrite(INIT__ERROR, 0, "Init", "Database init FAILED!");
  231. LogStop();
  232. return EXIT_FAILURE;
  233. }
  234. LogWrite(INIT__INFO, 0, "Init", "Loading opcodes 2.0..");
  235. EQOpcodeVersions = database.GetVersions();
  236. map<int16,int16>::iterator version_itr2;
  237. int16 version1 = 0;
  238. for (version_itr2 = EQOpcodeVersions.begin(); version_itr2 != EQOpcodeVersions.end(); version_itr2++) {
  239. version1 = version_itr2->first;
  240. EQOpcodeManager[version1] = new RegularOpcodeManager();
  241. map<string, uint16> eq = database.GetOpcodes(version1);
  242. if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
  243. LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. void NetConnection::UpdateWindowTitle(char* iNewTitle) {
  250. #ifdef WIN32
  251. char tmp[500];
  252. if (iNewTitle) {
  253. snprintf(tmp, sizeof(tmp), "%s", iNewTitle);
  254. }
  255. else {
  256. snprintf(tmp, sizeof(tmp), "%i Server(s), %i Client(s) Connected", net.numservers, net.numclients);
  257. }
  258. SetConsoleTitle(tmp);
  259. #endif
  260. }