From 9c5783f030d7c876ccf6e05e8182d88f84a97fc3 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 20 Sep 2024 00:22:43 +0200 Subject: it works, sometimes --- main.asm | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 6 deletions(-) (limited to 'main.asm') diff --git a/main.asm b/main.asm index 7644482..8c19651 100644 --- a/main.asm +++ b/main.asm @@ -2,26 +2,53 @@ include 'sys.asm' format ELF64 executable -segment writable readable struc db [data] { common . db data .size = $ - . } + +segment writable readable +connect_header db 'HTTP/1.1 200 OK', 13, 10 + db 'Content-Type: text/html', 13, 10 + db 'Connection: close', 13, 10 + db 13, 10 + db 'asmsiteHello, World!', 10 +connect_header_len = $ - connect_header + stat_msg db 'Get file stats', 10 open_msg db 'Open file', 10 read_msg db 'Read file', 10 close_msg db 'Close file', 10 +socket_msg db 'Open socket', 10 +bind_msg db 'Bind socket', 10 +listen_msg db 'Listen', 10 +accept_msg db 'Wait for connection', 10 msg_dn db ':', 10 indexname db 'index.html' -;; indexstat stat -indexlen rq 1 +indexlen dq 0 + +indexfd dd -1 +socketfd dd -1 +connfd dd -1 -indexfd dd 0 -indexbuf dd ? -indexbuf_len rq 1 +indexbuf dd 0 +indexbuf_len dq 0 + +struc servaddr_in +{ + .sin_family dw 0 + .sin_port dw 0 + .sin_addr dd 0 + .sin_zero dq 0 + ;; .size = $ - .sin_family +} +serveraddr servaddr_in +serveraddr_len = $ - serveraddr.sin_family +clientaddr servaddr_in +clientaddr_len dd serveraddr_len segment readable executable @@ -50,10 +77,46 @@ _main: cmp qword rax, 0 jl error + ls_write STDOUT, socket_msg, socket_msg.size + ls_socket AF_INET, SOCK_STREAM, 0 + mov qword [socketfd], rax + cmp [socketfd], 0 + jl error + + ls_write STDOUT, bind_msg, bind_msg.size + mov [serveraddr.sin_family], AF_INET + mov [serveraddr.sin_port], 0xb80b + mov [serveraddr.sin_addr], INADDR_ANY + ls_bind qword [socketfd], serveraddr, serveraddr_len + cmp rax, 0 + jl error + ls_write STDOUT, listen_msg, listen_msg.size + ls_listen qword [socketfd], 5 + cmp rax, 0 + jl error + +listen: + ls_write STDOUT, accept_msg, accept_msg.size + ls_accept qword [socketfd], clientaddr, clientaddr_len + cmp rax, 0 + jl error + mov qword [connfd], rax + + ls_write qword [connfd], connect_header, connect_header_len + + ls_close qword [connfd] + + jmp listen + + ls_close qword [indexfd] + ls_close qword [socketfd] + ls_close qword [connfd] ls_exit 0 error: ls_close qword [indexfd] + ls_close qword [socketfd] + ls_close qword [connfd] ls_exit 1 -- cgit v1.2.3