-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket_server.asm
More file actions
189 lines (130 loc) · 2.6 KB
/
Socket_server.asm
File metadata and controls
189 lines (130 loc) · 2.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
global _start
%define hton(x) ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24)
%define htons(x) ((x >> 8) & 0xFF) | ((x & 0xFF) << 8)
NULL equ 0
FL equ 10
_port equ 5555
_ip equ 0xc0a80123
IP equ hton(_ip)
PORT equ htons(_port)
SOCK_STREAM equ 1
AF_INET equ 2
PF_INET equ AF_INET
struc sockaddr_in
sin_port: resd 1
sin_addr: resq 1
sin_family: resb 1
sin_zero: resb 8
endstruc
;struc sockaddr_in
; sin_port: resd 1
; sin_addr: resq 1
; sin_family: resb 1
; sin_zero: resb 8
;endstruc
section .data
servidor:
istruc sockaddr_in
at sin_port, dd PORT
at sin_addr, dq IP
at sin_family, db AF_INET
at sin_zero, db NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
iend
conn dq 0
initializing: db "[!!!] Inicializando conexão!", FL, NULL
.length: equ $-initializing
waiting: db "[*] Aguardando Conexão!", FL, NULL
.length: equ $-waiting
received: db "[+] Conexão Recebida!", FL, NULL
.length: equ $-received
sock_return dq 0
sockfd dq 0
section .text
_start:
; print inicialização de socket
lea rsi, [initializing]
mov rdx, initializing.length
call printf
; jmp EXIT_SUCCESS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
xor rsi, rsi
xor rdx, rdx
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call make_connection
; mov socket-descriptor to mem variable
mov qword [sockfd], rax
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call bind
mov qword [sock_return], rax
cmp qword [sock_return], 0
jne EXIT_ERROR
call listen
mov qword [sock_return], rax
cmp qword [sock_return], 0
jne EXIT_ERROR
lea rsi, [waiting]
mov rdx, waiting.length
call printf
call accept
mov qword [sock_return], rax
cmp qword [sock_return], 0
jne EXIT_ERROR
lea rsi, [received]
mov rdi, waiting.length
call printf
; move a fd da conn para a conn variable
mov qword [conn], rax
jmp EXIT_SUCCESS
global accept
accept:
mov rax, 43
mov rdi, qword [sockfd]
mov rsi, NULL
mov rdx, NULL
syscall
mov qword [conn], rax
ret
global listen
listen:
mov rax, 50
mov rdi, qword [sockfd]
mov rsi, 1
syscall
ret
global bind
bind:
mov rax, 49
mov rdi, qword [sockfd]
lea rsi, [servidor+sockaddr_in]
mov rdx, 64
syscall
ret
global make_connection
make_connection:
mov rax, 41
mov rdi, PF_INET
mov rsi, SOCK_STREAM
mov rdx, 0
syscall
ret
global printf
printf:
mov rax, 1
mov rdi, 1
syscall
ret
EXIT_SUCCESS:
mov rax, 60
mov rdi, 0
syscall
EXIT_ERROR:
mov rax, 60
mov rdi, 1
syscall
;servidor:
; istruc sockaddr_in
; at sin_port, dd PORT
; at sin_addr, dq IP
; at sin_family, db AF_INET
; at sin_zero, db NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
; iend