9
3

LoginServer.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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), version <= 561 ? false : true)){
  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. LogWrite(WORLD__INFO, 0, "World", "InitLoginServer() attempt connect to %s on port %u.", address, port);
  392. loginserver.Connect(address, port);
  393. return true;
  394. }
  395. void LoginServer::InitLoginServerVariables()
  396. {
  397. minLockedStatus = rule_manager.GetGlobalRule(R_World, ServerLockedOverrideStatus)->GetSInt16();
  398. maxPlayers = rule_manager.GetGlobalRule(R_World, MaxPlayers)->GetSInt16();
  399. minGameFullStatus = rule_manager.GetGlobalRule(R_World, MaxPlayersOverrideStatus)->GetSInt16();
  400. }
  401. bool LoginServer::Connect(const char* iAddress, int16 iPort) {
  402. if(!pTryReconnect)
  403. return false;
  404. char errbuf[TCPConnection_ErrorBufferSize];
  405. memset(errbuf, 0, TCPConnection_ErrorBufferSize);
  406. if (iAddress == 0) {
  407. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: address == 0");
  408. return false;
  409. }
  410. else {
  411. if ((LoginServerIP = ResolveIP(iAddress, errbuf)) == 0) {
  412. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Resolving IP address: '%s'", errbuf);
  413. return false;
  414. }
  415. }
  416. if (iPort != 0)
  417. LoginServerPort = iPort;
  418. if (LoginServerIP == 0 || LoginServerPort == 0) {
  419. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Connect info incomplete, cannot connect");
  420. return false;
  421. }
  422. if (tcpc->Connect(LoginServerIP, LoginServerPort, errbuf)) {
  423. LogWrite(WORLD__INFO, 0, "World", "Connected to LoginServer: %s: %i", iAddress, LoginServerPort);
  424. SendInfo();
  425. SendStatus();
  426. return true;
  427. }
  428. else {
  429. LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: '%s'", errbuf);
  430. return false;
  431. }
  432. }
  433. void LoginServer::GetLatestTables(){
  434. ServerPacket* pack = new ServerPacket(ServerOP_GetLatestTables, sizeof(GetLatestTables_Struct));
  435. GetLatestTables_Struct* data = (GetLatestTables_Struct*)pack->pBuffer;
  436. data->table_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  437. data->data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  438. SendPacket(pack);
  439. delete pack;
  440. }
  441. void LoginServer::SendInfo() {
  442. ServerPacket* pack = new ServerPacket;
  443. pack->opcode = ServerOP_LSInfo;
  444. pack->size = sizeof(ServerLSInfo_Struct);
  445. pack->pBuffer = new uchar[pack->size];
  446. memset(pack->pBuffer, 0, pack->size);
  447. ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
  448. strcpy(lsi->protocolversion, EQEMU_PROTOCOL_VERSION);
  449. strcpy(lsi->serverversion, CURRENT_VERSION);
  450. strcpy(lsi->name, net.GetWorldName());
  451. strcpy(lsi->account, net.GetWorldAccount());
  452. lsi->dbversion = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
  453. #ifdef _DEBUG
  454. lsi->servertype = 4;
  455. #endif
  456. string passwdSha512 = sha512(net.GetWorldPassword());
  457. memcpy(lsi->password, (char*)passwdSha512.c_str(), passwdSha512.length());
  458. strcpy(lsi->address, net.GetWorldAddress());
  459. SendPacket(pack);
  460. delete pack;
  461. }
  462. void LoginServer::SendStatus() {
  463. statusupdate_timer->Start();
  464. ServerPacket* pack = new ServerPacket;
  465. pack->opcode = ServerOP_LSStatus;
  466. pack->size = sizeof(ServerLSStatus_Struct);
  467. pack->pBuffer = new uchar[pack->size];
  468. memset(pack->pBuffer, 0, pack->size);
  469. ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
  470. if (net.world_locked)
  471. lss->status = -2;
  472. else if(loginserver.maxPlayers > -1 && numclients >= loginserver.maxPlayers)
  473. lss->status = -3;
  474. else
  475. lss->status = 1;
  476. lss->num_zones = numzones;
  477. lss->num_players = numclients;
  478. lss->world_max_level = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt8();
  479. SendPacket(pack);
  480. delete pack;
  481. }
  482. void LoginServer::SendDeleteCharacter ( CharacterTimeStamp_Struct* cts ) {
  483. ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
  484. CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
  485. cdu->account_id = cts->account_id;
  486. cdu->char_id = cts->char_id;
  487. cdu->update_field = DELETE_UPDATE_FLAG;
  488. cdu->update_data = 1;
  489. SendPacket(outpack);
  490. safe_delete(outpack);
  491. }
  492. void LoginServer::SendFilterNameResponse ( int8 resp, int32 acct_id , int32 char_id ) {
  493. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, sizeof(WorldCharNameFilterResponse_Struct));
  494. WorldCharNameFilterResponse_Struct* wcfr = (WorldCharNameFilterResponse_Struct*)outpack->pBuffer;
  495. wcfr->response = resp;
  496. wcfr->account_id = acct_id;
  497. wcfr->char_id = char_id;
  498. SendPacket(outpack);
  499. safe_delete(outpack);
  500. }
  501. int32 LoginServer::DetermineCharacterLoginRequest ( UsertoWorldRequest_Struct* utwr ) {
  502. LogWrite(LOGIN__TRACE, 9, "Login", "Enter: %s", __FUNCTION__);
  503. ServerPacket* outpack = new ServerPacket;
  504. outpack->opcode = ServerOP_UsertoWorldResp;
  505. outpack->size = sizeof(UsertoWorldResponse_Struct);
  506. outpack->pBuffer = new uchar[outpack->size];
  507. memset(outpack->pBuffer, 0, outpack->size);
  508. UsertoWorldResponse_Struct* utwrs = (UsertoWorldResponse_Struct*) outpack->pBuffer;
  509. utwrs->lsaccountid = utwr->lsaccountid;
  510. utwrs->char_id = utwr->char_id;
  511. utwrs->ToID = utwr->FromID;
  512. int32 timestamp = Timer::GetUnixTimeStamp();
  513. utwrs->access_key = timestamp;
  514. // set default response to 0
  515. utwrs->response = 0;
  516. sint16 lowestStatus = database.GetLowestCharacterAdminStatus( utwr->lsaccountid );
  517. sint16 status = 0;
  518. if(lowestStatus == -2)
  519. status = -1;
  520. else
  521. status = database.GetCharacterAdminStatus ( utwr->lsaccountid , utwr->char_id );
  522. if(status < 100 && zone_list.ClientConnected(utwr->lsaccountid))
  523. status = -9;
  524. if(status < 0){
  525. LogWrite(WORLD__ERROR, 0, "World", "Login Rejected based on PLAY_ERROR (UserStatus) (MinStatus: %i), UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  526. switch(status){
  527. case -10:
  528. utwrs->response = PLAY_ERROR_CHAR_NOT_LOADED;
  529. break;
  530. case -9:
  531. utwrs->response = 0;//PLAY_ERROR_ACCOUNT_IN_USE;
  532. break;
  533. case -8:
  534. utwrs->response = PLAY_ERROR_LOADING_ERROR;
  535. break;
  536. case -1:
  537. utwrs->response = PLAY_ERROR_ACCOUNT_BANNED;
  538. break;
  539. default:
  540. utwrs->response = PLAY_ERROR_PROBLEM;
  541. }
  542. }
  543. else if(net.world_locked == true){
  544. LogWrite(WORLD__INFO, 0, "World", "Login Lock Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
  545. // has high enough status, allow it
  546. if(status >= loginserver.minLockedStatus)
  547. utwrs->response = 1;
  548. }
  549. else if(loginserver.maxPlayers > -1 && ((sint16)client_list.Count()) >= loginserver.maxPlayers)
  550. {
  551. LogWrite(WORLD__INFO, 0, "World", "Login GameFull Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minGameFullStatus,status,utwr->char_id );
  552. // has high enough status, allow it
  553. if(status >= loginserver.minGameFullStatus)
  554. {
  555. utwrs->response = 1;
  556. }
  557. else
  558. utwrs->response = -3; // server full response is -3
  559. }
  560. else
  561. utwrs->response = 1;
  562. /*sint32 x = database.CommandRequirement("$MAXCLIENTS");
  563. if( (sint32)numplayers >= x && x != -1 && x != 255 && status < 80)
  564. utwrs->response = -3;
  565. if(status == -1)
  566. utwrs->response = -1;
  567. if(status == -2)
  568. utwrs->response = -2;
  569. */
  570. //printf("Response is %i for %i\n",utwrs->response,id);struct sockaddr_in sa;
  571. int32 ipv4addr = 0;
  572. int result = 0;
  573. #ifdef WIN32
  574. struct sockaddr_in myaddr;
  575. ZeroMemory(&myaddr, sizeof(myaddr));
  576. result = InetPton(AF_INET, utwr->ip_address, &(myaddr.sin_addr));
  577. if(result)
  578. ipv4addr = ntohl(myaddr.sin_addr.s_addr);
  579. #else
  580. result = inet_pton(AF_INET, utwr->ip_address, &ipv4addr);
  581. if(result)
  582. ipv4addr = ntohl(ipv4addr);
  583. #endif
  584. if (((result > 0 && IsPrivateAddress(ipv4addr)) || (strcmp(net.GetWorldAddress(), utwr->ip_address) == 0)) && (strlen(net.GetInternalWorldAddress()) > 0))
  585. strcpy(utwrs->ip_address, net.GetInternalWorldAddress());
  586. else
  587. strcpy(utwrs->ip_address, net.GetWorldAddress());
  588. LogWrite(CCLIENT__INFO, 0, "World", "New client login attempt from %s, providing %s as the world server address.",utwr->ip_address, utwrs->ip_address );
  589. utwrs->port = net.GetWorldPort();
  590. utwrs->worldid = utwr->worldid;
  591. SendPacket(outpack);
  592. delete outpack;
  593. LogWrite(LOGIN__TRACE, 9, "Login", "Exit: %s with timestamp=%u", __FUNCTION__, timestamp);
  594. // depending on the response determined above, this could return 0 (for failure)
  595. return timestamp;
  596. }