Subversion Repositories Kolibri OS

Rev

Rev 3143 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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