Rev 8026 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
10051 | ace_dent | 3 | ;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;; |
3545 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
||
7 | ;; ;; |
||
8 | ;; Written by hidnplayr@kolibrios.org ;; |
||
9 | ;; ;; |
||
10 | ;; Based on the code of 4.4BSD ;; |
||
11 | ;; ;; |
||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
13 | ;; Version 2, June 1991 ;; |
||
14 | ;; ;; |
||
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
16 | |||
17 | |||
6476 | hidnplayr | 18 | TCP_BIT_SENDALOT = 1 shl 0 |
19 | |||
5976 | hidnplayr | 20 | ;-----------------------------------------------------------------; |
21 | ; ; |
||
6011 | hidnplayr | 22 | ; tcp_output ; |
5976 | hidnplayr | 23 | ; ; |
24 | ; IN: eax = socket pointer ; |
||
25 | ; ; |
||
26 | ; OUT: eax = 0 on success/errorcode ; |
||
27 | ; ; |
||
28 | ;-----------------------------------------------------------------; |
||
3545 | hidnplayr | 29 | align 4 |
6011 | hidnplayr | 30 | proc tcp_output |
3545 | hidnplayr | 31 | |
4347 | hidnplayr | 32 | locals |
33 | temp_bits db ? |
||
8026 | hidnplayr | 34 | rcv_window dd ? |
4347 | hidnplayr | 35 | endl |
36 | |||
4366 | hidnplayr | 37 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket=%x state=%u\n", eax, [eax + TCP_SOCKET.t_state] |
3545 | hidnplayr | 38 | |
39 | push eax |
||
40 | lea ecx, [eax + SOCKET.mutex] |
||
41 | call mutex_lock |
||
42 | pop eax |
||
43 | |||
3652 | hidnplayr | 44 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket locked\n" |
45 | |||
3545 | hidnplayr | 46 | ; We'll detect the length of the data to be transmitted, and flags to be used |
47 | ; If there is some data, or any critical controls to send (SYN / RST), then transmit |
||
48 | ; Otherwise, investigate further |
||
49 | |||
50 | mov ebx, [eax + TCP_SOCKET.SND_MAX] |
||
51 | cmp ebx, [eax + TCP_SOCKET.SND_UNA] |
||
52 | jbe .not_idle |
||
53 | |||
54 | mov ebx, [eax + TCP_SOCKET.t_idle] |
||
55 | cmp ebx, [eax + TCP_SOCKET.t_rxtcur] |
||
56 | jbe .not_idle |
||
57 | |||
58 | ; We have been idle for a while and no ACKS are expected to clock out any data we send.. |
||
59 | ; Slow start to get ack "clock" running again. |
||
60 | |||
61 | mov ebx, [eax + TCP_SOCKET.t_maxseg] |
||
62 | mov [eax + TCP_SOCKET.SND_CWND], ebx |
||
63 | |||
64 | .not_idle: |
||
65 | .again: |
||
4347 | hidnplayr | 66 | mov [temp_bits], 0 |
3545 | hidnplayr | 67 | |
6476 | hidnplayr | 68 | ; Calculate offset |
3545 | hidnplayr | 69 | |
6476 | hidnplayr | 70 | mov ebx, [eax + TCP_SOCKET.SND_NXT] |
71 | sub ebx, [eax + TCP_SOCKET.SND_UNA] |
||
3545 | hidnplayr | 72 | |
6476 | hidnplayr | 73 | ; Determine window |
3545 | hidnplayr | 74 | |
6476 | hidnplayr | 75 | mov ecx, [eax + TCP_SOCKET.SND_WND] |
76 | cmp ecx, [eax + TCP_SOCKET.SND_CWND] |
||
77 | jb @f |
||
78 | mov ecx, [eax + TCP_SOCKET.SND_CWND] |
||
79 | @@: |
||
80 | |||
81 | ; get flags in dl |
||
82 | |||
83 | call tcp_outflags |
||
84 | |||
3545 | hidnplayr | 85 | ;------------------------ |
86 | ; data being forced out ? |
||
87 | |||
88 | ; If in persist timeout with window of 0, send 1 byte. |
||
89 | ; Otherwise, if window is small but nonzero, and timer expired, |
||
90 | ; we will send what we can and go to transmit state |
||
91 | |||
6916 | hidnplayr | 92 | test [eax + TCP_SOCKET.t_flags], TF_FORCE |
93 | jz .no_force |
||
3545 | hidnplayr | 94 | |
3556 | hidnplayr | 95 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: forcing data out\n" |
3545 | hidnplayr | 96 | |
97 | test ecx, ecx |
||
98 | jnz .no_zero_window |
||
99 | |||
100 | cmp ebx, [eax + STREAM_SOCKET.snd.size] |
||
101 | jae @f |
||
102 | and dl, not (TH_FIN) |
||
5442 | hidnplayr | 103 | @@: |
3545 | hidnplayr | 104 | |
105 | inc ecx |
||
106 | jmp .no_force |
||
107 | |||
108 | .no_zero_window: |
||
3603 | hidnplayr | 109 | and [eax + TCP_SOCKET.timer_flags], not timer_flag_persist |
3545 | hidnplayr | 110 | mov [eax + TCP_SOCKET.t_rxtshift], 0 |
111 | |||
112 | .no_force: |
||
113 | |||
114 | ;-------------------------------- |
||
6476 | hidnplayr | 115 | ; Calculate how much data to send |
3545 | hidnplayr | 116 | |
117 | mov esi, [eax + STREAM_SOCKET.snd.size] |
||
118 | cmp esi, ecx |
||
119 | jb @f |
||
120 | mov esi, ecx |
||
121 | @@: |
||
122 | sub esi, ebx |
||
123 | |||
124 | ;------------------------ |
||
6476 | hidnplayr | 125 | ; check for window shrink |
3545 | hidnplayr | 126 | |
6476 | hidnplayr | 127 | ; If FIN has been sent, but not ACKed, but we havent been called to retransmit, esi will be -1 |
3545 | hidnplayr | 128 | ; Otherwise, window shrank after we sent into it. |
129 | |||
8026 | hidnplayr | 130 | jge .not_persist |
3545 | hidnplayr | 131 | |
132 | ; enter persist state |
||
6476 | hidnplayr | 133 | |
3545 | hidnplayr | 134 | xor esi, esi |
135 | |||
136 | ; If window shrank to 0 |
||
6476 | hidnplayr | 137 | |
3545 | hidnplayr | 138 | test ecx, ecx |
139 | jnz @f |
||
140 | |||
141 | ; cancel pending retransmit |
||
6476 | hidnplayr | 142 | |
3603 | hidnplayr | 143 | and [eax + TCP_SOCKET.timer_flags], not timer_flag_retransmission |
3545 | hidnplayr | 144 | |
145 | ; pull SND_NXT back to (closed) window, We will enter persist state below. |
||
6476 | hidnplayr | 146 | |
3545 | hidnplayr | 147 | push [eax + TCP_SOCKET.SND_UNA] |
148 | pop [eax + TCP_SOCKET.SND_NXT] |
||
149 | @@: |
||
150 | |||
151 | ; If window didn't close completely, just wait for an ACK |
||
152 | |||
153 | .not_persist: |
||
154 | |||
155 | ;--------------------------- |
||
6476 | hidnplayr | 156 | ; Send one segment at a time |
3545 | hidnplayr | 157 | |
158 | cmp esi, [eax + TCP_SOCKET.t_maxseg] |
||
159 | jbe @f |
||
160 | mov esi, [eax + TCP_SOCKET.t_maxseg] |
||
4347 | hidnplayr | 161 | or [temp_bits], TCP_BIT_SENDALOT |
3545 | hidnplayr | 162 | @@: |
163 | |||
164 | ;-------------------------------------------- |
||
6476 | hidnplayr | 165 | ; Turn of FIN flag if send buffer not emptied |
3545 | hidnplayr | 166 | |
167 | mov edi, [eax + TCP_SOCKET.SND_NXT] |
||
168 | add edi, esi |
||
169 | sub edi, [eax + TCP_SOCKET.SND_UNA] |
||
170 | cmp edi, [eax + STREAM_SOCKET.snd.size] |
||
171 | jae @f |
||
172 | and dl, not (TH_FIN) |
||
173 | @@: |
||
174 | |||
175 | ;------------------------------- |
||
6476 | hidnplayr | 176 | ; calculate window advertisement |
3545 | hidnplayr | 177 | |
8026 | hidnplayr | 178 | xor ecx, ecx |
179 | test [eax + SOCKET.state], SS_CANTRCVMORE |
||
180 | jnz @f |
||
6413 | hidnplayr | 181 | mov ecx, SOCKET_BUFFER_SIZE |
3545 | hidnplayr | 182 | sub ecx, [eax + STREAM_SOCKET.rcv.size] |
8026 | hidnplayr | 183 | @@: |
3545 | hidnplayr | 184 | |
185 | ;------------------------------ |
||
6476 | hidnplayr | 186 | ; Sender silly window avoidance |
3545 | hidnplayr | 187 | |
188 | test esi, esi |
||
189 | jz .len_zero |
||
190 | |||
191 | cmp esi, [eax + TCP_SOCKET.t_maxseg] |
||
4387 | hidnplayr | 192 | je .send |
3545 | hidnplayr | 193 | |
194 | add ebx, esi ; offset + length |
||
195 | cmp ebx, [eax + STREAM_SOCKET.snd.size] |
||
196 | jb @f |
||
197 | |||
198 | test [eax + TCP_SOCKET.t_flags], TF_NODELAY |
||
4387 | hidnplayr | 199 | jnz .send |
3545 | hidnplayr | 200 | |
201 | mov ebx, [eax + TCP_SOCKET.SND_MAX] |
||
202 | cmp ebx, [eax + TCP_SOCKET.SND_UNA] |
||
4387 | hidnplayr | 203 | je .send |
3545 | hidnplayr | 204 | @@: |
205 | |||
6916 | hidnplayr | 206 | test [eax + TCP_SOCKET.t_flags], TF_FORCE |
4387 | hidnplayr | 207 | jnz .send |
3545 | hidnplayr | 208 | |
209 | mov ebx, [eax + TCP_SOCKET.max_sndwnd] |
||
210 | shr ebx, 1 |
||
211 | cmp esi, ebx |
||
4387 | hidnplayr | 212 | jae .send |
3545 | hidnplayr | 213 | |
214 | mov ebx, [eax + TCP_SOCKET.SND_NXT] |
||
215 | cmp ebx, [eax + TCP_SOCKET.SND_MAX] |
||
4387 | hidnplayr | 216 | jb .send |
3545 | hidnplayr | 217 | |
218 | .len_zero: |
||
219 | |||
220 | ;---------------------------------------- |
||
6476 | hidnplayr | 221 | ; Check if a window update should be sent |
3545 | hidnplayr | 222 | |
3556 | hidnplayr | 223 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: window=%d\n", ecx |
3545 | hidnplayr | 224 | |
225 | ; Compare available window to amount of window known to peer (as advertised window less next expected input) |
||
226 | ; If the difference is at least two max size segments, or at least 50% of the maximum possible window, |
||
227 | ; Then we want to send a window update to the peer. |
||
228 | |||
229 | test ecx, ecx |
||
230 | jz .no_window |
||
231 | |||
232 | push ecx |
||
233 | mov cl, [eax + TCP_SOCKET.RCV_SCALE] |
||
234 | mov ebx, TCP_max_win |
||
235 | shl ebx, cl |
||
236 | pop ecx |
||
8024 | hidnplayr | 237 | sub ebx, [eax + TCP_SOCKET.RCV_ADV] |
238 | add ebx, [eax + TCP_SOCKET.RCV_NXT] |
||
3652 | hidnplayr | 239 | |
3545 | hidnplayr | 240 | cmp ebx, ecx |
8026 | hidnplayr | 241 | jl @f |
3545 | hidnplayr | 242 | mov ebx, ecx |
243 | @@: |
||
244 | |||
6476 | hidnplayr | 245 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: we can increase window by %d bytes\n", ebx |
246 | |||
3545 | hidnplayr | 247 | mov edi, [eax + TCP_SOCKET.t_maxseg] |
248 | shl edi, 1 |
||
4387 | hidnplayr | 249 | cmp ebx, edi |
250 | jae .send |
||
3545 | hidnplayr | 251 | |
8026 | hidnplayr | 252 | cmp ebx, SOCKET_BUFFER_SIZE/2 |
253 | jae .send |
||
3545 | hidnplayr | 254 | |
255 | .no_window: |
||
256 | |||
257 | ;-------------------------- |
||
6476 | hidnplayr | 258 | ; Should a segment be sent? |
3545 | hidnplayr | 259 | |
6476 | hidnplayr | 260 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: Should a segment be sent?\n" |
261 | |||
3545 | hidnplayr | 262 | test [eax + TCP_SOCKET.t_flags], TF_ACKNOW ; we need to ACK |
4387 | hidnplayr | 263 | jnz .send |
3545 | hidnplayr | 264 | |
265 | test dl, TH_SYN + TH_RST ; we need to send a SYN or RST |
||
4387 | hidnplayr | 266 | jnz .send |
3545 | hidnplayr | 267 | |
268 | mov ebx, [eax + TCP_SOCKET.SND_UP] ; when urgent pointer is beyond start of send bufer |
||
269 | cmp ebx, [eax + TCP_SOCKET.SND_UNA] |
||
4387 | hidnplayr | 270 | ja .send |
3545 | hidnplayr | 271 | |
6476 | hidnplayr | 272 | ; Do we need to send a FIN according to our state? |
273 | |||
3545 | hidnplayr | 274 | test dl, TH_FIN |
6476 | hidnplayr | 275 | jz .enter_persist ; no reason to send, enter persist state |
3545 | hidnplayr | 276 | |
6476 | hidnplayr | 277 | ; Do so if we didnt do it already |
3545 | hidnplayr | 278 | |
279 | test [eax + TCP_SOCKET.t_flags], TF_SENTFIN |
||
4387 | hidnplayr | 280 | jz .send |
3545 | hidnplayr | 281 | |
6476 | hidnplayr | 282 | ; Or when we need to retransmit the FIN |
283 | |||
3545 | hidnplayr | 284 | mov ebx, [eax + TCP_SOCKET.SND_NXT] |
285 | cmp ebx, [eax + TCP_SOCKET.SND_UNA] |
||
4387 | hidnplayr | 286 | je .send |
3545 | hidnplayr | 287 | |
288 | ;-------------------- |
||
6476 | hidnplayr | 289 | ; Enter persist state |
3545 | hidnplayr | 290 | |
291 | .enter_persist: |
||
292 | |||
3600 | hidnplayr | 293 | cmp [eax + STREAM_SOCKET.snd.size], 0 ; Data ready to send? |
8026 | hidnplayr | 294 | je @f |
295 | test [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission or timer_flag_persist |
||
3600 | hidnplayr | 296 | jnz @f |
297 | |||
3556 | hidnplayr | 298 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: Entering persist state\n" |
3545 | hidnplayr | 299 | |
300 | mov [eax + TCP_SOCKET.t_rxtshift], 0 |
||
6011 | hidnplayr | 301 | call tcp_set_persist |
3545 | hidnplayr | 302 | @@: |
303 | |||
304 | ;---------------------------- |
||
6476 | hidnplayr | 305 | ; No reason to send a segment |
3545 | hidnplayr | 306 | |
3556 | hidnplayr | 307 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: No reason to send a segment\n" |
3545 | hidnplayr | 308 | |
309 | pusha |
||
310 | lea ecx, [eax + SOCKET.mutex] |
||
311 | call mutex_unlock |
||
312 | popa |
||
313 | |||
6914 | hidnplayr | 314 | xor eax, eax |
3545 | hidnplayr | 315 | ret |
316 | |||
317 | |||
318 | ;----------------------------------------------- |
||
319 | ; |
||
6476 | hidnplayr | 320 | ; Send a segment |
3545 | hidnplayr | 321 | ; |
322 | ; eax = socket pointer |
||
323 | ; esi = data len |
||
324 | ; dl = flags |
||
325 | ; |
||
326 | ;----------------------------------------------- |
||
4387 | hidnplayr | 327 | .send: |
3545 | hidnplayr | 328 | |
3556 | hidnplayr | 329 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: socket=%x length=%u flags=%x\n", eax, esi, dl |
3545 | hidnplayr | 330 | |
331 | push eax ; save socket ptr |
||
332 | push esi ; and data length too |
||
333 | mov edi, sizeof.TCP_header ; edi will contain headersize |
||
334 | |||
335 | ;------------------------------------ |
||
336 | ; Send options with first SYN segment |
||
337 | |||
338 | test dl, TH_SYN |
||
339 | jz .options_done |
||
340 | |||
341 | push [eax + TCP_SOCKET.ISS] |
||
342 | pop [eax + TCP_SOCKET.SND_NXT] |
||
343 | |||
344 | test [eax + TCP_SOCKET.t_flags], TF_NOOPT |
||
345 | jnz .options_done |
||
346 | |||
347 | mov ecx, 1460 ;;;; FIXME: use routing blablabla to determine MSS |
||
348 | or ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16 |
||
349 | bswap ecx |
||
350 | push ecx |
||
351 | add di, 4 |
||
352 | |||
3556 | hidnplayr | 353 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added maxseg option\n" |
3545 | hidnplayr | 354 | |
355 | test [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE |
||
356 | jz .no_scale |
||
357 | |||
358 | test dl, TH_ACK |
||
359 | jz .scale_opt |
||
360 | |||
361 | test [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE |
||
362 | jz .no_scale |
||
363 | |||
364 | .scale_opt: |
||
365 | mov cl, [eax + TCP_SOCKET.request_r_scale] |
||
366 | mov ch, TCP_OPT_NOP |
||
367 | pushw cx |
||
368 | pushw TCP_OPT_WINDOW + 3 shl 8 |
||
369 | add di, 4 |
||
370 | |||
3556 | hidnplayr | 371 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added scale option\n" |
3545 | hidnplayr | 372 | |
373 | .no_scale: |
||
374 | .no_syn: |
||
375 | |||
376 | ;------------------------------------ |
||
377 | ; Make the timestamp option if needed |
||
378 | |||
379 | test [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP |
||
380 | jz .no_timestamp |
||
381 | |||
382 | test dl, TH_RST |
||
383 | jnz .no_timestamp |
||
384 | |||
385 | test dl, TH_ACK |
||
386 | jz .timestamp |
||
387 | |||
388 | test [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP |
||
389 | jz .no_timestamp |
||
390 | |||
391 | .timestamp: |
||
392 | pushd 0 |
||
393 | pushd [timer_ticks] |
||
394 | pushd TCP_OPT_NOP + TCP_OPT_NOP shl 8 + TCP_OPT_TIMESTAMP shl 16 + 10 shl 24 |
||
395 | add di, 12 |
||
396 | |||
3556 | hidnplayr | 397 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: added timestamp\n" |
3545 | hidnplayr | 398 | |
399 | .no_timestamp: |
||
400 | |||
401 | ; |
||
402 | |||
403 | .options_done: |
||
404 | |||
405 | ; eax = socket ptr |
||
406 | ; edx = flags |
||
407 | ; edi = header size |
||
408 | ; esi = data len |
||
409 | |||
410 | ;--------------------------------------------- |
||
6476 | hidnplayr | 411 | ; check if we dont exceed the max segment size |
3545 | hidnplayr | 412 | |
413 | add esi, edi ; total TCP segment size |
||
414 | cmp esi, [eax + TCP_SOCKET.t_maxseg] |
||
415 | jbe .no_overflow |
||
416 | |||
417 | mov esi, [eax + TCP_SOCKET.t_maxseg] |
||
4347 | hidnplayr | 418 | or [temp_bits], TCP_BIT_SENDALOT |
3545 | hidnplayr | 419 | .no_overflow: |
420 | |||
5442 | hidnplayr | 421 | ; Update stats |
6476 | hidnplayr | 422 | |
5442 | hidnplayr | 423 | test esi, esi |
424 | jz .zero_data |
||
425 | |||
6916 | hidnplayr | 426 | test [eax + TCP_SOCKET.t_flags], TF_FORCE |
427 | jz @f |
||
5442 | hidnplayr | 428 | cmp esi, 1 |
429 | jne @f |
||
430 | inc [TCPS_sndprobe] |
||
431 | jmp .eos |
||
432 | @@: |
||
433 | |||
434 | mov ebx, [eax + TCP_SOCKET.SND_NXT] |
||
435 | cmp ebx, [eax + TCP_SOCKET.SND_MAX] |
||
436 | jae @f |
||
437 | inc [TCPS_sndrexmitpack] |
||
438 | add [TCPS_sndrexmitbyte], esi |
||
439 | jmp .eos |
||
440 | @@: |
||
441 | inc [TCPS_sndpack] |
||
442 | add [TCPS_sndbyte], esi |
||
443 | jmp .eos |
||
444 | |||
445 | .zero_data: |
||
446 | test [eax + TCP_SOCKET.t_flags], TF_ACKNOW |
||
447 | jz @f |
||
448 | inc [TCPS_sndacks] |
||
449 | jmp .eos |
||
450 | @@: |
||
451 | test dl, TH_SYN + TH_FIN + TH_RST |
||
452 | jz @f |
||
453 | inc [TCPS_sndctrl] |
||
454 | jmp .eos |
||
455 | @@: |
||
456 | mov ebx, [eax + TCP_SOCKET.SND_UP] |
||
457 | cmp ebx, [eax + TCP_SOCKET.SND_UNA] |
||
458 | jb @f |
||
459 | inc [TCPS_sndurg] |
||
460 | jmp .eos |
||
461 | @@: |
||
462 | inc [TCPS_sndwinup] |
||
463 | |||
464 | .eos: |
||
465 | |||
6914 | hidnplayr | 466 | ;--------------------------------------------------- |
467 | ; Dont increase sequence number when resending a FIN |
||
468 | |||
469 | test dl, TH_FIN |
||
470 | jz .no_fin_retransmit |
||
471 | |||
472 | test [eax + TCP_SOCKET.t_flags], TF_SENTFIN |
||
473 | jz .no_fin_retransmit |
||
474 | |||
475 | mov ebx, [eax + TCP_SOCKET.SND_NXT] |
||
476 | cmp ebx, [eax + TCP_SOCKET.SND_MAX] |
||
477 | jne .no_fin_retransmit |
||
478 | |||
479 | dec [eax + TCP_SOCKET.SND_NXT] |
||
480 | |||
481 | .no_fin_retransmit: |
||
482 | |||
4347 | hidnplayr | 483 | ;---------------------------------------------------- |
484 | ; Calculate the receive window. |
||
485 | ; Dont shrink window, but avoid silly window syndrome |
||
486 | |||
8026 | hidnplayr | 487 | xor ebx, ebx |
488 | test [eax + SOCKET.state], SS_CANTRCVMORE |
||
489 | jnz @f |
||
6413 | hidnplayr | 490 | mov ebx, SOCKET_BUFFER_SIZE |
4347 | hidnplayr | 491 | sub ebx, [eax + STREAM_SOCKET.rcv.size] |
492 | |||
6413 | hidnplayr | 493 | cmp ebx, SOCKET_BUFFER_SIZE/4 |
8026 | hidnplayr | 494 | jge @f |
4347 | hidnplayr | 495 | cmp ebx, [eax + TCP_SOCKET.t_maxseg] |
8026 | hidnplayr | 496 | jge @f |
4347 | hidnplayr | 497 | xor ebx, ebx |
498 | @@: |
||
499 | |||
8026 | hidnplayr | 500 | |
501 | mov cl, [eax + TCP_SOCKET.RCV_SCALE] |
||
502 | push eax |
||
503 | mov eax, TCP_max_win |
||
504 | shl eax, cl |
||
505 | cmp ebx, eax |
||
506 | jle @f |
||
507 | mov ebx, eax |
||
4347 | hidnplayr | 508 | @@: |
8026 | hidnplayr | 509 | pop eax |
4347 | hidnplayr | 510 | |
8026 | hidnplayr | 511 | |
4347 | hidnplayr | 512 | mov ecx, [eax + TCP_SOCKET.RCV_ADV] |
513 | sub ecx, [eax + TCP_SOCKET.RCV_NXT] |
||
514 | cmp ebx, ecx |
||
6909 | hidnplayr | 515 | jg @f |
4347 | hidnplayr | 516 | mov ebx, ecx |
517 | @@: |
||
518 | |||
8026 | hidnplayr | 519 | ;; TODO URGENT POINTER |
520 | |||
6474 | hidnplayr | 521 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: window=%u\n", ebx |
8026 | hidnplayr | 522 | mov [rcv_window], ebx |
4347 | hidnplayr | 523 | |
524 | mov cl, [eax + TCP_SOCKET.RCV_SCALE] |
||
525 | shr ebx, cl |
||
526 | xchg bl, bh |
||
527 | |||
3545 | hidnplayr | 528 | ;----------------------------------------------------------------- |
529 | ; Start by pushing all TCP header values in reverse order on stack |
||
530 | ; (essentially, creating the tcp header on the stack!) |
||
531 | |||
6476 | hidnplayr | 532 | pushw 0 ; UrgentPointer |
533 | pushw 0 ; Checksum |
||
534 | pushw bx ; Window |
||
535 | shl edi, 2 ; DataOffset |
||
3545 | hidnplayr | 536 | shl dx, 8 |
6476 | hidnplayr | 537 | or dx, di ; Flags |
3545 | hidnplayr | 538 | pushw dx |
6476 | hidnplayr | 539 | shr edi, 2 ; DataOffset |
3545 | hidnplayr | 540 | |
6476 | hidnplayr | 541 | push [eax + TCP_SOCKET.RCV_NXT] ; AckNumber |
3545 | hidnplayr | 542 | ntohd [esp] |
543 | |||
6476 | hidnplayr | 544 | push [eax + TCP_SOCKET.SND_NXT] ; SequenceNumber |
3545 | hidnplayr | 545 | ntohd [esp] |
546 | |||
6476 | hidnplayr | 547 | push [eax + TCP_SOCKET.RemotePort] ; DestinationPort |
548 | push [eax + TCP_SOCKET.LocalPort] ; SourcePort |
||
3545 | hidnplayr | 549 | |
6476 | hidnplayr | 550 | push edi ; header size |
3545 | hidnplayr | 551 | |
552 | ;--------------------- |
||
553 | ; Create the IP packet |
||
554 | |||
555 | mov ecx, esi |
||
5842 | hidnplayr | 556 | mov ebx, [eax + IP_SOCKET.device] |
3545 | hidnplayr | 557 | mov edx, [eax + IP_SOCKET.LocalIP] ; source ip |
5842 | hidnplayr | 558 | mov edi, [eax + IP_SOCKET.RemoteIP] ; dest ip |
559 | mov al, [eax + IP_SOCKET.ttl] |
||
560 | mov ah, IP_PROTO_TCP |
||
6011 | hidnplayr | 561 | call ipv4_output |
3545 | hidnplayr | 562 | jz .ip_error |
563 | |||
5442 | hidnplayr | 564 | ;------------------------------------------ |
565 | ; Move TCP header from stack to TCP segment |
||
3545 | hidnplayr | 566 | |
567 | push ecx |
||
568 | mov ecx, [esp + 4] |
||
569 | lea esi, [esp + 8] |
||
6474 | hidnplayr | 570 | shr ecx, 2 ; count is in bytes, we will work with dwords |
3711 | clevermous | 571 | rep movsd |
6474 | hidnplayr | 572 | pop ecx ; full TCP packet size |
3545 | hidnplayr | 573 | |
6474 | hidnplayr | 574 | pop esi ; headersize |
575 | add esp, esi ; remove it from stack |
||
3545 | hidnplayr | 576 | |
6474 | hidnplayr | 577 | push eax ; packet ptr for send proc |
3545 | hidnplayr | 578 | |
6474 | hidnplayr | 579 | mov edx, edi ; begin of data |
580 | sub edx, esi ; begin of packet (edi = begin of data) |
||
3545 | hidnplayr | 581 | push ecx |
6474 | hidnplayr | 582 | sub ecx, esi ; data size |
3545 | hidnplayr | 583 | |
584 | ;-------------- |
||
585 | ; Copy the data |
||
586 | |||
587 | ; eax = ptr to ring struct |
||
588 | ; ecx = buffer size |
||
589 | ; edi = ptr to buffer |
||
590 | |||
5522 | hidnplayr | 591 | mov eax, [esp + 12] ; get socket ptr |
3545 | hidnplayr | 592 | |
593 | push edx |
||
594 | push [eax + TCP_SOCKET.SND_NXT] ; we'll need this for timing the transmission |
||
595 | test ecx, ecx |
||
596 | jz .nodata |
||
597 | mov edx, [eax + TCP_SOCKET.SND_NXT] |
||
5442 | hidnplayr | 598 | add [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number |
3545 | hidnplayr | 599 | sub edx, [eax + TCP_SOCKET.SND_UNA] ; offset |
600 | add eax, STREAM_SOCKET.snd |
||
6011 | hidnplayr | 601 | call socket_ring_read |
3545 | hidnplayr | 602 | .nodata: |
603 | pop edi |
||
604 | pop esi ; begin of data |
||
605 | pop ecx ; full packet size |
||
5522 | hidnplayr | 606 | mov eax, [esp + 8] ; socket ptr |
3545 | hidnplayr | 607 | |
6476 | hidnplayr | 608 | ;---------------------------- |
609 | ; initialize retransmit timer |
||
3545 | hidnplayr | 610 | |
611 | ;TODO: check t_force and persist |
||
612 | |||
613 | test [esi + TCP_header.Flags], TH_SYN + TH_FIN ; syn and fin take a sequence number |
||
614 | jz @f |
||
615 | inc [eax + TCP_SOCKET.SND_NXT] |
||
616 | test [esi + TCP_header.Flags], TH_FIN |
||
617 | jz @f |
||
618 | or [eax + TCP_SOCKET.t_flags], TF_SENTFIN ; if we sent a fin, set the sentfin flag |
||
619 | @@: |
||
620 | |||
621 | mov edx, [eax + TCP_SOCKET.SND_NXT] |
||
622 | cmp edx, [eax + TCP_SOCKET.SND_MAX] ; is this a retransmission? |
||
623 | jbe @f |
||
624 | mov [eax + TCP_SOCKET.SND_MAX], edx ; [eax + TCP_SOCKET.SND_NXT] from before we updated it |
||
625 | |||
626 | cmp [eax + TCP_SOCKET.t_rtt], 0 ; are we currently timing anything? |
||
627 | je @f |
||
628 | mov [eax + TCP_SOCKET.t_rtt], 1 ; nope, start transmission timer |
||
629 | mov [eax + TCP_SOCKET.t_rtseq], edi |
||
5442 | hidnplayr | 630 | inc [TCPS_segstimed] |
3545 | hidnplayr | 631 | @@: |
632 | |||
633 | ; set retransmission timer if not already set, and not doing an ACK or keepalive probe |
||
6476 | hidnplayr | 634 | |
3600 | hidnplayr | 635 | test [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission |
636 | jnz .retransmit_set |
||
3545 | hidnplayr | 637 | |
638 | cmp edx, [eax + TCP_SOCKET.SND_UNA] ; edx is still [eax + TCP_SOCKET.SND_NXT] |
||
639 | je .retransmit_set |
||
640 | |||
641 | mov edx, [eax + TCP_SOCKET.t_rxtcur] |
||
642 | mov [eax + TCP_SOCKET.timer_retransmission], edx |
||
3603 | hidnplayr | 643 | or [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission |
3545 | hidnplayr | 644 | |
3600 | hidnplayr | 645 | test [eax + TCP_SOCKET.timer_flags], timer_flag_persist |
646 | jz .retransmit_set |
||
3603 | hidnplayr | 647 | and [eax + TCP_SOCKET.timer_flags], not timer_flag_persist |
3545 | hidnplayr | 648 | mov [eax + TCP_SOCKET.t_rxtshift], 0 |
649 | .retransmit_set: |
||
650 | |||
651 | ;-------------------- |
||
652 | ; Create the checksum |
||
653 | |||
4387 | hidnplayr | 654 | xor dx, dx |
655 | test [ebx + NET_DEVICE.hwacc], NET_HWACC_TCP_IPv4_OUT |
||
656 | jnz .checksum_ok |
||
657 | |||
6011 | hidnplayr | 658 | tcp_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP) |
4387 | hidnplayr | 659 | |
660 | .checksum_ok: |
||
3545 | hidnplayr | 661 | mov [esi + TCP_header.Checksum], dx |
662 | |||
663 | ;---------------- |
||
664 | ; Send the packet |
||
665 | |||
3556 | hidnplayr | 666 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: Sending with device %x\n", ebx |
3545 | hidnplayr | 667 | call [ebx + NET_DEVICE.transmit] |
668 | jnz .send_error |
||
669 | |||
670 | ;--------------- |
||
671 | ; Ok, data sent! |
||
672 | |||
673 | pop ecx |
||
674 | pop eax |
||
675 | |||
6011 | hidnplayr | 676 | call net_ptr_to_num4 |
3644 | hidnplayr | 677 | inc [TCP_segments_tx + edi] |
5442 | hidnplayr | 678 | inc [TCPS_sndtotal] |
3545 | hidnplayr | 679 | |
680 | ; update advertised receive window |
||
6474 | hidnplayr | 681 | |
8026 | hidnplayr | 682 | mov ecx, [rcv_window] |
3545 | hidnplayr | 683 | test ecx, ecx |
684 | jz @f |
||
685 | add ecx, [eax + TCP_SOCKET.RCV_NXT] |
||
686 | cmp ecx, [eax + TCP_SOCKET.RCV_ADV] |
||
687 | jbe @f |
||
688 | mov [eax + TCP_SOCKET.RCV_ADV], ecx |
||
689 | @@: |
||
690 | |||
691 | ; update last ack sent |
||
6476 | hidnplayr | 692 | |
3545 | hidnplayr | 693 | push [eax + TCP_SOCKET.RCV_NXT] |
694 | pop [eax + TCP_SOCKET.last_ack_sent] |
||
695 | |||
4347 | hidnplayr | 696 | ; clear the ACK flags |
6476 | hidnplayr | 697 | |
3545 | hidnplayr | 698 | and [eax + TCP_SOCKET.t_flags], not (TF_ACKNOW + TF_DELACK) |
699 | |||
700 | ;-------------- |
||
701 | ; unlock socket |
||
702 | |||
3652 | hidnplayr | 703 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: unlocking socket 0x%x\n", eax |
704 | |||
3545 | hidnplayr | 705 | push eax |
706 | lea ecx, [eax + SOCKET.mutex] |
||
707 | call mutex_unlock |
||
708 | pop eax |
||
709 | |||
710 | ;----------------------------- |
||
711 | ; Check if we need more output |
||
712 | |||
4347 | hidnplayr | 713 | test [temp_bits], TCP_BIT_SENDALOT |
6011 | hidnplayr | 714 | jnz tcp_output.again |
3545 | hidnplayr | 715 | |
3556 | hidnplayr | 716 | DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_send: success!\n" |
3545 | hidnplayr | 717 | |
718 | xor eax, eax |
||
719 | ret |
||
720 | |||
721 | |||
722 | .ip_error: |
||
723 | pop ecx |
||
724 | add esp, ecx |
||
725 | add esp, 4 |
||
726 | pop eax |
||
727 | |||
728 | mov [eax + TCP_SOCKET.timer_retransmission], TCP_time_re_min |
||
3602 | hidnplayr | 729 | or [eax + TCP_SOCKET.timer_flags], timer_flag_retransmission |
3545 | hidnplayr | 730 | |
731 | lea ecx, [eax + SOCKET.mutex] |
||
732 | call mutex_unlock |
||
733 | |||
3602 | hidnplayr | 734 | DEBUGF DEBUG_NETWORK_ERROR, "TCP_send: IP error\n" |
3545 | hidnplayr | 735 | |
736 | or eax, -1 |
||
737 | ret |
||
738 | |||
739 | |||
740 | .send_error: |
||
3652 | hidnplayr | 741 | add esp, 4 |
742 | pop eax |
||
3545 | hidnplayr | 743 | |
744 | lea ecx, [eax + SOCKET.mutex] |
||
745 | call mutex_unlock |
||
746 | |||
3602 | hidnplayr | 747 | DEBUGF DEBUG_NETWORK_ERROR, "TCP_send: sending failed\n" |
3545 | hidnplayr | 748 | |
749 | or eax, -2 |
||
750 | ret |
||
751 | |||
752 | |||
10051 | ace_dent | 753 | endp |