client.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. */
  6. #include "../common/debug.h"
  7. #ifdef WIN32
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. #include <winsock.h>
  11. #include <process.h>
  12. #else
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <unistd.h>
  17. #endif
  18. #include <string.h>
  19. #include <iomanip>
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include "net.h"
  23. #include "client.h"
  24. #include "../common/EQStream.h"
  25. #include "../common/packet_dump.h"
  26. #include "../common/packet_functions.h"
  27. #include "../common/emu_opcodes.h"
  28. #include "../common/MiscFunctions.h"
  29. #include "LWorld.h"
  30. #include "LoginDatabase.h"
  31. #include "../common/ConfigReader.h"
  32. #include "../common/Log.h"
  33. extern NetConnection net;
  34. extern LWorldList world_list;
  35. extern ClientList client_list;
  36. extern LoginDatabase database;
  37. extern map<int16,OpcodeManager*>EQOpcodeManager;
  38. extern ConfigReader configReader;
  39. using namespace std;
  40. Client::Client(EQStream* ieqnc) {
  41. eqnc = ieqnc;
  42. ip = eqnc->GetrIP();
  43. port = ntohs(eqnc->GetrPort());
  44. account_id = 0;
  45. lsadmin = 0;
  46. worldadmin = 0;
  47. lsstatus = 0;
  48. version = 0;
  49. kicked = false;
  50. verified = false;
  51. memset(bannedreason, 0, sizeof(bannedreason));
  52. //worldresponse_timer = new Timer(10000);
  53. //worldresponse_timer->Disable();
  54. memset(key,0,10);
  55. LoginMode = None;
  56. num_updates = 0;
  57. updatetimer = new Timer(500);
  58. updatelisttimer = new Timer(10000);
  59. //keepalive = new Timer(5000);
  60. //logintimer = new Timer(500); // Give time for the servers to send updates
  61. //keepalive->Start();
  62. //updatetimer->Start();
  63. //logintimer->Disable();
  64. disconnectTimer = 0;
  65. memset(ClientSession,0,25);
  66. request_num = 0;
  67. login_account = 0;
  68. createRequest = 0;
  69. playWaitTimer = NULL;
  70. start = false;
  71. update_position = 0;
  72. update_packets = 0;
  73. needs_world_list = true;
  74. }
  75. Client::~Client() {
  76. //safe_delete(worldresponse_timer);
  77. //safe_delete(logintimer);
  78. safe_delete(login_account);
  79. eqnc->Close();
  80. safe_delete(playWaitTimer);
  81. safe_delete(createRequest);
  82. safe_delete(disconnectTimer);
  83. safe_delete(updatetimer);
  84. }
  85. bool Client::Process() {
  86. if(!start && !eqnc->CheckActive()){
  87. if(!playWaitTimer)
  88. playWaitTimer = new Timer(5000);
  89. else if(playWaitTimer->Check()){
  90. safe_delete(playWaitTimer);
  91. return false;
  92. }
  93. return true;
  94. }
  95. else if(!start){
  96. safe_delete(playWaitTimer);
  97. start = true;
  98. }
  99. if (disconnectTimer && disconnectTimer->Check())
  100. {
  101. safe_delete(disconnectTimer);
  102. getConnection()->SendDisconnect();
  103. }
  104. if (!kicked) {
  105. /************ Get all packets from packet manager out queue and process them ************/
  106. EQApplicationPacket *app = 0;
  107. /*if(logintimer && logintimer->Check())
  108. {
  109. database.LoadCharacters(GetLoginAccount());
  110. SendLoginAccepted();
  111. logintimer->Disable();
  112. }*/
  113. /*if(worldresponse_timer && worldresponse_timer->Check())
  114. {
  115. FatalError(WorldDownErrorMessage);
  116. worldresponse_timer->Disable();
  117. }*/
  118. if(playWaitTimer != NULL && playWaitTimer->Check ( ) )
  119. {
  120. SendPlayFailed(PLAY_ERROR_SERVER_TIMEOUT);
  121. safe_delete(playWaitTimer);
  122. }
  123. if(!needs_world_list && updatetimer && updatetimer->Check()){
  124. if(updatelisttimer && updatelisttimer->Check()){
  125. if(num_updates >= 180){ //30 minutes
  126. getConnection()->SendDisconnect();
  127. }
  128. else{
  129. vector<PacketStruct*>::iterator itr;
  130. if(update_packets){
  131. for(itr = update_packets->begin(); itr != update_packets->end(); itr++){
  132. safe_delete(*itr);
  133. }
  134. }
  135. safe_delete(update_packets);
  136. update_packets = world_list.GetServerListUpdate(version);
  137. }
  138. num_updates++;
  139. }
  140. else{
  141. if(!update_packets){
  142. update_packets = world_list.GetServerListUpdate(version);
  143. }
  144. else{
  145. if(update_position < update_packets->size()){
  146. QueuePacket(update_packets->at(update_position)->serialize());
  147. update_position++;
  148. }
  149. else
  150. update_position = 0;
  151. }
  152. }
  153. }
  154. while(app = eqnc->PopPacket())
  155. {
  156. switch(app->GetOpcode())
  157. {
  158. case OP_LoginRequestMsg:{
  159. DumpPacket(app);
  160. PacketStruct* packet = configReader.getStruct("LS_LoginRequest", 1);
  161. if(packet && packet->LoadPacketData(app->pBuffer,app->size)){
  162. version = packet->getType_int16_ByName("version");
  163. LogWrite(LOGIN__DEBUG, 0, "Login", "Classic Client Version Provided: %i", version);
  164. if (version == 0 || EQOpcodeManager.count(GetOpcodeVersion(version)) == 0)
  165. {
  166. safe_delete(packet);
  167. packet = configReader.getStruct("LS_LoginRequest", 1208);
  168. if (packet && packet->LoadPacketData(app->pBuffer, app->size)) {
  169. version = packet->getType_int16_ByName("version");
  170. }
  171. else
  172. break;
  173. }
  174. //[7:19 PM] Kirmmin: Well, I very quickly learned that unknown3 in LS_LoginRequest packet is the same value as cl_eqversion in the eq2_defaults.ini file.
  175. LogWrite(LOGIN__DEBUG, 0, "Login", "New Client Version Provided: %i", version);
  176. if (EQOpcodeManager.count(GetOpcodeVersion(version)) == 0) {
  177. LogWrite(LOGIN__ERROR, 0, "Login", "Incompatible client version provided: %i", version);
  178. SendLoginDenied();
  179. return false;
  180. }
  181. if(EQOpcodeManager.count(GetOpcodeVersion(version)) > 0 && getConnection()){
  182. getConnection()->SetClientVersion(GetVersion());
  183. EQ2_16BitString username = packet->getType_EQ2_16BitString_ByName("username");
  184. EQ2_16BitString password = packet->getType_EQ2_16BitString_ByName("password");
  185. LoginAccount* acct = database.LoadAccount(username.data.c_str(),password.data.c_str(), net.IsAllowingAccountCreation());
  186. if(acct){
  187. Client* otherclient = client_list.FindByLSID(acct->getLoginAccountID());
  188. if(otherclient)
  189. otherclient->getConnection()->SendDisconnect(); // This person is already logged in, we don't want them logged in twice, kick the previous client as it might be a ghost
  190. }
  191. if(acct){
  192. SetAccountName(username.data.c_str());
  193. database.UpdateAccountIPAddress(acct->getLoginAccountID(), getConnection()->GetrIP());
  194. LogWrite(LOGIN__INFO, 0, "Login", "%s successfully logged in.", (char*)username.data.c_str());
  195. }
  196. else
  197. {
  198. if (username.size > 0)
  199. LogWrite(LOGIN__ERROR, 0, "Login", "%s login failed!", (char*)username.data.c_str());
  200. else
  201. LogWrite(LOGIN__ERROR, 0, "Login", "[UNKNOWN USER] login failed!");
  202. }
  203. if(!acct)
  204. SendLoginDenied();
  205. else{
  206. needs_world_list = true;
  207. SetLoginAccount(acct);
  208. SendLoginAccepted();
  209. }
  210. }
  211. else{
  212. cout << "Error bad version: " << version << endl;
  213. SendLoginDeniedBadVersion();
  214. }
  215. }
  216. else{
  217. cout << "Error loading LS_LoginRequest packet: \n";
  218. //DumpPacket(app);
  219. }
  220. safe_delete(packet);
  221. break;
  222. }
  223. case OP_KeymapLoadMsg:{
  224. // cout << "Received OP_KeymapNoneMsg\n";
  225. //dunno what this is for
  226. break;
  227. }
  228. case OP_AllWSDescRequestMsg:{
  229. SendWorldList();
  230. needs_world_list = false;
  231. database.LoadCharacters(GetLoginAccount(), GetVersion());
  232. SendCharList();
  233. break;
  234. }
  235. case OP_LsClientCrashlogReplyMsg:{
  236. // DumpPacket(app);
  237. SaveErrorsToDB(app, "Crash Log", GetVersion());
  238. break;
  239. }
  240. case OP_LsClientVerifylogReplyMsg:{
  241. // DumpPacket(app);
  242. SaveErrorsToDB(app, "Verify Log", GetVersion());
  243. break;
  244. }
  245. case OP_LsClientAlertlogReplyMsg:{
  246. // DumpPacket(app);
  247. SaveErrorsToDB(app, "Alert Log", GetVersion());
  248. break;
  249. }
  250. case OP_LsClientBaselogReplyMsg:{
  251. // DumpPacket(app);
  252. SaveErrorsToDB(app, "Base Log", GetVersion());
  253. break;
  254. }
  255. case OP_AllCharactersDescRequestMsg:{
  256. break;
  257. }
  258. case OP_CreateCharacterRequestMsg:{
  259. PacketStruct* packet = configReader.getStruct("CreateCharacter", GetVersion());
  260. DumpPacket(app);
  261. playWaitTimer = new Timer ( 15000 );
  262. playWaitTimer->Start ( );
  263. cout << "Char Create Request From: " << GetAccountName() << "....";
  264. if(packet->LoadPacketData(app->pBuffer,app->size)){
  265. cout << "Loaded Successfully\n";
  266. packet->setDataByName("account_id",GetAccountID());
  267. LWorld* world_server = world_list.FindByID(packet->getType_int32_ByName("server_id"));
  268. if(!world_server)
  269. {
  270. DumpPacket(app->pBuffer, app->size);
  271. cout << GetAccountName() << " attempted creation of character with an invalid server id of: " << packet->getType_int32_ByName("server_id") << "\n";
  272. break;
  273. }
  274. else
  275. {
  276. ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, app->size+sizeof(int16));
  277. int16 out_version = GetVersion();
  278. memcpy(outpack->pBuffer, &out_version, sizeof(int16));
  279. memcpy(outpack->pBuffer + sizeof(int16), app->pBuffer, app->size);
  280. uchar* tmp = outpack->pBuffer;
  281. if(out_version<=283)
  282. tmp+=2;
  283. else
  284. tmp += 7;
  285. int32 account_id = GetAccountID();
  286. memcpy(tmp, &account_id, sizeof(int32));
  287. world_server->SendPacket(outpack);
  288. safe_delete(outpack);
  289. createRequest = packet;
  290. }
  291. }
  292. else{
  293. cout << "Error loading Char Create Packet!!\n";
  294. safe_delete(packet);
  295. }
  296. // world_list.SendWorldChanged(create.profile.server_id, false, this);
  297. break;
  298. }
  299. case OP_PlayCharacterRequestMsg:{
  300. int32 char_id = 0;
  301. int32 server_id = 0;
  302. PacketStruct* request = configReader.getStruct("LS_PlayRequest",GetVersion());
  303. if(request && request->LoadPacketData(app->pBuffer,app->size)){
  304. char_id = request->getType_int32_ByName("char_id");
  305. if (GetVersion() <= 283) {
  306. server_id = database.GetServer(GetAccountID(), char_id, request->getType_EQ2_16BitString_ByName("name").data);
  307. }
  308. else {
  309. server_id = request->getType_int32_ByName("server_id");
  310. }
  311. LWorld* world = world_list.FindByID(server_id);
  312. string name = database.GetCharacterName(char_id,server_id);
  313. if(world && name.length() > 0){
  314. pending_play_char_id = char_id;
  315. ServerPacket* outpack = new ServerPacket(ServerOP_UsertoWorldReq, sizeof(UsertoWorldRequest_Struct));
  316. UsertoWorldRequest_Struct* req = (UsertoWorldRequest_Struct*)outpack->pBuffer;
  317. req->char_id = char_id;
  318. req->lsaccountid = GetAccountID();
  319. req->worldid = server_id;
  320. struct in_addr in;
  321. in.s_addr = GetIP();
  322. strcpy(req->ip_address, inet_ntoa(in));
  323. world->SendPacket(outpack);
  324. delete outpack;
  325. safe_delete(playWaitTimer);
  326. playWaitTimer = new Timer ( 5000 );
  327. playWaitTimer->Start ( );
  328. }
  329. else{
  330. cout << GetAccountName() << " sent invalid Play Request: \n";
  331. SendPlayFailed(PLAY_ERROR_PROBLEM);
  332. DumpPacket(app);
  333. }
  334. }
  335. safe_delete(request);
  336. break;
  337. }
  338. case OP_DeleteCharacterRequestMsg:{
  339. PacketStruct* request = configReader.getStruct("LS_DeleteCharacterRequest", GetVersion());
  340. PacketStruct* response = configReader.getStruct("LS_DeleteCharacterResponse", GetVersion());
  341. if(request && response && request->LoadPacketData(app->pBuffer,app->size)){
  342. EQ2_16BitString name = request->getType_EQ2_16BitString_ByName("name");
  343. int32 acct_id = GetAccountID();
  344. int32 char_id = request->getType_int32_ByName("char_id");
  345. int32 server_id = request->getType_int32_ByName("server_id");
  346. if(database.VerifyDelete(acct_id, char_id, name.data.c_str())){
  347. response->setDataByName("response", 1);
  348. GetLoginAccount()->removeCharacter((char*)name.data.c_str());
  349. LWorld* world_server = world_list.FindByID(server_id);
  350. if(world_server != NULL)
  351. world_server->SendDeleteCharacter ( char_id , acct_id );
  352. }
  353. else
  354. response->setDataByName("response", 0);
  355. response->setDataByName("server_id", server_id);
  356. response->setDataByName("char_id", char_id);
  357. response->setDataByName("account_id", account_id);
  358. response->setMediumStringByName("name", (char*)name.data.c_str());
  359. response->setDataByName("max_characters", 10);
  360. EQ2Packet* outapp = response->serialize();
  361. QueuePacket(outapp);
  362. this->SendCharList();
  363. }
  364. safe_delete(request);
  365. safe_delete(response);
  366. break;
  367. }
  368. default: {
  369. const char* name = app->GetOpcodeName();
  370. if (name)
  371. LogWrite(OPCODE__DEBUG, 1, "Opcode", "%s Received %04X (%i)", name, app->GetRawOpcode(), app->GetRawOpcode());
  372. else
  373. LogWrite(OPCODE__DEBUG, 1, "Opcode", "Received %04X (%i)", app->GetRawOpcode(), app->GetRawOpcode());
  374. }
  375. }
  376. delete app;
  377. }
  378. }
  379. if (!eqnc->CheckActive()) {
  380. return false;
  381. }
  382. return true;
  383. }
  384. void Client::SaveErrorsToDB(EQApplicationPacket* app, char* type, int32 version){
  385. int32 size = 0;
  386. z_stream zstream;
  387. if (version >= 284) {
  388. memcpy(&size, app->pBuffer + sizeof(int32), sizeof(int32));
  389. zstream.next_in = app->pBuffer + 8;
  390. zstream.avail_in = app->size - 8;
  391. }
  392. else { //box set
  393. size = 0xFFFF;
  394. zstream.next_in = app->pBuffer + 2;
  395. zstream.avail_in = app->size - 2;
  396. }
  397. size++;
  398. char* message = new char[size];
  399. memset(message, 0, size);
  400. int zerror = 0;
  401. zstream.next_out = (BYTE*)message;
  402. zstream.avail_out = size;
  403. zstream.zalloc = Z_NULL;
  404. zstream.zfree = Z_NULL;
  405. zstream.opaque = Z_NULL;
  406. zerror = inflateInit( &zstream);
  407. if(zerror != Z_OK) {
  408. safe_delete_array(message);
  409. return;
  410. }
  411. zerror = inflate( &zstream, 0 );
  412. if(message && strlen(message) > 0)
  413. database.SaveClientLog(type, message, GetLoginAccount()->getLoginName(), GetVersion());
  414. safe_delete_array(message);
  415. }
  416. void Client::CharacterApproved(int32 server_id,int32 char_id)
  417. {
  418. if(createRequest && server_id == createRequest->getType_int32_ByName("server_id")){
  419. LWorld* world_server = world_list.FindByID(server_id);
  420. if(!world_server)
  421. return;
  422. PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply", GetVersion());
  423. if(packet){
  424. packet->setDataByName("account_id", GetAccountID());
  425. packet->setDataByName("unknown", 0xFFFFFFFF);
  426. packet->setDataByName("response", CREATESUCCESS_REPLY);
  427. packet->setMediumStringByName("name", (char*)createRequest->getType_EQ2_16BitString_ByName("name").data.c_str());
  428. EQ2Packet* outapp = packet->serialize();
  429. QueuePacket(outapp);
  430. safe_delete(packet);
  431. database.SaveCharacter(createRequest, GetLoginAccount(),char_id, GetVersion());
  432. // refresh characters for this account
  433. database.LoadCharacters(GetLoginAccount(), GetVersion());
  434. SendCharList();
  435. if (GetVersion() <= 546)
  436. {
  437. pending_play_char_id = char_id;
  438. ServerPacket* outpack = new ServerPacket(ServerOP_UsertoWorldReq, sizeof(UsertoWorldRequest_Struct));
  439. UsertoWorldRequest_Struct* req = (UsertoWorldRequest_Struct*)outpack->pBuffer;
  440. req->char_id = char_id;
  441. req->lsaccountid = GetAccountID();
  442. req->worldid = server_id;
  443. struct in_addr in;
  444. in.s_addr = GetIP();
  445. strcpy(req->ip_address, inet_ntoa(in));
  446. world_server->SendPacket(outpack);
  447. delete outpack;
  448. }
  449. }
  450. }
  451. else{
  452. cout << GetAccountName() << " received invalid CharacterApproval from server: " << server_id << endl;
  453. }
  454. safe_delete(createRequest);
  455. }
  456. void Client::CharacterRejected(int8 reason_number)
  457. {
  458. PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply", GetVersion());
  459. if(createRequest && packet){
  460. packet->setDataByName("account_id", GetAccountID());
  461. int8 clientReasonNum = reason_number;
  462. // reason numbers change and instead of updating the world server
  463. // the login server will hold the up to date #'s
  464. /*
  465. switch(reason_number)
  466. {
  467. // these error codes seem to be removed now, they shutdown the client rather immediately
  468. // for now we are just going to play a joke on them and say they can't create a new character.
  469. case INVALIDRACE_REPLY:
  470. case INVALIDGENDER_REPLY:
  471. clientReasonNum = 8;
  472. break;
  473. case BADNAMELENGTH_REPLY:
  474. clientReasonNum = 9;
  475. break;
  476. case NAMEINVALID_REPLY:
  477. clientReasonNum = 10;
  478. break;
  479. case NAMEFILTER_REPLY:
  480. clientReasonNum = 11;
  481. break;
  482. case NAMETAKEN_REPLY:
  483. clientReasonNum = 12;
  484. break;
  485. case OVERLOADEDSERVER_REPLY:
  486. clientReasonNum = 13;
  487. break;
  488. }
  489. */
  490. packet->setDataByName("response", clientReasonNum);
  491. packet->setMediumStringByName("name", "");
  492. EQ2Packet* outapp = packet->serialize();
  493. QueuePacket(outapp);
  494. safe_delete(packet);
  495. }
  496. /*LS_CreateCharacterReply reply(GetAccountID(), reason_number, create.profile.name.data);
  497. EQ2Packet* outapp = reply.serialize();
  498. QueuePacket(outapp);
  499. create.Clear();*/
  500. }
  501. void Client::SendCharList(){
  502. /*PacketStruct* packet = configReader.getStruct("LS_CreateCharacterReply");
  503. packet->setDataByName("account_id", GetAccountID());
  504. packet->setDataByName("response", reason_number);
  505. packet->setDataByName("name", &create.profile.name);
  506. EQ2Packet* outapp = packet->serialize();
  507. QueuePacket(outapp);
  508. safe_delete(packet);*/
  509. LogWrite(LOGIN__INFO, 0, "Login", "[%s] sending character list.", GetAccountName());
  510. LS_CharSelectList list;
  511. list.loadData(GetAccountID(), GetLoginAccount()->charlist, GetVersion());
  512. EQ2Packet* outapp = list.serialize(GetVersion());
  513. DumpPacket(outapp->pBuffer, outapp->size);
  514. QueuePacket(outapp);
  515. }
  516. void Client::SendLoginDeniedBadVersion(){
  517. EQ2Packet* app = new EQ2Packet(OP_LoginReplyMsg, 0, sizeof(LS_LoginResponse));
  518. LS_LoginResponse* ls_response = (LS_LoginResponse*)app->pBuffer;
  519. ls_response->reply_code = 6;
  520. ls_response->unknown03 = 0xFFFFFFFF;
  521. ls_response->unknown04 = 0xFFFFFFFF;
  522. QueuePacket(app);
  523. StartDisconnectTimer();
  524. }
  525. void Client::SendLoginDenied(){
  526. EQ2Packet* app = new EQ2Packet(OP_LoginReplyMsg, 0, sizeof(LS_LoginResponse));
  527. LS_LoginResponse* ls_response = (LS_LoginResponse*)app->pBuffer;
  528. ls_response->reply_code = 1;
  529. ls_response->unknown03 = 0xFFFFFFFF;
  530. ls_response->unknown04 = 0xFFFFFFFF;
  531. QueuePacket(app);
  532. StartDisconnectTimer();
  533. }
  534. void Client::SendLoginAccepted() {
  535. PacketStruct* packet = configReader.getStruct("LS_LoginReplyMsg", GetVersion());
  536. int i = 0;
  537. if (packet)
  538. {
  539. packet->setDataByName("account_id", 1);
  540. //packet->setDataByName("login_response", 0);
  541. //packet->setDataByName("reset_appearance", 0);
  542. packet->setDataByName("do_not_force_soga", 1);
  543. //packet->setDataByName("race_unknown", 0x3F);
  544. //packet->setDataByName("unknown11", 2); // can be 7
  545. packet->setDataByName("sub_level", 2);
  546. packet->setDataByName("race_flag", 0x1FFFFF);
  547. packet->setDataByName("class_flag", 0x7FFFFFE);
  548. packet->setMediumStringByName("username", GetAccountName());
  549. packet->setMediumStringByName("password", GetAccountName());
  550. packet->setDataByName("unknown5", 0x7C);
  551. packet->setDataByName("unknown7", 0xFF6FFFBF);
  552. // Image 2020 Notes
  553. // Login Server only supports AoM at current time, but we will need to keep in mind the structure for 60100 or later calls for additional custom fields
  554. // >=60100
  555. packet->setDataByName("unknown11", 0x0E);
  556. packet->setDataByName("unknown7a", 0xFFFF);
  557. packet->setDataByName("unknown8", 0xFF, 1);
  558. EQ2Packet* outapp = packet->serialize();
  559. printf("Out struct:\n");
  560. DumpPacket(outapp->pBuffer, outapp->size);
  561. QueuePacket(outapp);
  562. safe_delete(packet);
  563. }
  564. }
  565. void Client::SendWorldList(){
  566. EQ2Packet* pack = world_list.MakeServerListPacket(lsadmin, version);
  567. EQ2Packet* dupe = pack->Copy();
  568. DumpPacket(dupe->pBuffer,dupe->size);
  569. QueuePacket(dupe);
  570. return;
  571. }
  572. void Client::QueuePacket(EQ2Packet* app){
  573. eqnc->EQ2QueuePacket(app);
  574. }
  575. void Client::WorldResponse(int32 worldid, int8 response, char* ip_address, int32 port, int32 access_key)
  576. {
  577. LWorld* world = world_list.FindByID(worldid);
  578. if(world == 0) {
  579. FatalError(0);
  580. return;
  581. }
  582. if(response != 1){
  583. if(response == PLAY_ERROR_CHAR_NOT_LOADED){
  584. string pending_play_char_name = database.GetCharacterName(pending_play_char_id, worldid);
  585. if(database.VerifyDelete(GetAccountID(), pending_play_char_id, pending_play_char_name.c_str())){
  586. GetLoginAccount()->removeCharacter((char*)pending_play_char_name.c_str());
  587. }
  588. }
  589. FatalError(response);
  590. return;
  591. }
  592. PacketStruct* response_packet = configReader.getStruct("LS_PlayResponse", GetVersion());
  593. if(response_packet){
  594. safe_delete(playWaitTimer);
  595. response_packet->setDataByName("response", 1);
  596. response_packet->setSmallStringByName("server", ip_address);
  597. response_packet->setDataByName("port", port);
  598. response_packet->setDataByName("account_id", GetAccountID());
  599. response_packet->setDataByName("access_code", access_key);
  600. EQ2Packet* outapp = response_packet->serialize();
  601. QueuePacket(outapp);
  602. safe_delete(response_packet);
  603. }
  604. return;
  605. }
  606. void Client::FatalError(int8 response) {
  607. safe_delete(playWaitTimer);
  608. SendPlayFailed(response);
  609. }
  610. void Client::SendPlayFailed(int8 response){
  611. PacketStruct* response_packet = configReader.getStruct("LS_PlayResponse", GetVersion());
  612. if(response_packet){
  613. response_packet->setDataByName("response", response);
  614. response_packet->setSmallStringByName("server", "");
  615. response_packet->setDataByName("port", 0);
  616. response_packet->setDataByName("account_id", GetAccountID());
  617. response_packet->setDataByName("access_code", 0);
  618. EQ2Packet* outapp = response_packet->serialize();
  619. QueuePacket(outapp);
  620. safe_delete(response_packet);
  621. }
  622. }
  623. void ClientList::Add(Client* client) {
  624. MClientList.writelock();
  625. client_list[client] = true;
  626. MClientList.releasewritelock();
  627. }
  628. Client* ClientList::Get(int32 ip, int16 port) {
  629. Client* ret = 0;
  630. map<Client*, bool>::iterator itr;
  631. MClientList.readlock();
  632. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  633. if(itr->first->GetIP() == ip && itr->first->GetPort() == port){
  634. ret = itr->first;
  635. break;
  636. }
  637. }
  638. MClientList.releasereadlock();
  639. return ret;
  640. }
  641. void ClientList::FindByCreateRequest(){
  642. Client* client = 0;
  643. map<Client*, bool>::iterator itr;
  644. MClientList.readlock();
  645. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  646. if(itr->first->AwaitingCharCreationRequest()){
  647. if(!client)
  648. client = itr->first;
  649. else{
  650. client = 0;//more than 1 character waiting, dont want to send rejection to wrong one
  651. break;
  652. }
  653. }
  654. }
  655. MClientList.releasereadlock();
  656. if(client)
  657. client->CharacterRejected(UNKNOWNERROR_REPLY);
  658. }
  659. Client* ClientList::FindByLSID(int32 lsaccountid) {
  660. Client* client = 0;
  661. map<Client*, bool>::iterator itr;
  662. MClientList.readlock();
  663. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  664. if(itr->first->GetAccountID() == lsaccountid){
  665. client = itr->first;
  666. break;
  667. }
  668. }
  669. MClientList.releasereadlock();
  670. return client;
  671. }
  672. void ClientList::SendPacketToAllClients(EQ2Packet* app){
  673. Client* client = 0;
  674. map<Client*, bool>::iterator itr;
  675. MClientList.readlock();
  676. if(client_list.size() > 0){
  677. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  678. itr->first->QueuePacket(app->Copy());
  679. }
  680. }
  681. safe_delete(app);
  682. MClientList.releasereadlock();
  683. }
  684. void ClientList::Process() {
  685. Client* client = 0;
  686. vector<Client*> erase_list;
  687. map<Client*, bool>::iterator itr;
  688. MClientList.readlock();
  689. for(itr = client_list.begin(); itr != client_list.end(); itr++){
  690. client = itr->first;
  691. if(!client->Process())
  692. erase_list.push_back(client);
  693. }
  694. MClientList.releasereadlock();
  695. if(erase_list.size() > 0){
  696. vector<Client*>::iterator erase_itr;
  697. MClientList.writelock();
  698. for(erase_itr = erase_list.begin(); erase_itr != erase_list.end(); erase_itr++){
  699. client = *erase_itr;
  700. struct in_addr in;
  701. in.s_addr = client->getConnection()->GetRemoteIP();
  702. net.numclients--;
  703. LogWrite(LOGIN__INFO, 0, "Login", "Removing client from ip: %s on port %i, Account Name: %s", inet_ntoa(in), ntohs(client->getConnection()->GetRemotePort()), client->GetAccountName());
  704. client->getConnection()->SetState(CLOSED);
  705. net.UpdateWindowTitle();
  706. client_list.erase(client);
  707. }
  708. MClientList.releasewritelock();
  709. }
  710. }
  711. void Client::StartDisconnectTimer() {
  712. if (!disconnectTimer)
  713. {
  714. disconnectTimer = new Timer(1000);
  715. disconnectTimer->Start();
  716. }
  717. }