Subversion Repositories Kolibri OS

Rev

Rev 3804 | Rev 3814 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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              = 4096
  18.  
  19. STATUS_CONNECTING       = 0
  20. STATUS_CONNECTED        = 1
  21. STATUS_NEEDPASSWORD     = 2
  22. STATUS_LOGGED_IN        = 3
  23.  
  24. OPERATION_LIST          = 0
  25. OPERATION_RETR          = 1
  26. OPERATION_STOR          = 2
  27.  
  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:
  49. ; disable all events
  50.         mcall   40, 0
  51. ; load libraries
  52.         stdcall dll.Load, @IMPORT
  53.         test    eax, eax
  54.         jnz     exit
  55. ; initialize console
  56.         invoke  con_start, 1
  57.         invoke  con_init, 80, 25, 80, 250, str_title
  58. ; Check for parameters, if there are some, resolve the address right away
  59.         cmp     byte [s], 0
  60.         jne     resolve
  61.  
  62. main:
  63. ; Clear screen
  64.         invoke  con_cls
  65. ; Welcome user
  66.         invoke  con_write_asciiz, str_welcome
  67. ; write prompt (in green color)
  68.         invoke  con_set_flags, 0x0a
  69.         invoke  con_write_asciiz, str_prompt
  70. ; read string
  71.         invoke  con_gets, s, 256
  72. ; check for exit
  73.         test    eax, eax
  74.         jz      done
  75.         cmp     byte [s], 10
  76.         jz      done
  77. ; reset color back to grey and print newline
  78.         invoke  con_set_flags, 0x07
  79.         invoke  con_write_asciiz, str_newline
  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
  89. ; Say to the user that we're resolving
  90.         invoke  con_write_asciiz, str_resolve
  91.         invoke  con_write_asciiz, s
  92. ; resolve name
  93.         push    esp     ; reserve stack place
  94.         invoke  getaddrinfo, s, 0, 0, esp
  95.         pop     esi
  96. ; test for error
  97.         test    eax, eax
  98.         jnz     error_resolve
  99. ; write results
  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
  109.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  110.         cmp     eax, -1
  111.         je      error_socket
  112.         mov     [socketnum], eax
  113. ; connect to the server
  114.         invoke  con_write_asciiz, str_connect
  115.         mcall   connect, [socketnum], sockaddr1, 18
  116.         mov     [status], STATUS_CONNECTING
  117. ; Tell the user we're waiting for the server now.
  118.         invoke  con_write_asciiz, str_waiting
  119.  
  120. ; Reset 'offset' variable, it's used by the data receiver
  121.         mov     [offset], 0
  122.  
  123. wait_for_servercommand:
  124. ; Any commands still in our buffer?
  125.         cmp     [offset], 0
  126.         je      .receive                        ; nope, receive some more
  127.         mov     esi, [offset]
  128.         mov     edi, s
  129.         mov     ecx, [size]
  130.         add     ecx, esi
  131.         jmp     .byteloop
  132.  
  133. ; receive socket data
  134.   .receive:
  135.         mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
  136.         inc     eax
  137.         jz      error_socket
  138.         dec     eax
  139.         jz      wait_for_servercommand
  140.  
  141.         mov     [offset], 0
  142.  
  143. ; extract commands, copy them to "s" buffer
  144.         lea     ecx, [eax + buffer_ptr]         ; ecx = end pointer
  145.         mov     esi, buffer_ptr                 ; esi = current pointer
  146.         mov     edi, s
  147.   .byteloop:
  148.         cmp     esi, ecx
  149.         jae     wait_for_servercommand
  150.         lodsb
  151.         cmp     al, 10                          ; excellent, we might have a command
  152.         je      .got_command
  153.         cmp     al, 13                          ; just ignore this byte
  154.         je      .byteloop
  155.         stosb
  156.         jmp     .byteloop
  157.   .got_command:                                 ; we have a newline check if its a command
  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:
  167.         xor     al, al
  168.         stosb
  169.  
  170.         invoke  con_set_flags, 0x03             ; change color
  171.         invoke  con_write_asciiz, s             ; print servercommand
  172.         invoke  con_write_asciiz, str_newline
  173.         invoke  con_set_flags, 0x07             ; reset color
  174.  
  175.         jmp     server_parser                   ; parse command
  176.  
  177.  
  178.  
  179. wait_for_usercommand:
  180.  
  181. ; change color to green for user input
  182.         invoke  con_set_flags, 0x0a
  183.  
  184. ; If we are not yet connected, request username/password
  185.         cmp     [status], STATUS_CONNECTED
  186.         je      .connected
  187.  
  188.         cmp     [status], STATUS_NEEDPASSWORD
  189.         je      .needpass
  190.  
  191. ; write prompt
  192.         invoke  con_write_asciiz, str_prompt
  193. ; read string
  194.         invoke  con_gets, s, 256
  195.  
  196. ; print a newline and reset the color back to grey
  197.         invoke  con_write_asciiz, str_newline
  198.         invoke  con_set_flags, 0x07
  199.  
  200.         cmp     dword[s], "cwd "
  201.         je      cmd_cwd
  202.  
  203.         cmp     dword[s], "mkd "
  204.         je      cmd_mkd
  205.  
  206.         cmp     dword[s], "rmd "
  207.         je      cmd_rmd
  208.  
  209.         cmp     dword[s], "pwd" + 10 shl 24
  210.         je      cmd_pwd
  211.  
  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.  
  224.         cmp     dword[s], "stor"
  225.         je      cmd_stor
  226.  
  227.         cmp     dword[s], "dele"
  228.         je      cmd_dele
  229.  
  230.   @@:
  231.         cmp     byte[s+4], 10
  232.         jne     @f
  233.  
  234.         cmp     dword[s], "list"
  235.         je      cmd_list
  236.  
  237.         cmp     dword[s], "help"
  238.         je      cmd_help
  239.  
  240.         cmp     dword[s], "cdup"
  241.         je      cmd_cdup
  242.  
  243.   @@:
  244. ; Uh oh.. unknown command, tell the user and wait for new input
  245.         invoke  con_write_asciiz, str_unknown
  246.         jmp     wait_for_usercommand
  247.  
  248.  
  249.   .connected:
  250. ; request username
  251.         invoke  con_write_asciiz, str_user
  252.         mov     dword[s], "USER"
  253.         mov     byte[s+4], " "
  254.         jmp     .send
  255.  
  256.  
  257.   .needpass:
  258. ; request password
  259.         invoke  con_write_asciiz, str_pass
  260.         mov     dword[s], "PASS"
  261.         mov     byte[s+4], " "
  262.         invoke  con_set_flags, 0x00     ; black text on black background for password
  263.  
  264.   .send:
  265. ; read string
  266.         mov     esi, s+5
  267.         invoke  con_gets, esi, 256
  268.  
  269. ; find end of string
  270.         mov     edi, s+5
  271.         mov     ecx, 256
  272.         xor     al, al
  273.         repne   scasb
  274.         lea     esi, [edi-s-1]
  275. ; and send it to the server
  276.         mcall   send, [socketnum], s, , 0
  277.  
  278.         invoke  con_write_asciiz, str_newline
  279.         invoke  con_set_flags, 0x07     ; reset color
  280.         jmp     wait_for_servercommand
  281.  
  282.  
  283.  
  284. open_dataconnection:                    ; only passive for now..
  285.         cmp     [status], STATUS_LOGGED_IN
  286.         jne     .fail
  287.  
  288.         mov     dword[s], "PASV"
  289.         mov     byte[s+4], 10
  290.         mcall   send, [socketnum], s, 5, 0
  291.         ret
  292.  
  293.   .fail:
  294.         invoke  con_write_asciiz, str_err_socket
  295.         ret
  296.  
  297.  
  298.  
  299. error_socket:
  300.         invoke  con_write_asciiz, str_err_socket
  301.         jmp     wait_for_keypress
  302.  
  303. error_resolve:
  304.         invoke  con_write_asciiz, str_err_resolve
  305.  
  306. wait_for_keypress:
  307.         invoke  con_write_asciiz, str_push
  308.         invoke  con_getch2
  309.         jmp     main
  310.  
  311. done:
  312.         invoke  con_exit, 1
  313.  
  314. exit:
  315.         mcall   close, [socketnum]
  316.         mcall   -1
  317.  
  318.  
  319.  
  320. ; data
  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
  325.  
  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
  340.  
  341. str_open        db "opening data socket",10,0
  342.  
  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 <directory> - change working directoy on the server",10
  348.                 db "dele <file>     - delete file from the server",10
  349.                 db "list            - list files and folders in current server directory",10
  350.                 db "lcwd <path>     - change local working directory",10
  351.                 db "mkd <directory> - make directory on the server",10
  352.                 db "pwd             - print server working directory",10
  353.                 db "retr <file>     - retreive file from the server",10
  354.                 db "rmd <directory> - remove directory from the server",10
  355.                 db "stor <file>     - store file on the server",10
  356.                 db 10,0
  357.  
  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',\
  391.         con_get_flags,  'con_get_flags', \
  392.         con_set_flags,  'con_set_flags'
  393.  
  394.  
  395. i_end:
  396.  
  397. ; uninitialised data
  398.  
  399. status          db ?
  400. active_passive  db ?
  401.  
  402. socketnum       dd ?
  403. datasocket      dd ?
  404. offset          dd ?
  405. size            dd ?
  406. operation       dd ?
  407.  
  408. filestruct:
  409. .subfn  dd ?
  410. .offset dd ?
  411.         dd ?
  412. .size   dd ?
  413. .ptr    dd ?
  414. .name   rb 1024
  415.  
  416. buffer_ptr      rb BUFFERSIZE+1
  417. buffer_ptr2     rb BUFFERSIZE+1
  418.  
  419. s               rb 1024
  420.  
  421. mem:
  422.