<commit_before>/*
 * AscEmu Framework based on ArcEmu MMORPG Server
 * Copyright (C) 2014-2015 AscEmu Team <http://www.ascemu.org/>
 * Copyright (C) 2008-2012 ArcEmu Team <http://www.ArcEmu.org/>
 * Copyright (C) 2005-2007 Ascent Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "CommonTypes.hpp"
#include "LogonCommClient.h"
#include "WorldPacket.h"

#pragma pack(push, 1)
typedef struct
{
    uint16 opcode;
    uint32 size;
} logonpacket;
#pragma pack(pop)

static void swap32(uint32* p) { *p = ((*p >> 24 & 0xff)) | ((*p >> 8) & 0xff00) | ((*p << 8) & 0xff0000) | (*p << 24); }

LogonCommClientSocket::LogonCommClientSocket(SOCKET fd) : Socket(fd, 724288, 262444)
{
    // do nothing
    last_ping = last_pong = (uint32)UNIXTIME;
    remaining = opcode = 0;
    _id = 0;
    latency = 0;
    use_crypto = false;
    authenticated = 0;
    pingtime = 0;

    LOG_DEBUG("Created LogonCommClientSocket %u", m_fd);
}

void LogonCommClientSocket::OnRead()
{
    while (true)
    {
        if (!remaining)
        {
            if (readBuffer.GetSize() < 6)
                return;     // no header

            // read header
            readBuffer.Read((uint8*)&opcode, 2);
            readBuffer.Read((uint8*)&remaining, 4);

            // decrypt the first two bytes
            if (use_crypto)
            {
                _recvCrypto.Process((uint8*)&opcode, (uint8*)&opcode, 2);
                _recvCrypto.Process((uint8*)&remaining, (uint8*)&remaining, 4);
            }

            // convert network byte order
            swap32(&remaining);
        }

        // do we have a full packet?
        if (readBuffer.GetSize() < remaining)
            return;

        // create the buffer
        WorldPacket buff(opcode, remaining);
        if (remaining)
        {
            buff.resize(remaining);
            //Read(remaining, (uint8*)buff.contents());
            readBuffer.Read((uint8*)buff.contents(), remaining);
        }

        // decrypt the rest of the packet
        if (use_crypto && remaining)
            _recvCrypto.Process((unsigned char*)buff.contents(), (unsigned char*)buff.contents(), remaining);

        // handle the packet
        HandlePacket(buff);

        remaining = 0;
        opcode = 0;
    }
}

void LogonCommClientSocket::HandlePacket(WorldPacket& recvData)
{
    static logonpacket_handler Handlers[RMSG_COUNT] =
    {
        NULL,                                                   // RMSG_NULL
        NULL,                                                   // RCMSG_REGISTER_REALM
        &LogonCommClientSocket::HandleRegister,                 // RSMSG_REALM_REGISTERED
        NULL,                                                   // RCMSG_REQUEST_SESSION
        &LogonCommClientSocket::HandleSessionInfo,              // RSMSG_SESSION_RESULT
        NULL,                                                   // RCMSG_PING
        &LogonCommClientSocket::HandlePong,                     // RSMSG_PONG
        NULL,                                                   // RCMSG_SQL_EXECUTE
        NULL,                                                   // RCMSG_RELOAD_ACCOUNTS
        NULL,                                                   // RCMSG_AUTH_CHALLENGE
        &LogonCommClientSocket::HandleAuthResponse,             // RSMSG_AUTH_RESPONSE
        &LogonCommClientSocket::HandleRequestAccountMapping,    // RSMSG_REQUEST_ACCOUNT_CHARACTER_MAPPING
        NULL,                                                   // RCMSG_ACCOUNT_CHARACTER_MAPPING_REPLY
        NULL,                                                   // RCMSG_UPDATE_CHARACTER_MAPPING_COUNT
        &LogonCommClientSocket::HandleDisconnectAccount,        // RSMSG_DISCONNECT_ACCOUNT
        NULL,                                                   // RCMSG_TEST_CONSOLE_LOGIN
        &LogonCommClientSocket::HandleConsoleAuthResult,        // RSMSG_CONSOLE_LOGIN_RESULT
        NULL,                                                   // RCMSG_MODIFY_DATABASE
        &LogonCommClientSocket::HandlePopulationRequest,        // RSMSG_REALM_POP_REQ
        NULL,                                                   // RCMSG_REALM_POP_RES
    };

    if (recvData.GetOpcode() >= RMSG_COUNT || Handlers[recvData.GetOpcode()] == 0)
    {
        LOG_ERROR("Got unknown packet from logoncomm: %u", recvData.GetOpcode());
        return;
    }

    (this->*(Handlers[recvData.GetOpcode()]))(recvData);
}

void LogonCommClientSocket::HandleRegister(WorldPacket& recvData)
{
    uint32 realmlid;
    uint32 error;
    string realmname;
    recvData >> error >> realmlid >> realmname;

#ifdef WIN32
    Log.Success("LogonCommClient", "Realm `%s` (UNICODE) registered as realm %u.", _StringToANSI(realmname.c_str()), realmlid);
#else
    Log.Success("LogonCommClient", "Realm `%s` registered as realm %u.", realmname.c_str(), realmlid);
#endif

    LogonCommHandler::getSingleton().AdditionAck(_id, realmlid);
    realm_ids.insert(realmlid);
}

void LogonCommClientSocket::HandleSessionInfo(WorldPacket& recvData)
{
    uint32 request_id;
    recvData >> request_id;

    Mutex & m = sLogonCommHandler.GetPendingLock();
    m.Acquire();

    // find the socket with this request
    WorldSocket* sock = sLogonCommHandler.GetSocketByRequest(request_id);<commit_msg>Fix more issues in LogonCommClient.cpp<commit_after>/*
 * AscEmu Framework based on ArcEmu MMORPG Server
 * Copyright (C) 2014-2015 AscEmu Team <http://www.ascemu.org/>
 * Copyright (C) 2008-2012 ArcEmu Team <http://www.ArcEmu.org/>
 * Copyright (C) 2005-2007 Ascent Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "CommonTypes.hpp"
#include "LogonCommClient.h"
#include "LogonCommHandler.h"
#include "WorldPacket.h"

#pragma pack(push, 1)
typedef struct
{
    uint16 opcode;
    uint32 size;
} logonpacket;
#pragma pack(pop)

static void swap32(uint32* p) { *p = ((*p >> 24 & 0xff)) | ((*p >> 8) & 0xff00) | ((*p << 8) & 0xff0000) | (*p << 24); }

LogonCommClientSocket::LogonCommClientSocket(SOCKET fd) : Socket(fd, 724288, 262444)
{
    // do nothing
    last_ping = last_pong = (uint32)UNIXTIME;
    remaining = opcode = 0;
    _id = 0;
    latency = 0;
    use_crypto = false;
    authenticated = 0;
    pingtime = 0;

    LOG_DEBUG("Created LogonCommClientSocket %u", m_fd);
}

void LogonCommClientSocket::OnRead()
{
    while (true)
    {
        if (!remaining)
        {
            if (readBuffer.GetSize() < 6)
                return;     // no header

            // read header
            readBuffer.Read((uint8*)&opcode, 2);
            readBuffer.Read((uint8*)&remaining, 4);

            // decrypt the first two bytes
            if (use_crypto)
            {
                _recvCrypto.Process((uint8*)&opcode, (uint8*)&opcode, 2);
                _recvCrypto.Process((uint8*)&remaining, (uint8*)&remaining, 4);
            }

            // convert network byte order
            swap32(&remaining);
        }

        // do we have a full packet?
        if (readBuffer.GetSize() < remaining)
            return;

        // create the buffer
        WorldPacket buff(opcode, remaining);
        if (remaining)
        {
            buff.resize(remaining);
            //Read(remaining, (uint8*)buff.contents());
            readBuffer.Read((uint8*)buff.contents(), remaining);
        }

        // decrypt the rest of the packet
        if (use_crypto && remaining)
            _recvCrypto.Process((unsigned char*)buff.contents(), (unsigned char*)buff.contents(), remaining);

        // handle the packet
        HandlePacket(buff);

        remaining = 0;
        opcode = 0;
    }
}

void LogonCommClientSocket::HandlePacket(WorldPacket& recvData)
{
    static logonpacket_handler Handlers[RMSG_COUNT] =
    {
        NULL,                                                   // RMSG_NULL
        NULL,                                                   // RCMSG_REGISTER_REALM
        &LogonCommClientSocket::HandleRegister,                 // RSMSG_REALM_REGISTERED
        NULL,                                                   // RCMSG_REQUEST_SESSION
        &LogonCommClientSocket::HandleSessionInfo,              // RSMSG_SESSION_RESULT
        NULL,                                                   // RCMSG_PING
        &LogonCommClientSocket::HandlePong,                     // RSMSG_PONG
        NULL,                                                   // RCMSG_SQL_EXECUTE
        NULL,                                                   // RCMSG_RELOAD_ACCOUNTS
        NULL,                                                   // RCMSG_AUTH_CHALLENGE
        &LogonCommClientSocket::HandleAuthResponse,             // RSMSG_AUTH_RESPONSE
        &LogonCommClientSocket::HandleRequestAccountMapping,    // RSMSG_REQUEST_ACCOUNT_CHARACTER_MAPPING
        NULL,                                                   // RCMSG_ACCOUNT_CHARACTER_MAPPING_REPLY
        NULL,                                                   // RCMSG_UPDATE_CHARACTER_MAPPING_COUNT
        &LogonCommClientSocket::HandleDisconnectAccount,        // RSMSG_DISCONNECT_ACCOUNT
        NULL,                                                   // RCMSG_TEST_CONSOLE_LOGIN
        &LogonCommClientSocket::HandleConsoleAuthResult,        // RSMSG_CONSOLE_LOGIN_RESULT
        NULL,                                                   // RCMSG_MODIFY_DATABASE
        &LogonCommClientSocket::HandlePopulationRequest,        // RSMSG_REALM_POP_REQ
        NULL,                                                   // RCMSG_REALM_POP_RES
    };

    if (recvData.GetOpcode() >= RMSG_COUNT || Handlers[recvData.GetOpcode()] == 0)
    {
        LOG_ERROR("Got unknown packet from logoncomm: %u", recvData.GetOpcode());
        return;
    }

    (this->*(Handlers[recvData.GetOpcode()]))(recvData);
}

void LogonCommClientSocket::HandleRegister(WorldPacket& recvData)
{
    uint32 realmlid;
    uint32 error;
    std::string realmname;
    recvData >> error >> realmlid >> realmname;

#ifdef WIN32
    Log.Success("LogonCommClient", "Realm `%s` (UNICODE) registered as realm %u.", _StringToANSI(realmname.c_str()), realmlid);
#else
    Log.Success("LogonCommClient", "Realm `%s` registered as realm %u.", realmname.c_str(), realmlid);
#endif

    LogonCommHandler::getSingleton().AdditionAck(_id, realmlid);
    realm_ids.insert(realmlid);
}

void LogonCommClientSocket::HandleSessionInfo(WorldPacket& recvData)
{
    uint32 request_id;
    recvData >> request_id;

    Mutex & m = sLogonCommHandler.GetPendingLock();
    m.Acquire();

    // find the socket with this request
    WorldSocket* sock = sLogonCommHandler.GetSocketByRequest(request_id);
{'fraction_non_alphanumeric': 0.0986354447439353, 'fraction_numerical': 0.015835579514824796, 'mean_word_length': 1.9337781072399307, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195316', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3278, 'n_words': 775}