Subversion Repositories Kolibri OS

Rev

Rev 3704 | Rev 4655 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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