LWorld.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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, lss->world_max_level);
  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. database.UpdateWorldVersion(GetAccountID(), in_version);
  683. pStatsTimer = new Timer ( 60000 );
  684. pStatsTimer->Start ( 60000 );
  685. return true;
  686. }
  687. void LWorldList::SendWorldChanged(int32 server_id, bool sendtoallclients, Client* sendto){
  688. EQ2Packet* outapp = new EQ2Packet(OP_WorldStatusChangeMsg, 0, sizeof(LS_WorldStatusChanged));
  689. LS_WorldStatusChanged* world_changed = (LS_WorldStatusChanged*)outapp->pBuffer;
  690. world_changed->server_id = server_id;
  691. LWorld* world = world_list.FindByID(server_id);
  692. if(!world || world->ShowDown())
  693. world_changed->up = 0;
  694. else
  695. world_changed->up = 1;
  696. if(sendtoallclients || sendto == 0)
  697. client_list.SendPacketToAllClients(outapp);
  698. else
  699. sendto->QueuePacket(outapp);
  700. world_list.SetUpdateServerList(true);
  701. }
  702. void LWorld::UpdateWorldList(LWorld* to) {
  703. world_list.SetUpdateServerList( true );
  704. }
  705. void LWorld::ChangeToPlaceholder() {
  706. ip = 0;
  707. status = -1;
  708. pPlaceholder = true;
  709. if (Link != 0 && RemoteID == 0) {
  710. Link->Disconnect();
  711. }
  712. UpdateWorldList();
  713. }
  714. 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) {
  715. ip = in_ip;
  716. accountid = in_accountid;
  717. // strcpy(account, in_account);
  718. strcpy(worldname, in_name);
  719. strcpy(address, in_address);
  720. status = in_status;
  721. admin_id = in_adminid;
  722. num_players = in_players;
  723. num_zones = in_zones;
  724. }
  725. LWorldList::LWorldList() {
  726. server_update_thread = true;
  727. NextID = 1;
  728. tcplistener = new TCPServer(net.GetPort(), true);
  729. if (net.GetLoginMode() == Slave)
  730. OutLink = new TCPConnection(true);
  731. else
  732. OutLink = 0;
  733. UpdateServerList = true;
  734. #ifdef WIN32
  735. _beginthread(ServerUpdateLoop, 0, this);
  736. #else
  737. pthread_t thread;
  738. pthread_create(&thread, NULL, &ServerUpdateLoop, this);
  739. #endif
  740. }
  741. LWorldList::~LWorldList() {
  742. server_update_thread = false;
  743. while(!server_update_thread){
  744. Sleep(100);
  745. }
  746. safe_delete(tcplistener);
  747. safe_delete(OutLink);
  748. }
  749. void LWorldList::Shutdown() {
  750. LinkedListIterator<LWorld*> iterator(list);
  751. iterator.Reset();
  752. while(iterator.MoreElements())
  753. {
  754. iterator.RemoveCurrent ( );
  755. }
  756. safe_delete(tcplistener);
  757. }
  758. void LWorldList::Add(LWorld* worldserver) {
  759. LWorld* worldExist = FindByID(worldserver->GetID ( ) );
  760. if( worldExist )
  761. {
  762. worldExist->Kick();
  763. MWorldMap.writelock();
  764. worldmap.erase(worldExist->GetID());
  765. MWorldMap.releasewritelock();
  766. safe_delete(worldExist);
  767. }
  768. MWorldMap.writelock();
  769. worldmap[worldserver->GetID()] = worldserver;
  770. MWorldMap.releasewritelock();
  771. database.ResetWorldServerStatsConnectedTime(worldserver);
  772. database.UpdateWorldIPAddress(worldserver->GetID(), worldserver->GetIP());
  773. }
  774. void LWorldList::AddInitiateWorld ( LWorld* world )
  775. {
  776. list.Insert ( world );
  777. }
  778. void LWorldList::KickGhostIP(int32 ip, LWorld* NotMe, int16 iClientPort) {
  779. if (ip == 0)
  780. return;
  781. map<int32,LWorld*>::iterator map_list;
  782. MWorldMap.readlock();
  783. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  784. LWorld* world = map_list->second;
  785. if (!world->IsKicked() && world->GetIP() == ip && world != NotMe) {
  786. if ((iClientPort == 0 && world->GetType() == World) || (iClientPort != 0 && world->GetClientPort() == iClientPort)) {
  787. struct in_addr in;
  788. in.s_addr = world->GetIP();
  789. // cout << "Removing GhostIP LWorld(" << world->GetID() << ") from ip: " << inet_ntoa(in) << " port: " << (int16)(world->GetPort());
  790. if (!world->Connected())
  791. {
  792. // cout << " (it wasnt connected)";
  793. // cout << endl;
  794. if (NotMe) {
  795. in.s_addr = NotMe->GetIP();
  796. cout << "NotMe(" << NotMe->GetID() << ") = " << inet_ntoa(in) << ":" << NotMe->GetPort() << " (" << NotMe->GetClientPort() << ")" << endl;
  797. }
  798. world->Kick("Ghost IP kick");
  799. }
  800. }
  801. }
  802. }
  803. MWorldMap.releasereadlock();
  804. }
  805. void LWorldList::KickGhost(ConType in_type, int32 in_accountid, LWorld* ButNotMe) {
  806. map<int32,LWorld*>::iterator map_list;
  807. MWorldMap.readlock();
  808. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  809. LWorld* world = map_list->second;
  810. if (!world->IsKicked() && world->GetType() == in_type && world != ButNotMe && (in_accountid == 0 || world->GetAccountID() == in_accountid)) {
  811. if (world->GetIP() != 0) {
  812. //struct in_addr in;
  813. //in.s_addr = world->GetIP();
  814. // cout << "Removing GhostAcc LWorld(" << world->GetID() << ") from ip: " << inet_ntoa(in) << " port: " << (int16)(world->GetPort()) << endl;
  815. }
  816. if (world->GetType() == Login && world->IsOutgoingUplink()) {
  817. world->Kick("Ghost Acc Kick", false);
  818. // cout << "softkick" << endl;
  819. }
  820. else
  821. world->Kick("Ghost Acc Kick");
  822. }
  823. }
  824. MWorldMap.releasereadlock();
  825. }
  826. void LWorldList::UpdateWorldStats(){
  827. map<int32,LWorld*>::iterator map_list;
  828. MWorldMap.readlock();
  829. for(map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  830. LWorld* world = map_list->second;
  831. if(world && world->GetAccountID() > 0)
  832. database.UpdateWorldServerStats(world, world->GetStatus());
  833. }
  834. MWorldMap.releasereadlock();
  835. }
  836. void LWorldList::Process() {
  837. TCPConnection* newtcp = 0;
  838. LWorld* newworld = 0;
  839. LinkedListIterator<LWorld*> iterator(list);
  840. iterator.Reset();
  841. while(iterator.MoreElements())
  842. {
  843. if(iterator.GetData( )->GetID ( ) > 0 )
  844. {
  845. LWorld* world = iterator.GetData ( );
  846. iterator.RemoveCurrent ( false );
  847. Add( world );
  848. }
  849. else
  850. {
  851. if(! iterator.GetData ( )->Process ( ) )
  852. iterator.RemoveCurrent ( );
  853. else
  854. iterator.Advance();
  855. }
  856. }
  857. while ((newtcp = tcplistener->NewQueuePop())) {
  858. newworld = new LWorld(newtcp);
  859. newworld->SetID(0);
  860. AddInitiateWorld(newworld);
  861. struct in_addr in;
  862. in.s_addr = newtcp->GetrIP();
  863. LogWrite(LOGIN__INFO, 0, "Login", "New Server connection: %s port %i", inet_ntoa(in), ntohs(newtcp->GetrPort()));
  864. net.numservers++;
  865. net.UpdateWindowTitle();
  866. world_list.UpdateWorldList();
  867. }
  868. map<int32,LWorld*>::iterator map_list;
  869. for( map_list = worldmap.begin(); map_list != worldmap.end(); ) {
  870. LWorld* world = map_list->second;
  871. int32 account_id = world->GetAccountID();
  872. if (world->IsKicked() && !world->IsNeverKick()) {
  873. map_list++;
  874. worldmap.erase ( account_id );
  875. net.numservers--;
  876. net.UpdateWindowTitle();
  877. safe_delete ( world );
  878. continue;
  879. }
  880. else if (!world->Process()) {
  881. //struct in_addr in;
  882. //in.s_addr = world->GetIP();
  883. if (world->GetAccountID() == 0 || !(world->ShowDown()) || world->GetType() == Chat) {
  884. map_list++;
  885. worldmap.erase ( account_id );
  886. net.numservers--;
  887. net.UpdateWindowTitle();
  888. if(account_id > 0){
  889. LWorld* world2 = FindByID(account_id);
  890. if(world2)
  891. world2->ShowDownActive(true);
  892. }
  893. SendWorldChanged(account_id, true);
  894. safe_delete ( world );
  895. continue;
  896. }
  897. else {
  898. world->ChangeToPlaceholder();
  899. }
  900. }
  901. map_list++;
  902. }
  903. }
  904. // Sends packet to all World and Chat servers, local and remote (but not to remote login server's ::Process())
  905. void LWorldList::SendPacket(ServerPacket* pack, LWorld* butnotme) {
  906. map<int32,LWorld*>::iterator map_list;
  907. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  908. LWorld* world = map_list->second;
  909. if (world != butnotme) {
  910. if (world->GetType() == Login) {
  911. ServerPacket* outpack = new ServerPacket(ServerOP_EncapPacket, sizeof(ServerEncapPacket_Struct) + pack->size);
  912. ServerEncapPacket_Struct* seps = (ServerEncapPacket_Struct*) outpack->pBuffer;
  913. seps->ToID = 0xFFFFFFFF;
  914. seps->opcode = pack->opcode;
  915. seps->size = pack->size;
  916. memcpy(seps->data, pack->pBuffer, pack->size);
  917. world->SendPacket(outpack);
  918. delete outpack;
  919. }
  920. else if (world->GetRemoteID() == 0) {
  921. world->SendPacket(pack);
  922. }
  923. }
  924. }
  925. }
  926. // Sends a packet to every local TCP Connection, all types
  927. void LWorldList::SendPacketLocal(ServerPacket* pack, LWorld* butnotme) {
  928. map<int32,LWorld*>::iterator map_list;
  929. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  930. LWorld* world = map_list->second;
  931. if (world != butnotme && world->GetRemoteID() == 0) {
  932. world->SendPacket(pack);
  933. }
  934. }
  935. }
  936. // Sends the packet to all login servers
  937. void LWorldList::SendPacketLogin(ServerPacket* pack, LWorld* butnotme) {
  938. map<int32,LWorld*>::iterator map_list;
  939. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++ ) {
  940. LWorld* world = map_list->second;
  941. if (world != butnotme && world->GetType() == Login) {
  942. world->SendPacket(pack);
  943. }
  944. }
  945. }
  946. void LWorldList::UpdateWorldList(LWorld* to) {
  947. map<int32,LWorld*>::iterator map_list;
  948. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  949. LWorld* world = map_list->second;
  950. if (net.GetLoginMode() != Mesh || world->GetRemoteID() == 0)
  951. world->UpdateWorldList(to);
  952. }
  953. }
  954. LWorld* LWorldList::FindByID(int32 LWorldID) {
  955. if(worldmap.count(LWorldID) > 0)
  956. return worldmap[LWorldID];
  957. return 0;
  958. }
  959. LWorld* LWorldList::FindByIP(int32 ip) {
  960. map<int32,LWorld*>::iterator map_list;
  961. LWorld* world = 0;
  962. LWorld* ret = 0;
  963. MWorldMap.readlock();
  964. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  965. world = map_list->second;
  966. if (world->GetIP() == ip){
  967. ret = world;
  968. break;
  969. }
  970. }
  971. MWorldMap.releasereadlock();
  972. return ret;
  973. }
  974. LWorld* LWorldList::FindByAddress(char* address) {
  975. map<int32,LWorld*>::iterator map_list;
  976. LWorld* world = 0;
  977. LWorld* ret = 0;
  978. MWorldMap.readlock();
  979. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  980. world = map_list->second;
  981. if (strcasecmp(world->GetAddress(), address) == 0){
  982. ret = world;
  983. break;
  984. }
  985. }
  986. MWorldMap.releasereadlock();
  987. return ret;
  988. }
  989. LWorld* LWorldList::FindByLink(TCPConnection* in_link, int32 in_id) {
  990. if (in_link == 0)
  991. return 0;
  992. LWorld* world = 0;
  993. LWorld* ret = 0;
  994. map<int32,LWorld*>::iterator map_list;
  995. MWorldMap.readlock();
  996. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  997. world = map_list->second;
  998. if (world->GetLink() == in_link && world->GetRemoteID() == in_id){
  999. ret = world;
  1000. break;
  1001. }
  1002. }
  1003. MWorldMap.releasereadlock();
  1004. return ret;
  1005. }
  1006. LWorld* LWorldList::FindByAccount(int32 in_accountid, ConType in_type) {
  1007. if (in_accountid == 0)
  1008. return 0;
  1009. LWorld* world = 0;
  1010. LWorld* ret = 0;
  1011. map<int32,LWorld*>::iterator map_list;
  1012. MWorldMap.readlock();
  1013. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1014. world = map_list->second;
  1015. if (world->GetAccountID() == in_accountid && world->GetType() == in_type){
  1016. ret = world;
  1017. break;
  1018. }
  1019. }
  1020. MWorldMap.releasereadlock();
  1021. return ret;
  1022. }
  1023. int8 LWorld::GetWorldStatus(){
  1024. if(IsDevelServer() && IsLocked() == false)
  1025. return 1;
  1026. else if(IsInit && IsLocked() == false)
  1027. return 0;
  1028. else
  1029. return 2;
  1030. }
  1031. void LWorld::SendDeleteCharacter ( int32 char_id , int32 account_id )
  1032. {
  1033. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  1034. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  1035. cdu->account_id = account_id;
  1036. cdu->char_id = char_id;
  1037. cdu->update_field = DELETE_UPDATE_FLAG;
  1038. cdu->update_data = 1;
  1039. SendPacket(outpack);
  1040. }
  1041. vector<PacketStruct*>* LWorldList::GetServerListUpdate(int16 version){
  1042. vector<PacketStruct*>* ret = new vector<PacketStruct*>;
  1043. map<int32,LWorld*>::iterator map_list;
  1044. PacketStruct* packet = 0;
  1045. MWorldMap.readlock();
  1046. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1047. LWorld* world = map_list->second;
  1048. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1049. packet = configReader.getStruct("LS_WorldUpdate", version);
  1050. if(packet){
  1051. packet->setDataByName("server_id", world->GetID());
  1052. packet->setDataByName("up", 1);
  1053. if(world->IsLocked())
  1054. packet->setDataByName("locked", 1);
  1055. ret->push_back(packet);
  1056. }
  1057. }
  1058. }
  1059. MWorldMap.releasereadlock();
  1060. return ret;
  1061. }
  1062. EQ2Packet* LWorldList::MakeServerListPacket(int8 lsadmin, int16 version) {
  1063. // if the latest world list has already been loaded, just return the string
  1064. MWorldMap.readlock();
  1065. if (!UpdateServerList && ServerListData.count(version))
  1066. {
  1067. MWorldMap.releasereadlock();
  1068. return ServerListData[version];
  1069. }
  1070. //LWorld* world = 0;
  1071. int32 ServerNum = 0;
  1072. /* while(iterator.MoreElements()){
  1073. world = iterator.GetData();
  1074. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World)
  1075. ServerNum++;
  1076. iterator.Advance();
  1077. }
  1078. ServerNum+=3;
  1079. */
  1080. uint32 tmpCount = 0;
  1081. map<int32, LWorld*>::iterator map_list;
  1082. for (map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1083. LWorld* world = map_list->second;
  1084. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1085. tmpCount++;
  1086. }
  1087. }
  1088. PacketStruct* packet = configReader.getStruct("LS_WorldList", version);
  1089. packet->setArrayLengthByName("num_worlds", tmpCount);
  1090. string world_data;
  1091. for (map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1092. LWorld* world = map_list->second;
  1093. if ((world->IsInit || (world->ShowDown() && world->ShowDownActive())) && world->GetType() == World) {
  1094. ServerNum++;
  1095. packet->setArrayDataByName("id", world->GetID(), ServerNum - 1);
  1096. if (version <= 283) {
  1097. packet->setArrayDataByName("name", world->GetName(), ServerNum - 1);
  1098. if (!world->ShowDown())
  1099. packet->setArrayDataByName("online", 1, ServerNum - 1);
  1100. if (world->IsLocked())
  1101. packet->setArrayDataByName("locked", 1, ServerNum - 1);
  1102. packet->setArrayDataByName("unknown2", 1, ServerNum - 1);
  1103. packet->setArrayDataByName("unknown3", 1, ServerNum - 1);
  1104. packet->setArrayDataByName("load", world->GetWorldStatus(), ServerNum - 1);
  1105. }
  1106. else
  1107. {
  1108. if (version < 1212)
  1109. packet->setArrayDataByName("allowed_races", 0xFFFFFFFF, ServerNum - 1);
  1110. else if (version < 60006)
  1111. packet->setArrayDataByName("allowed_races", 0x000FFFFF, ServerNum - 1); // + Freeblood
  1112. else
  1113. packet->setArrayDataByName("allowed_races", 0x001FFFFF, ServerNum - 1); // + Aerakyn
  1114. packet->setArrayDataByName("number_online_flag", 1, ServerNum - 1);
  1115. packet->setArrayDataByName("num_players", world->GetPlayerNum(), ServerNum - 1);
  1116. packet->setArrayDataByName("name", world->GetName(), ServerNum - 1);
  1117. packet->setArrayDataByName("name2", world->GetName(), ServerNum - 1);
  1118. packet->setArrayDataByName("feature_set", 0, ServerNum - 1);
  1119. packet->setArrayDataByName("load", world->GetWorldStatus(), ServerNum - 1);
  1120. if (world->IsLocked())
  1121. packet->setArrayDataByName("locked", 1, ServerNum - 1);
  1122. if (world->ShowDown())
  1123. packet->setArrayDataByName("tag", 0, ServerNum - 1);
  1124. else
  1125. packet->setArrayDataByName("tag", 1, ServerNum - 1);
  1126. if (version < 1212)
  1127. packet->setArrayDataByName("unknown", ServerNum, ServerNum - 1);
  1128. }
  1129. }
  1130. }
  1131. EQ2Packet* pack = packet->serialize();
  1132. #ifdef DEBUG
  1133. //Only dump these for people trying to debug this...
  1134. printf("WorldList:\n");
  1135. DumpPacket(pack->pBuffer, pack->size);
  1136. #endif
  1137. if (ServerListData.count(version))
  1138. {
  1139. map<int32, EQ2Packet*>::iterator it = ServerListData.find(version);
  1140. EQ2Packet* tmpPack = ServerListData[version];
  1141. safe_delete(tmpPack);
  1142. ServerListData.erase(it);
  1143. }
  1144. ServerListData.insert(make_pair(version, pack));
  1145. MWorldMap.releasereadlock();
  1146. SetUpdateServerList(false);
  1147. return ServerListData[version];
  1148. }
  1149. void LWorldList::SendWorldStatus(LWorld* chat, char* adminname) {
  1150. struct in_addr in;
  1151. int32 count = 0;
  1152. map<int32,LWorld*>::iterator map_list;
  1153. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1154. LWorld* world = map_list->second;
  1155. if (world->GetIP() != 0 && world->GetType() == World) {
  1156. chat->Message(adminname, "Name: %s", world->GetName());
  1157. in.s_addr = world->GetIP();
  1158. if (world->GetAccountID() != 0) {
  1159. chat->Message(adminname, " Account: %s", world->GetAccount());
  1160. }
  1161. chat->Message(adminname, " Number of Zones: %i", world->GetZoneNum());
  1162. chat->Message(adminname, " Number of Players: %i", world->GetPlayerNum());
  1163. chat->Message(adminname, " IP: %s", inet_ntoa(in));
  1164. if (!world->IsAddressIP()) {
  1165. chat->Message(adminname, " Address: %s", world->GetAddress());
  1166. }
  1167. count++;
  1168. }
  1169. }
  1170. chat->Message(adminname, "%i worlds listed.", count);
  1171. }
  1172. void LWorldList::RemoveByLink(TCPConnection* in_link, int32 in_id, LWorld* ButNotMe) {
  1173. if (in_link == 0)
  1174. return;
  1175. map<int32,LWorld*>::iterator map_list;
  1176. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1177. LWorld* world = map_list->second;
  1178. if (world != ButNotMe && world->GetLink() == in_link && (in_id == 0 || world->GetRemoteID() == in_id)) {
  1179. // world->Link = 0;
  1180. map_list++;
  1181. worldmap.erase ( world->GetID ( ) );
  1182. safe_delete ( world );
  1183. continue;
  1184. }
  1185. }
  1186. }
  1187. void LWorldList::RemoveByID(int32 in_id) {
  1188. if (in_id == 0)
  1189. return;
  1190. LWorld* existWorld = FindByID(in_id);
  1191. if ( existWorld != NULL )
  1192. {
  1193. MWorldMap.writelock();
  1194. worldmap.erase ( in_id );
  1195. MWorldMap.releasewritelock();
  1196. safe_delete ( existWorld );
  1197. }
  1198. }
  1199. bool LWorldList::Init() {
  1200. database.ResetWorldStats ( );
  1201. if (!tcplistener->IsOpen()) {
  1202. return tcplistener->Open(net.GetPort());
  1203. }
  1204. return false;
  1205. }
  1206. void LWorldList::InitWorlds(){
  1207. vector<LWorld*> server_list;
  1208. database.GetServerAccounts(&server_list);
  1209. vector<LWorld*>::iterator iter;
  1210. int i = 0;
  1211. for(iter = server_list.begin(); iter != server_list.end(); iter++, i++){
  1212. LWorld* world = FindByID(server_list[i]->GetAccountID());
  1213. if(!world){
  1214. server_list[i]->ShowDown(true);
  1215. server_list[i]->ShowDownActive(true);
  1216. server_list[i]->SetID ( server_list[i]->GetAccountID ( ) );
  1217. Add ( server_list[i] );
  1218. }
  1219. }
  1220. }
  1221. void LWorldList::ListWorldsToConsole() {
  1222. struct in_addr in;
  1223. cout << "World List:" << endl;
  1224. cout << "============================" << endl;
  1225. map<int32,LWorld*>::iterator map_list;
  1226. for( map_list = worldmap.begin(); map_list != worldmap.end(); map_list++) {
  1227. LWorld* world = map_list->second;
  1228. in.s_addr = world->GetIP();
  1229. if (world->GetType() == World) {
  1230. if (world->GetRemoteID() == 0)
  1231. cout << "ID: " << world->GetID() << ", Name: " << world->GetName() << ", Local, IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", Status: " << world->GetStatus() << ", AccID: " << world->GetAccountID() << endl;
  1232. else
  1233. 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;
  1234. }
  1235. else if (world->GetType() == Chat) {
  1236. cout << "ID: " << world->GetID() << ", Chat Server, IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1237. }
  1238. else if (world->GetType() == Login) {
  1239. if (world->IsOutgoingUplink()) {
  1240. if (world->Connected())
  1241. cout << "ID: " << world->GetID() << ", Login Server (out), IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1242. else
  1243. cout << "ID: " << world->GetID() << ", Login Server (nc), IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1244. }
  1245. else
  1246. cout << "ID: " << world->GetID() << ", Login Server (in), IP: " << inet_ntoa(in) << ":" << world->GetPort() << " (" << world->GetClientPort() << "), AccID: " << world->GetAccountID() << endl;
  1247. }
  1248. else {
  1249. cout << "ID: " << world->GetID() << ", Unknown Type, Name: " << world->GetName() << ", IP: " << inet_ntoa(in) << ":" << world->GetPort() << ", AccID: " << world->GetAccountID() << endl;
  1250. }
  1251. }
  1252. cout << "============================" << endl;
  1253. }
  1254. void LWorldList::AddServerZoneUpdates(LWorld* world, map<int32, LoginZoneUpdate> updates){
  1255. int32 server_id = world->GetID();
  1256. map<int32, LoginZoneUpdate>::iterator itr;
  1257. for(itr = updates.begin(); itr != updates.end(); itr++){
  1258. if(zone_updates_already_used.size() >= 1500 || zone_updates_already_used[server_id].count(itr->first) > 0){
  1259. world->Kick("Hacking attempt.");
  1260. return;
  1261. }
  1262. zone_updates_already_used[server_id][itr->first] = true;
  1263. }
  1264. server_zone_updates.Put(server_id, updates);
  1265. }
  1266. //devn00b temp
  1267. void LWorldList::AddServerEquipmentUpdates(LWorld* world, map<int32, LoginEquipmentUpdate> updates){
  1268. int32 server_id = world->GetID();
  1269. map<int32, LoginEquipmentUpdate>::iterator itr;
  1270. for(itr = updates.begin(); itr != updates.end(); itr++){
  1271. 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__);
  1272. /*if(equip_updates_already_used.size() >= 1500 || equip_updates_already_used[server_id].count(itr->first) > 0)
  1273. {
  1274. LogWrite(LOGIN__ERROR, 0, "Login", "World ID '%i': Hacking attempt. (function: %s, line: %i", world->GetAccountID(), __FUNCTION__, __LINE__);
  1275. world->Kick("Hacking attempt.");
  1276. return;
  1277. }*/
  1278. equip_updates_already_used[server_id][itr->first] = true;
  1279. }
  1280. server_equip_updates.Put(server_id, updates);
  1281. }
  1282. void LWorldList::RequestServerUpdates(LWorld* world){
  1283. if(world){
  1284. ServerPacket* pack = new ServerPacket(ServerOP_ZoneUpdates, sizeof(ZoneUpdateRequest_Struct));
  1285. ZoneUpdateRequest_Struct* request = (ZoneUpdateRequest_Struct*)pack->pBuffer;
  1286. request->max_per_batch = MAX_UPDATE_COUNT;
  1287. world->SendPacket(pack);
  1288. delete pack;
  1289. zone_update_timeouts.Put(world->GetID(), Timer::GetCurrentTime2() + 30000);
  1290. }
  1291. }
  1292. void LWorldList::ProcessServerUpdates(){
  1293. MutexMap<int32, map<int32, LoginZoneUpdate> >::iterator itr = server_zone_updates.begin();
  1294. while(itr.Next()){
  1295. if(itr->second.size() > 0){
  1296. database.SetServerZoneDescriptions(itr->first, itr->second);
  1297. if(itr->second.size() == MAX_UPDATE_COUNT)
  1298. awaiting_zone_update.Put(itr->first, Timer::GetCurrentTime2() + 10000); //only process 20 updates in a 10 second period to avoid network problems
  1299. server_zone_updates.erase(itr->first);
  1300. }
  1301. if(zone_update_timeouts.count(itr->first) == 0 || zone_update_timeouts.Get(itr->first) <= Timer::GetCurrentTime2()){
  1302. zone_update_timeouts.erase(itr->first);
  1303. server_zone_updates.erase(itr->first);
  1304. }
  1305. }
  1306. LWorld* world = 0;
  1307. MWorldMap.readlock();
  1308. map<int32, LWorld*>::iterator map_itr;
  1309. for(map_itr = worldmap.begin(); map_itr != worldmap.end(); map_itr++){
  1310. world = map_itr->second;
  1311. if(world && world->GetID()){
  1312. if(last_updated.count(world) == 0 || last_updated.Get(world) <= Timer::GetCurrentTime2()){
  1313. zone_updates_already_used[world->GetID()].clear();
  1314. RequestServerUpdates(world);
  1315. last_updated.Put(world, Timer::GetCurrentTime2() + 21600000);
  1316. }
  1317. if(awaiting_zone_update.count(world->GetID()) > 0 && awaiting_zone_update.Get(world->GetID()) <= Timer::GetCurrentTime2()){
  1318. awaiting_zone_update.erase(world->GetID());
  1319. RequestServerUpdates(world);
  1320. }
  1321. }
  1322. }
  1323. ProcessLSEquipUpdates();
  1324. MWorldMap.releasereadlock();
  1325. }
  1326. void LWorldList::RequestServerEquipUpdates(LWorld* world)
  1327. {
  1328. if(world)
  1329. {
  1330. ServerPacket *pack_equip = new ServerPacket(ServerOP_LoginEquipment, sizeof(EquipmentUpdateRequest_Struct));
  1331. EquipmentUpdateRequest_Struct *request_equip = (EquipmentUpdateRequest_Struct *)pack_equip->pBuffer;
  1332. request_equip->max_per_batch = MAX_LOGIN_APPEARANCE_COUNT; // item appearance data smaller, request more at a time?
  1333. LogWrite(LOGIN__DEBUG, 1, "Login", "Sending equipment update requests to world: (%s)... (Batch Size: %i)", world->GetName(), request_equip->max_per_batch);
  1334. world->SendPacket(pack_equip);
  1335. delete pack_equip;
  1336. equip_update_timeouts.Put(world->GetID(), Timer::GetCurrentTime2() + 30000);
  1337. }
  1338. }
  1339. void LWorldList::ProcessLSEquipUpdates()
  1340. {
  1341. // process login_equipment updates
  1342. MutexMap<int32, map<int32, LoginEquipmentUpdate> >::iterator itr_equip = server_equip_updates.begin();
  1343. while(itr_equip.Next())
  1344. {
  1345. if(itr_equip->second.size() > 0)
  1346. {
  1347. LogWrite(LOGIN__DEBUG, 1, "Login", "Setting Login Appearances...");
  1348. database.SetServerEquipmentAppearances(itr_equip->first, itr_equip->second);
  1349. if(itr_equip->second.size() == MAX_LOGIN_APPEARANCE_COUNT)
  1350. awaiting_equip_update.Put(itr_equip->first, Timer::GetCurrentTime2() + 10000); //only process 100 updates in a 10 second period to avoid network problems
  1351. server_equip_updates.erase(itr_equip->first);
  1352. }
  1353. if(equip_update_timeouts.count(itr_equip->first) == 0 || equip_update_timeouts.Get(itr_equip->first) <= Timer::GetCurrentTime2())
  1354. {
  1355. LogWrite(LOGIN__DEBUG, 1, "Login", "Clearing Login Appearances Update Timers...");
  1356. equip_update_timeouts.erase(itr_equip->first);
  1357. server_equip_updates.erase(itr_equip->first);
  1358. }
  1359. }
  1360. LWorld* world = 0;
  1361. MWorldMap.readlock();
  1362. map<int32, LWorld*>::iterator map_itr;
  1363. for(map_itr = worldmap.begin(); map_itr != worldmap.end(); map_itr++)
  1364. {
  1365. world = map_itr->second;
  1366. if(world && world->GetID())
  1367. {
  1368. if(last_equip_updated.count(world) == 0 || last_equip_updated.Get(world) <= Timer::GetCurrentTime2())
  1369. {
  1370. LogWrite(LOGIN__DEBUG, 1, "Login", "Clearing Login Appearances Update Counters...");
  1371. equip_updates_already_used[world->GetID()].clear();
  1372. RequestServerEquipUpdates(world);
  1373. last_equip_updated.Put(world, Timer::GetCurrentTime2() + 900000); // every 15 mins
  1374. }
  1375. if( awaiting_equip_update.count(world->GetID()) > 0 && awaiting_equip_update.Get(world->GetID()) <= Timer::GetCurrentTime2())
  1376. {
  1377. LogWrite(LOGIN__DEBUG, 1, "Login", "Erase awaiting equip updates...");
  1378. awaiting_equip_update.erase(world->GetID());
  1379. RequestServerEquipUpdates(world);
  1380. }
  1381. }
  1382. }
  1383. MWorldMap.releasereadlock();
  1384. }
  1385. ThreadReturnType ServerUpdateLoop(void* tmp) {
  1386. #ifdef WIN32
  1387. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  1388. #endif
  1389. if (tmp == 0) {
  1390. ThrowError("ServerUpdateLoop(): tmp = 0!");
  1391. THREAD_RETURN(NULL);
  1392. }
  1393. LWorldList* worldList = (LWorldList*) tmp;
  1394. while (worldList->ContinueServerUpdates()) {
  1395. Sleep(1000);
  1396. worldList->ProcessServerUpdates();
  1397. }
  1398. worldList->ResetServerUpdates();
  1399. THREAD_RETURN(NULL);
  1400. }