Subversion Repositories Kolibri OS

Rev

Rev 871 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 871 Rev 914
Line 13... Line 13...
13
;;                                                              ;;
13
;;                                                              ;;
14
;;  See file COPYING for details                                ;;
14
;;  See file COPYING for details                                ;;
15
;;  v0.6 : Added reset handling in the established state        ;;
15
;;  v0.6 : Added reset handling in the established state        ;;
16
;;         Added a timer per socket to allow delays when        ;;
16
;;         Added a timer per socket to allow delays when        ;;
17
;;         rx window gets below 1KB                             ;;
17
;;         rx window gets below 1KB                             ;;
-
 
18
;;                                                              ;;
18
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 19... Line 20...
19
 
20
 
Line 20... Line 21...
20
$Revision: 871 $
21
$Revision: 914 $
21
 
22
 
22
 
23
 
Line 28... Line 29...
28
TCB_FIN_WAIT_1     equ        5
29
TCB_FIN_WAIT_1     equ        5
29
TCB_FIN_WAIT_2     equ        6
30
TCB_FIN_WAIT_2     equ        6
30
TCB_CLOSE_WAIT     equ        7
31
TCB_CLOSE_WAIT     equ        7
31
TCB_CLOSING        equ        8
32
TCB_CLOSING        equ        8
32
TCB_LAST_ACK       equ        9
33
TCB_LAST_ACK       equ        9
33
TCB_TIME_WAIT      equ        10
34
TCB_TIMED_WAIT	   equ	      10
34
TCB_CLOSED         equ        11
35
TCB_CLOSED         equ        11
Line -... Line 36...
-
 
36
 
-
 
37
TH_FIN	= 0x01
-
 
38
TH_SYN	= 0x02
-
 
39
TH_RST	= 0x04
-
 
40
TH_PUSH = 0x08
-
 
41
TH_ACK	= 0x10
-
 
42
TH_URG	= 0x20
35
 
43
 
Line 36... Line 44...
36
TWOMSL              equ     10      ; # of secs to wait before closing socket
44
TWOMSL              equ     10      ; # of secs to wait before closing socket
37
 
45
 
Line 100... Line 108...
100
;   Description
108
;   Description
101
;       Handles sockets in the timewait state, closing them
109
;       Handles sockets in the timewait state, closing them
102
;       when the TCB timer expires
110
;       when the TCB timer expires
103
;
111
;
104
;***************************************************************************
112
;***************************************************************************
105
tcp_tcb_handler:
-
 
106
    ; scan through all the sockets, decrementing active timers
-
 
Line 107... Line 113...
107
 
113
 
108
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
114
proc tcp_tcb_handler stdcall uses ebx
Line 109... Line -...
109
    mov     ecx, NUM_SOCKETS
-
 
110
 
-
 
111
tth1:
-
 
112
    sub     eax, SOCKETBUFFSIZE
-
 
113
    cmp     [eax + sockets + 32], dword 0
-
 
114
    jne     tth2
-
 
115
 
-
 
116
tth1a:
115
    ; scan through all the sockets, decrementing active timers
Line -... Line 116...
-
 
116
 
-
 
117
	mov	ebx, net_sockets
-
 
118
 
-
 
119
	cmp	[ebx + SOCKET.NextPtr], 0
117
    cmp     [eax + sockets + 72], dword 0
120
	je	.exit
-
 
121
	DEBUGF	1, "K : sockets:\n"
-
 
122
 
118
    jne     tth4
123
  .next_socket:
-
 
124
	mov	ebx, [ebx + SOCKET.NextPtr]
-
 
125
	or	ebx, ebx
-
 
126
	jz	.exit
-
 
127
 
-
 
128
	DEBUGF	1, "K :   %x: %x-%x-%x-%u\n", ebx, [ebx + SOCKET.LocalPort]:4, [ebx + SOCKET.RemoteIP], [ebx + SOCKET.RemotePort]:4, [ebx + SOCKET.TCBState]
-
 
129
 
-
 
130
	cmp	[ebx + SOCKET.TCBTimer], 0
-
 
131
	jne	.decrement_tcb
Line 119... Line 132...
119
 
132
	cmp	[ebx + SOCKET.wndsizeTimer], 0
120
    loop    tth1
133
	jne	.decrement_wnd
121
    ret
-
 
122
 
134
	jmp	.next_socket
123
tth2:
-
 
124
    ; decrement it, delete socket if TCB timer = 0 & socket in timewait state
-
 
125
    pusha
-
 
126
    dec     dword [eax + sockets + 32]
-
 
127
    cmp     [eax + sockets + 32], dword 0
-
 
128
    jne     tth3
-
 
129
 
135
 
130
    cmp     [eax + sockets + 28], dword TCB_TIME_WAIT
-
 
131
    jne     tth3
-
 
132
 
-
 
133
    ; OK, delete socket
-
 
134
    mov     edi, eax
-
 
135
    add     edi, sockets
-
 
136
 
-
 
137
    xor     eax, eax
-
 
138
    mov     ecx, SOCKETHEADERSIZE
-
 
139
    cld
-
 
Line -... Line 136...
-
 
136
  .decrement_tcb:
140
    rep     stosb
137
    ; decrement it, delete socket if TCB timer = 0 & socket in timewait state
Line 141... Line 138...
141
 
138
	dec	[ebx + SOCKET.TCBTimer]
-
 
139
	jnz	.next_socket
142
tth3:
140
 
-
 
141
	cmp	[ebx + SOCKET.TCBState], TCB_TIMED_WAIT
Line -... Line 142...
-
 
142
	jne	.next_socket
143
    popa
143
 
144
 
-
 
145
    jmp     tth1a
144
	push	[ebx + SOCKET.PrevPtr]
146
 
145
	stdcall net_socket_free, ebx
147
    loop    tth1
-
 
Line 148... Line -...
148
    ret
-
 
149
 
-
 
150
    ; TODO - prove it works!
-
 
151
tth4:
146
	pop	ebx
152
    dec     dword [eax + sockets + 72]
147
	jmp	.next_socket
-
 
148
 
Line 153... Line 149...
153
    loop    tth1
149
  .decrement_wnd:
154
    ret
150
    ; TODO - prove it works!
155
 
151
	dec	[ebx + SOCKET.wndsizeTimer]
Line 167... Line 163...
167
;   Description
163
;   Description
168
;       Handles queued TCP data
164
;       Handles queued TCP data
169
;       This is a kernel function, called by stack_handler
165
;       This is a kernel function, called by stack_handler
170
;
166
;
171
;***************************************************************************
167
;***************************************************************************
-
 
168
 
172
tcp_tx_handler:
169
proc tcp_tx_handler stdcall
173
    ; decrement all resend buffers timers. If they
170
    ; decrement all resend buffers timers. If they
174
    ; expire, queue them for sending, and restart the timer.
171
    ; expire, queue them for sending, and restart the timer.
175
    ; If the retries counter reach 0, delete the entry
172
    ; If the retries counter reach 0, delete the entry
Line 176... Line 173...
176
 
173
 
177
    mov     esi, resendQ
174
    mov     esi, resendQ
Line 178... Line 175...
178
    mov     ecx, 0
175
    mov     ecx, 0
179
 
176
 
180
tth001:
177
  .next_resendq:
-
 
178
    cmp     ecx, NUMRESENDENTRIES
181
    cmp     ecx, NUMRESENDENTRIES
179
	je	.exit		    ; None left
182
    je      tth003              ; None left
180
	;cmp     [esi], byte 0xFF                                    ; XTODO: 0xff -> 0
183
    cmp     [esi], byte 0xFF
181
	cmp	dword[esi + 4], 0
184
    jne     tth002              ; found one
182
	jne	@f		     ; found one
185
    inc     ecx
183
    inc     ecx
Line 186... Line -...
186
    add     esi, 4
-
 
187
    jmp     tth001
184
	add	esi, 8
188
 
185
	jmp	.next_resendq
189
tth002:
-
 
190
    ; we have one. decrement it's timer by 1
-
 
191
    dec     word [esi+2]
186
 
192
    mov     ax, [esi+2]
187
    @@: ; we have one. decrement it's timer by 1
193
    cmp     ax, 0
188
	dec	word[esi + 2]
194
    je      tth002a
189
	jz	@f
Line 195... Line 190...
195
    inc     ecx
190
    inc     ecx
-
 
191
	add	esi, 8
196
    add     esi, 4
192
	jmp	.next_resendq	    ; Timer not zero, so move on
197
    jmp     tth001              ; Timer not zero, so move on
193
 
198
 
194
    @@:
199
tth002a:
195
	;mov     bl, 0xff                                             ; XTODO: bl -> ebx, 0xff -> 0
200
    mov     bl, 0xff
196
	xor	ebx, ebx
201
    ; restart timer, and decrement retries
-
 
202
    ; After the first resend, back of on next, by a factor of 5
-
 
203
    mov     [esi+2], word TCP_TIMEOUT * 5
197
    ; restart timer, and decrement retries
Line 204... Line 198...
204
    dec     byte [esi+1]
198
    ; After the first resend, back of on next, by a factor of 5
-
 
199
	mov	[esi + 2], word TCP_TIMEOUT * 5
205
    mov     al, [esi+1]
200
	dec	byte[esi + 1]
206
    cmp     al, 0
-
 
Line 207... Line 201...
207
    jne     tth004
201
	jnz	@f
208
 
202
 
Line 209... Line 203...
209
    ; retries now 0, so delete from queue
203
    ; retries now 0, so delete from queue
210
    xchg     [esi], bl
204
	;xchg     [esi], bl                                          ; XTODO: bl -> ebx
211
tth004:
205
	xchg	 [esi + 4], ebx
212
 
206
 
Line 213... Line 207...
213
    ; resend packet
207
    @@: ; resend packet
-
 
208
	pushad
214
    pusha
209
 
