123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029 |
- /*
- EQ2Emulator: Everquest II Server Emulator
- Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
- This file is part of EQ2Emulator.
- EQ2Emulator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- EQ2Emulator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "../common/debug.h"
- #include "../common/Log.h"
- #include <iostream>
- using namespace std;
- #include <string.h>
- #include <stdio.h>
- #include <iomanip>
- using namespace std;
- #include <stdlib.h>
- #include "../common/version.h"
- #include "../common/GlobalHeaders.h"
- #include "../common/sha512.h"
- #ifdef WIN32
- #include <process.h>
- #include <WinSock2.h>
- #include <windows.h>
- #define strncasecmp _strnicmp
- #define strcasecmp _stricmp
- #else // Pyro: fix for linux
- #include <sys/socket.h>
- #ifdef FREEBSD //Timothy Whitman - January 7, 2003
- #include <sys/types.h>
- #endif
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <errno.h>
- #include "../common/unix.h"
- #define SOCKET_ERROR -1
- #define INVALID_SOCKET -1
- extern int errno;
- #endif
- #include "../common/servertalk.h"
- #include "LoginServer.h"
- #include "../common/packet_dump.h"
- #include "net.h"
- #include "zoneserver.h"
- #include "WorldDatabase.h"
- #include "Variables.h"
- #include "World.h"
- #include "../common/ConfigReader.h"
- #include "Rules/Rules.h"
- extern sint32 numzones;
- extern sint32 numclients;
- extern NetConnection net;
- extern LoginServer loginserver;
- extern WorldDatabase database;
- extern ZoneAuth zone_auth;
- extern Variables variables;
- extern ZoneList zone_list;
- extern ClientList client_list;
- extern volatile bool RunLoops;
- volatile bool LoginLoopRunning = false;
- extern ConfigReader configReader;
- extern RuleManager rule_manager;
- bool AttemptingConnect = false;
- LoginServer::LoginServer(const char* iAddress, int16 iPort) {
- LoginServerIP = ResolveIP(iAddress);
- LoginServerPort = iPort;
- statusupdate_timer = new Timer(LoginServer_StatusUpdateInterval);
- tcpc = new TCPConnection(false);
- pTryReconnect = true;
- try_auto_update = true;
- updates_always = false;
- updates_verbose = false;
- updates_auto_data = false;
- updates_ask = false;
- minLockedStatus = 100;
- maxPlayers = -1;
- minGameFullStatus = 100;
- update_server_verified = false;
- last_checked_time = 0;
- UpdateServerPort = 0;
- UpdateServerIP = 0;
- update_server_completed = false;
- data_waiting = 0;
- zone_updates = 0;
- loginEquip_updates = 0;
- }
- LoginServer::~LoginServer() {
- delete statusupdate_timer;
- delete tcpc;
- }
- void LoginServer::SendImmediateEquipmentUpdatesForChar(int32 char_id) {
- LogWrite(WORLD__DEBUG, 5, "World", "Sending login equipment updates for char_id: %u", char_id);
- int16 count = 0;
- if(!loginEquip_updates)
- loginEquip_updates = database.GetEquipmentUpdates(char_id);
- if(loginEquip_updates && loginEquip_updates->size() > 0)
- {
- map<int32, LoginEquipmentUpdate> send_map;
- int32 size = 0;
- MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
- while(itr.Next())
- {
- send_map[itr->first] = itr->second;
- size += sizeof(EquipmentUpdate_Struct);
- loginEquip_updates->erase(itr->first);
- count++;
- }
- ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
- EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
- updates->total_updates = count;
- int32 pos = sizeof(EquipmentUpdateList_Struct);
- map<int32, LoginEquipmentUpdate>::iterator send_itr;
- for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
- {
- EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
- update->id = send_itr->first;
- update->world_char_id = send_itr->second.world_char_id;
- update->equip_type = send_itr->second.equip_type;
- update->red = send_itr->second.red;
- update->green = send_itr->second.green;
- update->blue = send_itr->second.blue;
- update->highlight_red = send_itr->second.red;
- update->highlight_green = send_itr->second.green;
- update->highlight_blue = send_itr->second.blue;
- update->slot = send_itr->second.slot;
- pos += sizeof(EquipmentUpdate_Struct);
- }
- SendPacket(outpack);
- outpack->Deflate();
- safe_delete(outpack);
- }
- if(loginEquip_updates && count)
- loginEquip_updates->clear();
- if(loginEquip_updates && loginEquip_updates->size() == 0)
- {
- database.UpdateLoginEquipment();
- safe_delete(loginEquip_updates);
- }
- }
- bool LoginServer::Process() {
- if(last_checked_time > Timer::GetCurrentTime2())
- return true;
- last_checked_time = Timer::GetCurrentTime2() + 50;
- bool ret = true;
- if (statusupdate_timer->Check()) {
- this->SendStatus();
- }
- /************ Get all packets from packet manager out queue and process them ************/
- ServerPacket *pack = 0;
- while((pack = tcpc->PopPacket()))
- {
- switch(pack->opcode)
- {
- case ServerOP_LSFatalError:
- {
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LSFatalError", pack->opcode, pack->opcode);
- LogWrite(WORLD__ERROR, 0, "World", "Login Server returned a fatal error: %s\n", pack->pBuffer);
- tcpc->Disconnect();
- ret = false;
- net.ReadLoginINI();
- break;
- }
- case ServerOP_CharTimeStamp:
- {
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharTimeStamp", pack->opcode, pack->opcode);
- if(pack->size != sizeof(CharacterTimeStamp_Struct))
- break;
- CharacterTimeStamp_Struct* cts = (CharacterTimeStamp_Struct*) pack->pBuffer;
- // determine if the character exists and retrieve its latest timestamp from the world server
- bool char_exist = false;
- //int32 character_timestamp = database.GetCharacterTimeStamp(cts->char_id,cts->account_id,&char_exist);
- if(!char_exist)
- {
- //Character doesn't exist, get rid of it
- SendDeleteCharacter ( cts );
- break;
- }
- break;
- }
- case ServerOP_GetTableData:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetTableData", pack->opcode, pack->opcode);
- uchar* tmp = new uchar[pack->size];
- memcpy(tmp, pack->pBuffer, pack->size);
- data_updates_waiting.push_back(tmp);
- break;
- }
- case ServerOP_GetTableQuery:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetTableQuery", pack->opcode, pack->opcode);
- uchar* tmp = new uchar[pack->size];
- memcpy(tmp, pack->pBuffer, pack->size);
- table_updates_waiting.push_back(tmp);
- break;
- }
- case ServerOP_GetLatestTables:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_GetLatestTables", pack->opcode, pack->opcode);
- data_waiting = new uchar[pack->size];
- memcpy(data_waiting, pack->pBuffer, pack->size);
- break;
- }
- // Push Character Select "item appearances" to login_equipment table
- case ServerOP_LoginEquipment:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_LoginEquipment", pack->opcode, pack->opcode);
- LogWrite(MISC__TODO, 0, "TODO", "Implement map<character id <map<slot id, updatestruct> > method to update Login.\n%s, %s, %i", __FILE__, __FUNCTION__, __LINE__);
- if( pack->size == sizeof(EquipmentUpdateRequest_Struct) )
- {
- int16 max = ((EquipmentUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
- int16 count = 0;
- if(!loginEquip_updates)
- loginEquip_updates = database.GetEquipmentUpdates();
- if(loginEquip_updates && loginEquip_updates->size() > 0)
- {
- map<int32, LoginEquipmentUpdate> send_map;
- int32 size = 0;
- MutexMap<int32, LoginEquipmentUpdate>::iterator itr = loginEquip_updates->begin();
- while(itr.Next() && count < max)
- {
- send_map[itr->first] = itr->second;
- size += sizeof(EquipmentUpdate_Struct);
- loginEquip_updates->erase(itr->first);
- count++;
- }
- ServerPacket* outpack = new ServerPacket(ServerOP_LoginEquipment, size + sizeof(EquipmentUpdateList_Struct)+5);
- EquipmentUpdateList_Struct* updates = (EquipmentUpdateList_Struct*)outpack->pBuffer;
- updates->total_updates = count;
- int32 pos = sizeof(EquipmentUpdateList_Struct);
- map<int32, LoginEquipmentUpdate>::iterator send_itr;
- for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++)
- {
- EquipmentUpdate_Struct* update = (EquipmentUpdate_Struct*)(outpack->pBuffer + pos);
- update->id = send_itr->first;
- update->world_char_id = send_itr->second.world_char_id;
- update->equip_type = send_itr->second.equip_type;
- update->red = send_itr->second.red;
- update->green = send_itr->second.green;
- update->blue = send_itr->second.blue;
- update->highlight_red = send_itr->second.red;
- update->highlight_green = send_itr->second.green;
- update->highlight_blue = send_itr->second.blue;
- update->slot = send_itr->second.slot;
- pos += sizeof(EquipmentUpdate_Struct);
- }
- SendPacket(outpack);
- outpack->Deflate();
- safe_delete(outpack);
- }
- if(loginEquip_updates && count < max)
- loginEquip_updates->clear();
- if(loginEquip_updates && loginEquip_updates->size() == 0)
- {
- database.UpdateLoginEquipment();
- safe_delete(loginEquip_updates);
- }
- }
- break;
- }
- case ServerOP_ZoneUpdates:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ZoneUpdates", pack->opcode, pack->opcode);
- if(pack->size == sizeof(ZoneUpdateRequest_Struct)){
- int16 max = ((ZoneUpdateRequest_Struct*)pack->pBuffer)->max_per_batch;
- int16 count = 0;
- if(!zone_updates)
- zone_updates = database.GetZoneUpdates();
- if(zone_updates && zone_updates->size() > 0){
- map<int32, LoginZoneUpdate> send_map;
- int32 size = 0;
- MutexMap<int32, LoginZoneUpdate>::iterator itr = zone_updates->begin();
- while(itr.Next() && count < max){
- send_map[itr->first] = itr->second;
- size += sizeof(ZoneUpdate_Struct) + itr->second.name.length() + itr->second.description.length();
- zone_updates->erase(itr->first);
- count++;
- }
- ServerPacket* outpack = new ServerPacket(ServerOP_ZoneUpdates, size + sizeof(ZoneUpdateList_Struct)+5);
- ZoneUpdateList_Struct* updates = (ZoneUpdateList_Struct*)outpack->pBuffer;
- updates->total_updates = count;
- int32 pos = sizeof(ZoneUpdateList_Struct);
- map<int32, LoginZoneUpdate>::iterator send_itr;
- for(send_itr = send_map.begin(); send_itr != send_map.end(); send_itr++){
- ZoneUpdate_Struct* update = (ZoneUpdate_Struct*)(outpack->pBuffer + pos);
- update->zone_id = send_itr->first;
- update->zone_name_length = send_itr->second.name.length();
- update->zone_desc_length = send_itr->second.description.length();
- strcpy(update->data, send_itr->second.name.c_str());
- strcpy(update->data + send_itr->second.name.length(), send_itr->second.description.c_str());
- pos += sizeof(ZoneUpdate_Struct) + send_itr->second.name.length() + send_itr->second.description.length();
- }
- SendPacket(outpack);
- outpack->Deflate();
- safe_delete(outpack);
- }
- if(zone_updates && count < max)
- zone_updates->clear();
- if(zone_updates && zone_updates->size() == 0){
- database.UpdateLoginZones();
- safe_delete(zone_updates);
- }
- }
- break;
- }
- case ServerOP_CharacterCreate:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_CharacterCreate", pack->opcode, pack->opcode);
- int16 version = 1;
- if(pack->pBuffer[0] > 0)
- memcpy(&version, pack->pBuffer, sizeof(int16));
- //DumpPacket(pack->pBuffer,pack->size);
- PacketStruct* packet = configReader.getStruct("CreateCharacter", version);
- int8 resp = 0;
- int32 acct_id = 0;
- int32 char_id = 0;
- if(packet && packet->LoadPacketData(pack->pBuffer+sizeof(int16),pack->size - sizeof(int16))){
- EQ2_16BitString name = packet->getType_EQ2_16BitString_ByName("name");
- resp = database.CheckNameFilter(name.data.c_str());
- acct_id = packet->getType_int32_ByName("account_id");
- LogWrite(WORLD__DEBUG, 0, "World", "Response: %i", (int)resp);
-
- sint16 lowestStatus = database.GetLowestCharacterAdminStatus(acct_id);
- if(lowestStatus == -2)
- resp = UNKNOWNERROR_REPLY2;
- else if(resp == CREATESUCCESS_REPLY)
- char_id = database.SaveCharacter(packet, acct_id);
- }
- else{
- LogWrite(WORLD__ERROR, 0, "World", "Invalid creation request!");
- resp = UNKNOWNERROR_REPLY;
- }
- // send name filter response data back to the login server
- SendFilterNameResponse ( resp , acct_id , char_id );
- safe_delete(packet);
- break;
- }
- case ServerOP_BasicCharUpdate: {
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_BasicCharUpdate", pack->opcode, pack->opcode);
- if(pack->size != sizeof(CharDataUpdate_Struct))
- break;
- CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*) pack->pBuffer;
- switch(cdu->update_field)
- {
- case DELETE_UPDATE_FLAG:
- {
- LogWrite(WORLD__DEBUG, 0, "World", "Delete character request: %i %i",cdu->account_id,cdu->char_id );
- database.DeleteCharacter(cdu->account_id,cdu->char_id);
- break;
- }
- }
- break;
- }
- case ServerOP_UsertoWorldReq:{
- LogWrite(OPCODE__DEBUG, 0, "Opcode", "Opcode 0x%X (%i): ServerOP_UsertoWorldReq", pack->opcode, pack->opcode);
- UsertoWorldRequest_Struct* utwr = (UsertoWorldRequest_Struct*) pack->pBuffer;
- /*int32 id = database.GetAccountIDFromLSID(utwr->lsaccountid);
- sint16 status = database.CheckStatus(id);
- */
- int32 access_key = 0;
- // if it is a accepted login, we add the zone auth request
- access_key = DetermineCharacterLoginRequest ( utwr );
- if ( access_key != 0 )
- {
- zone_auth.PurgeInactiveAuth();
- char* characterName = database.GetCharacterName( utwr->char_id );
- if(characterName != 0){
- ZoneAuthRequest* zar = new ZoneAuthRequest(utwr->lsaccountid,characterName,access_key);
- zar->setFirstLogin ( true );
- zone_auth.AddAuth(zar);
- safe_delete_array(characterName);
- }
- }
- break;
- }
- case ServerOP_ResetDatabase:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): ServerOP_ResetDatabase", pack->opcode, pack->opcode);
- database.ResetDatabase();
- break;
- }
- case UpdateServerOP_Verified:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_Verified", pack->opcode, pack->opcode);
- update_server_verified = true;
- break;
- }
- case UpdateServerOP_DisplayMsg:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_DisplayMsg", pack->opcode, pack->opcode);
- LogWrite(WORLD__WARNING, 0, "World", "\t%s", pack->pBuffer);
- break;
- }
- case UpdateServerOP_Completed:{
- LogWrite(OPCODE__DEBUG, 1, "Opcode", "Opcode 0x%X (%i): UpdateServerOP_Completed", pack->opcode, pack->opcode);
- update_server_completed = true;
- break;
- }
- default:
- {
- LogWrite(WORLD__ERROR, 0, "World", "Unhandled opcode: %i", pack->opcode);
- DumpPacket(pack);
- }
- }
- safe_delete(pack);
- // break out if ret is now false
- if (!ret)
- break;
- }
- return ret;
- }
- // this should always be called in a new thread
- #ifdef WIN32
- void AutoInitLoginServer(void *tmp) {
- #else
- void *AutoInitLoginServer(void *tmp) {
- #endif
- if (loginserver.GetState() == TCPS_Ready) {
- InitLoginServer();
- }
- #ifndef WIN32
- return 0;
- #endif
- }
- bool InitLoginServer() {
- if (loginserver.GetState() != TCPS_Ready) {
- LogWrite(WORLD__ERROR, 0, "World", "InitLoginServer() while already attempting connect.");
- return false;
- }
- if (!net.LoginServerInfo) {
- LogWrite(WORLD__ERROR, 0, "World", "Login server info not loaded.");
- return false;
- }
- AttemptingConnect = true;
- int16 port;
- char* address = net.GetLoginInfo(&port);
- loginserver.Connect(address, port);
- return true;
- }
- void LoginServer::InitLoginServerVariables()
- {
- minLockedStatus = rule_manager.GetGlobalRule(R_World, ServerLockedOverrideStatus)->GetSInt16();
- maxPlayers = rule_manager.GetGlobalRule(R_World, MaxPlayers)->GetSInt16();
- minGameFullStatus = rule_manager.GetGlobalRule(R_World, MaxPlayersOverrideStatus)->GetSInt16();
- }
- void LoginServer::ProcessDataUpdate(uchar* data){
- TableDataQuery data_query;
- TableQuery table_queries;
- data_query.DeSerialize(data);
- string query;
- for(int32 i=0;i<data_query.num_queries;i++)
- {
- query = string("replace into ").append(data_query.tablename).append(" ");
- query.append(data_query.columns).append(" values(").append(data_query.queries[i]->query).append(")\0");
- char* new_query = new char[query.length() + 1];
- memset(new_query, 0, query.length() + 1);
- memcpy(new_query, query.c_str(), query.length());
- table_queries.AddQuery(new_query);
- }
- strcpy(table_queries.tablename, data_query.tablename);
- table_queries.latest_version = data_query.version;
- string queries = table_queries.GetQueriesString();
- char answer = 0;
- if(UpdatesAsk() || UpdatesVerbose())
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Queries:\n%s", queries.c_str());
- if(!UpdatesAuto() && UpdatesAsk())
- {
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest data version? ([Y]es, [N]o, [A]ll)");
- cin >> answer;
- if(toupper(answer) == 'A')
- UpdatesAuto(true);
- }
- if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
- {
- bool success = database.RunDatabaseQueries(&table_queries, false, true);
- string table = string(data_query.tablename);
- if(table != last_data_update_table)
- {
- if(success)
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tData Updated: '%s' = SUCCESS!", data_query.tablename);
- else
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Data Updated: '%s' = FAILED!", data_query.tablename);
-
- last_data_update_table = table;
- }
- }
- }
- void LoginServer::ProcessTableUpdate(uchar* data) {
- TableQuery table_query;
- table_query.DeSerialize(data);
- string queries = table_query.GetQueriesString();
- char answer = 0;
- if(UpdatesAsk() || UpdatesVerbose())
- {
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Found for '%s'!", table_query.tablename);
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tNew Version: %i", table_query.latest_version);
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tYour Version: %i", table_query.your_version);
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Pending Query:\n%s", queries.c_str());
- }
- if(!UpdatesAuto() && UpdatesAsk())
- {
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Update to the latest table version? ([Y]es, [N]o, [A]ll)");
- cin >> answer;
- if(toupper(answer) == 'A')
- UpdatesAuto(true);
- }
- if(UpdatesAuto() || (answer && toupper(answer) == 'Y'))
- {
- bool success = database.RunDatabaseQueries(&table_query, false);
- if(success)
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "\tUpdate Table: '%s' = SUCCESS!", table_query.tablename);
- else
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Update Table: '%s' = FAILED!", table_query.tablename);
- }
- }
- int32 LoginServer::ProcessTableUpdates(uchar* data) {
- int32 ret = 0;
- /*LatestTableVersions table_versions;
- table_versions.DeSerialize(data);
- int32 total_tables = table_versions.GetTotalTables();
-
- int32 max_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Max Version: %i", max_version);
- vector<ServerPacket*> packet_requests;
- ServerPacket* outpack = 0;
- for(int32 i=0;i<total_tables;i++){
- float version = database.CheckTableVersions(table_versions.GetTable(i).name);
- if( (version < table_versions.GetTable(i).version) )
- {
- outpack = new ServerPacket(ServerOP_GetTableQuery, sizeof(TableVersion));
- TableVersion* out_version = (TableVersion*)outpack->pBuffer;
- strcpy(out_version->name, table_versions.GetTable(i).name);
- out_version->max_table_version = max_version;
- out_version->version = (int32)version;
- out_version->last = 0;
- packet_requests.push_back(outpack);
- ret++;
- }
- }
- if( ret )
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates!");
- else
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--No Table Updates, continuing...");
- int32 total_packets = packet_requests.size();
- for(int32 i=0;i<total_packets;i++)
- {
- outpack = packet_requests[i];
- if((i+1) == total_packets)
- {
- TableVersion* out_version = (TableVersion*)outpack->pBuffer;
- out_version->last = 1;
- }
- SendPacket(outpack);
- safe_delete(outpack);
- }*/
- return ret;
- }
- int32 LoginServer::ProcessDataUpdates(uchar* data) {
- int32 ret = 0;
- /*LatestTableVersions table_versions;
- table_versions.DeSerialize(data);
- int32 total_tables = table_versions.GetTotalTables();
- vector<ServerPacket*> packet_requests;
- ServerPacket* outpack = 0;
- for(int32 i=0;i<total_tables;i++){
- if(UpdatesAutoData()){
- float version = database.GetLatestDataTableVersion(table_versions.GetTable(i).name);
- if(version >= 0 && version < table_versions.GetTable(i).data_version )
- {
- ServerPacket* outpack = new ServerPacket(ServerOP_GetTableData, sizeof(TableVersion));
- TableVersion* out_version = (TableVersion*)outpack->pBuffer;
- strcpy(out_version->name, table_versions.GetTable(i).name);
- out_version->max_data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
- out_version->data_version = (sint32)version;
- out_version->last = 0;
- memset(out_version->column_names, 0, 1000);
- string columns = database.GetColumnNames(out_version->name);
- if(columns.length() > 1)
- strcpy(out_version->column_names, columns.c_str());
- packet_requests.push_back(outpack);
- ret++;
- }
- }
- }
- if( ret )
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Data Updates!");
- int32 total_packets = packet_requests.size();
- for(int32 i=0;i<total_packets;i++)
- {
- outpack = packet_requests[i];
- if((i+1) == total_packets)
- {
- TableVersion* out_version = (TableVersion*)outpack->pBuffer;
- out_version->last = 1;
- }
- SendPacket(outpack);
- safe_delete(outpack);
- }*/
- return ret;
- }
- bool LoginServer::CheckAndWait(Timer* timer){
- Timer::SetCurrentTime();
- if(timer->Check(false)){
- if(timer->GetDuration() == 5000){
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "Please wait...");
- timer->SetTimer(25000);
- timer->Start();
- }
- else
- return false;
- }
- Sleep(10);
- if(!loginserver.Process())
- tcpc->Disconnect();
- return Connected();
- }
- bool LoginServer::ConnectToUpdateServer(const char* iAddress, int16 iPort)
- {
- if(!try_auto_update)
- return true;
- char errbuf[TCPConnection_ErrorBufferSize];
- memset(errbuf, 0, TCPConnection_ErrorBufferSize);
- if (iAddress == 0)
- {
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: address == 0");
- return false;
- }
- else
- {
- if ((UpdateServerIP = ResolveIP(iAddress, errbuf)) == 0)
- {
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "LoginServer::ConnectToUpdateServer: Resolving IP address: '%s'", errbuf[0] == '\0' ? errbuf : " ");
- return false;
- }
- }
- if (iPort != 0)
- UpdateServerPort = iPort;
- if (UpdateServerIP == 0 || UpdateServerPort == 0)
- {
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: Connect info incomplete, cannot connect.");
- return false;
- }
- if (tcpc->Connect(UpdateServerIP, UpdateServerPort, errbuf))
- {
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Connected to Update Server: %s:%i", iAddress, UpdateServerPort);
- SendInfo();
- //wait for a max of 30 seconds to be verified by patcher
- //display a message at 5 seconds
- Timer waitTimer(5000);
- waitTimer.Start();
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-Checking for updates.");
- GetLatestTables();
- while(CheckAndWait(&waitTimer) && !update_server_verified);
- database.CheckVersionTable();
- if(!update_server_verified)
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Error while getting updates!");
- if(update_server_verified)
- {
- update_server_completed = false;
- waitTimer.SetTimer(5000);
- waitTimer.Start();
- while(CheckAndWait(&waitTimer) && !data_waiting);
- if(data_waiting)
- {
- int32 requests = ProcessTableUpdates(data_waiting);
- if(requests > 0)
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Found Table Updates.");
- uchar* data = 0;
- waitTimer.SetTimer(30000);
- waitTimer.Start();
- if(requests > 0)
- {
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Table Updates.");
- database.DisableConstraints();
- deque<uchar*> alter_table_updates_waiting;
- while((CheckAndWait(&waitTimer) && !update_server_completed) || table_updates_waiting.size() > 0)
- {
- if(table_updates_waiting.size() > 0)
- {
- data = table_updates_waiting.front();
- table_updates_waiting.pop_front();
- ProcessTableUpdate(data);
- safe_delete(data);
- waitTimer.SetTimer(30000);
- waitTimer.Start();
- }
- }
- if(!CheckAndWait(&waitTimer))
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
- else
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Table Update complete.");
- database.EnableConstraints();
- }
- else
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Schema...");
- requests = ProcessDataUpdates(data_waiting);
- if(requests > 0)
- {
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Processing Data Updates.");
- LogWrite(INIT__PATCHER_WARNING, 0, "Patcher", "** Data Updates are SLOW! Do not abort! **");
- database.DisableConstraints();
- deque<uchar*> alter_table_updates_waiting;
- int32 count = 0;
- while((CheckAndWait(&waitTimer) && count < requests) || data_updates_waiting.size() > 0)
- {
- if(data_updates_waiting.size() > 0)
- {
- data = data_updates_waiting.front();
- data_updates_waiting.pop_front();
- ProcessDataUpdate(data);
- count++;
- safe_delete_array(data);
- waitTimer.SetTimer(30000);
- waitTimer.Start();
- }
- }
- if(!CheckAndWait(&waitTimer))
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "Timeout waiting for updates to complete! Try again later.");
- else
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "--Data Updates complete.");
- database.EnableConstraints();
- }
- else
- LogWrite(INIT__PATCHER_DEBUG, 0, "Patcher", "-No changes to DB Data...");
- safe_delete_array(data_waiting);
- }
- try_auto_update = false;
- }
- tcpc->Disconnect();
- return update_server_verified;
- }
- else
- {
- LogWrite(INIT__PATCHER_ERROR, 0, "Patcher", "PatchServer::ConnectToUpdateServer: '%s'", errbuf[0] == '\0' ? errbuf : " ");
- return false;
- }
- }
- bool LoginServer::Connect(const char* iAddress, int16 iPort) {
- if(!pTryReconnect)
- return false;
- char errbuf[TCPConnection_ErrorBufferSize];
- memset(errbuf, 0, TCPConnection_ErrorBufferSize);
- if (iAddress == 0) {
- LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: address == 0");
- return false;
- }
- else {
- if ((LoginServerIP = ResolveIP(iAddress, errbuf)) == 0) {
- LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Resolving IP address: '%s'", errbuf);
- return false;
- }
- }
- if (iPort != 0)
- LoginServerPort = iPort;
- if (LoginServerIP == 0 || LoginServerPort == 0) {
- LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: Connect info incomplete, cannot connect");
- return false;
- }
- if (tcpc->Connect(LoginServerIP, LoginServerPort, errbuf)) {
- LogWrite(WORLD__INFO, 0, "World", "Connected to LoginServer: %s: %i", iAddress, LoginServerPort);
- SendInfo();
- SendStatus();
- return true;
- }
- else {
- LogWrite(WORLD__ERROR, 0, "World", "LoginServer::Connect: '%s'", errbuf);
- return false;
- }
- }
- void LoginServer::GetLatestTables(){
- ServerPacket* pack = new ServerPacket(ServerOP_GetLatestTables, sizeof(GetLatestTables_Struct));
- GetLatestTables_Struct* data = (GetLatestTables_Struct*)pack->pBuffer;
- data->table_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
- data->data_version = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
- SendPacket(pack);
- delete pack;
- }
- void LoginServer::SendInfo() {
- ServerPacket* pack = new ServerPacket;
- pack->opcode = ServerOP_LSInfo;
- pack->size = sizeof(ServerLSInfo_Struct);
- pack->pBuffer = new uchar[pack->size];
- memset(pack->pBuffer, 0, pack->size);
- ServerLSInfo_Struct* lsi = (ServerLSInfo_Struct*) pack->pBuffer;
- strcpy(lsi->protocolversion, EQEMU_PROTOCOL_VERSION);
- strcpy(lsi->serverversion, CURRENT_VERSION);
- strcpy(lsi->name, net.GetWorldName());
- strcpy(lsi->account, net.GetWorldAccount());
- lsi->dbversion = CURRENT_DATABASE_MAJORVERSION*100 + CURRENT_DATABASE_MINORVERSION;
- #ifdef _DEBUG
- lsi->servertype = 4;
- #endif
- int8 tmppass[16];
- string passwdSha512 = sha512(net.GetWorldPassword());
- memcpy(lsi->password, (char*)passwdSha512.c_str(), passwdSha512.length());
- strcpy(lsi->address, net.GetWorldAddress());
- SendPacket(pack);
- delete pack;
- }
- void LoginServer::SendStatus() {
- statusupdate_timer->Start();
- ServerPacket* pack = new ServerPacket;
- pack->opcode = ServerOP_LSStatus;
- pack->size = sizeof(ServerLSStatus_Struct);
- pack->pBuffer = new uchar[pack->size];
- memset(pack->pBuffer, 0, pack->size);
- ServerLSStatus_Struct* lss = (ServerLSStatus_Struct*) pack->pBuffer;
- if (net.world_locked)
- lss->status = -2;
- else if(loginserver.maxPlayers > -1 && numclients >= loginserver.maxPlayers)
- lss->status = -3;
- else
- lss->status = 1;
- lss->num_zones = numzones;
- lss->num_players = numclients;
- lss->world_max_level = rule_manager.GetGlobalRule(R_Player, MaxLevel)->GetInt8();
- SendPacket(pack);
- delete pack;
- }
- void LoginServer::SendDeleteCharacter ( CharacterTimeStamp_Struct* cts ) {
- ServerPacket* outpack = new ServerPacket(ServerOP_BasicCharUpdate, sizeof(CharDataUpdate_Struct));
- CharDataUpdate_Struct* cdu = (CharDataUpdate_Struct*)outpack->pBuffer;
- cdu->account_id = cts->account_id;
- cdu->char_id = cts->char_id;
- cdu->update_field = DELETE_UPDATE_FLAG;
- cdu->update_data = 1;
- SendPacket(outpack);
- safe_delete(outpack);
- }
- void LoginServer::SendFilterNameResponse ( int8 resp, int32 acct_id , int32 char_id ) {
- ServerPacket* outpack = new ServerPacket(ServerOP_CharacterCreate, sizeof(WorldCharNameFilterResponse_Struct));
- WorldCharNameFilterResponse_Struct* wcfr = (WorldCharNameFilterResponse_Struct*)outpack->pBuffer;
- wcfr->response = resp;
- wcfr->account_id = acct_id;
- wcfr->char_id = char_id;
- SendPacket(outpack);
- safe_delete(outpack);
- }
- int32 LoginServer::DetermineCharacterLoginRequest ( UsertoWorldRequest_Struct* utwr ) {
- LogWrite(LOGIN__TRACE, 9, "Login", "Enter: %s", __FUNCTION__);
- ServerPacket* outpack = new ServerPacket;
- outpack->opcode = ServerOP_UsertoWorldResp;
- outpack->size = sizeof(UsertoWorldResponse_Struct);
- outpack->pBuffer = new uchar[outpack->size];
- memset(outpack->pBuffer, 0, outpack->size);
- UsertoWorldResponse_Struct* utwrs = (UsertoWorldResponse_Struct*) outpack->pBuffer;
- utwrs->lsaccountid = utwr->lsaccountid;
- utwrs->char_id = utwr->char_id;
- utwrs->ToID = utwr->FromID;
- int32 timestamp = Timer::GetUnixTimeStamp();
- utwrs->access_key = timestamp;
- // set default response to 0
- utwrs->response = 0;
- sint16 lowestStatus = database.GetLowestCharacterAdminStatus( utwr->lsaccountid );
- sint16 status = 0;
-
- if(lowestStatus == -2)
- status = -1;
- else
- status = database.GetCharacterAdminStatus ( utwr->lsaccountid , utwr->char_id );
- if(status < 100 && zone_list.ClientConnected(utwr->lsaccountid))
- status = -9;
- if(status < 0){
- switch(status){
- case -10:
- utwrs->response = PLAY_ERROR_CHAR_NOT_LOADED;
- break;
- case -9:
- utwrs->response = 0;//PLAY_ERROR_ACCOUNT_IN_USE;
- break;
- case -8:
- utwrs->response = PLAY_ERROR_LOADING_ERROR;
- break;
- case -1:
- utwrs->response = PLAY_ERROR_ACCOUNT_BANNED;
- break;
- default:
- utwrs->response = PLAY_ERROR_PROBLEM;
- }
- }
- else if(net.world_locked == true){
- LogWrite(WORLD__INFO, 0, "World", "Login Lock Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minLockedStatus,status,utwr->char_id );
- // has high enough status, allow it
- if(status >= loginserver.minLockedStatus)
- utwrs->response = 1;
- }
- else if(loginserver.maxPlayers > -1 && ((sint16)client_list.Count()) >= loginserver.maxPlayers)
- {
- LogWrite(WORLD__INFO, 0, "World", "Login GameFull Check (MinStatus: %i):, UserStatus: %i, CharID: %i",loginserver.minGameFullStatus,status,utwr->char_id );
- // has high enough status, allow it
- if(status >= loginserver.minGameFullStatus)
- {
- utwrs->response = 1;
- }
- else
- utwrs->response = -3; // server full response is -3
- }
- else
- utwrs->response = 1;
- /*sint32 x = database.CommandRequirement("$MAXCLIENTS");
- if( (sint32)numplayers >= x && x != -1 && x != 255 && status < 80)
- utwrs->response = -3;
- if(status == -1)
- utwrs->response = -1;
- if(status == -2)
- utwrs->response = -2;
- */
- //printf("Response is %i for %i\n",utwrs->response,id);
- if((strcmp(net.GetWorldAddress(), utwr->ip_address)==0) && (strlen(net.GetInternalWorldAddress())>0))
- strcpy(utwrs->ip_address, net.GetInternalWorldAddress());
- else
- strcpy(utwrs->ip_address, net.GetWorldAddress());
- utwrs->port = net.GetWorldPort();
- utwrs->worldid = utwr->worldid;
- SendPacket(outpack);
- delete outpack;
- LogWrite(LOGIN__TRACE, 9, "Login", "Exit: %s with timestamp=%u", __FUNCTION__, timestamp);
- // depending on the response determined above, this could return 0 (for failure)
- return timestamp;
- }
|