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

📄 stack.inc

📁 Menuet 操作系统源代码。 非常难得的东西
💻 INC
📖 第 1 页 / 共 3 页
字号:
    mov     eax, IPIN_QUEUE

sot_notlocal:
       ; Send it.
    pop     ebx
    call    queue

    mov     esi, [sktAddr]
    
    ; increment SND.NXT in socket
    add     esi, 48
    call    inc_inet_esi
     
sot_done:
    pop     eax      ; Get the socket number back, so we can return it
   
sot_exit:
    ret

   
   
;***************************************************************************
;   Function
;      socket_close
;
;   Description
;       socket # in ebx
;       returns 0 for ok, -1 for socket not open (fail)
;
;***************************************************************************
socket_close:
    shl     ebx, 12
    add     ebx, sockets
    mov     eax, 0xFFFFFFFF         ; assume this operation will fail..
    cmp     [ebx], dword SOCK_EMPTY
    jz      sc_exit
   
    ; Clear the socket varaibles
    xor     eax, eax
    mov     edi,ebx
    mov     ecx,SOCKETHEADERSIZE
    cld
    rep     stosb
   
sc_exit:
    ret
    
   

;***************************************************************************
;   Function
;      socket_close_tcp
;
;   Description
;       socket # in ebx
;       returns 0 for ok, -1 for socket not open (fail)
;
;***************************************************************************
socket_close_tcp:
    shl     ebx, 12
    add     ebx, sockets
    mov     [sktAddr], ebx
    mov     eax, 0xFFFFFFFF         ; assume this operation will fail..
    cmp     [ebx], dword SOCK_EMPTY
    jz      sct_exit

    ; 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, 0x11        ; FIN + ACK
    mov     ecx, 0
    mov     esi, 0
    
    call    buildTCPPacket
    
    mov     ebx, [sktAddr]   

    ; increament SND.NXT in socket
    mov     esi, 48
    add     esi, ebx
    call    inc_inet_esi


    ; Get the socket state
    mov     eax, [ebx + 28]
    cmp     eax, TCB_LISTEN
    je      destroyTCB
    cmp     eax, TCB_SYN_SENT
    je      destroyTCB
    cmp     eax, TCB_SYN_RECEIVED
    je      sct_finwait1
    cmp     eax, TCB_ESTABLISHED
    je      sct_finwait1
    
    ; assume CLOSE WAIT
    ; Send a fin, then enter last-ack state
    mov     eax, TCB_LAST_ACK
    mov     [ebx + 28], eax
    xor     eax, eax
    jmp     sct_send
       
sct_finwait1:
    ; Send a fin, then enter finwait2 state
    mov     eax, TCB_FIN_WAIT_1
    mov     [ebx + 28], eax
    xor     eax, eax

sct_send:
    mov     eax, NET1OUT_QUEUE

    mov     edx, [stack_ip]
    mov     ecx, [ sktAddr ]
    mov     ecx, [ ecx + 16 ]
    cmp     edx, ecx
    jne     sct_notlocal
    mov     eax, IPIN_QUEUE

sct_notlocal:
       ; Send it.
    pop     ebx
    call    queue
    jmp     sct_exit
      
destroyTCB:
    pop     eax
    ; Clear the socket varaibles
    xor     eax, eax
    mov     edi,ebx
    mov     ecx,SOCKETHEADERSIZE
    cld
    rep     stosb
   
sct_exit:
    ret


   
;***************************************************************************
;   Function
;      socket_poll
;
;   Description
;       socket # in ebx
;       returns count in eax.
;
;***************************************************************************
socket_poll:
    shl     ebx, 12
    add     ebx, sockets
    mov     eax, [ebx + 24]
    
    ret
   
   
   
;***************************************************************************
;   Function
;      socket_status
;
;   Description
;       socket # in ebx
;       returns TCB state in eax.
;
;***************************************************************************
socket_status:
    shl     ebx, 12
    add     ebx, sockets
    mov     eax, [ebx + 28]
    
    ret
   
   
   
