Rev 1716 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1159 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
1514 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
1159 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; SOCKET.INC ;; |
||
7 | ;; ;; |
||
1529 | hidnplayr | 8 | ;; Written by hidnplayr@kolibrios.org, ;; |
9 | ;; and Clevermouse. ;; |
||
1159 | hidnplayr | 10 | ;; ;; |
1529 | hidnplayr | 11 | ;; Based on code by mike.dld ;; |
1159 | hidnplayr | 12 | ;; ;; |
1529 | hidnplayr | 13 | ;; GNU GENERAL PUBLIC LICENSE ;; |
14 | ;; Version 2, June 1991 ;; |
||
15 | ;; ;; |
||
1159 | hidnplayr | 16 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
17 | |||
1206 | hidnplayr | 18 | $Revision: 1733 $ |
1159 | hidnplayr | 19 | |
1514 | hidnplayr | 20 | virtual at 0 |
1249 | hidnplayr | 21 | |
1514 | hidnplayr | 22 | SOCKET: |
23 | .NextPtr dd ? ; pointer to next socket in list |
||
24 | .PrevPtr dd ? ; pointer to previous socket in list |
||
25 | .Number dd ? ; socket number |
||
1249 | hidnplayr | 26 | |
1514 | hidnplayr | 27 | .lock dd ? ; lock mutex |
1249 | hidnplayr | 28 | |
1514 | hidnplayr | 29 | .PID dd ? ; application process id |
30 | .Domain dd ? ; INET/UNIX/.. |
||
1543 | hidnplayr | 31 | .Type dd ? ; RAW/STREAM/DGRAP |
32 | .Protocol dd ? ; ICMP/IPv4/ARP/TCP/UDP |
||
1716 | hidnplayr | 33 | .errorcode dd ? |
1249 | hidnplayr | 34 | |
1514 | hidnplayr | 35 | .options dd ? |
1529 | hidnplayr | 36 | .state dd ? |
1543 | hidnplayr | 37 | .backlog dw ? ; how many incomming connections that can be queued |
1249 | hidnplayr | 38 | |
1536 | hidnplayr | 39 | .snd_proc dd ? |
40 | .rcv_proc dd ? |
||
41 | |||
1514 | hidnplayr | 42 | .end: |
43 | end virtual |
||
1249 | hidnplayr | 44 | |
1514 | hidnplayr | 45 | virtual at SOCKET.end |
1254 | hidnplayr | 46 | |
1514 | hidnplayr | 47 | IP_SOCKET: |
1318 | hidnplayr | 48 | |
1514 | hidnplayr | 49 | .LocalIP dd ? |
50 | rd 3 ; for IPv6 addresses |
||
1249 | hidnplayr | 51 | |
1514 | hidnplayr | 52 | .RemoteIP dd ? |
53 | rd 3 ; for IPv6 addresses |
||
1249 | hidnplayr | 54 | |
1514 | hidnplayr | 55 | .end: |
56 | end virtual |
||
1159 | hidnplayr | 57 | |
1514 | hidnplayr | 58 | virtual at IP_SOCKET.end |
1249 | hidnplayr | 59 | |
1514 | hidnplayr | 60 | TCP_SOCKET: |
61 | |||
1542 | hidnplayr | 62 | .LocalPort dw ? |
63 | .RemotePort dw ? |
||
1514 | hidnplayr | 64 | |
65 | .OrigRemoteIP dd ? ; original remote IP address (used to reset to LISTEN state) |
||
66 | .OrigRemotePort dw ? ; original remote port (used to reset to LISTEN state) |
||
67 | |||
68 | .t_state dd ? ; TCB state |
||
69 | .t_rxtshift dd ? |
||
70 | .t_rxtcur dd ? |
||
71 | .t_dupacks dd ? |
||
72 | .t_maxseg dd ? |
||
73 | .t_force dd ? |
||
74 | .t_flags dd ? |
||
75 | |||
76 | ;--------------- |
||
77 | ; RFC783 page 21 |
||
78 | |||
79 | ; send sequence |
||
80 | .SND_UNA dd ? ; sequence number of unack'ed sent Packets |
||
81 | .SND_NXT dd ? ; next send sequence number to use |
||
82 | .SND_UP dd ? |
||
83 | .SND_WL1 dd ? ; window minus one |
||
84 | .SND_WL2 dd ? ; |
||
85 | .ISS dd ? ; initial send sequence number |
||
86 | .SND_WND dd ? ; send window |
||
87 | |||
88 | ; receive sequence |
||
89 | .RCV_WND dw ? ; receive window |
||
90 | .RCV_NXT dd ? ; next receive sequence number to use |
||
91 | .RCV_UP dd ? |
||
92 | .IRS dd ? ; initial receive sequence number |
||
93 | |||
94 | ;--------------------- |
||
95 | ; Additional variables |
||
96 | |||
97 | ; receive variables |
||
98 | .RCV_ADV dd ? |
||
99 | |||
100 | ; retransmit variables |
||
101 | .SND_MAX dd ? |
||
102 | |||
103 | ; congestion control |
||
104 | .SND_CWND dd ? |
||
105 | .SND_SSTHRESH dd ? |
||
106 | |||
107 | ;---------------------- |
||
108 | ; Transmit timing stuff |
||
109 | .t_idle dd ? |
||
110 | .t_rtt dd ? |
||
111 | .t_rtseq dd ? |
||
112 | .t_srtt dd ? |
||
113 | .t_rttvar dd ? |
||
114 | .t_rttmin dd ? |
||
115 | .max_sndwnd dd ? |
||
116 | |||
117 | ;----------------- |
||
118 | ; Out-of-band data |
||
119 | .t_oobflags dd ? |
||
120 | .t_iobc dd ? |
||
121 | .t_softerror dd ? |
||
122 | |||
123 | |||
124 | ;--------- |
||
125 | ; RFC 1323 |
||
126 | .SND_SCALE db ? ; Scale factor |
||
127 | .RCV_SCALE db ? |
||
128 | .request_r_scale db ? |
||
129 | .requested_s_scale dd ? |
||
130 | |||
131 | .ts_recent dd ? |
||
132 | .ts_recent_age dd ? |
||
133 | .last_ack_sent dd ? |
||
134 | |||
1519 | hidnplayr | 135 | |
136 | ;------- |
||
137 | ; Timers |
||
1529 | hidnplayr | 138 | .timer_retransmission dw ? ; rexmt |
1519 | hidnplayr | 139 | .timer_ack dw ? |
140 | .timer_persist dw ? |
||
1529 | hidnplayr | 141 | .timer_keepalive dw ? ; keepalive/syn timeout |
142 | .timer_timed_wait dw ? ; also used as 2msl timer |
||
1519 | hidnplayr | 143 | |
1249 | hidnplayr | 144 | .end: |
1514 | hidnplayr | 145 | end virtual |
1249 | hidnplayr | 146 | |
1514 | hidnplayr | 147 | virtual at IP_SOCKET.end |
1249 | hidnplayr | 148 | |
1514 | hidnplayr | 149 | UDP_SOCKET: |
1249 | hidnplayr | 150 | |
1542 | hidnplayr | 151 | .LocalPort dw ? |
152 | .RemotePort dw ? |
||
1514 | hidnplayr | 153 | .firstpacket db ? |
1249 | hidnplayr | 154 | |
155 | .end: |
||
1514 | hidnplayr | 156 | end virtual |
1249 | hidnplayr | 157 | |
1514 | hidnplayr | 158 | virtual at IP_SOCKET.end |
1249 | hidnplayr | 159 | |
1514 | hidnplayr | 160 | ICMP_SOCKET: |
161 | |||
1542 | hidnplayr | 162 | .Identifier dw ? |
1514 | hidnplayr | 163 | |
164 | .end: |
||
165 | end virtual |
||
166 | |||
1529 | hidnplayr | 167 | struc RING_BUFFER { |
168 | .start_ptr dd ? ; Pointer to start of buffer |
||
169 | .end_ptr dd ? ; pointer to end of buffer |
||
170 | .read_ptr dd ? ; Read pointer |
||
171 | .write_ptr dd ? ; Write pointer |
||
172 | .size dd ? ; Number of bytes buffered |
||
1533 | hidnplayr | 173 | .end: |
1529 | hidnplayr | 174 | } |
1514 | hidnplayr | 175 | |
1529 | hidnplayr | 176 | virtual at 0 |
177 | |||
178 | RING_BUFFER RING_BUFFER |
||
179 | |||
180 | end virtual |
||
181 | |||
182 | virtual at TCP_SOCKET.end |
||
183 | |||
184 | STREAM_SOCKET: |
||
1533 | hidnplayr | 185 | .rcv rd RING_BUFFER.end/4 |
186 | .snd rd RING_BUFFER.end/4 |
||
1529 | hidnplayr | 187 | .end: |
188 | |||
189 | end virtual |
||
190 | |||
1274 | hidnplayr | 191 | struct socket_queue_entry |
192 | .data_ptr dd ? |
||
1514 | hidnplayr | 193 | .buf_ptr dd ? |
1274 | hidnplayr | 194 | .data_size dd ? |
195 | .size: |
||
196 | ends |
||
197 | |||
1514 | hidnplayr | 198 | |
1249 | hidnplayr | 199 | SOCKETBUFFSIZE equ 4096 ; in bytes |
1514 | hidnplayr | 200 | |
1249 | hidnplayr | 201 | SOCKET_QUEUE_SIZE equ 10 ; maximum number ofincoming packets queued for 1 socket |
1514 | hidnplayr | 202 | ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start |
1543 | hidnplayr | 203 | SOCKET_QUEUE_LOCATION equ (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*socket_queue_entry.size - queue.data) |
1249 | hidnplayr | 204 | |
1159 | hidnplayr | 205 | uglobal |
1514 | hidnplayr | 206 | net_sockets rd 4 |
1543 | hidnplayr | 207 | last_socket_num dd ? |
1159 | hidnplayr | 208 | last_UDP_port dw ? ; These values give the number of the last used ephemeral port |
209 | last_TCP_port dw ? ; |
||
210 | endg |
||
211 | |||
212 | |||
1257 | hidnplayr | 213 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 214 | ; |
215 | ; SOCKET_init |
||
216 | ; |
||
1257 | hidnplayr | 217 | ;----------------------------------------------------------------- |
1529 | hidnplayr | 218 | macro SOCKET_init { |
1159 | hidnplayr | 219 | |
1514 | hidnplayr | 220 | xor eax, eax |
221 | mov edi, net_sockets |
||
1543 | hidnplayr | 222 | mov ecx, 5 |
1514 | hidnplayr | 223 | rep stosd |
1159 | hidnplayr | 224 | |
1529 | hidnplayr | 225 | @@: |
226 | pseudo_random eax |
||
227 | cmp ax, MIN_EPHEMERAL_PORT |
||
228 | jl @r |
||
229 | cmp ax, MAX_EPHEMERAL_PORT |
||
230 | jg @r |
||
231 | mov [last_UDP_port], ax |
||
232 | |||
233 | @@: |
||
234 | pseudo_random eax |
||
235 | cmp ax, MIN_EPHEMERAL_PORT |
||
236 | jl @r |
||
237 | cmp ax, MAX_EPHEMERAL_PORT |
||
238 | jg @r |
||
239 | mov [last_TCP_port], ax |
||
240 | |||
241 | } |
||
242 | |||
243 | |||
1257 | hidnplayr | 244 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 245 | ; |
246 | ; Socket API (function 74) |
||
247 | ; |
||
1257 | hidnplayr | 248 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 249 | align 4 |
250 | sys_socket: |
||
1542 | hidnplayr | 251 | cmp ebx, 9 ; highest possible number |
1529 | hidnplayr | 252 | jg @f |
1542 | hidnplayr | 253 | jmp dword [sock_sysfn_table + 4*ebx] |
1529 | hidnplayr | 254 | @@: |
255 | cmp ebx, 255 |
||
256 | jz SOCKET_debug |
||
1159 | hidnplayr | 257 | |
1529 | hidnplayr | 258 | s_error: |
259 | DEBUGF 1,"socket error\n" |
||
260 | mov dword [esp+32], -1 |
||
261 | |||
262 | ret |
||
263 | |||
1536 | hidnplayr | 264 | align 4 |
1529 | hidnplayr | 265 | sock_sysfn_table: |
1514 | hidnplayr | 266 | dd SOCKET_open ; 0 |
267 | dd SOCKET_close ; 1 |
||
268 | dd SOCKET_bind ; 2 |
||
269 | dd SOCKET_listen ; 3 |
||
270 | dd SOCKET_connect ; 4 |
||
271 | dd SOCKET_accept ; 5 |
||
272 | dd SOCKET_send ; 6 |
||
273 | dd SOCKET_receive ; 7 |
||
1542 | hidnplayr | 274 | dd SOCKET_set_opt ; 8 |
275 | dd SOCKET_get_opt ; 9 |
||
1159 | hidnplayr | 276 | |
1257 | hidnplayr | 277 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 278 | ; |
279 | ; SOCKET_open |
||
280 | ; |
||
281 | ; IN: domain in ecx |
||
282 | ; type in edx |
||
1196 | hidnplayr | 283 | ; protocol in esi |
1159 | hidnplayr | 284 | ; OUT: eax is socket num, -1 on error |
285 | ; |
||
1257 | hidnplayr | 286 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 287 | align 4 |
1514 | hidnplayr | 288 | SOCKET_open: |
1159 | hidnplayr | 289 | |
1536 | hidnplayr | 290 | DEBUGF 1,"SOCKET_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi |
1159 | hidnplayr | 291 | |
1514 | hidnplayr | 292 | call SOCKET_alloc |
1185 | hidnplayr | 293 | jz s_error |
1159 | hidnplayr | 294 | |
1542 | hidnplayr | 295 | mov [esp+32], edi ; return socketnumber |
296 | |||
1514 | hidnplayr | 297 | mov [eax + SOCKET.Domain], ecx |
298 | mov [eax + SOCKET.Type], edx |
||
299 | mov [eax + SOCKET.Protocol], esi |
||
1159 | hidnplayr | 300 | |
1529 | hidnplayr | 301 | cmp ecx, AF_INET4 |
1536 | hidnplayr | 302 | jne .no_inet4 |
1529 | hidnplayr | 303 | |
1542 | hidnplayr | 304 | cmp edx, SOCK_DGRAM |
1536 | hidnplayr | 305 | je .udp |
306 | |||
1542 | hidnplayr | 307 | cmp edx, SOCK_STREAM |
1536 | hidnplayr | 308 | je .tcp |
1529 | hidnplayr | 309 | |
1541 | hidnplayr | 310 | cmp edx, SOCK_RAW |
311 | je .raw |
||
312 | |||
1536 | hidnplayr | 313 | .no_inet4: |
314 | ret |
||
315 | |||
1542 | hidnplayr | 316 | align 4 |
317 | .raw: |
||
1543 | hidnplayr | 318 | test esi, esi ; IP_PROTO_IP |
319 | jz .ip |
||
1529 | hidnplayr | 320 | |
1542 | hidnplayr | 321 | cmp esi, IP_PROTO_ICMP |
322 | je .icmp |
||
1529 | hidnplayr | 323 | |
1542 | hidnplayr | 324 | cmp esi, IP_PROTO_UDP |
325 | je .udp |
||
1533 | hidnplayr | 326 | |
1542 | hidnplayr | 327 | cmp esi, IP_PROTO_TCP |
328 | je .tcp |
||
1536 | hidnplayr | 329 | |
1159 | hidnplayr | 330 | ret |
331 | |||
1542 | hidnplayr | 332 | align 4 |
1536 | hidnplayr | 333 | .udp: |
1542 | hidnplayr | 334 | mov [eax + SOCKET.Protocol], IP_PROTO_UDP |
1536 | hidnplayr | 335 | mov [eax + SOCKET.snd_proc], SOCKET_send_udp |
1541 | hidnplayr | 336 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram |
1529 | hidnplayr | 337 | ret |
338 | |||
1542 | hidnplayr | 339 | align 4 |
340 | .tcp: |
||
341 | mov [eax + SOCKET.Protocol], IP_PROTO_TCP |
||
1543 | hidnplayr | 342 | mov [eax + SOCKET.snd_proc], SOCKET_send_tcp |
343 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_tcp |
||
344 | ret |
||
1529 | hidnplayr | 345 | |
346 | |||
1543 | hidnplayr | 347 | align 4 |
348 | .ip: |
||
349 | mov [eax + SOCKET.snd_proc], SOCKET_send_ip |
||
350 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram |
||
1541 | hidnplayr | 351 | ret |
352 | |||
1542 | hidnplayr | 353 | |
354 | align 4 |
||
1541 | hidnplayr | 355 | .icmp: |
356 | mov [eax + SOCKET.snd_proc], SOCKET_send_icmp |
||
357 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_dgram |
||
358 | ret |
||
359 | |||
360 | |||
361 | |||
1257 | hidnplayr | 362 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 363 | ; |
364 | ; SOCKET_bind |
||
365 | ; |
||
366 | ; IN: socket number in ecx |
||
367 | ; pointer to sockaddr struct in edx |
||
368 | ; length of that struct in esi |
||
369 | ; OUT: 0 on success |
||
370 | ; |
||
1257 | hidnplayr | 371 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 372 | align 4 |
1514 | hidnplayr | 373 | SOCKET_bind: |
1159 | hidnplayr | 374 | |
1514 | hidnplayr | 375 | DEBUGF 1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi |
1159 | hidnplayr | 376 | |
1514 | hidnplayr | 377 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 378 | jz s_error |
1159 | hidnplayr | 379 | |
380 | cmp esi, 2 |
||
1185 | hidnplayr | 381 | jl s_error |
1159 | hidnplayr | 382 | |
383 | cmp word [edx], AF_INET4 |
||
1249 | hidnplayr | 384 | je .af_inet4 |
1159 | hidnplayr | 385 | |
1249 | hidnplayr | 386 | cmp word [edx], AF_UNIX |
387 | je .af_unix |
||
388 | |||
389 | jmp s_error |
||
390 | |||
391 | .af_unix: |
||
392 | ; TODO: write code here |
||
393 | |||
1514 | hidnplayr | 394 | mov dword [esp+32], 0 |
1249 | hidnplayr | 395 | ret |
396 | |||
1159 | hidnplayr | 397 | .af_inet4: |
398 | |||
1514 | hidnplayr | 399 | DEBUGF 1,"af_inet4\n" |
400 | |||
1159 | hidnplayr | 401 | cmp esi, 6 |
1185 | hidnplayr | 402 | jl s_error |
1159 | hidnplayr | 403 | |
1543 | hidnplayr | 404 | push word [edx + 2] |
405 | pop word [eax + UDP_SOCKET.LocalPort] |
||
1159 | hidnplayr | 406 | |
1543 | hidnplayr | 407 | push dword [edx + 4] |
408 | pop [eax + IP_SOCKET.LocalIP] |
||
1159 | hidnplayr | 409 | |
410 | DEBUGF 1,"local ip: %u.%u.%u.%u\n",\ |
||
1514 | hidnplayr | 411 | [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\ |
412 | [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1 |
||
1159 | hidnplayr | 413 | |
1514 | hidnplayr | 414 | mov dword [esp+32], 0 |
1159 | hidnplayr | 415 | ret |
416 | |||
417 | |||
418 | |||
419 | |||
1257 | hidnplayr | 420 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 421 | ; |
422 | ; SOCKET_connect |
||
423 | ; |
||
424 | ; IN: socket number in ecx |
||
425 | ; pointer to sockaddr struct in edx |
||
426 | ; length of that struct in esi |
||
427 | ; OUT: 0 on success |
||
428 | ; |
||
1257 | hidnplayr | 429 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 430 | align 4 |
1514 | hidnplayr | 431 | SOCKET_connect: |
1159 | hidnplayr | 432 | |
1541 | hidnplayr | 433 | DEBUGF 1,"SOCKET_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi |
1159 | hidnplayr | 434 | |
1514 | hidnplayr | 435 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 436 | jz s_error |
1159 | hidnplayr | 437 | |
1254 | hidnplayr | 438 | cmp esi, 8 |
1185 | hidnplayr | 439 | jl s_error |
1159 | hidnplayr | 440 | |
441 | cmp word [edx], AF_INET4 |
||
442 | je .af_inet4 |
||
443 | |||
1185 | hidnplayr | 444 | jmp s_error |
1159 | hidnplayr | 445 | |
446 | .af_inet4: |
||
1543 | hidnplayr | 447 | cmp [eax + IP_SOCKET.LocalIP], 0 |
448 | jne @f |
||
449 | push [IP_LIST] |
||
450 | pop [eax + IP_SOCKET.LocalIP] |
||
451 | @@: |
||
1159 | hidnplayr | 452 | |
1542 | hidnplayr | 453 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
1159 | hidnplayr | 454 | je .udp |
455 | |||
1542 | hidnplayr | 456 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1206 | hidnplayr | 457 | je .tcp |
1159 | hidnplayr | 458 | |
1542 | hidnplayr | 459 | cmp [eax + SOCKET.Protocol], IP_PROTO_IP |
460 | je .ip |
||
1541 | hidnplayr | 461 | |
1542 | hidnplayr | 462 | cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP |
463 | je .ip |
||
464 | |||
1185 | hidnplayr | 465 | jmp s_error |
1159 | hidnplayr | 466 | |
1542 | hidnplayr | 467 | align 4 |
1254 | hidnplayr | 468 | .udp: |
1543 | hidnplayr | 469 | lea ebx, [eax + SOCKET.lock] |
470 | call wait_mutex |
||
471 | |||
472 | push word [edx + 2] |
||
473 | pop [eax + UDP_SOCKET.RemotePort] |
||
474 | |||
475 | push dword [edx + 4] |
||
476 | pop [eax + IP_SOCKET.RemoteIP] |
||
477 | |||
478 | cmp [eax + UDP_SOCKET.LocalPort], 0 |
||
479 | jne @f |
||
480 | call SOCKET_find_port |
||
481 | @@: |
||
482 | |||
1514 | hidnplayr | 483 | mov [eax + UDP_SOCKET.firstpacket], 0 |
1159 | hidnplayr | 484 | |
1543 | hidnplayr | 485 | push eax |
486 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
||
1733 | hidnplayr | 487 | pop eax |
1159 | hidnplayr | 488 | |
1543 | hidnplayr | 489 | mov [eax + SOCKET.lock], 0 |
1514 | hidnplayr | 490 | mov dword [esp+32], 0 |
1159 | hidnplayr | 491 | ret |
492 | |||
1542 | hidnplayr | 493 | align 4 |
1254 | hidnplayr | 494 | .tcp: |
1514 | hidnplayr | 495 | lea ebx, [eax + SOCKET.lock] |
1281 | hidnplayr | 496 | call wait_mutex |
1159 | hidnplayr | 497 | |
1543 | hidnplayr | 498 | push word [edx + 2] |
499 | pop [eax + TCP_SOCKET.RemotePort] |
||
1159 | hidnplayr | 500 | |
1543 | hidnplayr | 501 | push dword [edx + 4] |
502 | pop [eax + IP_SOCKET.RemoteIP] |
||
1159 | hidnplayr | 503 | |
1543 | hidnplayr | 504 | cmp [eax + TCP_SOCKET.LocalPort], 0 |
505 | jne @f |
||
506 | call SOCKET_find_port |
||
507 | @@: |
||
1159 | hidnplayr | 508 | |
1529 | hidnplayr | 509 | mov [eax + TCP_SOCKET.timer_persist], 0 |
510 | mov [eax + TCP_SOCKET.t_state], TCB_SYN_SENT |
||
1733 | hidnplayr | 511 | push [TCP_sequence_num] |
1529 | hidnplayr | 512 | add [TCP_sequence_num], 6400 |
1733 | hidnplayr | 513 | pop [eax + TCP_SOCKET.ISS] |
1533 | hidnplayr | 514 | mov [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init |
1519 | hidnplayr | 515 | |
1533 | hidnplayr | 516 | TCP_sendseqinit eax |
1529 | hidnplayr | 517 | |
1543 | hidnplayr | 518 | ; mov [ebx + TCP_SOCKET.timer_retransmission], ;; todo: create macro to set retransmission timer |
1533 | hidnplayr | 519 | |
1543 | hidnplayr | 520 | mov ebx, eax |
1159 | hidnplayr | 521 | |
1543 | hidnplayr | 522 | lea eax, [ebx + STREAM_SOCKET.snd] |
523 | call SOCKET_ring_create |
||
524 | |||
525 | lea eax, [ebx + STREAM_SOCKET.rcv] |
||
526 | call SOCKET_ring_create |
||
527 | |||
528 | mov [ebx + SOCKET.lock], 0 |
||
1733 | hidnplayr | 529 | |
530 | mov eax, ebx |
||
531 | call TCP_output |
||
532 | |||
1543 | hidnplayr | 533 | mov dword [esp+32], 0 |
1159 | hidnplayr | 534 | ret |
535 | |||
1542 | hidnplayr | 536 | align 4 |
537 | .ip: |
||
1543 | hidnplayr | 538 | lea ebx, [eax + SOCKET.lock] |
539 | call wait_mutex |
||
540 | |||
1541 | hidnplayr | 541 | push dword [edx + 4] |
542 | pop dword [eax + IP_SOCKET.RemoteIP] |
||
1159 | hidnplayr | 543 | |
1543 | hidnplayr | 544 | push eax |
545 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up data receiving queue |
||
546 | pop eax |
||
547 | |||
548 | mov [eax + SOCKET.lock], 0 |
||
549 | mov dword [esp+32], 0 |
||
1541 | hidnplayr | 550 | ret |
551 | |||
552 | |||
1257 | hidnplayr | 553 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 554 | ; |
555 | ; SOCKET_listen |
||
556 | ; |
||
557 | ; IN: socket number in ecx |
||
558 | ; backlog in edx |
||
559 | ; OUT: eax is socket num, -1 on error |
||
560 | ; |
||
1257 | hidnplayr | 561 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 562 | align 4 |
1514 | hidnplayr | 563 | SOCKET_listen: |
1159 | hidnplayr | 564 | |
1541 | hidnplayr | 565 | DEBUGF 1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx |
1159 | hidnplayr | 566 | |
1514 | hidnplayr | 567 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 568 | jz s_error |
1159 | hidnplayr | 569 | |
1514 | hidnplayr | 570 | cmp word [eax + SOCKET.Domain], AF_INET4 |
1254 | hidnplayr | 571 | jne s_error |
572 | |||
1542 | hidnplayr | 573 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1254 | hidnplayr | 574 | jne s_error |
575 | |||
1543 | hidnplayr | 576 | cmp [eax + TCP_SOCKET.LocalPort], 0 |
577 | je s_error |
||
1514 | hidnplayr | 578 | |
1543 | hidnplayr | 579 | cmp [eax + IP_SOCKET.LocalIP], 0 |
580 | jne @f |
||
581 | push [IP_LIST] |
||
582 | pop [eax + IP_SOCKET.LocalIP] |
||
583 | @@: |
||
584 | |||
1159 | hidnplayr | 585 | cmp edx, MAX_backlog |
1543 | hidnplayr | 586 | jle @f |
1514 | hidnplayr | 587 | mov edx, MAX_backlog |
1543 | hidnplayr | 588 | @@: |
1159 | hidnplayr | 589 | |
1543 | hidnplayr | 590 | mov [eax + SOCKET.backlog], dx |
591 | or [eax + SOCKET.options], SO_ACCEPTCON |
||
1514 | hidnplayr | 592 | mov [eax + TCP_SOCKET.t_state], TCB_LISTEN |
1159 | hidnplayr | 593 | |
1543 | hidnplayr | 594 | push eax |
595 | init_queue (eax + SOCKET_QUEUE_LOCATION) ; Set up sockets queue |
||
596 | pop eax |
||
597 | |||
1159 | hidnplayr | 598 | mov dword [esp+32], 0 |
1514 | hidnplayr | 599 | |
1159 | hidnplayr | 600 | ret |
601 | |||
602 | |||
1257 | hidnplayr | 603 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 604 | ; |
605 | ; SOCKET_accept |
||
606 | ; |
||
607 | ; IN: socket number in ecx |
||
608 | ; addr in edx |
||
609 | ; addrlen in esi |
||
610 | ; OUT: eax is socket num, -1 on error |
||
611 | ; |
||
1257 | hidnplayr | 612 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 613 | align 4 |
1514 | hidnplayr | 614 | SOCKET_accept: |
1159 | hidnplayr | 615 | |
1541 | hidnplayr | 616 | DEBUGF 1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi |
1159 | hidnplayr | 617 | |
1514 | hidnplayr | 618 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 619 | jz s_error |
1159 | hidnplayr | 620 | |
1543 | hidnplayr | 621 | test [eax + SOCKET.options], SO_ACCEPTCON |
622 | jz s_error |
||
623 | |||
1514 | hidnplayr | 624 | cmp word [eax + SOCKET.Domain], AF_INET4 |
1543 | hidnplayr | 625 | jne s_error |
1249 | hidnplayr | 626 | |
1542 | hidnplayr | 627 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1543 | hidnplayr | 628 | jne s_error |
1249 | hidnplayr | 629 | |
1543 | hidnplayr | 630 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error |
1249 | hidnplayr | 631 | |
1543 | hidnplayr | 632 | mov eax, [esi] |
633 | call SOCKET_ptr_to_num |
||
634 | jz s_error |
||
635 | mov dword [esp+32], eax |
||
1159 | hidnplayr | 636 | ret |
1514 | hidnplayr | 637 | |
1159 | hidnplayr | 638 | |
1257 | hidnplayr | 639 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 640 | ; |
641 | ; SOCKET_close |
||
642 | ; |
||
643 | ; IN: socket number in ecx |
||
644 | ; OUT: eax is socket num, -1 on error |
||
645 | ; |
||
1257 | hidnplayr | 646 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 647 | align 4 |
1514 | hidnplayr | 648 | SOCKET_close: |
1159 | hidnplayr | 649 | |
1541 | hidnplayr | 650 | DEBUGF 1,"SOCKET_close: socknum: %u\n", ecx |
1159 | hidnplayr | 651 | |
1514 | hidnplayr | 652 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 653 | jz s_error |
1159 | hidnplayr | 654 | |
1514 | hidnplayr | 655 | cmp [eax + SOCKET.Domain], AF_INET4 |
1249 | hidnplayr | 656 | jne s_error |
1159 | hidnplayr | 657 | |
1542 | hidnplayr | 658 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
1514 | hidnplayr | 659 | je .free |
1159 | hidnplayr | 660 | |
1542 | hidnplayr | 661 | cmp [eax + SOCKET.Protocol], IP_PROTO_ICMP |
1514 | hidnplayr | 662 | je .free |
1159 | hidnplayr | 663 | |
1542 | hidnplayr | 664 | cmp [eax + SOCKET.Protocol], IP_PROTO_IP |
665 | je .free |
||
666 | |||
667 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
||
1206 | hidnplayr | 668 | je .tcp |
1159 | hidnplayr | 669 | |
1185 | hidnplayr | 670 | jmp s_error |
1159 | hidnplayr | 671 | |
672 | .tcp: |
||
1536 | hidnplayr | 673 | cmp [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED ; state must be LISTEN, SYN_SENT or CLOSED |
674 | jl .free |
||
1318 | hidnplayr | 675 | |
1536 | hidnplayr | 676 | call TCP_output |
677 | mov dword [esp+32], 0 |
||
1159 | hidnplayr | 678 | |
1536 | hidnplayr | 679 | ret |
1159 | hidnplayr | 680 | |
1514 | hidnplayr | 681 | .free: |
682 | call SOCKET_free |
||
683 | mov dword [esp+32], 0 |
||
1159 | hidnplayr | 684 | |
685 | ret |
||
686 | |||
687 | |||
1257 | hidnplayr | 688 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 689 | ; |
690 | ; SOCKET_receive |
||
691 | ; |
||
692 | ; IN: socket number in ecx |
||
1249 | hidnplayr | 693 | ; addr to buffer in edx |
694 | ; length of buffer in esi |
||
1159 | hidnplayr | 695 | ; flags in edi |
696 | ; OUT: eax is number of bytes copied, -1 on error |
||
697 | ; |
||
1257 | hidnplayr | 698 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 699 | align 4 |
1514 | hidnplayr | 700 | SOCKET_receive: |
1159 | hidnplayr | 701 | |
1536 | hidnplayr | 702 | DEBUGF 1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi |
1514 | hidnplayr | 703 | |
704 | call SOCKET_num_to_ptr |
||
1185 | hidnplayr | 705 | jz s_error |
1159 | hidnplayr | 706 | |
1536 | hidnplayr | 707 | jmp [eax + SOCKET.rcv_proc] |
1533 | hidnplayr | 708 | |
1536 | hidnplayr | 709 | |
710 | align 4 |
||
1541 | hidnplayr | 711 | SOCKET_receive_dgram: |
1536 | hidnplayr | 712 | |
1541 | hidnplayr | 713 | DEBUGF 1,"SOCKET_receive: DGRAM\n" |
1536 | hidnplayr | 714 | |
1281 | hidnplayr | 715 | mov ebx, esi |
1514 | hidnplayr | 716 | mov edi, edx ; addr to buffer |
1159 | hidnplayr | 717 | |
1536 | hidnplayr | 718 | get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, s_error ; destroys esi and ecx |
719 | |||
1249 | hidnplayr | 720 | mov ecx, [esi + socket_queue_entry.data_size] |
721 | DEBUGF 1,"Got %u bytes of data\n", ecx |
||
1159 | hidnplayr | 722 | |
1281 | hidnplayr | 723 | cmp ecx, ebx |
1536 | hidnplayr | 724 | jg .too_small |
1514 | hidnplayr | 725 | |
1536 | hidnplayr | 726 | push [esi + socket_queue_entry.buf_ptr] ; save the buffer addr so we can clear it later |
1514 | hidnplayr | 727 | mov esi, [esi + socket_queue_entry.data_ptr] |
1249 | hidnplayr | 728 | DEBUGF 1,"Source buffer: %x, real addr: %x\n", [esp], esi |
1536 | hidnplayr | 729 | mov dword[esp+32+4], ecx ; return number of bytes copied |
1159 | hidnplayr | 730 | |
1514 | hidnplayr | 731 | ; copy the data |
1249 | hidnplayr | 732 | shr ecx, 1 |
733 | jnc .nb |
||
734 | movsb |
||
1536 | hidnplayr | 735 | .nb: |
736 | shr ecx, 1 |
||
1249 | hidnplayr | 737 | jnc .nw |
738 | movsw |
||
1536 | hidnplayr | 739 | .nw: |
740 | test ecx, ecx |
||
1274 | hidnplayr | 741 | jz .nd |
742 | rep movsd |
||
1536 | hidnplayr | 743 | .nd: |
1159 | hidnplayr | 744 | |
1536 | hidnplayr | 745 | call kernel_free ; remove the packet |
1533 | hidnplayr | 746 | ret |
1514 | hidnplayr | 747 | |
1536 | hidnplayr | 748 | .too_small: |
1533 | hidnplayr | 749 | |
1536 | hidnplayr | 750 | DEBUGF 1,"Buffer too small...\n" |
751 | jmp s_error |
||
752 | |||
753 | align 4 |
||
754 | SOCKET_receive_tcp: |
||
755 | |||
1541 | hidnplayr | 756 | DEBUGF 1,"SOCKET_receive: TCP\n" |
1536 | hidnplayr | 757 | |
1533 | hidnplayr | 758 | mov ecx, esi |
759 | mov edi, edx |
||
760 | add eax, STREAM_SOCKET.rcv |
||
761 | call SOCKET_ring_read |
||
1534 | hidnplayr | 762 | call SOCKET_ring_free |
1533 | hidnplayr | 763 | |
1536 | hidnplayr | 764 | mov dword[esp+32], ecx ; return number of bytes copied |
1533 | hidnplayr | 765 | |
1159 | hidnplayr | 766 | ret |
767 | |||
768 | |||
1257 | hidnplayr | 769 | ;----------------------------------------------------------------- |
1159 | hidnplayr | 770 | ; |
771 | ; SOCKET_send |
||
772 | ; |
||
773 | ; |
||
774 | ; IN: socket number in ecx |
||
1206 | hidnplayr | 775 | ; pointer to data in edx |
776 | ; datalength in esi |
||
1159 | hidnplayr | 777 | ; flags in edi |
778 | ; OUT: -1 on error |
||
779 | ; |
||
1257 | hidnplayr | 780 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 781 | align 4 |
1514 | hidnplayr | 782 | SOCKET_send: |
1159 | hidnplayr | 783 | |
1541 | hidnplayr | 784 | DEBUGF 1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi |
1159 | hidnplayr | 785 | |
1514 | hidnplayr | 786 | call SOCKET_num_to_ptr |
1185 | hidnplayr | 787 | jz s_error |
1159 | hidnplayr | 788 | |
1536 | hidnplayr | 789 | jmp [eax + SOCKET.snd_proc] |
1206 | hidnplayr | 790 | |
791 | |||
1536 | hidnplayr | 792 | align 4 |
793 | SOCKET_send_udp: |
||
1529 | hidnplayr | 794 | |
1541 | hidnplayr | 795 | DEBUGF 1,"SOCKET_send: UDP\n" |
1159 | hidnplayr | 796 | |
797 | mov ecx, esi |
||
798 | mov esi, edx |
||
799 | |||
1514 | hidnplayr | 800 | call UDP_output |
1159 | hidnplayr | 801 | |
1514 | hidnplayr | 802 | mov dword [esp+32], 0 |
1159 | hidnplayr | 803 | ret |
804 | |||
805 | |||
1536 | hidnplayr | 806 | align 4 |
807 | SOCKET_send_tcp: |
||
1254 | hidnplayr | 808 | |
1541 | hidnplayr | 809 | DEBUGF 1,"SOCKET_send: TCP\n" |
1254 | hidnplayr | 810 | |
1533 | hidnplayr | 811 | push eax |
812 | mov ecx, esi |
||
813 | mov esi, edx |
||
814 | add eax, STREAM_SOCKET.snd |
||
815 | call SOCKET_ring_write |
||
816 | pop eax |
||
1536 | hidnplayr | 817 | |
1514 | hidnplayr | 818 | call TCP_output |
1254 | hidnplayr | 819 | |
1249 | hidnplayr | 820 | mov [esp+32], eax |
821 | ret |
||
1159 | hidnplayr | 822 | |
1249 | hidnplayr | 823 | |
1543 | hidnplayr | 824 | align 4 |
825 | SOCKET_send_ip: |
||
1249 | hidnplayr | 826 | |
1543 | hidnplayr | 827 | DEBUGF 1,"type: IP\n" |
828 | |||
829 | mov ecx, esi |
||
830 | mov esi, edx |
||
831 | |||
832 | call IPv4_output_raw |
||
833 | |||
834 | mov dword [esp+32], eax |
||
835 | ret |
||
836 | |||
1541 | hidnplayr | 837 | align 4 |
838 | SOCKET_send_icmp: |
||
1249 | hidnplayr | 839 | |
1541 | hidnplayr | 840 | DEBUGF 1,"SOCKET_send: ICMP\n" |
841 | |||
842 | mov ecx, esi |
||
843 | call ICMP_output_raw |
||
844 | |||
845 | mov dword [esp+32], 0 |
||
846 | ret |
||
847 | |||
848 | |||
849 | |||
850 | |||
1257 | hidnplayr | 851 | ;----------------------------------------------------------------- |
1256 | clevermous | 852 | ; |
1257 | hidnplayr | 853 | ; SOCKET_get_options |
1256 | clevermous | 854 | ; |
1514 | hidnplayr | 855 | ; IN: ecx = socket number |
856 | ; edx = pointer to the options: |
||
1257 | hidnplayr | 857 | ; dd level, optname, optval, optlen |
1256 | clevermous | 858 | ; OUT: -1 on error |
859 | ; |
||
860 | ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP. |
||
861 | ; TODO: find best way to notify that send()'ed data were acknowledged |
||
1299 | clevermous | 862 | ; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*. |
1256 | clevermous | 863 | ; |
1257 | hidnplayr | 864 | ;----------------------------------------------------------------- |
865 | align 4 |
||
1514 | hidnplayr | 866 | SOCKET_get_opt: |
1257 | hidnplayr | 867 | |
1536 | hidnplayr | 868 | DEBUGF 1,"SOCKET_get_opt\n" |
1514 | hidnplayr | 869 | |
870 | call SOCKET_num_to_ptr |
||
871 | jz s_error |
||
872 | |||
1256 | clevermous | 873 | cmp dword [edx], IP_PROTO_TCP |
1514 | hidnplayr | 874 | jnz s_error |
1256 | clevermous | 875 | cmp dword [edx+4], -2 |
1299 | clevermous | 876 | jz @f |
877 | cmp dword [edx+4], -3 |
||
1514 | hidnplayr | 878 | jnz s_error |
1299 | clevermous | 879 | @@: |
1514 | hidnplayr | 880 | ; mov eax, [edx+12] |
881 | ; test eax, eax |
||
882 | ; jz .fail |
||
883 | ; cmp dword [eax], 4 |
||
884 | ; mov dword [eax], 4 |
||
885 | ; jb .fail |
||
886 | ; stdcall net_socket_num_to_addr, ecx |
||
887 | ; test eax, eax |
||
888 | ; jz .fail |
||
889 | ; ; todo: check that eax is really TCP socket |
||
890 | ; mov ecx, [eax + TCP_SOCKET.last_ack_number] |
||
891 | ; cmp dword [edx+4], -2 |
||
892 | ; jz @f |
||
893 | ; mov ecx, [eax + TCP_SOCKET.state] |
||
1299 | clevermous | 894 | @@: |
1256 | clevermous | 895 | mov eax, [edx+8] |
896 | test eax, eax |
||
897 | jz @f |
||
898 | mov [eax], ecx |
||
899 | @@: |
||
1257 | hidnplayr | 900 | mov dword [esp+32], 0 |
1256 | clevermous | 901 | ret |
1159 | hidnplayr | 902 | |
903 | |||
1529 | hidnplayr | 904 | |
1542 | hidnplayr | 905 | |
906 | align 4 |
||
907 | SOCKET_set_opt: |
||
908 | |||
909 | ret |
||
910 | |||
911 | |||
912 | |||
1257 | hidnplayr | 913 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 914 | ; |
1529 | hidnplayr | 915 | ; SOCKET_debug |
916 | ; |
||
917 | ; Copies socket variables to application buffer |
||
918 | ; |
||
919 | ; IN: ecx = socket number |
||
920 | ; edx = pointer to buffer |
||
921 | ; |
||
922 | ; OUT: -1 on error |
||
923 | ;----------------------------------------------------------------- |
||
924 | align 4 |
||
925 | SOCKET_debug: |
||
926 | |||
927 | DEBUGF 1,"socket_debug\n" |
||
928 | |||
929 | call SOCKET_num_to_ptr |
||
930 | jz s_error |
||
931 | |||
932 | mov esi, eax |
||
933 | mov edi, edx |
||
934 | mov ecx, SOCKETBUFFSIZE/4 |
||
935 | rep movsd |
||
936 | |||
937 | mov dword [esp+32], 0 |
||
938 | ret |
||
939 | |||
940 | |||
941 | ;----------------------------------------------------------------- |
||
942 | ; |
||
1514 | hidnplayr | 943 | ; SOCKET_find_port |
1206 | hidnplayr | 944 | ; |
1514 | hidnplayr | 945 | ; Fills in the local port number for TCP and UDP sockets |
946 | ; This procedure always works because the number of sockets is |
||
947 | ; limited to a smaller number then the number of possible ports |
||
1206 | hidnplayr | 948 | ; |
1514 | hidnplayr | 949 | ; IN: eax = socket pointer |
950 | ; OUT: / |
||
1206 | hidnplayr | 951 | ; |
1257 | hidnplayr | 952 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 953 | align 4 |
1514 | hidnplayr | 954 | SOCKET_find_port: |
1159 | hidnplayr | 955 | |
1536 | hidnplayr | 956 | DEBUGF 1,"SOCKET_find_port\n" |
1159 | hidnplayr | 957 | |
1514 | hidnplayr | 958 | push ebx esi ecx |
959 | |||
1542 | hidnplayr | 960 | cmp [eax + SOCKET.Protocol], IP_PROTO_UDP |
1206 | hidnplayr | 961 | je .udp |
1159 | hidnplayr | 962 | |
1542 | hidnplayr | 963 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1206 | hidnplayr | 964 | je .tcp |
1159 | hidnplayr | 965 | |
1514 | hidnplayr | 966 | jmp .error |
967 | |||
968 | .done: |
||
969 | mov [eax + UDP_SOCKET.LocalPort], bx |
||
970 | .error: |
||
971 | pop ecx esi ebx |
||
972 | ret |
||
973 | |||
1206 | hidnplayr | 974 | .udp: |
975 | mov bx, [last_UDP_port] |
||
1514 | hidnplayr | 976 | call .findit |
977 | mov [last_UDP_port], bx |
||
978 | jmp .done |
||
1206 | hidnplayr | 979 | |
980 | .tcp: |
||
981 | mov bx, [last_TCP_port] |
||
1514 | hidnplayr | 982 | call .findit |
983 | mov [last_TCP_port], bx |
||
984 | jmp .done |
||
1206 | hidnplayr | 985 | |
986 | |||
1514 | hidnplayr | 987 | .restart: |
988 | mov bx, MIN_EPHEMERAL_PORT |
||
989 | .findit: |
||
1206 | hidnplayr | 990 | inc bx |
991 | |||
992 | cmp bx, MAX_EPHEMERAL_PORT |
||
1514 | hidnplayr | 993 | jz .restart |
1206 | hidnplayr | 994 | |
1514 | hidnplayr | 995 | call SOCKET_check_port |
996 | jz .findit |
||
1206 | hidnplayr | 997 | |
998 | ret |
||
999 | |||
1257 | hidnplayr | 1000 | |
1001 | |||
1002 | ;----------------------------------------------------------------- |
||
1206 | hidnplayr | 1003 | ; |
1542 | hidnplayr | 1004 | ; SOCKET_check_port (to be used with AF_INET only!) |
1206 | hidnplayr | 1005 | ; |
1514 | hidnplayr | 1006 | ; Checks if a local port number is unused |
1007 | ; If the proposed port number is unused, it is filled in in the socket structure |
||
1206 | hidnplayr | 1008 | ; |
1514 | hidnplayr | 1009 | ; IN: eax = socket ptr (to find out if its a TCP/UDP socket) |
1010 | ; bx = proposed socket number |
||
1206 | hidnplayr | 1011 | ; |
1514 | hidnplayr | 1012 | ; OUT: ZF = cleared on error |
1013 | ; |
||
1257 | hidnplayr | 1014 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 1015 | align 4 |
1514 | hidnplayr | 1016 | SOCKET_check_port: |
1017 | |||
1536 | hidnplayr | 1018 | DEBUGF 1,"SOCKET_check_port\n" |
1514 | hidnplayr | 1019 | |
1542 | hidnplayr | 1020 | mov ecx, [eax + SOCKET.Protocol] |
1206 | hidnplayr | 1021 | mov esi, net_sockets |
1022 | |||
1023 | .next_socket: |
||
1514 | hidnplayr | 1024 | mov esi, [esi + SOCKET.NextPtr] |
1206 | hidnplayr | 1025 | or esi, esi |
1026 | jz .port_ok |
||
1027 | |||
1542 | hidnplayr | 1028 | cmp [esi + SOCKET.Protocol], ecx |
1206 | hidnplayr | 1029 | jne .next_socket |
1030 | |||
1514 | hidnplayr | 1031 | cmp [esi + UDP_SOCKET.LocalPort], bx |
1206 | hidnplayr | 1032 | jne .next_socket |
1033 | |||
1514 | hidnplayr | 1034 | DEBUGF 1,"local port %u already in use\n", bx |
1035 | ret |
||
1206 | hidnplayr | 1036 | |
1037 | .port_ok: |
||
1514 | hidnplayr | 1038 | mov [eax + UDP_SOCKET.LocalPort], bx |
1039 | or bx, bx ; set the zero-flag |
||
1040 | |||
1206 | hidnplayr | 1041 | ret |
1042 | |||
1043 | |||
1257 | hidnplayr | 1044 | |
1045 | ;----------------------------------------------------------------- |
||
1206 | hidnplayr | 1046 | ; |
1514 | hidnplayr | 1047 | ; SOCKET_input |
1206 | hidnplayr | 1048 | ; |
1536 | hidnplayr | 1049 | ; Updates a (stateless) socket with received data |
1206 | hidnplayr | 1050 | ; |
1514 | hidnplayr | 1051 | ; Note: the mutex should already be set ! |
1206 | hidnplayr | 1052 | ; |
1249 | hidnplayr | 1053 | ; IN: eax = socket ptr |
1514 | hidnplayr | 1054 | ; ebx = pointer to device struct |
1055 | ; ecx = data size |
||
1056 | ; esi = ptr to data |
||
1057 | ; [esp] = ptr to buf |
||
1058 | ; [esp + 4] = buf size |
||
1249 | hidnplayr | 1059 | ; |
1514 | hidnplayr | 1060 | ; OUT: / |
1206 | hidnplayr | 1061 | ; |
1257 | hidnplayr | 1062 | ;----------------------------------------------------------------- |
1206 | hidnplayr | 1063 | align 4 |
1514 | hidnplayr | 1064 | SOCKET_input: |
1206 | hidnplayr | 1065 | |
1536 | hidnplayr | 1066 | DEBUGF 1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx |
1206 | hidnplayr | 1067 | |
1514 | hidnplayr | 1068 | mov dword[esp+4], ecx |
1069 | push esi |
||
1249 | hidnplayr | 1070 | mov esi, esp |
1514 | hidnplayr | 1071 | |
1536 | hidnplayr | 1072 | add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, SOCKET_input.full |
1514 | hidnplayr | 1073 | |
1541 | hidnplayr | 1074 | DEBUGF 1,"SOCKET_input: queued packet successfully\n" |
1257 | hidnplayr | 1075 | add esp, socket_queue_entry.size |
1514 | hidnplayr | 1076 | mov [eax + SOCKET.lock], 0 |
1077 | jmp SOCKET_notify_owner |
||
1206 | hidnplayr | 1078 | |
1514 | hidnplayr | 1079 | .full: |
1541 | hidnplayr | 1080 | DEBUGF 2,"SOCKET_input: socket %x is full!\n", eax |
1514 | hidnplayr | 1081 | mov [eax + SOCKET.lock], 0 |
1082 | call kernel_free |
||
1083 | add esp, 8 |
||
1206 | hidnplayr | 1084 | |
1514 | hidnplayr | 1085 | ret |
1086 | |||
1533 | hidnplayr | 1087 | |
1088 | ;-------------------------- |
||
1089 | ; |
||
1090 | ; eax = ptr to ring struct (just a buffer of the right size) |
||
1091 | ; |
||
1092 | align 4 |
||
1093 | SOCKET_ring_create: |
||
1094 | |||
1543 | hidnplayr | 1095 | push esi |
1533 | hidnplayr | 1096 | mov esi, eax |
1543 | hidnplayr | 1097 | |
1098 | push edx |
||
1533 | hidnplayr | 1099 | stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW |
1543 | hidnplayr | 1100 | pop edx |
1533 | hidnplayr | 1101 | |
1102 | DEBUGF 1,"SOCKET_ring_created: %x\n", eax |
||
1103 | mov [esi + RING_BUFFER.start_ptr], eax |
||
1104 | mov [esi + RING_BUFFER.write_ptr], eax |
||
1105 | mov [esi + RING_BUFFER.read_ptr], eax |
||
1106 | mov [esi + RING_BUFFER.size], 0 |
||
1107 | add eax, SOCKET_MAXDATA |
||
1108 | mov [esi + RING_BUFFER.end_ptr], eax |
||
1543 | hidnplayr | 1109 | mov eax, esi |
1110 | pop esi |
||
1533 | hidnplayr | 1111 | |
1112 | ret |
||
1113 | |||
1514 | hidnplayr | 1114 | ;----------------------------------------------------------------- |
1115 | ; |
||
1533 | hidnplayr | 1116 | ; SOCKET_ring_write |
1529 | hidnplayr | 1117 | ; |
1533 | hidnplayr | 1118 | ; Adds data to a stream socket, and updates write pointer and size |
1529 | hidnplayr | 1119 | ; |
1120 | ; IN: eax = ptr to ring struct |
||
1121 | ; ecx = data size |
||
1122 | ; esi = ptr to data |
||
1123 | ; |
||
1533 | hidnplayr | 1124 | ; OUT: ecx = number of bytes stored |
1529 | hidnplayr | 1125 | ; |
1126 | ;----------------------------------------------------------------- |
||
1127 | align 4 |
||
1533 | hidnplayr | 1128 | SOCKET_ring_write: |
1529 | hidnplayr | 1129 | |
1533 | hidnplayr | 1130 | DEBUGF 1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx |
1529 | hidnplayr | 1131 | |
1533 | hidnplayr | 1132 | add [eax + RING_BUFFER.size], ecx |
1133 | cmp [eax + RING_BUFFER.size], SOCKET_MAXDATA |
||
1529 | hidnplayr | 1134 | jg .too_large |
1135 | |||
1136 | .copy: |
||
1533 | hidnplayr | 1137 | mov edi, [eax + RING_BUFFER.write_ptr] |
1541 | hidnplayr | 1138 | DEBUGF 2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi |
1529 | hidnplayr | 1139 | |
1140 | push ecx |
||
1533 | hidnplayr | 1141 | shr ecx, 1 |
1142 | jnc .nb |
||
1143 | movsb |
||
1536 | hidnplayr | 1144 | .nb: |
1533 | hidnplayr | 1145 | shr ecx, 1 |
1146 | jnc .nw |
||
1147 | movsw |
||
1536 | hidnplayr | 1148 | .nw: |
1533 | hidnplayr | 1149 | test ecx, ecx |
1150 | jz .nd |
||
1151 | rep movsd |
||
1536 | hidnplayr | 1152 | .nd: |
1529 | hidnplayr | 1153 | pop ecx |
1154 | |||
1533 | hidnplayr | 1155 | cmp edi, [eax + RING_BUFFER.end_ptr] |
1156 | jge .wrap |
||
1157 | mov [eax + RING_BUFFER.write_ptr], edi |
||
1158 | |||
1529 | hidnplayr | 1159 | ret |
1160 | |||
1533 | hidnplayr | 1161 | .wrap: |
1162 | sub edi, SOCKET_MAXDATA |
||
1163 | mov [eax + RING_BUFFER.write_ptr], edi |
||
1164 | |||
1165 | ret |
||
1166 | |||
1529 | hidnplayr | 1167 | .too_large: |
1168 | mov ecx, SOCKET_MAXDATA ; calculate number of bytes available in buffer |
||
1169 | sub ecx, [eax + RING_BUFFER.size] |
||
1533 | hidnplayr | 1170 | jge .full |
1529 | hidnplayr | 1171 | |
1172 | mov [eax + RING_BUFFER.size], SOCKET_MAXDATA ; update size, we will fill buffer completely |
||
1173 | jmp .copy |
||
1174 | |||
1175 | .full: |
||
1541 | hidnplayr | 1176 | DEBUGF 2,"SOCKET_ring_write: ring buffer is full!\n" |
1529 | hidnplayr | 1177 | xor ecx, ecx |
1178 | ret |
||
1179 | |||
1180 | |||
1181 | ;----------------------------------------------------------------- |
||
1182 | ; |
||
1183 | ; SOCKET_ring_read |
||
1184 | ; |
||
1533 | hidnplayr | 1185 | ; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET |
1529 | hidnplayr | 1186 | ; |
1187 | ; IN: eax = ptr to ring struct |
||
1188 | ; ecx = buffer size |
||
1189 | ; edi = ptr to buffer |
||
1190 | ; |
||
1533 | hidnplayr | 1191 | ; OUT: ecx = number of bytes read |
1529 | hidnplayr | 1192 | ; |
1193 | ;----------------------------------------------------------------- |
||
1194 | align 4 |
||
1195 | SOCKET_ring_read: |
||
1196 | |||
1533 | hidnplayr | 1197 | DEBUGF 1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx |
1529 | hidnplayr | 1198 | |
1533 | hidnplayr | 1199 | cmp ecx, [eax + RING_BUFFER.size] |
1200 | jg .less_data |
||
1529 | hidnplayr | 1201 | |
1202 | .copy: |
||
1533 | hidnplayr | 1203 | mov esi, [eax + RING_BUFFER.read_ptr] |
1529 | hidnplayr | 1204 | |
1541 | hidnplayr | 1205 | DEBUGF 2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi |
1529 | hidnplayr | 1206 | push ecx |
1533 | hidnplayr | 1207 | shr ecx, 1 |
1208 | jnc .nb |
||
1209 | movsb |
||
1536 | hidnplayr | 1210 | .nb: |
1533 | hidnplayr | 1211 | shr ecx, 1 |
1212 | jnc .nw |
||
1213 | movsw |
||
1536 | hidnplayr | 1214 | .nw: |
1533 | hidnplayr | 1215 | test ecx, ecx |
1216 | jz .nd |
||
1217 | rep movsd |
||
1536 | hidnplayr | 1218 | .nd: |
1529 | hidnplayr | 1219 | pop ecx |
1220 | |||
1533 | hidnplayr | 1221 | ; .no_data_at_all: |
1529 | hidnplayr | 1222 | ret |
1223 | |||
1533 | hidnplayr | 1224 | .less_data: |
1529 | hidnplayr | 1225 | mov ecx, [eax + RING_BUFFER.size] |
1533 | hidnplayr | 1226 | test ecx, ecx |
1227 | ; jz .no_data_at_all |
||
1529 | hidnplayr | 1228 | jmp .copy |
1229 | |||
1230 | |||
1231 | ;----------------------------------------------------------------- |
||
1232 | ; |
||
1233 | ; SOCKET_ring_free |
||
1234 | ; |
||
1235 | ; Free's some bytes from the ringbuffer |
||
1236 | ; |
||
1237 | ; IN: eax = ptr to ring struct |
||
1238 | ; ecx = data size |
||
1239 | ; |
||
1240 | ; OUT: ecx = number of bytes free-ed |
||
1241 | ; |
||
1242 | ;----------------------------------------------------------------- |
||
1243 | align 4 |
||
1244 | SOCKET_ring_free: |
||
1245 | |||
1536 | hidnplayr | 1246 | DEBUGF 1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax |
1529 | hidnplayr | 1247 | |
1248 | sub [eax + RING_BUFFER.size], ecx |
||
1533 | hidnplayr | 1249 | jl .sumthinwong |
1529 | hidnplayr | 1250 | add [eax + RING_BUFFER.read_ptr], ecx |
1251 | |||
1252 | mov edx, [eax + RING_BUFFER.end_ptr] |
||
1253 | cmp [eax + RING_BUFFER.read_ptr], edx |
||
1254 | jl @f |
||
1533 | hidnplayr | 1255 | sub [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA |
1529 | hidnplayr | 1256 | @@: |
1257 | ret |
||
1258 | |||
1533 | hidnplayr | 1259 | .sumthinwong: ; we could free all available bytes, but that would be stupid, i guess.. |
1260 | add [eax + RING_BUFFER.size], ecx |
||
1529 | hidnplayr | 1261 | xor ecx, ecx |
1262 | ret |
||
1263 | |||
1264 | |||
1265 | ;----------------------------------------------------------------- |
||
1266 | ; |
||
1514 | hidnplayr | 1267 | ; SOCKET_notify_owner |
1268 | ; |
||
1269 | ; notify's the owner of a socket that something happened |
||
1270 | ; |
||
1271 | ; IN: eax = socket ptr |
||
1272 | ; OUT: / |
||
1273 | ; |
||
1274 | ;----------------------------------------------------------------- |
||
1275 | align 4 |
||
1276 | SOCKET_notify_owner: |
||
1277 | |||
1536 | hidnplayr | 1278 | DEBUGF 1,"SOCKET_notify_owner: %x\n", eax |
1514 | hidnplayr | 1279 | |
1280 | call SOCKET_check |
||
1281 | jz .error |
||
1282 | |||
1543 | hidnplayr | 1283 | push eax ecx esi |
1514 | hidnplayr | 1284 | |
1285 | ; socket exists, now try to flag an event to the application |
||
1286 | |||
1287 | mov eax, [eax + SOCKET.PID] |
||
1206 | hidnplayr | 1288 | mov ecx, 1 |
1289 | mov esi, TASK_DATA + TASKDATA.pid |
||
1290 | |||
1291 | .next_pid: |
||
1514 | hidnplayr | 1292 | cmp [esi], eax |
1206 | hidnplayr | 1293 | je .found_pid |
1294 | inc ecx |
||
1295 | add esi, 0x20 |
||
1296 | cmp ecx, [TASK_COUNT] |
||
1297 | jbe .next_pid |
||
1298 | |||
1514 | hidnplayr | 1299 | ; PID not found, TODO: close socket! |
1300 | |||
1301 | jmp .error2 |
||
1302 | |||
1206 | hidnplayr | 1303 | .found_pid: |
1304 | shl ecx, 8 |
||
1514 | hidnplayr | 1305 | or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK |
1206 | hidnplayr | 1306 | mov [check_idle_semaphore], 200 |
1307 | |||
1536 | hidnplayr | 1308 | DEBUGF 1,"SOCKET_notify_owner: succes!\n" |
1514 | hidnplayr | 1309 | |
1310 | .error2: |
||
1543 | hidnplayr | 1311 | pop esi ecx eax |
1536 | hidnplayr | 1312 | |
1514 | hidnplayr | 1313 | .error: |
1314 | |||
1206 | hidnplayr | 1315 | ret |
1316 | |||
1317 | |||
1514 | hidnplayr | 1318 | ;-------------------------------------------------------------------- |
1319 | ; |
||
1320 | ; SOCKET_alloc |
||
1321 | ; |
||
1159 | hidnplayr | 1322 | ; Allocate memory for socket data and put new socket into the list |
1323 | ; Newly created socket is initialized with calling PID and number and |
||
1324 | ; put into beginning of list (which is a fastest way). |
||
1325 | ; |
||
1514 | hidnplayr | 1326 | ; IN: / |
1327 | ; OUT: eax = 0 on error, socket ptr otherwise |
||
1328 | ; edi = socket number |
||
1329 | ; ZF = cleared on error |
||
1159 | hidnplayr | 1330 | ; |
1514 | hidnplayr | 1331 | ;-------------------------------------------------------------------- |
1332 | align 4 |
||
1333 | SOCKET_alloc: |
||
1334 | |||
1335 | push ecx ebx |
||
1336 | |||
1159 | hidnplayr | 1337 | stdcall kernel_alloc, SOCKETBUFFSIZE |
1529 | hidnplayr | 1338 | DEBUGF 1, "SOCKET_alloc: ptr=%x\n", eax |
1159 | hidnplayr | 1339 | or eax, eax |
1340 | jz .exit |
||
1341 | |||
1514 | hidnplayr | 1342 | ; zero-initialize allocated memory |
1343 | push eax edi |
||
1159 | hidnplayr | 1344 | mov edi, eax |
1345 | mov ecx, SOCKETBUFFSIZE / 4 |
||
1346 | xor eax, eax |
||
1347 | rep stosd |
||
1514 | hidnplayr | 1348 | pop edi eax |
1159 | hidnplayr | 1349 | |
1536 | hidnplayr | 1350 | ; set send-and receive procedures to return -1 |
1351 | mov [eax + SOCKET.snd_proc], s_error |
||
1352 | mov [eax + SOCKET.rcv_proc], s_error |
||
1353 | |||
1514 | hidnplayr | 1354 | ; find first free socket number and use it |
1543 | hidnplayr | 1355 | mov ecx, [last_socket_num] |
1159 | hidnplayr | 1356 | .next_socket_number: |
1357 | inc ecx |
||
1543 | hidnplayr | 1358 | jz .next_socket_number ; avoid socket nr 0 |
1359 | cmp ecx, -1 |
||
1360 | je .next_socket_number ; avoid socket nr -1 |
||
1536 | hidnplayr | 1361 | mov ebx, net_sockets |
1159 | hidnplayr | 1362 | .next_socket: |
1514 | hidnplayr | 1363 | mov ebx, [ebx + SOCKET.NextPtr] |
1529 | hidnplayr | 1364 | test ebx, ebx |
1514 | hidnplayr | 1365 | jz .last_socket |
1536 | hidnplayr | 1366 | |
1514 | hidnplayr | 1367 | cmp [ebx + SOCKET.Number], ecx |
1159 | hidnplayr | 1368 | jne .next_socket |
1369 | jmp .next_socket_number |
||
1370 | |||
1514 | hidnplayr | 1371 | .last_socket: |
1543 | hidnplayr | 1372 | mov [last_socket_num], ecx |
1514 | hidnplayr | 1373 | mov [eax + SOCKET.Number], ecx |
1529 | hidnplayr | 1374 | DEBUGF 1, "SOCKET_alloc: number=%u\n", ecx |
1375 | mov edi, ecx |
||
1159 | hidnplayr | 1376 | |
1514 | hidnplayr | 1377 | ; Fill in PID |
1378 | mov ebx, [TASK_BASE] |
||
1379 | mov ebx, [ebx + TASKDATA.pid] |
||
1380 | mov [eax + SOCKET.PID], ebx |
||
1381 | |||
1529 | hidnplayr | 1382 | ; add socket to the list by re-arranging some pointers |
1514 | hidnplayr | 1383 | mov ebx, [net_sockets + SOCKET.NextPtr] |
1384 | |||
1385 | mov [eax + SOCKET.PrevPtr], net_sockets |
||
1386 | mov [eax + SOCKET.NextPtr], ebx |
||
1387 | |||
1529 | hidnplayr | 1388 | test ebx, ebx |
1514 | hidnplayr | 1389 | jz @f |
1390 | add ebx, SOCKET.lock ; lock the next socket |
||
1391 | call wait_mutex |
||
1392 | sub ebx, SOCKET.lock |
||
1393 | mov [ebx + SOCKET.PrevPtr], eax |
||
1536 | hidnplayr | 1394 | mov [ebx + SOCKET.lock], 0 ; and unlock it again |
1514 | hidnplayr | 1395 | @@: |
1396 | |||
1397 | mov [net_sockets + SOCKET.NextPtr], eax |
||
1529 | hidnplayr | 1398 | or eax, eax ; used to clear zero flag |
1159 | hidnplayr | 1399 | .exit: |
1514 | hidnplayr | 1400 | pop ebx ecx |
1401 | |||
1159 | hidnplayr | 1402 | ret |
1403 | |||
1514 | hidnplayr | 1404 | |
1405 | ;---------------------------------------------------- |
||
1159 | hidnplayr | 1406 | ; |
1514 | hidnplayr | 1407 | ; SOCKET_free |
1159 | hidnplayr | 1408 | ; |
1514 | hidnplayr | 1409 | ; Free socket data memory and remove socket from the list |
1410 | ; |
||
1411 | ; IN: eax = socket ptr |
||
1412 | ; OUT: / |
||
1413 | ; |
||
1414 | ;---------------------------------------------------- |
||
1415 | align 4 |
||
1416 | SOCKET_free: |
||
1159 | hidnplayr | 1417 | |
1536 | hidnplayr | 1418 | DEBUGF 1, "SOCKET_free: %x\n", eax |
1514 | hidnplayr | 1419 | |
1420 | call SOCKET_check |
||
1159 | hidnplayr | 1421 | jz .error |
1422 | |||
1514 | hidnplayr | 1423 | push ebx |
1424 | lea ebx, [eax + SOCKET.lock] |
||
1425 | call wait_mutex |
||
1426 | |||
1541 | hidnplayr | 1427 | DEBUGF 1, "SOCKET_free: freeing socket..\n" |
1514 | hidnplayr | 1428 | |
1529 | hidnplayr | 1429 | cmp [eax + SOCKET.Domain], AF_INET4 |
1536 | hidnplayr | 1430 | jnz .no_tcp |
1529 | hidnplayr | 1431 | |
1542 | hidnplayr | 1432 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1536 | hidnplayr | 1433 | jnz .no_tcp |
1529 | hidnplayr | 1434 | |
1536 | hidnplayr | 1435 | mov ebx, eax |
1436 | stdcall kernel_free, [ebx + STREAM_SOCKET.rcv + RING_BUFFER.start_ptr] |
||
1437 | stdcall kernel_free, [ebx + STREAM_SOCKET.snd + RING_BUFFER.start_ptr] |
||
1438 | mov eax, ebx |
||
1439 | .no_tcp: |
||
1529 | hidnplayr | 1440 | |
1514 | hidnplayr | 1441 | push eax ; this will be passed to kernel_free |
1442 | mov ebx, [eax + SOCKET.NextPtr] |
||
1443 | mov eax, [eax + SOCKET.PrevPtr] |
||
1444 | |||
1541 | hidnplayr | 1445 | DEBUGF 1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx |
1514 | hidnplayr | 1446 | |
1447 | test eax, eax |
||
1159 | hidnplayr | 1448 | jz @f |
1514 | hidnplayr | 1449 | mov [eax + SOCKET.NextPtr], ebx |
1450 | @@: |
||
1159 | hidnplayr | 1451 | |
1514 | hidnplayr | 1452 | test ebx, ebx |
1453 | jz @f |
||
1454 | mov [ebx + SOCKET.PrevPtr], eax |
||
1455 | @@: |
||
1249 | hidnplayr | 1456 | |
1514 | hidnplayr | 1457 | call kernel_free |
1458 | pop ebx |
||
1159 | hidnplayr | 1459 | |
1541 | hidnplayr | 1460 | DEBUGF 1, "SOCKET_free: success!\n" |
1514 | hidnplayr | 1461 | |
1159 | hidnplayr | 1462 | .error: |
1463 | ret |
||
1464 | |||
1543 | hidnplayr | 1465 | ;------------------------------------ |
1466 | ; |
||
1467 | ; SOCKET_fork |
||
1468 | ; |
||
1469 | ; Create a child socket |
||
1470 | ; |
||
1533 | hidnplayr | 1471 | ; IN: socket nr in ebx |
1543 | hidnplayr | 1472 | ; OUT: child socket nr in eax |
1473 | ; |
||
1474 | ;----------------------------------- |
||
1529 | hidnplayr | 1475 | align 4 |
1476 | SOCKET_fork: |
||
1477 | |||
1543 | hidnplayr | 1478 | DEBUGF 1,"SOCKET_fork: %x\n", ebx |
1529 | hidnplayr | 1479 | |
1543 | hidnplayr | 1480 | ; Exit if backlog queue is full |
1481 | mov eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size] |
||
1482 | cmp ax, [ebx + SOCKET.backlog] |
||
1483 | jge .fail |
||
1484 | |||
1529 | hidnplayr | 1485 | ; Allocate new socket |
1486 | call SOCKET_alloc |
||
1543 | hidnplayr | 1487 | jz .fail |
1529 | hidnplayr | 1488 | |
1543 | hidnplayr | 1489 | push esi ecx edi |
1490 | push eax |
||
1491 | mov esi, esp |
||
1492 | add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2 |
||
1493 | pop eax |
||
1494 | |||
1495 | ; Copy structure from current socket to new |
||
1496 | ; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket |
||
1497 | lea esi, [ebx + SOCKET.PID] |
||
1529 | hidnplayr | 1498 | lea edi, [eax + SOCKET.PID] |
1543 | hidnplayr | 1499 | mov ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4 |
1529 | hidnplayr | 1500 | rep movsd |
1501 | |||
1543 | hidnplayr | 1502 | and [eax + SOCKET.options], not SO_ACCEPTCON |
1529 | hidnplayr | 1503 | |
1543 | hidnplayr | 1504 | call SOCKET_notify_owner |
1505 | pop edi ecx esi |
||
1529 | hidnplayr | 1506 | |
1507 | ret |
||
1508 | |||
1543 | hidnplayr | 1509 | .fail2: |
1510 | add esp, 4+4+4 |
||
1511 | .fail: |
||
1512 | DEBUGF 1,"SOCKET_fork: failed\n" |
||
1513 | xor eax, eax |
||
1514 | ret |
||
1529 | hidnplayr | 1515 | |
1543 | hidnplayr | 1516 | |
1514 | hidnplayr | 1517 | ;--------------------------------------------------- |
1518 | ; |
||
1519 | ; SOCKET_num_to_ptr |
||
1520 | ; |
||
1159 | hidnplayr | 1521 | ; Get socket structure address by its number |
1522 | ; |
||
1514 | hidnplayr | 1523 | ; IN: ecx = socket number |
1533 | hidnplayr | 1524 | ; OUT: eax = 0 on error, socket ptr otherwise |
1514 | hidnplayr | 1525 | ; ZF = set on error |
1159 | hidnplayr | 1526 | ; |
1514 | hidnplayr | 1527 | ;--------------------------------------------------- |
1528 | align 4 |
||
1529 | SOCKET_num_to_ptr: |
||
1159 | hidnplayr | 1530 | |
1536 | hidnplayr | 1531 | DEBUGF 1,"SOCKET_num_to_ptr: %u ", ecx |
1514 | hidnplayr | 1532 | |
1533 | mov eax, net_sockets |
||
1534 | |||
1159 | hidnplayr | 1535 | .next_socket: |
1514 | hidnplayr | 1536 | mov eax, [eax + SOCKET.NextPtr] |
1537 | or eax, eax |
||
1159 | hidnplayr | 1538 | jz .error |
1514 | hidnplayr | 1539 | cmp [eax + SOCKET.Number], ecx |
1159 | hidnplayr | 1540 | jne .next_socket |
1541 | |||
1514 | hidnplayr | 1542 | test eax, eax |
1159 | hidnplayr | 1543 | |
1514 | hidnplayr | 1544 | DEBUGF 1,"(%x)\n", eax |
1159 | hidnplayr | 1545 | .error: |
1546 | ret |
||
1547 | |||
1514 | hidnplayr | 1548 | |
1549 | ;--------------------------------------------------- |
||
1159 | hidnplayr | 1550 | ; |
1514 | hidnplayr | 1551 | ; SOCKET_ptr_to_num |
1159 | hidnplayr | 1552 | ; |
1514 | hidnplayr | 1553 | ; Get socket number by its address |
1554 | ; |
||
1555 | ; IN: eax = socket ptr |
||
1556 | ; OUT: eax = 0 on error, socket num otherwise |
||
1557 | ; ZF = set on error |
||
1558 | ; |
||
1559 | ;--------------------------------------------------- |
||
1560 | align 4 |
||
1561 | SOCKET_ptr_to_num: |
||
1562 | |||
1536 | hidnplayr | 1563 | DEBUGF 1,"SOCKET_ptr_to_num: %x ", eax |
1514 | hidnplayr | 1564 | |
1565 | call SOCKET_check |
||
1159 | hidnplayr | 1566 | jz .error |
1567 | |||
1514 | hidnplayr | 1568 | mov eax, [eax + SOCKET.Number] |
1569 | |||
1570 | DEBUGF 1,"(%u)\n", eax |
||
1571 | |||
1572 | .error: |
||
1573 | ret |
||
1574 | |||
1575 | |||
1576 | ;--------------------------------------------------- |
||
1577 | ; |
||
1578 | ; SOCKET_check |
||
1579 | ; |
||
1580 | ; checks if the given value is really a socket ptr |
||
1581 | ; |
||
1582 | ; IN: eax = socket ptr |
||
1583 | ; OUT: eax = 0 on error, unchanged otherwise |
||
1584 | ; ZF = set on error |
||
1585 | ; |
||
1586 | ;--------------------------------------------------- |
||
1587 | align 4 |
||
1588 | SOCKET_check: |
||
1589 | |||
1536 | hidnplayr | 1590 | DEBUGF 1,"SOCKET_check: %x\n", eax |
1514 | hidnplayr | 1591 | |
1592 | push ebx |
||
1159 | hidnplayr | 1593 | mov ebx, net_sockets |
1514 | hidnplayr | 1594 | |
1159 | hidnplayr | 1595 | .next_socket: |
1514 | hidnplayr | 1596 | mov ebx, [ebx + SOCKET.NextPtr] |
1159 | hidnplayr | 1597 | or ebx, ebx |
1514 | hidnplayr | 1598 | jz .done |
1159 | hidnplayr | 1599 | cmp ebx, eax |
1514 | hidnplayr | 1600 | jnz .next_socket |
1159 | hidnplayr | 1601 | |
1514 | hidnplayr | 1602 | .done: |
1603 | mov eax, ebx |
||
1604 | test eax, eax |
||
1605 | pop ebx |
||
1606 | |||
1159 | hidnplayr | 1607 | ret |
1608 | |||
1514 | hidnplayr | 1609 | |
1610 | |||
1611 | ;--------------------------------------------------- |
||
1612 | ; |
||
1613 | ; SOCKET_check_owner |
||
1614 | ; |
||
1615 | ; checks if the caller application owns the socket |
||
1616 | ; |
||
1617 | ; IN: eax = socket ptr |
||
1618 | ; OUT: ZF = true/false |
||
1619 | ; |
||
1620 | ;--------------------------------------------------- |
||
1621 | align 4 |
||
1622 | SOCKET_check_owner: |
||
1623 | |||
1536 | hidnplayr | 1624 | DEBUGF 1,"SOCKET_check_owner: %x\n", eax |
1514 | hidnplayr | 1625 | |
1626 | push ebx |
||
1627 | mov ebx, [TASK_BASE] |
||
1628 | mov ebx, [ecx + TASKDATA.pid] |
||
1629 | cmp [eax + SOCKET.PID], ebx |
||
1630 | pop ebx |
||
1631 | |||
1159 | hidnplayr | 1632 | ret |
1514 | hidnplayr | 1633 | |
1634 | |||
1635 | |||
1636 | |||
1637 | ;--------------------------------------------------- |
||
1638 | ; |
||
1639 | ; SOCKET_process_end |
||
1640 | ; |
||
1641 | ; Kernel calls this function when a certain process ends |
||
1642 | ; This function will check if the process had any open sockets |
||
1643 | ; And update them accordingly |
||
1644 | ; |
||
1645 | ; IN: eax = pid |
||
1646 | ; OUT: / |
||
1647 | ; |
||
1648 | ;------------------------------------------------------ |
||
1649 | align 4 |
||
1650 | SOCKET_process_end: |
||
1651 | |||
1536 | hidnplayr | 1652 | DEBUGF 1,"SOCKET_process_end: %x\n", eax |
1514 | hidnplayr | 1653 | |
1654 | push ebx |
||
1655 | mov ebx, net_sockets |
||
1656 | |||
1657 | .next_socket: |
||
1658 | |||
1659 | mov ebx, [ebx + SOCKET.NextPtr] |
||
1660 | .test_socket: |
||
1661 | test ebx, ebx |
||
1662 | jz .done |
||
1663 | |||
1664 | cmp [ebx + SOCKET.PID], eax |
||
1665 | jne .next_socket |
||
1666 | |||
1667 | DEBUGF 1,"closing socket %x", eax, ebx |
||
1668 | |||
1669 | mov [ebx + SOCKET.PID], 0 |
||
1670 | |||
1542 | hidnplayr | 1671 | cmp [ebx + SOCKET.Protocol], IP_PROTO_UDP |
1514 | hidnplayr | 1672 | je .udp |
1673 | |||
1542 | hidnplayr | 1674 | cmp [ebx + SOCKET.Protocol], IP_PROTO_TCP |
1514 | hidnplayr | 1675 | je .tcp |
1676 | |||
1677 | jmp .next_socket ; kill all sockets for given PID |
||
1678 | |||
1679 | .udp: |
||
1680 | mov eax, ebx |
||
1681 | mov ebx, [ebx + SOCKET.NextPtr] |
||
1682 | call SOCKET_free |
||
1683 | jmp .test_socket |
||
1684 | |||
1685 | .tcp: |
||
1686 | |||
1687 | jmp .next_socket |
||
1688 | |||
1689 | .done: |
||
1690 | pop ebx |
||
1691 | |||
1692 | ret |