Paste: io.sockets.icmp

Author: mrjbq7
Mode: factor
Date: Sat, 4 Sep 2010 20:48:56
Plain Text |
USING: accessors alien.c-types alien.data classes.struct combinators
continuations grouping kernel io.binary io.sockets io.sockets.private
math.parser memoize sequences splitting system vocabs.parser ;

IN: io.sockets.icmp

<< {
    { [ os windows? ] [ "windows.winsock" ] }
    { [ os unix? ] [ "unix.ffi" ] }
} cond use-vocab >>

<PRIVATE

MEMO: IPPROTO_ICMP4 ( -- protocol )
    "icmp" getprotobyname proto>> ;

MEMO: IPPROTO_ICMP6 ( -- protocol )
    "ipv6-icmp" getprotobyname proto>> ;

PRIVATE>

TUPLE: icmp4 host ;

C: <icmp4> icmp4

M: icmp4 protocol drop IPPROTO_ICMP4 ;

! FIXME: The following is "mostly" the same as inet4 (port = 0)

M: icmp4 inet-ntop ( data addrspec -- str )
    drop 4 memory>byte-array [ number>string ] { } map-as "." join ;

M: icmp4 inet-pton ( str addrspec -- data )
    drop [ parse-inet4 ] [ invalid-inet4 ] recover ;

M: icmp4 address-size drop 4 ;

M: icmp4 protocol-family drop PF_INET ;

M: icmp4 sockaddr-size drop sockaddr-in heap-size ;

M: icmp4 empty-sockaddr drop sockaddr-in <struct> ;

M: icmp4 make-sockaddr ( inet -- sockaddr )
    sockaddr-in <struct>
        AF_INET >>family
        swap [ host>> "0.0.0.0" or ]
            [ inet-pton *uint >>addr ] bi ;

M: icmp4 parse-sockaddr ( sockaddr-in addrspec -- newaddrspec )
    [ addr>> <uint> ] dip inet-ntop <icmp4> ;



TUPLE: icmp6 host ;

C: <icmp6> icmp6

M: icmp6 protocol drop IPPROTO_ICMP6 ;

! FIXME: The following is "mostly" the same as inet6 (port = 0)

M: icmp6 inet-ntop ( data addrspec -- str )
    drop 16 memory>byte-array 2 <groups> [ be> >hex ] map ":" join ;

M: icmp6 inet-pton ( str addrspec -- data )
    drop [
        "::" split1 [ parse-inet6 ] bi@ pad-inet6 inet6-bytes
    ] [ invalid-inet6 ] recover ;

M: icmp6 address-size drop 16 ;

M: icmp6 protocol-family drop PF_INET6 ;

M: icmp6 sockaddr-size drop sockaddr-in6 heap-size ;

M: icmp6 empty-sockaddr drop sockaddr-in6 <struct> ;

M: icmp6 make-sockaddr ( inet -- sockaddr )
    sockaddr-in6 <struct>
        AF_INET6 >>family
        swap [ host>> "::" or ]
            [ inet-pton >>addr ] bi ;

M: icmp6 parse-sockaddr ( sockaddr-in addrspec -- newaddrspec )
    [ addr>> ] dip inet-ntop <icmp6> ;

Annotation: better

Author: mrjbq7
Mode: factor
Date: Sat, 4 Sep 2010 22:33:01
Plain Text |
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: accessors combinators generic kernel io.sockets
io.sockets.private memoize sequences system vocabs.parser ;

IN: io.sockets.icmp

<< {
    { [ os windows? ] [ "windows.winsock" ] }
    { [ os unix? ] [ "unix.ffi" ] }
} cond use-vocab >>

<PRIVATE

MEMO: IPPROTO_ICMP4 ( -- protocol )
    "icmp" getprotobyname proto>> ;

MEMO: IPPROTO_ICMP6 ( -- protocol )
    "ipv6-icmp" getprotobyname proto>> ;

PRIVATE>

TUPLE: icmp4 < ipv4 ;

C: <icmp4> icmp4

M: icmp4 protocol drop IPPROTO_ICMP4 ;

M: icmp4 port>> drop 0 ;


TUPLE: icmp6 host ;

C: <icmp6> icmp6

M: icmp6 protocol drop IPPROTO_ICMP6 ;

M: icmp6 port>> drop 0 ;


GENERIC: with-icmp ( addrspec -- addrspec )

M: ipv4 with-icmp host>> <icmp4> ;

M: ipv6 with-icmp host>> <icmp6> ;


TUPLE: icmp < hostname ;

C: <icmp> icmp

M: icmp resolve-host
    M\ icmp resolve-host (call-next-method)
    [ with-icmp ] map ;

Annotation: try again

Author: mrjbq7
Mode: factor
Date: Sat, 4 Sep 2010 22:38:27
Plain Text |
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: accessors combinators generic kernel io.sockets
io.sockets.private memoize sequences system vocabs.parser ;

IN: io.sockets.icmp

<< {
    { [ os windows? ] [ "windows.winsock" ] }
    { [ os unix? ] [ "unix.ffi" ] }
} cond use-vocab >>

<PRIVATE

MEMO: IPPROTO_ICMP4 ( -- protocol )
    "icmp" getprotobyname proto>> ;

MEMO: IPPROTO_ICMP6 ( -- protocol )
    "ipv6-icmp" getprotobyname proto>> ;

PRIVATE>

GENERIC: with-icmp ( addrspec -- addrspec )


TUPLE: icmp4 < ipv4 ;

C: <icmp4> icmp4

M: icmp4 protocol drop IPPROTO_ICMP4 ;

M: icmp4 port>> drop 0 ;

M: icmp4 parse-sockaddr
    M\ icmp4 parse-sockaddr (call-next-method)
    with-icmp ;


TUPLE: icmp6 host ;

C: <icmp6> icmp6

M: icmp6 protocol drop IPPROTO_ICMP6 ;

M: icmp6 port>> drop 0 ;

M: icmp6 parse-sockaddr
    M\ icmp6 parse-sockaddr (call-next-method)
    with-icmp ;



M: ipv4 with-icmp host>> <icmp4> ;

M: ipv6 with-icmp host>> <icmp6> ;


TUPLE: icmp < hostname ;

C: <icmp> icmp

M: icmp resolve-host
    M\ icmp resolve-host (call-next-method)
    [ with-icmp ] map ;

New Annotation

Summary:
Author:
Mode:
Body: