Rev 3804 | Rev 3814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3701 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2013. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;; ftpc.asm - FTP client for KolibriOS ;; |
||
7 | ;; ;; |
||
8 | ;; Written by hidnplayr@kolibrios.org ;; |
||
9 | ;; ;; |
||
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
11 | ;; Version 2, June 1991 ;; |
||
12 | ;; ;; |
||
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
14 | |||
15 | format binary as "" |
||
16 | |||
3800 | hidnplayr | 17 | BUFFERSIZE = 4096 |
3701 | hidnplayr | 18 | |
19 | STATUS_CONNECTING = 0 |
||
20 | STATUS_CONNECTED = 1 |
||
21 | STATUS_NEEDPASSWORD = 2 |
||
22 | STATUS_LOGGED_IN = 3 |
||
23 | |||
3800 | hidnplayr | 24 | OPERATION_LIST = 0 |
25 | OPERATION_RETR = 1 |
||
26 | OPERATION_STOR = 2 |
||
27 | |||
3701 | hidnplayr | 28 | use32 |
29 | ; standard header |
||
30 | db 'MENUET01' ; signature |
||
31 | dd 1 ; header version |
||
32 | dd start ; entry point |
||
33 | dd i_end ; initialized size |
||
34 | dd mem+0x1000 ; required memory |
||
35 | dd mem+0x1000 ; stack pointer |
||
36 | dd s ; parameters |
||
37 | dd 0 ; path |
||
38 | |||
39 | include '../../macros.inc' |
||
40 | purge mov,add,sub |
||
41 | include '../../proc32.inc' |
||
42 | include '../../dll.inc' |
||
43 | include '../../network.inc' |
||
44 | |||
45 | include 'usercommands.inc' |
||
46 | include 'servercommands.inc' |
||
47 | |||
48 | start: |
||
3813 | hidnplayr | 49 | ; disable all events |
3789 | hidnplayr | 50 | mcall 40, 0 |
3701 | hidnplayr | 51 | ; load libraries |
52 | stdcall dll.Load, @IMPORT |
||
53 | test eax, eax |
||
54 | jnz exit |
||
55 | ; initialize console |
||
3789 | hidnplayr | 56 | invoke con_start, 1 |
3813 | hidnplayr | 57 | invoke con_init, 80, 25, 80, 250, str_title |
58 | ; Check for parameters, if there are some, resolve the address right away |
||
3701 | hidnplayr | 59 | cmp byte [s], 0 |
60 | jne resolve |
||
61 | |||
62 | main: |
||
3813 | hidnplayr | 63 | ; Clear screen |
3789 | hidnplayr | 64 | invoke con_cls |
3701 | hidnplayr | 65 | ; Welcome user |
3813 | hidnplayr | 66 | invoke con_write_asciiz, str_welcome |
67 | ; write prompt (in green color) |
||
3789 | hidnplayr | 68 | invoke con_set_flags, 0x0a |
3813 | hidnplayr | 69 | invoke con_write_asciiz, str_prompt |
3701 | hidnplayr | 70 | ; read string |
3813 | hidnplayr | 71 | invoke con_gets, s, 256 |
3701 | hidnplayr | 72 | ; check for exit |
73 | test eax, eax |
||
74 | jz done |
||
3813 | hidnplayr | 75 | cmp byte [s], 10 |
3701 | hidnplayr | 76 | jz done |
3813 | hidnplayr | 77 | ; reset color back to grey and print newline |
78 | invoke con_set_flags, 0x07 |
||
79 | invoke con_write_asciiz, str_newline |
||
3701 | hidnplayr | 80 | |
81 | resolve: |
||
82 | ; delete terminating '\n' |
||
83 | mov esi, s |
||
84 | @@: |
||
85 | lodsb |
||
86 | cmp al, 0x20 |
||
87 | ja @r |
||
88 | mov byte [esi-1], 0 |
||
3813 | hidnplayr | 89 | ; Say to the user that we're resolving |
90 | invoke con_write_asciiz, str_resolve |
||
3789 | hidnplayr | 91 | invoke con_write_asciiz, s |
3701 | hidnplayr | 92 | ; resolve name |
93 | push esp ; reserve stack place |
||
3789 | hidnplayr | 94 | invoke getaddrinfo, s, 0, 0, esp |
3701 | hidnplayr | 95 | pop esi |
96 | ; test for error |
||
97 | test eax, eax |
||
3813 | hidnplayr | 98 | jnz error_resolve |
3701 | hidnplayr | 99 | ; write results |
3800 | hidnplayr | 100 | invoke con_write_asciiz, str8 ; ' (',0 |
101 | mov eax, [esi+addrinfo.ai_addr] ; convert IP address to decimal notation |
||
102 | mov eax, [eax+sockaddr_in.sin_addr] ; |
||
103 | mov [sockaddr1.ip], eax ; |
||
104 | invoke inet_ntoa, eax ; |
||
105 | invoke con_write_asciiz, eax ; print ip |
||
106 | invoke freeaddrinfo, esi ; free allocated memory |
||
107 | invoke con_write_asciiz, str9 ; ')',10,0 |
||
108 | ; open the socket |
||
3701 | hidnplayr | 109 | mcall socket, AF_INET4, SOCK_STREAM, 0 |
110 | cmp eax, -1 |
||
3813 | hidnplayr | 111 | je error_socket |
3701 | hidnplayr | 112 | mov [socketnum], eax |
3800 | hidnplayr | 113 | ; connect to the server |
3813 | hidnplayr | 114 | invoke con_write_asciiz, str_connect |
3701 | hidnplayr | 115 | mcall connect, [socketnum], sockaddr1, 18 |
116 | mov [status], STATUS_CONNECTING |
||
3813 | hidnplayr | 117 | ; Tell the user we're waiting for the server now. |
118 | invoke con_write_asciiz, str_waiting |
||
3701 | hidnplayr | 119 | |
3813 | hidnplayr | 120 | ; Reset 'offset' variable, it's used by the data receiver |
3790 | hidnplayr | 121 | mov [offset], 0 |
122 | |||
3789 | hidnplayr | 123 | wait_for_servercommand: |
3813 | hidnplayr | 124 | ; Any commands still in our buffer? |
3790 | hidnplayr | 125 | cmp [offset], 0 |
3813 | hidnplayr | 126 | je .receive ; nope, receive some more |
3790 | hidnplayr | 127 | mov esi, [offset] |
128 | mov edi, s |
||
129 | mov ecx, [size] |
||
130 | add ecx, esi |
||
131 | jmp .byteloop |
||
3701 | hidnplayr | 132 | |
133 | ; receive socket data |
||
3790 | hidnplayr | 134 | .receive: |
3789 | hidnplayr | 135 | mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0 |
3701 | hidnplayr | 136 | inc eax |
3813 | hidnplayr | 137 | jz error_socket |
3701 | hidnplayr | 138 | dec eax |
3789 | hidnplayr | 139 | jz wait_for_servercommand |
3701 | hidnplayr | 140 | |
3790 | hidnplayr | 141 | mov [offset], 0 |
3789 | hidnplayr | 142 | |
3701 | hidnplayr | 143 | ; extract commands, copy them to "s" buffer |
3789 | hidnplayr | 144 | lea ecx, [eax + buffer_ptr] ; ecx = end pointer |
3701 | hidnplayr | 145 | mov esi, buffer_ptr ; esi = current pointer |
146 | mov edi, s |
||
147 | .byteloop: |
||
3789 | hidnplayr | 148 | cmp esi, ecx |
149 | jae wait_for_servercommand |
||
3701 | hidnplayr | 150 | lodsb |
151 | cmp al, 10 ; excellent, we might have a command |
||
152 | je .got_command |
||
3789 | hidnplayr | 153 | cmp al, 13 ; just ignore this byte |
3701 | hidnplayr | 154 | je .byteloop |
155 | stosb |
||
156 | jmp .byteloop |
||
3789 | hidnplayr | 157 | .got_command: ; we have a newline check if its a command |
3790 | hidnplayr | 158 | cmp esi, ecx |
159 | je .no_more_data |
||
160 | mov [offset], esi |
||
161 | sub ecx, esi |
||
162 | mov [size], ecx |
||
163 | jmp .go_cmd |
||
164 | .no_more_data: |
||
165 | mov [offset], 0 |
||
166 | .go_cmd: |
||
3701 | hidnplayr | 167 | xor al, al |
168 | stosb |
||
169 | |||
3789 | hidnplayr | 170 | invoke con_set_flags, 0x03 ; change color |
171 | invoke con_write_asciiz, s ; print servercommand |
||
3813 | hidnplayr | 172 | invoke con_write_asciiz, str_newline |
3800 | hidnplayr | 173 | invoke con_set_flags, 0x07 ; reset color |
3701 | hidnplayr | 174 | |
3789 | hidnplayr | 175 | jmp server_parser ; parse command |
3701 | hidnplayr | 176 | |
3813 | hidnplayr | 177 | |
178 | |||
3789 | hidnplayr | 179 | wait_for_usercommand: |
3701 | hidnplayr | 180 | |
3813 | hidnplayr | 181 | ; change color to green for user input |
3789 | hidnplayr | 182 | invoke con_set_flags, 0x0a |
3701 | hidnplayr | 183 | |
3813 | hidnplayr | 184 | ; If we are not yet connected, request username/password |
3701 | hidnplayr | 185 | cmp [status], STATUS_CONNECTED |
186 | je .connected |
||
187 | |||
188 | cmp [status], STATUS_NEEDPASSWORD |
||
189 | je .needpass |
||
190 | |||
191 | ; write prompt |
||
3813 | hidnplayr | 192 | invoke con_write_asciiz, str_prompt |
3701 | hidnplayr | 193 | ; read string |
3813 | hidnplayr | 194 | invoke con_gets, s, 256 |
3794 | hidnplayr | 195 | |
3813 | hidnplayr | 196 | ; print a newline and reset the color back to grey |
197 | invoke con_write_asciiz, str_newline |
||
3789 | hidnplayr | 198 | invoke con_set_flags, 0x07 |
3701 | hidnplayr | 199 | |
3790 | hidnplayr | 200 | cmp dword[s], "cwd " |
201 | je cmd_cwd |
||
202 | |||
3813 | hidnplayr | 203 | cmp dword[s], "mkd " |
204 | je cmd_mkd |
||
3793 | hidnplayr | 205 | |
3813 | hidnplayr | 206 | cmp dword[s], "rmd " |
207 | je cmd_rmd |
||
208 | |||
3794 | hidnplayr | 209 | cmp dword[s], "pwd" + 10 shl 24 |
210 | je cmd_pwd |
||
211 | |||
3813 | hidnplayr | 212 | cmp dword[s], "bye" + 10 shl 24 |
213 | je cmd_bye |
||
214 | |||
215 | cmp byte[s+4], " " |
||
216 | jne @f |
||
217 | |||
218 | cmp dword[s], "lcwd" |
||
219 | je cmd_lcwd |
||
220 | |||
221 | cmp dword[s], "retr" |
||
222 | je cmd_retr |
||
223 | |||
3800 | hidnplayr | 224 | cmp dword[s], "stor" |
225 | je cmd_stor |
||
3793 | hidnplayr | 226 | |
3800 | hidnplayr | 227 | cmp dword[s], "dele" |
228 | je cmd_dele |
||
229 | |||
3813 | hidnplayr | 230 | @@: |
231 | cmp byte[s+4], 10 |
||
232 | jne @f |
||
3800 | hidnplayr | 233 | |
3813 | hidnplayr | 234 | cmp dword[s], "list" |
235 | je cmd_list |
||
3802 | hidnplayr | 236 | |
3813 | hidnplayr | 237 | cmp dword[s], "help" |
238 | je cmd_help |
||
3804 | hidnplayr | 239 | |
240 | cmp dword[s], "cdup" |
||
241 | je cmd_cdup |
||
242 | |||
3813 | hidnplayr | 243 | @@: |
244 | ; Uh oh.. unknown command, tell the user and wait for new input |
||
3789 | hidnplayr | 245 | invoke con_write_asciiz, str_unknown |
3701 | hidnplayr | 246 | jmp wait_for_usercommand |
247 | |||
248 | |||
249 | .connected: |
||
3813 | hidnplayr | 250 | ; request username |
3789 | hidnplayr | 251 | invoke con_write_asciiz, str_user |
3701 | hidnplayr | 252 | mov dword[s], "USER" |
253 | mov byte[s+4], " " |
||
254 | jmp .send |
||
255 | |||
256 | |||
257 | .needpass: |
||
3813 | hidnplayr | 258 | ; request password |
3789 | hidnplayr | 259 | invoke con_write_asciiz, str_pass |
3701 | hidnplayr | 260 | mov dword[s], "PASS" |
261 | mov byte[s+4], " " |
||
3803 | hidnplayr | 262 | invoke con_set_flags, 0x00 ; black text on black background for password |
3701 | hidnplayr | 263 | |
264 | .send: |
||
265 | ; read string |
||
266 | mov esi, s+5 |
||
3789 | hidnplayr | 267 | invoke con_gets, esi, 256 |
3701 | hidnplayr | 268 | |
3800 | hidnplayr | 269 | ; find end of string |
3701 | hidnplayr | 270 | mov edi, s+5 |
271 | mov ecx, 256 |
||
272 | xor al, al |
||
273 | repne scasb |
||
274 | lea esi, [edi-s-1] |
||
3800 | hidnplayr | 275 | ; and send it to the server |
3789 | hidnplayr | 276 | mcall send, [socketnum], s, , 0 |
3701 | hidnplayr | 277 | |
3813 | hidnplayr | 278 | invoke con_write_asciiz, str_newline |
3800 | hidnplayr | 279 | invoke con_set_flags, 0x07 ; reset color |
3789 | hidnplayr | 280 | jmp wait_for_servercommand |
3701 | hidnplayr | 281 | |
282 | |||
283 | |||
3800 | hidnplayr | 284 | open_dataconnection: ; only passive for now.. |
3701 | hidnplayr | 285 | cmp [status], STATUS_LOGGED_IN |
286 | jne .fail |
||
287 | |||
288 | mov dword[s], "PASV" |
||
289 | mov byte[s+4], 10 |
||
3789 | hidnplayr | 290 | mcall send, [socketnum], s, 5, 0 |
3701 | hidnplayr | 291 | ret |
292 | |||
293 | .fail: |
||
3813 | hidnplayr | 294 | invoke con_write_asciiz, str_err_socket |
3701 | hidnplayr | 295 | ret |
296 | |||
297 | |||
298 | |||
3813 | hidnplayr | 299 | error_socket: |
300 | invoke con_write_asciiz, str_err_socket |
||
301 | jmp wait_for_keypress |
||
3701 | hidnplayr | 302 | |
3813 | hidnplayr | 303 | error_resolve: |
304 | invoke con_write_asciiz, str_err_resolve |
||
305 | |||
306 | wait_for_keypress: |
||
307 | invoke con_write_asciiz, str_push |
||
3789 | hidnplayr | 308 | invoke con_getch2 |
3701 | hidnplayr | 309 | jmp main |
310 | |||
311 | done: |
||
3789 | hidnplayr | 312 | invoke con_exit, 1 |
313 | |||
3701 | hidnplayr | 314 | exit: |
315 | mcall close, [socketnum] |
||
316 | mcall -1 |
||
317 | |||
318 | |||
319 | |||
320 | ; data |
||
3813 | hidnplayr | 321 | str_title db 'FTP client',0 |
322 | str_welcome db 'FTP client for KolibriOS v0.08',10 |
||
323 | db 10 |
||
324 | db 'Please enter ftp server address.',10,0 |
||
3800 | hidnplayr | 325 | |
3813 | hidnplayr | 326 | str_prompt db '> ',0 |
327 | str_resolve db 'Resolving ',0 |
||
328 | str_newline db 10,0 |
||
329 | str_err_resolve db 10,'Name resolution failed.',10,0 |
||
330 | str_err_socket db 10,'Socket error.',10,0 |
||
331 | str8 db ' (',0 |
||
332 | str9 db ')',10,0 |
||
333 | str_push db 'Push any key to continue.',0 |
||
334 | str_connect db 'Connecting...',10,0 |
||
335 | str_waiting db 'Waiting for welcome message.',10,0 |
||
336 | str_user db "username: ",0 |
||
337 | str_pass db "password: ",0 |
||
338 | str_unknown db "Unknown command or insufficient parameters - type help for more information.",10,0 |
||
339 | str_lcwd db "Local working directory is now: ",0 |
||
3701 | hidnplayr | 340 | |
3813 | hidnplayr | 341 | str_open db "opening data socket",10,0 |
3701 | hidnplayr | 342 | |
3813 | hidnplayr | 343 | str_help db "available commands:",10 |
344 | db 10 |
||
345 | db "bye - close the connection",10 |
||
346 | db "cdup - change to parent of current directory on the server",10 |
||
347 | db "cwd |
||
348 | db "dele |
||
349 | db "list - list files and folders in current server directory",10 |
||
350 | db "lcwd |
||
351 | db "mkd |
||
352 | db "pwd - print server working directory",10 |
||
353 | db "retr |
||
354 | db "rmd |
||
355 | db "stor |
||
356 | db 10,0 |
||
357 | |||
3701 | hidnplayr | 358 | sockaddr1: |
359 | dw AF_INET4 |
||
360 | .port dw 0x1500 ; 21 |
||
361 | .ip dd 0 |
||
362 | rb 10 |
||
363 | |||
364 | sockaddr2: |
||
365 | dw AF_INET4 |
||
366 | .port dw 0 |
||
367 | .ip dd 0 |
||
368 | rb 10 |
||
369 | |||
370 | ; import |
||
371 | align 4 |
||
372 | @IMPORT: |
||
373 | |||
374 | library network, 'network.obj', console, 'console.obj' |
||
375 | |||
376 | import network, \ |
||
377 | getaddrinfo, 'getaddrinfo', \ |
||
378 | freeaddrinfo, 'freeaddrinfo', \ |
||
379 | inet_ntoa, 'inet_ntoa' |
||
380 | |||
381 | import console, \ |
||
382 | con_start, 'START', \ |
||
383 | con_init, 'con_init', \ |
||
384 | con_write_asciiz,'con_write_asciiz', \ |
||
385 | con_exit, 'con_exit', \ |
||
386 | con_gets, 'con_gets',\ |
||
387 | con_cls, 'con_cls',\ |
||
388 | con_getch2, 'con_getch2',\ |
||
389 | con_set_cursor_pos, 'con_set_cursor_pos',\ |
||
390 | con_write_string, 'con_write_string',\ |
||
3789 | hidnplayr | 391 | con_get_flags, 'con_get_flags', \ |
392 | con_set_flags, 'con_set_flags' |
||
3701 | hidnplayr | 393 | |
394 | |||
395 | i_end: |
||
396 | |||
3813 | hidnplayr | 397 | ; uninitialised data |
398 | |||
3794 | hidnplayr | 399 | status db ? |
3701 | hidnplayr | 400 | active_passive db ? |
3794 | hidnplayr | 401 | |
3701 | hidnplayr | 402 | socketnum dd ? |
403 | datasocket dd ? |
||
3790 | hidnplayr | 404 | offset dd ? |
405 | size dd ? |
||
3800 | hidnplayr | 406 | operation dd ? |
3701 | hidnplayr | 407 | |
3800 | hidnplayr | 408 | filestruct: |
409 | .subfn dd ? |
||
410 | .offset dd ? |
||
411 | dd ? |
||
412 | .size dd ? |
||
413 | .ptr dd ? |
||
414 | .name rb 1024 |
||
415 | |||
3794 | hidnplayr | 416 | buffer_ptr rb BUFFERSIZE+1 |
417 | buffer_ptr2 rb BUFFERSIZE+1 |
||
418 | |||
3790 | hidnplayr | 419 | s rb 1024 |
3701 | hidnplayr | 420 | |
421 | mem: |