diff options
author | fxqnlr <[email protected]> | 2024-09-20 00:22:43 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-20 00:22:43 +0200 |
commit | 9c5783f030d7c876ccf6e05e8182d88f84a97fc3 (patch) | |
tree | 150f2cccd904d1ed787cc6f413a12960ef26e5d2 /main.asm | |
parent | f0b8e96b51b1a8b19365c9d010639c8d67a42506 (diff) | |
download | asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar.gz asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.zip |
it works, sometimes
Diffstat (limited to 'main.asm')
-rw-r--r-- | main.asm | 75 |
1 files changed, 69 insertions, 6 deletions
@@ -2,26 +2,53 @@ include 'sys.asm' | |||
2 | 2 | ||
3 | format ELF64 executable | 3 | format ELF64 executable |
4 | 4 | ||
5 | segment writable readable | ||
6 | struc db [data] | 5 | struc db [data] |
7 | { | 6 | { |
8 | common | 7 | common |
9 | . db data | 8 | . db data |
10 | .size = $ - . | 9 | .size = $ - . |
11 | } | 10 | } |
11 | |||
12 | segment writable readable | ||
13 | connect_header db 'HTTP/1.1 200 OK', 13, 10 | ||
14 | db 'Content-Type: text/html', 13, 10 | ||
15 | db 'Connection: close', 13, 10 | ||
16 | db 13, 10 | ||
17 | db '<!DOCTYPE html><html><head><title>asmsite</title></head><body>Hello, World!</body></html>', 10 | ||
18 | connect_header_len = $ - connect_header | ||
19 | |||
12 | stat_msg db 'Get file stats', 10 | 20 | stat_msg db 'Get file stats', 10 |
13 | open_msg db 'Open file', 10 | 21 | open_msg db 'Open file', 10 |
14 | read_msg db 'Read file', 10 | 22 | read_msg db 'Read file', 10 |
15 | close_msg db 'Close file', 10 | 23 | close_msg db 'Close file', 10 |
24 | socket_msg db 'Open socket', 10 | ||
25 | bind_msg db 'Bind socket', 10 | ||
26 | listen_msg db 'Listen', 10 | ||
27 | accept_msg db 'Wait for connection', 10 | ||
16 | msg_dn db ':', 10 | 28 | msg_dn db ':', 10 |
17 | 29 | ||
18 | indexname db 'index.html' | 30 | indexname db 'index.html' |
19 | ;; indexstat stat | 31 | indexlen dq 0 |
20 | indexlen rq 1 | 32 | |
33 | indexfd dd -1 | ||
34 | socketfd dd -1 | ||
35 | connfd dd -1 | ||
21 | 36 | ||
22 | indexfd dd 0 | 37 | indexbuf dd 0 |
23 | indexbuf dd ? | 38 | indexbuf_len dq 0 |
24 | indexbuf_len rq 1 | 39 | |
40 | struc servaddr_in | ||
41 | { | ||
42 | .sin_family dw 0 | ||
43 | .sin_port dw 0 | ||
44 | .sin_addr dd 0 | ||
45 | .sin_zero dq 0 | ||
46 | ;; .size = $ - .sin_family | ||
47 | } | ||
48 | serveraddr servaddr_in | ||
49 | serveraddr_len = $ - serveraddr.sin_family | ||
50 | clientaddr servaddr_in | ||
51 | clientaddr_len dd serveraddr_len | ||
25 | 52 | ||
26 | segment readable executable | 53 | segment readable executable |
27 | 54 | ||
@@ -50,10 +77,46 @@ _main: | |||
50 | cmp qword rax, 0 | 77 | cmp qword rax, 0 |
51 | jl error | 78 | jl error |
52 | 79 | ||
80 | ls_write STDOUT, socket_msg, socket_msg.size | ||
81 | ls_socket AF_INET, SOCK_STREAM, 0 | ||
82 | mov qword [socketfd], rax | ||
83 | cmp [socketfd], 0 | ||
84 | jl error | ||
85 | |||
86 | ls_write STDOUT, bind_msg, bind_msg.size | ||
87 | mov [serveraddr.sin_family], AF_INET | ||
88 | mov [serveraddr.sin_port], 0xb80b | ||
89 | mov [serveraddr.sin_addr], INADDR_ANY | ||
90 | ls_bind qword [socketfd], serveraddr, serveraddr_len | ||
91 | cmp rax, 0 | ||
92 | jl error | ||
53 | 93 | ||
94 | ls_write STDOUT, listen_msg, listen_msg.size | ||
95 | ls_listen qword [socketfd], 5 | ||
96 | cmp rax, 0 | ||
97 | jl error | ||
98 | |||
99 | listen: | ||
100 | ls_write STDOUT, accept_msg, accept_msg.size | ||
101 | ls_accept qword [socketfd], clientaddr, clientaddr_len | ||
102 | cmp rax, 0 | ||
103 | jl error | ||
54 | 104 | ||
105 | mov qword [connfd], rax | ||
106 | |||
107 | ls_write qword [connfd], connect_header, connect_header_len | ||
108 | |||
109 | ls_close qword [connfd] | ||
110 | |||
111 | jmp listen | ||
112 | |||
113 | ls_close qword [indexfd] | ||
114 | ls_close qword [socketfd] | ||
115 | ls_close qword [connfd] | ||
55 | ls_exit 0 | 116 | ls_exit 0 |
56 | 117 | ||
57 | error: | 118 | error: |
58 | ls_close qword [indexfd] | 119 | ls_close qword [indexfd] |
120 | ls_close qword [socketfd] | ||
121 | ls_close qword [connfd] | ||
59 | ls_exit 1 | 122 | ls_exit 1 |