LWorld.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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 <string.h>
  8. #include <stdarg.h>
  9. #ifdef WIN32
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. #include <winsock.h>
  13. #include <process.h>
  14. #else
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include "../common/unix.h"
  22. #define SOCKET_ERROR -1
  23. #define INVALID_SOCKET -1
  24. extern int errno;
  25. #endif
  26. #include "../common/servertalk.h"
  27. #include "LWorld.h"
  28. #include "net.h"
  29. #include "client.h"
  30. #include "../common/packet_dump.h"
  31. #include "login_opcodes.h"
  32. #include "login_structs.h"
  33. #include "LoginDatabase.h"
  34. #include "PacketHeaders.h"
  35. #include "../common/ConfigReader.h"
  36. #ifdef WIN32
  37. #define snprintf _snprintf
  38. #define vsnprintf _vsnprintf
  39. #define strncasecmp _strnicmp
  40. #define strcasecmp _stricmp
  41. #endif
  42. extern ClientList client_list;
  43. extern NetConnection net;
  44. extern LWorldList world_list;
  45. extern LoginDatabase database;
  46. extern ConfigReader configReader;
  47. extern volatile bool RunLoops;
  48. #include "../common/Log.h"
  49. using namespace std;
  50. LWorld::LWorld(TCPConnection* in_con, bool in_OutgoingLoginUplink, int32 iIP, int16 iPort, bool iNeverKick) {
  51. Link = in_con;
  52. RemoteID = 0;
  53. LinkWorldID = 0;
  54. if (iIP)
  55. ip = iIP;
  56. else
  57. ip = in_con->GetrIP();
  58. struct in_addr in;
  59. in.s_addr = in_con->GetrIP();
  60. char* ipadd = inet_ntoa(in);
  61. if(ipadd)
  62. strncpy(IPAddr,ipadd,64);
  63. if (iPort)
  64. port = iPort;
  65. else
  66. port = in_con->GetrPort();
  67. ID = 0;
  68. pClientPort = 0;
  69. memset(account, 0, sizeof(account));
  70. memset(address, 0, sizeof(address));
  71. memset(worldname, 0, sizeof(worldname));
  72. status = 0;
  73. accountid = 0;
  74. admin_id = 0;
  75. IsInit = false;
  76. kicked = false;
  77. pNeverKick = iNeverKick;
  78. pPlaceholder = false;
  79. pshowdown = false;
  80. pConnected = in_con->Connected();
  81. pReconnectTimer = 0;
  82. pStatsTimer = NULL;
  83. isAuthenticated = false;
  84. if (in_OutgoingLoginUplink) {
  85. pClientPort = port;
  86. ptype = Login;
  87. OutgoingUplink = true;
  88. if (net.GetLoginMode() == Mesh) {
  89. pReconnectTimer = new Timer(INTERSERVER_TIMER);
  90. pReconnectTimer->Trigger();
  91. }
  92. }
  93. else {
  94. ptype = UnknownW;
  95. OutgoingUplink = false;
  96. }
  97. in.s_addr = GetIP();
  98. strcpy(address, inet_ntoa(in));
  99. isaddressip = true;
  100. num_players = 0;
  101. num_zones = 0;
  102. }
  103. LWorld::LWorld(int32 in_accountid, char* in_accountname, char* in_worldname, int32 in_admin_id) {
  104. pPlaceholder = true;
  105. Link = 0;
  106. ip = 0;
  107. port = 0;
  108. ID = 0;
  109. strcpy(IPAddr,"");
  110. pClientPort = 0;
  111. memset(account, 0, sizeof(account));
  112. memset(address, 0, sizeof(address));
  113. memset(worldname, 0, sizeof(worldname));
  114. status = 0;
  115. accountid = in_accountid;
  116. admin_id = in_admin_id;
  117. IsInit = false;
  118. kicked = false;
  119. pNeverKick = false;
  120. pshowdown = true;
  121. RemoteID = 0;
  122. LinkWorldID = 0;
  123. OutgoingUplink = false;
  124. pReconnectTimer = 0;
  125. pConnected = false;
  126. pStatsTimer = NULL;
  127. ptype = World;
  128. strcpy(account, in_accountname);
  129. strcpy(worldname, in_worldname);
  130. strcpy(address, "none");
  131. isaddressip = true;
  132. num_players = 0;
  133. num_zones = 0;
  134. }
  135. LWorld::LWorld(TCPConnection* in_RemoteLink, int32 in_ip, int32 in_RemoteID, int32 in_accountid, char* in_accountname, char* in_worldname, char* in_address, sint32 in_status, int32 in_adminid, bool in_showdown, int8 in_authlevel, bool in_placeholder, int32 iLinkWorldID) {
  136. Link = in_RemoteLink;
  137. RemoteID = in_RemoteID;
  138. LinkWorldID = iLinkWorldID;
  139. ip = in_ip;
  140. struct in_addr in;
  141. if(in_RemoteLink)
  142. in.s_addr = in_RemoteLink->GetrIP();
  143. else if (in_ip)
  144. in.s_addr = in_ip;
  145. char* ipadd = inet_ntoa(in);
  146. if(ipadd)
  147. strncpy(IPAddr,ipadd,64);
  148. port = 0;
  149. ID = 0;
  150. pClientPort = 0;
  151. memset(account, 0, sizeof(account));
  152. memset(address, 0, sizeof(address));
  153. memset(worldname, 0, sizeof(worldname));
  154. status = in_status;
  155. accountid = in_accountid;
  156. admin_id = in_adminid;
  157. IsInit = true;
  158. kicked = false;
  159. pNeverKick = false;
  160. pPlaceholder = in_placeholder;
  161. pshowdown = in_showdown;
  162. OutgoingUplink = false;
  163. pReconnectTimer = 0;
  164. pConnected = true;
  165. pStatsTimer = NULL;
  166. ptype = World;
  167. strcpy(account, in_accountname);
  168. strcpy(worldname, in_worldname);
  169. strcpy(address, in_address);
  170. isaddressip = false;
  171. num_players = 0;
  172. num_zones = 0;
  173. }
  174. LWorld::~LWorld() {
  175. safe_delete ( pStatsTimer );
  176. num_zones = 0;
  177. num_players = 0;
  178. database.UpdateWorldServerStats(this, -4);
  179. if (ptype == World && RemoteID == 0) {
  180. if (net.GetLoginMode() != Mesh || (!pPlaceholder && IsInit)) {
  181. ServerPacket* pack = new ServerPacket(ServerOP_WorldListRemove, sizeof(int32));
  182. *((int32*) pack->pBuffer) = GetID();
  183. world_list.SendPacketLogin(pack);
  184. delete pack;
  185. }
  186. }
  187. if (Link != 0 && RemoteID == 0) {
  188. world_list.RemoveByLink(Link, 0, this);
  189. if (OutgoingUplink)
  190. delete Link;
  191. else
  192. Link->Free();
  193. }
  194. Link = 0;
  195. safe_delete(pReconnectTimer);
  196. world_list.RemoveByID ( this->GetID ( ) );
  197. }
  198. bool LWorld::Process() {
  199. bool ret = true;
  200. if (Link == 0)
  201. return true;
  202. if (Link->Connected()) {
  203. if (!pConnected) {
  204. pConnected = true;
  205. }
  206. }
  207. else {
  208. pConnected = false;
  209. if (pReconnectTimer) {
  210. if (pReconnectTimer->Check() && Link->ConnectReady()) {
  211. pReconnectTimer->Start(pReconnectTimer->GetTimerTime() + (rand()%15000), false);
  212. }
  213. return true;
  214. }
  215. return false;
  216. }
  217. if (RemoteID != 0)
  218. return true;
  219. if(pStatsTimer && pStatsTimer->Check())
  220. {
  221. if(isAuthenticated && (database.IsServerAccountDisabled(account) || database.IsIPBanned(IPAddr)))
  222. {
  223. this->Kick(ERROR_BADPASSWORD);
  224. return false;
  225. }
  226. database.UpdateWorldServerStats(this, GetStatus());
  227. }
  228. ServerPacket* pack = 0;
  229. while (ret && (pack = Link->PopPacket())) {
  230. // this stops connections from sending invalid packets without first authenticating
  231. // with the login server to show it is a legit server
  232. if(!isAuthenticated && pack->opcode != ServerOP_LSInfo)
  233. {
  234. Kick("This connection has not authenticated.");
  235. break;
  236. }
  237. switch(pack->opcode) {
  238. case 0:
  239. break;
  240. case ServerOP_KeepAlive: {
  241. // ignore this
  242. break;
  243. }
  244. case ServerOP_LSFatalError: {
  245. net.Uplink_WrongVersion = true;
  246. ret = false;
  247. kicked = true;
  248. break;
  249. }
  250. case ServerOP_CharacterCreate: {
  251. WorldCharNameFilterResponse_Struct* wcnfr = (WorldCharNameFilterResponse_Struct*) pack->pBuffer;
  252. Client* client = client_list.FindByLSID(wcnfr->account_id);
  253. if(!client){
  254. if(wcnfr->account_id == 0){
  255. client_list.FindByCreateRequest();
  256. }
  257. break;
  258. }
  259. if(wcnfr->response == 1)
  260. {
  261. client->CharacterApproved(GetID(),wcnfr->char_id);
  262. }
  263. else
  264. {
  265. client->CharacterRejected(wcnfr->response);
  266. }
  267. break;
  268. }
  269. case ServerOP_UsertoWorldReq: {
  270. UsertoWorldRequest_Struct* ustwr = (UsertoWorldRequest_Struct*) pack->pBuffer;
  271. if (ustwr->ToID) {
  272. LWorld* world = world_list.FindByID(ustwr->ToID);
  273. if (!world) {
  274. break;
  275. }
  276. if (this->GetType() != Login) {
  277. break;
  278. }
  279. ustwr->FromID = this->GetID();
  280. world->SendPacket(pack);
  281. }
  282. break;
  283. }
  284. case ServerOP_UsertoWorldResp: {
  285. if (pack->size != sizeof(UsertoWorldResponse_Struct))
  286. break;
  287. UsertoWorldResponse_Struct* seps = (UsertoWorldResponse_Struct*) pack->pBuffer;
  288. if (seps->ToID) {
  289. LWorld* world = world_list.FindByID(seps->ToID);
  290. if (this->GetType() != Login) {
  291. break;
  292. }
  293. if (world) {
  294. seps->ToID = world->GetRemoteID();
  295. world->SendPacket(pack);
  296. }
  297. }
  298. else {
  299. Client* client = 0;
  300. client = client_list.FindByLSID(seps->lsaccountid);
  301. if(client == 0)
  302. break;
  303. if(this->GetID() != seps->worldid && this->GetType() != Login)
  304. break;
  305. client->WorldResponse(GetID(),seps->response, seps->ip_address, seps->port, seps->access_key);
  306. }
  307. break;
  308. }
  309. case ServerOP_CharTimeStamp: { // This is being sent to synch a new timestamp on the login server
  310. if(pack->size != sizeof(CharacterTimeStamp_Struct))
  311. break;
  312. CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) pack->pBuffer;
  313. if(!database.UpdateCharacterTimeStamp(cts->account_id,cts->char_id,cts->unix_timestamp,GetAccountID()))
  314. printf("TimeStamp update error with character id %i of account id %i on server %i\n",cts->char_id,cts->account_id,GetAccountID());
  315. //Todo: Synch with the other login servers
  316. break;
  317. }
  318. case ServerOP_GetTableData:{
  319. Kick("This is not an update server, update your LoginServer.ini file.");
  320. break;
  321. }
  322. case ServerOP_GetTableQuery:{
  323. Kick("This is not an update server, update your LoginServer.ini file.");
  324. break;
  325. }
  326. case ServerOP_GetLatestTables:{
  327. Kick("This is not an update server, update your LoginServer.ini file.");
  328. break;
  329. }
  330. case ServerOP_ZoneUpdate:{
  331. if(pack->size > CHARZONESTRUCT_MAXSIZE)
  332. break;
  333. CharZoneUpdate_Struct* czu = (CharZoneUpdate_Struct*)pack->pBuffer;
  334. database.UpdateCharacterZone(czu->account_id, czu->char_id, czu->zone_id, GetAccountID());
  335. break;
  336. }
  337. case ServerOP_RaceUpdate: {
  338. if(pack->size != sizeof(RaceUpdate_Struct))
  339. break;
  340. RaceUpdate_Struct* ru = (RaceUpdate_Struct*) pack->pBuffer;
  341. database.UpdateCharacterRace(ru->account_id , ru->char_id , ru->model_type , ru->race , this->GetAccountID ( ));
  342. break;
  343. }
  344. case ServerOP_BasicCharUpdate: {
  345. if(pack->size != sizeof(CharDataUpdate_Struct))
  346. break;
  347. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*) pack->pBuffer;
  348. switch(cdu->update_field)
  349. {
  350. case LEVEL_UPDATE_FLAG:
  351. {
  352. database.UpdateCharacterLevel(cdu->account_id,cdu->char_id,cdu->update_data,this->GetAccountID());
  353. break;
  354. }
  355. case CLASS_UPDATE_FLAG:
  356. {
  357. database.UpdateCharacterClass(cdu->account_id,cdu->char_id,cdu->update_data,this->GetAccountID());
  358. break;
  359. }
  360. case GENDER_UPDATE_FLAG:
  361. {
  362. database.UpdateCharacterGender(cdu->account_id,cdu->char_id,cdu->update_data,this->GetAccountID());
  363. break;
  364. }
  365. case DELETE_UPDATE_FLAG:
  366. {
  367. if(cdu->update_field == 1)
  368. database.DeleteCharacter(cdu->account_id,cdu->char_id,this->GetAccountID());
  369. break;
  370. }
  371. }
  372. break;
  373. }
  374. case ServerOP_LSInfo: {
  375. if (pack->size != sizeof(ServerLSInfo_Struct)) {
  376. this->Kick(ERROR_BADVERSION);
  377. ret = false;
  378. //struct in_addr in;
  379. //in.s_addr = GetIP();
  380. }
  381. else {
  382. ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
  383. if (strcmp(lsi->protocolversion, EQEMU_PROTOCOL_VERSION) != 0 || !database.CheckVersion(lsi->serverversion)) {
  384. cout << "ERROR - KICK BAD VERSION: Got versions: protocol: '" << lsi->protocolversion << "', database version: " << lsi->serverversion << endl;
  385. cout << "To allow all world server versions to login, run query on your login database (alternatively replacing * with the database version if preferred): insert into login_versions set version = '*';" << endl;
  386. this->Kick(ERROR_BADVERSION);
  387. ret = false;
  388. //struct in_addr in;
  389. //in.s_addr = GetIP();
  390. }
  391. else if (!SetupWorld(lsi->name, lsi->address, lsi->account, lsi->password, lsi->serverversion)) {
  392. this->Kick(ERROR_BADPASSWORD);
  393. ret = false;
  394. //struct in_addr in;
  395. //in.s_addr = GetIP();
  396. }
  397. else{
  398. isAuthenticated = true;
  399. devel_server = (lsi->servertype == 4);
  400. }
  401. }
  402. break;
  403. }
  404. case ServerOP_LSStatus: {
  405. ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
  406. if(lss->num_players > 5000 || lss->num_zones > 500) {
  407. this->Kick("Your server has exceeded a number of players and/or zone limit.");
  408. ret = false;
  409. break;
  410. }
  411. UpdateStatus(lss->status, lss->num_players, lss->num_zones);
  412. break;
  413. }
  414. case ServerOP_SystemwideMessage: {
  415. if (this->GetType() == Login) {
  416. // no looping plz =p
  417. //world_list.SendPacket(pack, this);
  418. }
  419. else if (this->GetType() == Chat) {
  420. world_list.SendPacket(pack);
  421. }
  422. else {
  423. }
  424. break;
  425. }
  426. case ServerOP_ListWorlds: {
  427. if (pack->size <= 1 || pack->pBuffer[pack->size - 1] != 0) {
  428. break;
  429. }
  430. world_list.SendWorldStatus(this, (char*) pack->pBuffer);
  431. break;
  432. }
  433. case ServerOP_WorldListUpdate: {
  434. break;
  435. }
  436. case ServerOP_WorldListRemove: {
  437. if (this->GetType() != Login) {
  438. // cout << "Error: ServerOP_WorldListRemove from a non-login connection? WTF!" << endl;
  439. break;
  440. }
  441. if (pack->size != sizeof(int32)) {
  442. // cout << "Wrong size on ServerOP_WorldListRemove. Got: " << pack->size << ", Expected: " << sizeof(int32) << endl;
  443. break;
  444. }
  445. cout << "Got world remove for remote #" << *((int32*) pack->pBuffer) << endl;
  446. if ((*((int32*) pack->pBuffer)) > 0) {
  447. LWorld* world = world_list.FindByLink(this->GetLink(), *((int32*) pack->pBuffer));
  448. if (world && world->GetRemoteID() != 0) {
  449. *((int32*) pack->pBuffer) = world->GetID();
  450. if (net.GetLoginMode() != Mesh)
  451. world_list.SendPacketLogin(pack, this);
  452. world_list.RemoveByID(*((int32*) pack->pBuffer));
  453. }
  454. }
  455. else {
  456. // cout << "Error: ServerOP_WorldListRemove: ID = 0? ops!" << endl;
  457. }
  458. break;
  459. }
  460. case ServerOP_TriggerWorldListRefresh: {
  461. world_list.UpdateWorldList();
  462. if (net.GetLoginMode() != Mesh)
  463. world_list.SendPacketLogin(pack, this);
  464. break;
  465. }
  466. case ServerOP_ZoneUpdates:{
  467. pack->Inflate();
  468. ZoneUpdateList_Struct* updates = 0;
  469. if(pack->size >= sizeof(ZoneUpdateList_Struct) && ((ZoneUpdateList_Struct*)pack->pBuffer)->total_updates <= MAX_UPDATE_COUNT){
  470. updates = (ZoneUpdateList_Struct*)pack->pBuffer;
  471. ZoneUpdate_Struct* zone = 0;
  472. int32 pos = sizeof(ZoneUpdateList_Struct);
  473. sint16 num_updates = 0;
  474. map<int32, LoginZoneUpdate> zone_updates;
  475. LoginZoneUpdate update;
  476. while(pos < pack->size && num_updates < updates->total_updates){
  477. zone = (ZoneUpdate_Struct*)(pack->pBuffer+pos);
  478. if((pos + zone->zone_name_length + zone->zone_desc_length + sizeof(ZoneUpdate_Struct)) <= pack->size){
  479. update.name = string(zone->data, zone->zone_name_length);
  480. update.description = string(zone->data + zone->zone_name_length, zone->zone_desc_length);
  481. pos += sizeof(ZoneUpdate_Struct) + zone->zone_name_length + zone->zone_desc_length;
  482. num_updates++;
  483. zone_updates[zone->zone_id] = update;
  484. }
  485. else
  486. break;
  487. }
  488. if(zone_updates.size() == updates->total_updates)
  489. world_list.AddServerZoneUpdates(this, zone_updates);
  490. else
  491. cout << "Error processing zone updates for server: " << GetAccount() << endl;
  492. }
  493. else
  494. Kick("Possible Hacking Attempt");
  495. break;
  496. }
  497. case ServerOP_LoginEquipment: {
  498. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode %04X (%i): ServerOP_LoginEquipment", pack->opcode, pack->opcode);
  499. pack->Inflate();
  500. EquipmentUpdateList_Struct* updates = 0;
  501. if(pack->size >= sizeof(EquipmentUpdateList_Struct) && ((EquipmentUpdateList_Struct*)pack->pBuffer)->total_updates <= MAX_LOGIN_APPEARANCE_COUNT){
  502. updates = (EquipmentUpdateList_Struct*)pack->pBuffer;
  503. EquipmentUpdate_Struct* equip = 0;
  504. int32 pos = sizeof(EquipmentUpdateList_Struct);
  505. sint16 num_updates = 0;
  506. map<int32, LoginEquipmentUpdate> equip_updates;
  507. LoginEquipmentUpdate update;
  508. while(pos < pack->size && num_updates < updates->total_updates){
  509. equip = (EquipmentUpdate_Struct*)(pack->pBuffer+pos);
  510. update.world_char_id = equip->world_char_id;
  511. update.equip_type = equip->equip_type;
  512. update.red = equip->red;
  513. update.green = equip->green;
  514. update.blue = equip->blue;
  515. update.highlight_red = equip->highlight_red;
  516. update.highlight_green = equip->highlight_green;
  517. update.highlight_blue = equip->highlight_blue;
  518. update.slot = equip->slot;
  519. pos += sizeof(EquipmentUpdate_Struct);
  520. num_updates++;
  521. equip_updates[equip->id] = update; // JohnAdams: I think I need item_appearances.id from World here?
  522. }
  523. LogWrite(LOGIN__DEBUG, 1, "Login", "Processing %i Login Appearance Updates...", num_updates);
  524. if(equip_updates.size() == updates->total_updates)
  525. {
  526. world_list.AddServerEquipmentUpdates(this, equip_updates);
  527. }
  528. else
  529. {
  530. LogWrite(LOGIN__ERROR, 0, "Login", "Error processing login appearance updates for server: %s\n\t%s, function %s, line %i", GetAccount(), __FILE__, __FUNCTION__, __LINE__);
  531. }
  532. }
  533. else
  534. {
  535. LogWrite(LOGIN__ERROR, 0, "Login", "World ID '%i', Possible Hacking Attempt (func: %s, line: %i", GetAccountID(), __FUNCTION__, __LINE__);
  536. Kick("Possible Hacking Attempt");
  537. }
  538. break;
  539. }
  540. case ServerOP_BugReport:{
  541. if(pack->size == sizeof(BugReport)){
  542. BugReport* report = (BugReport*)pack->pBuffer;
  543. database.SaveBugReport(GetAccountID(), report->category, report->subcategory, report->causes_crash, report->reproducible, report->summary, report->description, report->version, report->player, report->account_id, report->spawn_name, report->spawn_id, report->zone_id);
  544. }
  545. break;
  546. }
  547. case ServerOP_EncapPacket: {
  548. if (this->GetType() != Login) {
  549. // cout << "Error: ServerOP_EncapPacket from a non-login connection? WTF!" << endl;
  550. break;
  551. }
  552. ServerEncapPacket_Struct* seps = (ServerEncapPacket_Struct*) pack->pBuffer;
  553. if (seps->ToID == 0xFFFFFFFF) { // Broadcast
  554. ServerPacket* inpack = new ServerPacket(seps->opcode);
  555. inpack->size = seps->size;
  556. // Little trick here to save a memcpy, be careful if you change this any
  557. inpack->pBuffer = seps->data;
  558. world_list.SendPacketLocal(inpack, this);
  559. inpack->pBuffer = 0;
  560. delete inpack;
  561. }
  562. else {
  563. LWorld* world = world_list.FindByID(seps->ToID);
  564. if (world) {
  565. ServerPacket* inpack = new ServerPacket(seps->opcode);
  566. inpack->size = seps->size;
  567. // Little trick here to save a memcpy, be careful if you change this any
  568. inpack->pBuffer = seps->data;
  569. world->SendPacket(inpack);
  570. inpack->pBuffer = 0;
  571. delete inpack;
  572. }
  573. }
  574. if (net.GetLoginMode() != Mesh)
  575. world_list.SendPacketLogin(pack, this);
  576. break;
  577. }
  578. default:
  579. {
  580. cout << "Unknown LoginSOPcode: 0x" << hex << (int)pack->opcode << dec;
  581. cout << " size:" << pack->size << " from " << GetAccount() << endl;
  582. DumpPacket(pack->pBuffer, pack->size);
  583. //Kick("Possible Hacking Attempt");
  584. break;
  585. }
  586. }
  587. delete pack;
  588. }
  589. return ret;
  590. }
  591. void LWorld::SendPacket(ServerPacket* pack) {
  592. if (Link == 0)
  593. return;
  594. if (RemoteID) {
  595. ServerPacket* outpack = new ServerPacket(ServerOP_EncapPacket, sizeof(ServerEncapPacket_Struct) + pack->size);
  596. ServerEncapPacket_Struct* seps = (ServerEncapPacket_Struct*) outpack->pBuffer;
  597. seps->ToID = RemoteID;
  598. seps->opcode = pack->opcode;
  599. seps->size = pack->size;
  600. memcpy(seps->data, pack->pBuffer, pack->size);
  601. Link->SendPacket(outpack);
  602. delete outpack;
  603. }
  604. else {
  605. Link->SendPacket(pack);
  606. }
  607. }
  608. void LWorld::Message(const char* to, const char* message, ...) {
  609. va_list argptr;
  610. char buffer[256];
  611. va_start(argptr, message);
  612. vsnprintf(buffer, 256, message, argptr);
  613. va_end(argptr);
  614. ServerPacket* pack = new ServerPacket(ServerOP_EmoteMessage, sizeof(ServerEmoteMessage_Struct) + strlen(buffer) + 1);
  615. ServerEmoteMessage_Struct* sem = (ServerEmoteMessage_Struct*) pack->pBuffer;
  616. strcpy(sem->to, to);
  617. strcpy(sem->message, buffer);
  618. SendPacket(pack);
  619. delete pack;
  620. }
  621. void LWorld::Kick(const char* message, bool iSetKickedFlag) {
  622. if (iSetKickedFlag)
  623. kicked = true;
  624. if (message) {
  625. ServerPacket* pack = new ServerPacket(ServerOP_LSFatalError, strlen(message) + 1);
  626. strcpy((char*) pack->pBuffer, message);
  627. SendPacket(pack);
  628. delete pack;
  629. }
  630. if (Link && GetRemoteID() == 0)
  631. Link->Disconnect();
  632. }
  633. bool LWorld::CheckServerName(const char* name) {
  634. if (strlen(name) < 10)
  635. return false;
  636. for (size_t i=0; i<strlen(name); i++) {
  637. if (!((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z') || (name[i] >= '0' && name[i] <= '9') || name[i] == ' ' || name[i] == '\'' || name[i] == '-' || name[i] == '(' || name[i] == ')' || name[i] == '[' || name[i] == ']' || name[i] == '/' || name[i] == '.' || name[i] == ',' || name[i] == '_' || name[i] == '+' || name[i] == '=' || name[i] == ':' || name[i] == '~'))
  638. return false;
  639. }
  640. return true;
  641. }
  642. bool LWorld::SetupWorld(char* in_worldname, char* in_worldaddress, char* in_account, char* in_password, char* in_version) {
  643. if (strlen(in_worldaddress) > 3) {
  644. isaddressip = false;
  645. strcpy(address, in_worldaddress);
  646. }
  647. if (strlen(in_worldname) > 3) {
  648. char tmpAccount[30];
  649. memcpy(tmpAccount, in_account, 29);
  650. tmpAccount[29] = '\0';
  651. int32 id = database.CheckServerAccount(tmpAccount, in_password);
  652. if(id == 0)
  653. return false;
  654. if(database.IsServerAccountDisabled(tmpAccount) || database.IsIPBanned(IPAddr) || (isaddressip && database.IsIPBanned(address)))
  655. return false;
  656. LWorld* world = world_list.FindByID(id);
  657. if(world)
  658. world->Kick("Ghost Kick!");
  659. ID = id;
  660. accountid = id;
  661. strncpy(account,tmpAccount,30);
  662. char* name = database.GetServerAccountName(id);
  663. if(name)
  664. snprintf(worldname, (sizeof(worldname)) - 1, "%s", name);
  665. else{ //failed to get account
  666. account[0] = 0;
  667. IsInit = false;
  668. this->Kick ( "Could not load server information." );
  669. return false;
  670. }
  671. //world_list.KickGhostIP(GetIP(), this);
  672. IsInit = true;
  673. ptype = World;
  674. world_list.SendWorldChanged(id, true);
  675. }
  676. else {
  677. // name too short
  678. account[0] = 0;
  679. IsInit = false;
  680. return false;
  681. }
  682. pStatsTimer = new Timer ( 60000 );
  683. pStatsTimer->Start ( 60000 );
  684. return true;
  685. }
  686. void LWorldList::SendWorldChanged(int32 server_id, bool sendtoallclients, Client* sendto){
  687. EQ2Packet* outapp = new EQ2Packet(OP_WorldStatusChangeMsg, 0, sizeof(LS_WorldStatusChanged));
  688. LS_WorldStatusChanged* world_changed = (LS_WorldStatusChanged*)outapp->pBuffer;
  689. world_changed->server_id = server_id;
  690. LWorld* world = world_list.FindByID(server_id);
  691. if(!world || world->ShowDown())
  692. world_changed->up = 0;
  693. else
  694. world_changed->up = 1;
  695. if(sendtoallclients || sendto == 0)
  696. client_list.SendPacketToAllClients(outapp);
  697. else
  698. sendto->QueuePacket(outapp);
  699. world_list.SetUpdateServerList(true);
  700. }
  701. void LWorld::UpdateWorldList(LWorld* to) {
  702. world_list.SetUpdateServerList( true );
  703. }
  704. void LWorld::ChangeToPlaceholder() {
  705. ip = 0;
  706. status = -1;
  707. pPlaceholder = true;
  708. if (Link != 0 && RemoteID == 0) {
  709. Link->Disconnect();
  710. }
  711. UpdateWorldList();
  712. }
  713. void LWorld::SetRemoteInfo(int32 in_ip, int32 in_accountid, char* in_account, char* in_name, char* in_address, int32 in_status, int32 in_adminid, sint32 in_players, sint32 in_zones) {
  714. ip = in_ip;
  715. accountid = in_accountid;
  716. // strcpy(account, in_account);
  717. strcpy(worldname, in_name);
  718. strcpy(address, in_address);
  719. status = in_status;
  720. admin_id = in_adminid;
  721. num_players = in_players;
  722. num_zones = in_zones;
  723. }
  724. LWorldList::LWorldList() {
  725. server_update_thread = true;
  726. NextID = 1;
  727. tcplistener = new TCPServer(net.GetPort(), true);
  728. if (net.GetLoginMode() == Slave)
  729. OutLink = new TCPConnection(true);
  730. else
  731. OutLink = 0;
  732. UpdateServerList = true;
  733. #ifdef WIN32
  734. _beginthread(ServerUpdateLoop, 0, this);
  735. #else
  736. pthread_t thread;
  737. pthread_create(&thread, NULL, &ServerUpdateLoop, this);
  738. #endif
  739. }
  740. LWorldList::~LWorldList() {
  741. server_update_thread = false;
  742. while(!server_update_thread){
  743. Sleep(100);
  744. }
  745. safe_delete(tcplistener);
  746. safe_delete(OutLink);
  747. }
  748. void LWorldList::Shutdown() {
  749. LinkedListIterator<LWorld*> iterator(list);
  750. iterator.Reset();
  751. while(iterator.MoreElements())
  752. {
  753. iterator.RemoveCurrent ( );
  754. }
  755. safe_delete(tcplistener);
  756. }
  757. void LWorldList::Add(LWorld* worldserver) {
  758. LWorld* worldExist = FindByID(worldserver->GetID ( ) );
  759. if( worldExist )
  760. {
  761. worldExist->Kick();
  762. MWorldMap.writelock();
  763. worldmap.erase(worldExist->GetID());
  764. MWorldMap.releasewritelock();
  765. safe_delete(worldExist);
  766. }
  767. MWorldMap.writelock();
  768. worldmap[worldserver->GetID()] = worldserver;
  769. MWorldMap.releasewritelock();
  770. database.ResetWorldServerStatsConnectedTime(worldserver);
  771. database.UpdateWorldIPAddress(worldserver->GetID(), worldserver->GetIP());
  772. }
  773. void LWorldList::AddInitiateWorld ( LWorld* world )
  774. {
  775. list.Insert ( world );
  776. }
  777. void LWorldList::KickGhostIP(int32 ip, LWorld* NotMe, int16 iClientPort) {
  778. if (ip == 0)
  779. return;
  780. map<int32,LWorld*>::iterator map_list;
  781. MWorldMap.readlock();
  782. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  783. LWorld* world = map_list->second;
  784. if (!world->IsKicked() && world->GetIP() == ip && world != NotMe) {
  785. if ((iClientPort == 0 && world->GetType() == World) || (iClientPort != 0 && world->GetClientPort() == iClientPort)) {
  786. struct in_addr in;
  787. in.s_addr = world->GetIP();
  788. // cout << "Removing GhostIP LWorld(" << world->GetID() << ") from ip: " << inet_ntoa(in) << " port: " << (int16)(world->GetPort());
  789. if (!world->Connected())
  790. {
  791. // cout << " (it wasnt connected)";
  792. // cout << endl;
  793. if (NotMe) {
  794. in.s_addr = NotMe->GetIP();
  795. cout << "NotMe(" << NotMe->GetID() << ") = " << inet_ntoa(in) << ":" << NotMe->GetPort() << " (" << NotMe->GetClientPort() << ")" << endl;
  796. }
  797. world->Kick("Ghost IP kick");
  798. }
  799. }
  800. }
  801. }
  802. MWorldMap.releasereadlock();
  803. }
  804. void LWorldList::KickGhost(ConType in_type, int32 in_accountid, LWorld* ButNotMe) {
  805. map<int32,LWorld*>::iterator map_list;
  806. MWorldMap.readlock();
  807. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  808. LWorld* world = map_list->second;
  809. if (!world->IsKicked() && world->GetType() == in_type && world != ButNotMe && (in_accountid == 0 || world->GetAccountID() == in_accountid)) {
  810. if (world->GetIP() != 0) {
  811. //struct in_addr in;
  812. //in.s_addr = world->GetIP();
  813. // cout << "Removing GhostAcc LWorld(" << world->GetID() << ") from ip: " << inet_ntoa(in) << " port: " << (int16)(world->GetPort()) << endl;
  814. }
  815. if (world->GetType() == Login && world->IsOutgoingUplink()) {
  816. world->Kick("Ghost Acc Kick", false);
  817. // cout << "softkick" << endl;
  818. }
  819. else
  820. world->Kick("Ghost Acc Kick");
  821. }
  822. }
  823. MWorldMap.releasereadlock();
  824. }
  825. void LWorldList::UpdateWorldStats(){
  826. map<int32,LWorld*>::iterator map_list;
  827. MWorldMap.readlock();
  828. for(map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  829. LWorld* world = map_list->second;
  830. if(world && world->GetAccountID() > 0)
  831. database.UpdateWorldServerStats(world, world->GetStatus());
  832. }
  833. MWorldMap.releasereadlock();
  834. }
  835. void LWorldList::Process() {
  836. TCPConnection* newtcp = 0;
  837. LWorld* newworld = 0;
  838. LinkedListIterator<LWorld*> iterator(list);
  839. iterator.Reset();
  840. while(iterator.MoreElements())
  841. {
  842. if(iterator.GetData( )->GetID ( ) > 0 )
  843. {
  844. LWorld* world = iterator.GetData ( );
  845. iterator.RemoveCurrent ( false );
  846. Add( world );
  847. }
  848. else
  849. {
  850. if(! iterator.GetData ( )->Process ( ) )
  851. iterator.RemoveCurrent ( );
  852. else
  853. iterator.Advance();
  854. }
  855. }
  856. while ((newtcp = tcplistener->NewQueuePop())) {
  857. newworld = new LWorld(newtcp);
  858. newworld->SetID(0);
  859. AddInitiateWorld(newworld);
  860. struct in_addr in;
  861. in.s_addr = newtcp->GetrIP();
  862. LogWrite(LOGIN__INFO, 0, "Login", "New Server connection: %s port %i", inet_ntoa(in), ntohs(newtcp->GetrPort()));
  863. net.numservers++;
  864. net.UpdateWindowTitle();
  865. world_list.UpdateWorldList();
  866. }
  867. map<int32,LWorld*>::iterator map_list;
  868. for( map_list = worldmap.begin(); map_list != worldmap.end(); ) {
  869. LWorld* world = map_list->second;
  870. int32 account_id = world->GetAccountID();
  871. if (world->IsKicked() && !world->IsNeverKick()) {
  872. map_list++;
  873. worldmap.erase ( account_id );
  874. net.numservers--;
  875. net.UpdateWindowTitle();
  876. safe_delete ( world );
  877. continue;
  878. }
  879. else if (!world->Process()) {
  880. //struct in_addr in;
  881. //in.s_addr = world->GetIP();
  882. if (world->GetAccountID() == 0 || !(world->ShowDown()) || world->GetType() == Chat) {
  883. map_list++;
  884. worldmap.erase ( account_id );
  885. net.numservers--;
  886. net.UpdateWindowTitle();
  887. if(account_id > 0){
  888. LWorld* world2 = FindByID(account_id);
  889. if(world2)
  890. world2->ShowDownActive(true);
  891. }
  892. SendWorldChanged(account_id, true);
  893. safe_delete ( world );
  894. continue;
  895. }
  896. else {
  897. world->ChangeToPlaceholder();
  898. }
  899. }
  900. map_list++;
  901. }
  902. }
  903. // Sends packet to all World and Chat servers, local and remote (but not to remote login server's ::Process())
  904. void LWorldList::SendPacket(ServerPacket* pack, LWorld* butnotme) {
  905. map<int32,LWorld*>::iterator map_list;
  906. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  907. LWorld* world = map_list->second;
  908. if (world != butnotme) {
  909. if (world->GetType() == Login) {
  910. ServerPacket* outpack = new ServerPacket(ServerOP_EncapPacket, sizeof(ServerEncapPacket_Struct) + pack->size);
  911. ServerEncapPacket_Struct* seps = (ServerEncapPacket_Struct*) outpack->pBuffer;
  912. seps->ToID = 0xFFFFFFFF;
  913. seps->opcode = pack->opcode;
  914. seps->size = pack->size;
  915. memcpy(seps->data, pack->pBuffer, pack->size);
  916. world->SendPacket(outpack);
  917. delete outpack;
  918. }
  919. else if (world->GetRemoteID() == 0) {
  920. world->SendPacket(pack);
  921. }
  922. }
  923. }
  924. }
  925. // Sends a packet to every local TCP Connection, all types
  926. void LWorldList::SendPacketLocal(ServerPacket* pack, LWorld* butnotme) {
  927. map<int32,LWorld*>::iterator map_list;
  928. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  929. LWorld* world = map_list->second;
  930. if (world != butnotme && world->GetRemoteID() == 0) {
  931. world->SendPacket(pack);
  932. }
  933. }
  934. }
  935. // Sends the packet to all login servers
  936. void LWorldList::SendPacketLogin(ServerPacket* pack, LWorld* butnotme) {
  937. map<int32,LWorld*>::iterator map_list;
  938. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  939. LWorld* world = map_list->second;
  940. if (world != butnotme && world->GetType() == Login) {
  941. world->SendPacket(pack);
  942. }
  943. }
  944. }
  945. void LWorldList::UpdateWorldList(LWorld* to) {
  946. map<int32,LWorld*>::iterator map_list;
  947. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  948. LWorld* world = map_list->second;
  949. if (net.GetLoginMode() != Mesh || world->GetRemoteID() == 0)
  950. world->UpdateWorldList(to);
  951. }
  952. }
  953. LWorld* LWorldList::FindByID(int32 LWorldID) {
  954. if(worldmap.count(LWorldID) > 0)
  955. return worldmap[LWorldID];
  956. return 0;
  957. }
  958. LWorld* LWorldList::FindByIP(int32 ip) {
  959. map<int32,LWorld*>::iterator map_list;
  960. LWorld* world = 0;
  961. LWorld* ret = 0;
  962. MWorldMap.readlock();
  963. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  964. world = map_list->second;
  965. if (world->GetIP() == ip){
  966. ret = world;
  967. break;
  968. }
  969. }
  970. MWorldMap.releasereadlock();
  971. return ret;
  972. }
  973. LWorld* LWorldList::FindByAddress(char* address) {
  974. map<int32,LWorld*>::iterator map_list;
  975. LWorld* world = 0;
  976. LWorld* ret = 0;
  977. MWorldMap.readlock();
  978. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  979. world = map_list->second;
  980. if (strcasecmp(world->GetAddress(), address) == 0){
  981. ret = world;
  982. break;
  983. }
  984. }
  985. MWorldMap.releasereadlock();
  986. return ret;
  987. }
  988. LWorld* LWorldList::FindByLink(TCPConnection* in_link, int32 in_id) {
  989. if (in_link == 0)
  990. return 0;
  991. LWorld* world = 0;
  992. LWorld* ret = 0;
  993. map<int32,LWorld*>::iterator map_list;
  994. MWorldMap.readlock();
  995. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  996. world = map_list->second;
  997. if (world->GetLink() == in_link && world->GetRemoteID() == in_id){
  998. ret = world;
  999. break;
  1000. }
  1001. }
  1002. MWorldMap.releasereadlock();
  1003. return ret;
  1004. }
  1005. LWorld* LWorldList::FindByAccount(int32 in_accountid, ConType in_type) {
  1006. if (in_accountid == 0)
  1007. return 0;
  1008. LWorld* world = 0;
  1009. LWorld* ret = 0;
  1010. map<int32,LWorld*>::iterator map_list;
  1011. MWorldMap.readlock();
  1012. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1013. world = map_list->second;
  1014. if (world->GetAccountID() == in_accountid && world->GetType() == in_type){
  1015. ret = world;
  1016. break;
  1017. }
  1018. }
  1019. MWorldMap.releasereadlock();
  1020. return ret;
  1021. }
  1022. int8 LWorld::GetWorldStatus(){
  1023. if(IsDevelServer() && IsLocked() == false)
  1024. return 1;
  1025. else if(IsInit && IsLocked() == false)
  1026. return 0;
  1027. else
  1028. return 2;
  1029. }
  1030. void LWorld::SendDeleteCharacter ( int32 char_id , int32 account_id )
  1031. {
  1032. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  1033. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  1034. cdu->account_id = account_id;
  1035. cdu->char_id = char_id;
  1036. cdu->update_field = DELETE_UPDATE_FLAG;
  1037. cdu->update_data = 1;
  1038. SendPacket(outpack);
  1039. }
  1040. vector<PacketStruct*>* LWorldList::GetServerListUpdate(int16 version){
  1041. vector<PacketStruct*>* ret = new vector<PacketStruct*>;
  1042. map<int32,LWorld*>::iterator map_list;
  1043. PacketStruct* packet = 0;
  1044. MWorldMap.readlock();
  1045. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1046. LWorld* world = map_list->second;
  1047. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1048. packet = configReader.getStruct("LS_WorldUpdate", version);
  1049. if(packet){
  1050. packet->setDataByName("server_id", world->GetID());
  1051. packet->setDataByName("up", 1);
  1052. if(world->IsLocked())
  1053. packet->setDataByName("locked", 1);
  1054. ret->push_back(packet);
  1055. }
  1056. }
  1057. }
  1058. MWorldMap.releasereadlock();
  1059. return ret;
  1060. }
  1061. EQ2Packet* LWorldList::MakeServerListPacket(int8 lsadmin, int16 version) {
  1062. // if the latest world list has already been loaded, just return the string
  1063. MWorldMap.readlock();
  1064. if (!UpdateServerList && ServerListData.count(version))
  1065. {
  1066. MWorldMap.releasereadlock();
  1067. return ServerListData[version];
  1068. }
  1069. //LWorld* world = 0;
  1070. int32 ServerNum = 0;
  1071. /* while(iterator.MoreElements()){
  1072. world = iterator.GetData();
  1073. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World)
  1074. ServerNum++;
  1075. iterator.Advance();
  1076. }
  1077. ServerNum+=3;
  1078. */
  1079. uint32 tmpCount = 0;
  1080. map<int32, LWorld*>::iterator map_list;
  1081. for (map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1082. LWorld* world = map_list->second;
  1083. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1084. tmpCount++;
  1085. }
  1086. }
  1087. PacketStruct* packet = configReader.getStruct("LS_WorldList", version);
  1088. packet->setArrayLengthByName("num_worlds", tmpCount);
  1089. string world_data;
  1090. for (map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1091. LWorld* world = map_list->second;
  1092. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1093. ServerNum++;
  1094. packet->setArrayDataByName("id", world->GetID(), ServerNum - 1);
  1095. if (version <= 283) {
  1096. packet->setArrayDataByName("name", world->GetName(), ServerNum - 1);
  1097. if (!world->ShowDown())
  1098. packet->setArrayDataByName("online", 1, ServerNum - 1);
  1099. if (world->IsLocked())
  1100. packet->setArrayDataByName("locked", 1, ServerNum - 1);
  1101. packet->setArrayDataByName("unknown2", 1, ServerNum - 1);
  1102. packet->setArrayDataByName("unknown3", 1, ServerNum - 1);
  1103. packet->setArrayDataByName("load", world->GetWorldStatus(), ServerNum - 1);
  1104. }
  1105. else
  1106. {
  1107. if (version < 1212)
  1108. packet->setArrayDataByName("allowed_races", 0xFFFFFFFF, ServerNum - 1);
  1109. else if (version < 60006)
  1110. packet->setArrayDataByName("allowed_races", 0x000FFFFF, ServerNum - 1); // + Freeblood
  1111. else
  1112. packet->setArrayDataByName("allowed_races", 0x001FFFFF, ServerNum - 1); // + Aerakyn
  1113. packet->setArrayDataByName("number_online_flag", 1, ServerNum - 1);
  1114. packet->setArrayDataByName("num_players", world->GetPlayerNum(), ServerNum - 1);
  1115. packet->setArrayDataByName("name", world->GetName(), ServerNum - 1);
  1116. packet->setArrayDataByName("name2", world->GetName(), ServerNum - 1);
  1117. packet->setArrayDataByName("feature_set", 0, ServerNum - 1);
  1118. packet->setArrayDataByName("load", world->GetWorldStatus(), ServerNum - 1);
  1119. if (world->IsLocked())
  1120. packet->setArrayDataByName("locked", 1, ServerNum - 1);
  1121. if (world->ShowDown())
  1122. packet->setArrayDataByName("tag", 0, ServerNum - 1);
  1123. else
  1124. packet->setArrayDataByName("tag", 1, ServerNum - 1);
  1125. if (version < 1212)
  1126. packet->setArrayDataByName("unknown", ServerNum, ServerNum - 1);
  1127. }
  1128. }
  1129. }
  1130. EQ2Packet* pack = packet->serialize();
  1131. #ifdef DEBUG
  1132. //Only dump these for people trying to debug this...
  1133. printf("WorldList:\n");
  1134. DumpPacket(pack->pBuffer, pack->size);
  1135. #endif
  1136. if (ServerListData.count(version))
  1137. {
  1138. map<int32, EQ2Packet*>::iterator it = ServerListData.find(version);
  1139. EQ2Packet* tmpPack = ServerListData[version];
  1140. safe_delete(tmpPack);
  1141. ServerListData.erase(it);
  1142. }
  1143. ServerListData.insert(make_pair(version, pack));
  1144. MWorldMap.releasereadlock();
  1145. SetUpdateServerList(false);
  1146. return ServerListData[version];
  1147. }
  1148. void LWorldList::SendWorldStatus(LWorld* chat, char* adminname) {
  1149. struct in_addr in;
  1150. int32 count = 0;
  1151. map<int32,LWorld*>::iterator map_list;
  1152. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1153. LWorld* world = map_list->second;
  1154. if (world->GetIP() != 0 && world->GetType() == World) {
  1155. chat->Message(adminname, "Name: %s", world->GetName());
  1156. in.s_addr = world->GetIP();
  1157. if (world->GetAccountID() != 0) {
  1158. chat->Message(adminname, " Account: %s", world->GetAccount());
  1159. }
  1160. chat->Message(adminname, " Number of Zones: %i", world->GetZoneNum());
  1161. chat->Message(adminname, " Number of Players: %i", world->GetPlayerNum());
  1162. chat->Message(adminname, " IP: %s", inet_ntoa(in));
  1163. if (!world->IsAddressIP()) {
  1164. chat->Message(adminname, " Address: %s", world->GetAddress());
  1165. }
  1166. count++;
  1167. }
  1168. }
  1169. chat->Message(adminname, "%i worlds listed.", count);
  1170. }
  1171. void LWorldList::RemoveByLink(TCPConnection* in_link, int32 in_id, LWorld* ButNotMe) {
  1172. if (in_link == 0)
  1173. return;
  1174. map<int32,LWorld*>::iterator map_list;
  1175. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1176. LWorld* world = map_list->second;
  1177. if (world != ButNotMe && world->GetLink() == in_link && (in_id == 0 || world->GetRemoteID() == in_id)) {
  1178. // world->Link = 0;
  1179. map_list++;
  1180. worldmap.erase ( world->GetID ( ) );
  1181. safe_delete ( world );
  1182. continue;
  1183. }
  1184. }
  1185. }
  1186. void LWorldList::RemoveByID(int32 in_id) {
  1187. if (in_id == 0)
  1188. return;
  1189. LWorld* existWorld = FindByID(in_id);
  1190. if ( existWorld != NULL )
  1191. {
  1192. MWorldMap.writelock();
  1193. worldmap.erase ( in_id );
  1194. MWorldMap.releasewritelock();
  1195. safe_delete ( existWorld );
  1196. }
  1197. }
  1198. bool LWorldList::Init() {
  1199. database.ResetWorldStats ( );
  1200. if (!tcplistener->IsOpen()) {
  1201. return tcplistener->Open(net.GetPort());
  1202. }
  1203. return false;
  1204. }
  1205. void LWorldList::InitWorlds(){
  1206. vector<LWorld*> server_list;
  1207. database.GetServerAccounts(&server_list);
  1208. vector<LWorld*>::iterator iter;
  1209. int i = 0;
  1210. for(iter = server_list.begin(); iter != server_list.end(); iter++, i++){
  1211. LWorld* world = FindByID(server_list[i]->GetAccountID());
  1212. if(!world){
  1213. server_list[i]->ShowDown(true);
  1214. server_list[i]->ShowDownActive(true);
  1215. server_list[i]->SetID ( server_list[i]->GetAccountID ( ) );
  1216. Add ( server_list[i] );
  1217. }
  1218. }
  1219. }
  1220. void LWorldList::ListWorldsToConsole() {
  1221. struct in_addr in;
  1222. cout << "World List:" << endl;
  1223. cout << "============================" << endl;
  1224. map<int32,LWorld*>::iterator map_list;
  1225. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1226. LWorld* world = map_list->second;
  1227. in.s_addr = world->GetIP();
  1228. if (world->GetType() == World) {
  1229. if (world->GetRemoteID() == 0)
  1230. cout << "ID: " << world->GetID() << ", Name: " << world->GetName() << ", Local, IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", Status: " << world->GetStatus() << ", AccID: " << world->GetAccountID() << endl;
  1231. else
  1232. cout << "ID: " << world->GetID() << ", Name: " << world->GetName() << ", RemoteID: " << world->GetRemoteID() << ", LinkWorldID: " << world->GetLinkWorldID() << ", IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", Status: " << world->GetStatus() << ", AccID: " << world->GetAccountID() << endl;
  1233. }
  1234. else if (world->GetType() == Chat) {
  1235. cout << "ID: " << world->GetID() << ", Chat Server, IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1236. }
  1237. else if (world->GetType() == Login) {
  1238. if (world->IsOutgoingUplink()) {
  1239. if (world->Connected())
  1240. cout << "ID: " << world->GetID() << ", Login Server (out), IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1241. else
  1242. cout << "ID: " << world->GetID() << ", Login Server (nc), IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1243. }
  1244. else
  1245. cout << "ID: " << world->GetID() << ", Login Server (in), IP: " << inet_ntoa(in) << ":" << world->GetPort() << " (" << world->GetClientPort() << "), AccID: " << world->GetAccountID() << endl;
  1246. }
  1247. else {
  1248. cout << "ID: " << world->GetID() << ", Unknown Type, Name: " << world->GetName() << ", IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1249. }
  1250. }
  1251. cout << "============================" << endl;
  1252. }
  1253. void LWorldList::AddServerZoneUpdates(LWorld* world, map<int32, LoginZoneUpdate> updates){
  1254. int32 server_id = world->GetID();
  1255. map<int32, LoginZoneUpdate>::iterator itr;
  1256. for(itr = updates.begin(); itr != updates.end(); itr++){
  1257. if(zone_updates_already_used.size() >= 1500 || zone_updates_already_used[server_id].count(itr->first) > 0){
  1258. world->Kick("Hacking attempt.");
  1259. return;
  1260. }
  1261. zone_updates_already_used[server_id][itr->first] = true;
  1262. }
  1263. server_zone_updates.Put(server_id, updates);
  1264. }
  1265. //devn00b temp
  1266. void LWorldList::AddServerEquipmentUpdates(LWorld* world, map<int32, LoginEquipmentUpdate> updates){
  1267. int32 server_id = world->GetID();
  1268. map<int32, LoginEquipmentUpdate>::iterator itr;
  1269. for(itr = updates.begin(); itr != updates.end(); itr++){
  1270. LogWrite(MISC__TODO, 1, "TODO", "JA: Until we learn what this does, can't risk worlds being kicked performing login appearance updates...\n%s, func: %s, line: %i", __FILE__, __FUNCTION__, __LINE__);
  1271. /*if(equip_updates_already_used.size() >= 1500 || equip_updates_already_used[server_id].count(itr->first) > 0)
  1272. {
  1273. LogWrite(LOGIN__ERROR, 0, "Login", "World ID '%i': Hacking attempt. (function: %s, line: %i", world->GetAccountID(), __FUNCTION__, __LINE__);
  1274. world->Kick("Hacking attempt.");
  1275. return;
  1276. }*/
  1277. equip_updates_already_used[server_id][itr->first] = true;
  1278. }
  1279. server_equip_updates.Put(server_id, updates);
  1280. }
  1281. void LWorldList::RequestServerUpdates(LWorld* world){
  1282. if(world){
  1283. ServerPacket* pack = new ServerPacket(ServerOP_ZoneUpdates, sizeof(ZoneUpdateRequest_Struct));
  1284. ZoneUpdateRequest_Struct* request = (ZoneUpdateRequest_Struct*)pack->pBuffer;
  1285. request->max_per_batch = MAX_UPDATE_COUNT;
  1286. world->SendPacket(pack);
  1287. delete pack;
  1288. zone_update_timeouts.Put(world->GetID(), Timer::GetCurrentTime2() + 30000);
  1289. }
  1290. }
  1291. void LWorldList::ProcessServerUpdates(){
  1292. MutexMap<int32, map<int32, LoginZoneUpdate> >::iterator itr = server_zone_updates.begin();
  1293. while(itr.Next()){
  1294. if(itr->second.size() > 0){
  1295. database.SetServerZoneDescriptions(itr->first, itr->second);
  1296. if(itr->second.size() == MAX_UPDATE_COUNT)
  1297. awaiting_zone_update.Put(itr->first, Timer::GetCurrentTime2() + 10000); //only process 20 updates in a 10 second period to avoid network problems
  1298. server_zone_updates.erase(itr->first);
  1299. }
  1300. if(zone_update_timeouts.count(itr->first) == 0 || zone_update_timeouts.Get(itr->first) <= Timer::GetCurrentTime2()){
  1301. zone_update_timeouts.erase(itr->first);
  1302. server_zone_updates.erase(itr->first);
  1303. }
  1304. }
  1305. LWorld* world = 0;
  1306. MWorldMap.readlock();
  1307. map<int32, LWorld*>::iterator map_itr;
  1308. for(map_itr = worldmap.begin(); map_itr != worldmap.end(); map_itr++){
  1309. world = map_itr->second;
  1310. if(world && world->GetID()){
  1311. if(last_updated.count(world) == 0 || last_updated.Get(world) <= Timer::GetCurrentTime2()){
  1312. zone_updates_already_used[world->GetID()].clear();
  1313. RequestServerUpdates(world);
  1314. last_updated.Put(world, Timer::GetCurrentTime2() + 21600000);
  1315. }
  1316. if(awaiting_zone_update.count(world->GetID()) > 0 && awaiting_zone_update.Get(world->GetID()) <= Timer::GetCurrentTime2()){
  1317. awaiting_zone_update.erase(world->GetID());
  1318. RequestServerUpdates(world);
  1319. }
  1320. }
  1321. }
  1322. ProcessLSEquipUpdates();
  1323. MWorldMap.releasereadlock();
  1324. }
  1325. void LWorldList::RequestServerEquipUpdates(LWorld* world)
  1326. {
  1327. if(world)
  1328. {
  1329. ServerPacket *pack_equip = new ServerPacket(ServerOP_LoginEquipment, sizeof(EquipmentUpdateRequest_Struct));
  1330. EquipmentUpdateRequest_Struct *request_equip = (EquipmentUpdateRequest_Struct *)pack_equip->pBuffer;
  1331. request_equip->max_per_batch = MAX_LOGIN_APPEARANCE_COUNT; // item appearance data smaller, request more at a time?
  1332. LogWrite(LOGIN__DEBUG, 1, "Login", "Sending equipment update requests to world: (%s)... (Batch Size: %i)", world->GetName(), request_equip->max_per_batch);
  1333. world->SendPacket(pack_equip);
  1334. delete pack_equip;
  1335. equip_update_timeouts.Put(world->GetID(), Timer::GetCurrentTime2() + 30000);
  1336. }
  1337. }
  1338. void LWorldList::ProcessLSEquipUpdates()
  1339. {
  1340. // process login_equipment updates
  1341. MutexMap<int32, map<int32, LoginEquipmentUpdate> >::iterator itr_equip = server_equip_updates.begin();
  1342. while(itr_equip.Next())
  1343. {
  1344. if(itr_equip->second.size() > 0)
  1345. {
  1346. LogWrite(LOGIN__DEBUG, 1, "Login", "Setting Login Appearances...");
  1347. database.SetServerEquipmentAppearances(itr_equip->first, itr_equip->second);
  1348. if(itr_equip->second.size() == MAX_LOGIN_APPEARANCE_COUNT)
  1349. awaiting_equip_update.Put(itr_equip->first, Timer::GetCurrentTime2() + 10000); //only process 100 updates in a 10 second period to avoid network problems
  1350. server_equip_updates.erase(itr_equip->first);
  1351. }
  1352. if(equip_update_timeouts.count(itr_equip->first) == 0 || equip_update_timeouts.Get(itr_equip->first) <= Timer::GetCurrentTime2())
  1353. {
  1354. LogWrite(LOGIN__DEBUG, 1, "Login", "Clearing Login Appearances Update Timers...");
  1355. equip_update_timeouts.erase(itr_equip->first);
  1356. server_equip_updates.erase(itr_equip->first);
  1357. }
  1358. }
  1359. LWorld* world = 0;
  1360. MWorldMap.readlock();
  1361. map<int32, LWorld*>::iterator map_itr;
  1362. for(map_itr = worldmap.begin(); map_itr != worldmap.end(); map_itr++)
  1363. {
  1364. world = map_itr->second;
  1365. if(world && world->GetID())
  1366. {
  1367. if(last_equip_updated.count(world) == 0 || last_equip_updated.Get(world) <= Timer::GetCurrentTime2())
  1368. {
  1369. LogWrite(LOGIN__DEBUG, 1, "Login", "Clearing Login Appearances Update Counters...");
  1370. equip_updates_already_used[world->GetID()].clear();
  1371. RequestServerEquipUpdates(world);
  1372. last_equip_updated.Put(world, Timer::GetCurrentTime2() + 900000); // every 15 mins
  1373. }
  1374. if( awaiting_equip_update.count(world->GetID()) > 0 && awaiting_equip_update.Get(world->GetID()) <= Timer::GetCurrentTime2())
  1375. {
  1376. LogWrite(LOGIN__DEBUG, 1, "Login", "Erase awaiting equip updates...");
  1377. awaiting_equip_update.erase(world->GetID());
  1378. RequestServerEquipUpdates(world);
  1379. }
  1380. }
  1381. }
  1382. MWorldMap.releasereadlock();
  1383. }
  1384. ThreadReturnType ServerUpdateLoop(void* tmp) {
  1385. #ifdef WIN32
  1386. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  1387. #endif
  1388. if (tmp == 0) {
  1389. ThrowError("ServerUpdateLoop(): tmp = 0!");
  1390. THREAD_RETURN(NULL);
  1391. }
  1392. LWorldList* worldList = (LWorldList*) tmp;
  1393. while (worldList->ContinueServerUpdates()) {
  1394. Sleep(1000);
  1395. worldList->ProcessServerUpdates();
  1396. }
  1397. worldList->ResetServerUpdates();
  1398. THREAD_RETURN(NULL);
  1399. }