]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/headers/headers.factor
tools.test: Make the flag public. Finish porting tester changes to fuzzer.
[factor.git] / unmaintained / headers / headers.factor
1 ! Copyright (C) 2007 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.syntax byte-arrays io
4 io.sockets kernel structs math math.parser
5 prettyprint sequences ;
6 IN: io.sockets.headers
7
8 C-STRUCT: etherneth
9     { { "char" 6 } "dmac" }
10     { { "char" 6 } "smac" }
11     { "ushort" "type" } ;
12
13 : >mac-address ( byte-array -- string )
14     6 memory>byte-array
15     [ >hex 2 48 pad-left ] { } map-as ":" join ;
16
17 : etherneth. ( etherneth -- )
18     [ etherneth-dmac "Dest   MAC: " write >mac-address . ] keep
19     [ etherneth-smac "Source MAC: " write >mac-address . ] keep
20     [ etherneth-type "Type      : " write .h ] keep
21     drop ;
22
23 C-STRUCT: iph
24     { "uchar" "hl|v" } ! hl is 4 bits, v is 4 bits
25     { "uchar" "tos" }
26     { "short" "len" }
27     { "short" "id" }
28     { "short" "off" }
29     { "uchar" "ttl" }
30     { "uchar" "p" }
31     { "ushort" "ip_sum" }
32     { "uint" "ip_src" }
33     { "uint" "ip_dst" } ;
34
35 : iph-hl ( iph -- n )
36     iph-hl|v -4 shift ;
37
38 : iph-v ( iph -- n )
39     iph-hl|v 0x0f bitand ;
40
41 : set-iph-hl ( n iph -- )
42     [ iph-hl|v 0x0f bitand >r 4 shift r> bitor ] keep
43     set-iph-hl|v ;
44
45 : set-iph-v ( n iph -- )
46     [ iph-hl|v 0xf0 bitand bitor ] keep
47     set-iph-hl|v ;
48
49 C-STRUCT: icmph
50     { "uchar" "type" }
51     { "uchar" "code" }
52     { "short" "chksum" }
53     { "ushort" "id" }
54     { "ushort" "seq" } ;
55
56 C-STRUCT: udph
57     { "ushort" "sport" }
58     { "ushort" "dport" }
59     { "ushort" "len" }
60     { "ushort" "check" } ;
61
62 C-STRUCT: tcph
63     { "ushort" "sport" }
64     { "ushort" "dport" }
65     { "uint" "seq" }
66     { "uint" "ack" }
67     { "uchar" "x2|off" }
68     { "uchar" "flags" }
69     { "ushort" "win" }
70     { "ushort" "sum" }
71     { "ushort" "urp" } ;
72
73 : tcph-x2 ( iph -- n )
74     tcph-x2|off -4 shift ;
75
76 : tcph-off ( iph -- n )
77     tcph-x2|off 0x0f bitand ;
78
79 : set-tcph-x2 ( n iph -- )
80     [ tcph-x2|off 0x0f bitand >r 4 shift r> bitor ] keep
81     set-tcph-x2|off ;
82
83 : set-tcph-off ( n iph -- )
84     [ tcph-x2|off 0x0f bitand bitor ] keep
85     set-tcph-x2|off ;
86