From 06d75ece6452a33f11afd7e5ec3dd1f916c8583f Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 6 Mar 2023 16:19:36 -0400 Subject: TProtocol: Match both `parse_packet()`, and `run_task()` --- src/pso/TProtocol.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++---- src/pso/TSocket.cpp | 13 +++++---- src/pso/TTcpSocket.cpp | 4 +-- 3 files changed, 81 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/pso/TProtocol.cpp b/src/pso/TProtocol.cpp index b0be8ee..d30df7d 100644 --- a/src/pso/TProtocol.cpp +++ b/src/pso/TProtocol.cpp @@ -14,18 +14,84 @@ OBJECT_NAME(TProtocol); -void TProtocol::run_task() { +packet *server_packet = nullptr; + +void copy_packet(struct packet *pkt) { } -int TProtocol::send(u8 *data, size_t size) { - return TTcpSocket::send(data, size); +int TProtocol::handle_command(struct packet *pkt) { + } -int handle_command(packet *pkt) { +void TProtocol::parse_packet() { + // This is needed to match, either that or cast the sizeof to an int. + const int max_packet_size = sizeof(struct packet); + while (!is_empty()) { + m_packet.bytes[m_packet_offset++] = next(); + if (m_packet_offset == sizeof(packet_header)) { + if (m_is_encrypted) { + m_recv_crypt.encrypt(&m_packet.packet.header, sizeof(packet_header)); + } + } + // NOTE: The `const int &` is required here to match. + // Convert packet size to native endian. + const int &packet_size = m_packet_size = to_be_uint16_t(m_packet.packet.header.size); + // Is the packet size invalid? + if (packet_size > max_packet_size || !packet_size) { + m_packet_size = 0; + m_packet_offset = 0; + m_is_invalid_packet = true; + break; + // Do we have the rest of the packet yet? + } else if (m_packet_size > 0 && m_packet_offset >= m_packet_size) { + // Decrypt it if necessary. + if (m_is_encrypted) { + m_recv_crypt.encrypt(&m_packet.packet.data, m_packet_size-sizeof(packet_header)); + } + struct packet *pkt = &m_packet; + m_packet_size = 0; + m_packet_offset = 0; + if (m_handle_pings_only) { + if (pkt->packet.header.command == 0x1d) { + if (!handle_command(pkt)) { + continue; + } else { + break; + } + } else { + copy_packet(pkt); + } + } else if (handle_command(pkt)) { + break; + } + } + } } +short TProtocol::send(u8 *data, size_t size) { + return TTcpSocket::send(data, size); +} + +void TProtocol::run_task() { + TTcpSocket::some_stub(); + some_stub(); + if (m_is_invalid_packet) { + close(); + m_is_encrypted = 0; + m_connected = 0; + m_packet_offset = 0; + m_packet_size = 0; + } else if (!m_buffer_cleared) { + server_packet = &m_packet; + parse_packet(); + recv(); + } +} + +void TProtocol::some_stub() {}; + TProtocol::~TProtocol() { } @@ -56,7 +122,7 @@ TProtocol::TProtocol(TObject *parent, u16 sub_version, int language, char *seria m_connected = 0; m_joined_game = 0; m_has_meet_user_settings = 0; - m_buffer_ready = 0; + m_handle_pings_only = false; m_entry_count = 0; m_unused = 0; m_lobby_list_count = 0; diff --git a/src/pso/TSocket.cpp b/src/pso/TSocket.cpp index 0a0166e..3c5576f 100644 --- a/src/pso/TSocket.cpp +++ b/src/pso/TSocket.cpp @@ -5,8 +5,6 @@ #include "pso/TObject.h" #include "pso/TSocket.h" -u16 bswap_word(u16 val); - int TSocket::is_empty() { if (all_parents_unqueued_for_destruction()) { return !m_size; @@ -25,7 +23,7 @@ const u8 TSocket::next() { } void TSocket::set_port(u32 port) { - m_dst_port = bswap_word(port); + m_dst_port = to_le_uint16_t(port); } void TSocket::set_ip_address(u32 addr) { @@ -42,14 +40,19 @@ TSocket::TSocket(TObject *parent) : TObject(parent) { m_dst_port = 0; m_src_port = 0; m_sock_fd = -1; - m_sock_flags = 0; + m_is_invalid_packet = false; m_size = 0; m_buffer_offset = 0; m_buffer_cleared = true; m_callback = nullptr; } -u16 bswap_word(u16 val) { +u16 to_be_uint16_t(u16 val) { + u8 *ptr = reinterpret_cast(&val); + return ptr[0] + (ptr[1] << 8); +} + +u16 to_le_uint16_t(u16 val) { u8 *ptr = reinterpret_cast(&val); return ptr[1] + (ptr[0] << 8); } diff --git a/src/pso/TTcpSocket.cpp b/src/pso/TTcpSocket.cpp index f105298..e923abb 100644 --- a/src/pso/TTcpSocket.cpp +++ b/src/pso/TTcpSocket.cpp @@ -99,7 +99,7 @@ int TTcpSocket::send(u8 *data, size_t size) { m_send_crypt.encrypt(data, size); } - const s8 flags = sock_flags(); + const s8 flags = is_invalid_packet(); if (flags || !get_link_status()) { return -1; } else { @@ -138,7 +138,7 @@ int TTcpSocket::send(u8 *data, size_t size) { short TTcpSocket::send(u8 *data) { if (sock_fd() != -1) { - const s8 flags = sock_flags(); + const s8 flags = is_invalid_packet(); if (flags || !get_link_status()) { return -1; } else { -- cgit v1.2.3-13-gbd6f