PageRenderTime 31ms CodeModel.GetById 23ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/unmaintained/sniffer/io/bsd/bsd.factor

http://github.com/abeaumont/factor
Unknown | 89 lines | 69 code | 20 blank | 0 comment | 0 complexity | eef29936dda2c7074cd71a8b687ffc13 MD5 | raw file
 1! Copyright (C) 2007 Elie Chaftari, Doug Coleman.
 2! See http://factorcode.org/license.txt for BSD license.
 3USING: alien.c-types alien.syntax destructors hexdump io
 4io.buffers io.nonblocking io.sockets
 5io.unix.backend io.unix.files kernel libc locals math qualified
 6sequences io.sniffer.backend ;
 7QUALIFIED: unix
 8IN: io.sniffer.bsd
 9
10M: unix-io destruct-handle ( obj -- ) unix:close ;
11
12C-UNION: ifreq_props "sockaddr-in" "short" "int" "caddr_t" ;
13C-STRUCT: ifreq { { "char" 16 } "name" } { "ifreq_props" "props" } ;
14
15TUPLE: sniffer-spec path ifname ;
16
17C: <sniffer-spec> sniffer-spec
18
19: IOCPARM_MASK   0x1fff ; inline
20: IOCPARM_MAX    IOCPARM_MASK 1+ ; inline
21: IOC_VOID       0x20000000 ; inline
22: IOC_OUT        0x40000000 ; inline
23: IOC_IN         0x80000000 ; inline
24: IOC_INOUT      IOC_IN IOC_OUT bitor ; inline
25: IOC_DIRMASK    0xe0000000 ; inline
26
27:: ioc ( inout group num len -- n )
28    group first 8 shift num bitor
29    len IOCPARM_MASK bitand 16 shift bitor
30    inout bitor ;
31
32: io-len ( type -- n )
33    dup zero? [ heap-size ] unless ;
34
35: io ( group num -- n )
36    IOC_VOID -rot 0 io-len ioc ;
37
38: ior ( group num type -- n )
39    IOC_OUT -roll io-len ioc ;
40
41: iow ( group num type -- n )
42    IOC_IN -roll io-len ioc ;
43
44: iowr ( group num type -- n )
45    IOC_INOUT -roll io-len ioc ;
46
47: BIOCGBLEN ( -- n ) "B" 102 "uint" ior ; inline
48: BIOCSETIF ( -- n ) "B" 108 "ifreq" iow ; inline
49: BIOCPROMISC ( -- n ) "B" 105 io ; inline 
50: BIOCIMMEDIATE ( -- n ) "B" 112 "uint" iow ; inline
51
52: make-ifreq-props ( ifname -- ifreq )
53    "ifreq" <c-object>
54    12 <short> 16 0 pad-right over set-ifreq-props
55    swap malloc-char-string dup free-always
56    over set-ifreq-name ;
57
58: make-ioctl-buffer ( fd -- buffer )
59    BIOCGBLEN "char*" <c-object>
60    [ unix:ioctl io-error ] keep
61    *int <buffer> ;
62
63: ioctl-BIOSETIF ( fd ifreq -- )
64    >r BIOCSETIF r> unix:ioctl io-error ;
65
66: ioctl-BIOPROMISC ( fd -- )
67    BIOCPROMISC f unix:ioctl io-error ;
68
69: ioctl-BIOCIMMEDIATE
70    BIOCIMMEDIATE 1 <int> unix:ioctl io-error ;
71
72: ioctl-sniffer-fd ( fd ifname -- )
73    dupd make-ifreq-props ioctl-BIOSETIF
74    dup ioctl-BIOPROMISC
75    ioctl-BIOCIMMEDIATE ;
76
77M: unix-io <sniffer> ( obj -- sniffer )
78    [
79        [
80            sniffer-spec-path
81            open-read
82            dup close-later
83        ] keep
84        dupd sniffer-spec-ifname ioctl-sniffer-fd
85        dup make-ioctl-buffer
86        input-port <port> <line-reader>
87        \ sniffer construct-delegate
88    ] with-destructors ;
89