Subversion Repositories Kolibri OS

Rev

Rev 6476 | Rev 6914 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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