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/md5.h"
  26. #include "../common/version.h"
  27. #include "../common/GlobalHeaders.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\nDisabling reconnect.", pack->pBuffer);
  169. pTryReconnect = false;
  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. }
  406. return ret;
  407. }
  408. // this should always be called in a new thread
  409. #ifdef WIN32
  410. void AutoInitLoginServer(void *tmp) {
  411. #else
  412. void *AutoInitLoginServer(void *tmp) {
  413. #endif
  414. if (loginserver.GetState() == TCPS_Ready) {
  415. InitLoginServer();
  416. }
  417. #ifndef WIN32
  418. return 0;
  419. #endif
  420. }
  421. bool InitLoginServer() {
  422. if (loginserver.GetState() != TCPS_Ready) {
  423. LogWrite(WORLD__ERROR, 0, "World", "InitLoginServer() while already attempting connect.");
  424. return false;
  425. }
  426. if (!net.LoginServerInfo) {
  427. LogWrite(WORLD__ERROR, 0, "World", "Login server info not loaded.");
  428. return false;
  429. }
  430. AttemptingConnect = true;
  431. int16 port;
  432. char* address = net.GetLoginInfo(&port);
  433. loginserver.Connect(address, port);
  434. return true;
  435. }
  436. void LoginServer::InitLoginServerVariables()
  437. {
  438. minLockedStatus = rule_manager.GetGlobalRule(R_World, ServerLockedOverrideStatus)->GetSInt16();
  439. maxPlayers = rule_manager.GetGlobalRule(R_World, MaxPlayers)->GetSInt16();
  440. minGameFullStatus = rule_manager.GetGlobalRule(R_World, MaxPlayersOverrideStatus)->GetSInt16();
  441. }
  442. void LoginServer::ProcessDataUpdate(uchar* data){
  443. TableDataQuery data_query;
  444. TableQuery table_queries;
  445. data_query.DeSerialize(data);
  446. string query;
  447. for(int32 i=0;i<data_query.num_queries;i++)
  448. {
  449. query = string("replace into ").append(data_query.tablename).append(" ");
  450. query.append(data_query.columns).append(" values(").append(data_query.queries[i]->query).append(")\0");
  451. char* new_query = new char[query.length() + 1];
  452. memset(new_query, 0, query.length() + 1);
  453. memcpy(new_query, query.c_str(), query.length());
  454. table_queries.AddQuery(new_query);
  455. }
  456. strcpy(table_queries.tablename, data_query.tablename);
  457. table_queries.latest_version = data_query.version;
  458. string queries = table_queries.GetQueriesString();
  459. char answer = 0;
  460. if(UpdatesAsk() || UpdatesVerbose())
  461. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Queries:\n%s", queries.c_str());
  462. if(!UpdatesAuto() && UpdatesAsk())
  463. {
  464. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest data version? ([Y]es, [N]o, [A]ll)");
  465. cin >> answer;
  466. if(toupper(answer) == 'A')
  467. UpdatesAuto(true);
  468. }
  469. if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
  470. {
  471. bool success = database.RunDatabaseQueries(&table_queries, false, true);
  472. string table = string(data_query.tablename);
  473. if(table != last_data_update_table)
  474. {
  475. if(success)
  476. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tData Updated: '%s' = SUCCESS!", data_query.tablename);
  477. else
  478. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Data Updated: '%s' = FAILED!", data_query.tablename);
  479. last_data_update_table = table;
  480. }
  481. }
  482. }
  483. void LoginServer::ProcessTableUpdate(uchar* data) {
  484. TableQuery table_query;
  485. table_query.DeSerialize(data);
  486. string queries = table_query.GetQueriesString();
  487. char answer = 0;
  488. if(UpdatesAsk() || UpdatesVerbose())
  489. {
  490. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Found for '%s'!", table_query.tablename);
  491. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tNew Version: %i", table_query.latest_version);
  492. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tYour Version: %i", table_query.your_version);
  493. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Query:\n%s", queries.c_str());
  494. }
  495. if(!UpdatesAuto() && UpdatesAsk())
  496. {
  497. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest table version? ([Y]es, [N]o, [A]ll)");
  498. cin >> answer;
  499. if(toupper(answer) == 'A')
  500. UpdatesAuto(true);
  501. }
  502. if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
  503. {
  504. bool success = database.RunDatabaseQueries(&table_query, false);
  505. if(success)
  506. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Table: '%s' = SUCCESS!", table_query.tablename);
  507. else
  508. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Update Table: '%s' = FAILED!", table_query.tablename);
  509. }
  510. }
  511. int32 LoginServer::ProcessTableUpdates(uchar* data) {
  512. int32 ret = 0;
  513. /*LatestTableVersions table_versions;
  514. table_versions.DeSerialize(data);
  515. int32 total_tables = table_versions.GetTotalTables();
  516. int32 max_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  517. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Max Version: %i", max_version);
  518. vector<ServerPacket*> packet_requests;
  519. ServerPacket* outpack = 0;
  520. for(int32 i=0;i<total_tables;i++){
  521. float version = database.CheckTableVersions(table_versions.GetTable(i).name);
  522. if( (version < table_versions.GetTable(i).version) )
  523. {
  524. outpack = new ServerPacket(ServerOP_GetTableQuery, sizeof(TableVersion));
  525. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  526. strcpy(out_version->name, table_versions.GetTable(i).name);
  527. out_version->max_table_version = max_version;
  528. out_version->version = (int32)version;
  529. out_version->last = 0;
  530. packet_requests.push_back(outpack);
  531. ret++;
  532. }
  533. }
  534. if( ret )
  535. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates!");
  536. else
  537. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--No Table Updates, continuing...");
  538. int32 total_packets = packet_requests.size();
  539. for(int32 i=0;i<total_packets;i++)
  540. {
  541. outpack = packet_requests[i];
  542. if((i+1) == total_packets)
  543. {
  544. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  545. out_version->last = 1;
  546. }
  547. SendPacket(outpack);
  548. safe_delete(outpack);
  549. }*/
  550. return ret;
  551. }
  552. int32 LoginServer::ProcessDataUpdates(uchar* data) {
  553. int32 ret = 0;
  554. /*LatestTableVersions table_versions;
  555. table_versions.DeSerialize(data);
  556. int32 total_tables = table_versions.GetTotalTables();
  557. vector<ServerPacket*> packet_requests;
  558. ServerPacket* outpack = 0;
  559. for(int32 i=0;i<total_tables;i++){
  560. if(UpdatesAutoData()){
  561. float version = database.GetLatestDataTableVersion(table_versions.GetTable(i).name);
  562. if(version >= 0 && version < table_versions.GetTable(i).data_version )
  563. {
  564. ServerPacket* outpack = new ServerPacket(ServerOP_GetTableData, sizeof(TableVersion));
  565. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  566. strcpy(out_version->name, table_versions.GetTable(i).name);
  567. out_version->max_data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  568. out_version->data_version = (sint32)version;
  569. out_version->last = 0;
  570. memset(out_version->column_names, 0, 1000);
  571. string columns = database.GetColumnNames(out_version->name);
  572. if(columns.length() > 1)
  573. strcpy(out_version->column_names, columns.c_str());
  574. packet_requests.push_back(outpack);
  575. ret++;
  576. }
  577. }
  578. }
  579. if( ret )
  580. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Data Updates!");
  581. int32 total_packets = packet_requests.size();
  582. for(int32 i=0;i<total_packets;i++)
  583. {
  584. outpack = packet_requests[i];
  585. if((i+1) == total_packets)
  586. {
  587. TableVersion* out_version = (TableVersion*)outpack->pBuffer;
  588. out_version->last = 1;
  589. }
  590. SendPacket(outpack);
  591. safe_delete(outpack);
  592. }*/
  593. return ret;
  594. }
  595. bool LoginServer::CheckAndWait(Timer* timer){
  596. Timer::SetCurrentTime();
  597. if(timer->Check(false)){
  598. if(timer->GetDuration() == 5000){
  599. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Please wait...");
  600. timer->SetTimer(25000);
  601. timer->Start();
  602. }
  603. else
  604. return false;
  605. }
  606. Sleep(10);
  607. if(!loginserver.Process())
  608. tcpc->Disconnect();
  609. return Connected();
  610. }
  611. bool LoginServer::ConnectToUpdateServer(const char* iAddress, int16 iPort)
  612. {
  613. if(!try_auto_update)
  614. return true;
  615. char errbuf[TCPConnection_ErrorBufferSize];
  616. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  617. if (iAddress == 0)
  618. {
  619. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: address == 0");
  620. return false;
  621. }
  622. else
  623. {
  624. if ((UpdateServerIP = ResolveIP(iAddress, errbuf)) == 0)
  625. {
  626. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "LoginServer::ConnectToUpdateServer: Resolving IP address: '%s'", errbuf[0] == '\0' ? errbuf : " ");
  627. return false;
  628. }
  629. }
  630. if (iPort != 0)
  631. UpdateServerPort = iPort;
  632. if (UpdateServerIP == 0 || UpdateServerPort == 0)
  633. {
  634. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: Connect info incomplete, cannot connect.");
  635. return false;
  636. }
  637. if (tcpc->Connect(UpdateServerIP, UpdateServerPort, errbuf))
  638. {
  639. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Connected to Update Server: %s:%i", iAddress, UpdateServerPort);
  640. SendInfo();
  641. //wait for a max of 30 seconds to be verified by patcher
  642. //display a message at 5 seconds
  643. Timer waitTimer(5000);
  644. waitTimer.Start();
  645. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Checking for updates.");
  646. GetLatestTables();
  647. while(CheckAndWait(&waitTimer) && !update_server_verified);
  648. database.CheckVersionTable();
  649. if(!update_server_verified)
  650. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Error while getting updates!");
  651. if(update_server_verified)
  652. {
  653. update_server_completed = false;
  654. waitTimer.SetTimer(5000);
  655. waitTimer.Start();
  656. while(CheckAndWait(&waitTimer) && !data_waiting);
  657. if(data_waiting)
  658. {
  659. int32 requests = ProcessTableUpdates(data_waiting);
  660. if(requests > 0)
  661. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates.");
  662. uchar* data = 0;
  663. waitTimer.SetTimer(30000);
  664. waitTimer.Start();
  665. if(requests > 0)
  666. {
  667. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Table Updates.");
  668. database.DisableConstraints();
  669. deque<uchar*> alter_table_updates_waiting;
  670. while((CheckAndWait(&waitTimer) && !update_server_completed) || table_updates_waiting.size() > 0)
  671. {
  672. if(table_updates_waiting.size() > 0)
  673. {
  674. data = table_updates_waiting.front();
  675. table_updates_waiting.pop_front();
  676. ProcessTableUpdate(data);
  677. safe_delete(data);
  678. waitTimer.SetTimer(30000);
  679. waitTimer.Start();
  680. }
  681. }
  682. if(!CheckAndWait(&waitTimer))
  683. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
  684. else
  685. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Table Update complete.");
  686. database.EnableConstraints();
  687. }
  688. else
  689. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Schema...");
  690. requests = ProcessDataUpdates(data_waiting);
  691. if(requests > 0)
  692. {
  693. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Data Updates.");
  694. LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "** Data Updates are SLOW! Do not abort! **");
  695. database.DisableConstraints();
  696. deque<uchar*> alter_table_updates_waiting;
  697. int32 count = 0;
  698. while((CheckAndWait(&waitTimer) && count < requests) || data_updates_waiting.size() > 0)
  699. {
  700. if(data_updates_waiting.size() > 0)
  701. {
  702. data = data_updates_waiting.front();
  703. data_updates_waiting.pop_front();
  704. ProcessDataUpdate(data);
  705. count++;
  706. safe_delete_array(data);
  707. waitTimer.SetTimer(30000);
  708. waitTimer.Start();
  709. }
  710. }
  711. if(!CheckAndWait(&waitTimer))
  712. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
  713. else
  714. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Data Updates complete.");
  715. database.EnableConstraints();
  716. }
  717. else
  718. LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Data...");
  719. safe_delete_array(data_waiting);
  720. }
  721. try_auto_update = false;
  722. }
  723. tcpc->Disconnect();
  724. return update_server_verified;
  725. }
  726. else
  727. {
  728. LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: '%s'", errbuf[0] == '\0' ? errbuf : " ");
  729. return false;
  730. }
  731. }
  732. bool LoginServer::Connect(const char* iAddress, int16 iPort) {
  733. if(!pTryReconnect)
  734. return false;
  735. char errbuf[TCPConnection_ErrorBufferSize];
  736. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  737. if (iAddress == 0) {
  738. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: address == 0");
  739. return false;
  740. }
  741. else {
  742. if ((LoginServerIP = ResolveIP(iAddress, errbuf)) == 0) {
  743. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Resolving IP address: '%s'", errbuf);
  744. return false;
  745. }
  746. }
  747. if (iPort != 0)
  748. LoginServerPort = iPort;
  749. if (LoginServerIP == 0 || LoginServerPort == 0) {
  750. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Connect info incomplete, cannot connect");
  751. return false;
  752. }
  753. if (tcpc->Connect(LoginServerIP, LoginServerPort, errbuf)) {
  754. LogWrite(WORLD__INFO, 0, "World", "Connected to LoginServer: %s: %i", iAddress, LoginServerPort);
  755. SendInfo();
  756. SendStatus();
  757. return true;
  758. }
  759. else {
  760. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: '%s'", errbuf);
  761. return false;
  762. }
  763. }
  764. void LoginServer::GetLatestTables(){
  765. ServerPacket* pack = new ServerPacket(ServerOP_GetLatestTables, sizeof(GetLatestTables_Struct));
  766. GetLatestTables_Struct* data = (GetLatestTables_Struct*)pack->pBuffer;
  767. data->table_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  768. data->data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  769. SendPacket(pack);
  770. delete pack;
  771. }
  772. void LoginServer::SendInfo() {
  773. ServerPacket* pack = new ServerPacket;
  774. pack->opcode = ServerOP_LSInfo;
  775. pack->size = sizeof(ServerLSInfo_Struct);
  776. pack->pBuffer = new uchar[pack->size];
  777. memset(pack->pBuffer, 0, pack->size);
  778. ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
  779. strcpy(lsi->protocolversion, EQEMU_PROTOCOL_VERSION);
  780. strcpy(lsi->serverversion, CURRENT_VERSION);
  781. strcpy(lsi->name, net.GetWorldName());
  782. strcpy(lsi->account, net.GetWorldAccount());
  783. lsi->dbversion = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  784. #ifdef _DEBUG
  785. lsi->servertype = 4;
  786. #endif
  787. int8 tmppass[16];
  788. MD5::Generate((uchar*)net.GetWorldPassword(), strlen(net.GetWorldPassword()), tmppass);
  789. char* ptr = lsi->password;
  790. for(int i=0;i<15;i++){
  791. sprintf(ptr, "%02x", tmppass[i]);
  792. ptr += 2;
  793. }
  794. //memcpy(lsi->password, tmppass, 16);
  795. strcpy(lsi->address, net.GetWorldAddress());
  796. SendPacket(pack);
  797. delete pack;
  798. }
  799. void LoginServer::SendStatus() {
  800. statusupdate_timer->Start();
  801. ServerPacket* pack = new ServerPacket;
  802. pack->opcode = ServerOP_LSStatus;
  803. pack->size = sizeof(ServerLSStatus_Struct);
  804. pack->pBuffer = new uchar[pack->size];
  805. memset(pack->pBuffer, 0, pack->size);
  806. ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
  807. if (net.world_locked)
  808. lss->status = -2;
  809. else if(loginserver.maxPlayers > -1 && numclients >= loginserver.maxPlayers)
  810. lss->status = -3;
  811. else
  812. lss->status = 1;
  813. lss->num_zones = numzones;
  814. lss->num_players = numclients;
  815. lss->world_max_level = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt8();
  816. SendPacket(pack);
  817. delete pack;
  818. }
  819. void LoginServer::SendDeleteCharacter ( CharacterTimeStamp_Struct* cts ) {
  820. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  821. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  822. cdu->account_id = cts->account_id;
  823. cdu->char_id = cts->char_id;
  824. cdu->update_field = DELETE_UPDATE_FLAG;
  825. cdu->update_data = 1;
  826. SendPacket(outpack);
  827. safe_delete(outpack);
  828. }
  829. void LoginServer::SendFilterNameResponse ( int8 resp, int32 acct_id , int32 char_id ) {
  830. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, sizeof(WorldCharNameFilterResponse_Struct));
  831. WorldCharNameFilterResponse_Struct* wcfr = (WorldCharNameFilterResponse_Struct*)outpack->pBuffer;
  832. wcfr->response = resp;
  833. wcfr->account_id = acct_id;
  834. wcfr->char_id = char_id;
  835. SendPacket(outpack);
  836. safe_delete(outpack);
  837. }
  838. int32 LoginServer::DetermineCharacterLoginRequest ( UsertoWorldRequest_Struct* utwr ) {
  839. LogWrite(LOGIN__TRACE, 9, "Login", "Enter: %s", __FUNCTION__);
  840. ServerPacket* outpack = new ServerPacket;
  841. outpack->opcode = ServerOP_UsertoWorldResp;
  842. outpack->size = sizeof(UsertoWorldResponse_Struct);
  843. outpack->pBuffer = new uchar[outpack->size];
  844. memset(outpack->pBuffer, 0, outpack->size);
  845. UsertoWorldResponse_Struct* utwrs = (UsertoWorldResponse_Struct*) outpack->pBuffer;
  846. utwrs->lsaccountid = utwr->lsaccountid;
  847. utwrs->char_id = utwr->char_id;
  848. utwrs->ToID = utwr->FromID;
  849. int32 timestamp = Timer::GetUnixTimeStamp();
  850. utwrs->access_key = timestamp;
  851. // set default response to 0
  852. utwrs->response = 0;
  853. sint16 lowestStatus = database.GetLowestCharacterAdminStatus( utwr->lsaccountid );
  854. sint16 status = 0;
  855. if(lowestStatus == -2)
  856. status = -1;
  857. else
  858. status = database.GetCharacterAdminStatus ( utwr->lsaccountid , utwr->char_id );
  859. if(status < 100 && zone_list.ClientConnected(utwr->lsaccountid))
  860. status = -9;
  861. if(status < 0){
  862. switch(status){
  863. case -10:
  864. utwrs->response = PLAY_ERROR_CHAR_NOT_LOADED;
  865. break;
  866. case -9:
  867. utwrs->response = 0;//PLAY_ERROR_ACCOUNT_IN_USE;
  868. break;
  869. case -8:
  870. utwrs->response = PLAY_ERROR_LOADING_ERROR;
  871. break;
  872. case -1:
  873. utwrs->response = PLAY_ERROR_ACCOUNT_BANNED;
  874. break;
  875. default:
  876. utwrs->response = PLAY_ERROR_PROBLEM;
  877. }
  878. }
  879. else if(net.world_locked == true){
  880. LogWrite(WORLD__INFO, 0, "World", "Login Lock Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  881. // has high enough status, allow it
  882. if(status >= loginserver.minLockedStatus)
  883. utwrs->response = 1;
  884. }
  885. else if(loginserver.maxPlayers > -1 && ((sint16)client_list.Count()) >= loginserver.maxPlayers)
  886. {
  887. LogWrite(WORLD__INFO, 0, "World", "Login GameFull Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minGameFullStatus,status,utwr->char_id );
  888. // has high enough status, allow it
  889. if(status >= loginserver.minGameFullStatus)
  890. {
  891. utwrs->response = 1;
  892. }
  893. else
  894. utwrs->response = -3; // server full response is -3
  895. }
  896. else
  897. utwrs->response = 1;
  898. /*sint32 x = database.CommandRequirement("$MAXCLIENTS");
  899. if( (sint32)numplayers >= x && x != -1 && x != 255 && status < 80)
  900. utwrs->response = -3;
  901. if(status == -1)
  902. utwrs->response = -1;
  903. if(status == -2)
  904. utwrs->response = -2;
  905. */
  906. //printf("Response is %i for %i\n",utwrs->response,id);
  907. if((strcmp(net.GetWorldAddress(), utwr->ip_address)==0) && (strlen(net.GetInternalWorldAddress())>0))
  908. strcpy(utwrs->ip_address, net.GetInternalWorldAddress());
  909. else
  910. strcpy(utwrs->ip_address, net.GetWorldAddress());
  911. utwrs->port = net.GetWorldPort();
  912. utwrs->worldid = utwr->worldid;
  913. SendPacket(outpack);
  914. delete outpack;
  915. LogWrite(LOGIN__TRACE, 9, "Login", "Exit: %s with timestamp=%u", __FUNCTION__, timestamp);
  916. // depending on the response determined above, this could return 0 (for failure)
  917. return timestamp;
  918. }