215
 
210
    mov     eax, EMPTY_QUEUE
-
 
211
    call    dequeue
216
    mov     eax, EMPTY_QUEUE
212
    cmp     ax, NO_BUFFER
217
    call    dequeue
213
	jne	.tth004z
218
    cmp     ax, NO_BUFFER
-
 
219
    jne      tth004z
214
 
220
 
215
    ; TODO - try again in 10ms.
221
    ; TODO - try again in 10ms.
216
	;cmp     bl, 0xff                                            ; XTODO: 0xff -> 0
222
    cmp     bl, 0xff
217
	test	ebx, ebx
Line 223... Line 218...
223
    jne     tth004za
218
	jnz	@f
224
    mov     [esi], bl
219
	;mov     [esi], bl                                            ; XTODO: bl -> ebx
Line 225... Line -...
225
 
-
 
226
tth004za:
220
	mov	[esi + 4], ebx
227
    ; Mark it to expire in 10ms - 1 tick
221
 
228
    mov     [esi+1], byte 1
222
    @@: ; Mark it to expire in 10ms - 1 tick
229
    mov     [esi+2], word 1
223
	mov	byte[esi + 1], 1
Line 230... Line 224...
230
    jmp     tth005
224
	mov	word[esi + 2], 1
231
 
225
	jmp	.tth005
232
tth004z:
226
 
233
    ; we have a buffer # in ax
-
 
234
 
-
 
235
    push    eax
227
  .tth004z:
236
    push    ecx
228
    ; we have a buffer # in ax
237
    mov     ecx, IPBUFFSIZE
-
 
238
    mul     ecx
229
 
239
    add     eax, IPbuffs
230
	push	eax ecx
Line 240... Line 231...
240
 
231
    mov     ecx, IPBUFFSIZE
241
    ; we have the buffer address in eax
232
    mul     ecx
Line 242... Line 233...
242
    mov     edi, eax
233
    add     eax, IPbuffs
-
 
234
 
243
    pop     ecx
235
    ; we have the buffer address in eax
244
    ; get resend data address
236
    mov     edi, eax
-
 
237
    pop     ecx
Line 245... Line 238...
245
    inc     ecx
238
    ; Now get buffer location, and copy buffer across. argh! more copying,,
Line 246... Line 239...
246
    ; Now get buffer location, and copy buffer across. argh! more copying,,
239
	mov	esi, resendBuffer
Line 247... Line 240...
247
    mov     esi, resendBuffer - IPBUFFSIZE
240
    @@: add	esi, IPBUFFSIZE
248
tth004a:
241
	loop	@b
249
    add     esi, IPBUFFSIZE
-
 
250
    loop    tth004a
242
 
251
 
243
    ; we have resend buffer location in esi
Line 252... Line 244...
252
    ; we have resend buffer location in esi
244
    mov     ecx, IPBUFFSIZE
253
    mov     ecx, IPBUFFSIZE
245
 
Line 254... Line 246...
254
 
246
    ; copy data across
Line 255... Line -...
255
    ; copy data across
-
 
256
    cld
247
	push	edi
257
    rep     movsb
248
    cld
Line 258... Line 249...
258
 
249
    rep     movsb
259
    ; queue packet
250
	pop	edi
260
 
251
 
Line 261... Line 252...
261
 
252
    ; queue packet
262
 
253
 
263
    mov     eax, NET1OUT_QUEUE
-
 
264
 
254
 
Line 265... Line 255...
265
    mov     edx, [stack_ip]
255
 
266
    mov     ecx, [ edi + 16 ]
256
    mov     eax, NET1OUT_QUEUE
267
    cmp     edx, ecx
257
 
Line 297... Line 287...
297
;       IP buffer address given in edx
287
;       IP buffer address given in edx
298
;          IP buffer number in eax
288
;          IP buffer number in eax
299
;          Free up (or re-use) IP buffer when finished
289
;          Free up (or re-use) IP buffer when finished
300
;
290
;
301
;***************************************************************************
291
;***************************************************************************
-
 
292
 
302
tcp_rx:
293
proc tcp_rx stdcall uses ebx
303
    ; The process is as follows.
294
    ; The process is as follows.
304
    ; Look for a socket with matching remote IP, remote port, local port
295
    ; Look for a socket with matching remote IP, remote port, local port
305
    ; if not found, then
296
    ; if not found, then
306
    ; look for remote IP + local port match ( where sockets remote port = 0)
297
    ; look for remote IP + local port match ( where sockets remote port = 0)
307
    ; if not found, then
298
    ; if not found, then
Line 316... Line 307...
316
    ; Look for a socket where
307
    ; Look for a socket where
317
    ; IP Packet TCP Destination Port = local Port
308
    ; IP Packet TCP Destination Port = local Port
318
    ; IP Packet SA = Remote IP
309
    ; IP Packet SA = Remote IP
319
    ; IP Packet TCP Source Port = remote Port
310
    ; IP Packet TCP Source Port = remote Port
Line -... Line 311...
-
 
311
 
-
 
312
	mov	ebx, net_sockets
-
 
313
 
320
 
314
  .next_socket.1:
-
 
315
	mov	ebx, [ebx + SOCKET.NextPtr]
321
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
316
	or	ebx, ebx
322
    mov     ecx, NUM_SOCKETS
317
	jz	.next_socket.1.exit
323
ss1:
318
 
-
 
319
	cmp	[ebx + SOCKET.Status], SOCK_OPEN
-
 
320
	jne	.next_socket.1
-
 
321
 
-
 
322
;        DEBUGF  1, "K : tcp_rx - 1.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
324
    sub     eax, SOCKETBUFFSIZE
323
 
325
    movzx   ebx, word [edx + 22]     ; get the dest. port from the TCP hdr
324
	mov	ax, [edx + 20 + TCP_PACKET.DestinationPort]  ; get the dest. port from the TCP hdr
326
    cmp     [eax + sockets + 12], bx ; compare with socket's local port
325
	cmp	[ebx + SOCKET.LocalPort], ax		; get the dest. port from the TCP hdr
-
 
326
	jne	.next_socket.1				; different - try next socket
-
 
327
 
327
    jnz     nxttst1                        ; different - try next socket
328
;        DEBUGF  1, "K : tcp_rx - 1.addr: %x - %x\n", [edx + IP_PACKET.SourceAddress], [ebx + SOCKET.RemoteIP]
328
 
329
 
329
    movzx   ebx, word [edx + 20]       ; get the source port from the TCP hdr
330
	mov	eax, [edx + IP_PACKET.SourceAddress]	; get the source IP Addr from the IP hdr
330
    cmp     [eax + sockets + 20], bx ; compare with socket's remote port
331
	cmp	[ebx + SOCKET.RemoteIP], eax		; compare with socket's remote IP
331
    jnz     nxttst1                        ; different - try next socket
332
	jne	.next_socket.1				; different - try next socket
-
 
333
 
332
 
334
;        DEBUGF  1, "K : tcp_rx - 1.sport: %x - %x\n", [edx + 20 + TCP_PACKET.SourcePort]:4, [ebx + SOCKET.RemotePort]:4
333
 
335
 
334
    mov     ebx, [edx + 12]           ; get the source IP Addr from the IP hdr
336
	mov	ax, [edx + 20 + TCP_PACKET.SourcePort]	; get the source port from the TCP hdr
335
    cmp     [eax + sockets + 16], ebx ; compare with socket's remote IP
337
	cmp	[ebx + SOCKET.RemotePort], ax		; compare with socket's remote port
Line 336... Line 338...
336
    jnz     nxttst1                        ; different - try next socket
338
	jne	.next_socket.1				; different - try next socket
337
 
339
 
Line 338... Line 340...
338
    ; We have a complete match - use this socket
340
    ; We have a complete match - use this socket
339
    jmp     tcprx_001
-
 
Line 340... Line 341...
340
 
341
	jmp	.change_state
341
nxttst1:
342
 
342
    loop    ss1                     ; Return back if no match
343
  .next_socket.1.exit:
343
 
344
 
344
    ; If we got here, there was no match
345
    ; If we got here, there was no match
Line -... Line 346...
-
 
346
    ; Look for a socket where
-
 
347
    ; IP Packet TCP Destination Port = local Port
-
 
348
    ; IP Packet SA = Remote IP
345
    ; Look for a socket where
349
    ; socket remote Port = 0
-
 
350
 
-
 
351
	mov	ebx, net_sockets
-
 
352
 
346
    ; IP Packet TCP Destination Port = local Port
353
  .next_socket.2:
-
 
354
	mov	ebx, [ebx + SOCKET.NextPtr]
-
 
355
	or	ebx, ebx
-
 
356
	jz	.next_socket.2.exit
Line 347... Line -...
347
    ; IP Packet SA = Remote IP
-
 
-
 
357
 
-
 
358
	cmp	[ebx + SOCKET.Status], SOCK_OPEN
348
    ; socket remote Port = 0
359
	jne	.next_socket.2
Line 349... Line -...
349
 
-
 
350
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
-
 
351
    mov     ecx, NUM_SOCKETS
360
 
352
 
361
;        DEBUGF  1, "K : tcp_rx - 2.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
353
ss2:
362
 
354
    sub     eax, SOCKETBUFFSIZE
363
	mov	ax, [edx + 20 + TCP_PACKET.DestinationPort]  ; get the dest. port from the TCP hdr
355
 
364
	cmp	[ebx + SOCKET.LocalPort], ax		; compare with socket's local port
356
    movzx   ebx, word [edx + 22]     ; get the dest. port from the TCP hdr
365
	jne	.next_socket.2				; different - try next socket
357
    cmp     [eax + sockets + 12], bx ; compare with socket's local port
366
 
-
 
367
;        DEBUGF  1, "K : tcp_rx - 2.addr: %x - %x\n", [edx + IP_PACKET.SourceAddress], [ebx + SOCKET.RemoteIP]
358
    jnz     nxttst2                        ; different - try next socket
