Subversion Repositories Kolibri OS

Rev

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

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