LWorld.cpp 42 KB

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