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
//! Utilities for tracking checksum recalculation state.
use std::net::{Ipv4Addr, Ipv6Addr};
/// Controls how and when checksum recalculation happens for a packet.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChecksumMode {
/// Checksum updates are handled manually by the caller.
Manual,
/// Checksum updates happen automatically whenever a tracked field changes.
Automatic,
}
impl Default for ChecksumMode {
fn default() -> Self {
ChecksumMode::Manual
}
}
/// Tracks whether a packet's checksum needs to be recomputed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ChecksumState {
mode: ChecksumMode,
dirty: bool,
}
impl ChecksumState {
/// Creates a new checksum state with manual recalculation enabled.
pub fn new() -> Self {
Self::default()
}
/// Returns the current mode controlling checksum updates.
pub fn mode(&self) -> ChecksumMode {
self.mode
}
/// Sets how checksum updates should be handled.
pub fn set_mode(&mut self, mode: ChecksumMode) {
self.mode = mode;
}
/// Enables automatic checksum recomputation.
pub fn enable_automatic(&mut self) {
self.mode = ChecksumMode::Automatic;
}
/// Disables automatic checksum recomputation.
pub fn disable_automatic(&mut self) {
self.mode = ChecksumMode::Manual;
}
/// Returns true if checksum recomputation is automatic.
pub fn automatic(&self) -> bool {
matches!(self.mode, ChecksumMode::Automatic)
}
/// Marks the checksum as stale due to a field mutation.
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
/// Clears the dirty flag after a successful recomputation.
pub fn clear_dirty(&mut self) {
self.dirty = false;
}
/// Returns true if the checksum needs to be recomputed.
pub fn is_dirty(&self) -> bool {
self.dirty
}
}
/// Captures the pseudo-header inputs required for transport checksum calculations.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransportChecksumContext {
/// Transport checksum associated with an IPv4 pseudo-header.
Ipv4 {
source: Ipv4Addr,
destination: Ipv4Addr,
},
/// Transport checksum associated with an IPv6 pseudo-header.
Ipv6 {
source: Ipv6Addr,
destination: Ipv6Addr,
},
}
impl TransportChecksumContext {
/// Builds an IPv4 checksum context.
pub fn ipv4(source: Ipv4Addr, destination: Ipv4Addr) -> Self {
TransportChecksumContext::Ipv4 {
source,
destination,
}
}
/// Builds an IPv6 checksum context.
pub fn ipv6(source: Ipv6Addr, destination: Ipv6Addr) -> Self {
TransportChecksumContext::Ipv6 {
source,
destination,
}
}
}