Subversion Repositories Kolibri OS

Rev

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