Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3618 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
4468 hidnplayr 3
;; Copyright (C) KolibriOS team 2010-2014. All rights reserved.    ;;
3618 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  tftpc.asm - TFTP client for KolibriOS                          ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
9
;;                                                                 ;;
10
;;          GNU GENERAL PUBLIC LICENSE                             ;;
11
;;             Version 2, June 1991                                ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
3545 hidnplayr 15
format binary as ""
16
 
17
use32
3618 hidnplayr 18
        org     0x0
3545 hidnplayr 19
 
3618 hidnplayr 20
        db      'MENUET01'
21
        dd      0x1
22
        dd      START
23
        dd      I_END
24
        dd      IM_END+0x1000
25
        dd      IM_END+0x1000
26
        dd      0, 0
3545 hidnplayr 27
 
3618 hidnplayr 28
include '../../proc32.inc'
29
include '../../macros.inc'
30
include '../../libio.inc'
31
include '../../dll.inc'
3549 yogev_ezra 32
include '../../develop/libraries/box_lib/trunk/box_lib.mac'
3545 hidnplayr 33
 
3618 hidnplayr 34
include '../../network.inc'
3545 hidnplayr 35
 
36
 
3618 hidnplayr 37
filebuffer_size = 4*4096        ; 16kb   (dont try to change it yet..)
38
TIMEOUT         = 100
39
buffer_len      = 1500
3545 hidnplayr 40
 
3618 hidnplayr 41
opcode_rrq      = 1
42
opcode_wrq      = 2
43
opcode_data     = 3
44
opcode_ack      = 4
45
opcode_error    = 5
3545 hidnplayr 46
 
47
; read/write request packet
48
;
49
;  2 bytes     string    1 byte     string   1 byte
50
;  ------------------------------------------------
51
; | Opcode |  Filename  |   0  |    Mode    |   0  |
52
;  ------------------------------------------------
53
 
54
; data packet
55
;
56
;  2 bytes     2 bytes      n bytes
57
;  ----------------------------------
58
; | Opcode |   Block #  |   Data     |
59
;  ----------------------------------
60
 
61
; acknowledgement packet
62
;
63
;  2 bytes     2 bytes
64
;  ---------------------
65
; | Opcode |   Block #  |
66
;  ---------------------
67
 
68
; error packet
69
;
70
;  2 bytes  2 bytes        string    1 byte
71
;  ----------------------------------------
72
; | Opcode |  ErrorCode |   ErrMsg   |   0  |
73
;  ----------------------------------------
74
 
75
 
76
START:
77
 
78
        mcall   68, 11
79
 
80
        stdcall dll.Load, @IMPORT
81
        or      eax, eax
82
        jnz     exit
83
 
84
stop_transfer:
4468 hidnplayr 85
        mcall   40, EVM_MOUSE + EVM_MOUSE_FILTER + EVM_REDRAW + EVM_BUTTON + EVM_KEY + EVM_STACK
3545 hidnplayr 86
 
87
red_win:
4468 hidnplayr 88
        call    draw_window
3545 hidnplayr 89
 
90
align 4
91
still:
92
        mcall   10
93
 
94
        dec     eax
95
        jz      red_win
96
 
97
        dec     eax
98
        jz      key
99
 
100
        dec     eax
101
        jz      button
102
 
4468 hidnplayr 103
        invoke  edit_box_mouse, edit1
104
        invoke  edit_box_mouse, edit2
105
        invoke  edit_box_mouse, edit3
106
        invoke  edit_box_mouse, edit4
3545 hidnplayr 107
 
4468 hidnplayr 108
        invoke  option_box_mouse, Option_boxs1
109
        invoke  option_box_mouse, Option_boxs2
3545 hidnplayr 110
 
111
        jmp     still
112
 
113
button:
114
        mcall   17
115
 
4468 hidnplayr 116
        cmp     ah, 0x10        ; connect button
3545 hidnplayr 117
        je      start_transfer
118
 
119
        test    ah , ah
120
        jz      still
121
 