;***************************************************************************
;   Function
;      socket_read
;
;   Description
;       socket # in ebx
;       returns # of bytes remaining in eax, data in bl
;
;***************************************************************************
socket_read:
    shl     ebx, 12
    add     ebx, sockets
    mov     eax, [ebx + 24]         ; get count of bytes
    mov     ecx,1
    test    eax, eax
    jz      sr2
   
    dec     eax
    mov     esi, ebx            ; esi is address of socket
    mov     [ebx + 24], eax         ; store new count
    movzx   ebx, byte [ebx + SOCKETHEADERSIZE]  ; get the byte
    add     esi, SOCKETHEADERSIZE
    mov     edi, esi
    inc     esi
   
    mov     ecx, (SOCKETBUFFSIZE - SOCKETHEADERSIZE) / 4
    cld
    rep     movsd
    xor     ecx, ecx
    
sr1:
    jmp     sor_exit
   
sr2:
    xor     bl, bl
   
sor_exit:
    ret
   
   
   
;***************************************************************************
;   Function
;      socket_write
;
;   Description
;       socket in ebx
;       # of bytes to write in ecx
;       pointer to data in edx
;       returns 0 in eax ok, -1 == failed ( invalid socket, or
;       could not queue IP packet )
;
;***************************************************************************
socket_write:
    ; First, find the address of the socket descriptor
    shl     ebx, 12
    add     ebx, sockets         ; ebx = address of actual socket
   
    mov     eax, 0xFFFFFFFF
    ; If the socket is invalid, return with an error code
    cmp     [ebx], dword SOCK_EMPTY
    je      sw_exit


    mov     eax, EMPTY_QUEUE
    call    dequeue
    cmp     ax, NO_BUFFER
    je      sw_exit
   
    ; Save the queue entry number
    push    eax
   
    ; save the pointers to the data buffer & size
    push    edx
    push    ecx

    ; convert buffer pointer eax to the absolute address
    mov     ecx, IPBUFFSIZE
    mul     ecx
    add     eax, IPbuffs

    mov     edx, eax
   
    ; So, ebx holds the socket ptr, edx holds the IPbuffer ptr
   
    ; Fill in the IP header ( some data is in the socket descriptor)
    mov     eax, [ebx + 8]
    mov     [edx + 12], eax      ; source IP
    mov     eax, [ebx + 16]
    mov     [edx + 16], eax      ; Destination IP
   
    mov     al, 0x45
    mov     [edx], al         ; Version, IHL
    xor     al, al   
    mov     [edx + 1], al     ; Type of service
   
    pop     eax                   ; Get the UDP data length
    push    eax
   
    add     eax, 20 + 8           ; add IP header and UDP header lengths
    mov     [edx + 2], ah
    mov     [edx + 3], al
    xor     al, al   
    mov     [edx + 4], al
    mov     [edx + 5], al
    mov     al, 0x40
    mov     [edx + 6], al
    xor     al, al  
    mov     [edx + 7], al
    mov     al, 0x20
    mov     [edx + 8], al
    mov     al, 17
    mov     [edx + 9], al
   
    ; Checksum left unfilled
    xor     ax, ax
    mov     [edx + 10], ax
   
    ; Fill in the UDP header ( some data is in the socket descriptor)
    mov     ax, [ebx + 12]
    mov     [edx + 20], ax
   
    mov     ax, [ebx + 20]
    mov     [edx + 20 + 2], ax
   
    pop     eax
    push    eax
   
    add     eax, 8
    mov     [edx + 20 + 4], ah
    mov     [edx + 20 + 5], al
   
    ; Checksum left unfilled
    xor     ax, ax
    mov     [edx + 20 + 6], ax
   
    pop     ecx                  ; count of bytes to send
    mov     ebx, ecx            ; need the length later
    pop     eax                  ; get callers ptr to data to send
   
    ; Get the address of the callers data
    mov     edi,[0x3010]
    add     edi,0x10
    add     eax,[edi]
    mov     esi, eax
   
    mov     edi, edx
    add     edi, 28
    cld
    rep     movsb               ; copy the data across
   
    ; we have edx as IPbuffer ptr.
    ; Fill in the UDP checksum
    ; First, fill in pseudoheader
    mov     eax, [edx + 12]
    mov     [pseudoHeader], eax
    mov     eax, [edx + 16]
    mov     [pseudoHeader+4], eax
    mov     ax, 0x1100            ; 0 + protocol
    mov     [pseudoHeader+8], ax
    add     ebx, 8
    mov     eax, ebx
    mov     [pseudoHeader+10], ah
    mov     [pseudoHeader+11], al
      
    mov     eax, pseudoHeader
    mov     [checkAdd1], eax
    mov     [checkSize1], word 12
    mov     eax, edx
    add     eax, 20
    mov     [checkAdd2], eax
    mov     eax, ebx
    mov     [checkSize2], ax      ; was eax!! mjh 8/7/02
   
    call    checksum
   
    ; store it in the UDP checksum ( in the correct order! )
    mov     ax, [checkResult]

    ; If the UDP checksum computes to 0, we must make it 0xffff
    ; (0 is reserved for 'not used')
    cmp     ax, 0
    jne     sw_001
    mov     ax, 0xffff
   
