Subversion Repositories Kolibri OS

Rev

Rev 2554 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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