Subversion Repositories Kolibri OS

Rev

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

  1. server_parser:
  2.  
  3. ; Commands are always 3 numbers and followed by a space
  4. ; If a server decides it needs multiline output,
  5. ; first lines will have a dash instead of space after numbers,
  6. ; thus they are simply ignored in this simple command parser.
  7.  
  8.         cmp     dword[buf_cmd], "150 "
  9.         je      data_loop
  10.  
  11.         cmp     dword[buf_cmd], "220 "
  12.         je      welcome
  13.  
  14. ;        cmp     dword[buf_cmd], "226 "
  15. ;        je      transfer_ok
  16.  
  17.         cmp     dword[buf_cmd], "227 "
  18.         je      pasv_ok
  19.  
  20.         cmp     dword[buf_cmd], "230 "
  21.         je      login_ok
  22.  
  23. ;        cmp     dword[buf_cmd], "250"
  24. ;        je      op_ok
  25.  
  26.         cmp     dword[buf_cmd], "331 "
  27.         je      pass
  28.  
  29. ;        cmp     dword[buf_cmd], "421 "
  30. ;        je      timeout
  31.  
  32.         cmp     dword[buf_cmd], "503 "         ; login first
  33.         je      welcome
  34.  
  35.         cmp     dword[buf_cmd], "530 "         ; password incorrect
  36.         mov     [use_params], 0
  37.         je      welcome
  38.  
  39.         cmp     dword[buf_cmd], "550 "
  40.         je      close_datacon
  41.  
  42.         cmp     byte[buf_cmd+3], "-"
  43.         je      wait_for_servercommand
  44.         jmp     wait_for_usercommand
  45.  
  46.  
  47. welcome:
  48.  
  49.         mov     [status], STATUS_CONNECTED
  50.         jmp     wait_for_usercommand
  51.  
  52.  
  53. pass:
  54.  
  55.         mov     [status], STATUS_NEEDPASSWORD
  56.         jmp     wait_for_usercommand
  57.  
  58.  
  59. login_ok:
  60.  
  61.         mov     [status], STATUS_LOGGED_IN
  62.         cmp     [use_params], 0
  63.         je      wait_for_usercommand
  64.  
  65.         cmp     [param_path+4], 0
  66.         je      wait_for_usercommand
  67.         ; copy path to buf_cmd and execute CWD
  68.         mov     edi, buf_cmd
  69.         mov     esi, param_path
  70.   @@:
  71.         lodsb
  72.         stosb
  73.         cmp     byte[esi-1], 0
  74.         jne     @b
  75.         jmp     cmd_cwd
  76.  
  77. pasv_ok:
  78.  
  79.         sub     ecx, 4
  80.         jb      .fail
  81.         mov     al, "("
  82.         mov     edi, buf_cmd + 4
  83.         repne   scasb
  84.  
  85.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  86.         cmp     eax, -1
  87.         je      error_socket
  88.         mov     [datasocket], eax
  89.  
  90.         mov     esi, edi
  91.         call    ascii_dec
  92.         mov     byte[sockaddr2.ip+0], bl
  93.         call    ascii_dec
  94.         mov     byte[sockaddr2.ip+1], bl
  95.         call    ascii_dec
  96.         mov     byte[sockaddr2.ip+2], bl
  97.         call    ascii_dec
  98.         mov     byte[sockaddr2.ip+3], bl
  99.  
  100.         call    ascii_dec
  101.         mov     byte[sockaddr2.port+0], bl
  102.         call    ascii_dec
  103.         mov     byte[sockaddr2.port+1], bl
  104.  
  105.         invoke  con_write_asciiz, str_open
  106.         mcall   connect, [datasocket], sockaddr2, 18
  107.         cmp     eax, -1
  108.         je      error_socket
  109.         jmp     wait_for_servercommand
  110.  
  111.   .fail:
  112.         invoke  con_write_asciiz, str_unknown
  113.         jmp     wait_for_servercommand
  114.  
  115.  
  116. data_loop:
  117.  
  118.         invoke  con_write_asciiz, str_dot
  119.  
  120.         cmp     [operation], OPERATION_STOR
  121.         je      .stor
  122.  
  123. ; we are receiving data
  124.         mcall   recv, [datasocket], buf_buffer2, BUFFERSIZE, 0
  125.         test    ebx, ebx
  126.         jnz     .done
  127.         mov     byte[buf_buffer2 + eax], 0
  128.  
  129.         cmp     [operation], OPERATION_RETR
  130.         je      .retr
  131.  
  132.         cmp     [operation], OPERATION_RDIR
  133.         je      .rdir
  134.        
  135. ; not retreiving, just print to console
  136.         invoke  con_write_asciiz, buf_buffer2
  137.         jmp     data_loop
  138.  
  139. ; retreiving, save to file
  140.   .retr:
  141.         mov     [filestruct.ptr], buf_buffer2
  142.         mov     [filestruct.size], eax
  143.         push    eax
  144.         mcall   70, filestruct
  145.         pop     eax
  146.         add     [filestruct.offset], eax
  147.         jmp     data_loop
  148.  
  149. ; storing, send all data
  150.   .stor:
  151.         mcall   70, filestruct
  152.         cmp     eax, 6          ; end of file
  153.         je      .last_call
  154.         test    eax, eax        ; error
  155. ;        jne     .fileerror
  156.         add     [filestruct.offset], ebx
  157.         mov     esi, ebx
  158.         mcall   send, [datasocket], buf_buffer2, , 0
  159.         jmp     .stor
  160.  
  161.   .last_call:
  162.         mov     esi, ebx
  163.         mcall   send, [datasocket], buf_buffer2, , 0
  164.  
  165.   .done:
  166.         invoke  con_write_asciiz, str_close
  167.         mcall   close, [datasocket]
  168.         mov     [operation], OPERATION_NONE
  169.         jmp     wait_for_servercommand
  170.  
  171.   .rdir:
  172.         ; alloc/realloc memory block to store filenames
  173.         mov     ecx, eax                        ; eax is size of buffer received
  174.         inc     ecx
  175.         add     ecx, [size_fname]               ; added old size to form new required size
  176.         mcall   68, 20, , [ptr_fname]           ; realloc
  177.         test    eax, eax
  178.         je      error_heap
  179.         mov     [ptr_fname], eax                ; eax contains the new block now
  180.         mov     [ptr_queue], eax
  181.  
  182.         ; copy filenames into fname buffer
  183.         mov     esi, buf_buffer2
  184.         mov     edi, eax
  185.         add     edi, [size_fname]
  186.   .copy_buf:
  187.         lodsb
  188.         cmp     al, 13                          ; ignore any carriage return character
  189.         je      .copy_buf
  190.         stosb
  191.         cmp     al, 10                          ; linefeed marks end of filename
  192.         je      @f
  193.         inc     [queued]
  194.   @@:
  195.         test    al, al                          ; 0 marks end of buffer
  196.         jne     .copy_buf
  197.  
  198.         ; All received filenames have been copied, calculate new size of fname buffer
  199.         dec     edi                             ; dont count the trailing 0 byte
  200.         sub     edi, [ptr_fname]
  201.         mov     [size_fname], edi      
  202.         jmp     data_loop
  203.  
  204.  
  205. close_datacon:
  206.         cmp     [operation], OPERATION_NONE
  207.         je      wait_for_usercommand
  208.         invoke  con_write_asciiz, str_close
  209.         mcall   close, [datasocket]
  210.         jmp     wait_for_usercommand
  211.  
  212.  
  213. ascii_dec:
  214.  
  215.         xor     ebx, ebx
  216.         mov     cl, 4                   ; max length is 3 digits + 1 separator
  217.   .loop:
  218.         lodsb
  219.         sub     al, '0'
  220.         jb      .done
  221.         cmp     al, 9
  222.         ja      .done
  223.         lea     ebx, [ebx*4+ebx]        ; ebx *5
  224.         shl     ebx, 1                  ; ebx *2
  225.         add     bl, al
  226.         dec     cl
  227.         jnz     .loop
  228.  
  229.   .done:
  230.         ret