4468 hidnplayr 122
exit:
123
        mcall   -1
3545 hidnplayr 124
key:
125
        mcall   2
126
 
4468 hidnplayr 127
        invoke  edit_box_key, edit1
128
        invoke  edit_box_key, edit2
129
        invoke  edit_box_key, edit3
130
        invoke  edit_box_key, edit4
3545 hidnplayr 131
 
132
        jmp still
133
 
134
 
135
draw_window:
4468 hidnplayr 136
; get system colors
137
        mcall   48, 3, sc, 40
3545 hidnplayr 138
 
4468 hidnplayr 139
        mcall   12, 1
3545 hidnplayr 140
 
4468 hidnplayr 141
        mov     edx, [sc.work]
142
        or      edx, 0x34000000
143
        xor     esi, esi
144
        mov     edi, str_title
145
        mcall   0, 50 shl 16 + 400, 30 shl 16 + 180
146
 
147
        mov     ebx, 35 shl 16 + 10
148
        mov     ecx, 0x80000000
149
        or      ecx, [sc.work_text]
150
        mov     edx, str_server
151
        mcall   4
152
        mov     ebx, 5 shl 16 + 30
3545 hidnplayr 153
        mov     edx, str_source
154
        mcall
4468 hidnplayr 155
        mov     ebx, 11 shl 16 + 50
3545 hidnplayr 156
        mov     edx, str_destination
157
        mcall
4468 hidnplayr 158
        mov     ebx, 47 shl 16 + 72
3545 hidnplayr 159
        mov     edx, str_mode
160
        mcall
4468 hidnplayr 161
        mov     ebx, 160 shl 16 + 72
3545 hidnplayr 162
        mov     edx, str_method
163
        mcall
4468 hidnplayr 164
        mov     ebx, 270 shl 16 + 72
3545 hidnplayr 165
        mov     edx, str_blocksize
166
        mcall
167
 
4468 hidnplayr 168
        invoke  edit_box_draw, edit1
169
        invoke  edit_box_draw, edit2
170
        invoke  edit_box_draw, edit3
171
        invoke  edit_box_draw, edit4
3545 hidnplayr 172
 
4468 hidnplayr 173
        invoke  option_box_draw, Option_boxs1
174
        invoke  option_box_draw, Option_boxs2
3545 hidnplayr 175
 
4468 hidnplayr 176
        mov     esi, [sc.work_button]
177
        mcall   8, 210 shl 16 + 170, 105 shl 16 + 16, 0x10
3545 hidnplayr 178
 
4468 hidnplayr 179
        mov     ecx, 0x80000000
180
        or      ecx, [sc.work_button_text]
181
        mcall   4, 260 shl 16 + 110, , str_transfer
3545 hidnplayr 182
 
4468 hidnplayr 183
        mov     ecx, 0x80000000
184
        or      ecx, [sc.work_text]
185
        mcall   4, 350 shl 16 + 137, , str_kb_s
186
        mcall   4, 50 shl 16 + 137, , str_complete
3545 hidnplayr 187
 
4468 hidnplayr 188
        mcall   38, 10 shl 16 + 380, 130 shl 16 + 130, [sc.work_graph]
3545 hidnplayr 189
 
4468 hidnplayr 190
        mcall   47, 1 shl 31 + 7 shl 16 + 1, kbps, 305 shl 16 + 137, [sc.work_text]
191
        mcall   47, 1 shl 31 + 3 shl 16 + 1, done, 25 shl 16 + 137, ;[sc.work_text]
3545 hidnplayr 192
 
4468 hidnplayr 193
        mcall   12, 2
3545 hidnplayr 194
 
195
        ret
196
 
197
 
198
 
199
 
200
 
201
start_transfer:
202
 
203
        ; first, resolve the hostname
204
 
205
        push    esp     ; reserve stack place
206
 
207
        push    esp     ; fourth parameter
208
        push    0       ; third parameter
209
        push    0       ; second parameter
210
        push    dword SRV     ; first parameter
211
        call    [getaddrinfo]
212
 
213
        pop     esi
214
 
215
; test for error
216
        test    eax, eax
217
        jnz     still
218
 
219
        mov     esi, [esi]
220
        mov     esi, [esi + sockaddr_in.sin_addr]
221
        mov     dword [IP], esi
222
 
223
        mcall   socket, AF_INET4, SOCK_DGRAM, 0                ; socket_open
224
        cmp     eax, -1
225
        je      still
226
 
227
        mov     [socketnum], eax
228
 
229
        mcall   connect, [socketnum], sockaddr, sockaddr_len         ; socket_connect
230
        cmp     eax, -1
231
        je      still
232
 
233
        mov     word [I_END], opcode_rrq
234
        cmp     [option_group2],op3
235
        je      @f
236
        mov     word [I_END], opcode_wrq
237
      @@:
238
 
239
        xor     al , al
240
        mov     edi, remote_addr
241
        mov     ecx, 250
242
        repnz   scasb
243
        sub     edi, remote_addr-1
244
        mov     ecx, edi
245
        mov     edi, I_END+2
246
        mov     esi, remote_addr
247
        rep     movsb
248
 
249
        cmp     [option_group1], op1
250
        je      .ascii
251
 
252
        mov     esi, octet
253
        movsd
254
        movsb
255
 
256
        jmp     .send_request
257
 
258
      .ascii:
259
 
260
        mov     esi, netascii
261
        movsd
262
        movsd
263
 
264
      .send_request:
265
 
266
        xor     al, al
267
        stosb
268
 
269
        sub     edi, I_END
270
        mov     esi, edi
271
        mcall   send, [socketnum], I_END
272
 
4468 hidnplayr 273
        mcall   40, EVM_REDRAW + EVM_BUTTON + EVM_STACK
3545 hidnplayr 274
 
275
        mov     [last_ack], 0
276
 
277
 
278
 
279
 
280
receive_data_loop:
281
 
282
        mcall   23, TIMEOUT
283
 
284
        dec     eax
285
        jz      .red
286
 
287
        dec     eax
288
        jz      .key
289
 
290
 
3704 hidnplayr 291
        mcall   recv, [socketnum], buffer, buffer_len, MSG_DONTWAIT     ; receive data
3545 hidnplayr 292
 
293
        cmp     word[buffer], opcode_data
294
        jne     .error
295
 
296
        mov     bx, [last_ack]
297
        cmp     word [buffer + 2], bx
298
        jne     .packet_got_lost
299
        inc     [last_ack]
300
 
301
        cmp     eax, 4+512
302
        je      .continue
303
 
304
; last packet, or something else
305
.error:
306
 
307
.packet_got_lost:
308
 
309
 
310
 
311
.continue:
312
 
313
        mov     word[buffer], opcode_ack                ; send ack
314
        mcall   send, [socketnum], buffer, 4, 0
315
 
316
        jmp     receive_data_loop
317
 
318
.red:
319
 
320
        call    draw_window
321
 
322
        jmp     receive_data_loop
323
 
324
 
325
.key:
326
        mcall   2
327
        cmp     ah, 2
328
        jz      exit
329
 
330
        ; close socket ?
331
 
332
        jmp     receive_data_loop
333
 
334
 
335
 
336
 
337
 
338
 
339
;--------------------------------
340
 
341
 
342
send_:
343
 
344
        invoke  file_open, local_addr, O_READ
345
        or      eax, eax
346
        jz      .exit
347
        mov     [fh], eax
348
 
349
        stdcall mem.Alloc, filebuffer_size
350
        or      eax, eax
351
        jz      .exit
352
        mov     [fb], eax
353
 
354
        mov     [last_ack], 0
355
        mov     [fo], 0
356
 
357
.read_chunk:
358
 
359
        invoke  file_seek, [fh], [fo], SEEK_END
360
        cmp     eax, -1
361
        je      .exit
362
        invoke  file_read, [fh], [fb], filebuffer_size
363
        cmp     eax, -1
364
        je      .exit
365
        add     [fo], filebuffer_size
366
        cmp     eax, filebuffer_size
367
        je      .packet
368
 
369
        ; ijhidfhfdsndsfqk
370
 
371
.packet:
372
 
373
        movzx   esi, [last_ack]
374
        and     esi, 0x000000001f   ; last five bits    BUFFER SIZE MUST BE 16 kb for this to work !!!
375
        shl     esi, 9              ; = * 512
376
        add     esi, [fb]
377
        mov     edi, buffer
378
        mov     ax, opcode_data
379
        stosw
380
        mov     ax, [last_ack]
381
        stosw
382
        mov     ecx, 512/4
383
        rep     movsd
384
 
385
        mcall   send, [socketnum], buffer, 4+512, 0       ; send data
386
 
387
 
388
.loop:
389
 
390
        mcall   23, TIMEOUT
391
 
392
        dec     eax
393
        jz      .red
394
 
395
        dec     eax
396
        jz      .key
397
 
3704 hidnplayr 398
        mcall   recv, [socketnum], buffer, buffer_len, MSG_DONTWAIT  ; receive ack
3545 hidnplayr 399
 
400
        cmp     word[buffer], opcode_ack
401
        jne     .exit
402
 
403
        mov     ax, [last_ack]
404
        cmp     word[buffer+2], ax
405
        jne     .packet
406
        inc     [last_ack]
407
        test    [last_ack],0x001f
408
        jz      .read_chunk
409
        jmp     .packet
410
 
411
 
412
.red:
413
 
414
        call    draw_window
415
 
416
        jmp     .loop
417
 
418
 
419
.key:
420
        mcall   2
421
        cmp     ah, 2
422
        jz      exit
423
 
424
        ; close socket ?
425
 
426
        jmp     .loop
427
 
428
.exit:
429
        invoke  file_close, [fh]
430
        jmp     still
431
 
432
 
433
 
434
 
435
 
436
;-------------------------
437
; DATA
438
 
4468 hidnplayr 439
socketnum       dd 0
440
kbps            dd 0
441
done            dd 0
3545 hidnplayr 442
 
443
sockaddr:
4468 hidnplayr 444
                dw AF_INET4
445
                dw 0x4500       ; 69
446
IP              db 192, 168, 1, 115
447
 
3545 hidnplayr 448
sockaddr_len = $ - sockaddr
449
 
450
align 16
451
@IMPORT:
452
 
453
library box_lib , 'box_lib.obj', \
454
        io_lib  , 'libio.obj', \
455
        network , 'network.obj'
456
 
457
import  box_lib                                 ,\
458
        edit_box_draw    ,'edit_box'            ,\
459
        edit_box_key     ,'edit_box_key'        ,\
460
        edit_box_mouse   ,'edit_box_mouse'      ,\
461
        version_ed       ,'version_ed'          ,\
462
        init_checkbox    ,'init_checkbox2'      ,\
463
        check_box_draw   ,'check_box_draw2'     ,\
464
        check_box_mouse  ,'check_box_mouse2'    ,\
465
        version_ch       ,'version_ch2'         ,\
466
        option_box_draw  ,'option_box_draw'     ,\
467
        option_box_mouse ,'option_box_mouse'    ,\
468
        version_op       ,'version_op'
469
 
470
import  io_lib                                  ,\
471
        file_find_first , 'file_find_first'     ,\
472
        file_find_next  , 'file_find_next'      ,\
473
        file_find_close , 'file_find_close'     ,\
474
        file_size       , 'file_size'           ,\
475
        file_open       , 'file_open'           ,\
476
        file_read       , 'file_read'           ,\
477
        file_write      , 'file_write'          ,\
478
        file_seek       , 'file_seek'           ,\
479
        file_tell       , 'file_tell'           ,\
480
        file_eof?       , 'file_iseof'          ,\
481
        file_seteof     , 'file_seteof'         ,\
482
        file_truncate   , 'file_truncate'       ,\
483
        file_close      , 'file_close'
484
 
4468 hidnplayr 485
import  network                                 ,\
486
        inet_ntoa       , 'inet_ntoa'           ,\
487
        getaddrinfo     , 'getaddrinfo'         ,\
488
        freeaddrinfo    , 'freeaddrinfo'
3545 hidnplayr 489
 
490
 
4468 hidnplayr 491
edit1 edit_box 300, 80, 5, 0xffffff, 0x6f9480, 0, 0, 0, 99, SRV, mouse_dd, ed_focus, 13, 13
492
edit2 edit_box 300, 80, 25, 0xffffff, 0x6a9480, 0, 0, 0, 99, remote_addr, mouse_dd, ed_figure_only, 5, 5
493
edit3 edit_box 300, 80, 45, 0xffffff, 0x6a9480, 0, 0, 0, 99, local_addr, mouse_dd, ed_figure_only, 27, 27
494
edit4 edit_box 40, 340, 68, 0xffffff, 0x6a9480, 0, 0, 0, 5, BLK, mouse_dd, ed_figure_only, 3, 3
3545 hidnplayr 495
 
4468 hidnplayr 496
op1 option_box option_group1, 80, 68, 6, 12, 0xffffff, 0, 0, netascii, octet-netascii
497
op2 option_box option_group1, 80, 85, 6, 12, 0xFFFFFF, 0, 0, octet, get-octet
3545 hidnplayr 498
 
4468 hidnplayr 499
op3 option_box option_group2, 210, 68, 6, 12, 0xffffff, 0, 0, get, put-get
500
op4 option_box option_group2, 210, 85, 6, 12, 0xFFFFFF, 0, 0, put, BLK-put
3545 hidnplayr 501
 
502
option_group1   dd op1
503
option_group2   dd op3
4468 hidnplayr 504
Option_boxs1    dd op1, op2, 0
505
Option_boxs2    dd op3, op4, 0
3545 hidnplayr 506
 
4468 hidnplayr 507
str_title       db 'TFTP client', 0
508
str_server      db 'Server:', 0
509
str_source      db 'Remote file:', 0
510
str_destination db 'Local file:', 0
511
str_mode        db 'Mode:', 0
512
str_method      db 'Method:', 0
513
str_blocksize   db 'Blocksize:', 0
514
str_kb_s        db 'kB/s', 0
515
str_complete    db '% complete', 0
516
str_transfer    db 'Transfer', 0
3545 hidnplayr 517
 
518
str_error:
4468 hidnplayr 519
._0 db 'Not defined, see error message (if any).', 0
520
._1 db 'File not found.', 0
521
._2 db 'Access violation.', 0
522
._3 db 'Disk full or allocation exceeded.', 0
523
._4 db 'Illegal TFTP operation.', 0
524
._5 db 'Unknown transfer ID.', 0
525
._6 db 'File already exists.', 0
526
._7 db 'No such user.', 0
3545 hidnplayr 527
 
528
 
529
netascii db 'NetASCII'
530
octet    db 'Octet'
531
get      db 'GET'
532
put      db 'PUT'
533
 
4468 hidnplayr 534
BLK      db "512", 0, 0, 0
3545 hidnplayr 535
 
536
last_ack dw ?
537
 
538
fh       dd ?   ; file handle
539
fo       dd ?   ; file offset
540
fb       dd ?   ; file buffer
541
 
4468 hidnplayr 542
SRV             db "192.168.1.115", 0
543
                rb (SRV + 256 - $)
3545 hidnplayr 544
 
4468 hidnplayr 545
remote_addr     db "IMG00", 0
546
                rb (remote_addr + 256 - $)
3545 hidnplayr 547
 
4468 hidnplayr 548
local_addr      db "/hd0/1/KolibriOS/kernel.mnt", 0
549
                rb (local_addr + 256 - $)
3545 hidnplayr 550
 
551
I_END:
4468 hidnplayr 552
 
553
sc              system_colors
554
 
3545 hidnplayr 555
mouse_dd        dd ?
4468 hidnplayr 556
buffer          rb buffer_len
3545 hidnplayr 557
 
558
IM_END: