Initial commit: wowee native WoW 3.3.5a client

This commit is contained in:
Kelsi 2026-02-02 12:24:50 -08:00
commit ce6cb8f38e
147 changed files with 32347 additions and 0 deletions

90
src/network/packet.cpp Normal file
View file

@ -0,0 +1,90 @@
#include "network/packet.hpp"
#include <cstring>
namespace wowee {
namespace network {
Packet::Packet(uint16_t opcode) : opcode(opcode) {}
Packet::Packet(uint16_t opcode, const std::vector<uint8_t>& data)
: opcode(opcode), data(data), readPos(0) {}
void Packet::writeUInt8(uint8_t value) {
data.push_back(value);
}
void Packet::writeUInt16(uint16_t value) {
data.push_back(value & 0xFF);
data.push_back((value >> 8) & 0xFF);
}
void Packet::writeUInt32(uint32_t value) {
data.push_back(value & 0xFF);
data.push_back((value >> 8) & 0xFF);
data.push_back((value >> 16) & 0xFF);
data.push_back((value >> 24) & 0xFF);
}
void Packet::writeUInt64(uint64_t value) {
writeUInt32(value & 0xFFFFFFFF);
writeUInt32((value >> 32) & 0xFFFFFFFF);
}
void Packet::writeString(const std::string& value) {
for (char c : value) {
data.push_back(static_cast<uint8_t>(c));
}
data.push_back(0); // Null terminator
}
void Packet::writeBytes(const uint8_t* bytes, size_t length) {
data.insert(data.end(), bytes, bytes + length);
}
uint8_t Packet::readUInt8() {
if (readPos >= data.size()) return 0;
return data[readPos++];
}
uint16_t Packet::readUInt16() {
uint16_t value = 0;
value |= readUInt8();
value |= (readUInt8() << 8);
return value;
}
uint32_t Packet::readUInt32() {
uint32_t value = 0;
value |= readUInt8();
value |= (readUInt8() << 8);
value |= (readUInt8() << 16);
value |= (readUInt8() << 24);
return value;
}
uint64_t Packet::readUInt64() {
uint64_t value = readUInt32();
value |= (static_cast<uint64_t>(readUInt32()) << 32);
return value;
}
float Packet::readFloat() {
// Read as uint32 and reinterpret as float
uint32_t bits = readUInt32();
float value;
std::memcpy(&value, &bits, sizeof(float));
return value;
}
std::string Packet::readString() {
std::string result;
while (readPos < data.size()) {
uint8_t c = data[readPos++];
if (c == 0) break;
result += static_cast<char>(c);
}
return result;
}
} // namespace network
} // namespace wowee

9
src/network/socket.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "network/socket.hpp"
namespace wowee {
namespace network {
// Base class implementation (empty - pure virtual methods in derived classes)
} // namespace network
} // namespace wowee

227
src/network/tcp_socket.cpp Normal file
View file

