📄 tcp.inc
字号:
; In this case, we are expecting a SYN packet
; For now, if the packet is a SYN, process it, and send a response
; If not, ignore it
; Look at control flags
mov bl, [edx + 33]
and bl, 0x02
cmp bl, 0x02
jnz stl_exit
; We have a SYN. update the socket with this IP packets details,
; And send a response
mov ebx, [edx + 12] ; IP source address
mov [eax + sockets + 16], ebx
mov bx, [edx + 20] ; IP source port
mov [eax + sockets + 20], bx
mov ebx, [edx + 24] ; IRS
mov [eax + sockets + 40], ebx
mov [eax + sockets + 56], ebx
mov esi, sockets
add esi, eax
add esi, 56
call inc_inet_esi ; RCV.NXT
mov ebx, [eax + sockets + 36] ; ISS
mov [eax + sockets + 48], ebx ; SND.NXT
; Now construct the response, and queue for sending by IP
mov eax, EMPTY_QUEUE
call dequeue
cmp ax, NO_BUFFER
je stl_exit
push eax
mov bl, 0x12 ; SYN + ACK
mov ecx, 0
mov esi, 0
call buildTCPPacket
mov eax, NET1OUT_QUEUE
mov edx, [stack_ip]
mov ecx, [ sktAddr ]
mov ecx, [ ecx + 16 ]
cmp edx, ecx
jne stl_notlocal
mov eax, IPIN_QUEUE
stl_notlocal:
; Send it.
pop ebx
call queue
mov ebx, TCB_SYN_RECEIVED
mov esi, [sktAddr]
mov [esi + 28], ebx
; increament SND.NXT in socket
add esi, 48
call inc_inet_esi
stl_exit:
ret
stateTCB_SYN_SENT:
; We are awaiting an ACK to our SYN, with a SYM
; Look at control flags - expecting an ACK
mov bl, [edx + 33]
and bl, 0x12
cmp bl, 0x12
jnz stss_exit
mov ebx, TCB_ESTABLISHED
mov esi, [sktAddr]
mov [esi + 28], ebx
; Store the recv.nxt field
mov eax, [edx + 24]
; Update our recv.nxt field
mov esi, [sktAddr]
add esi, 56
mov [esi], eax
call inc_inet_esi
; Send an ACK
; Now construct the response, and queue for sending by IP
mov eax, EMPTY_QUEUE
call dequeue
cmp ax, NO_BUFFER
je stss_exit
push eax
mov bl, 0x10 ; ACK
mov ecx, 0
mov esi, 0
call buildTCPPacket
mov eax, NET1OUT_QUEUE
mov edx, [stack_ip]
mov ecx, [ sktAddr ]
mov ecx, [ ecx + 16 ]
cmp edx, ecx
jne stss_notlocal
mov eax, IPIN_QUEUE
stss_notlocal:
; Send it.
pop ebx
call queue
stss_exit:
ret
stateTCB_SYN_RECEIVED:
; In this case, we are expecting an ACK packet
; For now, if the packet is an ACK, process it,
; If not, ignore it
; Look at control flags - expecting an ACK
mov bl, [edx + 33]
and bl, 0x10
cmp bl, 0x10
jnz stsr_exit
mov ebx, TCB_ESTABLISHED
mov esi, [sktAddr]
mov [esi + 28], ebx
stsr_exit:
ret
stateTCB_ESTABLISHED:
; Here we are expecting data, or a request to close
; OR both...
; Did we receive a FIN?
mov bl, [edx + 33]
and bl, 0x01
cmp bl, 0x01
jnz ste_chkack
; It was a fin.
; Send an ACK to that fin, and enter closewait state
mov esi, [sktAddr]
mov ebx, TCB_CLOSE_WAIT
mov [esi + 28], ebx
add esi, 56
call inc_inet_esi
;; jmp ste_ack - NO, there may be data
ste_chkack:
; Check that we received an ACK
mov bl, [edx + 33]
and bl, 0x10
cmp bl, 0x10
jnz ste_exit
; Read the data bytes, store in socket buffer
xor ecx, ecx
mov ch, [edx + 2]
mov cl, [edx + 3]
sub ecx, 40 ; Discard 40 bytes of header
cmp ecx, 0
jnz ste_data ; Read data, if any
; If we had received a fin, we need to ACK it.
mov esi, [sktAddr]
mov ebx, [esi + 28]
cmp ebx, TCB_CLOSE_WAIT
jz ste_ack
jnz ste_exit
ste_data:
push ecx
mov esi, [sktAddr]
add [esi + 24], ecx ; increment the count of bytes in buffer
mov eax, [esi + 4] ; get socket owner PID
push eax
mov eax, [esi + 24] ; get # of bytes already in buffer
; point to the location to store the data
add esi, eax
sub esi, ecx
add esi, SOCKETHEADERSIZE
add edx, 40 ; edx now points to the data
mov edi, esi
mov esi, edx
cld
rep movsb ; copy the data across
; flag an event to the application
pop eax
mov ecx,1
mov esi,0x3020+0x4
news:
cmp [esi],eax
je foundPID1
inc ecx
add esi,0x20
cmp ecx,[0x3004]
jbe news
foundPID1:
shl ecx,8
or dword [ecx+0x80000+0xA8],dword 10000000b ; stack event
pop ecx
; Update our recv.nxt field
mov esi, [sktAddr]
add esi, 56
call add_inet_esi
ste_ack:
; Send an ACK
; Now construct the response, and queue for sending by IP
mov eax, EMPTY_QUEUE
call dequeue
cmp ax, NO_BUFFER
je ste_exit
push eax
mov bl, 0x10 ; ACK
mov ecx, 0
mov esi, 0
call buildTCPPacket
mov eax, NET1OUT_QUEUE
mov edx, [stack_ip]
mov ecx, [ sktAddr ]
mov ecx, [ ecx + 16 ]
cmp edx, ecx
jne ste_notlocal
mov eax, IPIN_QUEUE
ste_notlocal:
; Send it.
pop ebx
call queue
ste_exit:
ret
stateTCB_FIN_WAIT_1:
; We can either receive an ACK of a fin, or a fin
mov bl, [edx + 33]
and bl, 0x10
cmp bl, 0x10
jnz stfw1_001
; It was an ACK
mov esi, [sktAddr]
mov ebx, TCB_FIN_WAIT_2
mov [esi + 28], ebx
jmp stfw1_exit
stfw1_001:
; It must be a fin then
mov esi, [sktAddr]
mov ebx, TCB_CLOSING
mov [esi + 28], ebx
add esi, 56
call inc_inet_esi
; Send an ACK
mov eax, EMPTY_QUEUE
call dequeue
cmp ax, NO_BUFFER
je stfw1_exit
push eax
mov bl, 0x10 ; ACK
mov ecx, 0
mov esi, 0
call buildTCPPacket
mov eax, NET1OUT_QUEUE
mov edx, [stack_ip]
mov ecx, [ sktAddr ]
mov ecx, [ ecx + 16 ]
cmp edx, ecx
jne stfw1_notlocal
mov eax, IPIN_QUEUE
stfw1_notlocal:
; Send it.
pop ebx
call queue
stfw1_exit:
ret
stateTCB_FIN_WAIT_2:
; should be a fin
mov bl, [edx + 33]
and bl, 0x01
cmp bl, 0x01
jnz stfw2_exit
; It was a fin
mov esi, [sktAddr]
mov ebx, TCB_TIME_WAIT
mov [esi + 28], ebx
add esi, 56
call inc_inet_esi
; Send an ACK
mov eax, EMPTY_QUEUE
call dequeue
cmp ax, NO_BUFFER
je stfw2_exit
push eax
mov bl, 0x10 ; ACK
mov ecx, 0
mov esi, 0
call buildTCPPacket
mov eax, NET1OUT_QUEUE
mov edx, [stack_ip]
mov ecx, [ sktAddr ]
mov ecx, [ ecx + 16 ]
cmp edx, ecx
jne stfw2_notlocal
mov eax, IPIN_QUEUE
stfw2_notlocal:
; Send it.
pop ebx
call queue
mov edi, [sktAddr]
; delete the socket
xor eax, eax
mov ecx,SOCKETHEADERSIZE
cld
rep stosb
; Now delete the socket
stfw2_exit:
ret
stateTCB_CLOSE_WAIT:
; Intentionally left empty
; socket_close_tcp handles this
ret
stateTCB_CLOSING:
; We can either receive an ACK of a fin, or a fin
mov bl, [edx + 33]
and bl, 0x10
cmp bl, 0x10
jnz stc_exit
; It was an ACK
mov edi, [sktAddr]
; delete the socket
xor eax, eax
mov ecx,SOCKETHEADERSIZE
cld
rep stosb
stc_exit:
ret
stateTCB_LAST_ACK:
; Look at control flags - expecting an ACK
mov bl, [edx + 33]
and bl, 0x10
cmp bl, 0x10
jnz stla_exit
mov edi, [sktAddr]
; delete the socket
xor eax, eax
mov ecx,SOCKETHEADERSIZE
cld
rep stosb
stla_exit:
ret
stateTCB_TIME_WAIT:
ret
stateTCB_CLOSED:
ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -