Subversion Repositories Kolibri OS

Rev

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

Rev 3704 Rev 3789
Line 12... Line 12...
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 14... Line 14...
14
 
14
 
Line 15... Line -...
15
format binary as ""
-
 
16
 
-
 
17
__DEBUG__               = 0
15
format binary as ""
Line 18... Line 16...
18
__DEBUG_LEVEL__         = 1
16
 
19
BUFFERSIZE              = 1024
17
BUFFERSIZE              = 1024
20
 
18
 
Line 36... Line 34...
36
 
34
 
37
include '../../macros.inc'
35
include '../../macros.inc'
38
purge mov,add,sub
36
purge mov,add,sub
39
include '../../proc32.inc'
37
include '../../proc32.inc'
40
include '../../dll.inc'
-
 
41
include '../../debug-fdo.inc'
38
include '../../dll.inc'
Line 42... Line 39...
42
include '../../network.inc'
39
include '../../network.inc'
43
 
40
 
Line 44... Line 41...
44
include 'usercommands.inc'
41
include 'usercommands.inc'
45
include 'servercommands.inc'
42
include 'servercommands.inc'
Line 46... Line 43...
46
 
43
 
47
; entry point
44
; entry point
48
start:
45
start:
49
 
46
 
50
        DEBUGF  1, "hello"
47
        mcall   40, 0
51
; load libraries
48
; load libraries
52
        stdcall dll.Load, @IMPORT
-
 
53
        test    eax, eax
49
        stdcall dll.Load, @IMPORT
54
        jnz     exit
-
 
55
; initialize console
-
 
56
        push    1
-
 
57
        call    [con_start]
-
 
58
        push    title
-
 
59
        push    25
50
        test    eax, eax
Line 60... Line 51...
60
        push    80
51
        jnz     exit
61
        push    25
52
; initialize console
62
        push    80
53
        invoke  con_start, 1
Line 63... Line 54...
63
        call    [con_init]
54
        invoke  con_init, 80, 25, 80, 25, title
64
 
55
 
65
; Check for parameters
56
; Check for parameters
66
        cmp     byte [s], 0
-
 
67
        jne     resolve
57
        cmp     byte [s], 0
Line 68... Line 58...
68
 
58
        jne     resolve
69
main:
59
 
70
        call    [con_cls]
60
main:
71
; Welcome user
61
        invoke  con_cls
72
        push    str1
62
; Welcome user
73
        call    [con_write_asciiz]
63
        invoke  con_write_asciiz, str1
74
 
64
 
75
; write prompt
65
; write prompt
76
        push    str2
66
        invoke  con_set_flags, 0x0a
77
        call    [con_write_asciiz]
67
        invoke  con_write_asciiz, str2
78
; read string
68
; read string
79
        mov     esi, s
69
        mov     esi, s
80
        push    256
70
        invoke  con_gets, esi, 256
Line 81... Line 71...
81
        push    esi
71
        invoke  con_write_asciiz, str4  ; newline
82
        call    [con_gets]
-
 
83
; check for exit
72
        invoke  con_set_flags, 0x07
84
        test    eax, eax
73
; check for exit
85
        jz      done
74
        test    eax, eax
86
        cmp     byte [esi], 10
75
        jz      done
87
        jz      done
76
        cmp     byte [esi], 10
88
 
77
        jz      done
89
resolve:
78
 
Line 90... Line -...
90
 
-
 
91
; delete terminating '\n'
-
 
92
        mov     esi, s
79
resolve:
93
  @@:
-
 
94
        lodsb
80
; delete terminating '\n'
Line 95... Line 81...
95
        cmp     al, 0x20
81
        mov     esi, s
96
        ja      @r
82
  @@:
97
        mov     byte [esi-1], 0
-
 
98
 
-
 
99
;        call    [con_cls]
-
 
100
        push    str3
-
 
101
        call    [con_write_asciiz]
83
        lodsb
102
        push    s
84
        cmp     al, 0x20
103
        call    [con_write_asciiz]
85
        ja      @r
104
 
86
        mov     byte [esi-1], 0
105
; resolve name
87
 
Line 106... Line 88...
106
        push    esp     ; reserve stack place
88
        invoke  con_write_asciiz, str3
107
        push    esp     ; fourth parameter
-
 
108
        push    0       ; third parameter
89
        invoke  con_write_asciiz, s
109
        push    0       ; second parameter
90
 
Line 110... Line 91...
110
        push    s       ; first parameter
91
; resolve name
111
        call    [getaddrinfo]
92
        push    esp     ; reserve stack place
112
        pop     esi
93
        invoke  getaddrinfo, s, 0, 0, esp
113
; test for error
94
        pop     esi
114
        test    eax, eax
-
 
-
 
95
; test for error
115
        jnz     fail
96
        test    eax, eax
116
 
97
        jnz     fail
117
; write results
-
 
118
        push    str8
98
 
119
        call    [con_write_asciiz]
99
; write results
120
;        mov     edi, esi
-
 
121
 
100
        invoke  con_write_asciiz, str8
122
; convert IP address to decimal notation
-
 
123
        mov     eax, [esi+addrinfo.ai_addr]
-
 
124
        mov     eax, [eax+sockaddr_in.sin_addr]
-
 
Line -... Line 101...
-
 
101
;        mov     edi, esi
125
        mov     [sockaddr1.ip], eax
102
 
126
        push    eax
103
; convert IP address to decimal notation
127
        call    [inet_ntoa]
104
        mov     eax, [esi+addrinfo.ai_addr]
128
; write result
105
        mov     eax, [eax+sockaddr_in.sin_addr]
Line 129... Line -...
129
        push    eax
-
 
130
        call    [con_write_asciiz]
106
        mov     [sockaddr1.ip], eax
131
; free allocated memory
-
 
132
        push    esi
107
 
133
        call    [freeaddrinfo]
-
 
134
 
-
 
135
        push    str9
-
 
136
        call    [con_write_asciiz]
108
        invoke  inet_ntoa, eax
137
 
-
 
Line 138... Line -...
138
        mcall   socket, AF_INET4, SOCK_STREAM, 0
-
 
139
        cmp     eax, -1
109
; write result
Line 140... Line 110...
140
        je      fail2
110
        invoke  con_write_asciiz, eax
141
        mov     [socketnum], eax
-
 
Line 142... Line 111...
142
 
111
; free allocated memory
143
        push    str11
-
 
144
        call    [con_write_asciiz]
-
 
Line 145... Line 112...
145
 
112
        invoke  freeaddrinfo, esi
146
        mcall   connect, [socketnum], sockaddr1, 18
113
 
147
 
114
        invoke  con_write_asciiz, str9
148
        mcall   40, EVM_STACK
115
        mcall   socket, AF_INET4, SOCK_STREAM, 0
149
 
116
        cmp     eax, -1
150
        mov     [status], STATUS_CONNECTING
117
        je      socket_error
-
 
118
        mov     [socketnum], eax
-
 
119
 
Line 151... Line 120...
151
        mov     [offset], buffer_ptr
120
        invoke  con_write_asciiz, str11
152
 
121
        mcall   connect, [socketnum], sockaddr1, 18
153
        push    str12
122
        mov     [status], STATUS_CONNECTING
154
        call    [con_write_asciiz]
-
 
155
 
123
 
156
wait_for_serverdata:
124
        invoke  con_write_asciiz, str12
157
        mcall   10
125
 
158
 
126
wait_for_servercommand:
159
        call    [con_get_flags]
127
 
160
        test    eax, 0x200                      ; con window closed?
128
;        invoke  con_write_asciiz, str_dbg
161
        jnz     exit
129
 
162
 
130
; receive socket data
163
; receive socket data
131
        mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
164
        mcall   recv, [socketnum], [offset], BUFFERSIZE, MSG_DONTWAIT
132
        inc     eax
165
        inc     eax
133
        jz      socket_error
166
        jz      wait_for_serverdata
-
 
167
        dec     eax
134
        dec     eax
168
        jz      wait_for_serverdata
-
 
169
 
135
        jz      wait_for_servercommand
170
; extract commands, copy them to "s" buffer
136
 
171
        add     eax, buffer_ptr                 ; eax = end pointer
-
 
172
        mov     esi, buffer_ptr                 ; esi = current pointer
-
 
173
  .nextcommand:
-
 
174
        mov     edi, s
-
 
175
  .byteloop:
-
 
176
        cmp     esi, eax
-
 
177
        jae     wait_for_serverdata
-
 
178
        lodsb
-
 
179
        cmp     al, 10                          ; excellent, we might have a command
-
 
180
        je      .got_command
-
 
181
        cmp     al, 13                          ; just ignore this crap
-
 
182
        je      .byteloop
-
 
183
        stosb
-
 
184
        jmp     .byteloop
-
 
185
 
-
 
186
; we have a newline check if its a command
-
 
187
  .got_command:
-
 
Line -... Line 137...
-
 