@ -0,0 +1,227 @@
#include "network/tcp_socket.hpp"
#include "network/packet.hpp"
#include "core/logger.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <cstring>
namespace wowee {
namespace network {
TCPSocket::TCPSocket() = default;
TCPSocket::~TCPSocket() {
disconnect();
}
bool TCPSocket::connect(const std::string& host, uint16_t port) {
LOG_INFO("Connecting to ", host, ":", port);
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
LOG_ERROR("Failed to create socket");
return false;
}
// Set non-blocking
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
// Resolve host
struct hostent* server = gethostbyname(host.c_str());
if (server == nullptr) {
LOG_ERROR("Failed to resolve host: ", host);
close(sockfd);
sockfd = -1;
return false;
}
// Connect
struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
memcpy(&serverAddr.sin_addr.s_addr, server->h_addr, server->h_length);
serverAddr.sin_port = htons(port);
int result = ::connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if (result < 0 && errno != EINPROGRESS) {
LOG_ERROR("Failed to connect: ", strerror(errno));
close(sockfd);
sockfd = -1;
return false;
}
connected = true;
LOG_INFO("Connected to ", host, ":", port);
return true;
}
void TCPSocket::disconnect() {
if (sockfd >= 0) {
close(sockfd);
sockfd = -1;
}
connected = false;
receiveBuffer.clear();
}
void TCPSocket::send(const Packet& packet) {
if (!connected) return;
// Build complete packet with opcode
std::vector<uint8_t> sendData;
// Add opcode (1 byte) - always little-endian, but it's just 1 byte so doesn't matter
sendData.push_back(static_cast<uint8_t>(packet.getOpcode() & 0xFF));
// Add packet data
const auto& data = packet.getData();
sendData.insert(sendData.end(), data.begin(), data.end());
LOG_DEBUG("Sending packet: opcode=0x", std::hex, packet.getOpcode(), std::dec,
" size=", sendData.size(), " bytes");
// Send complete packet
ssize_t sent = ::send(sockfd, sendData.data(), sendData.size(), 0);
if (sent < 0) {
LOG_ERROR("Send failed: ", strerror(errno));
} else if (static_cast<size_t>(sent) != sendData.size()) {
LOG_WARNING("Partial send: ", sent, " of ", sendData.size(), " bytes");
}
}
void TCPSocket::update() {
if (!connected) return;
// Receive data into buffer
uint8_t buffer[4096];
ssize_t received = recv(sockfd, buffer, sizeof(buffer), 0);
if (received > 0) {
LOG_DEBUG("Received ", received, " bytes from server");
receiveBuffer.insert(receiveBuffer.end(), buffer, buffer + received);
// Try to parse complete packets from buffer
tryParsePackets();
}
else if (received == 0) {
LOG_INFO("Connection closed by server");
disconnect();
}
else if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_ERROR("Receive failed: ", strerror(errno));
disconnect();
}
}
void TCPSocket::tryParsePackets() {
// For auth packets, we need at least 1 byte (opcode)
while (receiveBuffer.size() >= 1) {
uint8_t opcode = receiveBuffer[0];
// Determine expected packet size based on opcode
// This is specific to authentication protocol
size_t expectedSize = getExpectedPacketSize(opcode);
if (expectedSize == 0) {
// Unknown opcode or need more data to determine size
LOG_WARNING("Unknown opcode or indeterminate size: 0x", std::hex, (int)opcode, std::dec);
break;
}
if (receiveBuffer.size() < expectedSize) {
// Not enough data yet
LOG_DEBUG("Waiting for more data: have ", receiveBuffer.size(),
" bytes, need ", expectedSize);
break;
}
// We have a complete packet!
LOG_DEBUG("Parsing packet: opcode=0x", std::hex, (int)opcode, std::dec,
" size=", expectedSize, " bytes");
// Create packet from buffer data
std::vector<uint8_t> packetData(receiveBuffer.begin(),
receiveBuffer.begin() + expectedSize);
Packet packet(opcode, packetData);
// Remove parsed data from buffer
receiveBuffer.erase(receiveBuffer.begin(), receiveBuffer.begin() + expectedSize);
// Call callback if set
if (packetCallback) {
packetCallback(packet);
}
}
}
size_t TCPSocket::getExpectedPacketSize(uint8_t opcode) {
// Authentication packet sizes (WoW 3.3.5a)
// Note: These are minimum sizes. Some packets are variable length.
switch (opcode) {
case 0x00: // LOGON_CHALLENGE response
// Need to read second byte to determine success/failure
if (receiveBuffer.size() >= 3) {
uint8_t status = receiveBuffer[2];
if (status == 0x00) {
// Success - need to calculate full size
// Minimum: opcode(1) + unknown(1) + status(1) + B(32) + glen(1) + g(1) + Nlen(1) + N(32) + salt(32) + unk(16) + flags(1)
// With typical values: 1 + 1 + 1 + 32 + 1 + 1 + 1 + 32 + 32 + 16 + 1 = 119 bytes minimum
// But N is usually 256 bytes, so more like: 1 + 1 + 1 + 32 + 1 + 1 + 1 + 256 + 32 + 16 + 1 = 343 bytes
// For safety, let's parse dynamically:
if (receiveBuffer.size() >= 36) { // enough to read g_len
uint8_t gLen = receiveBuffer[35];
size_t minSize = 36 + gLen + 1; // up to N_len
if (receiveBuffer.size() >= minSize) {
uint8_t nLen = receiveBuffer[36 + gLen];
size_t totalSize = 36 + gLen + 1 + nLen + 32 + 16 + 1;
return totalSize;
}
}
return 0; // Need more data
} else {
// Failure - just opcode + unknown + status
return 3;
}
}
return 0; // Need more data to determine
case 0x01: // LOGON_PROOF response
// opcode(1) + status(1) + M2(20) = 22 bytes on success
// opcode(1) + status(1) = 2 bytes on failure
if (receiveBuffer.size() >= 2) {
uint8_t status = receiveBuffer[1];
if (status == 0x00) {
return 22; // Success
} else {
return 2; // Failure
}
}
return 0; // Need more data
case 0x10: // REALM_LIST response
// Variable length - format: opcode(1) + size(2) + payload(size)
// Need to read size field (little-endian uint16 at offset 1-2)
if (receiveBuffer.size() >= 3) {
uint16_t size = receiveBuffer[1] | (receiveBuffer[2] << 8);
// Total packet size is: opcode(1) + size field(2) + payload(size)
return 1 + 2 + size;
}
return 0; // Need more data to read size field
default:
LOG_WARNING("Unknown auth packet opcode: 0x", std::hex, (int)opcode, std::dec);
return 0;
}
}
} // namespace network
} // namespace wowee

