LWorld.cpp 41 KB

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