LoginServer.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. #include "../common/Log.h"
  18. #include <iostream>
  19. using namespace std;
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <iomanip>
  23. using namespace std;
  24. #include <stdlib.h>
  25. #include "../common/version.h"
  26. #include "../common/GlobalHeaders.h"
  27. #include "../common/sha512.h"
  28. #ifdef WIN32
  29. #include <process.h>
  30. #include <WinSock2.h>
  31. #include <windows.h>
  32. #define strncasecmp _strnicmp
  33. #define strcasecmp _stricmp
  34. #else // Pyro: fix for linux
  35. #include <sys/socket.h>
  36. #ifdef FREEBSD //Timothy Whitman - January 7, 2003
  37. #include <sys/types.h>
  38. #endif
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <pthread.h>
  42. #include <unistd.h>
  43. #include <errno.h>
  44. #include "../common/unix.h"
  45. #define SOCKET_ERROR -1
  46. #define INVALID_SOCKET -1
  47. extern int errno;
  48. #endif
  49. #include "../common/servertalk.h"
  50. #include "LoginServer.h"
  51. #include "../common/packet_dump.h"
  52. #include "net.h"
  53. #include "zoneserver.h"
  54. #include "WorldDatabase.h"
  55. #include "Variables.h"
  56. #include "World.h"
  57. #include "../common/ConfigReader.h"
  58. #include "Rules/Rules.h"
  59. extern sint32 numzones;
  60. extern sint32 numclients;
  61. extern NetConnection net;
  62. extern LoginServer loginserver;
  63. extern WorldDatabase database;
  64. extern ZoneAuth zone_auth;
  65. extern Variables variables;
  66. extern ZoneList zone_list;
  67. extern ClientList client_list;
  68. extern volatile bool RunLoops;
  69. volatile bool LoginLoopRunning = false;
  70. extern ConfigReader configReader;
  71. extern RuleManager rule_manager;
  72. bool AttemptingConnect = false;
  73. LoginServer::LoginServer(const char* iAddress, int16 iPort) {
  74. LoginServerIP = ResolveIP(iAddress);
  75. LoginServerPort = iPort;
  76. statusupdate_timer = new Timer(LoginServer_StatusUpdateInterval);
  77. tcpc = new TCPConnection(false);
  78. pTryReconnect = true;
  79. try_auto_update = true;
  80. updates_always = false;
  81. updates_verbose = false;
  82. updates_auto_data = false;
  83. updates_ask = false;
  84. minLockedStatus = 100;
  85. maxPlayers = -1;
  86. minGameFullStatus = 100;
  87. update_server_verified = false;
  88. last_checked_time = 0;
  89. UpdateServerPort = 0;
  90. UpdateServerIP = 0;
  91. update_server_completed = false;
  92. data_waiting = 0;
  93. zone_updates = 0;
  94. loginEquip_updates = 0;
  95. }
  96. LoginServer::~LoginServer() {
  97. delete statusupdate_timer;
  98. delete tcpc;
  99. }
  100. void LoginServer::SendImmediateEquipmentUpdatesForChar(int32 char_id) {
  101. LogWrite(WORLD__DEBUG, 5, "World", "Sending login equipment updates for char_id: %u", char_id);
  102. int16 count = 0;
  103. if(!loginEquip_updates)
  104. loginEquip_updates = database.GetEquipmentUpdates(char_id);
  105. if(loginEquip_updates && loginEquip_updates->size() > 0)
  106. {
  107. map<int32, LoginEquipmentUpdate> send_map;
  108. int32 size = 0;
  109. MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
  110. while(itr.Next())
  111. {
  112. send_map[itr->first] = itr->second;
  113. size += sizeof(EquipmentUpdate_Struct);
  114. loginEquip_updates->erase(itr->first);
  115. count++;
  116. }
  117. ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
  118. EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
  119. updates->total_updates = count;
  120. int32 pos = sizeof(EquipmentUpdateList_Struct);
  121. map<int32, LoginEquipmentUpdate>::iterator send_itr;
  122. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
  123. {
  124. EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
  125. update->id = send_itr->first;
  126. update->world_char_id = send_itr->second.world_char_id;
  127. update->equip_type = send_itr->second.equip_type;
  128. update->red = send_itr->second.red;
  129. update->green = send_itr->second.green;
  130. update->blue = send_itr->second.blue;
  131. update->highlight_red = send_itr->second.red;
  132. update->highlight_green = send_itr->second.green;
  133. update->highlight_blue = send_itr->second.blue;
  134. update->slot = send_itr->second.slot;
  135. pos += sizeof(EquipmentUpdate_Struct);
  136. }
  137. SendPacket(outpack);
  138. outpack->Deflate();
  139. safe_delete(outpack);
  140. }
  141. if(loginEquip_updates && count)
  142. loginEquip_updates->clear();
  143. if(loginEquip_updates && loginEquip_updates->size() == 0)
  144. {
  145. database.UpdateLoginEquipment();
  146. safe_delete(loginEquip_updates);
  147. }
  148. }
  149. bool LoginServer::Process() {
  150. if(last_checked_time > Timer::GetCurrentTime2())
  151. return true;
  152. last_checked_time = Timer::GetCurrentTime2() + 50;
  153. bool ret = true;
  154. if (statusupdate_timer->Check()) {
  155. this->SendStatus();
  156. }
  157. /************ Get all packets from packet manager out queue and process them ************/
  158. ServerPacket *pack = 0;
  159. while((pack = tcpc->PopPacket()))
  160. {
  161. switch(pack->opcode)
  162. {
  163. case ServerOP_LSFatalError:
  164. {
  165. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LSFatalError", pack->opcode, pack->opcode);
  166. LogWrite(WORLD__ERROR, 0, "World", "Login Server returned a fatal error: %s\n", pack->pBuffer);
  167. tcpc->Disconnect();
  168. ret = false;
  169. net.ReadLoginINI();
  170. break;
  171. }
  172. case ServerOP_CharTimeStamp:
  173. {
  174. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharTimeStamp", pack->opcode, pack->opcode);
  175. if(pack->size != sizeof(CharacterTimeStamp_Struct))
  176. break;
  177. CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) pack->pBuffer;
  178. // determine if the character exists and retrieve its latest timestamp from the world server
  179. bool char_exist = false;
  180. //int32 character_timestamp = database.GetCharacterTimeStamp(cts->char_id,cts->account_id,&char_exist);
  181. if(!char_exist)
  182. {
  183. //Character doesn't exist, get rid of it
  184. SendDeleteCharacter ( cts );
  185. break;
  186. }
  187. break;
  188. }
  189. case ServerOP_GetTableData:{
  190. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetTableData", pack->opcode, pack->opcode);
  191. uchar* tmp = new uchar[pack->size];
  192. memcpy(tmp, pack->pBuffer, pack->size);
  193. data_updates_waiting.push_back(tmp);
  194. break;
  195. }
  196. case ServerOP_GetTableQuery:{
  197. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetTableQuery", pack->opcode, pack->opcode);
  198. uchar* tmp = new uchar[pack->size];
  199. memcpy(tmp, pack->pBuffer, pack->size);
  200. table_updates_waiting.push_back(tmp);
  201. break;
  202. }
  203. case ServerOP_GetLatestTables:{
  204. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetLatestTables", pack->opcode, pack->opcode);
  205. data_waiting = new uchar[pack->size];
  206. memcpy(data_waiting, pack->pBuffer, pack->size);
  207. break;
  208. }
  209. // Push Character Select "item appearances" to login_equipment table
  210. case ServerOP_LoginEquipment:{
  211. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LoginEquipment", pack->opcode, pack->opcode);
  212. LogWrite(MISC__TODO, 0, "TODO", "Implement map<character id <map<slot id, updatestruct> > method to update Login.\n%s, %s, %i", __FILE__, __FUNCTION__, __LINE__);
  213. if( pack->size == sizeof(EquipmentUpdateRequest_Struct) )
  214. {
  215. int16 max = ((EquipmentUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
  216. int16 count = 0;
  217. if(!loginEquip_updates)
  218. loginEquip_updates = database.GetEquipmentUpdates();
  219. if(loginEquip_updates && loginEquip_updates->size() > 0)
  220. {
  221. map<int32, LoginEquipmentUpdate> send_map;
  222. int32 size = 0;
  223. MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
  224. while(itr.Next() && count < max)
  225. {
  226. send_map[itr->first] = itr->second;
  227. size += sizeof(EquipmentUpdate_Struct);
  228. loginEquip_updates->erase(itr->first);
  229. count++;
  230. }
  231. ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
  232. EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
  233. updates->total_updates = count;
  234. int32 pos = sizeof(EquipmentUpdateList_Struct);
  235. map<int32, LoginEquipmentUpdate>::iterator send_itr;
  236. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
  237. {
  238. EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
  239. update->id = send_itr->first;
  240. update->world_char_id = send_itr->second.world_char_id;
  241. update->equip_type = send_itr->second.equip_type;
  242. update->red = send_itr->second.red;
  243. update->green = send_itr->second.green;
  244. update->blue = send_itr->second.blue;
  245. update->highlight_red = send_itr->second.red;
  246. update->highlight_green = send_itr->second.green;
  247. update->highlight_blue = send_itr->second.blue;
  248. update->slot = send_itr->second.slot;
  249. pos += sizeof(EquipmentUpdate_Struct);
  250. }
  251. SendPacket(outpack);
  252. outpack->Deflate();
  253. safe_delete(outpack);
  254. }
  255. if(loginEquip_updates && count < max)
  256. loginEquip_updates->clear();
  257. if(loginEquip_updates && loginEquip_updates->size() == 0)
  258. {
  259. database.UpdateLoginEquipment();
  260. safe_delete(loginEquip_updates);
  261. }
  262. }
  263. break;
  264. }
  265. case ServerOP_ZoneUpdates:{
  266. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ZoneUpdates", pack->opcode, pack->opcode);
  267. if(pack->size == sizeof(ZoneUpdateRequest_Struct)){
  268. int16 max = ((ZoneUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
  269. int16 count = 0;
  270. if(!zone_updates)
  271. zone_updates = database.GetZoneUpdates();
  272. if(zone_updates && zone_updates->size() > 0){
  273. map<int32, LoginZoneUpdate> send_map;
  274. int32 size = 0;
  275. MutexMap<int32, LoginZoneUpdate>::iterator itr = zone_updates->begin();
  276. while(itr.Next() && count < max){
  277. send_map[itr->first] = itr->second;
  278. size += sizeof(ZoneUpdate_Struct) + itr->second.name.length() + itr->second.description.length();
  279. zone_updates->erase(itr->first);
  280. count++;
  281. }
  282. ServerPacket* outpack = new ServerPacket(ServerOP_ZoneUpdates, size + sizeof(ZoneUpdateList_Struct)+5);
  283. ZoneUpdateList_Struct* updates = (ZoneUpdateList_Struct*)outpack->pBuffer;
  284. updates->total_updates = count;
  285. int32 pos = sizeof(ZoneUpdateList_Struct);
  286. map<int32, LoginZoneUpdate>::iterator send_itr;
  287. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++){
  288. ZoneUpdate_Struct* update = (ZoneUpdate_Struct*)(outpack->pBuffer + pos);
  289. update->zone_id = send_itr->first;
  290. update->zone_name_length = send_itr->second.name.length();
  291. update->zone_desc_length = send_itr->second.description.length();
  292. strcpy(update->data, send_itr->second.name.c_str());
  293. strcpy(update->data + send_itr->second.name.length(), send_itr->second.description.c_str());
  294. pos += sizeof(ZoneUpdate_Struct) + send_itr->second.name.length() + send_itr->second.description.length();
  295. }
  296. SendPacket(outpack);
  297. outpack->Deflate();
  298. safe_delete(outpack);
  299. }
  300. if(zone_updates && count < max)
  301. zone_updates->clear();
  302. if(zone_updates && zone_updates->size() == 0){
  303. database.UpdateLoginZones();
  304. safe_delete(zone_updates);
  305. }
  306. }
  307. break;
  308. }
  309. case ServerOP_CharacterCreate:{
  310. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharacterCreate", pack->opcode, pack->opcode);
  311. int16 version = 1;
  312. if(pack->pBuffer[0] > 0)
  313. memcpy(&version, pack->pBuffer, sizeof(int16));
  314. //DumpPacket(pack->pBuffer,pack->size);
  315. PacketStruct* packet = configReader.getStruct("CreateCharacter", version);
  316. int8 resp = 0;
  317. int32 acct_id = 0;
  318. int32 char_id = 0;
  319. if(packet && packet->LoadPacketData(pack->pBuffer+sizeof(int16),pack->size - sizeof(int16))){
  320. EQ2_16BitString name = packet->getType_EQ2_16BitString_ByName("name");
  321. resp = database.CheckNameFilter(name.data.c_str());
  322. acct_id = packet->getType_int32_ByName("account_id");
  323. LogWrite(WORLD__DEBUG, 0, "World", "Response: %i", (int)resp);
  324. sint16 lowestStatus = database.GetLowestCharacterAdminStatus(acct_id);
  325. if(lowestStatus == -2)
  326. resp = UNKNOWNERROR_REPLY2;
  327. else if(resp == CREATESUCCESS_REPLY)
  328. char_id = database.SaveCharacter(packet, acct_id);
  329. }
  330. else{
  331. LogWrite(WORLD__ERROR, 0, "World", "Invalid creation request!");
  332. resp = UNKNOWNERROR_REPLY;
  333. }
  334. // send name filter response data back to the login server
  335. SendFilterNameResponse ( resp , acct_id , char_id );
  336. safe_delete(packet);
  337. break;
  338. }
  339. case ServerOP_BasicCharUpdate: {
  340. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_BasicCharUpdate", pack->opcode, pack->opcode);
  341. if(pack->size != sizeof(CharDataUpdate_Struct))
  342. break;
  343. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*) pack->pBuffer;
  344. switch(cdu->update_field)
  345. {
  346. case DELETE_UPDATE_FLAG:
  347. {
  348. LogWrite(WORLD__DEBUG, 0, "World", "Delete character request: %i %i",cdu->account_id,cdu->char_id );
  349. database.DeleteCharacter(cdu->account_id,cdu->char_id);
  350. break;
  351. }
  352. }
  353. break;
  354. }
  355. case ServerOP_UsertoWorldReq:{
  356. LogWrite(OPCODE__DEBUG, 0, "Opcode", "Opcode 0x%X (%i): ServerOP_UsertoWorldReq", pack->opcode, pack->opcode);
  357. UsertoWorldRequest_Struct* utwr = (UsertoWorldRequest_Struct*) pack->pBuffer;
  358. /*int32 id = database.GetAccountIDFromLSID(utwr->lsaccountid);
  359. sint16 status = database.CheckStatus(id);
  360. */
  361. int32 access_key = 0;
  362. // if it is a accepted login, we add the zone auth request
  363. access_key = DetermineCharacterLoginRequest ( utwr );
  364. if ( access_key != 0 )
  365. {
  366. zone_auth.PurgeInactiveAuth();
  367. char* characterName = database.GetCharacterName( utwr->char_id );
  368. if(characterName != 0){
  369. ZoneAuthRequest* zar = new ZoneAuthRequest(utwr->lsaccountid,characterName,access_key);
  370. zar->setFirstLogin ( true );
  371. zone_auth.AddAuth(zar);
  372. safe_delete_array(characterName);
  373. }
  374. }
  375. break;
  376. }
  377. case ServerOP_ResetDatabase:{
  378. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ResetDatabase", pack->opcode, pack->opcode);
  379. database.ResetDatabase();
  380. break;
  381. }
  382. case UpdateServerOP_Verified:{
  383. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_Verified", pack->opcode, pack->opcode);
  384. update_server_verified = true;
  385. break;
  386. }
  387. case UpdateServerOP_DisplayMsg:{
  388. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_DisplayMsg", pack->opcode, pack->opcode);
  389. LogWrite(WORLD__WARNING, 0, "World", "\t%s", pack->pBuffer);
  390. break;
  391. }
  392. case UpdateServerOP_Completed:{
  393. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_Completed", pack->opcode, pack->opcode);
  394. update_server_completed = true;
  395. break;
  396. }
  397. default:
  398. {
  399. LogWrite(WORLD__ERROR, 0, "World", "Unhandled opcode: %i", pack->opcode);
  400. DumpPacket(pack);
  401. }
  402. }
  403. safe_delete(pack);
  404. // break out if ret is now false
  405. if (!ret)
  406. break;
  407. }
  408. return ret;
  409. }
  410. // this should always be called in a new thread
  411. #ifdef WIN32
  412. void AutoInitLoginServer(void *tmp) {
  413. #else
  414. void *AutoInitLoginServer(void *tmp) {
  415. #endif
  416. if (loginserver.GetState() == TCPS_Ready) {
  417. InitLoginServer();
  418. }
  419. #ifndef WIN32
  420. return 0;
  421. #endif
  422. }
  423. bool InitLoginServer() {
  424. if (loginserver.GetState() != TCPS_Ready) {
  425. LogWrite(WORLD__ERROR, 0, "World", "InitLoginServer() while already attempting connect.");
  426. return false;
  427. }
  428. if (!net.LoginServerInfo) {
  429. LogWrite(WORLD__ERROR, 0, "World", "Login server info not loaded.");
  430. return false;
  431. }
  432. AttemptingConnect = true;
  433. int16 port;
  434. char* address = net.GetLoginInfo(&port);
  435. loginserver.Connect(address, port);
  436. return true;
  437. }
  438. void LoginServer::InitLoginServerVariables()
  439. {
  440. minLockedStatus = rule_manager.GetGlobalRule(R_World, ServerLockedOverrideStatus)->GetSInt16();
  441. maxPlayers = rule_manager.GetGlobalRule(R_World, MaxPlayers)->GetSInt16();
  442. minGameFullStatus = rule_manager.GetGlobalRule(R_World, MaxPlayersOverrideStatus)->GetSInt16();
  443. }
  444. void LoginServer::ProcessDataUpdate(uchar* data){
  445. TableDataQuery data_query;
  446. TableQuery table_queries;
  447. data_query.DeSerialize(data);
  448. string query;
  449. for(int32 i=0;i<data_query.num_queries;i++)
  450. {
  451. query = string("replace into ").append(data_query.tablename).append(" ");
  452. query.append(data_query.columns).append(" values(").append(data_query.queries[i]->query).append(")\0");
  453. char* new_query = new char[query.length() + 1];
  454. memset(new_query, 0, query.length() + 1);
  455. memcpy(new_query, query.c_str(), query.length());
  456. table_queries.AddQuery(new_query);
  457. }
  458. strcpy(table_queries.tablename, data_query.tablename);
  459. table_queries.latest_version = data_query.version;
  460. string queries = table_queries.GetQueriesString();
  461. char answer = 0;
  462. if(UpdatesAsk() || UpdatesVerbose())
  463. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Queries:\n%s", queries.c_str());
  464. if(!UpdatesAuto() && UpdatesAsk())
  465. {
  466. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest data version? ([Y]es, [N]o, [A]ll)");
  467. cin >> answer;
  468. if(toupper(answer) == 'A')
  469. UpdatesAuto(true);
  470. }
  471. if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
  472. {
  473. bool success = database.RunDatabaseQueries(&table_queries, false, true);
  474. string table = string(data_query.tablename);
  475. if(table != last_data_update_table)
  476. {
  477. if(success)
  478. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tData Updated: '%s' = SUCCESS!", data_query.tablename);
  479. else
  480. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Data Updated: '%s' = FAILED!", data_query.tablename);
  481. last_data_update_table = table;
  482. }
  483. }
  484. }
  485. void LoginServer::ProcessTableUpdate(uchar* data) {
  486. TableQuery table_query;
  487. table_query.DeSerialize(data);
  488. string queries = table_query.GetQueriesString();
  489. char answer = 0;
  490. if(UpdatesAsk() || UpdatesVerbose())
  491. {
  492. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Found for '%s'!", table_query.tablename);
  493. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tNew Version: %i", table_query.latest_version);
  494. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tYour Version: %i", table_query.your_version);
  495. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Query:\n%s", queries.c_str());
  496. }
  497. if(!UpdatesAuto() && UpdatesAsk())
  498. {
  499. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest table version? ([Y]es, [N]o, [A]ll)");
  500. cin >> answer;
  501. if(toupper(answer) == 'A')
  502. UpdatesAuto(true);
  503. }
  504. if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
  505. {
  506. bool success = database.RunDatabaseQueries(&table_query, false);
  507. if(success)
  508. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Table: '%s' = SUCCESS!", table_query.tablename);
  509. else
  510. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Update Table: '%s' = FAILED!", table_query.tablename);
  511. }
  512. }
  513. int32 LoginServer::ProcessTableUpdates(uchar* data) {
  514. int32 ret = 0;
  515. /*LatestTableVersions table_versions;
  516. table_versions.DeSerialize(data);
  517. int32 total_tables = table_versions.GetTotalTables();
  518. int32 max_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  519. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Max Version: %i", max_version);
  520. vector<ServerPacket*> packet_requests;
  521. ServerPacket* outpack = 0;
  522. for(int32 i=0;i<total_tables;i++){
  523. float version = database.CheckTableVersions(table_versions.GetTable(i).name);
  524. if( (version < table_versions.GetTable(i).version) )
  525. {
  526. outpack = new ServerPacket(ServerOP_GetTableQuery, sizeof(TableVersion));
  527. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  528. strcpy(out_version->name, table_versions.GetTable(i).name);
  529. out_version->max_table_version = max_version;
  530. out_version->version = (int32)version;
  531. out_version->last = 0;
  532. packet_requests.push_back(outpack);
  533. ret++;
  534. }
  535. }
  536. if( ret )
  537. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates!");
  538. else
  539. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--No Table Updates, continuing...");
  540. int32 total_packets = packet_requests.size();
  541. for(int32 i=0;i<total_packets;i++)
  542. {
  543. outpack = packet_requests[i];
  544. if((i+1) == total_packets)
  545. {
  546. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  547. out_version->last = 1;
  548. }
  549. SendPacket(outpack);
  550. safe_delete(outpack);
  551. }*/
  552. return ret;
  553. }
  554. int32 LoginServer::ProcessDataUpdates(uchar* data) {
  555. int32 ret = 0;
  556. /*LatestTableVersions table_versions;
  557. table_versions.DeSerialize(data);
  558. int32 total_tables = table_versions.GetTotalTables();
  559. vector<ServerPacket*> packet_requests;
  560. ServerPacket* outpack = 0;
  561. for(int32 i=0;i<total_tables;i++){
  562. if(UpdatesAutoData()){
  563. float version = database.GetLatestDataTableVersion(table_versions.GetTable(i).name);
  564. if(version >= 0 && version < table_versions.GetTable(i).data_version )
  565. {
  566. ServerPacket* outpack = new ServerPacket(ServerOP_GetTableData, sizeof(TableVersion));
  567. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  568. strcpy(out_version->name, table_versions.GetTable(i).name);
  569. out_version->max_data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  570. out_version->data_version = (sint32)version;
  571. out_version->last = 0;
  572. memset(out_version->column_names, 0, 1000);
  573. string columns = database.GetColumnNames(out_version->name);
  574. if(columns.length() > 1)
  575. strcpy(out_version->column_names, columns.c_str());
  576. packet_requests.push_back(outpack);
  577. ret++;
  578. }
  579. }
  580. }
  581. if( ret )
  582. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Data Updates!");
  583. int32 total_packets = packet_requests.size();
  584. for(int32 i=0;i<total_packets;i++)
  585. {
  586. outpack = packet_requests[i];
  587. if((i+1) == total_packets)
  588. {
  589. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  590. out_version->last = 1;
  591. }
  592. SendPacket(outpack);
  593. safe_delete(outpack);
  594. }*/
  595. return ret;
  596. }
  597. bool LoginServer::CheckAndWait(Timer* timer){
  598. Timer::SetCurrentTime();
  599. if(timer->Check(false)){
  600. if(timer->GetDuration() == 5000){
  601. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Please wait...");
  602. timer->SetTimer(25000);
  603. timer->Start();
  604. }
  605. else
  606. return false;
  607. }
  608. Sleep(10);
  609. if(!loginserver.Process())
  610. tcpc->Disconnect();
  611. return Connected();
  612. }
  613. bool LoginServer::ConnectToUpdateServer(const char* iAddress, int16 iPort)
  614. {
  615. if(!try_auto_update)
  616. return true;
  617. char errbuf[TCPConnection_ErrorBufferSize];
  618. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  619. if (iAddress == 0)
  620. {
  621. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: address == 0");
  622. return false;
  623. }
  624. else
  625. {
  626. if ((UpdateServerIP = ResolveIP(iAddress, errbuf)) == 0)
  627. {
  628. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "LoginServer::ConnectToUpdateServer: Resolving IP address: '%s'", errbuf[0] == '\0' ? errbuf : " ");
  629. return false;
  630. }
  631. }
  632. if (iPort != 0)
  633. UpdateServerPort = iPort;
  634. if (UpdateServerIP == 0 || UpdateServerPort == 0)
  635. {
  636. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: Connect info incomplete, cannot connect.");
  637. return false;
  638. }
  639. if (tcpc->Connect(UpdateServerIP, UpdateServerPort, errbuf))
  640. {
  641. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Connected to Update Server: %s:%i", iAddress, UpdateServerPort);
  642. SendInfo();
  643. //wait for a max of 30 seconds to be verified by patcher
  644. //display a message at 5 seconds
  645. Timer waitTimer(5000);
  646. waitTimer.Start();
  647. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Checking for updates.");
  648. GetLatestTables();
  649. while(CheckAndWait(&waitTimer) && !update_server_verified);
  650. database.CheckVersionTable();
  651. if(!update_server_verified)
  652. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Error while getting updates!");
  653. if(update_server_verified)
  654. {
  655. update_server_completed = false;
  656. waitTimer.SetTimer(5000);
  657. waitTimer.Start();
  658. while(CheckAndWait(&waitTimer) && !data_waiting);
  659. if(data_waiting)
  660. {
  661. int32 requests = ProcessTableUpdates(data_waiting);
  662. if(requests > 0)
  663. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates.");
  664. uchar* data = 0;
  665. waitTimer.SetTimer(30000);
  666. waitTimer.Start();
  667. if(requests > 0)
  668. {
  669. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Table Updates.");
  670. database.DisableConstraints();
  671. deque<uchar*> alter_table_updates_waiting;
  672. while((CheckAndWait(&waitTimer) && !update_server_completed) || table_updates_waiting.size() > 0)
  673. {
  674. if(table_updates_waiting.size() > 0)
  675. {
  676. data = table_updates_waiting.front();
  677. table_updates_waiting.pop_front();
  678. ProcessTableUpdate(data);
  679. safe_delete(data);
  680. waitTimer.SetTimer(30000);
  681. waitTimer.Start();
  682. }
  683. }
  684. if(!CheckAndWait(&waitTimer))
  685. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
  686. else
  687. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Table Update complete.");
  688. database.EnableConstraints();
  689. }
  690. else
  691. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Schema...");
  692. requests = ProcessDataUpdates(data_waiting);
  693. if(requests > 0)
  694. {
  695. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Data Updates.");
  696. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "** Data Updates are SLOW! Do not abort! **");
  697. database.DisableConstraints();
  698. deque<uchar*> alter_table_updates_waiting;
  699. int32 count = 0;
  700. while((CheckAndWait(&waitTimer) && count < requests) || data_updates_waiting.size() > 0)
  701. {
  702. if(data_updates_waiting.size() > 0)
  703. {
  704. data = data_updates_waiting.front();
  705. data_updates_waiting.pop_front();
  706. ProcessDataUpdate(data);
  707. count++;
  708. safe_delete_array(data);
  709. waitTimer.SetTimer(30000);
  710. waitTimer.Start();
  711. }
  712. }
  713. if(!CheckAndWait(&waitTimer))
  714. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
  715. else
  716. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Data Updates complete.");
  717. database.EnableConstraints();
  718. }
  719. else
  720. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Data...");
  721. safe_delete_array(data_waiting);
  722. }
  723. try_auto_update = false;
  724. }
  725. tcpc->Disconnect();
  726. return update_server_verified;
  727. }
  728. else
  729. {
  730. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: '%s'", errbuf[0] == '\0' ? errbuf : " ");
  731. return false;
  732. }
  733. }
  734. bool LoginServer::Connect(const char* iAddress, int16 iPort) {
  735. if(!pTryReconnect)
  736. return false;
  737. char errbuf[TCPConnection_ErrorBufferSize];
  738. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  739. if (iAddress == 0) {
  740. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: address == 0");
  741. return false;
  742. }
  743. else {
  744. if ((LoginServerIP = ResolveIP(iAddress, errbuf)) == 0) {
  745. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Resolving IP address: '%s'", errbuf);
  746. return false;
  747. }
  748. }
  749. if (iPort != 0)
  750. LoginServerPort = iPort;
  751. if (LoginServerIP == 0 || LoginServerPort == 0) {
  752. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Connect info incomplete, cannot connect");
  753. return false;
  754. }
  755. if (tcpc->Connect(LoginServerIP, LoginServerPort, errbuf)) {
  756. LogWrite(WORLD__INFO, 0, "World", "Connected to LoginServer: %s: %i", iAddress, LoginServerPort);
  757. SendInfo();
  758. SendStatus();
  759. return true;
  760. }
  761. else {
  762. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: '%s'", errbuf);
  763. return false;
  764. }
  765. }
  766. void LoginServer::GetLatestTables(){
  767. ServerPacket* pack = new ServerPacket(ServerOP_GetLatestTables, sizeof(GetLatestTables_Struct));
  768. GetLatestTables_Struct* data = (GetLatestTables_Struct*)pack->pBuffer;
  769. data->table_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  770. data->data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  771. SendPacket(pack);
  772. delete pack;
  773. }
  774. void LoginServer::SendInfo() {
  775. ServerPacket* pack = new ServerPacket;
  776. pack->opcode = ServerOP_LSInfo;
  777. pack->size = sizeof(ServerLSInfo_Struct);
  778. pack->pBuffer = new uchar[pack->size];
  779. memset(pack->pBuffer, 0, pack->size);
  780. ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
  781. strcpy(lsi->protocolversion, EQEMU_PROTOCOL_VERSION);
  782. strcpy(lsi->serverversion, CURRENT_VERSION);
  783. strcpy(lsi->name, net.GetWorldName());
  784. strcpy(lsi->account, net.GetWorldAccount());
  785. lsi->dbversion = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  786. #ifdef _DEBUG
  787. lsi->servertype = 4;
  788. #endif
  789. int8 tmppass[16];
  790. string passwdSha512 = sha512(net.GetWorldPassword());
  791. memcpy(lsi->password, (char*)passwdSha512.c_str(), passwdSha512.length());
  792. strcpy(lsi->address, net.GetWorldAddress());
  793. SendPacket(pack);
  794. delete pack;
  795. }
  796. void LoginServer::SendStatus() {
  797. statusupdate_timer->Start();
  798. ServerPacket* pack = new ServerPacket;
  799. pack->opcode = ServerOP_LSStatus;
  800. pack->size = sizeof(ServerLSStatus_Struct);
  801. pack->pBuffer = new uchar[pack->size];
  802. memset(pack->pBuffer, 0, pack->size);
  803. ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
  804. if (net.world_locked)
  805. lss->status = -2;
  806. else if(loginserver.maxPlayers > -1 && numclients >= loginserver.maxPlayers)
  807. lss->status = -3;
  808. else
  809. lss->status = 1;
  810. lss->num_zones = numzones;
  811. lss->num_players = numclients;
  812. lss->world_max_level = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt8();
  813. SendPacket(pack);
  814. delete pack;
  815. }
  816. void LoginServer::SendDeleteCharacter ( CharacterTimeStamp_Struct* cts ) {
  817. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  818. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  819. cdu->account_id = cts->account_id;
  820. cdu->char_id = cts->char_id;
  821. cdu->update_field = DELETE_UPDATE_FLAG;
  822. cdu->update_data = 1;
  823. SendPacket(outpack);
  824. safe_delete(outpack);
  825. }
  826. void LoginServer::SendFilterNameResponse ( int8 resp, int32 acct_id , int32 char_id ) {
  827. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, sizeof(WorldCharNameFilterResponse_Struct));
  828. WorldCharNameFilterResponse_Struct* wcfr = (WorldCharNameFilterResponse_Struct*)outpack->pBuffer;
  829. wcfr->response = resp;
  830. wcfr->account_id = acct_id;
  831. wcfr->char_id = char_id;
  832. SendPacket(outpack);
  833. safe_delete(outpack);
  834. }
  835. int32 LoginServer::DetermineCharacterLoginRequest ( UsertoWorldRequest_Struct* utwr ) {
  836. LogWrite(LOGIN__TRACE, 9, "Login", "Enter: %s", __FUNCTION__);
  837. ServerPacket* outpack = new ServerPacket;
  838. outpack->opcode = ServerOP_UsertoWorldResp;
  839. outpack->size = sizeof(UsertoWorldResponse_Struct);
  840. outpack->pBuffer = new uchar[outpack->size];
  841. memset(outpack->pBuffer, 0, outpack->size);
  842. UsertoWorldResponse_Struct* utwrs = (UsertoWorldResponse_Struct*) outpack->pBuffer;
  843. utwrs->lsaccountid = utwr->lsaccountid;
  844. utwrs->char_id = utwr->char_id;
  845. utwrs->ToID = utwr->FromID;
  846. int32 timestamp = Timer::GetUnixTimeStamp();
  847. utwrs->access_key = timestamp;
  848. // set default response to 0
  849. utwrs->response = 0;
  850. sint16 lowestStatus = database.GetLowestCharacterAdminStatus( utwr->lsaccountid );
  851. sint16 status = 0;
  852. if(lowestStatus == -2)
  853. status = -1;
  854. else
  855. status = database.GetCharacterAdminStatus ( utwr->lsaccountid , utwr->char_id );
  856. if(status < 100 && zone_list.ClientConnected(utwr->lsaccountid))
  857. status = -9;
  858. if(status < 0){
  859. switch(status){
  860. case -10:
  861. utwrs->response = PLAY_ERROR_CHAR_NOT_LOADED;
  862. break;
  863. case -9:
  864. utwrs->response = 0;//PLAY_ERROR_ACCOUNT_IN_USE;
  865. break;
  866. case -8:
  867. utwrs->response = PLAY_ERROR_LOADING_ERROR;
  868. break;
  869. case -1:
  870. utwrs->response = PLAY_ERROR_ACCOUNT_BANNED;
  871. break;
  872. default:
  873. utwrs->response = PLAY_ERROR_PROBLEM;
  874. }
  875. }
  876. else if(net.world_locked == true){
  877. LogWrite(WORLD__INFO, 0, "World", "Login Lock Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  878. // has high enough status, allow it
  879. if(status >= loginserver.minLockedStatus)
  880. utwrs->response = 1;
  881. }
  882. else if(loginserver.maxPlayers > -1 && ((sint16)client_list.Count()) >= loginserver.maxPlayers)
  883. {
  884. LogWrite(WORLD__INFO, 0, "World", "Login GameFull Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minGameFullStatus,status,utwr->char_id );
  885. // has high enough status, allow it
  886. if(status >= loginserver.minGameFullStatus)
  887. {
  888. utwrs->response = 1;
  889. }
  890. else
  891. utwrs->response = -3; // server full response is -3
  892. }
  893. else
  894. utwrs->response = 1;
  895. /*sint32 x = database.CommandRequirement("$MAXCLIENTS");
  896. if( (sint32)numplayers >= x && x != -1 && x != 255 && status < 80)
  897. utwrs->response = -3;
  898. if(status == -1)
  899. utwrs->response = -1;
  900. if(status == -2)
  901. utwrs->response = -2;
  902. */
  903. //printf("Response is %i for %i\n",utwrs->response,id);
  904. if((strcmp(net.GetWorldAddress(), utwr->ip_address)==0) && (strlen(net.GetInternalWorldAddress())>0))
  905. strcpy(utwrs->ip_address, net.GetInternalWorldAddress());
  906. else
  907. strcpy(utwrs->ip_address, net.GetWorldAddress());
  908. utwrs->port = net.GetWorldPort();
  909. utwrs->worldid = utwr->worldid;
  910. SendPacket(outpack);
  911. delete outpack;
  912. LogWrite(LOGIN__TRACE, 9, "Login", "Exit: %s with timestamp=%u", __FUNCTION__, timestamp);
  913. // depending on the response determined above, this could return 0 (for failure)
  914. return timestamp;
  915. }