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 | |
parent | f0b8e96b51b1a8b19365c9d010639c8d67a42506 (diff) | |
download | asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar.gz asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.zip |
it works, sometimes
-rwxr-xr-x | debug | bin | 0 -> 15552 bytes | |||
-rw-r--r-- | main.asm | 75 | ||||
-rw-r--r-- | sys.asm | 38 |
3 files changed, 82 insertions, 31 deletions
Binary files differ | |||
@@ -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 |
@@ -6,6 +6,10 @@ O_RDONLY equ 0 | |||
6 | O_WRONLY equ 1 | 6 | O_WRONLY equ 1 |
7 | O_RDWR equ 2 | 7 | O_RDWR equ 2 |
8 | 8 | ||
9 | AF_INET equ 2 | ||
10 | SOCK_STREAM equ 1 | ||
11 | INADDR_ANY equ 0 | ||
12 | |||
9 | macro scall1 v_rax, v_rdi | 13 | macro scall1 v_rax, v_rdi |
10 | { | 14 | { |
11 | mov rax, v_rax | 15 | mov rax, v_rax |
@@ -38,31 +42,15 @@ macro ls_open filename, flags, mode { scall3 2, filename, flags, mode } | |||
38 | 42 | ||
39 | macro ls_close fd { scall1 3, fd } | 43 | macro ls_close fd { scall1 3, fd } |
40 | 44 | ||
41 | ;; kernel: arch/x86/include/uapi/asm/stat.h | 45 | ;; macro ls_stat filename, statbuf { scall2 4, filename, statbuf } |
42 | ;; struc stat | 46 | |
43 | ;; { | 47 | macro ls_socket family, type, protocol { scall3 41, family, type, protocol } |
44 | ;; .st_dev rd 1 | 48 | |
45 | ;; .st_ino rd 1 | 49 | macro ls_accept fd, upeer_sockaddr, upeer_addrlen { scall3 43, fd, upeer_sockaddr, upeer_addrlen } |
46 | ;; .st_mode rw 1 | 50 | |
47 | ;; .st_nlink rw 1 | 51 | macro ls_bind fd, umyaddr, addrlen { scall3 49, fd, umyaddr, addrlen } |
48 | ;; .st_uid rw 1 | 52 | |
49 | ;; .st_gid rw 1 | 53 | macro ls_listen fd, backlog { scall2 50, fd, backlog } |
50 | ;; .st_rdev rd 1 | ||
51 | ;; .st_size rd 1 | ||
52 | ;; .st_blksize rd 1 | ||
53 | ;; .st_blocks rd 1 | ||
54 | ;; | ||
55 | ;; .st_atime rd 1 | ||
56 | ;; .st_atime_nsec rd 1 | ||
57 | ;; .st_mtime rd 1 | ||
58 | ;; .st_mtime_nsec rd 1 | ||
59 | ;; .st_ctime rd 1 | ||
60 | ;; .st_ctime_nsec rd 1 | ||
61 | ;; | ||
62 | ;; .__unused4 rd 1 | ||
63 | ;; .__unused5 rd 1 | ||
64 | ;; } | ||
65 | macro ls_stat filename, statbuf { scall2 4, filename, statbuf } | ||
66 | 54 | ||
67 | macro ls_exit status { scall1 60, status } | 55 | macro ls_exit status { scall1 60, status } |
68 | 56 | ||