net.cpp 7.1 KB

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