368
 
359
 
369
	mov	eax, [edx + IP_PACKET.SourceAddress]	; get the source IP Addr from the IP hdr
Line 360... Line 370...
360
    mov     ebx, [edx + 12]          ; get the source IP Addr from the IP hdr
370
	cmp	[ebx + SOCKET.RemoteIP], eax		; compare with socket's remote IP
361
    cmp     [eax + sockets + 16], ebx ; compare with socket's remote IP
371
	jne	.next_socket.2				; different - try next socket
Line 362... Line 372...
362
    jnz     nxttst2                        ; different - try next socket
372
 
363
 
-
 
Line 364... Line 373...
364
    mov     ebx, 0
373
;        DEBUGF  1, "K : tcp_rx - 2.sport: 0000 - %x\n", [ebx + SOCKET.RemotePort]:4
365
    cmp     [eax + sockets + 20], bx ; only match a remote socket of 0
374
 
366
    jnz     nxttst2                        ; different - try next socket
375
	cmp	[ebx + SOCKET.RemotePort], 0		; only match a remote socket of 0
367
 
376
	jne	.next_socket.2				; different - try next socket
368
    ; We have a complete match - use this socket
377
 
Line -... Line 378...
-
 
378
    ; We have a complete match - use this socket
-
 
379
	jmp	.change_state
-
 
380
 
369
    jmp     tcprx_001
381
  .next_socket.2.exit:
-
 
382
 
-
 
383
    ; If we got here, there was no match
-
 
384
    ; Look for a socket where
370
 
385
    ; IP Packet TCP Destination Port = local Port
-
 
386
    ; socket Remote IP = 0
-
 
387
    ; socket remote Port = 0
-
 
388
 
-
 
389
	mov	ebx, net_sockets
-
 
390
 
-
 
391
  .next_socket.3:
-
 
392
	mov	ebx, [ebx + SOCKET.NextPtr]
-
 
393
	or	ebx, ebx
-
 
394
	jz	.next_socket.3.exit
-
 
395
 
-
 
396
	cmp	[ebx + SOCKET.Status], SOCK_OPEN
-
 
397
	jne	.next_socket.3
Line 371... Line -...
371
nxttst2:
-
 
372
    loop    ss2                     ; Return back if no match
398
 
Line 373... Line -...
373
 
-
 
374
    ; If we got here, there was no match
-
 
375
    ; Look for a socket where
-
 
376
    ; IP Packet TCP Destination Port = local Port
-
 
377
    ; socket Remote IP = 0
-
 
378
    ; socket remote Port = 0
399
;        DEBUGF  1, "K : tcp_rx - 3.dport: %x - %x\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [ebx + SOCKET.LocalPort]:4
379
 
-
 
380
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
-
 
381
    mov     ecx, NUM_SOCKETS
-
 
382
 
-
 
383
ss3:
400
 
Line 384... Line 401...
384
    sub     eax, SOCKETBUFFSIZE
401
	mov	ax, [edx + 20 + TCP_PACKET.DestinationPort]  ; get destination port from the TCP hdr
385
 
402
	cmp	[ebx + SOCKET.LocalPort], ax		; compare with socket's local port
Line 386... Line 403...
386
    movzx   ebx, word [edx + 22]     ; get destination port from the TCP hdr
403
	jne	.next_socket.3				; different - try next socket
387
    cmp     [eax + sockets + 12], bx ; compare with socket's local port
-
 
Line 388... Line 404...
388
    jnz     nxttst3                        ; different - try next socket
404
 
389
 
-
 
390
    mov     ebx, 0
-
 
Line -... Line 405...
-
 
405
;        DEBUGF  1, "K : tcp_rx - 3.addr: 00000000 - %x\n", [ebx + SOCKET.RemoteIP]
-
 
406
 
-
 
407
	cmp	[ebx + SOCKET.RemoteIP], 0		; only match a socket remote IP of 0
-
 
408
	jne	.next_socket.3				; different - try next socket
-
 
409
 
-
 
410
;        DEBUGF  1, "K : tcp_rx - 3.sport: 0000 - %x\n", [ebx + SOCKET.RemotePort]:4
-
 
411
 
-
 
412
	cmp	[ebx + SOCKET.RemotePort], 0		; only match a remote socket of 0
-
 
413
	jne	.next_socket.3				; different - try next socket
-
 
414
 
-
 
415
    ; We have a complete match - use this socket
-
 
416
	jmp	.change_state
-
 
417
 
391
    cmp     [eax + sockets + 20], bx ; only match a remote socket of 0
418
  .next_socket.3.exit:
-
 
419
 
-
 
420
    ; If we got here, we need to reject the packet
-
 
421
 
392
    jnz     nxttst3                        ; different - try next socket
422
	DEBUGF	1, "K : tcp_rx - dumped\n"
393
 
423
	DEBUGF	1, "K :   --------: %x-%x-%x (flags: %x)\n", [edx + 20 + TCP_PACKET.DestinationPort]:4, [edx + IP_PACKET.SourceAddress], [edx + 20 + TCP_PACKET.SourcePort]:4, [edx + 20 + TCP_PACKET.Flags]:2
394
    mov     ebx, 0
424
;       mov     ebx, net_sockets
395
    cmp     [eax + sockets + 16], ebx ; only match a socket remote IP of 0
425
;
396
    jnz     nxttst3                        ; different - try next socket
-
 
Line -... Line 426...
-
 
426
; .next_socket.4:
-
 
427
;       mov     ebx, [ebx + SOCKET.NextPtr]
397
 
428
;       or      ebx, ebx
398
    ; We have a complete match - use this socket
429
;       jz      .next_socket.4.exit
399
    jmp     tcprx_001
430
;       DEBUGF  1, "K :   %x: %x-%x-%x-%u\n", ebx, [ebx + SOCKET.LocalPort]:4, [ebx + SOCKET.RemoteIP], [ebx + SOCKET.RemotePort]:4, [ebx + SOCKET.TCBState]
Line 400... Line 431...
400
 
431
;       jne     .next_socket.4
401
nxttst3:
432
;
Line 402... Line 433...
402
    loop    ss3                     ; Return back if no match
433
; .next_socket.4.exit:
403
 
434
	inc	[dumped_rx_count]
404
    ; If we got here, we need to reject the packet
435
	jmp	.exit
Line 432... Line 463...
432
;          User data pointed to by esi
463
;          User data pointed to by esi
433
;       Data length in ecx
464
;       Data length in ecx
434
;          Transmit buffer number in eax
465
;          Transmit buffer number in eax
435
;
466
;
436
;***************************************************************************
467
;***************************************************************************
-
 
468
 
437
buildTCPPacket:
469
proc build_tcp_packet stdcall, sockAddr:DWORD
438
    push    ecx                        ; Save data length
470
    push    ecx                        ; Save data length
Line 439... Line 471...
439
 
471
 
440
    ; convert buffer pointer eax to the absolute address
472
    ; convert buffer pointer eax to the absolute address
441
    mov     ecx, IPBUFFSIZE
473
    mov     ecx, IPBUFFSIZE
442
    mul     ecx
474
    mul     ecx
Line 443... Line 475...
443
    add     eax, IPbuffs
475
    add     eax, IPbuffs
Line 444... Line 476...
444
 
476
 
Line 445... Line 477...
445
    mov     edx, eax
477
    mov     edx, eax
Line 446... Line 478...
446
 
478
 
Line 447... Line 479...
447
    mov     [edx + 33], bl            ; TCP flags
479
	mov	[edx + 20 + TCP_PACKET.Flags], bl		; TCP flags
448
 
480
 
449
    mov     ebx, [sktAddr]
481
	mov	ebx, [sockAddr]
450
 
482
 
451
    ; So, ebx holds the socket ptr, edx holds the IPbuffer ptr
483
    ; So, ebx holds the socket ptr, edx holds the IPbuffer ptr
452
 
484
 
453
    ; Fill in the IP header ( some data is in the socket descriptor)
-
 
454
    mov     eax, [ebx + 8]
485
    ; Fill in the IP header ( some data is in the socket descriptor)
455
    mov     [edx + 12], eax      ; source IP
-
 
456
    mov     eax, [ebx + 16]
486
	mov	eax, [ebx + SOCKET.LocalIP]
Line 457... Line 487...
457
    mov     [edx + 16], eax      ; Destination IP
487
	mov	[edx + IP_PACKET.SourceAddress], eax
458
 
488
	mov	eax, [ebx + SOCKET.RemoteIP]
Line 459... Line 489...
459
    mov     al, 0x45
489
	mov	[edx + IP_PACKET.DestinationAddress], eax
460
    mov     [edx], al         ; Version, IHL
-
 
461
    xor     al, al
-
 
462
    mov     [edx + 1], al     ; Type of service
490
 
463
 
491
	mov	[edx + IP_PACKET.VersionAndIHL], 0x45
464
    pop     eax                   ; Get the TCP data length
492
	mov	[edx + IP_PACKET.TypeOfService], 0
465
    push    eax
493
 
466
 
-
 
467
    add     eax, 20 + 20           ; add IP header and TCP header lengths
-
 
468
    mov     [edx + 2], ah
-
 
469
    mov     [edx + 3], al
-
 
470
    xor     al, al
494
    pop     eax                   ; Get the TCP data length
471
    mov     [edx + 4], al
495
    push    eax
472
    mov     [edx + 5], al
-
 
Line 473... Line 496...
473
    mov     al, 0x40
496
 
474
    mov     [edx + 6], al
-
 
475
    xor     al, al
497
    add     eax, 20 + 20           ; add IP header and TCP header lengths
Line 476... Line 498...
476
    mov     [edx + 7], al
498
	rol	ax, 8
477
    mov     al, 0x20
499
	mov	[edx + IP_PACKET.TotalLength], ax
