From 4b4c1796e96543eed6962fb7d222baa1379d18a9 Mon Sep 17 00:00:00 2001 From: nemasu Date: Fri, 18 Jan 2019 16:39:24 +0900 Subject: [PATCH 1/4] Custom port. --- bss.asm | 1 + data.asm | 25 ++++++++++++++++--------- http.asm | 22 +++++++++++----------- main.asm | 37 +++++++++++++------------------------ syscall.asm | 6 +++++- 5 files changed, 46 insertions(+), 45 deletions(-) diff --git a/bss.asm b/bss.asm index 1180cf9..6084a83 100644 --- a/bss.asm +++ b/bss.asm @@ -16,4 +16,5 @@ ;You should have received a copy of the GNU General Public License ;along with asmttpd. If not, see . listen_socket: resq 1 + listen_port: resw 1 one_constant: resq 1 diff --git a/data.asm b/data.asm index 961d2fc..69e25d7 100644 --- a/data.asm +++ b/data.asm @@ -16,13 +16,22 @@ ;You should have received a copy of the GNU General Public License ;along with asmttpd. If not, see . - sockaddr_in: ;struct - sin_family dw AF_INET - sin_port dw LISTEN_PORT - sin_addr dd 0 ;INADDR_ANY + struc sockaddr_in + sin_family: resw 1 + sin_port: resw 1 + sin_addr: resd 1 + endstruc + + sa: istruc sockaddr_in + at sin_family, dw AF_INET + at sin_port, dw 0 + at sin_addr, dd 0 ;INADDR_ANY + iend + directory_path dq 0 - request_type dq 0 - request_offset dq 0 + request_type dq 0 + request_offset dq 0 + timeval: ;struct tv_sec dq 0 tv_usec dq 0 @@ -44,10 +53,8 @@ msg_bind_error_len equ $ - msg_bind_error msg_error db "An error has occured, exiting",0x00 msg_error_len equ $ - msg_error - msg_help db "Usage: ./asmttpd /path/to/directory",0x00 + msg_help db "Usage: ./asmttpd /path/to/directory port",0x00 msg_help_len equ $ - msg_help - msg_using_directory dd "Using Document Root: ",0x00 - msg_using_directory_len equ $ - msg_using_directory msg_not_a_directory dd "Error: Specified document root is not a directory",0x00 msg_not_a_directory_len equ $ - msg_not_a_directory msg_request_log db 0x0a,"Request: ",0x00 diff --git a/http.asm b/http.asm index a6dc8d7..36a98fc 100644 --- a/http.asm +++ b/http.asm @@ -349,17 +349,17 @@ create_http404_response: ;rdi - pointer to buffer get_request_type: ;rdi - pointer to buffer, ret = rax: request type stackpush - cld - mov r10, -1 - mov rsi, rdi - find_request_string: - inc r10 - lodsb - cmp al, 0x20 - jne find_request_string - mov rax, r10 - add rax, 0x03 - mov [request_offset], rax + cld + mov r10, -1 + mov rsi, rdi + find_request_string: + inc r10 + lodsb + cmp al, 0x20 + jne find_request_string + mov rax, r10 + add rax, 0x03 + mov [request_offset], rax mov rax, REQ_UNK diff --git a/main.asm b/main.asm index b597511..67ed408 100644 --- a/main.asm +++ b/main.asm @@ -19,23 +19,19 @@ %include "constants.asm" %include "macros.asm" -%define ASMTTPD_VERSION "0.4.2" - -%define LISTEN_PORT 0x5000 ; PORT 80, network byte order +%define ASMTTPD_VERSION "0.4.3" %define THREAD_COUNT 10 ; Number of worker threads ;Follwing amd64 syscall standards for internal function calls: rdi rsi rdx r10 r8 r9 -section .data +section .data %include "data.asm" - + section .bss - %include "bss.asm" section .text - %include "string.asm" %include "http.asm" %include "syscall.asm" @@ -53,22 +49,17 @@ _start: call print_line mov rdi, [rsp] ;Num of args - cmp rdi,2 ;Exit if no argument, should be directory location + cmp rdi,3 ;Exit if no argument, should be directory location jne exit_with_help - + + mov rdi, [rsp+16+8]; Port (second) parameter + call string_atoi + xchg al, ah + mov [listen_port], eax + mov rax, [rsp+16] ;Directory (first) parameter mov [directory_path], rax - - mov rdi, msg_using_directory - mov rsi, msg_using_directory_len - call sys_write - mov rdi, [directory_path] - call get_string_length - mov rsi, rax - call print_line - - ; Register signal handlers ( just close all other threads by jumping to SYS_EXIT_GROUP ) mov r10, 8 ; sizeof(sigset_t) displays 128, but strace shows 8 ... so 8 it is! -_- xor rdx, rdx @@ -85,7 +76,6 @@ _start: mov rax, SYS_RT_SIGACTION syscall - ;Try opening directory mov rdi, [directory_path] call sys_open_directory @@ -191,8 +181,8 @@ worker_thread_continue: mov rdi, [rbp-16] call get_request_type - mov [request_type], rax - cmp BYTE [request_type], REQ_UNK + mov [request_type], rax + cmp BYTE [request_type], REQ_UNK je worker_thread_400_response ;Find request @@ -241,7 +231,7 @@ worker_thread_continue: dec r12 ; get rid of 0x00 ;Check if default document needed - cmp r9, [request_offset] ; Offset of document requested + cmp r9, [request_offset] ; Offset of document requested jne no_default_document mov rsi, default_document mov rdi, [rbp-16] @@ -254,7 +244,6 @@ worker_thread_continue: no_default_document: - ; Adds the file to the end of buffer ( where we juts put the document prefix ) mov rsi, [rbp-16] add rsi, r8 ; points to beginning of path diff --git a/syscall.asm b/syscall.asm index e356381..a5fecd7 100644 --- a/syscall.asm +++ b/syscall.asm @@ -123,8 +123,12 @@ sys_listen: sys_bind_server: stackpush + + mov rsi, [listen_port] + mov [sa + sin_port], rsi + mov rdi, [listen_socket] - mov rsi, sockaddr_in + mov rsi, sa mov rdx, 16 mov rax, SYS_BIND syscall From c447d766aaed7864037d44a9c47d23b5dd3c451c Mon Sep 17 00:00:00 2001 From: nemasu Date: Thu, 24 Jan 2019 13:00:55 +0900 Subject: [PATCH 2/4] Custom port. --- bss.asm | 1 + data.asm | 1 - debug.asm | 4 ++++ main.asm | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bss.asm b/bss.asm index 6084a83..a3e9fea 100644 --- a/bss.asm +++ b/bss.asm @@ -18,3 +18,4 @@ listen_socket: resq 1 listen_port: resw 1 one_constant: resq 1 + directory_path: resq 1 diff --git a/data.asm b/data.asm index 69e25d7..77ff882 100644 --- a/data.asm +++ b/data.asm @@ -28,7 +28,6 @@ at sin_addr, dd 0 ;INADDR_ANY iend - directory_path dq 0 request_type dq 0 request_offset dq 0 diff --git a/debug.asm b/debug.asm index 5d2e9a0..322e475 100644 --- a/debug.asm +++ b/debug.asm @@ -15,6 +15,10 @@ ; ;You should have received a copy of the GNU General Public License ;along with asmttpd. If not, see . + +;hint, use these with GDB +;set follow-fork-mode child + section .bss printbuffer: resb 1024;debug diff --git a/main.asm b/main.asm index 67ed408..504c533 100644 --- a/main.asm +++ b/main.asm @@ -58,7 +58,7 @@ _start: mov [listen_port], eax mov rax, [rsp+16] ;Directory (first) parameter - mov [directory_path], rax + mov [directory_path], rax ; Register signal handlers ( just close all other threads by jumping to SYS_EXIT_GROUP ) mov r10, 8 ; sizeof(sigset_t) displays 128, but strace shows 8 ... so 8 it is! -_- From 8258ccffc8f88aa6cad14325682ad6fb1a6a4e63 Mon Sep 17 00:00:00 2001 From: nemasu Date: Thu, 24 Jan 2019 13:03:32 +0900 Subject: [PATCH 3/4] Custom port. --- main.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.asm b/main.asm index 504c533..041bbfd 100644 --- a/main.asm +++ b/main.asm @@ -93,7 +93,7 @@ _start: mov rdi, [listen_socket] call sys_reuse - ;Bind to port 80 + ;Bind to port call sys_bind_server cmp rax, 0 jl exit_bind_error From f99c1fdafc050e34917face7ae73008e19d55921 Mon Sep 17 00:00:00 2001 From: nemasu Date: Thu, 24 Jan 2019 13:07:50 +0900 Subject: [PATCH 4/4] README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 906da8a..7facadc 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,14 @@ You will need `yasm` installed. Usage ======= -`sudo ./asmttpd /path/to/web_root` +`./asmttpd /path/to/web_root port_number` +Example: `./asmttpd ./web_root 8080` Changes ======= +2019-01-24 : asmttpd - 0.4.3 + +* Added port number as parameter. 2017-10-18 : asmttpd - 0.4.2