From 115fe1992e2c50fd108f7bc7cc31d7d2b1f5c6b8 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Sun, 26 Feb 2023 19:20:47 -0400 Subject: TSocket: Start work on, and match most of `TSocket` Still need to implement, and match `resolve_domain()`, but it's a good starting point for decompiling the netcode. --- context.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++- include/pso/TSocket.h | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/pso/forward.h | 5 +-- obj_files.mk | 1 + src/pso/TSocket.cpp | 59 +++++++++++++++++++++++++++++++++++ 5 files changed, 226 insertions(+), 5 deletions(-) create mode 100644 include/pso/TSocket.h create mode 100644 src/pso/TSocket.cpp diff --git a/context.h b/context.h index f42ba4a..007aa61 100644 --- a/context.h +++ b/context.h @@ -485,6 +485,13 @@ struct packet_header { void bswap(); } __packed__; +// pso/TSocket.h +// Union defs. +union ipv4_addr { + u32 addr; + TArray addr_bytes; +}; + // pso/TObject.h // Enum defs. enum object_flags { @@ -516,8 +523,79 @@ static inline void operator^=(object_flags &a, object_flags b) { a = a ^ b; }; static inline void operator&=(object_flags &a, object_flags b) { a = a & b; }; static inline void operator|=(object_flags &a, object_flags b) { a = a | b; }; -// pso/TPlyGuildCardTag.h +// pso/TSocket.h // Class defs. +class TSocket : public TObject { +private: + void set_flags(u8 flags) { + m_sock_flags |= flags; + }; + + void clear_flags(u8 flags) { + m_sock_flags &= ~flags; + }; + + void toggle_flags(u8 flags) { + m_sock_flags ^= flags; + }; + + u8 get_flags(u8 flags) { + return m_sock_flags & flags; + }; + + ipv4_addr m_dst_addr; + u16 m_dst_port; + u16 m_src_port; + ipv4_addr m_src_addr; + s16 m_sock_fd; + u8 m_sock_flags; + bool m_buffer_cleared; + s16 m_size; + s16 m_buffer_offset; + u32 m_unused; + TArray m_unused2; + TArray m_packet_buffer; + s16 m_stat; + u16 m_unused3; + u32 m_send_window; + u32 m_recv_window; + void (*m_callback)(TSocket *socket); +public: + TSocket(TObject *parent); + virtual ~TSocket(); + + virtual int open() = 0; + virtual int close() = 0; + virtual void recv() = 0; + virtual int send(u8 *data) = 0; + virtual int send(u8 *data, size_t size) = 0; + + int resolve_domain(char *domain); + void set_ip_address(u32 addr); + void set_port(u32 port); + const u8 next(); + int is_empty(); + + PRIVATE_MEMBER_ACCESSORS(ipv4_addr, dst_addr); + PRIVATE_MEMBER_ACCESSORS(u16, dst_port); + PRIVATE_MEMBER_ACCESSORS(u16, src_port); + PRIVATE_MEMBER_ACCESSORS(ipv4_addr, src_addr); + PRIVATE_MEMBER_ACCESSORS(s16, sock_fd); + PRIVATE_MEMBER_ACCESSORS(u8, sock_flags); + PRIVATE_MEMBER_ACCESSORS(bool, buffer_cleared); + PRIVATE_MEMBER_ACCESSORS(s16, size); + PRIVATE_MEMBER_ACCESSORS(s16, buffer_offset); + PRIVATE_MEMBER_ACCESSORS(u32, unused); + PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, unused2, 64); + PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, packet_buffer, 2048); + PRIVATE_MEMBER_ACCESSORS(s16, stat); + PRIVATE_MEMBER_ACCESSORS(u16, unused3); + PRIVATE_MEMBER_ACCESSORS(u32, send_window); + PRIVATE_MEMBER_ACCESSORS(u32, recv_window); + PRIVATE_MEMBER_ACCESSORS_FUNC(void, callback, TSocket *socket); +}; + +// pso/TPlyGuildCardTag.h class TPlyGuildCardTag { public: u8 tag0; diff --git a/include/pso/TSocket.h b/include/pso/TSocket.h new file mode 100644 index 0000000..1b571d9 --- /dev/null +++ b/include/pso/TSocket.h @@ -0,0 +1,86 @@ +#ifndef TSOCKET_H +#define TSOCKET_H + +#include +#include +#include +#include +#include +#include + +union ipv4_addr { + u32 addr; + TArray addr_bytes; +}; + +class TSocket : public TObject { +private: + void set_flags(u8 flags) { + m_sock_flags |= flags; + }; + + void clear_flags(u8 flags) { + m_sock_flags &= ~flags; + }; + + void toggle_flags(u8 flags) { + m_sock_flags ^= flags; + }; + + u8 get_flags(u8 flags) { + return m_sock_flags & flags; + }; + + ipv4_addr m_dst_addr; + u16 m_dst_port; + u16 m_src_port; + ipv4_addr m_src_addr; + s16 m_sock_fd; + u8 m_sock_flags; + bool m_buffer_cleared; + s16 m_size; + s16 m_buffer_offset; + u32 m_unused; + TArray m_unused2; + TArray m_packet_buffer; + s16 m_stat; + u16 m_unused3; + u32 m_send_window; + u32 m_recv_window; + void (*m_callback)(TSocket *socket); +public: + TSocket(TObject *parent); + virtual ~TSocket(); + + virtual int open() = 0; + virtual int close() = 0; + virtual void recv() = 0; + virtual int send(u8 *data) = 0; + virtual int send(u8 *data, size_t size) = 0; + + int resolve_domain(char *domain); + void set_ip_address(u32 addr); + void set_port(u32 port); + const u8 next(); + int is_empty(); + + PRIVATE_MEMBER_ACCESSORS(ipv4_addr, dst_addr); + PRIVATE_MEMBER_ACCESSORS(u16, dst_port); + PRIVATE_MEMBER_ACCESSORS(u16, src_port); + PRIVATE_MEMBER_ACCESSORS(ipv4_addr, src_addr); + PRIVATE_MEMBER_ACCESSORS(s16, sock_fd); + PRIVATE_MEMBER_ACCESSORS(u8, sock_flags); + PRIVATE_MEMBER_ACCESSORS(bool, buffer_cleared); + PRIVATE_MEMBER_ACCESSORS(s16, size); + PRIVATE_MEMBER_ACCESSORS(s16, buffer_offset); + PRIVATE_MEMBER_ACCESSORS(u32, unused); + PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, unused2, 64); + PRIVATE_MEMBER_ACCESSORS_ARRAY(u8, packet_buffer, 2048); + PRIVATE_MEMBER_ACCESSORS(s16, stat); + PRIVATE_MEMBER_ACCESSORS(u16, unused3); + PRIVATE_MEMBER_ACCESSORS(u32, send_window); + PRIVATE_MEMBER_ACCESSORS(u32, recv_window); + PRIVATE_MEMBER_ACCESSORS_FUNC(void, callback, TSocket *socket); +}; + +#endif diff --git a/include/pso/forward.h b/include/pso/forward.h index ec4d77a..165b9ad 100644 --- a/include/pso/forward.h +++ b/include/pso/forward.h @@ -1,10 +1,7 @@ #ifndef FORWARD_H #define FORWARD_H -/*template -class TMenuList; -template -class TMenuListEntry;*/ +class TSocket; class THeap; class TObject; class TMainTask; diff --git a/obj_files.mk b/obj_files.mk index d76a0e5..2098c26 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -1,5 +1,6 @@ O_FILES := $(BUILD_DIR)/src/main.o \ $(BUILD_DIR)/src/pso/TPlyGuildCardTag.o \ + $(BUILD_DIR)/src/pso/TSocket.o \ $(BUILD_DIR)/src/pso/THeap.o \ $(BUILD_DIR)/src/pso/protocol.o \ $(BUILD_DIR)/src/pso/TMainTask.o \ diff --git a/src/pso/TSocket.cpp b/src/pso/TSocket.cpp new file mode 100644 index 0000000..f9e954d --- /dev/null +++ b/src/pso/TSocket.cpp @@ -0,0 +1,59 @@ +#include +#include +#include "pso/macros.h" +#include "pso/TArray.h" +#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; + } else { + return true; + } +} + +const u8 TSocket::next() { + const u8 val = m_packet_buffer[m_buffer_offset++]; + if (m_buffer_offset >= m_size) { + m_buffer_offset = 0; + m_size = 0; + } + return val; +} + +void TSocket::set_port(u32 port) { + m_dst_port = bswap_word(port); +} + +void TSocket::set_ip_address(u32 addr) { + m_dst_addr.addr_bytes.fast_copy_reverse(addr); +} + +int TSocket::resolve_domain(char *domain) { + /* TODO: Implement, and match this. */ + return 0; +} + +TSocket::TSocket(TObject *parent) : TObject(parent) { + m_dst_addr.addr_bytes.fast_fill_with(0); + m_dst_port = 0; + m_src_port = 0; + m_sock_fd = -1; + m_sock_flags = 0; + m_size = 0; + m_buffer_offset = 0; + m_buffer_cleared = true; + m_callback = nullptr; +} + +u16 bswap_word(u16 val) { + u8 *ptr = reinterpret_cast(&val); + return ptr[1] + (ptr[0] << 8); +} + +TSocket::~TSocket() { + +} -- cgit v1.2.3-13-gbd6f