📄 ftps.asm
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; FTPS
; FTP Server
;
; Compile with FASM for Menuet
;
; note: telnet == 23, ftp cmd == 21, data on 20
use32
org 0x0
db 'MENUET00' ; 8 byte id
dd 38 ; required os
dd START ; program start
dd I_END ; program image size
dd 0x170000 ; required amount of memory
; esp = 0x7FFF0
dd 0x00000000 ; reserved=no extended header
; Various states of client connection
USER_NONE equ 0 ; Awaiting a connection
USER_CONNECTED equ 1 ; User just connected, prompt given
USER_USERNAME equ 2 ; User given username
USER_LOGGED_IN equ 3 ; User given password
START: ; start of execution
; Clear the screen memory
mov eax, ' '
mov edi,text
mov ecx,80*30 /4
cld
rep stosd
call draw_window
; init the receive buffer pointer
mov eax, buff
mov [buffptr], eax
; Init FTP server state machine
mov al, USER_NONE
mov [state], al
; Open the listening socket
call connect
still:
; check connection status
mov eax,53
mov ebx,6 ; Get socket status
mov ecx,[CmdSocket]
int 0x40
mov ebx, [CmdSocketStatus]
mov [CmdSocketStatus], eax
cmp eax, ebx
je waitev
; If the socket closed by remote host, open it again.
cmp eax, 7
je con
; If socket closed by Reset, open it again
cmp eax, 11
je con
; If a user has just connected, start by outputting welcome msg
cmp eax, 4
jne noc
mov esi, loginStr0
mov edx, loginStr0_end - loginStr0
call outputStr
mov al, USER_CONNECTED
mov [state], al
jmp noc
con:
; Need to call disconnect, since a remote close does not fully
; close the socket
call disconnect
call connect
jmp noc
noc:
; Display the changed connected status
call draw_window
waitev:
mov eax,23 ; wait here for event
mov ebx,1 ; Delay for up to 1s
int 0x40
cmp eax,1 ; redraw request ?
je red
cmp eax,2 ; key in buffer ?
je key
cmp eax,3 ; button in buffer ?
je button
; any data from the socket?
mov eax, 53
mov ebx, 2 ; Get # of bytes in input queue
mov ecx, [CmdSocket]
int 0x40
cmp eax, 0
jne read_input
jmp still
read_input:
mov eax, 53
mov ebx, 3 ; Get a byte from socket in bl
mov ecx, [CmdSocket]
int 0x40
call ftpRxCmdData ; process incoming ftp command
; Keep processing data until there is no more to process
mov eax, 53
mov ebx, 2 ; Get # of bytes in input queue
mov ecx, [CmdSocket]
int 0x40
cmp eax, 0
jne read_input
; Now redraw the text text field.
; Probably not required, since ftp requires no
; console i/o.
; Leave in for now, for debugging.
call draw_text
jmp still
red: ; REDRAW WINDOW
call draw_window
jmp still
key: ; KEY
mov eax,2 ; get but ignore
int 0x40
jmp still
button:
mov eax,17
int 0x40
cmp ah,1
jne still
; Exit button pressed, so close socket and quit
mov eax,53
mov ebx,8
mov ecx,[CmdSocket]
int 0x40
; ... terminate program
mov eax,-1
int 0x40
jmp still
; *********************************************
; ******* WINDOW DEFINITIONS AND DRAW ********
; *********************************************
draw_window:
pusha
mov eax,12
mov ebx,1
int 0x40
mov eax,0 ; DRAW WINDOW
mov ebx,100*65536+491 + 8 +15
mov ecx,100*65536+270 + 20 ; 20 for status bar
mov edx,[wcolor]
add edx,0x04000000
mov esi,window_label
mov edi,0
int 0x40
; draw status bar
mov eax, 13
mov ebx, 4*65536+484 + 8 +15
mov ecx, 271*65536 + 1
mov edx, 0x335577
int 0x40
mov esi,contlen-contt ; display connected status
mov edx, contt
mov eax, [CmdSocketStatus]
cmp eax, 4 ; 4 is connected
je pcon
mov esi,discontlen-discontt
mov edx, discontt
pcon:
mov eax,4 ; status text
mov ebx,380*65536+276
mov ecx,0x00ffffff
int 0x40
; Draw the text on the screen, clearing it first
; This can go when we loose debuggin info.
xor eax,eax
mov edi,text+80*30
mov ecx,80*30 /4
cld
rep stosd
call draw_text
mov eax,12
mov ebx,2
int 0x40
popa
ret
;***************************************************************************
; Function
; draw_text
;
; Description
; Updates the text on the screen. This is part of the debugging code
;
; Inputs
; Character to add in bl
;
;***************************************************************************
draw_text:
pusha
mov esi,text
mov eax,0
mov ebx,0
newletter:
mov cl,[esi]
cmp cl,[esi+30*80]
jne yesletter
jmp noletter
yesletter:
mov [esi+30*80],cl
; erase character
pusha
mov edx, 0 ; bg colour
mov ecx, ebx
add ecx, 26
shl ecx, 16
mov cx, 9
mov ebx, eax
add ebx, 6
shl ebx, 16
mov bx, 6
mov eax, 13
int 0x40
popa
; draw character
pusha
mov ecx, 0x00ffffff
push bx
mov ebx,eax
add ebx,6
shl ebx,16
pop bx
add bx,26
mov eax,4
mov edx,esi
mov esi,1
int 0x40
popa
noletter:
add esi,1
add eax,6
cmp eax,80*6
jb newletter
mov eax,0
add ebx,10
cmp ebx,24*10
jb newletter
popa
ret
;***************************************************************************
; Function
; ftpRxCmdData
;
; Description
; Prcoesses incoming command data, calling a handler for each command.
; Commands are built up in buff before being processed.
;
; Inputs
; Character to add in bl
;
;***************************************************************************
ftpRxCmdData:
; Quit if we are not connected
;( This case shouldn't be necessary, but be safe )
mov al, [state]
cmp al, USER_NONE
je frcd_exit
; Store the incoming character
mov esi, [buffptr]
mov [esi], bl
inc esi
mov [buffptr], esi
; For debugging, show the data coming in
pusha
call printChar
popa
; Do we have an end of line? (LF)
; if not, just exit
cmp bl, 0x0a
jne frcd_exit
; OK we have a complete command.
; Process, and send response
; There are a number of states involved in ftp,
; to do with logging in.
mov al, [state]
cmp al, USER_CONNECTED
jne fs001
; This should be the username
; TODO validate username
; OK, username accepted - ask for password
mov esi, loginStr1
mov edx, loginStr1_end - loginStr1
call outputStr
mov al, USER_USERNAME
mov [state], al
; init the receive buffer pointer
mov eax, buff
mov [buffptr], eax
jmp frcd_exit
fs001:
cmp al, USER_USERNAME
jne fs002
; This should be the password
; TODO validate password
; OK, password accepted - show they are logged in
mov esi, loginStr2
mov edx, loginStr2_end - loginStr2
call outputStr
mov al, USER_LOGGED_IN
mov [state], al
; init the receive buffer pointer
mov eax, buff
mov [buffptr], eax
jmp frcd_exit
fs002:
cmp al, USER_LOGGED_IN
jne fs003
; This should be a cmd
call findCmd
mov eax, [cmdPtr]
cmp eax, 0
je fs002b
call [cmdPtr]
fs002a:
; init the receive buffer pointer
mov eax, buff
mov [buffptr], eax
jmp frcd_exit
fs002b:
; an unsupported command was entered.
; Tell user that the command is not supported
mov esi, unsupStr
mov edx, unsupStr_end - unsupStr
call outputStr
jmp fs002a
fs003:
frcd_exit:
ret
;***************************************************************************
; Function
; outputStr
;
; Description
; Sends a string over the 'Command' socket
;
; Inputs
; String in esi
; Length in edx
;
;***************************************************************************
outputStr:
push esi
push edx
mov eax,53
mov ebx,7
mov ecx,[CmdSocket]
int 0x40
pop edx
pop esi
cmp eax, 0
je os_exit
; The TCP/IP transmit queue is full; Wait a bit, then retry
pusha
mov eax,5
mov ebx,1 ; Delay for up 100ms
int 0x40
popa
jmp outputStr
os_exit:
ret
;***************************************************************************
; Function
; outputDataStr
;
; Description
; Sends a string over the 'Data' socket
;
; Inputs
; String in esi
; Length in edx
;
;***************************************************************************
outputDataStr:
push esi
push edx
mov eax,53
mov ebx,7
mov ecx,[DataSocket]
int 0x40
pop edx
pop esi
cmp eax, 0
je ods_exit
; The TCP/IP transmit queue is full; Wait a bit, then retry
pusha
mov eax,5
mov ebx,2 ; Delay for upto 20ms
int 0x40
popa
jmp outputDataStr
ods_exit:
ret
;***************************************************************************
; Function
; printChar
;
; Description
; Writes a character to the screen; Used to display the data coming
; in from the user. Really only useful for debugging.
;
; Inputs
; Character in bl
;
;***************************************************************************
printChar:
cmp bl,13 ; BEGINNING OF LINE
jne nobol
mov ecx,[pos]
add ecx,1
boll1:
sub ecx,1
mov eax,ecx
xor edx,edx
mov ebx,80
div ebx
cmp edx,0
jne boll1
mov [pos],ecx
jmp newdata
nobol:
cmp bl,10 ; LINE DOWN
jne nolf
addx1:
add [pos],dword 1
mov eax,[pos]
xor edx,edx
mov ecx,80
div ecx
cmp edx,0
jnz addx1
mov eax,[pos]
jmp cm1
nolf:
cmp bl,8 ; BACKSPACE
jne nobasp
mov eax,[pos]
dec eax
mov [pos],eax
mov [eax+text],byte 32
mov [eax+text+60*80],byte 0
jmp newdata
nobasp:
cmp bl,15 ; CHARACTER
jbe newdata
putcha:
mov eax,[pos]
mov [eax+text],bl
mov eax,[pos]
add eax,1
cm1:
mov ebx,[scroll+4]
imul ebx,80
cmp eax,ebx
jb noeaxz
mov esi,text+80
mov edi,text
mov ecx,ebx
cld
rep movsb
mov eax,ebx
sub eax,80
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -