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
use super::super::*;
use std::slice::from_raw_parts;
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct UdpHeader {
pub source_port: u16,
pub destination_port: u16,
pub length: u16,
pub checksum: u16
}
impl UdpHeader {
pub fn without_ipv4_checksum(source_port: u16, destination_port: u16, payload_length: usize) -> Result<UdpHeader, ValueError> {
const MAX_PAYLOAD_LENGTH: usize = (std::u16::MAX as usize) - UdpHeader::SERIALIZED_SIZE;
if MAX_PAYLOAD_LENGTH < payload_length {
return Err(ValueError::UdpPayloadLengthTooLarge(payload_length));
}
Ok(UdpHeader{
source_port,
destination_port,
length: (UdpHeader::SERIALIZED_SIZE + payload_length) as u16,
checksum: 0
})
}
pub fn with_ipv4_checksum(source_port: u16, destination_port: u16, ip_header: &Ipv4Header, payload: &[u8]) -> Result<UdpHeader, ValueError> {
const MAX_PAYLOAD_LENGTH: usize = (std::u16::MAX as usize) - UdpHeader::SERIALIZED_SIZE;
if MAX_PAYLOAD_LENGTH < payload.len() {
return Err(ValueError::UdpPayloadLengthTooLarge(payload.len()));
}
let mut result = UdpHeader{
source_port,
destination_port,
length: (UdpHeader::SERIALIZED_SIZE + payload.len()) as u16,
checksum: 0
};
result.checksum = result.calc_checksum_ipv4_internal(ip_header.source, ip_header.destination, payload);
Ok(result)
}
pub fn calc_checksum_ipv4(&self, ip_header: &Ipv4Header, payload: &[u8]) -> Result<u16, ValueError> {
self.calc_checksum_ipv4_raw(ip_header.source, ip_header.destination, payload)
}
pub fn calc_checksum_ipv4_raw(&self, source: [u8;4], destination: [u8;4], payload: &[u8]) -> Result<u16, ValueError> {
const MAX_PAYLOAD_LENGTH: usize = (std::u16::MAX as usize) - UdpHeader::SERIALIZED_SIZE;
if MAX_PAYLOAD_LENGTH < payload.len() {
return Err(ValueError::UdpPayloadLengthTooLarge(payload.len()));
}
Ok(self.calc_checksum_ipv4_internal(source, destination, payload))
}
fn calc_checksum_ipv4_internal(&self, source: [u8;4], destination: [u8;4], payload: &[u8]) -> u16 {
self.calc_checksum_post_ip(
checksum::Sum16BitWords::new()
.add_4bytes(source)
.add_4bytes(destination)
.add_2bytes([0, ip_number::UDP])
.add_2bytes(self.length.to_be_bytes()),
payload
)
}
pub fn with_ipv6_checksum(source_port: u16, destination_port: u16, ip_header: &Ipv6Header, payload: &[u8]) -> Result<UdpHeader, ValueError> {
const MAX_PAYLOAD_LENGTH: usize = (std::u16::MAX as usize) - UdpHeader::SERIALIZED_SIZE;
if MAX_PAYLOAD_LENGTH <= payload.len() {
return Err(ValueError::UdpPayloadLengthTooLarge(payload.len()));
}
let mut result = UdpHeader{
source_port,
destination_port,
length: (UdpHeader::SERIALIZED_SIZE + payload.len()) as u16,
checksum: 0
};
result.checksum = result.calc_checksum_ipv6_internal(ip_header.source, ip_header.destination, payload);
Ok(result)
}
pub fn calc_checksum_ipv6(&self, ip_header: &Ipv6Header, payload: &[u8]) -> Result<u16, ValueError> {
self.calc_checksum_ipv6_raw(ip_header.source, ip_header.destination, payload)
}
pub fn calc_checksum_ipv6_raw(&self, source: [u8;16], destination: [u8;16], payload: &[u8]) -> Result<u16, ValueError> {
const MAX_PAYLOAD_LENGTH: usize = (std::u32::MAX as usize) - UdpHeader::SERIALIZED_SIZE;
if MAX_PAYLOAD_LENGTH < payload.len() {
return Err(ValueError::UdpPayloadLengthTooLarge(payload.len()));
}
Ok(self.calc_checksum_ipv6_internal(source, destination, payload))
}
fn calc_checksum_ipv6_internal(&self, source: [u8;16], destination: [u8;16], payload: &[u8]) -> u16 {
self.calc_checksum_post_ip(
checksum::Sum16BitWords::new()
.add_16bytes(source)
.add_16bytes(destination)
.add_2bytes([0, ip_number::UDP])
.add_2bytes(self.length.to_be_bytes()),
payload
)
}
fn calc_checksum_post_ip(&self, ip_pseudo_header_sum: checksum::Sum16BitWords, payload: &[u8]) -> u16 {
ip_pseudo_header_sum
.add_2bytes(self.source_port.to_be_bytes())
.add_2bytes(self.destination_port.to_be_bytes())
.add_2bytes(self.length.to_be_bytes())
.add_slice(payload)
.to_ones_complement_with_no_zero()
.to_be()
}
#[deprecated(
since = "0.10.0",
note = "Use UdpHeader::from_slice instead."
)]
#[inline]
pub fn read_from_slice(slice: &[u8]) -> Result<(UdpHeader, &[u8]), ReadError> {
UdpHeader::from_slice(slice)
}
#[inline]
pub fn from_slice(slice: &[u8]) -> Result<(UdpHeader, &[u8]), ReadError> {
Ok((
UdpHeaderSlice::from_slice(slice)?.to_header(),
&slice[UdpHeader::SERIALIZED_SIZE..]
))
}
#[inline]
pub fn from_bytes(bytes: [u8;8]) -> UdpHeader {
UdpHeader{
source_port: u16::from_be_bytes(
[
bytes[0],
bytes[1],
]
),
destination_port: u16::from_be_bytes(
[
bytes[2],
bytes[3],
]
),
length: u16::from_be_bytes(
[
bytes[4],
bytes[5],
]
),
checksum: u16::from_be_bytes(
[
bytes[6],
bytes[7],
]
),
}
}
pub fn read<T: io::Read + io::Seek + Sized>(reader: &mut T) -> Result<UdpHeader, io::Error> {
let bytes = {
let mut bytes : [u8;8] = [0;8];
reader.read_exact(&mut bytes)?;
bytes
};
Ok(UdpHeader::from_bytes(bytes))
}
pub fn write<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
writer.write_all(&self.to_bytes())?;
Ok(())
}
#[inline]
pub fn header_len(&self) -> usize {
UdpHeader::SERIALIZED_SIZE
}
#[inline]
pub fn to_bytes(&self) -> [u8;8] {
let source_port_be = self.source_port.to_be_bytes();
let destination_port_be = self.destination_port.to_be_bytes();
let length_be = self.length.to_be_bytes();
let checksum = self.checksum.to_be_bytes();
[
source_port_be[0],
source_port_be[1],
destination_port_be[0],
destination_port_be[1],
length_be[0],
length_be[1],
checksum[0],
checksum[1],
]
}
}
impl SerializedSize for UdpHeader {
const SERIALIZED_SIZE: usize = 8;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UdpHeaderSlice<'a> {
slice: &'a [u8]
}
impl<'a> UdpHeaderSlice<'a> {
#[inline]
pub fn from_slice(slice: &'a[u8]) -> Result<UdpHeaderSlice<'a>, ReadError> {
use crate::ReadError::*;
if slice.len() < UdpHeader::SERIALIZED_SIZE {
return Err(UnexpectedEndOfSlice(UdpHeader::SERIALIZED_SIZE));
}
Ok(UdpHeaderSlice{
slice: unsafe {
from_raw_parts(
slice.as_ptr(),
UdpHeader::SERIALIZED_SIZE
)
}
})
}
#[inline]
pub fn slice(&self) -> &'a [u8] {
self.slice
}
#[inline]
pub fn source_port(&self) -> u16 {
unsafe {
get_unchecked_be_u16(self.slice.as_ptr())
}
}
#[inline]
pub fn destination_port(&self) -> u16 {
unsafe {
get_unchecked_be_u16(self.slice.as_ptr().add(2))
}
}
#[inline]
pub fn length(&self) -> u16 {
unsafe {
get_unchecked_be_u16(self.slice.as_ptr().add(4))
}
}
#[inline]
pub fn checksum(&self) -> u16 {
unsafe {
get_unchecked_be_u16(self.slice.as_ptr().add(6))
}
}
#[inline]
pub fn to_header(&self) -> UdpHeader {
UdpHeader {
source_port: self.source_port(),
destination_port: self.destination_port(),
length: self.length(),
checksum: self.checksum()
}
}
}