LoginServer.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 <Ws2tcpip.h>
  32. #include <windows.h>
  33. #define strncasecmp _strnicmp
  34. #define strcasecmp _stricmp
  35. #else // Pyro: fix for linux
  36. #include <sys/socket.h>
  37. #ifdef FREEBSD //Timothy Whitman - January 7, 2003
  38. #include <sys/types.h>
  39. #endif
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <pthread.h>
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #include "../common/unix.h"
  46. #define SOCKET_ERROR -1
  47. #define INVALID_SOCKET -1
  48. extern int errno;
  49. #endif
  50. #include "../common/servertalk.h"
  51. #include "LoginServer.h"
  52. #include "../common/packet_dump.h"
  53. #include "net.h"
  54. #include "zoneserver.h"
  55. #include "WorldDatabase.h"
  56. #include "Variables.h"
  57. #include "World.h"
  58. #include "../common/ConfigReader.h"
  59. #include "Rules/Rules.h"
  60. extern sint32 numzones;
  61. extern sint32 numclients;
  62. extern NetConnection net;
  63. extern LoginServer loginserver;
  64. extern WorldDatabase database;
  65. extern ZoneAuth zone_auth;
  66. extern Variables variables;
  67. extern ZoneList zone_list;
  68. extern ClientList client_list;
  69. extern volatile bool RunLoops;
  70. volatile bool LoginLoopRunning = false;
  71. extern ConfigReader configReader;
  72. extern RuleManager rule_manager;
  73. bool AttemptingConnect = false;
  74. LoginServer::LoginServer(const char* iAddress, int16 iPort) {
  75. LoginServerIP = ResolveIP(iAddress);
  76. LoginServerPort = iPort;
  77. statusupdate_timer = new Timer(LoginServer_StatusUpdateInterval);
  78. tcpc = new TCPConnection(false);
  79. pTryReconnect = true;
  80. minLockedStatus = 100;
  81. maxPlayers = -1;
  82. minGameFullStatus = 100;
  83. last_checked_time = 0;
  84. zone_updates = 0;
  85. loginEquip_updates = 0;
  86. }
  87. LoginServer::~LoginServer() {
  88. delete statusupdate_timer;
  89. delete tcpc;
  90. }
  91. void LoginServer::SendImmediateEquipmentUpdatesForChar(int32 char_id) {
  92. LogWrite(WORLD__DEBUG, 5, "World", "Sending login equipment updates for char_id: %u", char_id);
  93. int16 count = 0;
  94. if(!loginEquip_updates)
  95. loginEquip_updates = database.GetEquipmentUpdates(char_id);
  96. if(loginEquip_updates && loginEquip_updates->size() > 0)
  97. {
  98. map<int32, LoginEquipmentUpdate> send_map;
  99. int32 size = 0;
  100. MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
  101. while(itr.Next())
  102. {
  103. send_map[itr->first] = itr->second;
  104. size += sizeof(EquipmentUpdate_Struct);
  105. loginEquip_updates->erase(itr->first);
  106. count++;
  107. }
  108. ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
  109. EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
  110. updates->total_updates = count;
  111. int32 pos = sizeof(EquipmentUpdateList_Struct);
  112. map<int32, LoginEquipmentUpdate>::iterator send_itr;
  113. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
  114. {
  115. EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
  116. update->id = send_itr->first;
  117. update->world_char_id = send_itr->second.world_char_id;
  118. update->equip_type = send_itr->second.equip_type;
  119. update->red = send_itr->second.red;
  120. update->green = send_itr->second.green;
  121. update->blue = send_itr->second.blue;
  122. update->highlight_red = send_itr->second.red;
  123. update->highlight_green = send_itr->second.green;
  124. update->highlight_blue = send_itr->second.blue;
  125. update->slot = send_itr->second.slot;
  126. pos += sizeof(EquipmentUpdate_Struct);
  127. }
  128. SendPacket(outpack);
  129. outpack->Deflate();
  130. safe_delete(outpack);
  131. }
  132. if(loginEquip_updates && count)
  133. loginEquip_updates->clear();
  134. if(loginEquip_updates && loginEquip_updates->size() == 0)
  135. {
  136. database.UpdateLoginEquipment();
  137. safe_delete(loginEquip_updates);
  138. }
  139. }
  140. bool LoginServer::Process() {
  141. if(last_checked_time > Timer::GetCurrentTime2())
  142. return true;
  143. last_checked_time = Timer::GetCurrentTime2() + 50;
  144. bool ret = true;
  145. if (statusupdate_timer->Check()) {
  146. this->SendStatus();
  147. }
  148. /************ Get all packets from packet manager out queue and process them ************/
  149. ServerPacket *pack = 0;
  150. while((pack = tcpc->PopPacket()))
  151. {
  152. switch(pack->opcode)
  153. {
  154. case ServerOP_LSFatalError:
  155. {
  156. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LSFatalError", pack->opcode, pack->opcode);
  157. LogWrite(WORLD__ERROR, 0, "World", "Login Server returned a fatal error: %s\n", pack->pBuffer);
  158. tcpc->Disconnect();
  159. ret = false;
  160. net.ReadLoginINI();
  161. break;
  162. }
  163. case ServerOP_CharTimeStamp:
  164. {
  165. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharTimeStamp", pack->opcode, pack->opcode);
  166. if(pack->size != sizeof(CharacterTimeStamp_Struct))
  167. break;
  168. CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) pack->pBuffer;
  169. // determine if the character exists and retrieve its latest timestamp from the world server
  170. bool char_exist = false;
  171. //int32 character_timestamp = database.GetCharacterTimeStamp(cts->char_id,cts->account_id,&char_exist);
  172. if(!char_exist)
  173. {
  174. //Character doesn't exist, get rid of it
  175. SendDeleteCharacter ( cts );
  176. break;
  177. }
  178. break;
  179. }
  180. // Push Character Select "item appearances" to login_equipment table
  181. case ServerOP_LoginEquipment:{
  182. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LoginEquipment", pack->opcode, pack->opcode);
  183. LogWrite(MISC__TODO, 0, "TODO", "Implement map<character id <map<slot id, updatestruct> > method to update Login.\n%s, %s, %i", __FILE__, __FUNCTION__, __LINE__);
  184. if( pack->size == sizeof(EquipmentUpdateRequest_Struct) )
  185. {
  186. int16 max = ((EquipmentUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
  187. int16 count = 0;
  188. if(!loginEquip_updates)
  189. loginEquip_updates = database.GetEquipmentUpdates();
  190. if(loginEquip_updates && loginEquip_updates->size() > 0)
  191. {
  192. map<int32, LoginEquipmentUpdate> send_map;
  193. int32 size = 0;
  194. MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
  195. while(itr.Next() && count < max)
  196. {
  197. send_map[itr->first] = itr->second;
  198. size += sizeof(EquipmentUpdate_Struct);
  199. loginEquip_updates->erase(itr->first);
  200. count++;
  201. }
  202. ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
  203. EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
  204. updates->total_updates = count;
  205. int32 pos = sizeof(EquipmentUpdateList_Struct);
  206. map<int32, LoginEquipmentUpdate>::iterator send_itr;
  207. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
  208. {
  209. EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
  210. update->id = send_itr->first;
  211. update->world_char_id = send_itr->second.world_char_id;
  212. update->equip_type = send_itr->second.equip_type;
  213. update->red = send_itr->second.red;
  214. update->green = send_itr->second.green;
  215. update->blue = send_itr->second.blue;
  216. update->highlight_red = send_itr->second.red;
  217. update->highlight_green = send_itr->second.green;
  218. update->highlight_blue = send_itr->second.blue;
  219. update->slot = send_itr->second.slot;
  220. pos += sizeof(EquipmentUpdate_Struct);
  221. }
  222. SendPacket(outpack);
  223. outpack->Deflate();
  224. safe_delete(outpack);
  225. }
  226. if(loginEquip_updates && count < max)
  227. loginEquip_updates->clear();
  228. if(loginEquip_updates && loginEquip_updates->size() == 0)
  229. {
  230. database.UpdateLoginEquipment();
  231. safe_delete(loginEquip_updates);
  232. }
  233. }
  234. break;
  235. }
  236. case ServerOP_ZoneUpdates:{
  237. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ZoneUpdates", pack->opcode, pack->opcode);
  238. if(pack->size == sizeof(ZoneUpdateRequest_Struct)){
  239. int16 max = ((ZoneUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
  240. int16 count = 0;
  241. if(!zone_updates)
  242. zone_updates = database.GetZoneUpdates();
  243. if(zone_updates && zone_updates->size() > 0){
  244. map<int32, LoginZoneUpdate> send_map;
  245. int32 size = 0;
  246. MutexMap<int32, LoginZoneUpdate>::iterator itr = zone_updates->begin();
  247. while(itr.Next() && count < max){
  248. send_map[itr->first] = itr->second;
  249. size += sizeof(ZoneUpdate_Struct) + itr->second.name.length() + itr->second.description.length();
  250. zone_updates->erase(itr->first);
  251. count++;
  252. }
  253. ServerPacket* outpack = new ServerPacket(ServerOP_ZoneUpdates, size + sizeof(ZoneUpdateList_Struct)+5);
  254. ZoneUpdateList_Struct* updates = (ZoneUpdateList_Struct*)outpack->pBuffer;
  255. updates->total_updates = count;
  256. int32 pos = sizeof(ZoneUpdateList_Struct);
  257. map<int32, LoginZoneUpdate>::iterator send_itr;
  258. for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++){
  259. ZoneUpdate_Struct* update = (ZoneUpdate_Struct*)(outpack->pBuffer + pos);
  260. update->zone_id = send_itr->first;
  261. update->zone_name_length = send_itr->second.name.length();
  262. update->zone_desc_length = send_itr->second.description.length();
  263. strcpy(update->data, send_itr->second.name.c_str());
  264. strcpy(update->data + send_itr->second.name.length(), send_itr->second.description.c_str());
  265. pos += sizeof(ZoneUpdate_Struct) + send_itr->second.name.length() + send_itr->second.description.length();
  266. }
  267. SendPacket(outpack);
  268. outpack->Deflate();
  269. safe_delete(outpack);
  270. }
  271. if(zone_updates && count < max)
  272. zone_updates->clear();
  273. if(zone_updates && zone_updates->size() == 0){
  274. database.UpdateLoginZones();
  275. safe_delete(zone_updates);
  276. }
  277. }
  278. break;
  279. }
  280. case ServerOP_CharacterCreate:{
  281. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharacterCreate", pack->opcode, pack->opcode);
  282. int16 version = 1;
  283. if(pack->pBuffer[0] > 0)
  284. memcpy(&version, pack->pBuffer, sizeof(int16));
  285. //DumpPacket(pack->pBuffer,pack->size);
  286. PacketStruct* packet = configReader.getStruct("CreateCharacter", version);
  287. int8 resp = 0;
  288. int32 acct_id = 0;
  289. int32 char_id = 0;
  290. if(packet && packet->LoadPacketData(pack->pBuffer+sizeof(int16),pack->size - sizeof(int16))){
  291. EQ2_16BitString name = packet->getType_EQ2_16BitString_ByName("name");
  292. resp = database.CheckNameFilter(name.data.c_str());
  293. acct_id = packet->getType_int32_ByName("account_id");
  294. LogWrite(WORLD__DEBUG, 0, "World", "Response: %i", (int)resp);
  295. sint16 lowestStatus = database.GetLowestCharacterAdminStatus(acct_id);
  296. if(lowestStatus == -2)
  297. resp = UNKNOWNERROR_REPLY2;
  298. else if(resp == CREATESUCCESS_REPLY)
  299. char_id = database.SaveCharacter(packet, acct_id);
  300. }
  301. else{
  302. LogWrite(WORLD__ERROR, 0, "World", "Invalid creation request!");
  303. resp = UNKNOWNERROR_REPLY;
  304. }
  305. // send name filter response data back to the login server
  306. SendFilterNameResponse ( resp , acct_id , char_id );
  307. safe_delete(packet);
  308. break;
  309. }
  310. case ServerOP_BasicCharUpdate: {
  311. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_BasicCharUpdate", pack->opcode, pack->opcode);
  312. if(pack->size != sizeof(CharDataUpdate_Struct))
  313. break;
  314. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*) pack->pBuffer;
  315. switch(cdu->update_field)
  316. {
  317. case DELETE_UPDATE_FLAG:
  318. {
  319. LogWrite(WORLD__DEBUG, 0, "World", "Delete character request: %i %i",cdu->account_id,cdu->char_id );
  320. database.DeleteCharacter(cdu->account_id,cdu->char_id);
  321. break;
  322. }
  323. }
  324. break;
  325. }
  326. case ServerOP_UsertoWorldReq:{
  327. LogWrite(OPCODE__DEBUG, 0, "Opcode", "Opcode 0x%X (%i): ServerOP_UsertoWorldReq", pack->opcode, pack->opcode);
  328. UsertoWorldRequest_Struct* utwr = (UsertoWorldRequest_Struct*) pack->pBuffer;
  329. /*int32 id = database.GetAccountIDFromLSID(utwr->lsaccountid);
  330. sint16 status = database.CheckStatus(id);
  331. */
  332. int32 access_key = 0;
  333. // if it is a accepted login, we add the zone auth request
  334. access_key = DetermineCharacterLoginRequest ( utwr );
  335. if ( access_key != 0 )
  336. {
  337. zone_auth.PurgeInactiveAuth();
  338. char* characterName = database.GetCharacterName( utwr->char_id );
  339. if(characterName != 0){
  340. ZoneAuthRequest* zar = new ZoneAuthRequest(utwr->lsaccountid,characterName,access_key);
  341. zar->setFirstLogin ( true );
  342. zone_auth.AddAuth(zar);
  343. safe_delete_array(characterName);
  344. }
  345. }
  346. break;
  347. }
  348. case ServerOP_ResetDatabase:{
  349. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ResetDatabase", pack->opcode, pack->opcode);
  350. database.ResetDatabase();
  351. break;
  352. }
  353. default:
  354. {
  355. LogWrite(WORLD__ERROR, 0, "World", "Unhandled opcode: %i", pack->opcode);
  356. DumpPacket(pack);
  357. }
  358. }
  359. safe_delete(pack);
  360. // break out if ret is now false
  361. if (!ret)
  362. break;
  363. }
  364. return ret;
  365. }
  366. // this should always be called in a new thread
  367. #ifdef WIN32
  368. void AutoInitLoginServer(void *tmp) {
  369. #else
  370. void *AutoInitLoginServer(void *tmp) {
  371. #endif
  372. if (loginserver.GetState() == TCPS_Ready) {
  373. InitLoginServer();
  374. }
  375. #ifndef WIN32
  376. return 0;
  377. #endif
  378. }
  379. bool InitLoginServer() {
  380. if (loginserver.GetState() != TCPS_Ready) {
  381. LogWrite(WORLD__ERROR, 0, "World", "InitLoginServer() while already attempting connect.");
  382. return false;
  383. }
  384. if (!net.LoginServerInfo) {
  385. LogWrite(WORLD__ERROR, 0, "World", "Login server info not loaded.");
  386. return false;
  387. }
  388. AttemptingConnect = true;
  389. int16 port;
  390. char* address = net.GetLoginInfo(&port);
  391. loginserver.Connect(address, port);
  392. return true;
  393. }
  394. void LoginServer::InitLoginServerVariables()
  395. {
  396. minLockedStatus = rule_manager.GetGlobalRule(R_World, ServerLockedOverrideStatus)->GetSInt16();
  397. maxPlayers = rule_manager.GetGlobalRule(R_World, MaxPlayers)->GetSInt16();
  398. minGameFullStatus = rule_manager.GetGlobalRule(R_World, MaxPlayersOverrideStatus)->GetSInt16();
  399. }
  400. bool LoginServer::Connect(const char* iAddress, int16 iPort) {
  401. if(!pTryReconnect)
  402. return false;
  403. char errbuf[TCPConnection_ErrorBufferSize];
  404. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  405. if (iAddress == 0) {
  406. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: address == 0");
  407. return false;
  408. }
  409. else {
  410. if ((LoginServerIP = ResolveIP(iAddress, errbuf)) == 0) {
  411. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Resolving IP address: '%s'", errbuf);
  412. return false;
  413. }
  414. }
  415. if (iPort != 0)
  416. LoginServerPort = iPort;
  417. if (LoginServerIP == 0 || LoginServerPort == 0) {
  418. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Connect info incomplete, cannot connect");
  419. return false;
  420. }
  421. if (tcpc->Connect(LoginServerIP, LoginServerPort, errbuf)) {
  422. LogWrite(WORLD__INFO, 0, "World", "Connected to LoginServer: %s: %i", iAddress, LoginServerPort);
  423. SendInfo();
  424. SendStatus();
  425. return true;
  426. }
  427. else {
  428. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: '%s'", errbuf);
  429. return false;
  430. }
  431. }
  432. void LoginServer::GetLatestTables(){
  433. ServerPacket* pack = new ServerPacket(ServerOP_GetLatestTables, sizeof(GetLatestTables_Struct));
  434. GetLatestTables_Struct* data = (GetLatestTables_Struct*)pack->pBuffer;
  435. data->table_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  436. data->data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  437. SendPacket(pack);
  438. delete pack;
  439. }
  440. void LoginServer::SendInfo() {
  441. ServerPacket* pack = new ServerPacket;
  442. pack->opcode = ServerOP_LSInfo;
  443. pack->size = sizeof(ServerLSInfo_Struct);
  444. pack->pBuffer = new uchar[pack->size];
  445. memset(pack->pBuffer, 0, pack->size);
  446. ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
  447. strcpy(lsi->protocolversion, EQEMU_PROTOCOL_VERSION);
  448. strcpy(lsi->serverversion, CURRENT_VERSION);
  449. strcpy(lsi->name, net.GetWorldName());
  450. strcpy(lsi->account, net.GetWorldAccount());
  451. lsi->dbversion = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  452. #ifdef _DEBUG
  453. lsi->servertype = 4;
  454. #endif
  455. string passwdSha512 = sha512(net.GetWorldPassword());
  456. memcpy(lsi->password, (char*)passwdSha512.c_str(), passwdSha512.length());
  457. strcpy(lsi->address, net.GetWorldAddress());
  458. SendPacket(pack);
  459. delete pack;
  460. }
  461. void LoginServer::SendStatus() {
  462. statusupdate_timer->Start();
  463. ServerPacket* pack = new ServerPacket;
  464. pack->opcode = ServerOP_LSStatus;
  465. pack->size = sizeof(ServerLSStatus_Struct);
  466. pack->pBuffer = new uchar[pack->size];
  467. memset(pack->pBuffer, 0, pack->size);
  468. ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
  469. if (net.world_locked)
  470. lss->status = -2;
  471. else if(loginserver.maxPlayers > -1 && numclients >= loginserver.maxPlayers)
  472. lss->status = -3;
  473. else
  474. lss->status = 1;
  475. lss->num_zones = numzones;
  476. lss->num_players = numclients;
  477. lss->world_max_level = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt8();
  478. SendPacket(pack);
  479. delete pack;
  480. }
  481. void LoginServer::SendDeleteCharacter ( CharacterTimeStamp_Struct* cts ) {
  482. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  483. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  484. cdu->account_id = cts->account_id;
  485. cdu->char_id = cts->char_id;
  486. cdu->update_field = DELETE_UPDATE_FLAG;
  487. cdu->update_data = 1;
  488. SendPacket(outpack);
  489. safe_delete(outpack);
  490. }
  491. void LoginServer::SendFilterNameResponse ( int8 resp, int32 acct_id , int32 char_id ) {
  492. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, sizeof(WorldCharNameFilterResponse_Struct));
  493. WorldCharNameFilterResponse_Struct* wcfr = (WorldCharNameFilterResponse_Struct*)outpack->pBuffer;
  494. wcfr->response = resp;
  495. wcfr->account_id = acct_id;
  496. wcfr->char_id = char_id;
  497. SendPacket(outpack);
  498. safe_delete(outpack);
  499. }
  500. int32 LoginServer::DetermineCharacterLoginRequest ( UsertoWorldRequest_Struct* utwr ) {
  501. LogWrite(LOGIN__TRACE, 9, "Login", "Enter: %s", __FUNCTION__);
  502. ServerPacket* outpack = new ServerPacket;
  503. outpack->opcode = ServerOP_UsertoWorldResp;
  504. outpack->size = sizeof(UsertoWorldResponse_Struct);
  505. outpack->pBuffer = new uchar[outpack->size];
  506. memset(outpack->pBuffer, 0, outpack->size);
  507. UsertoWorldResponse_Struct* utwrs = (UsertoWorldResponse_Struct*) outpack->pBuffer;
  508. utwrs->lsaccountid = utwr->lsaccountid;
  509. utwrs->char_id = utwr->char_id;
  510. utwrs->ToID = utwr->FromID;
  511. int32 timestamp = Timer::GetUnixTimeStamp();
  512. utwrs->access_key = timestamp;
  513. // set default response to 0
  514. utwrs->response = 0;
  515. sint16 lowestStatus = database.GetLowestCharacterAdminStatus( utwr->lsaccountid );
  516. sint16 status = 0;
  517. if(lowestStatus == -2)
  518. status = -1;
  519. else
  520. status = database.GetCharacterAdminStatus ( utwr->lsaccountid , utwr->char_id );
  521. if(status < 100 && zone_list.ClientConnected(utwr->lsaccountid))
  522. status = -9;
  523. if(status < 0){
  524. LogWrite(WORLD__ERROR, 0, "World", "Login Rejected based on PLAY_ERROR (UserStatus) (MinStatus: %i), UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  525. switch(status){
  526. case -10:
  527. utwrs->response = PLAY_ERROR_CHAR_NOT_LOADED;
  528. break;
  529. case -9:
  530. utwrs->response = 0;//PLAY_ERROR_ACCOUNT_IN_USE;
  531. break;
  532. case -8:
  533. utwrs->response = PLAY_ERROR_LOADING_ERROR;
  534. break;
  535. case -1:
  536. utwrs->response = PLAY_ERROR_ACCOUNT_BANNED;
  537. break;
  538. default:
  539. utwrs->response = PLAY_ERROR_PROBLEM;
  540. }
  541. }
  542. else if(net.world_locked == true){
  543. LogWrite(WORLD__INFO, 0, "World", "Login Lock Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  544. // has high enough status, allow it
  545. if(status >= loginserver.minLockedStatus)
  546. utwrs->response = 1;
  547. }
  548. else if(loginserver.maxPlayers > -1 && ((sint16)client_list.Count()) >= loginserver.maxPlayers)
  549. {
  550. LogWrite(WORLD__INFO, 0, "World", "Login GameFull Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minGameFullStatus,status,utwr->char_id );
  551. // has high enough status, allow it
  552. if(status >= loginserver.minGameFullStatus)
  553. {
  554. utwrs->response = 1;
  555. }
  556. else
  557. utwrs->response = -3; // server full response is -3
  558. }
  559. else
  560. utwrs->response = 1;
  561. /*sint32 x = database.CommandRequirement("$MAXCLIENTS");
  562. if( (sint32)numplayers >= x && x != -1 && x != 255 && status < 80)
  563. utwrs->response = -3;
  564. if(status == -1)
  565. utwrs->response = -1;
  566. if(status == -2)
  567. utwrs->response = -2;
  568. */
  569. //printf("Response is %i for %i\n",utwrs->response,id);struct sockaddr_in sa;
  570. int32 ipv4addr = 0;
  571. int result = 0;
  572. #ifdef WIN32
  573. struct sockaddr_in myaddr;
  574. ZeroMemory(&myaddr, sizeof(myaddr));
  575. result = InetPton(AF_INET, utwr->ip_address, &(myaddr.sin_addr));
  576. if(result)
  577. ipv4addr = ntohl(myaddr.sin_addr.s_addr);
  578. #else
  579. result = inet_pton(AF_INET, utwr->ip_address, &ipv4addr);
  580. if(result)
  581. ipv4addr = ntohl(ipv4addr);
  582. #endif
  583. if (((result > 0 && IsPrivateAddress(ipv4addr)) || (strcmp(net.GetWorldAddress(), utwr->ip_address) == 0)) && (strlen(net.GetInternalWorldAddress()) > 0))
  584. strcpy(utwrs->ip_address, net.GetInternalWorldAddress());
  585. else
  586. strcpy(utwrs->ip_address, net.GetWorldAddress());
  587. LogWrite(CCLIENT__INFO, 0, "World", "New client login attempt from %s, providing %s as the world server address.",utwr->ip_address, utwrs->ip_address );
  588. utwrs->port = net.GetWorldPort();
  589. utwrs->worldid = utwr->worldid;
  590. SendPacket(outpack);
  591. delete outpack;
  592. LogWrite(LOGIN__TRACE, 9, "Login", "Exit: %s with timestamp=%u", __FUNCTION__, timestamp);
  593. // depending on the response determined above, this could return 0 (for failure)
  594. return timestamp;
  595. }