137
;        invoke  con_write_asciiz, str_dbg2
-
 
138
 
Line -... Line 139...
-
 
139
; extract commands, copy them to "s" buffer
-
 
140
        lea     ecx, [eax + buffer_ptr]         ; ecx = end pointer
-
 
141
        mov     esi, buffer_ptr                 ; esi = current pointer
-
 
142
        mov     edi, s
Line -... Line 143...
-
 
143
  .byteloop:
-
 
144
        cmp     esi, ecx
Line 188... Line 145...
188
        xor     al, al
145
        jae     wait_for_servercommand
Line -... Line 146...
-
 
146
        lodsb
-
 
147
        cmp     al, 10                          ; excellent, we might have a command
189
        stosb
148
        je      .got_command
190
;        push    esi eax
149
        cmp     al, 13                          ; just ignore this byte
Line 191... Line 150...
191
 
150
        je      .byteloop
192
; print it to the screen
151
        stosb
Line 193... Line 152...
193
        pushd   s
152
        jmp     .byteloop
194
        call    [con_write_asciiz]
-
 
195
        pushd   str4                            ; newline
153
  .got_command:                                 ; we have a newline check if its a command
196
        call    [con_write_asciiz]
154
        xor     al, al
197
 
155
        stosb
198
;        cmp     byte[s+2], " "
-
 
199
;        jne     .not_command
-
 
200
 
156
 
201
        lea     ecx, [edi - s]
-
 
202
        call    server_parser
157
        sub     edi, s                          ; length
203
 
-
 
204
;  .not_command:
-
 
Line 205... Line 158...
205
;        pop     eax esi
158
        push    edi
206
;        jmp     .nextcommand
159
 
Line 207... Line 160...
207
 
160
        invoke  con_set_flags, 0x03             ; change color
208
 
161
        invoke  con_write_asciiz, s             ; print servercommand
Line 209... Line -...
209
 
-
 
210
 
162
        invoke  con_write_asciiz, str4          ; newline
211
wait_for_usercommand:
-
 
212
 
163
        invoke  con_set_flags, 0x07
Line 213... Line 164...
213
        cmp     [status], STATUS_CONNECTED
164
 
Line 214... Line -...
214
        je      .connected
-
 
215
 
165
        pop     ecx
216
        cmp     [status], STATUS_NEEDPASSWORD
-
 
217
        je      .needpass
166
        jmp     server_parser                   ; parse command
218
 
167
 
219
; write prompt
-
 
220
        push    str2
-
 
221
        call    [con_write_asciiz]
-
 
222
; read string
-
 
223
        mov     esi, s
168
wait_for_usercommand:
Line 224... Line 169...
224
        push    256
169
 
225
        push    esi
-
 
226
        call    [con_gets]
-
 
Line -... Line 170...
-
 
170
        invoke  con_set_flags, 0x0a
227
 
171
 
228
        call    [con_get_flags]
172
        cmp     [status], STATUS_CONNECTED
Line 229... Line -...
229
        test    eax, 0x200                      ; con window closed?
-
 
230
        jnz     exit
-
 
231
 
-
 
232
        cmp     dword[s], "list"
173
        je      .connected
233
        je      cmd_list
174
 
234
 
175
        cmp     [status], STATUS_NEEDPASSWORD
235
        cmp     dword[s], "help"
-
 
236
        je      cmd_help
-
 
237
 
176
        je      .needpass
Line 238... Line 177...
238
        push    str_unkown
177
 
239
        call    [con_write_asciiz]
178
; write prompt
240
 
179
        invoke  con_write_asciiz, str2
241
        jmp     wait_for_usercommand
180
; read string
242
 
181
        mov     esi, s
243
 
182
        invoke  con_gets, esi, 256
244
  .connected:
-
 
245
 
-
 
246
        push    str_user
-
 
247
        call    [con_write_asciiz]
-
 
Line -... Line 183...
-
 
183
        invoke  con_set_flags, 0x07
-
 
184
 
-
 
185
        cmp     dword[s], "list"
Line 248... Line 186...
248
 
186
        je      cmd_list
249
        mov     dword[s], "USER"
187
 
250
        mov     byte[s+4], " "
188
        cmp     dword[s], "help"
Line 251... Line 189...
251
 
189
        je      cmd_help
252
;        mov     [status], STATUS_NEEDPASSWORD
190
 
253
        inc     [status]
191
        invoke  con_write_asciiz, str_unknown
254
 
-
 
255
        jmp     .send
