Skip to content

Commit

Permalink
Merge pull request #43 from nemasu/custom_port
Browse files Browse the repository at this point in the history
Custom port
  • Loading branch information
nemasu authored Jan 24, 2019
2 parents 524648a + f99c1fd commit 1f48a92
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions bss.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.
listen_socket: resq 1
listen_port: resw 1
one_constant: resq 1
directory_path: resq 1
26 changes: 16 additions & 10 deletions data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

sockaddr_in: ;struct
sin_family dw AF_INET
sin_port dw LISTEN_PORT
sin_addr dd 0 ;INADDR_ANY
directory_path dq 0
request_type dq 0
request_offset dq 0
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

request_type dq 0
request_offset dq 0
timeval: ;struct
tv_sec dq 0
tv_usec dq 0
Expand All @@ -44,10 +52,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
Expand Down
4 changes: 4 additions & 0 deletions debug.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
;
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

;hint, use these with GDB
;set follow-fork-mode child

section .bss
printbuffer: resb 1024;debug

Expand Down
22 changes: 11 additions & 11 deletions http.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 15 additions & 26 deletions main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 [directory_path], rax
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
Expand All @@ -85,7 +76,6 @@ _start:
mov rax, SYS_RT_SIGACTION
syscall

;Try opening directory
mov rdi, [directory_path]
call sys_open_directory
Expand All @@ -103,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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f48a92

Please sign in to comment.