Subversion Repositories Kolibri OS

Rev

Rev 4922 | Rev 6394 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4922 Rev 5011
Line 36... Line 36...
36
        dd      start           ; entry point
36
        dd      start           ; entry point
37
        dd      i_end           ; initialized size
37
        dd      i_end           ; initialized size
38
        dd      mem+0x1000      ; required memory
38
        dd      mem+0x1000      ; required memory
39
        dd      mem+0x1000      ; stack pointer
39
        dd      mem+0x1000      ; stack pointer
40
        dd      buf_cmd         ; parameters
40
        dd      buf_cmd         ; parameters
41
        dd      0               ; path
41
        dd      path            ; path
Line 42... Line 42...
42
 
42
 
43
include '../../macros.inc'
43
include '../../macros.inc'
44
purge mov,add,sub
44
purge mov,add,sub
45
include '../../proc32.inc'
45
include '../../proc32.inc'
Line 62... Line 62...
62
        test    eax, eax
62
        test    eax, eax
63
        jnz     exit
63
        jnz     exit
64
; initialize console
64
; initialize console
65
        invoke  con_start, 1
65
        invoke  con_start, 1
66
        invoke  con_init, 80, 25, 80, 250, str_title
66
        invoke  con_init, 80, 25, 80, 250, str_title
-
 
67
; find path to main settings file (ftpc.ini)
-
 
68
        mov     edi, path               ; Calculate the length of zero-terminated string
-
 
69
        xor     al, al
-
 
70
        mov     ecx, 1024
-
 
71
        repne   scasb
-
 
72
        dec     edi
-
 
73
        mov     esi, str_ini            ; append it with '.ini', 0
-
 
74
        movsd
-
 
75
        movsb
-
 
76
; get settings from ini
-
 
77
        invoke  ini.get_str, path, str_active, str_ip, str_active_ip, 16, 0
-
 
78
        mov     esi, str_active_ip
-
 
79
  .ip_loop:
-
 
80
        lodsb
-
 
81
        test    al, al
-
 
82
        jz      .ip_ok
-
 
83
        cmp     al, ' '
-
 
84
        je      .ip_ok
-
 
85
        cmp     al, '.'
-
 
86
        jne     .ip_loop
-
 
87
        mov     byte[esi-1], ','
-
 
88
        jmp     .ip_loop
-
 
89
  .ip_ok:
-
 
90
        mov     byte[esi-1], 0
-
 
91
 
-
 
92
        invoke  ini.get_int, path, str_active, str_port_start, 64000
-
 
93
        mov     [acti_port_start], ax
-
 
94
 
-
 
95
        invoke  ini.get_int, path, str_active, str_port_stop, 65000
-
 
96
        mov     [acti_port_stop], ax
-
 
97
 
-
 
98
        invoke  ini.get_str, path, str_general, str_dir, buf_buffer1, BUFFERSIZE, 0
-
 
99
        mcall   30, 1, buf_buffer1                      ; set working directory
-
 
100
 
67
; Check for parameters, if there are some, resolve the address right away
101
; Check for parameters, if there are some, resolve the address right away
-
 
102
; TODO: parse ftp://user:password@server.com:port/folder/subfolder type urls.
68
        cmp     byte [buf_cmd], 0
103
        cmp     byte [buf_cmd], 0
69
        jne     resolve
104
        jne     resolve
Line 70... Line 105...
70
 
105
 
71
main:
106
main:
Line 86... Line 121...
86
; reset color back to grey and print newline
121
; reset color back to grey and print newline
87
        invoke  con_set_flags, 0x07
122
        invoke  con_set_flags, 0x07
88
        invoke  con_write_asciiz, str_newline
123
        invoke  con_write_asciiz, str_newline
Line 89... Line 124...
89
 
124
 
-
 
125
resolve:
-
 
126
        mov     [sockaddr1.port], 21 shl 8
90
resolve:
127
 
91
; delete terminating '\n'
128
; delete terminating '\n'
92
        mov     esi, buf_cmd
129
        mov     esi, buf_cmd
93
  @@:
130
  @@:
-
 
131
        lodsb
-
 
132
        cmp     al, ':'
94
        lodsb
133
        je      .do_port
95
        cmp     al, 0x20
134
        cmp     al, 0x20
96
        ja      @r
135
        ja      @r
