-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
143 lines (112 loc) · 2.94 KB
/
main.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
format ELF64 executable
MAX_REQ_LEN equ 1024 * 64
include "syscalls.asm"
segment readable executable
include "utils.asm"
entry main
main:
print STDOUT, socket_info
socket AF_INET, SOCK_STREAM, 0
mov [server_fd], rax
cmp rax, 0
jl fatal
setsockopt [server_fd], SOL_SOCKET, SO_REUSEADDR, enable, 4
cmp rax, 0
jl fatal
setsockopt [server_fd], SOL_SOCKET, SO_REUSEPORT, enable, 4
cmp rax, 0
jl fatal
mov [serveraddr_in.sin_family], word AF_INET
mov [serveraddr_in.sin_addr], dword 0
mov [serveraddr_in.sin_port], word 36895
print STDOUT, binding_info
bind [server_fd], serveraddr_in.sin_family, serveraddr_in.size
cmp rax, 0
jl fatal
print STDOUT, listening_info
listen [server_fd], MAX_BACKLOG
cmp rax, 0
jl fatal
.mainloop:
print STDOUT, accepting_info
accept [server_fd], clientaddr_in, clientaddr_len
mov [client_fd], rax
cmp rax, 0
jl fatal
read [client_fd], req, MAX_REQ_LEN
mov [req_len], rax
cmp rax, 0
jl fatal
mov rdi, req
call trim_first_line
serve_route req, home
serve_route req, projects
serve_route req, about
serve_route req, links
.serve_not_found:
print [client_fd], not_found_page_response
print [client_fd], not_found_page
close [client_fd]
jmp .mainloop
.serve_home:
print [client_fd], found_page_response
print [client_fd], index_page
close [client_fd]
jmp .mainloop
.serve_projects:
print [client_fd], found_page_response
print [client_fd], projects_page
close [client_fd]
jmp .mainloop
.serve_about:
print [client_fd], found_page_response
print [client_fd], about_page
close [client_fd]
jmp .mainloop
.serve_links:
print [client_fd], found_page_response
print [client_fd], links_page
close [client_fd]
jmp .mainloop
print STDOUT, closing_info
close [server_fd]
print STDOUT, done_info
exit EXIT_SUCCESS
fatal:
print STDERR, fatal_error
close [client_fd]
close [server_fd]
exit EXIT_FAILURE
segment readable writable
include "responses.asm"
struc servaddr_in_struc
{
.sin_family dw 0
.sin_port dw 0
.sin_addr dd 0
.sin_zero dq 0
.size = $ - .sin_family
}
;; variables
server_fd dq -1
client_fd dq -1
serveraddr_in servaddr_in_struc
clientaddr_in servaddr_in_struc
clientaddr_len dd clientaddr_in.size
enable dd 1
req rb MAX_REQ_LEN
req_len rq 1
;; debug messages
socket_info db "[INFO] Creating Socket...", 10, 0
binding_info db "[INFO] Binding Socket...", 10, 0
listening_info db "[INFO] Listening...", 10, 0
accepting_info db "[INFO] Accepting Connections...", 10, 0
closing_info db "[INFO] Closing Socket...", 10, 0
done_info db "[INFO] Done!", 10, 0
fatal_error db "[ERROR] Fatal! Cannot Recover.", 10, 0
message db "Hello from fasm!", 10, 0
;; routes
home db "", 0
about db "about", 0
projects db "projects", 0
links db "links", 0