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