-
 
136
        mov     byte [esi-1], 0
-
 
137
        jmp     .done
-
 
138
 
-
 
139
  .do_port:
-
 
140
        xor     eax, eax
-
 
141
        xor     ebx, ebx
-
 
142
        mov     byte [esi-1], 0
-
 
143
  .portloop:
-
 
144
        lodsb
-
 
145
        cmp     al, 0x20
-
 
146
        jbe     .port_done
-
 
147
        sub     al, '0'
-
 
148
        jb      error_hostname
-
 
149
        cmp     al, 9
-
 
150
        ja      error_hostname
-
 
151
        lea     ebx, [ebx*4 + ebx]
-
 
152
        shl     ebx, 1
-
 
153
        add     ebx, eax
-
 
154
        jmp     .portloop
-
 
155
 
-
 
156
  .port_done:
-
 
157
        xchg    bl, bh
-
 
158
        mov     [sockaddr1.port], bx
-
 
159
 
97
        mov     byte [esi-1], 0
160
  .done:
98
; Say to the user that we're resolving
161
; Say to the user that we're resolving
99
        invoke  con_write_asciiz, str_resolve
162
        invoke  con_write_asciiz, str_resolve
100
        invoke  con_write_asciiz, buf_cmd
163
        invoke  con_write_asciiz, buf_cmd
101
; resolve name
164
; resolve name
Line 116... Line 179...
116
        invoke  con_write_asciiz, str9          ; ')',10,0
179
        invoke  con_write_asciiz, str9          ; ')',10,0
117
; open the socket
180
; open the socket
118
        mcall   socket, AF_INET4, SOCK_STREAM, 0
181
        mcall   socket, AF_INET4, SOCK_STREAM, 0
119
        cmp     eax, -1
182
        cmp     eax, -1
120
        je      error_socket
183
        je      error_socket
121
        mov     [socketnum], eax
184
        mov     [controlsocket], eax
122
; connect to the server
185
; connect to the server
123
        invoke  con_write_asciiz, str_connect
186
        invoke  con_write_asciiz, str_connect
124
        mcall   connect, [socketnum], sockaddr1, 18
187
        mcall   connect, [controlsocket], sockaddr1, 18
125
        cmp     eax, -1
188
        cmp     eax, -1
126
        je      error_connect
189
        je      error_connect
127
        mov     [status], STATUS_CONNECTING
190
        mov     [status], STATUS_CONNECTING
128
; Tell the user we're waiting for the server now.
191
; Tell the user we're waiting for the server now.
129
        invoke  con_write_asciiz, str_waiting
192
        invoke  con_write_asciiz, str_waiting
Line 149... Line 212...
149
  .receive_loop:
212
  .receive_loop:
150
        mcall   23, 50          ; Wait for event with timeout
213
        mcall   23, 50          ; Wait for event with timeout
151
        mcall   26, 9
214
        mcall   26, 9
152
        cmp     eax, [timeout]
215
        cmp     eax, [timeout]
153
        jge     error_timeout
216
        jge     error_timeout
154
        mcall   recv, [socketnum], buf_buffer1, BUFFERSIZE, MSG_DONTWAIT
217
        mcall   recv, [controlsocket], buf_buffer1, BUFFERSIZE, MSG_DONTWAIT
155
        test    eax, eax
218
        test    eax, eax
156
        jnz     .got_data
219
        jnz     .got_data
157
        cmp     ebx, EWOULDBLOCK
220
        cmp     ebx, EWOULDBLOCK
158
        jne     error_socket
221
        jne     error_socket
159
        jmp     .receive_loop
222
        jmp     .receive_loop
Line 302... Line 365...
302
        xor     al, al
365
        xor     al, al
303
        repne   scasb
366
        repne   scasb
304
        lea     esi, [edi-buf_cmd]
367
        lea     esi, [edi-buf_cmd]
305
        mov     word[edi-2], 0x0a0d
368
        mov     word[edi-2], 0x0a0d
306
; and send it to the server
369
; and send it to the server
307
        mcall   send, [socketnum], buf_cmd, , 0
370
        mcall   send, [controlsocket], buf_cmd, , 0
Line 308... Line 371...
308
 
371
 
309
        invoke  con_write_asciiz, str_newline
372
        invoke  con_write_asciiz, str_newline
