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

📄 stack.inc

📁 MenuetOS是一个用汇编开发的32/64位PC操作系统
💻 INC
📖 第 1 页 / 共 4 页
字号:
    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

    ; If the sockets window timer is nonzero, do not queue packet
    ; TODO - done
    cmp     [ebx + 72], dword 0
    jne     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 

    push    ebx                 ; save ipbuffer number
    
    call    queue

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

    pop     ebx
       
    ; Copy the IP buffer to a resend queue
    ; If there isn't one, dont worry about it for now
    mov     esi, resendQ
    mov     ecx, 0
    
swt003:
    cmp     ecx, NUMRESENDENTRIES
    je      swt001              ; None found
    cmp     [esi], byte 0xFF
    je      swt002              ; found one
    inc     ecx
    add     esi, 4
    jmp     swt003
    
swt002:
    push    ebx
    
    ; OK, we have a buffer descriptor ptr in esi. 
    ; resend entry # in ecx
    ;  Populate it
    ;  socket #
    ;  retries count
    ;  retry time
    ;  fill IP buffer associated with this descriptor    

    mov     eax, [sktAddr]
    sub     eax, sockets
    shr     eax, 12             ; get skt #
    mov     [esi], al
    mov     [esi + 1], byte TCP_RETRIES
    mov     [esi + 2], word TCP_TIMEOUT
    
    inc     ecx
    ; Now get buffer location, and copy buffer across. argh! more copying,,
    mov     edi, resendBuffer - IPBUFFSIZE
swt002a:
    add     edi, IPBUFFSIZE
    loop    swt002a
    
    ; we have dest buffer location in edi
    pop     eax
    ; convert source buffer pointer eax to the absolute address
    mov     ecx, IPBUFFSIZE
    mul     ecx
    add     eax, IPbuffs
    mov     esi, eax
    
    ; do copy
    mov     ecx, IPBUFFSIZE
    cld
    rep     movsb
    
    inc     dword [arp_rx_count] ; ************ TEST ONLY!
    
swt001:    
    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"   
include "ETHERNET.INC"

⌨️ 快捷键说明

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