LoginServer.cpp 33 KB

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