Subversion Repositories Kolibri OS

Rev

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

  1. use32
  2.         org     0x0
  3. ; standard header
  4.         db      'MENUET01'      ; signature
  5.         dd      1               ; header version
  6.         dd      start           ; entry point
  7.         dd      I_END           ; initialized size
  8.         dd      mem             ; required memory
  9.         dd      mem             ; stack pointer
  10.         dd      0               ; parameters
  11.         dd      0               ; path
  12.  
  13.  
  14. BUFFERSIZE      equ 1500
  15. ; useful includes
  16. include '../macros.inc'
  17. purge mov,add,sub
  18. include '../proc32.inc'
  19. include '../dll.inc'
  20.  
  21. include '../network.inc'
  22.  
  23.  
  24.  
  25. ; ICMP types & codes
  26.  
  27. ICMP_ECHOREPLY                  equ 0               ; echo reply message
  28.  
  29. ICMP_UNREACH                    equ 3
  30. ICMP_UNREACH_NET                equ  0               ; bad net
  31. ICMP_UNREACH_HOST               equ  1               ; bad host
  32. ICMP_UNREACH_PROTOCOL           equ  2               ; bad protocol
  33. ICMP_UNREACH_PORT               equ  3               ; bad port
  34. ICMP_UNREACH_NEEDFRAG           equ  4               ; IP_DF caused drop
  35. ICMP_UNREACH_SRCFAIL            equ  5               ; src route failed
  36. ICMP_UNREACH_NET_UNKNOWN        equ  6               ; unknown net
  37. ICMP_UNREACH_HOST_UNKNOWN       equ  7               ; unknown host
  38. ICMP_UNREACH_ISOLATED           equ  8               ; src host isolated
  39. ICMP_UNREACH_NET_PROHIB         equ  9               ; prohibited access
  40. ICMP_UNREACH_HOST_PROHIB        equ 10              ; ditto
  41. ICMP_UNREACH_TOSNET             equ 11              ; bad tos for net
  42. ICMP_UNREACH_TOSHOST            equ 12              ; bad tos for host
  43. ICMP_UNREACH_FILTER_PROHIB      equ 13              ; admin prohib
  44. ICMP_UNREACH_HOST_PRECEDENCE    equ 14             ; host prec vio.
  45. ICMP_UNREACH_PRECEDENCE_CUTOFF  equ 15           ; prec cutoff
  46.  
  47. ICMP_SOURCEQUENCH               equ 4               ; Packet lost, slow down
  48.  
  49. ICMP_REDIRECT                   equ 5               ; shorter route, codes:
  50. ICMP_REDIRECT_NET               equ  0               ; for network
  51. ICMP_REDIRECT_HOST              equ  1               ; for host
  52. ICMP_REDIRECT_TOSNET            equ  2               ; for tos and net
  53. ICMP_REDIRECT_TOSHOST           equ  3               ; for tos and host
  54.  
  55. ICMP_ALTHOSTADDR                equ 6               ; alternate host address
  56. ICMP_ECHO                       equ  8               ; echo service
  57. ICMP_ROUTERADVERT               equ  9               ; router advertisement
  58. ICMP_ROUTERADVERT_NORMAL        equ  0                  ; normal advertisement
  59. ICMP_ROUTERADVERT_NOROUTE_COMMON equ 16         ; selective routing
  60.  
  61. ICMP_ROUTERSOLICIT              equ 10              ; router solicitation
  62. ICMP_TIMXCEED                   equ 11              ; time exceeded, code:
  63. ICMP_TIMXCEED_INTRANS           equ 0               ; ttl==0 in transit
  64. ICMP_TIMXCEED_REASS             equ 1               ; ttl==0 in reass
  65.  
  66. ICMP_PARAMPROB                  equ 12               ; ip header bad
  67. ICMP_PARAMPROB_ERRATPTR         equ 0            ; error at param ptr
  68. ICMP_PARAMPROB_OPTABSENT        equ 1            ; req. opt. absent
  69. ICMP_PARAMPROB_LENGTH           equ 2            ; bad length
  70.  
  71. ICMP_TSTAMP                     equ 13              ; timestamp request
  72. ICMP_TSTAMPREPLY                equ 14              ; timestamp reply
  73. ICMP_IREQ                       equ 15              ; information request
  74. ICMP_IREQREPLY                  equ 16              ; information reply
  75. ICMP_MASKREQ                    equ 17              ; address mask request
  76. ICMP_MASKREPLY                  equ 18              ; address mask reply
  77. ICMP_TRACEROUTE                 equ 30              ; traceroute
  78. ICMP_DATACONVERR                equ 31              ; data conversion error
  79. ICMP_MOBILE_REDIRECT            equ 32              ; mobile host redirect
  80. ICMP_IPV6_WHEREAREYOU           equ 33              ; IPv6 where-are-you
  81. ICMP_IPV6_IAMHERE               equ 34              ; IPv6 i-am-here
  82. ICMP_MOBILE_REGREQUEST          equ 35              ; mobile registration req
  83. ICMP_MOBILE_REGREPLY            equ 36              ; mobile registreation reply
  84. ICMP_SKIP                       equ 39              ; SKIP
  85.  
  86. ICMP_PHOTURIS                   equ 40              ; Photuris
  87. ICMP_PHOTURIS_UNKNOWN_INDEX     equ 1                ; unknown sec index
  88. ICMP_PHOTURIS_AUTH_FAILED       equ 2                ; auth failed
  89. ICMP_PHOTURIS_DECRYPT_FAILED    equ 3                ; decrypt failed
  90.  
  91.  
  92.  
  93. virtual at 0
  94.         ICMP_Packet:
  95.         .Type           db   ?
  96.         .Code           db   ?
  97.         .Checksum       dw   ?
  98.         .Identifier     dw   ?
  99.         .SequenceNumber dw   ?
  100.         .Data:
  101. end virtual
  102.  
  103.  
  104. ; entry point
  105. start:
  106. ; load libraries
  107.         stdcall dll.Load, @IMPORT
  108.         test    eax, eax
  109.         jnz     exit
  110. ; initialize console
  111.         push    1
  112.         call    [con_start]
  113.         push    title
  114.         push    25
  115.         push    80
  116.         push    25
  117.         push    80
  118.         call    [con_init]
  119. ; main loop
  120.         push    str1
  121.         call    [con_write_asciiz]
  122. main:
  123. ; write prompt
  124.         push    str2
  125.         call    [con_write_asciiz]
  126. ; read string
  127.         mov     esi, s
  128.         push    256
  129.         push    esi
  130.         call    [con_gets]
  131. ; check for exit
  132.         test    eax, eax
  133.         jz      done
  134.         cmp     byte [esi], 10
  135.         jz      done
  136. ; delete terminating '\n'
  137.         push    esi
  138. @@:
  139.         lodsb
  140.         test    al, al
  141.         jnz     @b
  142.         mov     byte [esi-2], al
  143.         pop     esi
  144. ; resolve name
  145.         push    esp     ; reserve stack place
  146.         push    esp     ; fourth parameter
  147.         push    0       ; third parameter
  148.         push    0       ; second parameter
  149.         push    esi     ; first parameter
  150.         call    [getaddrinfo]
  151.         pop     esi
  152. ; test for error
  153.         test    eax, eax
  154.         jnz     fail
  155.  
  156. ; convert IP address to decimal notation
  157.         mov     eax, [esi+addrinfo.ai_addr]
  158.         mov     eax, [eax+sockaddr_in.sin_addr]
  159.         mov     [sockaddr1.ip], eax
  160.         push    eax
  161.         call    [inet_ntoa]
  162. ; write result
  163.         mov     [ip_ptr], eax
  164.  
  165.         push    eax
  166.  
  167. ; free allocated memory
  168.         push    esi
  169.         call    [freeaddrinfo]
  170.  
  171.         push    str4
  172.         call    [con_write_asciiz]
  173.  
  174.         mcall   socket, AF_INET4, SOCK_RAW, IPPROTO_ICMP
  175.         cmp     eax, -1
  176.         jz      fail2
  177.         mov     [socketnum], eax
  178.  
  179.         mcall   connect, [socketnum], sockaddr1, 18
  180.  
  181.         mcall   40, 1 shl 7 ; + 7
  182. ;        call    [con_cls]
  183.  
  184.         mov     [count], 4
  185.  
  186. mainloop:
  187.         push    str3
  188.         call    [con_write_asciiz]
  189.         push    [ip_ptr]
  190.         call    [con_write_asciiz]
  191.  
  192.         mcall   26,9
  193.         mov     [time_reference], eax
  194.         mcall   send, [socketnum], icmp_packet, icmp_packet.length, 0
  195.  
  196.         mcall   23, 300 ; 3 seconds time-out
  197.         mcall   26,9
  198.         neg     [time_reference]
  199.         add     [time_reference], eax
  200.  
  201.         mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
  202.         cmp     eax, -1
  203.         je      .no_response
  204.  
  205. ; validate the packet
  206.         lea     esi, [buffer_ptr + ICMP_Packet.Data]
  207.         mov     edi, icmp_packet.data
  208.         mov     ecx, 32/4
  209.         repe    cmpsd
  210.         jne     .miscomp
  211.  
  212.         push    [time_reference]
  213.         push    str7
  214.         call    [con_printf]
  215.  
  216.         jmp     continue
  217.  
  218.   .miscomp:
  219.         sub     edi, icmp_packet.data
  220.         push    edi
  221.         push    str9
  222.         call    [con_printf]
  223.         jmp     continue
  224.  
  225.   .no_response:
  226.         push    str8
  227.         call    [con_write_asciiz]
  228.  
  229.    continue:
  230.         dec     [count]
  231.         jz      done
  232.         mcall   5, 100  ; wait a second
  233.         inc     [icmp_packet.id]
  234.         jmp     mainloop
  235.  
  236.  
  237.  
  238. done:
  239.         push    str10
  240.         call    [con_write_asciiz]
  241.         call    [con_getch2]
  242.         push    1
  243.         call    [con_exit]
  244. exit:
  245.         mcall   -1
  246.  
  247. fail:
  248.         push    str5
  249.         call    [con_write_asciiz]
  250.         jmp     done
  251. fail2:
  252.         push    str6
  253.         call    [con_write_asciiz]
  254.         jmp     done
  255.  
  256.  
  257. ; data
  258. title   db      'ICMP - test application',0
  259. str1    db      'ICMP test application v0.1',10,' for KolibriOS # 1540 or later. ',10,10,0
  260. str2    db      '> ',0
  261. str3    db      'Ping to: ',0
  262. str4    db      10,0
  263. str5    db      'Name resolution failed.',10,10,0
  264. str6    db      'Could not open socket',10,10,0
  265. str7    db      ' time= %u0ms',10,0
  266. str8    db      ' timeout!',10,0
  267. str9    db      ' miscompare at offset %u',10,0
  268. str10   db      10,10,'Press any key to exit',0
  269.  
  270. sockaddr1:
  271.         dw AF_INET4
  272. .port   dw 0
  273. .ip     dd 0
  274.         rb 10
  275.  
  276. time_reference  dd ?
  277. ip_ptr          dd ?
  278. count           dd ?
  279.  
  280.  
  281. ; import
  282. align 4
  283. @IMPORT:
  284.  
  285. library network, 'network.obj', console, 'console.obj'
  286. import  network,        \
  287.         getaddrinfo,    'getaddrinfo',  \
  288.         freeaddrinfo,   'freeaddrinfo', \
  289.         inet_ntoa,      'inet_ntoa'
  290.  
  291. import  console,        \
  292.         con_start,      'START',        \
  293.         con_init,       'con_init',     \
  294.         con_write_asciiz,       'con_write_asciiz',     \
  295.         con_printf,       'con_printf',     \
  296.         con_exit,       'con_exit',     \
  297.         con_gets,       'con_gets',\
  298.         con_cls,        'con_cls',\
  299.         con_getch2,     'con_getch2',\
  300.         con_set_cursor_pos, 'con_set_cursor_pos'
  301.  
  302. socketnum       dd ?
  303.  
  304. icmp_packet:    db 8            ; type
  305.                 db 0            ; code
  306.                 dw 0            ;
  307.  .id            dw 0x0000       ; identifier
  308.  .seq           dw 0x0001       ; sequence number
  309.  .data          db 'abcdefghijklmnopqrstuvwxyz012345678'
  310.  .length = $ - icmp_packet
  311.  
  312. I_END:
  313.  
  314. buffer_ptr      rb BUFFERSIZE
  315.  
  316. s       rb 256
  317. align   4
  318. rb      4096    ; stack
  319. mem:
  320.