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