Subversion Repositories Kolibri OS

Rev

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

  1. use32
  2. ; standard header
  3.         db      'MENUET01'      ; signature
  4.         dd      1               ; header version
  5.         dd      start           ; entry point
  6.         dd      i_end           ; initialized size
  7.         dd      mem             ; required memory
  8.         dd      mem             ; stack pointer
  9.         dd      0               ; parameters
  10.         dd      0               ; path
  11.  
  12.  
  13. BUFFERSIZE      equ 1500
  14. ; useful includes
  15. include '../macros.inc'
  16. purge mov,add,sub
  17. include '../proc32.inc'
  18. include 'dll.inc'
  19.  
  20. include '../network.inc'
  21.  
  22. ; entry point
  23. start:
  24. ; load libraries
  25.         stdcall dll.Load, @IMPORT
  26.         test    eax, eax
  27.         jnz     exit
  28. ; initialize console
  29.         push    1
  30.         call    [con_start]
  31.         push    title
  32.         push    -1
  33.         push    -1
  34.         push    -1
  35.         push    -1
  36.         call    [con_init]
  37. ; main loop
  38.         push    str1
  39.         call    [con_write_asciiz]
  40. main:
  41. ; write prompt
  42.         push    str2
  43.         call    [con_write_asciiz]
  44. ; read string
  45.         mov     esi, s
  46.         push    256
  47.         push    esi
  48.         call    [con_gets]
  49. ; check for exit
  50.         test    eax, eax
  51.         jz      done
  52.         cmp     byte [esi], 10
  53.         jz      done
  54. ; delete terminating '\n'
  55.         push    esi
  56. @@:
  57.         lodsb
  58.         test    al, al
  59.         jnz     @b
  60.         mov     byte [esi-2], al
  61.         pop     esi
  62. ; resolve name
  63.         push    esp     ; reserve stack place
  64.         push    esp     ; fourth parameter
  65.         push    0       ; third parameter
  66.         push    0       ; second parameter
  67.         push    esi     ; first parameter
  68.         call    [getaddrinfo]
  69.         pop     esi
  70. ; test for error
  71.         test    eax, eax
  72.         jnz     fail
  73.  
  74. ; write results
  75.         push    str3
  76.         call    [con_write_asciiz]
  77. ;        mov     edi, esi
  78.  
  79. ; convert IP address to decimal notation
  80.         mov     eax, [esi+addrinfo.ai_addr]
  81.         mov     eax, [eax+sockaddr_in.sin_addr]
  82.         mov     [sockaddr1.ip], eax
  83.         push    eax
  84.         call    [inet_ntoa]
  85. ; write result
  86.         push    eax
  87.         call    [con_write_asciiz]
  88. ; free allocated memory
  89.         push    esi
  90.         call    [freeaddrinfo]
  91.  
  92.         push    str4
  93.         call    [con_write_asciiz]
  94.  
  95.         mcall   socket, AF_INET4, IPPROTO_TCP, 0
  96.         cmp     eax, -1
  97.         jz      fail2
  98.         mov     [socketnum], eax
  99.  
  100.         mcall   connect, [socketnum], sockaddr1, 18
  101.  
  102.         mcall   40, 1 shl 7 ; + 7
  103.         call    [con_cls]
  104.  
  105.         mcall   51, 1, thread, mem - 2048
  106.  
  107. mainloop:
  108.         mcall   10
  109.  
  110.         mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
  111.         cmp     eax, -1
  112.         je      mainloop
  113.  
  114.         mov     esi, buffer_ptr
  115.         mov     byte [esi + eax], 0
  116.  
  117.        @@:
  118.         cmp     byte [esi], 0xff        ; 'IAC' = Interpret As Command
  119.         jne     @f
  120.         ; TODO: parse options, for now, we will reply with 'WONT' to everything
  121.         mov     byte [esi + 1], 252     ; WONT
  122.         add     esi, 3                  ; a command is always 3 bytes
  123.         jmp     @r
  124.  
  125.        @@:
  126.         push    esi
  127.  
  128.         cmp     esi, buffer_ptr
  129.         je      .nocommands
  130.  
  131.         mov     edx, buffer_ptr
  132.         sub     esi, buffer_ptr
  133.         xor     edi, edi
  134.         mcall   send, [socketnum]
  135.  
  136.   .nocommands:
  137.         call    [con_write_asciiz]
  138.         jmp     mainloop
  139.  
  140.  
  141. ; write newline and continue main loop
  142.         push    str4
  143. @@:
  144.         call    [con_write_asciiz]
  145.         jmp     main
  146. fail:
  147.         push    str5
  148.         jmp     @b
  149. fail2:
  150.         push    str6
  151.         jmp     @b
  152.  
  153. done:
  154.         push    1
  155.         call    [con_exit]
  156. exit:
  157.         mcall   -1
  158.  
  159.  
  160.  
  161. thread:
  162.         mcall   40, 0
  163.         call    [con_getch2]
  164.         mov     byte [send_data], al
  165.         mcall   send, [socketnum], send_data, 1
  166.         jmp     thread
  167.  
  168. ; data
  169. title   db      'Telnet',0
  170. str1    db      'Telnet v0.1',10,' for KolibriOS # 1250 or later. ',10,10,0
  171. str2    db      '> ',0
  172. str3    db      'Connecting to: ',0
  173. str4    db      10,0
  174. str5    db      'Name resolution failed.',10,10,0
  175. str6    db      'Could not open socket',10,10,0
  176. str7    db      'Got data!',10,10,0
  177.  
  178. sockaddr1:
  179.         dw AF_INET4
  180. .port   dw 23 shl 8
  181. .ip     dd 0
  182.         rb 10
  183.  
  184.  
  185.  
  186. ; import
  187. align 4
  188. @IMPORT:
  189.  
  190. library network, 'network.obj', console, 'console.obj'
  191. import  network,        \
  192.         getaddrinfo,    'getaddrinfo',  \
  193.         freeaddrinfo,   'freeaddrinfo', \
  194.         inet_ntoa,      'inet_ntoa'
  195. import  console,        \
  196.         con_start,      'START',        \
  197.         con_init,       'con_init',     \
  198.         con_write_asciiz,       'con_write_asciiz',     \
  199.         con_exit,       'con_exit',     \
  200.         con_gets,       'con_gets',\
  201.         con_cls,        'con_cls',\
  202.         con_getch2,     'con_getch2'
  203. i_end:
  204.  
  205. socketnum       dd ?
  206. buffer_ptr      rb BUFFERSIZE
  207. send_data       rb 100
  208.  
  209. s       rb      256
  210. align   4
  211. rb      4096    ; stack
  212. mem:
  213.