sw_001:
    mov     [edx + 20 + 6], ah
    mov     [edx + 20 + 7], al
   
    ; Fill in the IP header checksum
    mov     eax, edx
    mov     [checkAdd1], eax
    mov     [checkSize1], word 20
    mov     [checkAdd2], dword 0
    mov     [checkSize2], word 0
   
    call    checksum
   
    mov     ax, [checkResult]
    mov     [edx + 10], ah
    mov     [edx + 11], al
   
    ; Check destination IP address.
    ; If it is the local host IP, route it back to IP_RX
   
    pop     ebx   
    mov     eax, NET1OUT_QUEUE

    mov     ecx, [ edx + 16]
    mov     edx, [stack_ip]
    cmp     edx, ecx
    jne     sw_notlocal
    mov     eax, IPIN_QUEUE

sw_notlocal:
    ; Send it.
    call    queue
    
    xor     eax, eax

sw_exit:
    ret
   


;***************************************************************************
;   Function
;      socket_write_tcp
;
;   Description
;       socket in ebx
;       # of bytes to write in ecx
;       pointer to data in edx
;       returns 0 in eax ok, -1 == failed ( invalid socket, or
;       could not queue IP packet )
;
;***************************************************************************
socket_write_tcp:
    ; First, find the address of the socket descriptor
    shl     ebx, 12
    add     ebx, sockets         ; ebx = address of actual socket

    mov     [sktAddr], ebx
   
    mov     eax, 0xFFFFFFFF
    ; If the socket is invalid, return with an error code
    cmp     [ebx], dword SOCK_EMPTY
    je      swt_exit


    mov     eax, EMPTY_QUEUE
    call    dequeue
    cmp     ax, NO_BUFFER
    je      swt_exit
   
    push    eax
    
    mov     bl, 0x10        ; ACK

    ; Get the address of the callers data
    mov     edi,[0x3010]
    add     edi,0x10
    add     edx,[edi]
    mov     esi, edx
    
    pop     eax
    push    eax
    
    push    ecx
    call    buildTCPPacket
    pop     ecx
    
    ; Check destination IP address.
    ; If it is the local host IP, route it back to IP_RX
    
    pop     ebx   
    push    ecx
    mov     eax, NET1OUT_QUEUE

    mov     edx, [stack_ip]
    mov     ecx, [ sktAddr ]
    mov     ecx, [ ecx + 16 ]
    cmp     edx, ecx
    jne     swt_notlocal
    mov     eax, IPIN_QUEUE
    
swt_notlocal:
    pop     ecx
       ; Send it.
    call    queue

    mov     esi, [sktAddr]
    
    ; increament SND.NXT in socket
    ; Amount to increment by is in ecx
    add     esi, 48
    call    add_inet_esi
   
    xor     eax, eax
    
swt_exit:
    ret


   
; Below, the main network layer source code is included
; 
   
include "QUEUE.INC"
include "IP.INC"
include "TCP.INC"   
include "UDP.INC"   
if SLIP_PRESENT = TRUE   
include "SLIP.INC"
end if
include "ETHERNET.INC"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -