LoginServer.cpp 33 KB

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