From 9c5783f030d7c876ccf6e05e8182d88f84a97fc3 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 20 Sep 2024 00:22:43 +0200 Subject: it works, sometimes --- debug | Bin 0 -> 15552 bytes main.asm | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- sys.asm | 38 +++++++++++--------------------- 3 files changed, 82 insertions(+), 31 deletions(-) create mode 100755 debug diff --git a/debug b/debug new file mode 100755 index 0000000..a0634a1 Binary files /dev/null and b/debug differ 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 diff --git a/sys.asm b/sys.asm index b9ab190..a237890 100644 --- a/sys.asm +++ b/sys.asm @@ -6,6 +6,10 @@ O_RDONLY equ 0 O_WRONLY equ 1 O_RDWR equ 2 +AF_INET equ 2 +SOCK_STREAM equ 1 +INADDR_ANY equ 0 + macro scall1 v_rax, v_rdi { mov rax, v_rax @@ -38,31 +42,15 @@ macro ls_open filename, flags, mode { scall3 2, filename, flags, mode } macro ls_close fd { scall1 3, fd } -;; kernel: arch/x86/include/uapi/asm/stat.h -;; struc stat -;; { -;; .st_dev rd 1 -;; .st_ino rd 1 -;; .st_mode rw 1 -;; .st_nlink rw 1 -;; .st_uid rw 1 -;; .st_gid rw 1 -;; .st_rdev rd 1 -;; .st_size rd 1 -;; .st_blksize rd 1 -;; .st_blocks rd 1 -;; -;; .st_atime rd 1 -;; .st_atime_nsec rd 1 -;; .st_mtime rd 1 -;; .st_mtime_nsec rd 1 -;; .st_ctime rd 1 -;; .st_ctime_nsec rd 1 -;; -;; .__unused4 rd 1 -;; .__unused5 rd 1 -;; } -macro ls_stat filename, statbuf { scall2 4, filename, statbuf } +;; macro ls_stat filename, statbuf { scall2 4, filename, statbuf } + +macro ls_socket family, type, protocol { scall3 41, family, type, protocol } + +macro ls_accept fd, upeer_sockaddr, upeer_addrlen { scall3 43, fd, upeer_sockaddr, upeer_addrlen } + +macro ls_bind fd, umyaddr, addrlen { scall3 49, fd, umyaddr, addrlen } + +macro ls_listen fd, backlog { scall2 50, fd, backlog } macro ls_exit status { scall1 60, status } -- cgit v1.2.3