⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.inc

📁 MenuetOS是一个用汇编开发的32/64位PC操作系统
💻 INC
📖 第 1 页 / 共 3 页
字号:
    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 or RST?
    mov     bl, [edx + 33]
    and     bl, 0x05
    cmp     bl, 0
    je      ste_chkack
    
    ; It was a fin or reset.
    
    ; Remove resend entries from the queue  - I dont want to send any more data
    pusha

    mov     ebx, [sktAddr]
    sub     ebx, sockets
    shr     ebx, 12             ; get skt #

    mov     esi, resendQ
    mov     ecx, 0
    
ste001:
    cmp     ecx, NUMRESENDENTRIES
    je      ste003              ; None left
    cmp     [esi], bl
    je      ste002              ; found one
    inc     ecx
    add     esi, 4
    jmp     ste001
    
ste002:
    dec     dword [arp_rx_count] ; ************ TEST ONLY!
    
    mov     [esi], byte 0xFF
    jmp     ste001

ste003:
    popa
    
    ; was it a reset?
    mov     bl, [edx + 33]
    and     bl, 0x04
    cmp     bl, 0x04
    jne     ste003a
    
    mov     esi, [sktAddr]
    mov     ebx, TCB_CLOSED
    mov     [esi + 28], ebx
    jmp     ste_exit
    
ste003a:
    ; 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
    mov     eax, [esi]              ; save original
    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


    ; TODO - done, I think!
    ; First, look at the incoming window. If this is less than or equal to 1024,
    ; Set the socket window timer to 1. This will stop an additional packets being 
    ; queued.
    ; ** I may need to tweak this value, since I do not know how many packets are already queued
    mov     ch, [edx + 34]
    mov     cl, [edx + 35]
    cmp     cx, 1024
    ja      ste004
    
    mov     ecx, [sktAddr]
    mov     [ecx+72], dword 1
    
ste004:
    
    ; OK, here is the deal
    ; My recv.nct field holds the seq of the expected next rec byte
    ; if the recevied sequence number is not equal to this, do not 
    ; increment the recv.nxt field, do not copy data - just send a 
    ; repeat ack.
    
    ; recv.nxt is in dword [edx+24], in inext format
    ; recv seq is in [sktAddr]+56, in inet format
    ; just do a comparision
    mov     ecx, [sktAddr]
    add     ecx, 56
    
    cmp     [ecx - 56 + 28], dword TCB_CLOSE_WAIT
    mov     ecx, [ecx]
    jne     stenofin
    mov     ecx, eax
    
stenofin:   
    cmp     ecx, [edx+24]
    jne     ste_ack

    
    ; 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:        
    mov     esi, [sktAddr]

    ; Get data length
    xor     ecx, ecx
    mov     ch, [edx+2]
    mov     cl, [edx+3]
    sub     ecx, 40
    
    mov     bl, [edx + 33]
    and     bl, 0x01
    cmp     bl, 0x01
    jne     stfw2001

    ; Change state, as we have a fin
    mov     ebx, TCB_TIME_WAIT
    mov     [esi + 28], ebx
    
    inc     ecx                     ; FIN is part of the sequence space
    
stfw2001:
    add     esi, 56
    call    add_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

    ; Only delete the socket if we received the FIN
    
    mov     bl, [edx + 33]
    and     bl, 0x01
    cmp     bl, 0x01
    jne     stfw2_exit

;    mov     edi, [sktAddr]

    ; delete the socket. Should really wait for 2MSL
;    xor     eax, eax
;    mov     ecx,SOCKETHEADERSIZE
;    cld
;    rep     stosb

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 + -