2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2013-06-07 19:26:03 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; ping.asm - ICMP echo client for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; TODO: more precise timer, ttl, user selectable size/number of packets
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
format binary as ""
|
|
|
|
|
2013-06-07 19:26:03 +02:00
|
|
|
BUFFERSIZE = 1500
|
2013-10-12 15:59:29 +02:00
|
|
|
IDENTIFIER = 0x1337
|
2013-06-07 19:26:03 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
use32
|
|
|
|
org 0x0
|
|
|
|
|
|
|
|
db 'MENUET01' ; signature
|
|
|
|
dd 1 ; header version
|
|
|
|
dd start ; entry point
|
|
|
|
dd I_END ; initialized size
|
|
|
|
dd mem ; required memory
|
|
|
|
dd mem ; stack pointer
|
|
|
|
dd s ; parameters
|
|
|
|
dd 0 ; path
|
|
|
|
|
|
|
|
|
|
|
|
; useful includes
|
2013-06-07 19:26:03 +02:00
|
|
|
include '../../macros.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
purge mov,add,sub
|
2013-06-07 19:26:03 +02:00
|
|
|
include '../../proc32.inc'
|
|
|
|
include '../../dll.inc'
|
|
|
|
include '../../network.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
include 'icmp.inc'
|
|
|
|
|
|
|
|
|
|
|
|
start:
|
|
|
|
; load libraries
|
|
|
|
stdcall dll.Load, @IMPORT
|
|
|
|
test eax, eax
|
|
|
|
jnz exit
|
|
|
|
; initialize console
|
|
|
|
push 1
|
|
|
|
call [con_start]
|
|
|
|
push title
|
|
|
|
push 25
|
|
|
|
push 80
|
|
|
|
push 25
|
|
|
|
push 80
|
|
|
|
call [con_init]
|
|
|
|
; main loop
|
|
|
|
cmp byte[s], 0
|
|
|
|
jne resolve
|
2013-10-13 22:27:48 +02:00
|
|
|
|
|
|
|
push str_welcome
|
|
|
|
call [con_write_asciiz]
|
2013-05-28 19:34:26 +02:00
|
|
|
main:
|
|
|
|
; write prompt
|
2013-10-13 22:27:48 +02:00
|
|
|
push str_prompt
|
2013-05-28 19:34:26 +02:00
|
|
|
call [con_write_asciiz]
|
|
|
|
; read string
|
|
|
|
mov esi, s
|
|
|
|
push 256
|
|
|
|
push esi
|
|
|
|
call [con_gets]
|
|
|
|
; check for exit
|
|
|
|
test eax, eax
|
|
|
|
jz done
|
|
|
|
cmp byte [esi], 10
|
|
|
|
jz done
|
|
|
|
; delete terminating '\n'
|
|
|
|
push esi
|
|
|
|
@@:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jnz @b
|
|
|
|
mov byte [esi-2], al
|
|
|
|
pop esi
|
|
|
|
|
2013-10-13 22:27:48 +02:00
|
|
|
; reset stats
|
|
|
|
mov [stats.tx], 0
|
|
|
|
mov [stats.rx], 0
|
|
|
|
mov [stats.time], 0
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
resolve:
|
|
|
|
; resolve name
|
|
|
|
push esp ; reserve stack place
|
|
|
|
push esp ; fourth parameter
|
|
|
|
push 0 ; third parameter
|
|
|
|
push 0 ; second parameter
|
|
|
|
push s ; first parameter
|
|
|
|
call [getaddrinfo]
|
|
|
|
pop esi
|
|
|
|
; test for error
|
|
|
|
test eax, eax
|
|
|
|
jnz fail
|
|
|
|
|
|
|
|
; convert IP address to decimal notation
|
|
|
|
mov eax, [esi+addrinfo.ai_addr]
|
|
|
|
mov eax, [eax+sockaddr_in.sin_addr]
|
|
|
|
mov [sockaddr1.ip], eax
|
|
|
|
push eax
|
|
|
|
call [inet_ntoa]
|
|
|
|
; write result
|
|
|
|
mov [ip_ptr], eax
|
|
|
|
|
|
|
|
push eax
|
|
|
|
|
|
|
|
; free allocated memory
|
|
|
|
push esi
|
|
|
|
call [freeaddrinfo]
|
|
|
|
|
|
|
|
push str4
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
|
|
|
mcall socket, AF_INET4, SOCK_RAW, IPPROTO_ICMP
|
|
|
|
cmp eax, -1
|
|
|
|
jz fail2
|
|
|
|
mov [socketnum], eax
|
|
|
|
|
|
|
|
mcall connect, [socketnum], sockaddr1, 18
|
|
|
|
|
|
|
|
mcall 40, 1 shl 7 ; + 7
|
|
|
|
; call [con_cls]
|
|
|
|
|
|
|
|
mov [count], 4
|
|
|
|
|
|
|
|
push str3
|
|
|
|
call [con_write_asciiz]
|
2013-10-12 15:59:29 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
push [ip_ptr]
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
push (icmp_packet.length - ICMP_Packet.Data)
|
|
|
|
push str3b
|
|
|
|
call [con_printf]
|
|
|
|
|
|
|
|
mainloop:
|
|
|
|
inc [stats.tx]
|
2013-05-28 19:34:26 +02:00
|
|
|
mcall 26,9
|
|
|
|
mov [time_reference], eax
|
|
|
|
mcall send, [socketnum], icmp_packet, icmp_packet.length, 0
|
|
|
|
|
|
|
|
mcall 23, 300 ; 3 seconds time-out
|
|
|
|
mcall 26,9
|
2013-10-12 21:39:47 +02:00
|
|
|
sub eax, [time_reference]
|
|
|
|
xor edx, edx
|
|
|
|
mov cx, 10
|
|
|
|
mul cx
|
|
|
|
mov [time_reference], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, MSG_DONTWAIT
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je .no_response
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
sub eax, ICMP_Packet.Data
|
|
|
|
jb .no_response ; FIXME: use other error message?
|
|
|
|
mov [recvd], eax
|
|
|
|
|
|
|
|
cmp word[buffer_ptr + ICMP_Packet.Identifier], IDENTIFIER
|
|
|
|
jne .no_response ; FIXME: use other error message?
|
|
|
|
|
|
|
|
; OK, we have a response, update stats and let the user know
|
|
|
|
inc [stats.rx]
|
|
|
|
mov eax, [time_reference]
|
|
|
|
add [stats.time], eax
|
|
|
|
|
|
|
|
push str11 ; TODO: print IP address of packet sender
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; validate the packet
|
|
|
|
lea esi, [buffer_ptr + ICMP_Packet.Data]
|
2013-10-12 15:59:29 +02:00
|
|
|
mov ecx, [recvd]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov edi, icmp_packet.data
|
2013-10-12 15:59:29 +02:00
|
|
|
repe cmpsb
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .miscomp
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; All OK, print to the user!
|
2013-05-28 19:34:26 +02:00
|
|
|
push [time_reference]
|
2013-10-12 15:59:29 +02:00
|
|
|
movzx eax, word[buffer_ptr + ICMP_Packet.SequenceNumber]
|
|
|
|
push eax
|
|
|
|
push [recvd]
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
push str7
|
|
|
|
call [con_printf]
|
|
|
|
|
|
|
|
jmp continue
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; Error in packet, print it to user
|
2013-05-28 19:34:26 +02:00
|
|
|
.miscomp:
|
|
|
|
sub edi, icmp_packet.data
|
|
|
|
push edi
|
|
|
|
push str9
|
|
|
|
call [con_printf]
|
|
|
|
jmp continue
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; Timeout!
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_response:
|
|
|
|
push str8
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; Send more ICMP packets ?
|
2013-05-28 19:34:26 +02:00
|
|
|
continue:
|
|
|
|
dec [count]
|
|
|
|
jz done
|
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
mcall 5, 100 ; wait a second
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
inc [icmp_packet.seq]
|
|
|
|
jmp mainloop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; Done..
|
2013-05-28 19:34:26 +02:00
|
|
|
done:
|
2013-10-13 22:27:48 +02:00
|
|
|
cmp [stats.rx], 0
|
|
|
|
jne @f
|
|
|
|
xor eax, eax
|
|
|
|
jmp .zero
|
|
|
|
@@:
|
2013-10-12 15:59:29 +02:00
|
|
|
xor edx, edx
|
|
|
|
mov eax, [stats.time]
|
|
|
|
div [stats.rx]
|
2013-10-13 22:27:48 +02:00
|
|
|
.zero:
|
2013-10-12 15:59:29 +02:00
|
|
|
push eax
|
|
|
|
push [stats.rx]
|
|
|
|
push [stats.tx]
|
|
|
|
push str12
|
|
|
|
call [con_printf]
|
2013-10-13 22:27:48 +02:00
|
|
|
jmp main
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
; DNS error
|
2013-05-28 19:34:26 +02:00
|
|
|
fail:
|
|
|
|
push str5
|
|
|
|
call [con_write_asciiz]
|
2013-10-13 22:27:48 +02:00
|
|
|
jmp main
|
2013-10-12 15:59:29 +02:00
|
|
|
|
|
|
|
; Socket error
|
2013-05-28 19:34:26 +02:00
|
|
|
fail2:
|
|
|
|
push str6
|
|
|
|
call [con_write_asciiz]
|
2013-10-13 22:27:48 +02:00
|
|
|
jmp main
|
|
|
|
|
|
|
|
; Finally.. exit!
|
|
|
|
exit:
|
|
|
|
push 1
|
|
|
|
call [con_exit]
|
|
|
|
|
|
|
|
mcall -1
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
; data
|
2013-10-13 22:27:48 +02:00
|
|
|
title db 'ICMP echo (ping) client',0
|
|
|
|
str_welcome db 'Please enter the hostname or IP-address of the host you want to ping,',10
|
|
|
|
db 'or just press enter to exit.',10,0
|
|
|
|
str_prompt db 10,'> ',0
|
2013-10-12 15:59:29 +02:00
|
|
|
str3 db 'Pinging to ',0
|
|
|
|
str3b db ' with %u data bytes',10,0
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
str4 db 10,0
|
|
|
|
str5 db 'Name resolution failed.',10,0
|
|
|
|
str6 db 'Could not open socket',10,0
|
2013-10-12 15:59:29 +02:00
|
|
|
|
|
|
|
str11 db 'Answer: ',0
|
2013-10-12 21:39:47 +02:00
|
|
|
str7 db 'bytes=%u seq=%u time=%u ms',10,0
|
2013-10-12 15:59:29 +02:00
|
|
|
str8 db 'timeout!',10,0
|
|
|
|
str9 db 'miscompare at offset %u',10,0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 21:39:47 +02:00
|
|
|
str12 db 10,'Ping stats:',10,'%u packets sent, %u packets received',10,'average response time=%u ms',10,0
|
2013-10-12 15:59:29 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
sockaddr1:
|
|
|
|
dw AF_INET4
|
|
|
|
.port dw 0
|
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
|
|
|
|
|
|
|
time_reference dd ?
|
|
|
|
ip_ptr dd ?
|
|
|
|
count dd ?
|
2013-10-12 15:59:29 +02:00
|
|
|
recvd dd ? ; received number of bytes in last packet
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-10-12 15:59:29 +02:00
|
|
|
stats:
|
2013-10-13 22:27:48 +02:00
|
|
|
.tx dd ?
|
|
|
|
.rx dd ?
|
|
|
|
.time dd ?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; import
|
|
|
|
align 4
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library network, 'network.obj', console, 'console.obj'
|
|
|
|
import network, \
|
|
|
|
getaddrinfo, 'getaddrinfo', \
|
|
|
|
freeaddrinfo, 'freeaddrinfo', \
|
|
|
|
inet_ntoa, 'inet_ntoa'
|
|
|
|
|
|
|
|
import console, \
|
|
|
|
con_start, 'START', \
|
|
|
|
con_init, 'con_init', \
|
|
|
|
con_write_asciiz, 'con_write_asciiz', \
|
|
|
|
con_printf, 'con_printf', \
|
|
|
|
con_exit, 'con_exit', \
|
|
|
|
con_gets, 'con_gets',\
|
|
|
|
con_cls, 'con_cls',\
|
|
|
|
con_getch2, 'con_getch2',\
|
|
|
|
con_set_cursor_pos, 'con_set_cursor_pos'
|
|
|
|
|
|
|
|
socketnum dd ?
|
|
|
|
|
|
|
|
icmp_packet: db 8 ; type
|
|
|
|
db 0 ; code
|
|
|
|
dw 0 ;
|
2013-10-12 15:59:29 +02:00
|
|
|
.id dw IDENTIFIER ; identifier
|
|
|
|
.seq dw 0x0000 ; sequence number
|
|
|
|
.data db 'abcdefghijklmnopqrstuvwxyz012345'
|
2013-05-28 19:34:26 +02:00
|
|
|
.length = $ - icmp_packet
|
|
|
|
|
|
|
|
I_END:
|
|
|
|
|
|
|
|
buffer_ptr rb BUFFERSIZE
|
|
|
|
|
|
|
|
s rb 1024
|
|
|
|
rb 4096 ; stack
|
|
|
|
mem:
|