Subversion Repositories Kolibri OS

Rev

Rev 2543 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. format binary as ""
  2.  
  3. use32
  4. ; standard header
  5.         db      'MENUET01'      ; signature
  6.         dd      1               ; header version
  7.         dd      start           ; entry point
  8.         dd      i_end           ; initialized size
  9.         dd      mem             ; required memory
  10.         dd      mem             ; stack pointer
  11.         dd      0               ; parameters
  12.         dd      0               ; path
  13.  
  14. __DEBUG__ equ 0
  15. __DEBUG_LEVEL__ equ 1
  16.  
  17.  
  18. BUFFERSIZE      equ 4096
  19. ; useful includes
  20. include '../macros.inc'
  21. purge mov,add,sub
  22. include '../proc32.inc'
  23. include '../dll.inc'
  24. include '../debug-fdo.inc'
  25.  
  26. include '../network.inc'
  27.  
  28. ; entry point
  29. start:
  30. ; load libraries
  31.         stdcall dll.Load, @IMPORT
  32.         test    eax, eax
  33.         jnz     exit
  34. ; initialize console
  35.         push    1
  36.         call    [con_start]
  37.         push    title
  38.         push    25
  39.         push    80
  40.         push    25
  41.         push    80
  42.         call    [con_init]
  43. ; main loop
  44.         push    str1
  45.         call    [con_write_asciiz]
  46. main:
  47. ; write prompt
  48.         push    str2
  49.         call    [con_write_asciiz]
  50. ; read string
  51.         mov     esi, s
  52.         push    256
  53.         push    esi
  54.         call    [con_gets]
  55. ; check for exit
  56.         test    eax, eax
  57.         jz      done
  58.         cmp     byte [esi], 10
  59.         jz      done
  60. ; delete terminating '\n'
  61.         push    esi
  62. @@:
  63.         lodsb
  64.         test    al, al
  65.         jnz     @b
  66.         mov     byte [esi-2], al
  67.         pop     esi
  68. ; resolve name
  69.         push    esp     ; reserve stack place
  70.         push    esp     ; fourth parameter
  71.         push    0       ; third parameter
  72.         push    0       ; second parameter
  73.         push    esi     ; first parameter
  74.         call    [getaddrinfo]
  75.         pop     esi
  76. ; test for error
  77.         test    eax, eax
  78.         jnz     fail
  79.  
  80. ; write results
  81.         push    str3
  82.         call    [con_write_asciiz]
  83. ;        mov     edi, esi
  84.  
  85. ; convert IP address to decimal notation
  86.         mov     eax, [esi+addrinfo.ai_addr]
  87.         mov     eax, [eax+sockaddr_in.sin_addr]
  88.         mov     [sockaddr1.ip], eax
  89.         push    eax
  90.         call    [inet_ntoa]
  91. ; write result
  92.         push    eax
  93.         call    [con_write_asciiz]
  94. ; free allocated memory
  95.         push    esi
  96.         call    [freeaddrinfo]
  97.  
  98.         push    str4
  99.         call    [con_write_asciiz]
  100.  
  101.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  102.         cmp     eax, -1
  103.         jz      fail2
  104.         mov     [socketnum], eax
  105.  
  106.         mcall   connect, [socketnum], sockaddr1, 18
  107.  
  108.         mcall   40, 1 shl 7 ; + 7
  109.         call    [con_cls]
  110.  
  111.         mcall   18, 7
  112.         push    eax
  113.         mcall   51, 1, thread, mem - 2048
  114.         pop     ecx
  115.         mcall   18, 3
  116.  
  117. mainloop:
  118.     DEBUGF  1, 'TELNET: Waiting for events\n'
  119.         mcall   10
  120.     DEBUGF  1, 'TELNET: EVENT %x !\n', eax
  121.  
  122.         mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
  123.         cmp     eax, -1
  124.         je      mainloop
  125.  
  126.     DEBUGF  1, 'TELNET: got %u bytes of data !\n', eax
  127.  
  128.         mov     esi, buffer_ptr
  129.         lea     edi, [esi + eax]
  130.         mov     byte [edi], 0
  131.  
  132.   .scan_cmd:
  133.         cmp     byte [esi], 0xff        ; Interpret As Command
  134.         jne     .no_cmd
  135.         ; TODO: parse options, for now, we will reply with 'WONT' to everything
  136.         mov     byte [esi + 1], 252     ; WONT
  137.         add     esi, 3                  ; a command is always 3 bytes
  138.         jmp     .scan_cmd
  139.   .no_cmd:
  140.  
  141.         cmp     esi, buffer_ptr
  142.         je      .print_loop
  143.  
  144.     DEBUGF  1, 'TELNET: sending data\n'
  145.  
  146.         push    esi edi
  147.         sub     esi, buffer_ptr
  148.         mcall   send, [socketnum], buffer_ptr, , 0
  149.         pop     edi esi
  150.  
  151.   .print_loop:
  152.     DEBUGF  1, 'TELNET: printloop\n'
  153.         cmp     esi, edi
  154.         jae     mainloop
  155.  
  156.         cmp     byte [esi], 0x1b        ; escape character
  157.         jne     .print_byte
  158.         inc     esi
  159.  
  160.         cmp     word [esi], 0x485b      ; move cursor to beginning
  161.         jne     @f
  162.         inc     esi
  163.         inc     esi
  164.  
  165.     DEBUGF  1, 'TELNET: resetting cursor \n'
  166.  
  167.         push    0
  168.         push    0
  169.         call    [con_set_cursor_pos]
  170.         jmp     .print_loop
  171.  
  172.   @@:
  173.         inc     esi
  174.         inc     esi
  175.         jmp     .print_loop
  176.  
  177.   .print_byte:
  178.         push    dword 1
  179.         push    esi                     ; next string to print
  180.         inc     esi
  181.         call    [con_write_string]
  182.         jmp     .print_loop
  183.  
  184.  
  185. fail:
  186.         push    str5
  187.         jmp     main
  188. fail2:
  189.         push    str6
  190.         jmp     main
  191.  
  192. done:
  193.         push    1
  194.         call    [con_exit]
  195. exit:
  196.         mcall   -1
  197.  
  198.  
  199.  
  200. thread:
  201.         mcall   40, 0
  202.   .loop:
  203.         call    [con_getch2]
  204.         mov     byte [send_data], al
  205.         mcall   send, [socketnum], send_data, 1
  206.         jmp     .loop
  207.  
  208. ; data
  209. title   db      'Telnet',0
  210. str1    db      'Telnet v0.1',10,' for KolibriOS # 1281 or later. ',10,10,'If you dont know where to connect to, try towel.blinkenlights.nl',10,10,0
  211. str2    db      '> ',0
  212. str3    db      'Connecting to: ',0
  213. str4    db      10,0
  214. str5    db      'Name resolution failed.',10,10,0
  215. str6    db      'Could not open socket',10,10,0
  216. str7    db      'Got data!',10,10,0
  217.  
  218. sockaddr1:
  219.         dw AF_INET4
  220. .port   dw 23
  221. .ip     dd 0
  222.         rb 10
  223.  
  224. include_debug_strings    ; ALWAYS present in data section
  225.  
  226.  
  227.  
  228. ; import
  229. align 4
  230. @IMPORT:
  231.  
  232. library network, 'network.obj', console, 'console.obj'
  233. import  network,        \
  234.         getaddrinfo,    'getaddrinfo',  \
  235.         freeaddrinfo,   'freeaddrinfo', \
  236.         inet_ntoa,      'inet_ntoa'
  237. import  console,        \
  238.         con_start,      'START',        \
  239.         con_init,       'con_init',     \
  240.         con_write_asciiz,       'con_write_asciiz',     \
  241.         con_exit,       'con_exit',     \
  242.         con_gets,       'con_gets',\
  243.         con_cls,        'con_cls',\
  244.         con_getch2,     'con_getch2',\
  245.         con_set_cursor_pos, 'con_set_cursor_pos',\
  246.         con_write_string, 'con_write_string'
  247.  
  248.  
  249. i_end:
  250.  
  251. socketnum       dd ?
  252. buffer_ptr      rb BUFFERSIZE+1
  253. send_data       rb 100
  254.  
  255. s       rb      256
  256. align   4
  257. rb      4096    ; stack
  258. mem:
  259.