1
+ open Sexplib
2
+ open Sexplib.Std
3
+
1
4
let icmp_code = 0x01
2
5
let igmp_code = 0x02
3
6
let tcp_code = 0x06
@@ -60,31 +63,53 @@ let string_of_mac (x:int64) : string =
60
63
(get_byte x 5 ) (get_byte x 4 ) (get_byte x 3 )
61
64
(get_byte x 2 ) (get_byte x 1 ) (get_byte x 0 )
62
65
66
+ let bytes_of_sexp s =
67
+ match s with
68
+ | Sexp. Atom w ->
69
+ begin
70
+ let n = String. length w in
71
+ let buf = Cstruct. create n in
72
+ for i = 0 to n - 1 do
73
+ Cstruct. set_char buf i w.[i]
74
+ done ;
75
+ buf
76
+ end
77
+ | _ ->
78
+ failwith " bytes_of_sexp: expected Atom"
79
+
80
+ let sexp_of_bytes s =
81
+ let n = Cstruct. len s in
82
+ let buf = Buffer. create n in
83
+ for i = 0 to n - 1 do
84
+ Buffer. add_char buf (Cstruct. get_char s i)
85
+ done ;
86
+ Sexp. Atom (Buffer. contents buf)
87
+
63
88
type bytes = Cstruct .t
64
89
65
- type int8 = int
90
+ type int8 = int with sexp
66
91
67
- type int16 = int
92
+ type int16 = int with sexp
68
93
69
- type int48 = int64
94
+ type int48 = int64 with sexp
70
95
71
- type dlAddr = int48
96
+ type dlAddr = int48 with sexp
72
97
73
- type dlTyp = int16
98
+ type dlTyp = int16 with sexp
74
99
75
- type dlVlan = int16 option
100
+ type dlVlan = int16 option with sexp
76
101
77
- type dlVlanPcp = int8
102
+ type dlVlanPcp = int8 with sexp
78
103
79
- type dlVlanDei = bool
104
+ type dlVlanDei = bool with sexp
80
105
81
- type nwAddr = int32
106
+ type nwAddr = int32 with sexp
82
107
83
- type nwProto = int8
108
+ type nwProto = int8 with sexp
84
109
85
- type nwTos = int8
110
+ type nwTos = int8 with sexp
86
111
87
- type tpPort = int16
112
+ type tpPort = int16 with sexp
88
113
89
114
let mk_pseudo_header (src : nwAddr ) (dst : nwAddr ) (proto : int ) (len : int ) =
90
115
(* XXX(seliopou): pseudo_header's allocated on every call. Given the usage
@@ -111,7 +136,7 @@ module Tcp = struct
111
136
; psh : bool
112
137
; rst : bool
113
138
; syn : bool
114
- ; fin : bool }
139
+ ; fin : bool } with sexp
115
140
116
141
let to_string f = Printf. sprintf
117
142
" { ns = %B; cwr = %B; ece = %B; urg = %B; ack = %B; psh = %B; rst = %B; \
@@ -154,7 +179,7 @@ module Tcp = struct
154
179
; window : int16
155
180
; chksum : int8
156
181
; urgent : int8
157
- ; payload : bytes }
182
+ ; payload : bytes } with sexp
158
183
159
184
let format fmt v =
160
185
let open Format in
@@ -233,6 +258,7 @@ module Udp = struct
233
258
; dst : tpPort
234
259
; chksum : int16
235
260
; payload : bytes }
261
+ with sexp
236
262
237
263
let format fmt v =
238
264
let open Format in
@@ -277,7 +303,7 @@ module Icmp = struct
277
303
code : int8 ;
278
304
chksum : int16 ;
279
305
payload : bytes
280
- }
306
+ } with sexp
281
307
282
308
cstruct icmp {
283
309
uint8_t typ;
@@ -361,7 +387,7 @@ module Dns = struct
361
387
name : string ;
362
388
typ : int16 ;
363
389
class_ : int16
364
- }
390
+ } with sexp
365
391
366
392
cstruct qd {
367
393
(* preceeded by name *)
@@ -402,7 +428,7 @@ module Dns = struct
402
428
class_ : int16 ;
403
429
ttl : int ; (* TTL is signed 32-bit int *)
404
430
rdata : bytes
405
- }
431
+ } with sexp
406
432
407
433
cstruct rr {
408
434
(* preceeded by name *)
@@ -455,6 +481,7 @@ module Dns = struct
455
481
; answers : Rr .t list
456
482
; authority : Rr .t list
457
483
; additional : Rr .t list }
484
+ with sexp
458
485
459
486
let format fmt v =
460
487
let open Format in
@@ -534,7 +561,7 @@ module Igmp1and2 = struct
534
561
mrt : int8 ;
535
562
chksum : int16 ;
536
563
addr : nwAddr ;
537
- }
564
+ } with sexp
538
565
539
566
cstruct igmp1and2 {
540
567
uint8_t mrt;
@@ -578,7 +605,7 @@ module Igmp3 = struct
578
605
typ : int8 ;
579
606
addr : nwAddr ;
580
607
sources : nwAddr list ;
581
- }
608
+ } with sexp
582
609
583
610
cstruct grouprec {
584
611
uint8_t typ;
@@ -622,7 +649,7 @@ module Igmp3 = struct
622
649
type t = {
623
650
chksum : int16 ;
624
651
grs : GroupRec .t list ;
625
- }
652
+ } with sexp
626
653
627
654
cstruct igmp3 {
628
655
uint8_t reserved1;
@@ -675,11 +702,12 @@ module Igmp = struct
675
702
| Igmp1and2 of Igmp1and2 .t
676
703
| Igmp3 of Igmp3 .t
677
704
| Unparsable of (int8 * bytes )
705
+ with sexp
678
706
679
707
type t = {
680
708
ver_and_typ : int8 ;
681
709
msg : msg
682
- }
710
+ } with sexp
683
711
684
712
cenum igmp_msg_type {
685
713
IGMP_MSG_QUERY = 0x11 ;
@@ -758,14 +786,15 @@ module Ip = struct
758
786
| Icmp of Icmp .t
759
787
| Igmp of Igmp .t
760
788
| Unparsable of (nwProto * bytes )
789
+ with sexp
761
790
762
791
module Flags = struct
763
792
(* * [Flags] is the type of IPv4 flags. *)
764
793
765
794
type t =
766
795
{ df : bool (* * Don't fragment. *)
767
796
; mf : bool (* * More fragments. *)
768
- }
797
+ } with sexp
769
798
770
799
let to_string v = Printf. sprintf " { df = %B; mf = %B }" v.df v.mf
771
800
@@ -792,7 +821,7 @@ module Ip = struct
792
821
dst : nwAddr ;
793
822
options : bytes ;
794
823
tp : tp
795
- }
824
+ } with sexp
796
825
797
826
let format_tp fmt = function
798
827
| Tcp tcp -> Tcp. format fmt tcp
@@ -923,6 +952,7 @@ module Arp = struct
923
952
type t =
924
953
| Query of dlAddr * nwAddr * nwAddr
925
954
| Reply of dlAddr * nwAddr * dlAddr * nwAddr
955
+ with sexp
926
956
927
957
let format fmt v =
928
958
let open Format in
@@ -1011,6 +1041,7 @@ type nw =
1011
1041
| Ip of Ip .t
1012
1042
| Arp of Arp .t
1013
1043
| Unparsable of (dlTyp * bytes )
1044
+ with sexp
1014
1045
1015
1046
type packet = {
1016
1047
dlSrc : dlAddr ;
@@ -1019,7 +1050,7 @@ type packet = {
1019
1050
dlVlanDei : dlVlanDei ;
1020
1051
dlVlanPcp : dlVlanPcp ;
1021
1052
nw : nw
1022
- }
1053
+ } with sexp
1023
1054
1024
1055
let format_nw fmt v =
1025
1056
let open Format in
0 commit comments