Skip to content

Commit 5fbd4ea

Browse files
committed
checksum: make pseudo header construction its own function
1 parent 138de77 commit 5fbd4ea

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/Packet.ml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ type nwTos = int8
8686

8787
type tpPort = int16
8888

89+
let mk_pseudo_header (src : nwAddr) (dst : nwAddr) (proto : int) (len : int) =
90+
(* XXX(seliopou): pseudo_header's allocated on every call. Given the usage
91+
* pattern of this library, though, would it be safe to allocate once and
92+
* reuse? *)
93+
let pseudo_header = Cstruct.create 12 in
94+
Cstruct.BE.set_uint32 pseudo_header 0 src;
95+
Cstruct.BE.set_uint32 pseudo_header 4 dst;
96+
Cstruct.set_uint8 pseudo_header 8 0;
97+
Cstruct.set_uint8 pseudo_header 9 proto;
98+
Cstruct.BE.set_uint16 pseudo_header 10 len;
99+
pseudo_header
100+
89101
module Tcp = struct
90102

91103
module Flags = struct
@@ -204,15 +216,8 @@ module Tcp = struct
204216

205217

206218
let checksum (bits : Cstruct.t) (src : nwAddr) (dst : nwAddr) (pkt : t) =
207-
(* XXX(seliopou): pseudo_header's allocated on every call. Would it be safe
208-
* to allocate once and reuse? *)
209-
let pseudo_header = Cstruct.create 12 in
210219
let length = len pkt in
211-
Cstruct.BE.set_uint32 pseudo_header 0 src;
212-
Cstruct.BE.set_uint32 pseudo_header 4 dst;
213-
Cstruct.set_uint8 pseudo_header 8 0;
214-
Cstruct.set_uint8 pseudo_header 9 0x6;
215-
Cstruct.BE.set_uint16 pseudo_header 10 length;
220+
let pseudo_header = mk_pseudo_header src dst 0x6 length in
216221
set_tcp_chksum bits 0;
217222
let chksum = Checksum.ones_complement_list
218223
(if (length mod 2) = 0

0 commit comments

Comments
 (0)