478
    mov     [edx + 8], al
500
	mov	[edx + IP_PACKET.Identification], 0
Line 479... Line 501...
479
    mov     al, 6                         ; TCP protocol
501
	mov	[edx + IP_PACKET.FlagsAndFragmentOffset], 0x0040
480
    mov     [edx + 9], al
502
	mov	[edx + IP_PACKET.TimeToLive], 0x20
Line 481... Line 503...
481
 
503
	mov	[edx + IP_PACKET.Protocol], PROTOCOL_TCP
482
    ; Checksum left unfilled
-
 
483
    xor     ax, ax
504
 
Line 484... Line 505...
484
    mov     [edx + 10], ax
505
    ; Checksum left unfilled
485
 
506
	mov	[edx + IP_PACKET.HeaderChecksum], 0
486
    ; Fill in the TCP header ( some data is in the socket descriptor)
507
 
Line 487... Line 508...
487
    mov     ax, [ebx + 12]
508
	; Fill in the TCP header (some data is in the socket descriptor)
488
    mov     [edx + 20], ax        ; Local Port
509
	mov	ax, [ebx + SOCKET.LocalPort]
489
 
510
	mov	[edx + 20 + TCP_PACKET.SourcePort], ax		; Local Port
Line 490... Line 511...
490
    mov     ax, [ebx + 20]
511
 
491
    mov     [edx + 20 + 2], ax    ; desitination Port
512
	mov	ax, [ebx + SOCKET.RemotePort]
492
 
-
 
493
    ; Checksum left unfilled
513
	mov	[edx + 20 + TCP_PACKET.DestinationPort], ax	; desitination Port
Line 494... Line 514...
494
    xor     ax, ax
514
 
495
    mov     [edx + 20 + 16], ax
-
 
496
 
515
    ; Checksum left unfilled
Line 497... Line 516...
497
    ; sequence number
516
	mov	[edx + 20 + TCP_PACKET.Checksum], 0
498
    mov     eax, [ebx + 48]
-
 
499
    mov     [edx + 20 + 4], eax
517
 
Line 500... Line 518...
500
 
518
    ; sequence number
501
    ; ack number
519
	mov	eax, [ebx + SOCKET.SND_NXT]
Line 502... Line 520...
502
    mov     eax, [ebx + 56]
520
	mov	[edx + 20 + TCP_PACKET.SequenceNumber], eax
503
    mov     [edx + 20 + 8], eax
521
 
Line 504... Line 522...
504
 
522
    ; ack number
505
    ; window ( 0x2000 is default ).I could accept 4KB, fa0, ( skt buffer size)
523
	mov	eax, [ebx + SOCKET.RCV_NXT]
506
    ; 768 bytes seems better
524
	mov	[edx + 20 + TCP_PACKET.AckNumber], eax
507
    mov     ax, 0x0003
525
 
Line 508... Line -...
508
    mov     [edx + 20 + 14], ax
-
 
509
 
526
    ; window ( 0x2000 is default ).I could accept 4KB, fa0, ( skt buffer size)
510
    ; Urgent pointer (0)
527
    ; 768 bytes seems better
511
    mov     ax, 0
528
	mov	[edx + 20 + TCP_PACKET.Window], 0x0003
512
    mov     [edx + 20 + 18], ax
529
 
513
 
530
    ; Urgent pointer (0)
514
    ; data offset ( 0x50 )
531
	mov	[edx + 20 + TCP_PACKET.UrgentPointer], 0
515
    mov     al, 0x50
532
 
516
    mov     [edx + 20 + 12], al
-
 
517
 
533
    ; data offset ( 0x50 )
518
    pop     ecx                  ; count of bytes to send
534
	mov	[edx + 20 + TCP_PACKET.DataOffset], 0x50
519
    mov     ebx, ecx            ; need the length later
-
 
520
 
535
 
521
    cmp     ebx, 0
536
    pop     ecx                  ; count of bytes to send
Line 522... Line 537...
522
    jz      btp_001
537
    mov     ebx, ecx            ; need the length later
523
 
538
 
524
    mov     edi, edx
539
    cmp     ebx, 0
525
    add     edi, 40
540
	jz	@f
526
    cld
541
 
527
    rep     movsb               ; copy the data across
542
    mov     edi, edx
528
 
543
    add     edi, 40
529
btp_001:
544
    cld
Line 530... Line 545...
530
    ; we have edx as IPbuffer ptr.
545
    rep     movsb               ; copy the data across
Line 531... Line 546...
531
    ; Fill in the TCP checksum
546
 
532
    ; First, fill in pseudoheader
547
    @@: ; we have edx as IPbuffer ptr.
533
    mov     eax, [edx + 12]
-
 
534
    mov     [pseudoHeader], eax
548
    ; Fill in the TCP checksum
535
    mov     eax, [edx + 16]
549
    ; First, fill in pseudoheader
Line 536... Line 550...
536
    mov     [pseudoHeader+4], eax
550
	mov	eax, [edx + IP_PACKET.SourceAddress]
537
    mov     ax, 0x0600            ; 0 + protocol
551
    mov     [pseudoHeader], eax
538
    mov     [pseudoHeader+8], ax
552
	mov	eax, [edx + IP_PACKET.DestinationAddress]
539
    add     ebx, 20
-
 
540
    mov     eax, ebx
553
	mov	[pseudoHeader + 4], eax
541
    mov     [pseudoHeader+10], ah
554
	mov	word[pseudoHeader + 8], PROTOCOL_TCP shl 8 + 0
Line 542... Line 555...
542
    mov     [pseudoHeader+11], al
555
    add     ebx, 20
-
 
556
	mov	[pseudoHeader + 10], bh
Line 543... Line 557...
543
 
557
	mov	[pseudoHeader + 11], bl
544
    mov     eax, pseudoHeader
558
 
545
    mov     [checkAdd1], eax
559
    mov     eax, pseudoHeader
546
    mov     [checkSize1], word 12
-
 
547
    mov     eax, edx
560
    mov     [checkAdd1], eax
548
    add     eax, 20
561
	mov	word[checkSize1], 12
549
    mov     [checkAdd2], eax
-
 
550
    mov     eax, ebx
-
 
551
    mov     [checkSize2], ax
-
 
552
 
-
 
553
    call    checksum
-
 
554
 
562
    mov     eax, edx
555
    ; store it in the TCP checksum ( in the correct order! )
-
 
556
    mov     ax, [checkResult]
-
 
557
 
-
 
558
    mov     [edx + 20 + 16], ah
-
 
559
    mov     [edx + 20 + 17], al
-
 
560
 
-
 
561
    ; Fill in the IP header checksum
-
 
562
    GET_IHL eax,edx              ; get IP-Header length
-
 
563
    stdcall checksum_jb,edx,eax  ; buf_ptr, buf_size
-
 
564
 
-
 
565
    mov     [edx + 10], ah
-
 
566
    mov     [edx + 11], al
563
    add     eax, 20
567
 
564
    mov     [checkAdd2], eax
568
    ret
-
 
569
 
-
 
570
 
565
    mov     eax, ebx
571
; Increments the 32 bit value pointed to by esi in internet order
566
    mov     [checkSize2], ax
-
 
567
 
Line 572... Line 568...
572
inc_inet_esi:
568
    call    checksum
573
    push    eax
569
 
574
    add     esi, 3
570
    ; store it in the TCP checksum ( in the correct order! )
575
    mov     al, byte[esi]
571
    mov     ax, [checkResult]
576
    inc     al
-
 
577
    mov     byte[esi], al
572
	rol	ax, 8
578
    cmp     al, 0
573
	mov	[edx + 20 + TCP_PACKET.Checksum], ax
579
    jnz     iie_exit
-
 
580
    dec     esi
-
 
581
    mov     al, byte[esi]
-
 
582
    inc     al
-
 
583
    mov     byte[esi], al
-
 
584
    cmp     al, 0
-
 
585
    jnz     iie_exit
-
 
586
    dec     esi
-
 
587
    mov     al, byte[esi]
574
 
588
    inc     al
-
 
589
    mov     byte[esi], al
-
 
590
    cmp     al, 0
-
 
591
    jnz     iie_exit
-
 
592
    dec     esi
-
 
593
    mov     al, byte[esi]
575
    ; Fill in the IP header checksum
594
    inc     al
-
 
595
    mov     byte[esi], al
-
 
596
 
-
 
597
iie_exit:
576
	GET_IHL eax, edx	       ; get IP-Header length
598
    pop     eax
577
	stdcall checksum_jb, edx, eax  ; buf_ptr, buf_size
599
    ret
578
	rol	ax, 8
-
 
579
	mov	[edx + IP_PACKET.HeaderChecksum], ax
Line 600... Line 580...
600
 
580
 
601
 
581
    ret
602
; Increments the 32 bit value pointed to by esi in internet order
582
endp
603
; by the value in ecx
583
 
604
add_inet_esi:
584
 
605
    push    eax
585
; Increments the 32 bit value pointed to by esi in internet order
606
 
586
proc inc_inet_esi stdcall
607
    mov     al, [esi]
587
    push    eax
608
    shl     eax, 8
588
	mov	eax, [esi]
609
    inc     esi
589
	bswap	eax
610
    mov     al, [esi]
590
	inc	eax
611
    shl     eax, 8
591
	bswap	eax
612
    inc     esi
592
	mov	[esi], eax
613
    mov     al, [esi]
593
    pop     eax
Line 614... Line 594...
614
    shl     eax, 8
594
    ret
615
    inc     esi
595
endp
616
    mov     al, [esi]
596
 
