Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2012. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  ping.asm - ICMP echo 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.  
  16. format binary as ""
  17.  
  18. use32
  19.         org     0x0
  20.  
  21.         db      'MENUET01'      ; signature
  22.         dd      1               ; header version
  23.         dd      start           ; entry point
  24.         dd      I_END           ; initialized size
  25.         dd      mem             ; required memory
  26.         dd      mem             ; stack pointer
  27.         dd      s               ; parameters
  28.         dd      0               ; path
  29.  
  30.  
  31. BUFFERSIZE      equ 1500
  32. ; useful includes
  33. include '../macros.inc'
  34. purge mov,add,sub
  35. include '../proc32.inc'
  36. include '../dll.inc'
  37. include '../network.inc'
  38.  
  39. include 'icmp.inc'
  40.  
  41.  
  42. start:
  43. ; load libraries
  44.         stdcall dll.Load, @IMPORT
  45.         test    eax, eax
  46.         jnz     exit
  47. ; initialize console
  48.         push    1
  49.         call    [con_start]
  50.         push    title
  51.         push    25
  52.         push    80
  53.         push    25
  54.         push    80
  55.         call    [con_init]
  56. ; main loop
  57.         cmp     byte[s], 0
  58.         jne     resolve
  59. main:
  60. ; write prompt
  61.         push    str2
  62.         call    [con_write_asciiz]
  63. ; read string
  64.         mov     esi, s
  65.         push    256
  66.         push    esi
  67.         call    [con_gets]
  68. ; check for exit
  69.         test    eax, eax
  70.         jz      done
  71.         cmp     byte [esi], 10
  72.         jz      done
  73. ; delete terminating '\n'
  74.         push    esi
  75. @@:
  76.         lodsb
  77.         test    al, al
  78.         jnz     @b
  79.         mov     byte [esi-2], al
  80.         pop     esi
  81.  
  82. resolve:
  83. ; resolve name
  84.         push    esp     ; reserve stack place
  85.         push    esp     ; fourth parameter
  86.         push    0       ; third parameter
  87.         push    0       ; second parameter
  88.         push    s       ; first parameter
  89.         call    [getaddrinfo]
  90.         pop     esi
  91. ; test for error
  92.         test    eax, eax
  93.         jnz     fail
  94.  
  95. ; convert IP address to decimal notation
  96.         mov     eax, [esi+addrinfo.ai_addr]
  97.         mov     eax, [eax+sockaddr_in.sin_addr]
  98.         mov     [sockaddr1.ip], eax
  99.         push    eax
  100.         call    [inet_ntoa]
  101. ; write result
  102.         mov     [ip_ptr], eax
  103.  
  104.         push    eax
  105.  
  106. ; free allocated memory
  107.         push    esi
  108.         call    [freeaddrinfo]
  109.  
  110.         push    str4
  111.         call    [con_write_asciiz]
  112.  
  113.         mcall   socket, AF_INET4, SOCK_RAW, IPPROTO_ICMP
  114.         cmp     eax, -1
  115.         jz      fail2
  116.         mov     [socketnum], eax
  117.  
  118.         mcall   connect, [socketnum], sockaddr1, 18
  119.  
  120.         mcall   40, 1 shl 7 ; + 7
  121. ;        call    [con_cls]
  122.  
  123.         mov     [count], 4
  124.  
  125. mainloop:
  126.         push    str3
  127.         call    [con_write_asciiz]
  128.         push    [ip_ptr]
  129.         call    [con_write_asciiz]
  130.  
  131.         mcall   26,9
  132.         mov     [time_reference], eax
  133.         mcall   send, [socketnum], icmp_packet, icmp_packet.length, 0
  134.  
  135.         mcall   23, 300 ; 3 seconds time-out
  136.         mcall   26,9
  137.         neg     [time_reference]
  138.         add     [time_reference], eax
  139.  
  140.         mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
  141.         cmp     eax, -1
  142.         je      .no_response
  143.  
  144. ; validate the packet
  145.         lea     esi, [buffer_ptr + ICMP_Packet.Data]
  146.         mov     edi, icmp_packet.data
  147.         mov     ecx, 32/4
  148.         repe    cmpsd
  149.         jne     .miscomp
  150.  
  151.         push    [time_reference]
  152.         push    str7
  153.         call    [con_printf]
  154.  
  155.         jmp     continue
  156.  
  157.   .miscomp:
  158.         sub     edi, icmp_packet.data
  159.         push    edi
  160.         push    str9
  161.         call    [con_printf]
  162.         jmp     continue
  163.  
  164.   .no_response:
  165.         push    str8
  166.         call    [con_write_asciiz]
  167.  
  168.    continue:
  169.         dec     [count]
  170.         jz      done
  171.         mcall   5, 100  ; wait a second
  172.         inc     [icmp_packet.id]
  173.         jmp     mainloop
  174.  
  175.  
  176.  
  177. done:
  178.         push    str10
  179.         call    [con_write_asciiz]
  180.         call    [con_getch2]
  181.         push    1
  182.         call    [con_exit]
  183. exit:
  184.         mcall   -1
  185.  
  186. fail:
  187.         push    str5
  188.         call    [con_write_asciiz]
  189.         jmp     done
  190. fail2:
  191.         push    str6
  192.         call    [con_write_asciiz]
  193.         jmp     done
  194.  
  195.  
  196. ; data
  197. title   db      'ICMP - echo client',0
  198. str2    db      '> ',0
  199. str3    db      'Ping to ',0
  200. str4    db      10,0
  201. str5    db      'Name resolution failed.',10,0
  202. str6    db      'Could not open socket',10,0
  203. str7    db      ' time= %u0ms',10,0
  204. str8    db      ' timeout!',10,0
  205. str9    db      ' miscompare at offset %u',10,0
  206. str10   db      10,'Press any key to exit',0
  207.  
  208. sockaddr1:
  209.         dw AF_INET4
  210. .port   dw 0
  211. .ip     dd 0
  212.         rb 10
  213.  
  214. time_reference  dd ?
  215. ip_ptr          dd ?
  216. count           dd ?
  217.  
  218.  
  219. ; import
  220. align 4
  221. @IMPORT:
  222.  
  223. library network, 'network.obj', console, 'console.obj'
  224. import  network,        \
  225.         getaddrinfo,    'getaddrinfo',  \
  226.         freeaddrinfo,   'freeaddrinfo', \
  227.         inet_ntoa,      'inet_ntoa'
  228.  
  229. import  console,        \
  230.         con_start,      'START',        \
  231.         con_init,       'con_init',     \
  232.         con_write_asciiz,       'con_write_asciiz',     \
  233.         con_printf,       'con_printf',     \
  234.         con_exit,       'con_exit',     \
  235.         con_gets,       'con_gets',\
  236.         con_cls,        'con_cls',\
  237.         con_getch2,     'con_getch2',\
  238.         con_set_cursor_pos, 'con_set_cursor_pos'
  239.  
  240. socketnum       dd ?
  241.  
  242. icmp_packet:    db 8            ; type
  243.                 db 0            ; code
  244.                 dw 0            ;
  245.  .id            dw 0x0000       ; identifier
  246.  .seq           dw 0x0001       ; sequence number
  247.  .data          db 'abcdefghijklmnopqrstuvwxyz012345678'
  248.  .length = $ - icmp_packet
  249.  
  250. I_END:
  251.  
  252. buffer_ptr      rb BUFFERSIZE
  253.  
  254. s               rb 1024
  255.                 rb 4096    ; stack
  256. mem:
  257.