-
Notifications
You must be signed in to change notification settings - Fork 768
/
server.asm
125 lines (102 loc) · 4.64 KB
/
server.asm
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
section .data
listen_sock equ 3 ; File descriptor for listening socket
conn_sock equ 4 ; File descriptor for connection socket
backlog equ 5 ; Maximum length to which the queue of pending connections may grow
buf_size equ 1024 ; Buffer size for receiving data
crlf db 0x0D, 0x0A ; Carriage return, Line feed
response db 'HTTP/1.1 200 OK', crlf
db 'Content-Type: text/plain', crlf
db '', crlf
db 'Hello, World!', crlf
db 0 ; Null terminator
section .bss
addr resb 16 ; Buffer for storing remote address
buf resb buf_size ; Buffer for receiving data
section .text
global _start
_start:
; Create a socket
mov eax, 102 ; sys_socketcall syscall number
mov ebx, 1 ; socketcall: SYS_SOCKET
mov ecx, 1 ; AF_INET: IPv4 protocol family
mov edx, 1 ; SOCK_STREAM: TCP socket type
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
mov dword [listen_sock], eax ; Store the listening socket file descriptor
; Bind the socket to an address and port
; (Assuming binding to port 8080 and any available address)
mov eax, 102 ; sys_socketcall syscall number
mov ebx, 2 ; socketcall: SYS_BIND
mov ecx, dword [listen_sock] ; Socket file descriptor
lea edx, [addr] ; Pointer to the sockaddr_in structure
mov esi, 16 ; Size of the sockaddr_in structure
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
; Listen for incoming connections
mov eax, 106 ; sys_socketcall syscall number
mov ebx, 4 ; socketcall: SYS_LISTEN
mov ecx, dword [listen_sock] ; Socket file descriptor
mov edx, backlog ; Maximum length to which the queue of pending connections may grow
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
accept_loop:
; Accept incoming connections
mov eax, 102 ; sys_socketcall syscall number
mov ebx, 5 ; socketcall: SYS_ACCEPT
mov ecx, dword [listen_sock] ; Socket file descriptor
lea edx, [addr] ; Pointer to the sockaddr_in structure to store the remote address
lea esi, [conn_sock] ; Pointer to store the new connection socket file descriptor
mov edi, 16 ; Size of the sockaddr_in structure
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
; Read data from the client
mov eax, 3 ; sys_read syscall number
mov ebx, dword [conn_sock] ; Connection socket file descriptor
lea ecx, [buf] ; Buffer to store received data
mov edx, buf_size ; Maximum number of bytes to read
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
; Send the response back to the client
mov eax, 4 ; sys_write syscall number
mov ebx, dword [conn_sock] ; Connection socket file descriptor
lea ecx, [response] ; Response buffer
mov edx, response_len ; Length of the response
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
; Close the connection socket
mov eax, 6 ; sys_close syscall number
mov ebx, dword [conn_sock] ; Connection socket file descriptor
int 0x80 ; call kernel
; Check for errors
test eax, eax
js error_handling ; Jump to error_handling if syscall returned with an error
; Go back to accept more connections
jmp accept_loop
error_handling:
; Handle error
; Print an error message or perform error recovery
jmp exit
exit:
; Close the listening socket
mov eax, 6 ; sys_close syscall number
mov ebx, dword [listen_sock] ; Listening socket file descriptor
int 0x80 ; call kernel
; Exit the program
mov eax, 1 ; sys_exit syscall number
xor ebx, ebx ; Exit code 0
int 0x80 ; call kernel
section .data
prompt_msg db 'Enter your message: ', 0
prompt_len equ $ - prompt_msg