310
        invoke  con_set_flags, 0x07     ; reset color
373
        invoke  con_set_flags, 0x07             ; reset color
Line -... Line 374...
-
 
374
        jmp     wait_for_servercommand
-
 
375
 
-
 
376
 
-
 
377
 
-
 
378
; files for rdir operation are queued
-
 
379
transfer_queued:
-
 
380
 
-
 
381
        mov     esi, [ptr_queue]                ; always pointing to current part of ptr_fname_start
-
 
382
        mov     edi, buf_cmd+5                  ; always point to filename for retr command
-
 
383
  .build_filename:
-
 
384
        lodsb   
-
 
385
        stosb
-
 
386
        cmp     al, 10
-
 
387
        je      .get_file                       ; filename ends with character 10
-
 
388
        test    al, al
-
 
389
        jnz     .build_filename
-
 
390
 
-
 
391
        ; Error occured, we reached the end of the buffer before [queued] reached 0
-
 
392
        mov     [queued], 0
-
 
393
        mcall   68, 13, [ptr_fname]             ; free buffer
-
 
394
        test    eax, eax
311
        jmp     wait_for_servercommand
395
        jz      error_heap
312
 
396
        jmp     wait_for_usercommand
-
 
397
 
-
 
398
  .get_file:
-
 
399
        mov     byte[edi], 0                    ; end filename with 0 byte
-
 
400
        mov     [ptr_queue], esi
-
 
401
        dec     [queued]
-
 
402
        jnz     cmd_retr
-
 
403
 
-
 
404
        mcall   68, 13, [ptr_fname]             ; free buffer
-
 
405
        test    eax, eax
-
 
406
        jz      error_heap
-
 
407
        jmp     cmd_retr
-
 
408
 
-
 
409
 
313
 
410
 
Line 314... Line 411...
314
 
411
open_dataconnection:
315
open_dataconnection:                    ; only passive for now..
412
 
Line -... Line 413...
-
 
413
        test    [mode], 1
-
 
414
        jnz     .active
-
 
415
 
-
 
416
        mcall   send, [controlsocket], str_PASV, str_PASV.length, 0
-
 
417
        ret
-
 
418
 
-
 
419
  .active:
-
 
420
        mcall   socket, AF_INET4, SOCK_STREAM, 0
-
 
421
        cmp     eax, -1
-
 
422
        je      error_socket
-
 
423
        mov     [datasocket], eax
-
 
424
 
-
 
425
        mov     ax, [acti_port_start]
-
 
426
        xchg    al, ah
-
 
427
        mov     [sockaddr2.port], ax
-
 
428
 
-
 
429
        mcall   bind, [datasocket], sockaddr2, 18
-
 
430
        cmp     eax, -1
-
 
431
        je      error_socket
-
 
432
 
-
 
433
        mcall   listen, [datasocket], 1
-
 
434
        cmp     eax, -1
316
        cmp     [status], STATUS_LOGGED_IN
435
        je      error_socket
-
 
436
 
-
 
437
        mov     dword[buf_buffer1], 'PORT'
-
 
438
        mov     byte[buf_buffer1+4], ' '
-
 
439
        mov     edi, buf_buffer1+5
-
 
440
        mov     esi, str_active_ip
-
 
441
  .loop:
-
 
442
        lodsb
-
 
443
        test    al, al
-
 
444
        jz      .ip_ok
-
 
445
        stosb
-
 
446
        jmp     .loop
-
 
447
  .ip_ok:
-
 
448
        mov     al, ','
-
 
449
        stosb
317
        jne     .fail
450
        movzx   eax, byte[sockaddr2.port+0]
-
 
451
        call    dword_ascii
-
 
452
        mov     al, ','
-
 
453
        stosb
-
 
454
        movzx   eax, byte[sockaddr2.port+1]
-
 
455
        call    dword_ascii
-
 
456
        mov     ax, 0x0a0d
-
 
457
        stosw
318
 
458
        lea     esi, [edi - buf_buffer1]
319
        mcall   send, [socketnum], str_PASV, str_PASV.length, 0
459
        mcall   send, [controlsocket], buf_buffer1, , 0
320
        ret
460
 
-
 
461
        mcall   accept, [datasocket], sockaddr2, 18        ; time to accept the awaiting connection..
321
 
462
        cmp     eax, -1
-
 
