|
| 1 | +section .data |
| 2 | + listen_sock equ 3 ; File descriptor for listening socket |
| 3 | + conn_sock equ 4 ; File descriptor for connection socket |
| 4 | + backlog equ 5 ; Maximum length to which the queue of pending connections may grow |
| 5 | + buf_size equ 1024 ; Buffer size for receiving data |
| 6 | + |
| 7 | + crlf db 0x0D, 0x0A ; Carriage return, Line feed |
| 8 | + response db 'HTTP/1.1 200 OK', crlf |
| 9 | + db 'Content-Type: text/plain', crlf |
| 10 | + db '', crlf |
| 11 | + db 'Hello, World!', crlf |
| 12 | + db 0 ; Null terminator |
| 13 | + |
| 14 | +section .bss |
| 15 | + addr resb 16 ; Buffer for storing remote address |
| 16 | + buf resb buf_size ; Buffer for receiving data |
| 17 | + |
| 18 | +section .text |
| 19 | + global _start |
| 20 | + |
| 21 | +_start: |
| 22 | + ; Create a socket |
| 23 | + mov eax, 102 ; sys_socketcall syscall number |
| 24 | + mov ebx, 1 ; socketcall: SYS_SOCKET |
| 25 | + mov ecx, 1 ; AF_INET: IPv4 protocol family |
| 26 | + mov edx, 1 ; SOCK_STREAM: TCP socket type |
| 27 | + int 0x80 ; call kernel |
| 28 | + |
| 29 | + ; Check for errors |
| 30 | + test eax, eax |
| 31 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 32 | + |
| 33 | + mov dword [listen_sock], eax ; Store the listening socket file descriptor |
| 34 | + |
| 35 | + ; Bind the socket to an address and port |
| 36 | + ; (Assuming binding to port 8080 and any available address) |
| 37 | + mov eax, 102 ; sys_socketcall syscall number |
| 38 | + mov ebx, 2 ; socketcall: SYS_BIND |
| 39 | + mov ecx, dword [listen_sock] ; Socket file descriptor |
| 40 | + lea edx, [addr] ; Pointer to the sockaddr_in structure |
| 41 | + mov esi, 16 ; Size of the sockaddr_in structure |
| 42 | + int 0x80 ; call kernel |
| 43 | + |
| 44 | + ; Check for errors |
| 45 | + test eax, eax |
| 46 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 47 | + |
| 48 | + ; Listen for incoming connections |
| 49 | + mov eax, 106 ; sys_socketcall syscall number |
| 50 | + mov ebx, 4 ; socketcall: SYS_LISTEN |
| 51 | + mov ecx, dword [listen_sock] ; Socket file descriptor |
| 52 | + mov edx, backlog ; Maximum length to which the queue of pending connections may grow |
| 53 | + int 0x80 ; call kernel |
| 54 | + |
| 55 | + ; Check for errors |
| 56 | + test eax, eax |
| 57 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 58 | + |
| 59 | +accept_loop: |
| 60 | + ; Accept incoming connections |
| 61 | + mov eax, 102 ; sys_socketcall syscall number |
| 62 | + mov ebx, 5 ; socketcall: SYS_ACCEPT |
| 63 | + mov ecx, dword [listen_sock] ; Socket file descriptor |
| 64 | + lea edx, [addr] ; Pointer to the sockaddr_in structure to store the remote address |
| 65 | + lea esi, [conn_sock] ; Pointer to store the new connection socket file descriptor |
| 66 | + mov edi, 16 ; Size of the sockaddr_in structure |
| 67 | + int 0x80 ; call kernel |
| 68 | + |
| 69 | + ; Check for errors |
| 70 | + test eax, eax |
| 71 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 72 | + |
| 73 | + ; Read data from the client |
| 74 | + mov eax, 3 ; sys_read syscall number |
| 75 | + mov ebx, dword [conn_sock] ; Connection socket file descriptor |
| 76 | + lea ecx, [buf] ; Buffer to store received data |
| 77 | + mov edx, buf_size ; Maximum number of bytes to read |
| 78 | + int 0x80 ; call kernel |
| 79 | + |
| 80 | + ; Check for errors |
| 81 | + test eax, eax |
| 82 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 83 | + |
| 84 | + ; Send the response back to the client |
| 85 | + mov eax, 4 ; sys_write syscall number |
| 86 | + mov ebx, dword [conn_sock] ; Connection socket file descriptor |
| 87 | + lea ecx, [response] ; Response buffer |
| 88 | + mov edx, response_len ; Length of the response |
| 89 | + int 0x80 ; call kernel |
| 90 | + |
| 91 | + ; Check for errors |
| 92 | + test eax, eax |
| 93 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 94 | + |
| 95 | + ; Close the connection socket |
| 96 | + mov eax, 6 ; sys_close syscall number |
| 97 | + mov ebx, dword [conn_sock] ; Connection socket file descriptor |
| 98 | + int 0x80 ; call kernel |
| 99 | + |
| 100 | + ; Check for errors |
| 101 | + test eax, eax |
| 102 | + js error_handling ; Jump to error_handling if syscall returned with an error |
| 103 | + |
| 104 | + ; Go back to accept more connections |
| 105 | + jmp accept_loop |
| 106 | + |
| 107 | +error_handling: |
| 108 | + ; Handle error |
| 109 | + ; Print an error message or perform error recovery |
| 110 | + jmp exit |
| 111 | + |
| 112 | +exit: |
| 113 | + ; Close the listening socket |
| 114 | + mov eax, 6 ; sys_close syscall number |
| 115 | + mov ebx, dword [listen_sock] ; Listening socket file descriptor |
| 116 | + int 0x80 ; call kernel |
| 117 | + |
| 118 | + ; Exit the program |
| 119 | + mov eax, 1 ; sys_exit syscall number |
| 120 | + xor ebx, ebx ; Exit code 0 |
| 121 | + int 0x80 ; call kernel |
| 122 | + |
| 123 | +section .data |
| 124 | + prompt_msg db 'Enter your message: ', 0 |
| 125 | + prompt_len equ $ - prompt_msg |
0 commit comments