Subversion Repositories Kolibri OS

Rev

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

Rev 2571 Rev 2578
Line 36... Line 36...
36
        dd      1               ; header version
36
        dd      1               ; header version
37
        dd      start           ; entry point
37
        dd      start           ; entry point
38
        dd      i_end           ; initialized size
38
        dd      i_end           ; initialized size
39
        dd      mem+0x1000      ; required memory
39
        dd      mem+0x1000      ; required memory
40
        dd      mem+0x1000      ; stack pointer
40
        dd      mem+0x1000      ; stack pointer
41
        dd      0               ; parameters
41
        dd      params          ; parameters
42
        dd      path            ; path
42
        dd      path            ; path
Line 43... Line 43...
43
 
43
 
44
include '../macros.inc'
44
include '../macros.inc'
45
purge mov,add,sub
45
purge mov,add,sub
Line 56... Line 56...
56
; load libraries
56
; load libraries
57
        stdcall dll.Load, @IMPORT
57
        stdcall dll.Load, @IMPORT
58
        test    eax, eax
58
        test    eax, eax
59
        jnz     exit
59
        jnz     exit
Line -... Line 60...
-
 
60
 
-
 
61
        mcall   68, 11                  ; init heap
60
 
62
 
61
; find path to main settings file
63
; find path to main settings file
62
        mov     edi, path      ; Calculate the length of zero-terminated string
64
        mov     edi, path               ; Calculate the length of zero-terminated string
63
        xor     al , al
65
        xor     al , al
64
        mov     ecx, 1024
66
        mov     ecx, 1024
65
        repne   scasb
67
        repne   scasb
66
        dec     edi
68
        dec     edi
67
        mov     esi, filename
69
        mov     esi, filename           ; append it with '.ini'
68
        movsd
70
        movsd
Line 69... Line 71...
69
        movsb
71
        movsb
70
 
72
 
Line 119... Line 121...
119
        je      listen_err
121
        je      listen_err
Line 120... Line 122...
120
 
122
 
121
        push    str2b
123
        push    str2b
Line -... Line 124...
-
 
124
        call    [con_write_asciiz]
-
 
125
 
-
 
126
mainloop:
-
 
127
        mcall   10                              ; Wait here for incoming connections on the base socket (socketnum)
-
 
128
 
122
        call    [con_write_asciiz]
129
        mcall   51, 1, threadstart, 0           ; Start a new thread for every incoming connection
Line -... Line 130...
-
 
130
                                                ; NOTE: upon initialisation of the thread, stack will not be available!
123
 
131
        jmp     mainloop
124
        mcall   10
132
 
125
 
133
threadstart:
-
 
134
        mcall   68, 12, sizeof.thread_data      ; allocate the thread data struct
-
 
135
        cmp     eax, -1
-
 
136
        je      exit
-
 
137
 
-
 
138
        lea     esp, [eax + thread_data.stack]  ; init stack
-
 
139
        push    eax                             ; save pointer to thread_data on stack
-
 
140
 
-
 
141
        mcall   40, 1 shl 7                     ; we only want network events for this thread
Line -... Line 142...
-
 
142
 
-
 
143
        push    str8
-
 
144
        call    [con_write_asciiz]                                              ; print on the console that we have created the new thread successfully
-
 
145
 
126
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length
146
        mcall   accept, [socketnum], sockaddr1, sockaddr1.length                ; time to accept the awaiting connection..
Line 127... Line 147...
127
        cmp     eax, -1
147
        cmp     eax, -1
Line 128... Line 148...
128
        je      acpt_err
148
        je      thread_exit
129
 
149
        mov     edx, [esp]                                                      ; pointer to thread_data
Line -... Line 150...
-
 
150
        mov     [edx + thread_data.socketnum], eax
-
 
151
 
130
        mov     [socketnum2], eax
152
        mcall   send, [edx + thread_data.socketnum], str220, str220.length, 0   ; send welcome string to the FTP client
131
 
153
 
-
 
154
threadloop:
-
 
155
        mcall   10
132
        mcall   send, [socketnum2], str220, str220.length, 0    ; send welcome string
156
 
-
 
157
        mov     edx, [esp]                                                      ; pointer to thread_data
-
 
158
 
133
 
159
        cmp     [edx + thread_data.mode], MODE_PASSIVE_WAIT
134
  .loop:
160
        jne     @f
135
        mcall   10
161
        mov     ecx, [edx + thread_data.passivesocknum]
136
 
162
        lea     edx, [edx + thread_data.datasock]
Line 137... Line 163...
137
        cmp     [mode], MODE_PASSIVE_WAIT
163
        mov     esi, sizeof.thread_data.datasock
138
        jne     @f
164
        mcall   accept
139
        mcall   accept, [passivesocknum], datasock, datasock.length
165
        mov     edx, [esp]                                                      ; pointer to thread_data
Line 140... Line 166...
140
        cmp     eax, -1
166
        cmp     eax, -1
141
        je      @f
167
        je      @f
142
        mov     [datasocketnum], eax
168
        mov     [edx + thread_data.datasocketnum], eax
143
        mov     [mode], MODE_PASSIVE_OK
169
        mov     [edx + thread_data.mode], MODE_PASSIVE_OK
-
 
170
 
144
 
171
        push    str_datasock
-
 
172
        call    [con_write_asciiz]                                              ; print on the console that the datasock is now ready
145
        push    str_datasock
173
       @@:
-
 
174
 
Line -... Line 175...
-
 
175
        mov     ecx, [edx + thread_data.socketnum]
146
        call    [con_write_asciiz]
176
        lea     edx, [edx + thread_data.buffer]
Line 147... Line 177...
147
       @@:
177
        mov     esi, sizeof.thread_data.buffer
148
 
178
        mcall   recv
-
 
179
        cmp     eax, -1                                                         ; error?
149
        mcall   recv, [socketnum2], buffer, buffer.length
180
        je      threadloop
150
        cmp     eax, -1
181
        or      eax, eax                                                        ; 0 bytes read?
151
        je      .loop
182
        jz      threadloop
152
        or      eax, eax
183
        push    eax                                                             ; save number of bytes read on stack
Line 153... Line 184...
153
        jz      .loop
184
 
154
        push    eax
185
        mov     edx, [esp+4]                                                    ; pointer to thread_data
155
 
186
        mov     byte [edx + thread_data.buffer + eax], 0                        ; append received data with a 0 byte
Line 156... Line 187...
156
        mov     byte[buffer+eax], 0
187
 
157
 
-
 
158
        pushd   0x0a
-
 
159
        call    [con_set_flags]
-
 
160
        push    buffer
-
 
161
        call    [con_write_asciiz]
-
 
162
        pushd   0x07
-
 
163
        call    [con_set_flags]
-
 
164
 
-
 
165
        pop     ecx
-
 
Line 166... Line 188...
166
        mov     esi, buffer
188
        pushd   0x0a                                                            ; print received data to console (in green color)
167
        call    parse_cmd
-
 
168
 
189
        call    [con_set_flags]
169
        jmp     .loop
190
        lea     eax, [edx + thread_data.buffer]
170
 
-
 
171
acpt_err:
191
        push    eax
172
 
192
        call    [con_write_asciiz]
173
        pushd   0x0c
193
        pushd   0x07
Line 174... Line 194...
174
        call    [con_set_flags]
194
        call    [con_set_flags]
175
 
-
 
176
        push    str8
195
 
177
        call    [con_write_asciiz]
196
        pop     ecx                                                             ; number of bytes read
178
        jmp     done
-
 
179
 
197
        lea     esi, [edx + thread_data.buffer]
180
listen_err:
198
        call    parse_cmd
181
 
199
 
Line 182... Line 200...
182
        pushd   0x0c
200
        jmp     threadloop
183
        call    [con_set_flags]
-
 
184
 
201
 
185
        push    str3
202
listen_err:
186
        call    [con_write_asciiz]
-
 
187
        jmp     done
203
        pushd   0x0c
188
 
204
        call    [con_set_flags]
189
bind_err:
205
        push    str3
Line 190... Line 206...
190
 
206
        call    [con_write_asciiz]
Line 210... Line 226...
210
        call    [con_exit]
226
        call    [con_exit]
211
exit:
227
exit:
212
        mcall   -1
228
        mcall   -1
Line -... Line 229...
-
 
229
 
-
 
230
 
-
 
231
thread_exit:
-
 
232
        push    str_bye
-
 
233
        call    [con_write_asciiz]      ; say bye bye
-
 
234
        pop     ecx                     ; get the thread_data pointer from stack
-
 
235
        mcall   68, 13                  ; free the memory
-
 
236
        mcall   -1                      ; and kill the thread
-
 
237
 
-
 
238
 
Line 213... Line -...
213
 
-
 
214
 
239
 
215
 
240
; initialized data
216
; data
241
 
217
title   db      'KolibriOS FTP daemon 0.1', 0
242
title           db 'KolibriOS FTP daemon 0.1', 0
218
str1    db      'Starting FTP daemon on port %u', 0
243
str1            db 'Starting FTP daemon on port %u', 0
219
str2    db      '.', 0
244
str2            db '.', 0
220
str2b   db      ' OK!',10,10,0
245
str2b           db ' OK!',10,10,0
221
str3    db      'Listen error',10,10,0
246
str3            db 'Listen error',10,10,0
222
str4    db      'Bind error',10,10,0
247
str4            db 'Bind error',10,10,0
223
str5    db      'Setsockopt error.',10,10,0
248
;str5            db 'Setsockopt error.',10,10,0
-
 
249
str6            db 'Could not open socket',10,10,0
Line 224... Line 250...
224
str6    db      'Could not open socket',10,10,0
250
str7            db 'Got data!',10,10,0
225
str7    db      'Got data!',10,10,0
251
str8            db 10,'New thread created!',10,10,0
226
str8    db      'Error accepting connection',10,10,0
252
str_bye         db 10,'Closing thread!',10,10,0
227
 
253
 
228
str_logged_in   db 'Login ok',10,10,0
254
str_logged_in   db 'Login ok',10,10,0
229
str_pass_ok     db 'Password ok - Logged in',10,10,0
255
str_pass_ok     db 'Password ok - Logged in',10,10,0
230
str_pwd         db 'Current directory is "%s"\n',0
256
str_pwd         db 'Current directory is "%s"\n',0
Line 231... Line 257...
231
str_err2        db 'ERROR: cannot open directory',10,10,0
257
str_err2        db 'ERROR: cannot open directory',10,10,0
232
str_datasock    db 'Passive data socket connected!',10,10,0
-
 
233
str_notfound    db 'ERROR: file not found',10,10,0
-
 
234
str_sockerr     db 'ERROR: socket error',10,10,0
258
str_datasock    db 'Passive data socket connected!',10,10,0
Line 235... Line -...
235
 
-
 
236
str_newline     db 10,0
259
str_notfound    db 'ERROR: file not found',10,10,0
-
 
260
str_sockerr     db 'ERROR: socket error',10,10,0
-
 
261
 
-
 
262
str_newline     db 10, 0
237
 
263
str_mask        db '*', 0
-
 
264
 
-
 
265
months          dd 'Jan '
-
 
266
                dd 'Feb '
-
 
267
                dd 'Mar '
-
 
268
                dd 'Apr '
-
 
269
                dd 'May '
238
 
270
                dd 'Jun '
Line 239... Line 271...
239
str_mask        db '*', 0
271
                dd 'Jul '
240
 
272
                dd 'Aug '
241
 
273
                dd 'Sep '
242
months:
274
                dd 'Oct '
Line 254... Line 286...
254
.ip     dd 0
286
  .ip           dd 0
255
        rb 10
287
                rb 10
256
.length = $ - sockaddr1
288
  .length       = $ - sockaddr1
Line 257... Line 289...
257
 
289
 
-
 
290
; import
258
; import
291
 
259
align 4
292
align 4
Line 260... Line 293...
260
@IMPORT:
293
@IMPORT:
261
 
294
 
Line 290... Line 323...
290
        file.find.close , 'file_find_close'
323
        file.find.close,        'file_find_close'
Line 291... Line 324...
291
 
324
 
Line 292... Line -...
292
 
-
 
293
i_end:
-
 
294
 
-
 
295
socketnum       dd ?
325
 
296
 
-
 
297
 
-
 
298
; thread specific data
-
 
299
socketnum2      dd ?
-
 
300
state           dd ?
-
 
301
home_dir        db '/rd/1/', 0
-
 
Line 302... Line 326...
302
                rb 1024
326
i_end:
303
work_dir        rb 1024
327
 
304
fpath           rb 1024*3
-
 
305
 
-
 
306
type            db ?
328
; uninitialised data
Line 307... Line 329...
307
mode            db ?    ; active/passive
329
 
308
 
-
 
309
passivesocknum  dd ?
-
 
310
datasocketnum   dd ?
-
 
311
 
-
 
312
datasock:
-
 
Line 313... Line -...
313
        dw AF_INET4
-
 
314
.port   dw ?
-
 
Line 315... Line -...
315
.ip     dd ?
-
 
316
        rb 10
-