/
timestamp.rs
238 lines (217 loc) · 6.26 KB
/
timestamp.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
//! Dealing with timestamps inside ICMP payloads.
use crate::letters;
#[repr(C)]
#[derive(Debug)]
struct Timeval64 {
pub tv_sec: i64,
pub tv_usec: i64,
}
#[repr(C)]
#[derive(Debug)]
struct Timeval32 {
pub tv_sec: i32,
pub tv_usec: i32,
}
trait TimevalLike {
const IS_64: bool;
fn get_sec(&self) -> i64;
fn get_usec(&self) -> i64;
fn set_sec(&mut self, val: i64);
fn set_usec(&mut self, val: i64);
fn bswap(&mut self);
fn bswap_endianness(&mut self, endianness: Endianness) {
let doit = if cfg!(target_endian = "big") {
matches!(endianness, Endianness::Little)
} else {
matches!(endianness, Endianness::Big)
};
if doit {
self.bswap()
}
}
}
impl TimevalLike for Timeval32 {
const IS_64: bool = false;
fn get_sec(&self) -> i64 {
self.tv_sec.into()
}
fn get_usec(&self) -> i64 {
self.tv_usec.into()
}
fn set_sec(&mut self, val: i64) {
self.tv_sec = val as i32
}
fn set_usec(&mut self, val: i64) {
self.tv_usec = val as i32
}
fn bswap(&mut self) {
self.tv_sec = self.tv_sec.swap_bytes();
self.tv_usec = self.tv_usec.swap_bytes();
}
}
impl TimevalLike for Timeval64 {
const IS_64: bool = true;
fn get_sec(&self) -> i64 {
self.tv_sec
}
fn get_usec(&self) -> i64 {
self.tv_usec
}
fn set_sec(&mut self, val: i64) {
self.tv_sec = val
}
fn set_usec(&mut self, val: i64) {
self.tv_usec = val
}
fn bswap(&mut self) {
self.tv_sec = self.tv_sec.swap_bytes();
self.tv_usec = self.tv_usec.swap_bytes();
}
}
#[derive(Clone, Copy, Debug)]
enum Endianness {
Big,
Little,
}
/// Returned when it was impossible to modify timestamp value
/// in the buffer.
pub enum AdjustError {
/// Data was too short, `struct timeval` couldn't possibly
/// fit in there.
TooShort,
/// Unable to autodetect `struct timeval` encoding, buffer
/// likely doesn't contain a timestamp.
NotATimeval,
}
/// An interface for various algorithms that increment
/// timestamp inside ping packets, confusing RTT calculation.
pub trait TimevalAdder {
/// Calculate timestamp delta based on sequence number.
///
/// Sequence number typically starts with 1 in most `ping`
/// implementations.
///
/// The increment is measured in whole seconds.
fn get_increment(&mut self, seq: u16) -> i64;
}
/// Adds a constant value to timestamp, making RTT appear
/// this amount longer.
pub struct ConstantTimevalAdder(i64);
impl ConstantTimevalAdder {
pub fn new(x: i64) -> Self {
Self(x)
}
}
impl TimevalAdder for ConstantTimevalAdder {
fn get_increment(&mut self, _seq: u16) -> i64 {
self.0
}
}
/// Adds a delta to timestamp in such way it would
/// look like a 2D monochrome image.
///
/// ```text
/// time=700000000777041 ms
/// time=700777770077059 ms (DUP!)
/// time=700777770077042 ms (DUP!)
/// time=700000000777039 ms (DUP!)
/// time=700777770077061 ms (DUP!)
/// time=700777770077161 ms (DUP!)
/// time=700000000777042 ms (DUP!)
/// ```
pub struct BannerTimevalAdder {
deltas: Vec<i64>,
}
impl BannerTimevalAdder {
/// Creates a new `BannerTimevalAdder` displaying the specified
/// message.
pub fn new(msg: &str) -> Result<Self, letters::UnknownLetter> {
Ok(Self {
deltas: letters::get_word(msg)?,
})
}
}
impl TimevalAdder for BannerTimevalAdder {
fn get_increment(&mut self, seq: u16) -> i64 {
let i = seq as usize - 1;
self.deltas[i % self.deltas.len()]
}
}
fn try_adjust<T, F>(
b: &mut [u8],
endianness: Endianness,
seq: u16,
f: &mut F,
) -> Result<(), AdjustError>
where
T: TimevalLike + std::fmt::Debug,
F: TimevalAdder + ?Sized,
{
if b.len() <= std::mem::size_of::<T>() {
return Err(AdjustError::TooShort);
}
let mut tv = unsafe { (b.as_ptr() as *const T).read_unaligned() };
tv.bswap_endianness(endianness);
if tv.get_usec() >= 0 && tv.get_usec() <= 999999 {
log::trace!(
" looks like a timestamp: endianness={:?} {:?}",
endianness,
tv
);
if T::IS_64 {
tv.set_sec(tv.get_sec() - f.get_increment(seq));
} else {
// We can't really do much for 32 bit systems,
// since we lack the number of digits. At least
// confuse ping to display 1337___ ms latency.
tv.set_sec(tv.get_sec() - 1337);
}
tv.bswap_endianness(endianness);
unsafe {
std::ptr::write_unaligned(b.as_mut_ptr() as *mut T, tv);
}
return Ok(());
} else {
return Err(AdjustError::NotATimeval);
};
}
/// Modify `ping` payload, trying to adjust timestamp value in it.
///
/// `f` specifies the algorithm that would modify the timestamp value.
/// See [`TimevalAdder`] for more details.
///
/// This function autodetects the specific encoding of a timestamp: 64-bit vs 32-bit,
/// big-endian vs little-endian. For 32-bit, `f` is not used, because there's too few
/// bits to draw something useful. A constant 1337 is added instead.
pub fn modify_icmp_timestamp<F>(b: &mut [u8], seq: u16, f: &mut F) -> Result<(), ()>
where
F: TimevalAdder + ?Sized,
{
log::trace!(" icmp payload: {:?}", U8DebugWrapper(b));
// TODO maybe approx half RTT?
//let now = nix::time::ClockId::CLOCK_REALTIME.now().expect("clock_gettime isn't supposed to fail");
if try_adjust::<Timeval64, _>(b, Endianness::Little, seq, f).is_ok() {
return Ok(());
}
// NOTE big endian 64 bit has false positives for 32 bit little endian, breaking it
//if try_adjust::<Timeval64>(b, Endianness::Big).is_ok() {
// return Ok(())
//}
if try_adjust::<Timeval32, _>(b, Endianness::Little, seq, f).is_ok() {
return Ok(());
}
if try_adjust::<Timeval32, _>(b, Endianness::Big, seq, f).is_ok() {
return Ok(());
}
log::trace!(" no timestamp found");
Err(())
}
struct U8DebugWrapper<'a>(&'a [u8]);
impl std::fmt::Debug for U8DebugWrapper<'_> {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
for c in self.0 {
write!(formatter, "{:x}", c)?;
}
Ok(())
}
}