463
        je      error_socket
-
 
464
        push    eax
-
 
465
        mcall   close, [datasocket]
-
 
466
        pop     [datasocket]
-
 
467
 
-
 
468
        mcall   recv, [controlsocket], buf_buffer1, BUFFERSIZE, 0
-
 
469
 
-
 
470
        ret
-
 
471
 
-
 
472
; eax = input
-
 
473
; edi = ptr where to write
-
 
474
dword_ascii:
-
 
475
 
-
 
476
        push    edx ebx ecx
-
 
477
        mov     ebx, 10
-
 
478
        xor     ecx, ecx
-
 
479
 
-
 
480
       @@:
-
 
481
        xor     edx, edx
-
 
482
        div     ebx
-
 
483
        add     edx, '0'
-
 
484
        push    dx
-
 
485
        inc     ecx
-
 
486
        test    eax, eax
-
 
487
        jnz     @r
-
 
488
 
-
 
489
       @@:
322
  .fail:
490
        pop     ax
Line -... Line 491...
-
 
491
        stosb
-
 
492
        dec     ecx
-
 
493
        jnz     @r
-
 
494
 
-
 
495
        pop     ecx ebx edx
323
        invoke  con_get_flags
496
        ret
324
        push    eax
497
 
325
        invoke  con_set_flags, 0x0c                     ; print errors in red
498
error_hostname:
326
        invoke  con_write_asciiz, str_err_socket
499
        invoke  con_set_flags, 0x0c                     ; print errors in red
Line 343... Line 516...
343
        jmp     wait_for_keypress
516
        jmp     wait_for_keypress
Line 344... Line 517...
344
 
517
 
345
error_resolve:
518
error_resolve:
346
        invoke  con_set_flags, 0x0c                     ; print errors in red
519
        invoke  con_set_flags, 0x0c                     ; print errors in red
-
 
520
        invoke  con_write_asciiz, str_err_resolve
Line 347... Line 521...
347
        invoke  con_write_asciiz, str_err_resolve
521
        jmp     wait_for_keypress
348
 
522
 
349
error_heap:
523
error_heap:
Line 350... Line 524...
350
	invoke  con_set_flags, 0x0c                     ; print errors in red
524
        invoke  con_set_flags, 0x0c                     ; print errors in red
351
        invoke  con_write_asciiz, str_err_heap
525
        invoke  con_write_asciiz, str_err_heap
352
	
526
        
353
wait_for_keypress:
527
wait_for_keypress:
354
        invoke  con_set_flags, 0x07                     ; reset color to grey
528
        invoke  con_set_flags, 0x07                     ; reset color to grey
355
        invoke  con_write_asciiz, str_push
529
        invoke  con_write_asciiz, str_push
Line 356... Line 530...
356
        invoke  con_getch2
530
        invoke  con_getch2
357
        mcall   close, [socketnum]
531
        mcall   close, [controlsocket]
Line 358... Line 532...
358
        jmp     main
532
        jmp     main
359
 
533
 
360
done:
534
done:
361
        invoke  con_exit, 1
535
        invoke  con_exit, 1
Line 379... Line 553...
379
str_err_resolve db 10,'Name resolution failed.',10,0
553
str_err_resolve db 10,'Name resolution failed.',10,0
380
str_err_socket  db 10,'Socket error.',10,0
554
str_err_socket  db 10,'Socket error.',10,0
381
str_err_heap	db 10,'Cannot allocate memory from heap.',10,0
555
str_err_heap    db 10,'Cannot allocate memory from heap.',10,0
382
str_err_timeout db 10,'Timeout - no response from server.',10,0
556
str_err_timeout db 10,'Timeout - no response from server.',10,0
383
str_err_connect db 10,'Cannot connect to the server.',10,0
557
str_err_connect db 10,'Cannot connect to the server.',10,0
-
 
558
str_err_host    db 10,'Invalid hostname.',10,0
384
str8            db ' (',0
559
str8            db ' (',0
385
str9            db ')',10,0
560
str9            db ')',10,0
386
str_push        db 'Push any key to continue.',0
561
str_push        db 'Push any key to continue.',0
387
str_connect     db 'Connecting...',10,0
562
str_connect     db 'Connecting...',10,0
388
str_waiting     db 'Waiting for welcome message.',10,0
563
str_waiting     db 'Waiting for welcome message.',10,0
Line 409... Line 584...
409
                db "rmd  - remove directory from the server",10
