Subversion Repositories Kolibri OS

Rev

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