2013-06-24 23:33:16 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright (C) KolibriOS team 2013. All rights reserved. ;;
|
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; ftpc.asm - FTP client for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
format binary as ""
|
|
|
|
|
|
|
|
BUFFERSIZE = 1024
|
|
|
|
|
|
|
|
STATUS_CONNECTING = 0
|
|
|
|
STATUS_CONNECTED = 1
|
|
|
|
STATUS_NEEDPASSWORD = 2
|
|
|
|
STATUS_LOGGED_IN = 3
|
|
|
|
|
|
|
|
use32
|
|
|
|
; standard header
|
|
|
|
db 'MENUET01' ; signature
|
|
|
|
dd 1 ; header version
|
|
|
|
dd start ; entry point
|
|
|
|
dd i_end ; initialized size
|
|
|
|
dd mem+0x1000 ; required memory
|
|
|
|
dd mem+0x1000 ; stack pointer
|
|
|
|
dd s ; parameters
|
|
|
|
dd 0 ; path
|
|
|
|
|
|
|
|
include '../../macros.inc'
|
|
|
|
purge mov,add,sub
|
|
|
|
include '../../proc32.inc'
|
|
|
|
include '../../dll.inc'
|
|
|
|
include '../../network.inc'
|
|
|
|
|
|
|
|
include 'usercommands.inc'
|
|
|
|
include 'servercommands.inc'
|
|
|
|
|
|
|
|
; entry point
|
|
|
|
start:
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
mcall 40, 0
|
2013-06-24 23:33:16 +04:00
|
|
|
; load libraries
|
|
|
|
stdcall dll.Load, @IMPORT
|
|
|
|
test eax, eax
|
|
|
|
jnz exit
|
|
|
|
; initialize console
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_start, 1
|
2013-07-10 01:14:47 +04:00
|
|
|
invoke con_init, 80, 25, 80, 250, title
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
; Check for parameters
|
|
|
|
cmp byte [s], 0
|
|
|
|
jne resolve
|
|
|
|
|
|
|
|
main:
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_cls
|
2013-06-24 23:33:16 +04:00
|
|
|
; Welcome user
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str1
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
; write prompt
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_set_flags, 0x0a
|
|
|
|
invoke con_write_asciiz, str2
|
2013-06-24 23:33:16 +04:00
|
|
|
; read string
|
|
|
|
mov esi, s
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_gets, esi, 256
|
|
|
|
invoke con_write_asciiz, str4 ; newline
|
|
|
|
invoke con_set_flags, 0x07
|
2013-06-24 23:33:16 +04:00
|
|
|
; check for exit
|
|
|
|
test eax, eax
|
|
|
|
jz done
|
|
|
|
cmp byte [esi], 10
|
|
|
|
jz done
|
|
|
|
|
|
|
|
resolve:
|
|
|
|
; delete terminating '\n'
|
|
|
|
mov esi, s
|
|
|
|
@@:
|
|
|
|
lodsb
|
|
|
|
cmp al, 0x20
|
|
|
|
ja @r
|
|
|
|
mov byte [esi-1], 0
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str3
|
|
|
|
invoke con_write_asciiz, s
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
; resolve name
|
|
|
|
push esp ; reserve stack place
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke getaddrinfo, s, 0, 0, esp
|
2013-06-24 23:33:16 +04:00
|
|
|
pop esi
|
|
|
|
; test for error
|
|
|
|
test eax, eax
|
|
|
|
jnz fail
|
|
|
|
|
|
|
|
; write results
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str8
|
2013-06-24 23:33:16 +04:00
|
|
|
; mov edi, esi
|
|
|
|
|
|
|
|
; convert IP address to decimal notation
|
|
|
|
mov eax, [esi+addrinfo.ai_addr]
|
|
|
|
mov eax, [eax+sockaddr_in.sin_addr]
|
|
|
|
mov [sockaddr1.ip], eax
|
2013-07-10 00:19:08 +04:00
|
|
|
|
|
|
|
invoke inet_ntoa, eax
|
2013-06-24 23:33:16 +04:00
|
|
|
; write result
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, eax
|
2013-06-24 23:33:16 +04:00
|
|
|
; free allocated memory
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke freeaddrinfo, esi
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str9
|
2013-06-24 23:33:16 +04:00
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
|
|
|
cmp eax, -1
|
2013-07-10 00:19:08 +04:00
|
|
|
je socket_error
|
2013-06-24 23:33:16 +04:00
|
|
|
mov [socketnum], eax
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str11
|
2013-06-24 23:33:16 +04:00
|
|
|
mcall connect, [socketnum], sockaddr1, 18
|
|
|
|
mov [status], STATUS_CONNECTING
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str12
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:55:18 +04:00
|
|
|
mov [offset], 0
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
wait_for_servercommand:
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:55:18 +04:00
|
|
|
cmp [offset], 0
|
|
|
|
je .receive
|
|
|
|
mov esi, [offset]
|
|
|
|
mov edi, s
|
|
|
|
mov ecx, [size]
|
|
|
|
add ecx, esi
|
|
|
|
jmp .byteloop
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
; receive socket data
|
2013-07-10 00:55:18 +04:00
|
|
|
.receive:
|
2013-07-10 00:19:08 +04:00
|
|
|
mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
|
2013-06-24 23:33:16 +04:00
|
|
|
inc eax
|
2013-07-10 00:19:08 +04:00
|
|
|
jz socket_error
|
2013-06-24 23:33:16 +04:00
|
|
|
dec eax
|
2013-07-10 00:19:08 +04:00
|
|
|
jz wait_for_servercommand
|
|
|
|
|
2013-07-10 00:55:18 +04:00
|
|
|
mov [offset], 0
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
; extract commands, copy them to "s" buffer
|
2013-07-10 00:19:08 +04:00
|
|
|
lea ecx, [eax + buffer_ptr] ; ecx = end pointer
|
2013-06-24 23:33:16 +04:00
|
|
|
mov esi, buffer_ptr ; esi = current pointer
|
|
|
|
mov edi, s
|
|
|
|
.byteloop:
|
2013-07-10 00:19:08 +04:00
|
|
|
cmp esi, ecx
|
|
|
|
jae wait_for_servercommand
|
2013-06-24 23:33:16 +04:00
|
|
|
lodsb
|
|
|
|
cmp al, 10 ; excellent, we might have a command
|
|
|
|
je .got_command
|
2013-07-10 00:19:08 +04:00
|
|
|
cmp al, 13 ; just ignore this byte
|
2013-06-24 23:33:16 +04:00
|
|
|
je .byteloop
|
|
|
|
stosb
|
|
|
|
jmp .byteloop
|
2013-07-10 00:19:08 +04:00
|
|
|
.got_command: ; we have a newline check if its a command
|
2013-07-10 00:55:18 +04:00
|
|
|
cmp esi, ecx
|
|
|
|
je .no_more_data
|
|
|
|
mov [offset], esi
|
|
|
|
sub ecx, esi
|
|
|
|
mov [size], ecx
|
|
|
|
jmp .go_cmd
|
|
|
|
.no_more_data:
|
|
|
|
mov [offset], 0
|
|
|
|
.go_cmd:
|
2013-06-24 23:33:16 +04:00
|
|
|
xor al, al
|
|
|
|
stosb
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_set_flags, 0x03 ; change color
|
|
|
|
invoke con_write_asciiz, s ; print servercommand
|
|
|
|
invoke con_write_asciiz, str4 ; newline
|
|
|
|
invoke con_set_flags, 0x07
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
jmp server_parser ; parse command
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
wait_for_usercommand:
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_set_flags, 0x0a
|
|
|
|
|
2013-06-24 23:33:16 +04:00
|
|
|
cmp [status], STATUS_CONNECTED
|
|
|
|
je .connected
|
|
|
|
|
|
|
|
cmp [status], STATUS_NEEDPASSWORD
|
|
|
|
je .needpass
|
|
|
|
|
|
|
|
; write prompt
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str2
|
2013-06-24 23:33:16 +04:00
|
|
|
; read string
|
|
|
|
mov esi, s
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_gets, esi, 256
|
2013-07-10 01:59:02 +04:00
|
|
|
|
|
|
|
invoke con_write_asciiz, str4 ; newline
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_set_flags, 0x07
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
cmp dword[s], "list"
|
|
|
|
je cmd_list
|
|
|
|
|
|
|
|
cmp dword[s], "help"
|
|
|
|
je cmd_help
|
|
|
|
|
2013-07-10 00:55:18 +04:00
|
|
|
cmp dword[s], "cwd "
|
|
|
|
je cmd_cwd
|
|
|
|
|
2013-07-10 01:31:47 +04:00
|
|
|
cmp dword[s], "retr"
|
|
|
|
je cmd_retr
|
|
|
|
|
2013-07-10 01:59:02 +04:00
|
|
|
cmp dword[s], "pwd" + 10 shl 24
|
|
|
|
je cmd_pwd
|
|
|
|
|
2013-07-10 01:31:47 +04:00
|
|
|
; cmp dword[s], "stor"
|
|
|
|
; je cmd_stor
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str_unknown
|
2013-06-24 23:33:16 +04:00
|
|
|
jmp wait_for_usercommand
|
|
|
|
|
|
|
|
|
|
|
|
.connected:
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str_user
|
2013-06-24 23:33:16 +04:00
|
|
|
mov dword[s], "USER"
|
|
|
|
mov byte[s+4], " "
|
|
|
|
jmp .send
|
|
|
|
|
|
|
|
|
|
|
|
.needpass:
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str_pass
|
2013-06-24 23:33:16 +04:00
|
|
|
mov dword[s], "PASS"
|
|
|
|
mov byte[s+4], " "
|
|
|
|
|
|
|
|
.send:
|
|
|
|
; read string
|
|
|
|
mov esi, s+5
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_gets, esi, 256
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
mov edi, s+5
|
|
|
|
mov ecx, 256
|
|
|
|
xor al, al
|
|
|
|
repne scasb
|
|
|
|
lea esi, [edi-s-1]
|
2013-07-10 00:19:08 +04:00
|
|
|
mcall send, [socketnum], s, , 0
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str4 ; newline
|
|
|
|
invoke con_set_flags, 0x07
|
|
|
|
jmp wait_for_servercommand
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open_dataconnection:
|
|
|
|
cmp [status], STATUS_LOGGED_IN
|
|
|
|
jne .fail
|
|
|
|
|
|
|
|
mov dword[s], "PASV"
|
|
|
|
mov byte[s+4], 10
|
2013-07-10 00:19:08 +04:00
|
|
|
mcall send, [socketnum], s, 5, 0
|
2013-06-24 23:33:16 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str6
|
2013-06-24 23:33:16 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
socket_error:
|
|
|
|
invoke con_write_asciiz, str6
|
2013-06-24 23:33:16 +04:00
|
|
|
jmp fail.wait
|
|
|
|
|
|
|
|
fail:
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str5
|
2013-06-24 23:33:16 +04:00
|
|
|
.wait:
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_write_asciiz, str10
|
|
|
|
invoke con_getch2
|
2013-06-24 23:33:16 +04:00
|
|
|
jmp main
|
|
|
|
|
|
|
|
done:
|
2013-07-10 00:19:08 +04:00
|
|
|
invoke con_exit, 1
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 00:19:08 +04:00
|
|
|
exit:
|
2013-06-24 23:33:16 +04:00
|
|
|
mcall close, [socketnum]
|
|
|
|
mcall -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; data
|
|
|
|
title db 'FTP client',0
|
2013-07-10 01:59:02 +04:00
|
|
|
str1 db 'FTP client for KolibriOS v0.04',10,10,'Please enter ftp server address.',10,0
|
2013-06-24 23:33:16 +04:00
|
|
|
str2 db '> ',0
|
|
|
|
str3 db 'Resolving ',0
|
|
|
|
str4 db 10,0
|
|
|
|
str5 db 10,'Name resolution failed.',10,0
|
|
|
|
str6 db 10,'Socket error.',10,0
|
|
|
|
str8 db ' (',0
|
|
|
|
str9 db ')',10,0
|
|
|
|
str10 db 'Push any key to continue.',0
|
2013-07-10 00:19:08 +04:00
|
|
|
str11 db 'Connecting...',10,0
|
|
|
|
str12 db 'Waiting for welcome message.',10,0
|
2013-06-24 23:33:16 +04:00
|
|
|
str_user db "username: ",0
|
|
|
|
str_pass db "password: ",0
|
2013-07-10 00:19:08 +04:00
|
|
|
str_unknown db "unknown command",10,0
|
2013-07-10 01:59:02 +04:00
|
|
|
str_help db "available commands:",10
|
|
|
|
db "help list cwd retr pwd",10,10,0
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
str_open db "opening data socket",10,0
|
|
|
|
|
|
|
|
sockaddr1:
|
|
|
|
dw AF_INET4
|
|
|
|
.port dw 0x1500 ; 21
|
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
|
|
|
|
|
|
|
sockaddr2:
|
|
|
|
dw AF_INET4
|
|
|
|
.port dw 0
|
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
|
|
|
|
|
|
|
|
|
|
|
; import
|
|
|
|
align 4
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library network, 'network.obj', console, 'console.obj'
|
|
|
|
|
|
|
|
import network, \
|
|
|
|
getaddrinfo, 'getaddrinfo', \
|
|
|
|
freeaddrinfo, 'freeaddrinfo', \
|
|
|
|
inet_ntoa, 'inet_ntoa'
|
|
|
|
|
|
|
|
import console, \
|
|
|
|
con_start, 'START', \
|
|
|
|
con_init, 'con_init', \
|
|
|
|
con_write_asciiz,'con_write_asciiz', \
|
|
|
|
con_exit, 'con_exit', \
|
|
|
|
con_gets, 'con_gets',\
|
|
|
|
con_cls, 'con_cls',\
|
|
|
|
con_getch2, 'con_getch2',\
|
|
|
|
con_set_cursor_pos, 'con_set_cursor_pos',\
|
|
|
|
con_write_string, 'con_write_string',\
|
2013-07-10 00:19:08 +04:00
|
|
|
con_get_flags, 'con_get_flags', \
|
|
|
|
con_set_flags, 'con_set_flags'
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
i_end:
|
|
|
|
|
2013-07-10 01:59:02 +04:00
|
|
|
align 4
|
|
|
|
status db ?
|
2013-06-24 23:33:16 +04:00
|
|
|
active_passive db ?
|
2013-07-10 01:59:02 +04:00
|
|
|
|
|
|
|
align 4
|
2013-06-24 23:33:16 +04:00
|
|
|
socketnum dd ?
|
|
|
|
datasocket dd ?
|
2013-07-10 00:55:18 +04:00
|
|
|
offset dd ?
|
|
|
|
size dd ?
|
2013-06-24 23:33:16 +04:00
|
|
|
|
2013-07-10 01:59:02 +04:00
|
|
|
buffer_ptr rb BUFFERSIZE+1
|
|
|
|
buffer_ptr2 rb BUFFERSIZE+1
|
|
|
|
|
2013-07-10 00:55:18 +04:00
|
|
|
s rb 1024
|
2013-06-24 23:33:16 +04:00
|
|
|
|
|
|
|
mem:
|