Skip to content

Commit d358f4e

Browse files
committed
Merge pull request #32 from frenetic-lang/emmett_sexp
Emmett sexp
2 parents 73c0169 + 12ae919 commit d358f4e

File tree

7 files changed

+106
-59
lines changed

7 files changed

+106
-59
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: c
22
script: bash -ex .travis-ci.sh
33
env:
44
global:
5-
- OPAM_DEPENDS="cstruct quickcheck ounit pa_ounit"
5+
- OPAM_DEPENDS="cstruct quickcheck ounit pa_ounit sexplib"
66
- CONFIG_FLAGS="--enable-tests --enable-quickcheck"
77
- EXTRA_LD_LIBRARY_PATH="$TRAVIS_BUILD_DIR/_build/lib"
88
matrix:

_oasis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Flag quickcheck
1616

1717
Library packet
1818
Path: lib
19-
BuildDepends: str, cstruct, cstruct.syntax
19+
BuildDepends: str, cstruct, cstruct.syntax, sexplib, sexplib.syntax
2020
Modules: Packet, Checksum
2121
CSources: checksum_stubs.c
2222
CCOpt: -g

_tags

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: c53dcb3b24d3ca38ca313d3ea6cbeb08)
2+
# DO NOT EDIT (digest: a7c1dc1fdbc77f33d8bd73c38ff713eb)
33
# Ignore VCS directories, you can use the same kind of rule outside
44
# OASIS_START/STOP if you want to exclude directories that contains
55
# useless stuff for the build process
@@ -23,15 +23,21 @@
2323
<lib/packet.{cma,cmxa}>: use_libpacket_stubs
2424
<lib/*.ml{,i}>: pkg_cstruct
2525
<lib/*.ml{,i}>: pkg_cstruct.syntax
26+
<lib/*.ml{,i}>: pkg_sexplib
27+
<lib/*.ml{,i}>: pkg_sexplib.syntax
2628
<lib/*.ml{,i}>: pkg_str
2729
"lib/checksum_stubs.c": pkg_cstruct
2830
"lib/checksum_stubs.c": pkg_cstruct.syntax
31+
"lib/checksum_stubs.c": pkg_sexplib
32+
"lib/checksum_stubs.c": pkg_sexplib.syntax
2933
"lib/checksum_stubs.c": pkg_str
3034
# Library quickcheck
3135
"quickcheck/quickcheck.cmxs": use_quickcheck
3236
<quickcheck/*.ml{,i}>: pkg_cstruct
3337
<quickcheck/*.ml{,i}>: pkg_cstruct.syntax
3438
<quickcheck/*.ml{,i}>: pkg_quickcheck
39+
<quickcheck/*.ml{,i}>: pkg_sexplib
40+
<quickcheck/*.ml{,i}>: pkg_sexplib.syntax
3541
<quickcheck/*.ml{,i}>: pkg_str
3642
<quickcheck/*.ml{,i}>: use_packet
3743
# Executable testtool
@@ -41,6 +47,8 @@
4147
"test/Test.byte": pkg_pa_ounit
4248
"test/Test.byte": pkg_pa_ounit.syntax
4349
"test/Test.byte": pkg_quickcheck
50+
"test/Test.byte": pkg_sexplib
51+
"test/Test.byte": pkg_sexplib.syntax
4452
"test/Test.byte": pkg_str
4553
"test/Test.byte": use_packet
4654
"test/Test.byte": use_quickcheck
@@ -50,6 +58,8 @@
5058
<test/*.ml{,i}>: pkg_pa_ounit
5159
<test/*.ml{,i}>: pkg_pa_ounit.syntax
5260
<test/*.ml{,i}>: pkg_quickcheck
61+
<test/*.ml{,i}>: pkg_sexplib
62+
<test/*.ml{,i}>: pkg_sexplib.syntax
5363
<test/*.ml{,i}>: pkg_str
5464
<test/*.ml{,i}>: use_packet
5565
<test/*.ml{,i}>: use_quickcheck

lib/META

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version = "0.3.0"
44
description =
55
"Serialization for some common network packets, including ethernet frames, IP, TCP, and ARP."
6-
requires = "str cstruct cstruct.syntax"
6+
requires = "str cstruct cstruct.syntax sexplib sexplib.syntax"
77
archive(byte) = "packet.cma"
88
archive(byte, plugin) = "packet.cma"
99
archive(native) = "packet.cmxa"

lib/Packet.ml

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
open Sexplib
2+
open Sexplib.Std
3+
14
let icmp_code = 0x01
25
let igmp_code = 0x02
36
let tcp_code = 0x06
@@ -60,31 +63,53 @@ let string_of_mac (x:int64) : string =
6063
(get_byte x 5) (get_byte x 4) (get_byte x 3)
6164
(get_byte x 2) (get_byte x 1) (get_byte x 0)
6265

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+
6388
type bytes = Cstruct.t
6489

65-
type int8 = int
90+
type int8 = int with sexp
6691

67-
type int16 = int
92+
type int16 = int with sexp
6893

69-
type int48 = int64
94+
type int48 = int64 with sexp
7095

71-
type dlAddr = int48
96+
type dlAddr = int48 with sexp
7297

73-
type dlTyp = int16
98+
type dlTyp = int16 with sexp
7499

75-
type dlVlan = int16 option
100+
type dlVlan = int16 option with sexp
76101

77-
type dlVlanPcp = int8
102+
type dlVlanPcp = int8 with sexp
78103

79-
type dlVlanDei = bool
104+
type dlVlanDei = bool with sexp
80105

81-
type nwAddr = int32
106+
type nwAddr = int32 with sexp
82107

83-
type nwProto = int8
108+
type nwProto = int8 with sexp
84109

85-
type nwTos = int8
110+
type nwTos = int8 with sexp
86111

87-
type tpPort = int16
112+
type tpPort = int16 with sexp
88113

89114
let mk_pseudo_header (src : nwAddr) (dst : nwAddr) (proto : int) (len : int) =
90115
(* XXX(seliopou): pseudo_header's allocated on every call. Given the usage
@@ -111,7 +136,7 @@ module Tcp = struct
111136
; psh : bool
112137
; rst : bool
113138
; syn : bool
114-
; fin : bool }
139+
; fin : bool } with sexp
115140

116141
let to_string f = Printf.sprintf
117142
"{ ns = %B; cwr = %B; ece = %B; urg = %B; ack = %B; psh = %B; rst = %B; \
@@ -154,7 +179,7 @@ module Tcp = struct
154179
; window : int16
155180
; chksum : int8
156181
; urgent : int8
157-
; payload : bytes }
182+
; payload : bytes } with sexp
158183

159184
let format fmt v =
160185
let open Format in
@@ -233,6 +258,7 @@ module Udp = struct
233258
; dst : tpPort
234259
; chksum : int16
235260
; payload : bytes }
261+
with sexp
236262

237263
let format fmt v =
238264
let open Format in
@@ -277,7 +303,7 @@ module Icmp = struct
277303
code : int8;
278304
chksum : int16;
279305
payload : bytes
280-
}
306+
} with sexp
281307

282308
cstruct icmp {
283309
uint8_t typ;
@@ -361,7 +387,7 @@ module Dns = struct
361387
name : string;
362388
typ : int16;
363389
class_ : int16
364-
}
390+
} with sexp
365391

366392
cstruct qd {
367393
(* preceeded by name *)
@@ -402,7 +428,7 @@ module Dns = struct
402428
class_ : int16;
403429
ttl : int; (* TTL is signed 32-bit int *)
404430
rdata : bytes
405-
}
431+
} with sexp
406432

407433
cstruct rr {
408434
(* preceeded by name *)
@@ -455,6 +481,7 @@ module Dns = struct
455481
; answers : Rr.t list
456482
; authority : Rr.t list
457483
; additional : Rr.t list }
484+
with sexp
458485

459486
let format fmt v =
460487
let open Format in
@@ -534,7 +561,7 @@ module Igmp1and2 = struct
534561
mrt: int8;
535562
chksum : int16;
536563
addr : nwAddr;
537-
}
564+
} with sexp
538565

539566
cstruct igmp1and2 {
540567
uint8_t mrt;
@@ -578,7 +605,7 @@ module Igmp3 = struct
578605
typ : int8;
579606
addr : nwAddr;
580607
sources : nwAddr list;
581-
}
608+
} with sexp
582609

583610
cstruct grouprec {
584611
uint8_t typ;
@@ -622,7 +649,7 @@ module Igmp3 = struct
622649
type t = {
623650
chksum : int16;
624651
grs : GroupRec.t list;
625-
}
652+
} with sexp
626653

627654
cstruct igmp3 {
628655
uint8_t reserved1;
@@ -675,11 +702,12 @@ module Igmp = struct
675702
| Igmp1and2 of Igmp1and2.t
676703
| Igmp3 of Igmp3.t
677704
| Unparsable of (int8 * bytes)
705+
with sexp
678706

679707
type t = {
680708
ver_and_typ : int8;
681709
msg : msg
682-
}
710+
} with sexp
683711

684712
cenum igmp_msg_type {
685713
IGMP_MSG_QUERY = 0x11;
@@ -758,14 +786,15 @@ module Ip = struct
758786
| Icmp of Icmp.t
759787
| Igmp of Igmp.t
760788
| Unparsable of (nwProto * bytes)
789+
with sexp
761790

762791
module Flags = struct
763792
(** [Flags] is the type of IPv4 flags. *)
764793

765794
type t =
766795
{ df : bool (** Don't fragment. *)
767796
; mf : bool (** More fragments. *)
768-
}
797+
} with sexp
769798

770799
let to_string v = Printf.sprintf "{ df = %B; mf = %B }" v.df v.mf
771800

@@ -792,7 +821,7 @@ module Ip = struct
792821
dst : nwAddr;
793822
options : bytes;
794823
tp : tp
795-
}
824+
} with sexp
796825

797826
let format_tp fmt = function
798827
| Tcp tcp -> Tcp.format fmt tcp
@@ -923,6 +952,7 @@ module Arp = struct
923952
type t =
924953
| Query of dlAddr * nwAddr * nwAddr
925954
| Reply of dlAddr * nwAddr * dlAddr * nwAddr
955+
with sexp
926956

927957
let format fmt v =
928958
let open Format in
@@ -1011,6 +1041,7 @@ type nw =
10111041
| Ip of Ip.t
10121042
| Arp of Arp.t
10131043
| Unparsable of (dlTyp * bytes)
1044+
with sexp
10141045

10151046
type packet = {
10161047
dlSrc : dlAddr;
@@ -1019,7 +1050,7 @@ type packet = {
10191050
dlVlanDei : dlVlanDei;
10201051
dlVlanPcp : dlVlanPcp;
10211052
nw : nw
1022-
}
1053+
} with sexp
10231054

10241055
let format_nw fmt v =
10251056
let open Format in

0 commit comments

Comments
 (0)