584
                db "rmd  - remove directory from the server",10
410
                db "stor      - store file on the server",10
585
                db "stor      - store file on the server",10
411
	            db "rdir            - retreive all files from current server dir",10
586
                    db "rdir            - retreive all files from current server dir",10
412
                db 10,0
587
                db 10,0
Line -... Line 588...
-
 
588
 
-
 
589
str_ini         db '.ini', 0
-
 
590
str_active      db 'active', 0
-
 
591
str_port_start  db 'port_start', 0
-
 
592
str_port_stop   db 'port_stop', 0
-
 
593
str_ip          db 'ip', 0
-
 
594
str_dir         db 'dir', 0
-
 
595
str_general     db 'general', 0
413
 
596
 
-
 
597
queued          dd 0
Line 414... Line 598...
414
queued		dd 0
598
mode            db 0    ; passive = 0, active = 1
Line 415... Line 599...
415
 
599
 
416
; FTP strings
600
; FTP strings
Line 417... Line 601...
417
 
601
 
418
str_PASV        db 'PASV',13,10
602
str_PASV        db 'PASV',13,10
419
.length = $ - str_PASV
603
.length = $ - str_PASV
420
 
604
 
421
sockaddr1:
605
sockaddr1:
Line 422... Line 606...
422
        dw AF_INET4
606
        dw AF_INET4
423
.port   dw 0x1500       ; 21
607
.port   dw ?
424
.ip     dd 0
608
.ip     dd ?
425
        rb 10
609
        rb 10
426
 
610
 
Line 427... Line 611...
427
sockaddr2:
611
sockaddr2:
428
        dw AF_INET4
612
        dw AF_INET4
429
.port   dw 0
613
.port   dw ?
Line 430... Line 614...
430
.ip     dd 0
614
.ip     dd ?
Line 431... Line 615...
431
        rb 10
615
        rb 10
432
 
616
 
433
; import
617
; import
434
align 4
618
align 4
Line 452... Line 636...
452
        con_set_cursor_pos, 'con_set_cursor_pos',\
636
        con_set_cursor_pos, 'con_set_cursor_pos',\
453
        con_write_string, 'con_write_string',\
637
        con_write_string, 'con_write_string',\
454
        con_get_flags,  'con_get_flags', \
638
        con_get_flags,  'con_get_flags', \
455
        con_set_flags,  'con_set_flags'
639
        con_set_flags,  'con_set_flags'
Line -... Line 640...
-
 
640
 
-
 
641
import  libini,         \
-
 
642
        ini.get_str,    'ini_get_str',\
-
 
643
        ini.get_int,    'ini_get_int'
Line 456... Line 644...
456
 
644
 
Line 457... Line 645...
457
 
645
 
Line 458... Line 646...
458
i_end:
646
i_end:
459
 
-
 
Line 460... Line 647...
460
; uninitialised data
647
 
461
 
648
; uninitialised data
462
status          db ?
649
 
463
active_passive  db ?
650
status          db ?
464
 
651
 
-
 
652
controlsocket   dd ?
Line -... Line 653...
-
 
653
datasocket      dd ?
465
socketnum       dd ?
654
offset          dd ?
466
datasocket      dd ?
655
size            dd ?
-
 
656
operation       dd ?
-
 
657
timeout         dd ?
-
 
658
 
467
offset          dd ?
659
ptr_fname       dd ?
-
 
660
size_fname      dd ?
468
size            dd ?
661
ptr_queue       dd ?
Line 469... Line 662...
469
operation       dd ?
662
 
470
 
663
acti_port_start dw ?
471
size_fname	dd ?
664
acti_port_stop  dw ?
472
ptr_queue	dd ?
665
acti_port       dw ?
Line 483... Line 676...
483
 
676
 
484
buf_buffer1     rb BUFFERSIZE+1
677
buf_buffer1     rb BUFFERSIZE+1
485
buf_buffer2     rb BUFFERSIZE+1
678
buf_buffer2     rb BUFFERSIZE+1
Line -... Line 679...
-
 
679
buf_cmd         rb 1024                 ; buffer for holding command string
-
 
680
 
486
buf_cmd         rb 1024			; buffer for holding command string
681
path            rb 1024