1 ! Copyright (C) 2010 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors byte-arrays calendar checksums
5 checksums.internet combinators combinators.smart continuations
6 destructors io.sockets io.sockets.icmp io.timeouts kernel pack
13 TUPLE: echo type identifier sequence data ;
15 : <echo> ( sequence data -- echo )
16 [ 8 16 random-bits ] 2dip echo boa ;
18 : echo>byte-array ( echo -- byte-array )
21 [ type>> 0 0 ] ! code checksum
24 ] output>array "CCSSS" pack-be
25 ] [ data>> ] bi append [
26 internet checksum-bytes 2 4
27 ] keep replace-slice ;
29 : byte-array>echo ( byte-array -- echo )
30 dup internet checksum-bytes B{ 0 0 } assert=
32 "CCSSS" unpack-be { 0 3 4 } swap nths first3
35 : send-ping ( addr datagram -- )
36 [ 0 { } <echo> echo>byte-array ] 2dip send ;
38 : recv-ping ( datagram -- echo )
39 receive drop 20 tail byte-array>echo ;
43 : ping ( host -- reply )
44 <icmp> resolve-host [ icmp4? ] filter random
46 1 seconds over set-timeout
47 [ [ send-ping ] [ recv-ping ] bi ] with-disposal ;
49 : local-ping ( -- reply )
52 : alive? ( host -- ? )
53 [ ping drop t ] [ 2drop f ] recover ;