forked from Genymobile/gnirehtet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_header.rs
693 lines (584 loc) · 22.7 KB
/
tcp_header.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
* Copyright (C) 2017 Genymobile
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use super::ipv4_header::Ipv4HeaderData;
use byteorder::{BigEndian, ByteOrder};
use std::mem;
pub struct TcpHeader<'a> {
raw: &'a [u8],
data: &'a TcpHeaderData,
}
pub struct TcpHeaderMut<'a> {
raw: &'a mut [u8],
data: &'a mut TcpHeaderData,
}
#[derive(Clone)]
pub struct TcpHeaderData {
source_port: u16,
destination_port: u16,
sequence_number: u32,
acknowledgement_number: u32,
header_length: u8,
flags: u16,
window: u16,
}
pub const FLAG_FIN: u16 = 1;
pub const FLAG_SYN: u16 = 1 << 1;
pub const FLAG_RST: u16 = 1 << 2;
pub const FLAG_PSH: u16 = 1 << 3;
pub const FLAG_ACK: u16 = 1 << 4;
#[allow(dead_code)]
impl TcpHeaderData {
pub fn parse(raw: &[u8]) -> Self {
let data_offset_and_flags = BigEndian::read_u16(&raw[12..14]);
Self {
source_port: BigEndian::read_u16(&raw[0..2]),
destination_port: BigEndian::read_u16(&raw[2..4]),
sequence_number: BigEndian::read_u32(&raw[4..8]),
acknowledgement_number: BigEndian::read_u32(&raw[8..12]),
header_length: ((data_offset_and_flags & 0xF000) >> 10) as u8,
flags: data_offset_and_flags & 0x1FF,
window: BigEndian::read_u16(&raw[14..16]),
}
}
#[inline]
pub fn bind<'c, 'a: 'c, 'b: 'c>(&'a self, raw: &'b [u8]) -> TcpHeader<'c> {
TcpHeader::new(raw, self)
}
#[inline]
pub fn bind_mut<'c, 'a: 'c, 'b: 'c>(&'a mut self, raw: &'b mut [u8]) -> TcpHeaderMut<'c> {
TcpHeaderMut::new(raw, self)
}
#[inline]
pub fn header_length(&self) -> u8 {
self.header_length
}
#[inline]
pub fn source_port(&self) -> u16 {
self.source_port
}
#[inline]
pub fn destination_port(&self) -> u16 {
self.destination_port
}
#[inline]
pub fn sequence_number(&self) -> u32 {
self.sequence_number
}
#[inline]
pub fn acknowledgement_number(&self) -> u32 {
self.acknowledgement_number
}
#[inline]
pub fn window(&self) -> u16 {
self.window
}
#[inline]
pub fn flags(&self) -> u16 {
self.flags
}
#[inline]
pub fn is_fin(&self) -> bool {
self.flags & FLAG_FIN != 0
}
#[inline]
pub fn is_syn(&self) -> bool {
self.flags & FLAG_SYN != 0
}
#[inline]
pub fn is_rst(&self) -> bool {
self.flags & FLAG_RST != 0
}
#[inline]
pub fn is_psh(&self) -> bool {
self.flags & FLAG_PSH != 0
}
#[inline]
pub fn is_ack(&self) -> bool {
self.flags & FLAG_ACK != 0
}
}
// shared definition for UdpHeader and UdpHeaderMut
macro_rules! tcp_header_common {
($name:ident, $raw_type:ty, $data_type:ty) => {
// for readability, declare structs manually outside the macro
#[allow(dead_code)]
impl<'a> $name<'a> {
pub fn new(raw: $raw_type, data: $data_type) -> Self {
Self {
raw: raw,
data: data,
}
}
#[inline]
pub fn raw(&self) -> &[u8] {
self.raw
}
#[inline]
pub fn data(&self) -> &TcpHeaderData {
self.data
}
#[inline]
pub fn header_length(&self) -> u8 {
self.data.header_length
}
#[inline]
pub fn source_port(&self) -> u16 {
self.data.source_port
}
#[inline]
pub fn destination_port(&self) -> u16 {
self.data.destination_port
}
#[inline]
pub fn sequence_number(&self) -> u32 {
self.data.sequence_number
}
#[inline]
pub fn acknowledgement_number(&self) -> u32 {
self.data.acknowledgement_number
}
#[inline]
pub fn window(&self) -> u16 {
self.data.window
}
#[inline]
pub fn flags(&self) -> u16 {
self.data.flags
}
#[inline]
pub fn is_fin(&self) -> bool {
self.data.is_fin()
}
#[inline]
pub fn is_syn(&self) -> bool {
self.data.is_syn()
}
#[inline]
pub fn is_rst(&self) -> bool {
self.data.is_rst()
}
#[inline]
pub fn is_psh(&self) -> bool {
self.data.is_psh()
}
#[inline]
pub fn is_ack(&self) -> bool {
self.data.is_ack()
}
}
};
}
tcp_header_common!(TcpHeader, &'a [u8], &'a TcpHeaderData);
tcp_header_common!(TcpHeaderMut, &'a mut [u8], &'a mut TcpHeaderData);
// additional methods for the mutable version
#[allow(dead_code)]
impl<'a> TcpHeaderMut<'a> {
#[inline]
pub fn raw_mut(&mut self) -> &mut [u8] {
self.raw
}
#[inline]
pub fn data_mut(&mut self) -> &mut TcpHeaderData {
self.data
}
#[inline]
pub fn set_source_port(&mut self, source_port: u16) {
self.data.source_port = source_port;
BigEndian::write_u16(&mut self.raw[0..2], source_port);
}
#[inline]
pub fn set_destination_port(&mut self, destination_port: u16) {
self.data.destination_port = destination_port;
BigEndian::write_u16(&mut self.raw[2..4], destination_port);
}
pub fn swap_source_and_destination(&mut self) {
mem::swap(&mut self.data.source_port, &mut self.data.destination_port);
for i in 0..2 {
self.raw.swap(i, i + 2);
}
}
#[inline]
pub fn set_sequence_number(&mut self, sequence_number: u32) {
self.data.sequence_number = sequence_number;
BigEndian::write_u32(&mut self.raw[4..8], sequence_number);
}
#[inline]
pub fn set_acknowledgement_number(&mut self, acknowledgement_number: u32) {
self.data.acknowledgement_number = acknowledgement_number;
BigEndian::write_u32(&mut self.raw[8..12], acknowledgement_number);
}
#[inline]
pub fn set_flags(&mut self, flags: u16) {
self.data.flags = flags;
let mut data_offset_and_flags = BigEndian::read_u16(&self.raw[12..14]);
data_offset_and_flags = data_offset_and_flags & 0xFE00 | flags & 0x1FF;
BigEndian::write_u16(&mut self.raw[12..14], data_offset_and_flags);
}
#[inline]
pub fn shrink_options(&mut self) {
self.set_data_offset(5);
}
#[inline]
fn set_data_offset(&mut self, data_offset: u8) {
let mut data_offset_and_flags = BigEndian::read_u16(&self.raw[12..14]);
data_offset_and_flags = data_offset_and_flags & 0x0FFF | (u16::from(data_offset) << 12);
BigEndian::write_u16(&mut self.raw[12..14], data_offset_and_flags);
self.data.header_length = data_offset << 2;
}
#[inline]
fn checksum(&self) -> u16 {
BigEndian::read_u16(&self.raw[16..18])
}
#[inline]
fn set_checksum(&mut self, checksum: u16) {
BigEndian::write_u16(&mut self.raw[16..18], checksum);
}
pub fn update_checksum(&mut self, ipv4_header_data: &Ipv4HeaderData, payload: &[u8]) {
// pseudo-header checksum (cf rfc793 section 3.1)
let source = ipv4_header_data.source();
let destination = ipv4_header_data.destination();
let transport_length =
ipv4_header_data.total_length() - u16::from(ipv4_header_data.header_length());
let header_length = self.header_length();
debug_assert!(header_length % 2 == 0 && header_length >= 20);
let payload_length = transport_length - u16::from(header_length);
debug_assert_eq!(
payload_length as usize,
payload.len(),
"Payload length does not match"
);
let mut sum = 6u32; // protocol: TCP = 6
sum += source >> 16;
sum += source & 0xFFFF;
sum += destination >> 16;
sum += destination & 0xFFFF;
sum += u32::from(transport_length);
// reset checksum field, so that it can be added with other bytes
self.set_checksum(0);
// checksum computation is the most CPU-intensive task in gnirehtet
// prefer optimization over readability/safety
let mut hsum = 0; // high-order bytes sum
unsafe {
let mut p = self.raw.as_ptr();
let end = p.offset(header_length as isize);
while p < end {
hsum += u32::from(*p);
sum += u32::from(*p.offset(1));
p = p.offset(2);
}
let mut p = payload.as_ptr();
// -1 to ignore the last if payload_length is odd
let end = p.offset(payload_length as isize - 1);
while p < end {
hsum += u32::from(*p);
sum += u32::from(*p.offset(1));
p = p.offset(2);
}
if payload_length % 2 != 0 {
// if payload length is odd, the last byte is considered high-order
hsum += u32::from(*payload.get_unchecked((payload_length - 1) as usize));
}
}
// add high-order bytes sum to the global sum
sum += hsum << 8;
while (sum & !0xFFFF) != 0 {
sum = (sum & 0xFFFF) + (sum >> 16);
}
self.set_checksum(!sum as u16);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::relay::ipv4_packet::Ipv4Packet;
use crate::relay::transport_header::TransportHeaderMut;
use byteorder::{BigEndian, WriteBytesExt};
fn create_packet() -> Vec<u8> {
let mut raw = Vec::new();
raw.reserve(44);
raw.write_u8(4u8 << 4 | 5).unwrap(); // version_and_ihl
raw.write_u8(0).unwrap(); //ToS
raw.write_u16::<BigEndian>(44).unwrap(); // total length
raw.write_u32::<BigEndian>(0).unwrap(); // id_flags_fragment_offset
raw.write_u8(0).unwrap(); // TTL
raw.write_u8(6).unwrap(); // protocol (TCP)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u32::<BigEndian>(0x12345678).unwrap(); // source address
raw.write_u32::<BigEndian>(0xA2A24242).unwrap(); // destination address
raw.write_u16::<BigEndian>(0x1234).unwrap(); // source port
raw.write_u16::<BigEndian>(0x5678).unwrap(); // destination port
raw.write_u32::<BigEndian>(0x111).unwrap(); // sequence number
raw.write_u32::<BigEndian>(0x222).unwrap(); // acknowledgement number
raw.write_u16::<BigEndian>(5 << 12).unwrap(); // data offset + flags(0)
raw.write_u16::<BigEndian>(0).unwrap(); // window (don't care for these tests)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u16::<BigEndian>(0).unwrap(); // urgent pointer
raw.write_u32::<BigEndian>(0x1122EEFF).unwrap(); // payload
raw
}
fn create_odd_packet() -> Vec<u8> {
let mut raw = Vec::new();
raw.reserve(45);
raw.write_u8(4u8 << 4 | 5).unwrap(); // version_and_ihl
raw.write_u8(0).unwrap(); //ToS
raw.write_u16::<BigEndian>(45).unwrap(); // total length
raw.write_u32::<BigEndian>(0).unwrap(); // id_flags_fragment_offset
raw.write_u8(0).unwrap(); // TTL
raw.write_u8(6).unwrap(); // protocol (TCP)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u32::<BigEndian>(0x12345678).unwrap(); // source address
raw.write_u32::<BigEndian>(0xA2A24242).unwrap(); // destination address
raw.write_u16::<BigEndian>(0x1234).unwrap(); // source port
raw.write_u16::<BigEndian>(0x5678).unwrap(); // destination port
raw.write_u32::<BigEndian>(0x111).unwrap(); // sequence number
raw.write_u32::<BigEndian>(0x222).unwrap(); // acknowledgement number
raw.write_u16::<BigEndian>(5 << 12).unwrap(); // data offset + flags(0)
raw.write_u16::<BigEndian>(0).unwrap(); // window (don't care for these tests)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u16::<BigEndian>(0).unwrap(); // urgent pointer
// payload
raw.write_u32::<BigEndian>(0x1122EEFF).unwrap();
raw.write_u8(0x88).unwrap();
raw
}
fn create_empty_packet() -> Vec<u8> {
let mut raw = Vec::new();
raw.reserve(40);
raw.write_u8(4u8 << 4 | 5).unwrap(); // version_and_ihl
raw.write_u8(0).unwrap(); //ToS
raw.write_u16::<BigEndian>(40).unwrap(); // total length
raw.write_u32::<BigEndian>(0).unwrap(); // id_flags_fragment_offset
raw.write_u8(0).unwrap(); // TTL
raw.write_u8(6).unwrap(); // protocol (TCP)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u32::<BigEndian>(0x12345678).unwrap(); // source address
raw.write_u32::<BigEndian>(0xA2A24242).unwrap(); // destination address
raw.write_u16::<BigEndian>(0x1234).unwrap(); // source port
raw.write_u16::<BigEndian>(0x5678).unwrap(); // destination port
raw.write_u32::<BigEndian>(0x111).unwrap(); // sequence number
raw.write_u32::<BigEndian>(0x222).unwrap(); // acknowledgement number
raw.write_u16::<BigEndian>(5 << 12).unwrap(); // data offset + flags(0)
raw.write_u16::<BigEndian>(0).unwrap(); // window (don't care for these tests)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u16::<BigEndian>(0).unwrap(); // urgent pointer
raw
}
fn create_tcp_header() -> Vec<u8> {
let mut raw = Vec::new();
raw.reserve(20);
raw.write_u16::<BigEndian>(0x1234).unwrap(); // source port
raw.write_u16::<BigEndian>(0x5678).unwrap(); // destination port
raw.write_u32::<BigEndian>(0x111).unwrap(); // sequence number
raw.write_u32::<BigEndian>(0x222).unwrap(); // acknowledgement number
raw.write_u16::<BigEndian>(5 << 12).unwrap(); // data offset + flags(0)
raw.write_u16::<BigEndian>(0).unwrap(); // window (don't care for these tests)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u16::<BigEndian>(0).unwrap(); // urgent pointer
raw
}
#[test]
fn edit_header() {
let raw = &mut create_tcp_header()[..];
let mut header_data = TcpHeaderData::parse(raw);
let mut header = header_data.bind_mut(raw);
header.set_source_port(1111);
header.set_destination_port(2222);
header.set_sequence_number(300);
header.set_acknowledgement_number(101);
header.set_flags(FLAG_FIN | FLAG_ACK);
assert_eq!(1111, header.source_port());
assert_eq!(2222, header.destination_port());
assert_eq!(300, header.sequence_number());
assert_eq!(101, header.acknowledgement_number());
assert_eq!(FLAG_FIN | FLAG_ACK, header.flags());
{
let raw = header.raw();
// assert that the buffer has been modified
let raw_source_port = BigEndian::read_u16(&raw[0..2]);
let raw_destination_port = BigEndian::read_u16(&raw[2..4]);
let raw_sequence_number = BigEndian::read_u32(&raw[4..8]);
let raw_acknowledgement_number = BigEndian::read_u32(&raw[8..12]);
let raw_data_offset_and_flags = BigEndian::read_u16(&raw[12..14]);
assert_eq!(1111, raw_source_port);
assert_eq!(2222, raw_destination_port);
assert_eq!(300, raw_sequence_number);
assert_eq!(101, raw_acknowledgement_number);
assert_eq!(0x5011, raw_data_offset_and_flags);
}
header.swap_source_and_destination();
assert_eq!(2222, header.source_port());
assert_eq!(1111, header.destination_port());
let raw = header.raw();
let raw_source_port = BigEndian::read_u16(&raw[0..2]);
let raw_destination_port = BigEndian::read_u16(&raw[2..4]);
assert_eq!(2222, raw_source_port);
assert_eq!(1111, raw_destination_port);
}
#[test]
fn compute_checksum() {
let raw = &mut create_packet()[..];
let mut ipv4_packet = Ipv4Packet::parse(raw);
let (ipv4_header, mut transport) = ipv4_packet.split_mut();
if let Some((TransportHeaderMut::Tcp(ref mut tcp_header), ref payload)) = transport {
// set a fake checksum value to assert that it is correctly computed
tcp_header.set_checksum(0x79);
tcp_header.update_checksum(ipv4_header.data(), payload);
let checksum = tcp_header.checksum();
let expected_checksum = {
// pseudo-header
let mut sum: u32 = 0x1234 + 0x5678 + 0xA2A2 + 0x4242 + 0x0006 + 0x0018;
// header
sum += 0x1234
+ 0x5678
+ 0x0000
+ 0x0111
+ 0x0000
+ 0x0222
+ 0x5000
+ 0x0000
+ 0x0000
+ 0x0000;
// payload
sum += 0x1122 + 0xEEFF;
while (sum & !0xFFFF) != 0 {
sum = (sum & 0xFFFF) + (sum >> 16);
}
!sum as u16
};
assert_eq!(expected_checksum, checksum);
} else {
panic!("Not a TCP packet");
}
}
#[test]
fn compute_checksum_odd() {
let raw = &mut create_odd_packet()[..];
let mut ipv4_packet = Ipv4Packet::parse(raw);
let (ipv4_header, mut transport) = ipv4_packet.split_mut();
if let Some((TransportHeaderMut::Tcp(ref mut tcp_header), ref payload)) = transport {
// set a fake checksum value to assert that it is correctly computed
tcp_header.set_checksum(0x79);
tcp_header.update_checksum(ipv4_header.data(), payload);
let checksum = tcp_header.checksum();
let expected_checksum = {
// pseudo-header
let mut sum: u32 = 0x1234 + 0x5678 + 0xA2A2 + 0x4242 + 0x0006 + 0x0019;
// header
sum += 0x1234
+ 0x5678
+ 0x0000
+ 0x0111
+ 0x0000
+ 0x0222
+ 0x5000
+ 0x0000
+ 0x0000
+ 0x0000;
// payload
sum += 0x1122 + 0xEEFF + 0x8800;
while (sum & !0xFFFF) != 0 {
sum = (sum & 0xFFFF) + (sum >> 16);
}
!sum as u16
};
assert_eq!(expected_checksum, checksum);
} else {
panic!("Not a TCP packet");
}
}
#[test]
fn compute_checksum_empty_payload() {
let raw = &mut create_empty_packet()[..];
let mut ipv4_packet = Ipv4Packet::parse(raw);
let (ipv4_header, mut transport) = ipv4_packet.split_mut();
if let Some((TransportHeaderMut::Tcp(ref mut tcp_header), ref payload)) = transport {
// set a fake checksum value to assert that it is correctly computed
tcp_header.set_checksum(0x79);
tcp_header.update_checksum(ipv4_header.data(), payload);
let checksum = tcp_header.checksum();
let expected_checksum = {
// pseudo-header
let mut sum: u32 = 0x1234 + 0x5678 + 0xA2A2 + 0x4242 + 0x0006 + 0x0014;
// header
sum += 0x1234
+ 0x5678
+ 0x0000
+ 0x0111
+ 0x0000
+ 0x0222
+ 0x5000
+ 0x0000
+ 0x0000
+ 0x0000;
while (sum & !0xFFFF) != 0 {
sum = (sum & 0xFFFF) + (sum >> 16);
}
!sum as u16
};
assert_eq!(expected_checksum, checksum);
} else {
panic!("Not a TCP packet");
}
}
fn create_long_packet() -> Vec<u8> {
let mut raw = Vec::new();
raw.reserve(45);
raw.write_u8(4u8 << 4 | 5).unwrap(); // version_and_ihl
raw.write_u8(0).unwrap(); //ToS
raw.write_u16::<BigEndian>(1240).unwrap(); // total length
raw.write_u32::<BigEndian>(0).unwrap(); // id_flags_fragment_offset
raw.write_u8(0).unwrap(); // TTL
raw.write_u8(6).unwrap(); // protocol (TCP)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u32::<BigEndian>(0x12345678).unwrap(); // source address
raw.write_u32::<BigEndian>(0xA2A24242).unwrap(); // destination address
raw.write_u16::<BigEndian>(0x1234).unwrap(); // source port
raw.write_u16::<BigEndian>(0x5678).unwrap(); // destination port
raw.write_u32::<BigEndian>(0x111).unwrap(); // sequence number
raw.write_u32::<BigEndian>(0x222).unwrap(); // acknowledgement number
raw.write_u16::<BigEndian>(5 << 12).unwrap(); // data offset + flags(0)
raw.write_u16::<BigEndian>(0).unwrap(); // window (don't care for these tests)
raw.write_u16::<BigEndian>(0).unwrap(); // checksum
raw.write_u16::<BigEndian>(0).unwrap(); // urgent pointer
// payload
for i in 0u16..1200 {
raw.write_u8(i as u8).unwrap();
}
raw
}
// built-in rust bench is still unstable, use a simple test instead
// run with: cargo test bench_checksum --release -- --nocapture
// manual benchmark
#[ignore]
#[test]
fn bench_checksum() {
let raw = &mut create_long_packet()[..];
let mut ipv4_packet = Ipv4Packet::parse(raw);
let (ipv4_header, mut transport) = ipv4_packet.split_mut();
if let Some((TransportHeaderMut::Tcp(ref mut tcp_header), ref payload)) = transport {
use std::time::Instant;
let start = Instant::now();
for _ in 0..5000000 {
tcp_header.update_checksum(ipv4_header.data(), payload);
}
let duration = start.elapsed();
let ms = duration.as_secs() * 1000 + duration.subsec_nanos() as u64 / 1000000;
println!("5000000 TCP checksums: {}ms", ms);
} else {
panic!("Not a TCP packet");
}
}
}