Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;
  2. ; Kolibrios FTP Daemon
  3. ;
  4. ; hidnplayr@gmail.com
  5. ;
  6. ; GPLv2
  7. ;
  8.  
  9. BUFFERSIZE      equ 4096
  10.  
  11.  
  12. use32
  13.         db      'MENUET01'      ; signature
  14.         dd      1               ; header version
  15.         dd      start           ; entry point
  16.         dd      i_end           ; initialized size
  17.         dd      mem+0x1000      ; required memory
  18.         dd      mem+0x1000      ; stack pointer
  19.         dd      0               ; parameters
  20.         dd      path            ; path
  21.  
  22. include '../macros.inc'
  23. purge mov,add,sub
  24. include '../proc32.inc'
  25. include '../dll.inc'
  26.  
  27. include '../network.inc'
  28. include 'commands.inc'
  29.  
  30. align 4
  31. start:
  32. ; load libraries
  33.         stdcall dll.Load, @IMPORT
  34.         test    eax, eax
  35.         jnz     exit
  36.  
  37. ; find path to main settings file
  38.         mov     edi, path      ; Calculate the length of zero-terminated string
  39.         xor     al , al
  40.         mov     ecx, 1024
  41.         repne   scasb
  42.         dec     edi
  43.         mov     esi, filename
  44.         movsd
  45.         movsb
  46.  
  47. ; initialize console
  48.         push    1
  49.         call    [con_start]
  50.         push    title
  51.         push    25
  52.         push    80
  53.         push    25
  54.         push    80
  55.         call    [con_init]
  56.  
  57.         mcall   40, 1 shl 7     ; we only want network events
  58.  
  59.         push    str1
  60.         call    [con_write_asciiz]
  61.  
  62.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  63.         cmp     eax, -1
  64.         je      sock_err
  65.  
  66.         mov     [socketnum], eax
  67.  
  68. ;;        mcall   setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
  69. ;;        cmp     eax, -1
  70. ;;        je      opt_err
  71.  
  72.         invoke  ini.get_int, path, str_ftpd, str_port, 21
  73.         mov     [sockaddr1.port], ax
  74.  
  75.         mcall   bind, [socketnum], sockaddr1, sockaddr1.length
  76.         cmp     eax, -1
  77.         je      bind_err
  78.  
  79.         invoke  ini.get_int, path, str_ftpd, str_conn, 1        ; Backlog (max connections)
  80.         mov     edx, eax
  81.         mcall   listen, [socketnum]
  82.         cmp     eax, -1
  83.         je      listen_err
  84.  
  85.         push    str2
  86.         call    [con_write_asciiz]
  87.  
  88.         mcall   10
  89.  
  90.         mcall   accept, [socketnum], sockaddr1, sockaddr1.length
  91.         cmp     eax, -1
  92.         je      acpt_err
  93.  
  94.         mov     [socketnum2], eax
  95.  
  96. ;;        mcall   close, [socketnum]
  97.  
  98.         mcall   send, [socketnum2], str220, str220.length       ; send welcome string
  99.  
  100.   .loop:
  101.         mcall   10
  102.  
  103.         mcall   recv, [socketnum2], buffer, buffer.length
  104.  
  105.         push    buffer
  106.         call    [con_write_asciiz]
  107.  
  108.         mov     esi, buffer
  109.         call    parse_cmd
  110.  
  111.         jmp     .loop
  112.  
  113. acpt_err:
  114.         push    str8
  115.         call    [con_write_asciiz]
  116.         jmp     done
  117.  
  118. listen_err:
  119.         push    str3
  120.         call    [con_write_asciiz]
  121.         jmp     done
  122.  
  123. bind_err:
  124.         push    str4
  125.         call    [con_write_asciiz]
  126.         jmp     done
  127.  
  128. sock_err:
  129.         push    str6
  130.         call    [con_write_asciiz]
  131.         jmp     done
  132.  
  133. done:
  134.         call    [con_getch2]
  135.         push    1
  136.         call    [con_exit]
  137. exit:
  138.         mcall   -1
  139.  
  140.  
  141.  
  142. ; data
  143. title   db      'KolibriOS FTP daemon 1.0',0
  144. str1    db      'Opening socket',10, 0
  145. str2    db      'Listening for incoming connections...',10,0
  146. str3    db      'Listen error',10,10,0
  147. str4    db      'Bind error',10,10,0
  148. str5    db      'Setsockopt error.',10,10,0
  149. str6    db      'Could not open socket',10,10,0
  150. str7    db      'Got data!',10,10,0
  151. str8    db      'Error accepting connection',10,10,0
  152.  
  153. filename db '.ini', 0
  154. str_port db 'port', 0
  155. str_ftpd db 'ftpd', 0
  156. str_conn db 'conn', 0
  157.  
  158. sockaddr1:
  159.         dw AF_INET4
  160. .port   dw 21
  161. .ip     dd 0
  162.         rb 10
  163. .length = $ - sockaddr1
  164.  
  165. ; import
  166. align 4
  167. @IMPORT:
  168.  
  169. library console, 'console.obj', \
  170.         libini, 'libini.obj', \
  171.         libio, 'libio.obj'
  172.  
  173. import  console,        \
  174.         con_start,      'START',        \
  175.         con_init,       'con_init',     \
  176.         con_write_asciiz,       'con_write_asciiz',     \
  177.         con_exit,       'con_exit',     \
  178.         con_gets,       'con_gets',\
  179.         con_cls,        'con_cls',\
  180.         con_printf,     'con_printf',\
  181.         con_getch2,     'con_getch2',\
  182.         con_set_cursor_pos, 'con_set_cursor_pos'
  183.  
  184. import  libini,         \
  185.         ini.get_str,    'ini_get_str',\
  186.         ini.get_int,    'ini_get_int'
  187.  
  188. import  libio,          \
  189.         libio.init , 'lib_init'   , \
  190.         file.size  , 'file_size'  , \
  191.         file.open  , 'file_open'  , \
  192.         file.read  , 'file_read'  , \
  193.         file.close , 'file_close'
  194.  
  195.  
  196. i_end:
  197.  
  198. socketnum       dd ?
  199. socketnum2      dd ?
  200.  
  201. buffer          rb BUFFERSIZE
  202. .length = BUFFERSIZE
  203.  
  204. path    rb 1024
  205. mem:
  206.