View file

@ -0,0 +1,236 @@
#include "network/world_socket.hpp"
#include "network/packet.hpp"
#include "auth/crypto.hpp"
#include "core/logger.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <cstring>
namespace wowee {
namespace network {
// WoW 3.3.5a RC4 encryption keys (hardcoded in client)
static const uint8_t ENCRYPT_KEY[] = {
0xC2, 0xB3, 0x72, 0x3C, 0xC6, 0xAE, 0xD9, 0xB5,
0x34, 0x3C, 0x53, 0xEE, 0x2F, 0x43, 0x67, 0xCE
};
static const uint8_t DECRYPT_KEY[] = {
0xCC, 0x98, 0xAE, 0x04, 0xE8, 0x97, 0xEA, 0xCA,
0x12, 0xDD, 0xC0, 0x93, 0x42, 0x91, 0x53, 0x57
};
WorldSocket::WorldSocket() = default;
WorldSocket::~WorldSocket() {
disconnect();
}
bool WorldSocket::connect(const std::string& host, uint16_t port) {
LOG_INFO("Connecting to world server: ", host, ":", port);
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
LOG_ERROR("Failed to create socket");
return false;
}
// Set non-blocking
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
// Resolve host
struct hostent* server = gethostbyname(host.c_str());
if (server == nullptr) {
LOG_ERROR("Failed to resolve host: ", host);
close(sockfd);
sockfd = -1;
return false;
}
// Connect
struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
memcpy(&serverAddr.sin_addr.s_addr, server->h_addr, server->h_length);
serverAddr.sin_port = htons(port);
int result = ::connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if (result < 0 && errno != EINPROGRESS) {
LOG_ERROR("Failed to connect: ", strerror(errno));
close(sockfd);
sockfd = -1;
return false;
}
connected = true;
LOG_INFO("Connected to world server: ", host, ":", port);
return true;
}
void WorldSocket::disconnect() {
if (sockfd >= 0) {
close(sockfd);
sockfd = -1;
}
connected = false;
encryptionEnabled = false;
receiveBuffer.clear();
LOG_INFO("Disconnected from world server");
}
bool WorldSocket::isConnected() const {
return connected;
}
void WorldSocket::send(const Packet& packet) {
if (!connected) return;
const auto& data = packet.getData();
uint16_t opcode = packet.getOpcode();
uint16_t size = static_cast<uint16_t>(data.size());
// Build header (6 bytes for outgoing): size(2) + opcode(4)
std::vector<uint8_t> sendData;
sendData.reserve(6 + size);
// Size (2 bytes, big-endian) - payload size only, does NOT include header
sendData.push_back((size >> 8) & 0xFF);
sendData.push_back(size & 0xFF);
// Opcode (4 bytes, big-endian)
sendData.push_back((opcode >> 24) & 0xFF);
sendData.push_back((opcode >> 16) & 0xFF);
sendData.push_back((opcode >> 8) & 0xFF);
sendData.push_back(opcode & 0xFF);
// Encrypt header if encryption is enabled
if (encryptionEnabled) {
encryptCipher.process(sendData.data(), 6);
LOG_DEBUG("Encrypted outgoing header: opcode=0x", std::hex, opcode, std::dec);
}
// Add payload (unencrypted)
sendData.insert(sendData.end(), data.begin(), data.end());
LOG_DEBUG("Sending world packet: opcode=0x", std::hex, opcode, std::dec,
" size=", size, " bytes (", sendData.size(), " total)");
// Send complete packet
ssize_t sent = ::send(sockfd, sendData.data(), sendData.size(), 0);
if (sent < 0) {
LOG_ERROR("Send failed: ", strerror(errno));
} else if (static_cast<size_t>(sent) != sendData.size()) {
LOG_WARNING("Partial send: ", sent, " of ", sendData.size(), " bytes");
}
}
void WorldSocket::update() {
if (!connected) return;
// Receive data into buffer
uint8_t buffer[4096];
ssize_t received = recv(sockfd, buffer, sizeof(buffer), 0);
if (received > 0) {
LOG_DEBUG("Received ", received, " bytes from world server");
receiveBuffer.insert(receiveBuffer.end(), buffer, buffer + received);
// Try to parse complete packets from buffer
tryParsePackets();
}
else if (received == 0) {
LOG_INFO("World server connection closed");
disconnect();
}
else if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_ERROR("Receive failed: ", strerror(errno));
disconnect();
}
}
void WorldSocket::tryParsePackets() {
// World server packets have 4-byte incoming header: size(2) + opcode(2)
while (receiveBuffer.size() >= 4) {
// Copy header for decryption
uint8_t header[4];
memcpy(header, receiveBuffer.data(), 4);
// Decrypt header if encryption is enabled
if (encryptionEnabled) {
decryptCipher.process(header, 4);
}
// Parse header (big-endian)
uint16_t size = (header[0] << 8) | header[1];
uint16_t opcode = (header[2] << 8) | header[3];
// Total packet size: header(4) + payload(size)
size_t totalSize = 4 + size;
if (receiveBuffer.size() < totalSize) {
// Not enough data yet
LOG_DEBUG("Waiting for more data: have ", receiveBuffer.size(),
" bytes, need ", totalSize);
break;
}
// We have a complete packet!
LOG_DEBUG("Parsing world packet: opcode=0x", std::hex, opcode, std::dec,
" size=", size, " bytes");
// Extract payload (skip header)
std::vector<uint8_t> packetData(receiveBuffer.begin() + 4,
receiveBuffer.begin() + totalSize);
// Create packet with opcode and payload
Packet packet(opcode, packetData);
// Remove parsed data from buffer
receiveBuffer.erase(receiveBuffer.begin(), receiveBuffer.begin() + totalSize);
// Call callback if set
if (packetCallback) {
packetCallback(packet);
}
}
}
void WorldSocket::initEncryption(const std::vector<uint8_t>& sessionKey) {
if (sessionKey.size() != 40) {
LOG_ERROR("Invalid session key size: ", sessionKey.size(), " (expected 40)");
return;
}
LOG_INFO("Initializing world server header encryption");
// Convert hardcoded keys to vectors
std::vector<uint8_t> encryptKey(ENCRYPT_KEY, ENCRYPT_KEY + 16);
std::vector<uint8_t> decryptKey(DECRYPT_KEY, DECRYPT_KEY + 16);
// Compute HMAC-SHA1(key, sessionKey) for each cipher
std::vector<uint8_t> encryptHash = auth::Crypto::hmacSHA1(encryptKey, sessionKey);
std::vector<uint8_t> decryptHash = auth::Crypto::hmacSHA1(decryptKey, sessionKey);
LOG_DEBUG("Encrypt hash: ", encryptHash.size(), " bytes");
LOG_DEBUG("Decrypt hash: ", decryptHash.size(), " bytes");
// Initialize RC4 ciphers with HMAC results
encryptCipher.init(encryptHash);
decryptCipher.init(decryptHash);
// Drop first 1024 bytes of keystream (WoW protocol requirement)
encryptCipher.drop(1024);
decryptCipher.drop(1024);
encryptionEnabled = true;
LOG_INFO("World server encryption initialized successfully");
}
} // namespace network
} // namespace wowee