summaryrefslogtreecommitdiff
path: root/main.asm
blob: 03148b4808ae23a378489f08fc6a7b618978bedd (plain) (blame)
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
include 'sys.asm'

format ELF64 executable

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 'Server: asmsite', 13, 10
	    db 'Connection: close', 13, 10
	    db 13, 10
            db '<!DOCTYPE html><html><head><title>asmsite</title></head><body>Hello, World!</body></html>', 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'
indexlen dq 0

indexfd dd -1
socketfd dd -1
connfd dd -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

struc timespec
{
  .time dq 0
  .time_nano dd 0
}
sleep_time timespec

segment readable executable

entry _main
_main:
  ls_write STDOUT, open_msg, open_msg.size
  ls_open indexname, O_RDONLY, 0
  mov qword [indexfd], rax
  cmp [indexfd], 0
  jl error

  ls_write STDOUT, read_msg, read_msg.size
  ls_read qword [indexfd], indexbuf, indexbuf_len
  cmp qword rax, 0
  jl error
  mov [indexlen], rax

  ls_write STDOUT, close_msg, close_msg.size
  ls_close qword [indexfd]
  cmp qword rax, 0
  jl error

  ls_write STDOUT, indexname, indexname.size
  ls_write STDOUT, msg_dn, msg_dn.size
  ls_write STDOUT, indexbuf, [indexlen]
  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], 4096
  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

  ;; mov [sleep_time.time], 10
  ;; mov [sleep_time.time_nano], 1000
  ;; ls_nanosleep sleep_time.time, 0

  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