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