Skip to content

Commit 7223d01

Browse files
committed
checksum: add padding for odd number of bytes
1 parent 50c7a1e commit 7223d01

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/Packet.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,18 @@ module Tcp = struct
207207
(* XXX(seliopou): pseudo_header's allocated on every call. Would it be safe
208208
* to allocate once and reuse? *)
209209
let pseudo_header = Cstruct.create 12 in
210+
let length = len pkt in
210211
Cstruct.BE.set_uint32 pseudo_header 0 src;
211212
Cstruct.BE.set_uint32 pseudo_header 4 dst;
212213
Cstruct.set_uint8 pseudo_header 8 0;
213214
Cstruct.set_uint8 pseudo_header 9 0x6;
214-
Cstruct.BE.set_uint16 pseudo_header 10 (len pkt);
215+
Cstruct.BE.set_uint16 pseudo_header 10 length;
215216
set_tcp_chksum bits 0;
216217
let chksum = Checksum.ones_complement_list
217-
[pseudo_header; Cstruct.sub bits 0 (len pkt)] in
218+
(if (length mod 2) = 0
219+
then [pseudo_header; Cstruct.sub bits 0 length]
220+
else [pseudo_header; Cstruct.sub bits 0 length; Cstruct.create 0]) in
218221
set_tcp_chksum bits chksum
219-
220222
end
221223

222224
module Udp = struct

0 commit comments

Comments
 (0)