Line 651... Line 631...
651
;   Description
631
;   Description
652
;       TCP state machine
632
;       TCP state machine
653
;       This is a kernel function, called by tcp_rx
633
;       This is a kernel function, called by tcp_rx
654
;
634
;
655
;       IP buffer address given in edx
635
;       IP buffer address given in edx
656
;          Socket/TCB address in [eax + sockets]
636
;          Socket/TCB address in ebx
657
;
637
;
658
;       The IP buffer will be released by the caller
638
;       The IP buffer will be released by the caller
659
;***************************************************************************
639
;***************************************************************************
660
tcpStateMachine:
-
 
661
    mov     ebx, sockets
-
 
662
    add     ebx, eax
-
 
663
    mov     [sktAddr], ebx
-
 
Line -... Line 640...
-
 
640
 
664
 
641
proc tcpStateMachine stdcall, sockAddr:DWORD
665
    ; as a packet has been received, update the TCB timer
642
    ; as a packet has been received, update the TCB timer
666
    mov     ecx, TWOMSL
-
 
Line 667... Line 643...
667
    mov     [ebx + 32], ecx
643
	mov	[ebx + SOCKET.TCBTimer], TWOMSL
668
 
644
 
669
    ; If the received packet has an ACK bit set,
645
    ; If the received packet has an ACK bit set,
670
    ; remove any packets in the resend queue that this
646
    ; remove any packets in the resend queue that this
671
    ; received packet acknowledges
647
    ; received packet acknowledges
672
    pusha
-
 
673
    mov     cl, [edx + 33]
-
 
674
    and     cl, 0x10
648
	pushad
675
    cmp     cl, 0x10
-
 
Line 676... Line 649...
676
    jne     tsm001                      ; No ACK, so no data yet
649
	test	[edx + 20 + TCP_PACKET.Flags], TH_ACK
677
 
650
	jz	.call_handler					; No ACK, so no data yet
Line 678... Line 651...
678
 
651
 
679
    ; get skt number in al
652
	; get skt number in eax
Line 680... Line 653...
680
    shr     eax, 12
653
	stdcall net_socket_addr_to_num, ebx
681
 
654
 
Line 682... Line 655...
682
    ; The ack number is in [edx + 28], inet format
655
    ; The ack number is in [edx + 28], inet format
683
    ; skt in al
656
	; skt in eax
684
 
657
 
-
 
658
    mov     esi, resendQ
685
    mov     esi, resendQ
659
	xor	ecx, ecx
686
    mov     ecx, 0
660
 
687
 
661
  .next_resendq:
688
t001:
662
    cmp     ecx, NUMRESENDENTRIES
689
    cmp     ecx, NUMRESENDENTRIES
663
	je	.call_handler	  ; None left
Line 690... Line 664...
690
    je      t003              ; None left
664
	;cmp     [esi], al                                       ; XTODO: al -> eax
Line 691... Line 665...
691
    cmp     [esi], al
665
	cmp	[esi + 4], eax
692
    je      t002              ; found one
666
	je	@f		  ; found one
Line 693... Line 667...
693
    inc     ecx
667
    inc     ecx
694
    add     esi, 4
-
 
695
    jmp     t001
668
	add	esi, 8
696
 
-
 
697
t002:                   ; Can we delete this buffer?
-
 
698
 
669
	jmp	.next_resendq
699
                        ; If yes, goto t004. No, goto t001
670
 
Line 700... Line 671...
700
    ; Get packet data address
671
    @@: 		  ; Can we delete this buffer?
701
 
672
 
702
    push    ecx
673
			  ; If yes, goto @@. No, goto .next_resendq
703
    inc     ecx
-
 
704
    ; Now get buffer location, and copy buffer across. argh! more copying,,
674
    ; Get packet data address
705
    mov     edi, resendBuffer - IPBUFFSIZE
675
 
706
t002a:
-
 
707
    add     edi, IPBUFFSIZE
-
 
708
    loop    t002a
-
 
709
 
-
 
710
    ; we have dest buffer location in edi. incoming packet in edx.
-
 
711
    ; Get this packets sequence number
676
    push    ecx
712
    ; preserve al, ecx, esi, edx
677
    ; Now get buffer location, and copy buffer across. argh! more copying,,
713
 
678
	imul	edi, ecx, IPBUFFSIZE
714
    mov     cl, [edi + 24]
679
	add	edi, resendBuffer
Line 715... Line 680...
715
    shl     ecx, 8
680
 
716
    mov     cl, [edi + 25]
-
 
717
    shl     ecx, 8
-
 
718
    mov     cl, [edi + 26]
681
    ; we have dest buffer location in edi. incoming packet in edx.
719
    shl     ecx, 8
682
    ; Get this packets sequence number
720
    mov     cl, [edi + 27]
-
 
721
    movzx   ebx, byte [edi + 3]
-
 
722
    mov     bh, [edi + 2]
-
 
Line 723... Line 683...
723
    sub     ebx, 40
683
    ; preserve al, ecx, esi, edx
724
    add     ecx, ebx          ; ecx is now seq# of last byte +1, intel format
684
	mov	ecx, [edi + 20 + TCP_PACKET.SequenceNumber]
725
 
685
	bswap	ecx
726
    ; get recievd ack #, in intel format
686
	movzx	ebx, word[edi + 2]
-
 
687
	xchg	bl, bh
Line 727... Line -...
727
    mov     bl, [edx + 28]
-
 
728
    shl     ebx, 8
688
    sub     ebx, 40
729
    mov     bl, [edx + 29]
689
    add     ecx, ebx          ; ecx is now seq# of last byte +1, intel format
730
    shl     ebx, 8
690
 
731
    mov     bl, [edx + 30]
-
 
Line 732... Line -...
732
    shl     ebx, 8
-
 
733
    mov     bl, [edx + 31]
691
    ; get recievd ack #, in intel format
734
 
-
 
735
    cmp     ebx, ecx        ; Finally. ecx = rx'ed ack. ebx = last byte in que
692
	mov	ebx, [edx + 20 + TCP_PACKET.AckNumber]
736
                            ; DANGER! need to handle case that we have just
693
	bswap	ebx
737
                            ; passed the 2**32, and wrapped round!
694
 
738
    pop     ecx
695
    cmp     ebx, ecx        ; Finally. ecx = rx'ed ack. ebx = last byte in que
739
 
-
 
740
    jae     t004             ; if rx > old, delete old
-
 
Line 741... Line 696...
741
    inc     ecx
696
                            ; DANGER! need to handle case that we have just
742
    add     esi, 4
697
                            ; passed the 2**32, and wrapped round!
Line 743... Line 698...
743
    jmp     t001
698
    pop     ecx
744
 
-
 
745
 
-
 
746
t004:
-
 
747
    dec     dword [arp_rx_count] ; ************ TEST ONLY!
-
 
748
 
-
 
Line -... Line 699...
-
 
699
	jae	@f		; if rx > old, delete old
749
    mov     [esi], byte 0xFF
700
 
-
 
701
    inc     ecx
750
    inc     ecx
702
	add	esi, 8
-
 
703
	jmp	.next_resendq
Line 751... Line 704...
751
    add     esi, 4
704
 
752
    jmp     t001
-
 
Line -... Line 705...
-
 
705
    ;@@: mov     byte[esi], 0xff                                 ; XTODO: 0xff -> 0
-
 
706
    @@: mov	dword[esi + 4], 0
-
 
707
    inc     ecx
Line 753... Line 708...
753
 
708
	add	esi, 8
754
t003:
709
	jmp	.next_resendq
755
 
710
 
756
tsm001:
711
  .call_handler:
Line 757... Line 712...
757
    popa
712
	popad
758
 
713
 
759
    ; Call handler for given TCB state
-
 
760
    mov     ebx, [eax + sockets+28]
-
 
761
    cmp     ebx, TCB_LISTEN
714
	; Call handler for given TCB state
Line 762... Line 715...
762
    jb      tsm_exit
715
 
763
    cmp     ebx, TCB_CLOSED
716
	mov	eax, [ebx + SOCKET.TCBState]
Line 764... Line 717...
764
    ja      tsm_exit
717
	cmp	eax, TCB_LISTEN
765
 
718
	jb	.exit
766
    dec     ebx
719
	cmp	eax, TCB_CLOSED
767
    call    dword [TCBStateHandler+ebx*4]
720
	ja	.exit
768
 
721
 
769
tsm_exit:
722
	stdcall [TCBStateHandler + (eax - 1) * 4], [sockAddr]
770
    ret
723
 
771
 
724
  .exit:
772
 
-
 
773
 
-
 
774
stateTCB_LISTEN:
725
    ret
775
    ; In this case, we are expecting a SYN packet
726
endp
776
    ; For now, if the packet is a SYN, process it, and send a response
727
 
Line 777... Line 728...
777
    ; If not, ignore it
728
 
778
 
729
proc stateTCB_LISTEN stdcall, sockAddr:DWORD
779
    ; Look at control flags
730
    ; In this case, we are expecting a SYN packet
780
    mov     bl, [edx + 33]
731
    ; For now, if the packet is a SYN, process it, and send a response
781
    and     bl, 0x02
732
    ; If not, ignore it
Line 782... Line 733...
782
    cmp     bl, 0x02
733
 
783
    jnz     stl_exit
734
    ; Look at control flags
784
 
735
	test	[edx + 20 + TCP_PACKET.Flags], TH_SYN
785
    ; We have a SYN. update the socket with this IP packets details,
736
	jz	.exit
786
    ; And send a response
-
 
787
 
737
 
Line 788... Line 738...
788
    mov     ebx, [edx + 12] ; IP source address
738
    ; We have a SYN. update the socket with this IP packets details,
789
    mov     [eax + sockets + 16], ebx
739
    ; And send a response
790
    mov     bx, [edx + 20] ; IP source port
740
 
791
    mov     [eax + sockets + 20], bx
741
	mov	eax, [edx + IP_PACKET.SourceAddress]
792
    mov     ebx, [edx + 24] ; IRS
-
 
793
    mov     [eax + sockets + 40], ebx
742
	mov	[ebx + SOCKET.RemoteIP], eax
