Some more loginserver refactoring to make things more sane to read

This commit is contained in:
Akkadius 2015-12-02 13:46:16 -06:00
parent 2fbf047853
commit a936796b45
5 changed files with 150 additions and 193 deletions

View File

@ -33,15 +33,15 @@ DatabaseMySQL::DatabaseMySQL(string user, string pass, string host, string port,
this->host = host; this->host = host;
this->name = name; this->name = name;
db = mysql_init(nullptr); database = mysql_init(nullptr);
if(db) if(database)
{ {
my_bool r = 1; my_bool r = 1;
mysql_options(db, MYSQL_OPT_RECONNECT, &r); mysql_options(database, MYSQL_OPT_RECONNECT, &r);
if(!mysql_real_connect(db, host.c_str(), user.c_str(), pass.c_str(), name.c_str(), atoi(port.c_str()), nullptr, 0)) if(!mysql_real_connect(database, host.c_str(), user.c_str(), pass.c_str(), name.c_str(), atoi(port.c_str()), nullptr, 0))
{ {
mysql_close(db); mysql_close(database);
server_log->Log(log_database, "Failed to connect to MySQL database. Error: %s", mysql_error(db)); server_log->Log(log_database, "Failed to connect to MySQL database. Error: %s", mysql_error(database));
exit(1); exit(1);
} }
} }
@ -53,15 +53,15 @@ DatabaseMySQL::DatabaseMySQL(string user, string pass, string host, string port,
DatabaseMySQL::~DatabaseMySQL() DatabaseMySQL::~DatabaseMySQL()
{ {
if(db) if(database)
{ {
mysql_close(db); mysql_close(database);
} }
} }
bool DatabaseMySQL::GetLoginDataFromAccountName(string name, string &password, unsigned int &id) bool DatabaseMySQL::GetLoginDataFromAccountName(string name, string &password, unsigned int &id)
{ {
if (!db) if (!database)
{ {
return false; return false;
} }
@ -73,13 +73,13 @@ bool DatabaseMySQL::GetLoginDataFromAccountName(string name, string &password, u
query << name; query << name;
query << "'"; query << "'";
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
return false; return false;
} }
res = mysql_use_result(db); res = mysql_use_result(database);
if (res) if (res)
{ {
@ -99,7 +99,7 @@ bool DatabaseMySQL::GetLoginDataFromAccountName(string name, string &password, u
bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, unsigned int &id, string &desc, unsigned int &list_id, bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, unsigned int &id, string &desc, unsigned int &list_id,
unsigned int &trusted, string &list_desc, string &account, string &password) unsigned int &trusted, string &list_desc, string &account, string &password)
{ {
if (!db) if (!database)
{ {
return false; return false;
} }
@ -108,7 +108,7 @@ bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, un
MYSQL_ROW row; MYSQL_ROW row;
char escaped_short_name[101]; char escaped_short_name[101];
unsigned long length; unsigned long length;
length = mysql_real_escape_string(db, escaped_short_name, short_name.substr(0, 100).c_str(), short_name.substr(0, 100).length()); length = mysql_real_escape_string(database, escaped_short_name, short_name.substr(0, 100).c_str(), short_name.substr(0, 100).length());
escaped_short_name[length + 1] = 0; escaped_short_name[length + 1] = 0;
stringstream query(stringstream::in | stringstream::out); stringstream query(stringstream::in | stringstream::out);
query << "SELECT ifnull(WSR.ServerID,999999) AS ServerID, WSR.ServerTagDescription, ifnull(WSR.ServerTrusted,0) AS ServerTrusted, ifnull(SLT.ServerListTypeID,3) AS ServerListTypeID, "; query << "SELECT ifnull(WSR.ServerID,999999) AS ServerID, WSR.ServerTagDescription, ifnull(WSR.ServerTrusted,0) AS ServerTrusted, ifnull(SLT.ServerListTypeID,3) AS ServerListTypeID, ";
@ -118,13 +118,13 @@ bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, un
query << escaped_short_name; query << escaped_short_name;
query << "'"; query << "'";
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
return false; return false;
} }
res = mysql_use_result(db); res = mysql_use_result(database);
if (res) if (res)
{ {
if ((row = mysql_fetch_row(res)) != nullptr) if ((row = mysql_fetch_row(res)) != nullptr)
@ -143,13 +143,13 @@ bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, un
query << "SELECT AccountName, AccountPassword FROM " << server.options.GetWorldAdminRegistrationTable(); query << "SELECT AccountName, AccountPassword FROM " << server.options.GetWorldAdminRegistrationTable();
query << " WHERE ServerAdminID = " << db_account_id; query << " WHERE ServerAdminID = " << db_account_id;
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
return false; return false;
} }
res = mysql_use_result(db); res = mysql_use_result(database);
if (res) if (res)
{ {
if ((row = mysql_fetch_row(res)) != nullptr) if ((row = mysql_fetch_row(res)) != nullptr)
@ -174,7 +174,7 @@ bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, un
void DatabaseMySQL::UpdateLSAccountData(unsigned int id, string ip_address) void DatabaseMySQL::UpdateLSAccountData(unsigned int id, string ip_address)
{ {
if (!db) if (!database)
{ {
return; return;
} }
@ -185,7 +185,7 @@ void DatabaseMySQL::UpdateLSAccountData(unsigned int id, string ip_address)
query << "', LastLoginDate = now() where LoginServerID = "; query << "', LastLoginDate = now() where LoginServerID = ";
query << id; query << id;
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
} }
@ -193,7 +193,7 @@ void DatabaseMySQL::UpdateLSAccountData(unsigned int id, string ip_address)
void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, string name, string password, string email) void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, string name, string password, string email)
{ {
if (!db) if (!database)
{ {
return; return;
} }
@ -204,7 +204,7 @@ void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, string name, string pas
query << password << "'), AccountCreateDate = now(), AccountEmail = '" << email; query << password << "'), AccountCreateDate = now(), AccountEmail = '" << email;
query << "', LastIPAddress = '0.0.0.0', LastLoginDate = now()"; query << "', LastIPAddress = '0.0.0.0', LastLoginDate = now()";
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
} }
@ -212,14 +212,14 @@ void DatabaseMySQL::UpdateLSAccountInfo(unsigned int id, string name, string pas
void DatabaseMySQL::UpdateWorldRegistration(unsigned int id, string long_name, string ip_address) void DatabaseMySQL::UpdateWorldRegistration(unsigned int id, string long_name, string ip_address)
{ {
if (!db) if (!database)
{ {
return; return;
} }
char escaped_long_name[101]; char escaped_long_name[101];
unsigned long length; unsigned long length;
length = mysql_real_escape_string(db, escaped_long_name, long_name.substr(0, 100).c_str(), long_name.substr(0, 100).length()); length = mysql_real_escape_string(database, escaped_long_name, long_name.substr(0, 100).c_str(), long_name.substr(0, 100).length());
escaped_long_name[length + 1] = 0; escaped_long_name[length + 1] = 0;
stringstream query(stringstream::in | stringstream::out); stringstream query(stringstream::in | stringstream::out);
query << "UPDATE " << server.options.GetWorldRegistrationTable() << " SET ServerLastLoginDate = now(), ServerLastIPAddr = '"; query << "UPDATE " << server.options.GetWorldRegistrationTable() << " SET ServerLastLoginDate = now(), ServerLastIPAddr = '";
@ -229,7 +229,7 @@ void DatabaseMySQL::UpdateWorldRegistration(unsigned int id, string long_name, s
query << "' WHERE ServerID = "; query << "' WHERE ServerID = ";
query << id; query << id;
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
} }
@ -237,7 +237,7 @@ void DatabaseMySQL::UpdateWorldRegistration(unsigned int id, string long_name, s
bool DatabaseMySQL::CreateWorldRegistration(string long_name, string short_name, unsigned int &id) bool DatabaseMySQL::CreateWorldRegistration(string long_name, string short_name, unsigned int &id)
{ {
if (!db) if (!database)
{ {
return false; return false;
} }
@ -247,20 +247,20 @@ bool DatabaseMySQL::CreateWorldRegistration(string long_name, string short_name,
char escaped_long_name[201]; char escaped_long_name[201];
char escaped_short_name[101]; char escaped_short_name[101];
unsigned long length; unsigned long length;
length = mysql_real_escape_string(db, escaped_long_name, long_name.substr(0, 100).c_str(), long_name.substr(0, 100).length()); length = mysql_real_escape_string(database, escaped_long_name, long_name.substr(0, 100).c_str(), long_name.substr(0, 100).length());
escaped_long_name[length + 1] = 0; escaped_long_name[length + 1] = 0;
length = mysql_real_escape_string(db, escaped_short_name, short_name.substr(0, 100).c_str(), short_name.substr(0, 100).length()); length = mysql_real_escape_string(database, escaped_short_name, short_name.substr(0, 100).c_str(), short_name.substr(0, 100).length());
escaped_short_name[length + 1] = 0; escaped_short_name[length + 1] = 0;
stringstream query(stringstream::in | stringstream::out); stringstream query(stringstream::in | stringstream::out);
query << "SELECT ifnull(max(ServerID),0) FROM " << server.options.GetWorldRegistrationTable(); query << "SELECT ifnull(max(ServerID),0) FROM " << server.options.GetWorldRegistrationTable();
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
return false; return false;
} }
res = mysql_use_result(db); res = mysql_use_result(database);
if (res) if (res)
{ {
if ((row = mysql_fetch_row(res)) != nullptr) if ((row = mysql_fetch_row(res)) != nullptr)
@ -273,7 +273,7 @@ bool DatabaseMySQL::CreateWorldRegistration(string long_name, string short_name,
query << ", ServerLongName = '" << escaped_long_name << "', ServerShortName = '" << escaped_short_name; query << ", ServerLongName = '" << escaped_long_name << "', ServerShortName = '" << escaped_short_name;
query << "', ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''"; query << "', ServerListTypeID = 3, ServerAdminID = 0, ServerTrusted = 0, ServerTagDescription = ''";
if (mysql_query(db, query.str().c_str()) != 0) if (mysql_query(database, query.str().c_str()) != 0)
{ {
server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str()); server_log->Log(log_database, "Mysql query failed: %s", query.str().c_str());
return false; return false;

View File

@ -35,7 +35,7 @@ public:
/** /**
* Constructor, sets our database to null. * Constructor, sets our database to null.
*/ */
DatabaseMySQL() { db = nullptr; } DatabaseMySQL() { database = nullptr; }
/** /**
* Constructor, tries to set our database to connect to the supplied options. * Constructor, tries to set our database to connect to the supplied options.
@ -50,7 +50,7 @@ public:
/** /**
* @return Returns true if the database successfully connected. * @return Returns true if the database successfully connected.
*/ */
virtual bool IsConnected() { return (db != nullptr); } virtual bool IsConnected() { return (database != nullptr); }
/** /**
* Retrieves the login data (password hash and account id) from the account name provided * Retrieves the login data (password hash and account id) from the account name provided
@ -88,7 +88,7 @@ public:
virtual bool CreateWorldRegistration(std::string long_name, std::string short_name, unsigned int &id); virtual bool CreateWorldRegistration(std::string long_name, std::string short_name, unsigned int &id);
protected: protected:
std::string user, pass, host, port, name; std::string user, pass, host, port, name;
MYSQL *db; MYSQL *database;
}; };
#endif #endif

View File

@ -41,7 +41,7 @@ ErrorLog::ErrorLog(const char* file_name)
ErrorLog::~ErrorLog() ErrorLog::~ErrorLog()
{ {
log_mutex->lock(); log_mutex->lock();
if(error_log) if (error_log)
{ {
fclose(error_log); fclose(error_log);
} }
@ -51,7 +51,7 @@ ErrorLog::~ErrorLog()
void ErrorLog::Log(eqLogType type, const char *message, ...) void ErrorLog::Log(eqLogType type, const char *message, ...)
{ {
if(type >= _log_largest_type) if (type >= _log_largest_type)
{ {
return; return;
} }
@ -70,21 +70,21 @@ void ErrorLog::Log(eqLogType type, const char *message, ...)
log_mutex->lock(); log_mutex->lock();
printf("[%s] [%02d.%02d.%02d - %02d:%02d:%02d] %s\n", printf("[%s] [%02d.%02d.%02d - %02d:%02d:%02d] %s\n",
eqLogTypes[type], eqLogTypes[type],
m_time->tm_mon+1, m_time->tm_mon + 1,
m_time->tm_mday, m_time->tm_mday,
m_time->tm_year%100, m_time->tm_year % 100,
m_time->tm_hour, m_time->tm_hour,
m_time->tm_min, m_time->tm_min,
m_time->tm_sec, m_time->tm_sec,
buffer); buffer);
if(error_log) if (error_log)
{ {
fprintf(error_log, "[%s] [%02d.%02d.%02d - %02d:%02d:%02d] %s\n", fprintf(error_log, "[%s] [%02d.%02d.%02d - %02d:%02d:%02d] %s\n",
eqLogTypes[type], eqLogTypes[type],
m_time->tm_mon+1, m_time->tm_mon + 1,
m_time->tm_mday, m_time->tm_mday,
m_time->tm_year%100, m_time->tm_year % 100,
m_time->tm_hour, m_time->tm_hour,
m_time->tm_min, m_time->tm_min,
m_time->tm_sec, m_time->tm_sec,
@ -98,7 +98,7 @@ void ErrorLog::Log(eqLogType type, const char *message, ...)
void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size) void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
{ {
if(type >= _log_largest_type) if (type >= _log_largest_type)
{ {
return; return;
} }
@ -112,21 +112,21 @@ void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
log_mutex->lock(); log_mutex->lock();
printf("[%s] [%02d.%02d.%02d - %02d:%02d:%02d] dumping packet of size %u:\n", printf("[%s] [%02d.%02d.%02d - %02d:%02d:%02d] dumping packet of size %u:\n",
eqLogTypes[type], eqLogTypes[type],
m_time->tm_mon+1, m_time->tm_mon + 1,
m_time->tm_mday, m_time->tm_mday,
m_time->tm_year%100, m_time->tm_year % 100,
m_time->tm_hour, m_time->tm_hour,
m_time->tm_min, m_time->tm_min,
m_time->tm_sec, m_time->tm_sec,
(unsigned int)size); (unsigned int)size);
if(error_log) if (error_log)
{ {
fprintf(error_log, "[%s] [%02d.%02d.%02d - %02d:%02d:%02d] dumping packet of size %u\n", fprintf(error_log, "[%s] [%02d.%02d.%02d - %02d:%02d:%02d] dumping packet of size %u\n",
eqLogTypes[type], eqLogTypes[type],
m_time->tm_mon+1, m_time->tm_mon + 1,
m_time->tm_mday, m_time->tm_mday,
m_time->tm_year%100, m_time->tm_year % 100,
m_time->tm_hour, m_time->tm_hour,
m_time->tm_min, m_time->tm_min,
m_time->tm_sec, m_time->tm_sec,
@ -138,14 +138,14 @@ void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
size_t j = 0; size_t j = 0;
size_t i = 0; size_t i = 0;
for(; i < size; ++i) for (; i < size; ++i)
{ {
if(i % 16 == 0) if (i % 16 == 0)
{ {
if(i != 0) if (i != 0)
{ {
printf(" | %s\n", ascii); printf(" | %s\n", ascii);
if(error_log) if (error_log)
{ {
fprintf(error_log, " | %s\n", ascii); fprintf(error_log, " | %s\n", ascii);
} }
@ -154,22 +154,22 @@ void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
memset(ascii, 0, 17); memset(ascii, 0, 17);
j = 0; j = 0;
} }
else if(i % 8 == 0) else if (i % 8 == 0)
{ {
printf("- "); printf("- ");
if(error_log) if (error_log)
{ {
fprintf(error_log, "- "); fprintf(error_log, "- ");
} }
} }
printf("%02X ", (unsigned int)data[i]); printf("%02X ", (unsigned int)data[i]);
if(error_log) if (error_log)
{ {
fprintf(error_log, "%02X ", (unsigned int)data[i]); fprintf(error_log, "%02X ", (unsigned int)data[i]);
} }
if(data[i] >= 32 && data[i] < 127) if (data[i] >= 32 && data[i] < 127)
{ {
ascii[j++] = data[i]; ascii[j++] = data[i];
} }
@ -180,26 +180,26 @@ void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
} }
size_t k = (i - 1) % 16; size_t k = (i - 1) % 16;
if(k < 8) if (k < 8)
{ {
printf(" "); printf(" ");
if(error_log) if (error_log)
{ {
fprintf(error_log, " "); fprintf(error_log, " ");
} }
} }
for(size_t h = k + 1; h < 16; ++h) for (size_t h = k + 1; h < 16; ++h)
{ {
printf(" "); printf(" ");
if(error_log) if (error_log)
{ {
fprintf(error_log, " "); fprintf(error_log, " ");
} }
} }
printf(" | %s\n", ascii); printf(" | %s\n", ascii);
if(error_log) if (error_log)
{ {
fprintf(error_log, " | %s\n", ascii); fprintf(error_log, " | %s\n", ascii);
fflush(error_log); fflush(error_log);
@ -207,4 +207,3 @@ void ErrorLog::LogPacket(eqLogType type, const char *data, size_t size)
log_mutex->unlock(); log_mutex->unlock();
} }

View File

@ -102,7 +102,7 @@ int main()
if (local_network.size() > 0) if (local_network.size() > 0)
server.options.WorldServerTypeTable(local_network); server.options.WorldServerTypeTable(local_network);
//Create our DB from options. /* Create database connection */
if (server.config->GetVariable("database", "subsystem").compare("MySQL") == 0) { if (server.config->GetVariable("database", "subsystem").compare("MySQL") == 0) {
#ifdef EQEMU_MYSQL_ENABLED #ifdef EQEMU_MYSQL_ENABLED
server_log->Log(log_debug, "MySQL Database Init."); server_log->Log(log_debug, "MySQL Database Init.");

View File

@ -31,12 +31,10 @@ ServerManager::ServerManager()
int listen_port = atoi(server.config->GetVariable("options", "listen_port").c_str()); int listen_port = atoi(server.config->GetVariable("options", "listen_port").c_str());
tcps = new EmuTCPServer(listen_port, true); tcps = new EmuTCPServer(listen_port, true);
if(tcps->Open(listen_port, error_buffer)) if(tcps->Open(listen_port, error_buffer)) {
{
server_log->Log(log_network, "ServerManager listening on port %u", listen_port); server_log->Log(log_network, "ServerManager listening on port %u", listen_port);
} }
else else {
{
server_log->Log(log_error, "ServerManager fatal error opening port on %u: %s", listen_port, error_buffer); server_log->Log(log_error, "ServerManager fatal error opening port on %u: %s", listen_port, error_buffer);
run_server = false; run_server = false;
} }
@ -44,8 +42,7 @@ ServerManager::ServerManager()
ServerManager::~ServerManager() ServerManager::~ServerManager()
{ {
if(tcps) if (tcps) {
{
tcps->Close(); tcps->Close();
delete tcps; delete tcps;
} }
@ -55,38 +52,32 @@ void ServerManager::Process()
{ {
ProcessDisconnect(); ProcessDisconnect();
EmuTCPConnection *tcp_c = nullptr; EmuTCPConnection *tcp_c = nullptr;
while(tcp_c = tcps->NewQueuePop()) while (tcp_c = tcps->NewQueuePop()) {
{
in_addr tmp; in_addr tmp;
tmp.s_addr = tcp_c->GetrIP(); tmp.s_addr = tcp_c->GetrIP();
server_log->Log(log_network, "New world server connection from %s:%d", inet_ntoa(tmp), tcp_c->GetrPort()); server_log->Log(log_network, "New world server connection from %s:%d", inet_ntoa(tmp), tcp_c->GetrPort());
WorldServer *cur = GetServerByAddress(tcp_c->GetrIP()); WorldServer *server_entity = GetServerByAddress(tcp_c->GetrIP());
if(cur) if (server_entity) {
{
server_log->Log(log_network, "World server already existed for %s, removing existing connection and updating current.", inet_ntoa(tmp)); server_log->Log(log_network, "World server already existed for %s, removing existing connection and updating current.", inet_ntoa(tmp));
cur->GetConnection()->Free(); server_entity->GetConnection()->Free();
cur->SetConnection(tcp_c); server_entity->SetConnection(tcp_c);
cur->Reset(); server_entity->Reset();
} }
else else {
{
WorldServer *w = new WorldServer(tcp_c); WorldServer *w = new WorldServer(tcp_c);
world_servers.push_back(w); world_servers.push_back(w);
} }
} }
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter)->Process() == false) {
if((*iter)->Process() == false)
{
server_log->Log(log_world, "World server %s had a fatal error and had to be removed from the login.", (*iter)->GetLongName().c_str()); server_log->Log(log_world, "World server %s had a fatal error and had to be removed from the login.", (*iter)->GetLongName().c_str());
delete (*iter); delete (*iter);
iter = world_servers.erase(iter); iter = world_servers.erase(iter);
} }
else else {
{
++iter; ++iter;
} }
} }
@ -95,20 +86,17 @@ void ServerManager::Process()
void ServerManager::ProcessDisconnect() void ServerManager::ProcessDisconnect()
{ {
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ EmuTCPConnection *connection = (*iter)->GetConnection();
EmuTCPConnection *c = (*iter)->GetConnection(); if (!connection->Connected()) {
if(!c->Connected())
{
in_addr tmp; in_addr tmp;
tmp.s_addr = c->GetrIP(); tmp.s_addr = connection->GetrIP();
server_log->Log(log_network, "World server disconnected from the server, removing server and freeing connection."); server_log->Log(log_network, "World server disconnected from the server, removing server and freeing connection.");
c->Free(); connection->Free();
delete (*iter); delete (*iter);
iter = world_servers.erase(iter); iter = world_servers.erase(iter);
} }
else else {
{
++iter; ++iter;
} }
} }
@ -117,10 +105,8 @@ void ServerManager::ProcessDisconnect()
WorldServer* ServerManager::GetServerByAddress(unsigned int address) WorldServer* ServerManager::GetServerByAddress(unsigned int address)
{ {
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter)->GetConnection()->GetrIP() == address) {
if((*iter)->GetConnection()->GetrIP() == address)
{
return (*iter); return (*iter);
} }
++iter; ++iter;
@ -138,10 +124,8 @@ EQApplicationPacket *ServerManager::CreateServerListPacket(Client *c)
string client_ip = inet_ntoa(in); string client_ip = inet_ntoa(in);
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter)->IsAuthorized() == false) {
if((*iter)->IsAuthorized() == false)
{
++iter; ++iter;
continue; continue;
} }
@ -149,16 +133,13 @@ EQApplicationPacket *ServerManager::CreateServerListPacket(Client *c)
in.s_addr = (*iter)->GetConnection()->GetrIP(); in.s_addr = (*iter)->GetConnection()->GetrIP();
string world_ip = inet_ntoa(in); string world_ip = inet_ntoa(in);
if(world_ip.compare(client_ip) == 0) if (world_ip.compare(client_ip) == 0) {
{
packet_size += (*iter)->GetLongName().size() + (*iter)->GetLocalIP().size() + 24; packet_size += (*iter)->GetLongName().size() + (*iter)->GetLocalIP().size() + 24;
} }
else if(client_ip.find(server.options.GetLocalNetwork()) != string::npos) else if (client_ip.find(server.options.GetLocalNetwork()) != string::npos) {
{
packet_size += (*iter)->GetLongName().size() + (*iter)->GetLocalIP().size() + 24; packet_size += (*iter)->GetLongName().size() + (*iter)->GetLocalIP().size() + 24;
} }
else else {
{
packet_size += (*iter)->GetLongName().size() + (*iter)->GetRemoteIP().size() + 24; packet_size += (*iter)->GetLongName().size() + (*iter)->GetRemoteIP().size() + 24;
} }
@ -167,98 +148,87 @@ EQApplicationPacket *ServerManager::CreateServerListPacket(Client *c)
} }
EQApplicationPacket *outapp = new EQApplicationPacket(OP_ServerListResponse, packet_size); EQApplicationPacket *outapp = new EQApplicationPacket(OP_ServerListResponse, packet_size);
ServerListHeader_Struct *sl = (ServerListHeader_Struct*)outapp->pBuffer; ServerListHeader_Struct *server_list = (ServerListHeader_Struct*)outapp->pBuffer;
sl->Unknown1 = 0x00000004; server_list->Unknown1 = 0x00000004;
sl->Unknown2 = 0x00000000; server_list->Unknown2 = 0x00000000;
sl->Unknown3 = 0x01650000; server_list->Unknown3 = 0x01650000;
/** /**
* Not sure what this is but it should be noted setting it to * Not sure what this is but it should be noted setting it to
* 0xFFFFFFFF crashes the client so: don't do that. * 0xFFFFFFFF crashes the client so: don't do that.
*/ */
sl->Unknown4 = 0x00000000; server_list->Unknown4 = 0x00000000;
sl->NumberOfServers = server_count; server_list->NumberOfServers = server_count;
unsigned char *data_ptr = outapp->pBuffer; unsigned char *data_pointer = outapp->pBuffer;
data_ptr += sizeof(ServerListHeader_Struct); data_pointer += sizeof(ServerListHeader_Struct);
iter = world_servers.begin(); iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter)->IsAuthorized() == false) {
if((*iter)->IsAuthorized() == false)
{
++iter; ++iter;
continue; continue;
} }
in.s_addr = (*iter)->GetConnection()->GetrIP(); in.s_addr = (*iter)->GetConnection()->GetrIP();
string world_ip = inet_ntoa(in); string world_ip = inet_ntoa(in);
if(world_ip.compare(client_ip) == 0) if (world_ip.compare(client_ip) == 0) {
{ memcpy(data_pointer, (*iter)->GetLocalIP().c_str(), (*iter)->GetLocalIP().size());
memcpy(data_ptr, (*iter)->GetLocalIP().c_str(), (*iter)->GetLocalIP().size()); data_pointer += ((*iter)->GetLocalIP().size() + 1);
data_ptr += ((*iter)->GetLocalIP().size() + 1);
} }
else if(client_ip.find(server.options.GetLocalNetwork()) != string::npos) else if (client_ip.find(server.options.GetLocalNetwork()) != string::npos) {
{ memcpy(data_pointer, (*iter)->GetLocalIP().c_str(), (*iter)->GetLocalIP().size());
memcpy(data_ptr, (*iter)->GetLocalIP().c_str(), (*iter)->GetLocalIP().size()); data_pointer += ((*iter)->GetLocalIP().size() + 1);
data_ptr += ((*iter)->GetLocalIP().size() + 1);
} }
else else {
{ memcpy(data_pointer, (*iter)->GetRemoteIP().c_str(), (*iter)->GetRemoteIP().size());
memcpy(data_ptr, (*iter)->GetRemoteIP().c_str(), (*iter)->GetRemoteIP().size()); data_pointer += ((*iter)->GetRemoteIP().size() + 1);
data_ptr += ((*iter)->GetRemoteIP().size() + 1);
} }
switch((*iter)->GetServerListID()) switch ((*iter)->GetServerListID()) {
{ case 1: {
case 1: *(unsigned int*)data_pointer = 0x00000030;
{
*(unsigned int*)data_ptr = 0x00000030;
break; break;
} }
case 2: case 2: {
{ *(unsigned int*)data_pointer = 0x00000009;
*(unsigned int*)data_ptr = 0x00000009;
break; break;
} }
default: default: {
{ *(unsigned int*)data_pointer = 0x00000001;
*(unsigned int*)data_ptr = 0x00000001;
} }
} }
data_ptr += 4;
*(unsigned int*)data_ptr = (*iter)->GetRuntimeID(); data_pointer += 4;
data_ptr += 4;
memcpy(data_ptr, (*iter)->GetLongName().c_str(), (*iter)->GetLongName().size()); *(unsigned int*)data_pointer = (*iter)->GetRuntimeID();
data_ptr += ((*iter)->GetLongName().size() + 1); data_pointer += 4;
memcpy(data_ptr, "EN", 2); memcpy(data_pointer, (*iter)->GetLongName().c_str(), (*iter)->GetLongName().size());
data_ptr += 3; data_pointer += ((*iter)->GetLongName().size() + 1);
memcpy(data_ptr, "US", 2); memcpy(data_pointer, "EN", 2);
data_ptr += 3; data_pointer += 3;
memcpy(data_pointer, "US", 2);
data_pointer += 3;
// 0 = Up, 1 = Down, 2 = Up, 3 = down, 4 = locked, 5 = locked(down) // 0 = Up, 1 = Down, 2 = Up, 3 = down, 4 = locked, 5 = locked(down)
if((*iter)->GetStatus() < 0) if ((*iter)->GetStatus() < 0) {
{ if ((*iter)->GetZonesBooted() == 0) {
if((*iter)->GetZonesBooted() == 0) *(uint32*)data_pointer = 0x01;
{
*(uint32*)data_ptr = 0x01;
} }
else else {
{ *(uint32*)data_pointer = 0x04;
*(uint32*)data_ptr = 0x04;
} }
} }
else else {
{ *(uint32*)data_pointer = 0x02;
*(uint32*)data_ptr = 0x02;
} }
data_ptr += 4; data_pointer += 4;
*(uint32*)data_ptr = (*iter)->GetPlayersOnline(); *(uint32*)data_pointer = (*iter)->GetPlayersOnline();
data_ptr += 4; data_pointer += 4;
++iter; ++iter;
} }
@ -270,10 +240,8 @@ void ServerManager::SendUserToWorldRequest(unsigned int server_id, unsigned int
{ {
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
bool found = false; bool found = false;
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter)->GetRuntimeID() == server_id) {
if((*iter)->GetRuntimeID() == server_id)
{
ServerPacket *outapp = new ServerPacket(ServerOP_UsertoWorldReq, sizeof(UsertoWorldRequest_Struct)); ServerPacket *outapp = new ServerPacket(ServerOP_UsertoWorldReq, sizeof(UsertoWorldRequest_Struct));
UsertoWorldRequest_Struct *utwr = (UsertoWorldRequest_Struct*)outapp->pBuffer; UsertoWorldRequest_Struct *utwr = (UsertoWorldRequest_Struct*)outapp->pBuffer;
utwr->worldid = server_id; utwr->worldid = server_id;
@ -281,8 +249,7 @@ void ServerManager::SendUserToWorldRequest(unsigned int server_id, unsigned int
(*iter)->GetConnection()->SendPacket(outapp); (*iter)->GetConnection()->SendPacket(outapp);
found = true; found = true;
if(server.options.IsDumpInPacketsOn()) if (server.options.IsDumpInPacketsOn()) {
{
DumpPacket(outapp); DumpPacket(outapp);
} }
delete outapp; delete outapp;
@ -290,8 +257,7 @@ void ServerManager::SendUserToWorldRequest(unsigned int server_id, unsigned int
++iter; ++iter;
} }
if(!found && server.options.IsTraceOn()) if (!found && server.options.IsTraceOn()) {
{
server_log->Log(log_client_error, "Client requested a user to world but supplied an invalid id of %u.", server_id); server_log->Log(log_client_error, "Client requested a user to world but supplied an invalid id of %u.", server_id);
} }
} }
@ -299,16 +265,13 @@ void ServerManager::SendUserToWorldRequest(unsigned int server_id, unsigned int
bool ServerManager::ServerExists(string l_name, string s_name, WorldServer *ignore) bool ServerManager::ServerExists(string l_name, string s_name, WorldServer *ignore)
{ {
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter) == ignore) {
if((*iter) == ignore)
{
++iter; ++iter;
continue; continue;
} }
if((*iter)->GetLongName().compare(l_name) == 0 && (*iter)->GetShortName().compare(s_name) == 0) if ((*iter)->GetLongName().compare(l_name) == 0 && (*iter)->GetShortName().compare(s_name) == 0) {
{
return true; return true;
} }
@ -320,18 +283,14 @@ bool ServerManager::ServerExists(string l_name, string s_name, WorldServer *igno
void ServerManager::DestroyServerByName(string l_name, string s_name, WorldServer *ignore) void ServerManager::DestroyServerByName(string l_name, string s_name, WorldServer *ignore)
{ {
list<WorldServer*>::iterator iter = world_servers.begin(); list<WorldServer*>::iterator iter = world_servers.begin();
while(iter != world_servers.end()) while (iter != world_servers.end()) {
{ if ((*iter) == ignore) {
if((*iter) == ignore)
{
++iter; ++iter;
} }
if((*iter)->GetLongName().compare(l_name) == 0 && (*iter)->GetShortName().compare(s_name) == 0) if ((*iter)->GetLongName().compare(l_name) == 0 && (*iter)->GetShortName().compare(s_name) == 0) {
{
EmuTCPConnection *c = (*iter)->GetConnection(); EmuTCPConnection *c = (*iter)->GetConnection();
if(c->Connected()) if (c->Connected()) {
{
c->Disconnect(); c->Disconnect();
} }
c->Free(); c->Free();
@ -342,4 +301,3 @@ void ServerManager::DestroyServerByName(string l_name, string s_name, WorldServe
++iter; ++iter;
} }
} }