192
        jmp     wait_for_usercommand
Line 256... Line 193...
256
 
193
 
257
 
-
 
258
  .needpass:
194
 
259
        push    str_pass
-
 
260
        call    [con_write_asciiz]
195
  .connected:
Line 261... Line -...
261
 
-
 
262
        mov     dword[s], "PASS"
-
 
263
        mov     byte[s+4], " "
-
 
Line -... Line 196...
-
 
196
 
-
 
197
        invoke  con_write_asciiz, str_user
264
 
198
        mov     dword[s], "USER"
Line 265... Line 199...
265
;        mov     [status], STATUS_LOGGED_IN
199
        mov     byte[s+4], " "
266
        inc     [status]
-
 
267
 
200
        jmp     .send
268
  .send:
201
 
269
; read string
-
 
270
        mov     esi, s+5
202
 
271
        push    256
203
  .needpass:
272
        push    esi
204
 
Line 273... Line 205...
273
        call    [con_gets]
205
        invoke  con_write_asciiz, str_pass
274
 
-
 
275
        mov     edi, s+5
206
        mov     dword[s], "PASS"
276
        mov     ecx, 256
-
 
Line -... Line 207...
-
 
207
        mov     byte[s+4], " "
277
        xor     al, al
208
 
278
        repne   scasb
209
  .send:
Line 279... Line 210...
279
        lea     esi, [edi-s-1]
210
; read string
280
        mcall   send, [socketnum], s
211
        mov     esi, s+5
281
 
212
        invoke  con_gets, esi, 256
282
        jmp     wait_for_usercommand
213
 
283
 
214
        mov     edi, s+5
284
 
215
        mov     ecx, 256
285
 
216
        xor     al, al
286
 
217
        repne   scasb
287
 
218
        lea     esi, [edi-s-1]
288
 
219
        mcall   send, [socketnum], s, , 0
289
open_dataconnection:
220
 
290
        cmp     [status], STATUS_LOGGED_IN
221
        invoke  con_write_asciiz, str4  ; newline
291
        jne     .fail
222
        invoke  con_set_flags, 0x07
292
 
223
        jmp     wait_for_servercommand
293
        mov     dword[s], "PASV"
224
 
294
        mov     byte[s+4], 10
225
 
295
        mcall   send, [socketnum], s, 5
226
 
296
 
227
open_dataconnection:
Line 297... Line 228...
297
        ret
228
        cmp     [status], STATUS_LOGGED_IN
Line -... Line 229...
-
 
229
        jne     .fail
-
 
230
 
-
 
231
        mov     dword[s], "PASV"
298
 
232
        mov     byte[s+4], 10
299
  .fail:
233
        mcall   send, [socketnum], s, 5, 0
300
        push    str6
234
        ret
301
        call    [con_write_asciiz]
235
 
302
 
236
  .fail:
Line 359... Line 293...
359
        dw AF_INET4
293
        dw AF_INET4
360
.port   dw 0
294
.port   dw 0
361
.ip     dd 0
295
.ip     dd 0
362
        rb 10
296
        rb 10
Line 363... Line -...
363
 
-
 
364
include_debug_strings    ; ALWAYS present in data section
-
 
365
 
-
 
Line 366... Line 297...
366
 
297
 
367
 
298
 
368
; import
299
; import
Line 384... Line 315...
384
        con_gets,       'con_gets',\
315
        con_gets,       'con_gets',\
385
        con_cls,        'con_cls',\
316
        con_cls,        'con_cls',\
386
        con_getch2,     'con_getch2',\
317
        con_getch2,     'con_getch2',\
387
        con_set_cursor_pos, 'con_set_cursor_pos',\
318
        con_set_cursor_pos, 'con_set_cursor_pos',\
388
        con_write_string, 'con_write_string',\
319
        con_write_string, 'con_write_string',\
-
 
320
        con_get_flags,  'con_get_flags', \
389
        con_get_flags,  'con_get_flags'
321
        con_set_flags,  'con_set_flags'
Line 390... Line 322...
390
 
322
 
Line 391... Line 323...
391
 
323
 
392
i_end:
324
i_end:
393
 
325
 
394
active_passive  db ?
326
active_passive  db ?
395
socketnum       dd ?
327
socketnum       dd ?
396
datasocket      dd ?
-
 
Line 397... Line 328...
397
buffer_ptr      rb 2*BUFFERSIZE
328
datasocket      dd ?
Line 398... Line 329...
398
status          db ?
329
buffer_ptr      rb 2*BUFFERSIZE