794
    mov     [eax + sockets + 56], ebx
743
	mov	ax, [edx + 20 + TCP_PACKET.SourcePort]
Line 795... Line 744...
795
    mov     esi, sockets
744
	mov	[ebx + SOCKET.RemotePort], ax
796
    add     esi, eax
745
	mov	eax, [edx + 20 + TCP_PACKET.SequenceNumber]
797
    add     esi, 56
746
	mov	[ebx + SOCKET.IRS], eax
798
    call    inc_inet_esi ; RCV.NXT
747
	mov	[ebx + SOCKET.RCV_NXT], eax
Line -... Line 748...
-
 
748
	lea	esi, [ebx + SOCKET.RCV_NXT]
-
 
749
    call    inc_inet_esi ; RCV.NXT
Line 799... Line -...
799
    mov     ebx, [eax + sockets + 36]    ; ISS
-
 
800
    mov     [eax + sockets + 48], ebx    ; SND.NXT
-
 
801
 
-
 
802
    ; Now construct the response, and queue for sending by IP
-
 
803
    mov     eax, EMPTY_QUEUE
750
	mov	eax, [ebx + SOCKET.ISS]
804
    call    dequeue
751
	mov	[ebx + SOCKET.SND_NXT], eax
805
    cmp     ax, NO_BUFFER
752
 
Line 806... Line 753...
806
    je      stl_exit
753
    ; Now construct the response, and queue for sending by IP
807
 
754
    mov     eax, EMPTY_QUEUE
-
 
755
    call    dequeue
Line 808... Line -...
808
    push    eax
-
 
809
    mov     bl, 0x12        ; SYN + ACK
756
    cmp     ax, NO_BUFFER
810
    mov     ecx, 0
757
	je	.exit
811
    mov     esi, 0
758
 
812
 
-
 
813
    call    buildTCPPacket
-
 
814
 
-
 
815
    mov     eax, NET1OUT_QUEUE
-
 
816
    mov     edx, [stack_ip]
-
 
817
    mov     ecx, [ sktAddr ]
-
 
818
    mov     ecx, [ ecx + 16 ]
-
 
819
    cmp     edx, ecx
-
 
Line -... Line 759...
-
 
759
    push    eax
-
 
760
	mov	bl, TH_SYN + TH_ACK
-
 
761
	xor	ecx, ecx
-
 
762
	xor	esi, esi
-
 
763
	stdcall build_tcp_packet, [sockAddr]
-
 
764
 
-
 
765
    mov     eax, NET1OUT_QUEUE
-
 
766
    mov     edx, [stack_ip]
-
 
767
	mov	ecx, [sockAddr]
-
 
768
	cmp	edx, [ecx + SOCKET.RemoteIP]
-
 
769
	jne	.not_local
-
 
770
    mov     eax, IPIN_QUEUE
-
 
771
 
-
 
772
  .not_local:
-
 
773
       ; Send it.
-
 
774
    pop     ebx
-
 
775
    call    queue
820
    jne     stl_notlocal
776
 
821
    mov     eax, IPIN_QUEUE
777
	mov	esi, [sockAddr]
Line 822... Line 778...
822
 
778
	mov	[esi + SOCKET.TCBState], TCB_SYN_RECEIVED
823
stl_notlocal:
779
 
824
       ; Send it.
-
 
825
    pop     ebx
780
	; increment SND.NXT in socket
826
    call    queue
781
	add	esi, SOCKET.SND_NXT
Line 827... Line 782...
827
 
782
    call    inc_inet_esi
828
 
783
 
829
    mov     ebx, TCB_SYN_RECEIVED
784
  .exit:
830
    mov     esi, [sktAddr]
785
    ret
831
    mov     [esi + 28], ebx
786
endp
-
 
787
 
832
 
788
 
Line 833... Line 789...
833
    ; increament SND.NXT in socket
789
proc stateTCB_SYN_SENT stdcall, sockAddr:DWORD
Line 834... Line -...
834
    add     esi, 48
-
 
835
    call    inc_inet_esi
790
    ; We are awaiting an ACK to our SYN, with a SYM
836
 
791
    ; Look at control flags - expecting an ACK
837
stl_exit:
-
 
838
    ret
792
 
Line 839... Line 793...
839
 
793
	mov	al, [edx + 20 + TCP_PACKET.Flags]
Line 840... Line 794...
840
 
794
	and	al, TH_SYN + TH_ACK
841
 
795
	cmp	al, TH_SYN + TH_ACK
842
stateTCB_SYN_SENT:
796
	je	.syn_ack
843
    ; We are awaiting an ACK to our SYN, with a SYM
-
 
844
    ; Look at control flags - expecting an ACK
797
 
845
    mov     bl, [edx + 33]
798
	test	al, TH_SYN
Line 846... Line 799...
846
    and     bl, 0x12
799
	jz	.exit
847
    cmp     bl, 0x12
800
 
848
    jnz     stss_exit
801
	mov	[ebx + SOCKET.TCBState], TCB_SYN_RECEIVED
849
 
802
	push	TH_SYN + TH_ACK
Line 850... Line 803...
850
    mov     ebx, TCB_ESTABLISHED
803
	jmp	.send
851
    mov     esi, [sktAddr]
804
 
-
 
805
  .syn_ack:
Line 852... Line -...
852
    mov     [esi + 28], ebx
-
 
853
 
806
	mov	[ebx + SOCKET.TCBState], TCB_ESTABLISHED
854
    ; Store the recv.nxt field
807
	push	TH_ACK
855
    mov     eax, [edx + 24]
808
 
856
 
809
  .send:
Line -... Line 810...
-
 
810
    ; Store the recv.nxt field
-
 
811
	mov	eax, [edx + 20 + TCP_PACKET.SequenceNumber]
-
 
812
 
-
 
813
    ; Update our recv.nxt field
-
 
814
	mov	[ebx + SOCKET.RCV_NXT], eax
-
 
815
	lea	esi, [ebx + SOCKET.RCV_NXT]
-
 
816
    call    inc_inet_esi
-
 
817
 
-
 
818
    ; Send an ACK
-
 
819
    ; Now construct the response, and queue for sending by IP
857
    ; Update our recv.nxt field
820
    mov     eax, EMPTY_QUEUE
858
    mov     esi, [sktAddr]
821
    call    dequeue
859
    add     esi, 56
-
 
860
    mov     [esi], eax
-
 
861
    call    inc_inet_esi
822
    cmp     ax, NO_BUFFER
862
 
-
 
863
    ; Send an ACK
-
 
864
    ; Now construct the response, and queue for sending by IP
-
 
865
    mov     eax, EMPTY_QUEUE
-
 
Line 866... Line 823...
866
    call    dequeue
823
	pop	ebx
867
    cmp     ax, NO_BUFFER
-
 
Line -... Line 824...
-
 
824
	je	.exit
-
 
825
 
-
 
826
    push    eax
Line 868... Line 827...
868
    je      stss_exit
827
 
869
 
828
	xor	ecx, ecx
870
    push    eax
829
	xor	esi, esi
Line 871... Line 830...
871
 
830
	stdcall build_tcp_packet, [sockAddr]
872
    mov     bl, 0x10        ; ACK
831
 
873
    mov     ecx, 0
832
    mov     eax, NET1OUT_QUEUE
874
    mov     esi, 0
833
 
875
 
834
    mov     edx, [stack_ip]
Line 876... Line 835...
876
    call    buildTCPPacket
835
	mov	ecx, [sockAddr]
Line 877... Line 836...
877
 
836
	cmp	edx, [ecx + SOCKET.RemoteIP]
878
    mov     eax, NET1OUT_QUEUE
837
	jne	.not_local
Line 879... Line -...
879
 
-
 
880
    mov     edx, [stack_ip]
838
    mov     eax, IPIN_QUEUE
881
    mov     ecx, [ sktAddr ]
839
 
Line 882... Line 840...
882
    mov     ecx, [ ecx + 16 ]
840
  .not_local:
883
    cmp     edx, ecx
841
       ; Send it.
Line 884... Line 842...
884
    jne     stss_notlocal
842
    pop     ebx
885
    mov     eax, IPIN_QUEUE
843
    call    queue
886
 
844
 
-
 
845
  .exit:
887
stss_notlocal:
846
    ret
888
       ; Send it.
847
endp
-
 
848
 
-
 
849
 
-
 
850
proc stateTCB_SYN_RECEIVED stdcall, sockAddr:DWORD
-
 
851
    ; In this case, we are expecting an ACK packet
-
 
852
    ; For now, if the packet is an ACK, process it,
-
 
853
    ; If not, ignore it
889
    pop     ebx
854
 
890
    call    queue
855
	test	[edx + 20 + TCP_PACKET.Flags], TH_RST		;xxx
-
 
856
	jz	.check_ack					;xxx
-
 
857
 
-
 
858
	push	[ebx + SOCKET.OrigRemotePort] [ebx + SOCKET.OrigRemoteIP]
-
 
859
	pop	[ebx + SOCKET.RemoteIP] [ebx + SOCKET.RemotePort]
-
 
860
 
-
 
861
	mov	[ebx + SOCKET.TCBState], TCB_LISTEN		;xxx
-
 
862
	jmp	.exit						;xxx
891
 
863
 
Line 892... Line -...
892
stss_exit:
-
 
893
    ret
864
  .check_ack:							;xxx
-
 
865
    ; Look at control flags - expecting an ACK
Line 894... Line 866...
894
 
866
	test	[edx + 20 + TCP_PACKET.Flags], TH_ACK
895
 
-
 
Line 896... Line -...
896
 
-
 
897
stateTCB_SYN_RECEIVED:
-
 
898
    ; In this case, we are expecting an ACK packet
-
 
899
    ; For now, if the packet is an ACK, process it,
-
 
900
    ; If not, ignore it
-
 
901
 
-
 
