Subversion Repositories Kolibri OS

Rev

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

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