From 7b4c890307fbc27737be683cd32c8c13d19beafa Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Mon, 29 Jul 2024 11:02:36 +0800 Subject: [PATCH] Naive incomplete chunk header packer --- src/chunks.asm | 79 ++++++++++++++++++++++++++++++++++ src/data/logger.asm | 3 ++ src/data/teeworlds.asm | 3 ++ src/data/teeworlds_strings.asm | 16 +++++++ src/macros.asm | 34 +++++++++++++++ src/teeworlds_asmr.asm | 17 +------- tests/assert.asm | 2 + tests/chunk_test.asm | 20 +++++++++ 8 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 src/chunks.asm create mode 100644 src/data/teeworlds_strings.asm create mode 100644 tests/chunk_test.asm diff --git a/src/chunks.asm b/src/chunks.asm new file mode 100644 index 0000000..23a1593 --- /dev/null +++ b/src/chunks.asm @@ -0,0 +1,79 @@ +pack_chunk_header: + ; pack_chunk_header [rax] [rdi] [rsi] + ; rax = flags + ; rdi = size + ; rsi = output buffer + ; + ; example: + ; + ; mov rax, 0 + ; set_rax_flag CHUNKFLAG_VITAL + ; mov rdi, 10 + ; mov rsi, my_chunk_buffer + ; call pack_chunk_header + ; + + push rax + + cmp rdi, 63 + jg .pack_chunk_header_error_size + + push rax + mov dword rax, [connection_sequence] + cmp rax, 255 + jg .pack_chunk_header_error_seq + pop rax + + ; Chunk header (vital) + ; +---------+---------+-----------------+--------+-----------------+ + ; | Flags | Size | Sequence number | Size | Sequence number | + ; | 2 bits | 6 bits | 2 bits | 6 bits | 8 bits | + ; +---------+---------+-----------------+--------+-----------------+ + + ; flags + mov [rsi], al + ; size (only in the 2nd byte for now) + mov [rsi+1], dil + + ; sequence only included if it is a vital chunk + is_rax_flag CHUNKFLAG_VITAL + jz .pack_chunk_header_end + + ; sequence (only in the 3rd byte for now) + mov byte al, [connection_sequence] + mov [rsi+2], al + + jmp .pack_chunk_header_end + +.pack_chunk_header_error_seq: + print s_unsupported_seq_size + mov rax, [connection_sequence] + call print_uint32 + exit 1 + +.pack_chunk_header_error_size: + print s_unsupported_chunk_size + mov rax, rdi + call print_uint32 + exit 1 + +.pack_chunk_header_end: + pop rax + ret + +queue_chunk: + ; queue_chunk [rax] [rdi] [rsi] [rdx] + ; rax = system (1=system 0=game) + ; rdi = payload + ; rsi = payload size + + ; resend flag is not supported yet + + push_registers + + ; increment if vital + ; connection_sequence + + pop_registers + ret + diff --git a/src/data/logger.asm b/src/data/logger.asm index 812ff85..4d26a82 100644 --- a/src/data/logger.asm +++ b/src/data/logger.asm @@ -3,3 +3,6 @@ char_space db 0x20 s_dbg_rax_digit db "[debug] value of rax is: ", 0 l_s_dbg_rax_digit equ $ - s_dbg_rax_digit + +s_dbg_reg_digit db "[debug] value of register is: ", 0 +l_s_dbg_reg_digit equ $ - s_dbg_reg_digit diff --git a/src/data/teeworlds.asm b/src/data/teeworlds.asm index e4bf51b..6ff3fb3 100644 --- a/src/data/teeworlds.asm +++ b/src/data/teeworlds.asm @@ -21,6 +21,9 @@ PACKETFLAG_RESEND equ 0b00_0010_00 PACKETFLAG_COMPRESSION equ 0b00_0100_00 PACKETFLAG_CONNLESS equ 0b00_1000_00 +CHUNKFLAG_VITAL equ 0b0100_0000 +CHUNKFLAG_RESEND equ 0b1000_0000 + token db 0xDD, 0xDD, 0xCC, 0xCC peer_token db 0xFF, 0xFF, 0xFF, 0xFF diff --git a/src/data/teeworlds_strings.asm b/src/data/teeworlds_strings.asm new file mode 100644 index 0000000..f2268e0 --- /dev/null +++ b/src/data/teeworlds_strings.asm @@ -0,0 +1,16 @@ +s_got_peer_token db "[client] got peer token: " +l_s_got_peer_token equ $ - s_got_peer_token +s_got_accept db "[client] got accept", 0x0a +l_s_got_accept equ $ - s_got_accept +s_got_ctrl_msg db "[client] got ctrl msg: " +l_s_got_ctrl_msg equ $ - s_got_ctrl_msg +s_unknown_ctrl_msg db "[client] unknown ctrl msg: " +l_s_unknown_ctrl_msg equ $ - s_unknown_ctrl_msg +s_got_packet_with_chunks db "[client] got packet with chunks: " +l_s_got_packet_with_chunks equ $ - s_got_packet_with_chunks +s_unhandled_packet db "[client] UNHANDLED PACKET!!" +l_s_unhandled_packet equ $ - s_unhandled_packet +s_unsupported_chunk_size db "[error] chunk sizes higher than 63 are not supported yet. got size: " +l_s_unsupported_chunk_size equ $ - s_unsupported_chunk_size +s_unsupported_seq_size db "[error] chunk sequence numbers higher than 255 are not supported yet. got sequence number: " +l_s_unsupported_seq_size equ $ - s_unsupported_seq_size diff --git a/src/macros.asm b/src/macros.asm index bd62e58..57c8841 100644 --- a/src/macros.asm +++ b/src/macros.asm @@ -29,6 +29,40 @@ pop_registers %endmacro +%macro is_rax_flag 1 + ; is_rax_flag [teeworlds bit flag constant] + ; + ; example: + ; + ; mov rax, [udp_recv_buf] + ; is_rax_flag PACKETFLAG_CONTROL + ; jnz on_ctrl_message + ; + ; no idea if this "if statement" is correct + push rax + and al, %1 + cmp al, 0 + pop rax +%endmacro + +%macro set_rax_flag 1 + ; set_rax_flag [teeworlds bit flag constant] + ; + ; sets a bit flag + ; + ; example: + ; + ; mov rax, 0 + ; set_rax_flag CHUNKFLAG_VITAL + ; + push rcx + + mov cl, %1 + or al, cl + + pop rcx +%endmacro + %macro push_registers 0 push rax push rbx diff --git a/src/teeworlds_asmr.asm b/src/teeworlds_asmr.asm index 89d66be..acc66b0 100644 --- a/src/teeworlds_asmr.asm +++ b/src/teeworlds_asmr.asm @@ -87,6 +87,7 @@ section .data %include "src/data/posix.asm" %include "src/data/syscalls.asm" %include "src/data/teeworlds.asm" + %include "src/data/teeworlds_strings.asm" %include "src/data/terminal.asm" %include "src/data/logger.asm" %include "src/data/hex.asm" @@ -113,8 +114,6 @@ section .data l_s_you_pressed_a equ $ - s_you_pressed_a s_you_pressed_d db "you pressed d",0x0a l_s_you_pressed_d equ $ - s_you_pressed_d - s_dbg_reg_digit db "[debug] value of register is: ", 0 - l_s_dbg_reg_digit equ $ - s_dbg_reg_digit s_got_file_desc db "got file descriptor: " l_s_got_file_desc equ $ - s_got_file_desc s_got_udp db "[client] got udp: " @@ -130,20 +129,6 @@ section .data s_packer_size db "[packer] amount of bytes packed: " l_s_packer_size equ $ - s_packer_size - ; teeworlds strings - s_got_peer_token db "[client] got peer token: " - l_s_got_peer_token equ $ - s_got_peer_token - s_got_accept db "[client] got accept", 0x0a - l_s_got_accept equ $ - s_got_accept - s_got_ctrl_msg db "[client] got ctrl msg: " - l_s_got_ctrl_msg equ $ - s_got_ctrl_msg - s_unknown_ctrl_msg db "[client] unknown ctrl msg: " - l_s_unknown_ctrl_msg equ $ - s_unknown_ctrl_msg - s_got_packet_with_chunks db "[client] got packet with chunks: " - l_s_got_packet_with_chunks equ $ - s_got_packet_with_chunks - s_unhandled_packet db "[client] UNHANDLED PACKET!!" - l_s_unhandled_packet equ $ - s_unhandled_packet - section .bss %include "src/bss/hex.asm" %include "src/bss/teeworlds.asm" diff --git a/tests/assert.asm b/tests/assert.asm index 3da5d68..48e9e29 100644 --- a/tests/assert.asm +++ b/tests/assert.asm @@ -5,6 +5,8 @@ section .data %include "src/data/syscalls.asm" %include "src/data/logger.asm" %include "src/data/hex.asm" + %include "src/data/teeworlds.asm" + %include "src/data/teeworlds_strings.asm" s_assert_ok db "[assert] OK", 0x0a l_s_assert_ok equ $ - s_assert_ok diff --git a/tests/chunk_test.asm b/tests/chunk_test.asm new file mode 100644 index 0000000..9cb490c --- /dev/null +++ b/tests/chunk_test.asm @@ -0,0 +1,20 @@ +%include "tests/assert.asm" +%include "src/chunks.asm" + +_start: + mov dword [connection_sequence], 9 + + mov rax, 0 + set_rax_flag CHUNKFLAG_VITAL + mov rdi, 6 + mov rsi, assert_actual_buf + call pack_chunk_header + + mov al, [assert_actual_buf] + assert_al_eq 0x40 + mov al, [assert_actual_buf + 1] + assert_al_eq 0x06 + mov al, [assert_actual_buf + 2] + assert_al_eq 0x09 + exit 0 +