902
    ; Look at control flags - expecting an ACK
-
 
903
    mov     bl, [edx + 33]
-
 
904
    and     bl, 0x10
-
 
905
    cmp     bl, 0x10
-
 
906
    jnz     stsr_exit
-
 
907
 
-
 
908
    mov     ebx, TCB_ESTABLISHED
-
 
909
    mov     esi, [sktAddr]
-
 
910
    mov     [esi + 28], ebx
-
 
911
 
-
 
912
stsr_exit:
-
 
913
    ret
-
 
914
 
867
	jz	.exit
915
 
868
 
916
 
-
 
917
stateTCB_ESTABLISHED:
869
	mov	[ebx + SOCKET.TCBState], TCB_ESTABLISHED
918
    ; Here we are expecting data, or a request to close
870
 
919
    ; OR both...
871
  .exit:
Line 920... Line 872...
920
 
872
    ret
921
    ; Did we receive a FIN or RST?
873
endp
922
    mov     bl, [edx + 33]
874
 
923
    and     bl, 0x05
-
 
924
    cmp     bl, 0
-
 
925
    je      ste_chkack
875
 
926
 
-
 
Line 927... Line 876...
927
    ; It was a fin or reset.
876
proc stateTCB_ESTABLISHED stdcall, sockAddr:DWORD
928
 
877
    ; Here we are expecting data, or a request to close
929
    ; Remove resend entries from the queue  - I dont want to send any more data
878
    ; OR both...
930
    pusha
-
 
931
 
879
 
932
    mov     ebx, [sktAddr]
880
    ; Did we receive a FIN or RST?
933
    sub     ebx, sockets
881
;xxx    test    [edx + 20 + TCP_PACKET.Flags], TH_FIN + TH_RST
934
    shr     ebx, 12             ; get skt #
882
;xxx    jz      .check_ack
935
 
883
	test	[edx + 20 + TCP_PACKET.Flags], TH_FIN		;xxx
936
    mov     esi, resendQ
-
 
937
    mov     ecx, 0
-
 
938
 
-
 
Line 939... Line 884...
939
ste001:
884
	jz	.check_ack					;xxx
Line 940... Line 885...
940
    cmp     ecx, NUMRESENDENTRIES
885
 
941
    je      ste003              ; None left
886
    ; It was a fin or reset.
942
    cmp     [esi], bl
887
 
943
    je      ste002              ; found one
888
    ; Remove resend entries from the queue  - I dont want to send any more data
944
    inc     ecx
889
	pushad
Line 945... Line 890...
945
    add     esi, 4
890
 
946
    jmp     ste001
891
	; get skt #
947
 
892
	stdcall net_socket_addr_to_num, ebx
948
ste002:
893
 
949
    dec     dword [arp_rx_count] ; ************ TEST ONLY!
-
 
950
 
-
 
951
    mov     [esi], byte 0xFF
894
    mov     esi, resendQ
952
    jmp     ste001
-
 
953
 
895
    mov     ecx, 0
954
ste003:
896
 
Line 955... Line -...
955
    popa
-
 
956
 
897
  .next_resendq:
957
    ; was it a reset?
898
    cmp     ecx, NUMRESENDENTRIES
Line 958... Line 899...
958
    mov     bl, [edx + 33]
899
	je	.last_resendq	    ; None left
959
    and     bl, 0x04
-
 
960
    cmp     bl, 0x04
900
	;cmp     [esi], al                                              ; XTODO: al -> eax
961
    jne     ste003a
901
	cmp	[esi + 4], eax
962
 
902
	je	@f		    ; found one
963
    mov     esi, [sktAddr]
-
 
964
    mov     ebx, TCB_CLOSED
-
 
965
    mov     [esi + 28], ebx
903
	inc	ecx
Line 966... Line 904...
966
    jmp     ste_exit
904
	add	esi, 8
967
 
-
 
968
ste003a:
-
 
969
    ; Send an ACK to that fin, and enter closewait state
905
	jmp	.next_resendq
970
 
906
 
971
    mov     esi, [sktAddr]
907
    ;@@: mov     byte[esi], 0xff                                         ; XTODO: 0xff -> 0
Line 972... Line 908...
972
    mov     ebx, TCB_CLOSE_WAIT
908
    @@: mov	dword[esi + 4], 0
973
    mov     [esi + 28], ebx
909
    inc     ecx
974
    add     esi, 56
-
 
Line 975... Line 910...
975
    mov     eax, [esi]              ; save original
910
	add	esi, 8
Line 976... Line 911...
976
    call    inc_inet_esi
911
	jmp	.next_resendq
977
    ;; jmp    ste_ack - NO, there may be data
912
 
Line 978... Line 913...
978
 
913
  .last_resendq:
Line 979... Line 914...
979
ste_chkack:
914
	popad
980
    ; Check that we received an ACK
915
 
981
    mov     bl, [edx + 33]
916
;xxx    ; was it a reset?
982
    and     bl, 0x10
-
 
Line 983... Line 917...
983
    cmp     bl, 0x10
917
;xxx    test    [edx + 20 + TCP_PACKET.Flags], TH_RST
984
    jnz     ste_exit
-
 
985
 
918
;xxx    jz      @f
Line 986... Line 919...
986
 
919
 
987
    ; TODO - done, I think!
920
;xxx    mov     [ebx + SOCKET.TCBState], TCB_CLOSED
Line 988... Line 921...
988
    ; First, look at the incoming window. If this is less than or equal to 1024,
921
;xxx    jmp     .exit
989
    ; Set the socket window timer to 1. This will stop an additional packets being
922
 
990
    ; queued.
923
    @@: ; Send an ACK to that fin, and enter closewait state
991
    ; ** I may need to tweak this value, since I do not know how many packets are already queued
924
 
Line 992... Line 925...
992
    mov     ch, [edx + 34]
925
	mov	[ebx + SOCKET.TCBState], TCB_CLOSE_WAIT
993
    mov     cl, [edx + 35]
926
	lea	esi, [ebx + SOCKET.RCV_NXT]
994
    cmp     cx, 1024
927
    mov     eax, [esi]              ; save original
995
    ja      ste004
928
    call    inc_inet_esi
996
 
929
    ;; jmp    ste_ack - NO, there may be data
997
    mov     ecx, [sktAddr]
930
 
998
    mov     [ecx+72], dword 1
931
  .check_ack:
Line 999... Line 932...
999
 
932
    ; Check that we received an ACK
1000
ste004:
933
	test	[edx + 20 + TCP_PACKET.Flags], TH_ACK
1001
 
934
	jz	.exit
Line 1002... Line 935...
1002
    ; OK, here is the deal
935
 
Line 1003... Line 936...
1003
    ; My recv.nct field holds the seq of the expected next rec byte
936
    ; TODO - done, I think!
1004
    ; if the recevied sequence number is not equal to this, do not
937
    ; First, look at the incoming window. If this is less than or equal to 1024,
1005
    ; increment the recv.nxt field, do not copy data - just send a
-
 
1006
    ; repeat ack.
938
	; Set the socket window timer to 1. This will stop an additional packets being queued.
Line 1007... Line 939...
1007
 
939
    ; ** I may need to tweak this value, since I do not know how many packets are already queued
1008
    ; recv.nxt is in dword [edx+24], in inext format
940
	mov	cx, [edx + 20 + TCP_PACKET.Window]
1009
    ; recv seq is in [sktAddr]+56, in inet format
941
	xchg	cl, ch
1010
    ; just do a comparision
942
    cmp     cx, 1024
1011
    mov     ecx, [sktAddr]
943
	ja	@f
1012
    add     ecx, 56
944
 
1013
 
945
	mov	[ebx + SOCKET.wndsizeTimer], 1
Line 1014... Line 946...
1014
    cmp     [ecx - 56 + 28], dword TCB_CLOSE_WAIT
946
 
Line 1015... Line 947...
1015
    mov     ecx, [ecx]
947
    @@: ; OK, here is the deal
1016
    jne     stenofin
948
    ; My recv.nct field holds the seq of the expected next rec byte
1017
    mov     ecx, eax
949
    ; if the recevied sequence number is not equal to this, do not
1018
 
-
 
1019
stenofin:
950
    ; increment the recv.nxt field, do not copy data - just send a
Line 1020... Line 951...
1020
    cmp     ecx, [edx+24]
951
    ; repeat ack.
Line 1021... Line 952...
1021
    jne     ste_ack
952
 
1022
 
953
	; recv.nxt is in dword [edx+24], in inet format
1023
 
954
    ; recv seq is in [sktAddr]+56, in inet format
1024
    ; Read the data bytes, store in socket buffer
-
 
1025
    xor     ecx, ecx
955
    ; just do a comparision
1026
    mov     ch, [edx + 2]
956
	mov	ecx, [ebx + SOCKET.RCV_NXT]
1027
    mov     cl, [edx + 3]
-
 
Line -... Line 957...
-
 
957
	cmp	[ebx + SOCKET.TCBState], TCB_CLOSE_WAIT
1028
    sub     ecx, 40                    ; Discard 40 bytes of header
958
	jne	@f
1029
 
959
    mov     ecx, eax
1030
    cmp     ecx, 0
960
 
Line 1031... Line 961...
1031
    jnz     ste_data                ; Read data, if any
961
    @@: cmp	ecx, [edx + 20 + TCP_PACKET.SequenceNumber]
1032
 
962
	jne	.ack
-
 
963
 
Line 1033... Line -...
1033
    ; If we had received a fin, we need to ACK it.
-
 
1034
    mov     esi, [sktAddr]
964
 
1035
    mov     ebx, [esi + 28]
965
    ; Read the data bytes, store in socket buffer
1036
    cmp     ebx, TCB_CLOSE_WAIT
966
	movzx	ecx, [edx + IP_PACKET.TotalLength]
1037
    jz      ste_ack
967
	xchg	cl, ch
