Rev 3704 | Rev 3790 | 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 | |||
17 | BUFFERSIZE = 1024 |
||
18 | |||
19 | STATUS_CONNECTING = 0 |
||
20 | STATUS_CONNECTED = 1 |
||
21 | STATUS_NEEDPASSWORD = 2 |
||
22 | STATUS_LOGGED_IN = 3 |
||
23 | |||
24 | use32 |
||
25 | ; standard header |
||
26 | db 'MENUET01' ; signature |
||
27 | dd 1 ; header version |
||
28 | dd start ; entry point |
||
29 | dd i_end ; initialized size |
||
30 | dd mem+0x1000 ; required memory |
||
31 | dd mem+0x1000 ; stack pointer |
||
32 | dd s ; parameters |
||
33 | dd 0 ; path |
||
34 | |||
35 | include '../../macros.inc' |
||
36 | purge mov,add,sub |
||
37 | include '../../proc32.inc' |
||
38 | include '../../dll.inc' |
||
39 | include '../../network.inc' |
||
40 | |||
41 | include 'usercommands.inc' |
||
42 | include 'servercommands.inc' |
||
43 | |||
44 | ; entry point |
||
45 | start: |
||
46 | |||
3789 | hidnplayr | 47 | mcall 40, 0 |
3701 | hidnplayr | 48 | ; load libraries |
49 | stdcall dll.Load, @IMPORT |
||
50 | test eax, eax |
||
51 | jnz exit |
||
52 | ; initialize console |
||
3789 | hidnplayr | 53 | invoke con_start, 1 |
54 | invoke con_init, 80, 25, 80, 25, title |
||
3701 | hidnplayr | 55 | |
56 | ; Check for parameters |
||
57 | cmp byte [s], 0 |
||
58 | jne resolve |
||
59 | |||
60 | main: |
||
3789 | hidnplayr | 61 | invoke con_cls |
3701 | hidnplayr | 62 | ; Welcome user |
3789 | hidnplayr | 63 | invoke con_write_asciiz, str1 |
3701 | hidnplayr | 64 | |
65 | ; write prompt |
||
3789 | hidnplayr | 66 | invoke con_set_flags, 0x0a |
67 | invoke con_write_asciiz, str2 |
||
3701 | hidnplayr | 68 | ; read string |
69 | mov esi, s |
||
3789 | hidnplayr | 70 | invoke con_gets, esi, 256 |
71 | invoke con_write_asciiz, str4 ; newline |
||
72 | invoke con_set_flags, 0x07 |
||
3701 | hidnplayr | 73 | ; check for exit |
74 | test eax, eax |
||
75 | jz done |
||
76 | cmp byte [esi], 10 |
||
77 | jz done |
||
78 | |||
79 | resolve: |
||
80 | ; delete terminating '\n' |
||
81 | mov esi, s |
||
82 | @@: |
||
83 | lodsb |
||
84 | cmp al, 0x20 |
||
85 | ja @r |
||
86 | mov byte [esi-1], 0 |
||
87 | |||
3789 | hidnplayr | 88 | invoke con_write_asciiz, str3 |
89 | invoke con_write_asciiz, s |
||
3701 | hidnplayr | 90 | |
91 | ; resolve name |
||
92 | push esp ; reserve stack place |
||
3789 | hidnplayr | 93 | invoke getaddrinfo, s, 0, 0, esp |
3701 | hidnplayr | 94 | pop esi |
95 | ; test for error |
||
96 | test eax, eax |
||
97 | jnz fail |
||
98 | |||
99 | ; write results |
||
3789 | hidnplayr | 100 | invoke con_write_asciiz, str8 |
3701 | hidnplayr | 101 | ; mov edi, esi |
102 | |||
103 | ; convert IP address to decimal notation |
||
104 | mov eax, [esi+addrinfo.ai_addr] |
||
105 | mov eax, [eax+sockaddr_in.sin_addr] |
||
106 | mov [sockaddr1.ip], eax |
||
3789 | hidnplayr | 107 | |
108 | invoke inet_ntoa, eax |
||
3701 | hidnplayr | 109 | ; write result |
3789 | hidnplayr | 110 | invoke con_write_asciiz, eax |
3701 | hidnplayr | 111 | ; free allocated memory |
3789 | hidnplayr | 112 | invoke freeaddrinfo, esi |
3701 | hidnplayr | 113 | |
3789 | hidnplayr | 114 | invoke con_write_asciiz, str9 |
3701 | hidnplayr | 115 | mcall socket, AF_INET4, SOCK_STREAM, 0 |
116 | cmp eax, -1 |
||
3789 | hidnplayr | 117 | je socket_error |
3701 | hidnplayr | 118 | mov [socketnum], eax |
119 | |||
3789 | hidnplayr | 120 | invoke con_write_asciiz, str11 |
3701 | hidnplayr | 121 | mcall connect, [socketnum], sockaddr1, 18 |
122 | mov [status], STATUS_CONNECTING |
||
123 | |||
3789 | hidnplayr | 124 | invoke con_write_asciiz, str12 |
3701 | hidnplayr | 125 | |
3789 | hidnplayr | 126 | wait_for_servercommand: |
3701 | hidnplayr | 127 | |
3789 | hidnplayr | 128 | ; invoke con_write_asciiz, str_dbg |
3701 | hidnplayr | 129 | |
130 | ; receive socket data |
||
3789 | hidnplayr | 131 | mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0 |
3701 | hidnplayr | 132 | inc eax |
3789 | hidnplayr | 133 | jz socket_error |
3701 | hidnplayr | 134 | dec eax |
3789 | hidnplayr | 135 | jz wait_for_servercommand |
3701 | hidnplayr | 136 | |
3789 | hidnplayr | 137 | ; invoke con_write_asciiz, str_dbg2 |
138 | |||
3701 | hidnplayr | 139 | ; extract commands, copy them to "s" buffer |
3789 | hidnplayr | 140 | lea ecx, [eax + buffer_ptr] ; ecx = end pointer |
3701 | hidnplayr | 141 | mov esi, buffer_ptr ; esi = current pointer |
142 | mov edi, s |
||
143 | .byteloop: |
||
3789 | hidnplayr | 144 | cmp esi, ecx |
145 | jae wait_for_servercommand |
||
3701 | hidnplayr | 146 | lodsb |
147 | cmp al, 10 ; excellent, we might have a command |
||
148 | je .got_command |
||
3789 | hidnplayr | 149 | cmp al, 13 ; just ignore this byte |
3701 | hidnplayr | 150 | je .byteloop |
151 | stosb |
||
152 | jmp .byteloop |
||
3789 | hidnplayr | 153 | .got_command: ; we have a newline check if its a command |
3701 | hidnplayr | 154 | xor al, al |
155 | stosb |
||
156 | |||
3789 | hidnplayr | 157 | sub edi, s ; length |
158 | push edi |
||
3701 | hidnplayr | 159 | |
3789 | hidnplayr | 160 | invoke con_set_flags, 0x03 ; change color |
161 | invoke con_write_asciiz, s ; print servercommand |
||
162 | invoke con_write_asciiz, str4 ; newline |
||
163 | invoke con_set_flags, 0x07 |
||
3701 | hidnplayr | 164 | |
3789 | hidnplayr | 165 | pop ecx |
166 | jmp server_parser ; parse command |
||
3701 | hidnplayr | 167 | |
3789 | hidnplayr | 168 | wait_for_usercommand: |
3701 | hidnplayr | 169 | |
3789 | hidnplayr | 170 | invoke con_set_flags, 0x0a |
3701 | hidnplayr | 171 | |
172 | cmp [status], STATUS_CONNECTED |
||
173 | je .connected |
||
174 | |||
175 | cmp [status], STATUS_NEEDPASSWORD |
||
176 | je .needpass |
||
177 | |||
178 | ; write prompt |
||
3789 | hidnplayr | 179 | invoke con_write_asciiz, str2 |
3701 | hidnplayr | 180 | ; read string |
181 | mov esi, s |
||
3789 | hidnplayr | 182 | invoke con_gets, esi, 256 |
183 | invoke con_set_flags, 0x07 |
||
3701 | hidnplayr | 184 | |
185 | cmp dword[s], "list" |
||
186 | je cmd_list |
||
187 | |||
188 | cmp dword[s], "help" |
||
189 | je cmd_help |
||
190 | |||
3789 | hidnplayr | 191 | invoke con_write_asciiz, str_unknown |
3701 | hidnplayr | 192 | jmp wait_for_usercommand |
193 | |||
194 | |||
195 | .connected: |
||
196 | |||
3789 | hidnplayr | 197 | invoke con_write_asciiz, str_user |
3701 | hidnplayr | 198 | mov dword[s], "USER" |
199 | mov byte[s+4], " " |
||
200 | jmp .send |
||
201 | |||
202 | |||
203 | .needpass: |
||
204 | |||
3789 | hidnplayr | 205 | invoke con_write_asciiz, str_pass |
3701 | hidnplayr | 206 | mov dword[s], "PASS" |
207 | mov byte[s+4], " " |
||
208 | |||
209 | .send: |
||
210 | ; read string |
||
211 | mov esi, s+5 |
||
3789 | hidnplayr | 212 | invoke con_gets, esi, 256 |
3701 | hidnplayr | 213 | |
214 | mov edi, s+5 |
||
215 | mov ecx, 256 |
||
216 | xor al, al |
||
217 | repne scasb |
||
218 | lea esi, [edi-s-1] |
||
3789 | hidnplayr | 219 | mcall send, [socketnum], s, , 0 |
3701 | hidnplayr | 220 | |
3789 | hidnplayr | 221 | invoke con_write_asciiz, str4 ; newline |
222 | invoke con_set_flags, 0x07 |
||
223 | jmp wait_for_servercommand |
||
3701 | hidnplayr | 224 | |
225 | |||
226 | |||
227 | open_dataconnection: |
||
228 | cmp [status], STATUS_LOGGED_IN |
||
229 | jne .fail |
||
230 | |||
231 | mov dword[s], "PASV" |
||
232 | mov byte[s+4], 10 |
||
3789 | hidnplayr | 233 | mcall send, [socketnum], s, 5, 0 |
3701 | hidnplayr | 234 | ret |
235 | |||
236 | .fail: |
||
3789 | hidnplayr | 237 | invoke con_write_asciiz, str6 |
3701 | hidnplayr | 238 | ret |
239 | |||
240 | |||
241 | |||
3789 | hidnplayr | 242 | socket_error: |
243 | invoke con_write_asciiz, str6 |
||
3701 | hidnplayr | 244 | jmp fail.wait |
245 | |||
246 | fail: |
||
3789 | hidnplayr | 247 | invoke con_write_asciiz, str5 |
3701 | hidnplayr | 248 | .wait: |
3789 | hidnplayr | 249 | invoke con_write_asciiz, str10 |
250 | invoke con_getch2 |
||
3701 | hidnplayr | 251 | jmp main |
252 | |||
253 | done: |
||
3789 | hidnplayr | 254 | invoke con_exit, 1 |
255 | |||
3701 | hidnplayr | 256 | exit: |
257 | mcall close, [socketnum] |
||
258 | mcall -1 |
||
259 | |||
260 | |||
261 | |||
262 | ; data |
||
263 | title db 'FTP client',0 |
||
3789 | hidnplayr | 264 | str1 db 'FTP client for KolibriOS v0.02',10,10,'Please enter ftp server address.',10,0 |
3701 | hidnplayr | 265 | str2 db '> ',0 |
266 | str3 db 'Resolving ',0 |
||
267 | str4 db 10,0 |
||
268 | str5 db 10,'Name resolution failed.',10,0 |
||
269 | str6 db 10,'Socket error.',10,0 |
||
270 | str8 db ' (',0 |
||
271 | str9 db ')',10,0 |
||
272 | str10 db 'Push any key to continue.',0 |
||
3789 | hidnplayr | 273 | str11 db 'Connecting...',10,0 |
274 | str12 db 'Waiting for welcome message.',10,0 |
||
3701 | hidnplayr | 275 | str_user db "username: ",0 |
276 | str_pass db "password: ",0 |
||
3789 | hidnplayr | 277 | str_unknown db "unknown command",10,0 |
3701 | hidnplayr | 278 | str_help db "available commands:",10,10 |
279 | db "help list",10,0 |
||
280 | |||
281 | str_open db "opening data socket",10,0 |
||
282 | |||
3789 | hidnplayr | 283 | str_dbg db 'debug',10,0 |
284 | str_dbg2 db 'debug2',10,0 |
||
285 | |||
3701 | hidnplayr | 286 | sockaddr1: |
287 | dw AF_INET4 |
||
288 | .port dw 0x1500 ; 21 |
||
289 | .ip dd 0 |
||
290 | rb 10 |
||
291 | |||
292 | sockaddr2: |
||
293 | dw AF_INET4 |
||
294 | .port dw 0 |
||
295 | .ip dd 0 |
||
296 | rb 10 |
||
297 | |||
298 | |||
299 | ; import |
||
300 | align 4 |
||
301 | @IMPORT: |
||
302 | |||
303 | library network, 'network.obj', console, 'console.obj' |
||
304 | |||
305 | import network, \ |
||
306 | getaddrinfo, 'getaddrinfo', \ |
||
307 | freeaddrinfo, 'freeaddrinfo', \ |
||
308 | inet_ntoa, 'inet_ntoa' |
||
309 | |||
310 | import console, \ |
||
311 | con_start, 'START', \ |
||
312 | con_init, 'con_init', \ |
||
313 | con_write_asciiz,'con_write_asciiz', \ |
||
314 | con_exit, 'con_exit', \ |
||
315 | con_gets, 'con_gets',\ |
||
316 | con_cls, 'con_cls',\ |
||
317 | con_getch2, 'con_getch2',\ |
||
318 | con_set_cursor_pos, 'con_set_cursor_pos',\ |
||
319 | con_write_string, 'con_write_string',\ |
||
3789 | hidnplayr | 320 | con_get_flags, 'con_get_flags', \ |
321 | con_set_flags, 'con_set_flags' |
||
3701 | hidnplayr | 322 | |
323 | |||
324 | i_end: |
||
325 | |||
326 | active_passive db ? |
||
327 | socketnum dd ? |
||
328 | datasocket dd ? |
||
329 | buffer_ptr rb 2*BUFFERSIZE |
||
330 | status db ? |
||
331 | |||
332 | s rb 1024 |
||
333 | |||
334 | mem: |