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