-
 
968
    sub     ecx, 40                    ; Discard 40 bytes of header
1038
    jnz     ste_exit
969
	jnz	.data			   ; Read data, if any
1039
 
970
 
Line 1040... Line 971...
1040
ste_data:
971
    ; If we had received a fin, we need to ACK it.
1041
    push    ecx
-
 
1042
    mov     esi, [sktAddr]
972
	cmp	[ebx + SOCKET.TCBState], TCB_CLOSE_WAIT
1043
 
-
 
1044
    add     [esi + 24], ecx      ; increment the count of bytes in buffer
973
	je	.ack
1045
 
974
	jmp	.exit
1046
    mov     eax, [esi + 4]       ; get socket owner PID
-
 
1047
    push    eax
975
 
1048
 
976
  .data:
-
 
977
    push    ecx
1049
    mov     eax, [esi + 24]      ; get # of bytes already in buffer
978
 
-
 
979
	add	[ebx + SOCKET.rxDataCount], ecx      ; increment the count of bytes in buffer
1050
 
980
 
1051
    ; point to the location to store the data
-
 
1052
    add     esi, eax
981
	mov	eax, [ebx + SOCKET.PID]       ; get socket owner PID
Line 1053... Line 982...
1053
    sub     esi, ecx
982
    push    eax
1054
    add     esi, SOCKETHEADERSIZE
983
 
1055
 
984
	mov	eax, [ebx + SOCKET.rxDataCount]      ; get # of bytes already in buffer
1056
    add     edx, 40        ; edx now points to the data
985
 
1057
    mov     edi, esi
986
    ; point to the location to store the data
Line 1058... Line 987...
1058
    mov     esi, edx
987
	lea	edi, [ebx + eax + SOCKETHEADERSIZE]
Line 1059... Line 988...
1059
 
988
	sub	edi, ecx
1060
    cld
989
 
1061
    rep     movsb          ; copy the data across
990
    add     edx, 40        ; edx now points to the data
-
 
991
    mov     esi, edx
Line 1062... Line -...
1062
 
-
 
1063
    ; flag an event to the application
992
 
Line 1064... Line 993...
1064
    pop     eax
993
    cld
1065
    mov     ecx,1
994
    rep     movsb          ; copy the data across
1066
    mov     esi,TASK_DATA+TASKDATA.pid
995
 
1067
 
-
 
1068
news:
996
    ; flag an event to the application
1069
    cmp     [esi],eax
997
    pop     eax
Line 1070... Line 998...
1070
    je      foundPID1
998
	mov	ecx, 1
1071
    inc     ecx
999
	mov	esi, TASK_DATA + TASKDATA.pid
1072
    add     esi,0x20
1000
 
1073
    cmp     ecx,[TASK_COUNT]
1001
  .next_pid:
Line 1074... Line 1002...
1074
    jbe     news
1002
	cmp	[esi], eax
1075
 
1003
	je	.found_pid
-
 
1004
    inc     ecx
Line 1076... Line -...
1076
foundPID1:
-
 
1077
    shl     ecx,8
1005
	add	esi, 0x20
1078
    or      dword [ecx+SLOT_BASE+APPDATA.event_mask],dword 10000000b ; stack event
1006
	cmp	ecx, [TASK_COUNT]
1079
 
-
 
1080
    pop     ecx
-
 
1081
 
-
 
1082
    ; Update our recv.nxt field
-
 
1083
    mov     esi, [sktAddr]
-
 
1084
    add     esi, 56
1007
	jbe	.next_pid
1085
    call    add_inet_esi
-
 
1086
 
-
 
1087
ste_ack:
-
 
1088
    ; Send an ACK
-
 
1089
    ; Now construct the response, and queue for sending by IP
-
 
Line 1090... Line 1008...
1090
    mov     eax, EMPTY_QUEUE
1008
 
1091
    call    dequeue
1009
  .found_pid:
1092
    cmp     ax, NO_BUFFER
-
 
Line 1093... Line -...
1093
    je      ste_exit
-
 
1094
 
-
 
1095
    push    eax
-
 
1096
 
1010
	shl	ecx, 8
1097
    mov     bl, 0x10        ; ACK
1011
	or	[ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
Line 1098... Line 1012...
1098
    mov     ecx, 0
1012
 
1099
    mov     esi, 0
1013
    pop     ecx
1100
 
1014
 
1101
    call    buildTCPPacket
1015
    ; Update our recv.nxt field
1102
 
1016
	lea	esi, [ebx + SOCKET.RCV_NXT]
Line 1103... Line 1017...
1103
    mov     eax, NET1OUT_QUEUE
1017
    call    add_inet_esi
Line 1104... Line 1018...
1104
 
1018
 
1105
    mov     edx, [stack_ip]
1019
  .ack:
1106
    mov     ecx, [ sktAddr ]
1020
    ; Send an ACK
1107
    mov     ecx, [ ecx + 16 ]
-
 
1108
    cmp     edx, ecx
1021
    ; Now construct the response, and queue for sending by IP
Line 1109... Line 1022...
1109
    jne     ste_notlocal
1022
    mov     eax, EMPTY_QUEUE
Line 1110... Line 1023...
1110
    mov     eax, IPIN_QUEUE
1023
    call    dequeue
1111
ste_notlocal:
1024
    cmp     ax, NO_BUFFER
1112
 
1025
	je	.exit
1113
       ; Send it.
-
 
1114
    pop     ebx
1026
 
1115
    call    queue
1027
    push    eax
Line 1116... Line 1028...
1116
 
1028
 
1117
ste_exit:
1029
	mov	bl, TH_ACK
1118
    ret
1030
	xor	ecx, ecx
1119
 
1031
	xor	esi, esi
Line 1120... Line -...
1120
 
-
 
1121
 
-
 
1122
stateTCB_FIN_WAIT_1:
-
 
1123
    ; We can either receive an ACK of a fin, or a fin
-
 
1124
    mov     bl, [edx + 33]
-
 
1125
    and     bl, 0x10
-
 
1126
    cmp     bl, 0x10
-
 
1127
    jnz     stfw1_001
-
 
1128
 
-
 
1129
    ; It was an ACK
-
 
1130
    mov     esi, [sktAddr]
-
 
1131
    mov     ebx, TCB_FIN_WAIT_2
-
 
1132
    mov     [esi + 28], ebx
-
 
1133
    jmp     stfw1_exit
-
 
1134
 
-
 
1135
stfw1_001:
-
 
1136
    ; It must be a fin then
1032
	stdcall build_tcp_packet, [sockAddr]
1137
    mov     esi, [sktAddr]
1033
 
-
 
1034
    mov     eax, NET1OUT_QUEUE
Line 1138... Line -...
1138
    mov     ebx, TCB_CLOSING
-
 
1139
    mov     [esi + 28], ebx
1035
 
1140
    add     esi, 56
1036
    mov     edx, [stack_ip]
1141
    call    inc_inet_esi
1037
	mov	ecx, [sockAddr]
1142
 
1038
	cmp	edx, [ecx + SOCKET.RemoteIP]
-
 
1039
	jne	.not_local
Line 1143... Line -...
1143
    ; Send an ACK
-
 
1144
    mov     eax, EMPTY_QUEUE
1040
    mov     eax, IPIN_QUEUE
1145
    call    dequeue
1041
 
1146
    cmp     ax, NO_BUFFER
1042
  .not_local:
1147
    je      stfw1_exit
-
 
1148
 
-
 
1149
    push    eax
1043
       ; Send it.
1150
 
-
 
1151
    mov     bl, 0x10        ; ACK
-
 
Line 1152... Line 1044...
1152
    mov     ecx, 0
1044
    pop     ebx
Line 1153... Line -...
1153
    mov     esi, 0
-
 
1154
 
-
 
1155
    call    buildTCPPacket
-
 
1156
    mov     eax, NET1OUT_QUEUE
-
 
1157
 
-
 
1158
    mov     edx, [stack_ip]
-
 
1159
    mov     ecx, [ sktAddr ]
1045
    call    queue
1160
    mov     ecx, [ ecx + 16 ]
1046
 
-
 
1047
  .exit:
Line 1161... Line -...
1161
    cmp     edx, ecx
-
 
1162
    jne     stfw1_notlocal
1048
    ret
1163
    mov     eax, IPIN_QUEUE
1049
endp
1164
 
1050
 
1165
stfw1_notlocal:
-
 
1166
    ; Send it.
-
 
1167
    pop     ebx
1051
 
1168
    call    queue
-
 
1169
 
-
 
Line 1170... Line 1052...
1170
stfw1_exit:
1052
proc stateTCB_FIN_WAIT_1 stdcall, sockAddr:DWORD
-
 
1053
	; We can either receive an ACK of a fin, or a fin
-
 
1054
	mov	al, [edx + 20 + TCP_PACKET.Flags]
1171
    ret
1055
	and	al, TH_FIN + TH_ACK
1172
 
1056
 
1173
 
1057
	cmp	al, TH_ACK
1174
 
1058
	jne	@f
Line 1175... Line 1059...
1175
stateTCB_FIN_WAIT_2:
1059
 
1176
    mov     esi, [sktAddr]
1060
    ; It was an ACK
-
 
1061
	mov	[ebx + SOCKET.TCBState], TCB_FIN_WAIT_2
Line 1177... Line -...
1177
 
-
 
1178
    ; Get data length
1062
	jmp	.exit
1179
    xor     ecx, ecx
1063
 
-
 
1064
    @@: mov	[ebx + SOCKET.TCBState], TCB_CLOSING
Line 1180... Line -...
1180
    mov     ch, [edx+2]
-
 
1181
    mov     cl, [edx+3]
1065
	cmp	al, TH_FIN
1182
    sub     ecx, 40
1066
	je	@f
-
 
1067
	mov	[ebx + SOCKET.TCBState], TCB_TIMED_WAIT