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
//! UDP: User Datagram Protocol

use core::ops::{Range, RangeFrom};
use core::{fmt, u16};

use as_slice::{AsMutSlice, AsSlice};
use byteorder::{ByteOrder, NetworkEndian as NE};
use cast::{u16, usize};
use owning_slice::Truncate;

use crate::{
    coap::{self, Unset},
    ipv6,
    traits::UncheckedIndex,
};

/* Packet structure */
const SOURCE: Range<usize> = 0..2;
const DESTINATION: Range<usize> = 2..4;
const LENGTH: Range<usize> = 4..6;
const CHECKSUM: Range<usize> = 6..8;
const PAYLOAD: RangeFrom<usize> = 8..;

/// Size of the UDP header
pub const HEADER_SIZE: u8 = PAYLOAD.start as u8;

/// UDP packet
pub struct Packet<BUFFER>
where
    BUFFER: AsSlice<Element = u8>,
{
    buffer: BUFFER,
}

impl<B> Packet<B>
where
    B: AsSlice<Element = u8>,
{
    /* Constructors */
    /// Parses the bytes as an UDP packet
    pub fn parse(bytes: B) -> Result<Self, B> {
        let nbytes = bytes.as_slice().len();
        if nbytes < usize(HEADER_SIZE) {
            return Err(bytes);
        }

        let packet = Packet { buffer: bytes };
        let len = packet.get_length();

        if len < u16(HEADER_SIZE) || usize(len) > nbytes {
            Err(packet.buffer)
        } else {
            Ok(packet)
        }
    }

    /* Getters */
    /// Returns the Source (port) field of the header
    pub fn get_source(&self) -> u16 {
        NE::read_u16(&self.header_()[SOURCE])
    }

    /// Returns the Destination (port) field of the header
    pub fn get_destination(&self) -> u16 {
        NE::read_u16(&self.header_()[DESTINATION])
    }

    /// Returns the Length field of the header
    pub fn get_length(&self) -> u16 {
        NE::read_u16(&self.header_()[LENGTH])
    }

    fn get_checksum(&self) -> u16 {
        NE::read_u16(&self.header_()[CHECKSUM])
    }

    /// Returns the length (header + data) of this packet
    pub fn len(&self) -> u16 {
        self.get_length()
    }

    /* Miscellaneous */
    /// View into the payload
    pub fn payload(&self) -> &[u8] {
        unsafe { self.as_slice().rf(PAYLOAD) }
    }

    /// Returns the byte representation of this UDP packet
    pub fn as_bytes(&self) -> &[u8] {
        self.as_slice()
    }

    /* Miscellaneous */
    pub(crate) fn compute_checksum(&self, src: ipv6::Addr, dest: ipv6::Addr) -> u16 {
        const NEXT_HEADER: u8 = 17;

        let mut sum: u32 = 0;

        // Pseudo-header
        for chunk in src.0.chunks_exact(2).chain(dest.0.chunks_exact(2)) {
            sum += u32::from(NE::read_u16(chunk));
        }

        // XXX should this be just `as u16`?
        let len = self.as_slice().len() as u32;
        sum += len >> 16;
        sum += len & 0xffff;

        sum += u32::from(NEXT_HEADER);

        // UDP message
        for (i, chunk) in self.as_slice().chunks(2).enumerate() {
            if i == 3 {
                // this is the checksum field, skip
                continue;
            }

            if chunk.len() == 1 {
                sum += u32::from(chunk[0]) << 8;
            } else {
                sum += u32::from(NE::read_u16(chunk));
            }
        }

        // fold carry-over
        while sum >> 16 != 0 {
            sum = (sum & 0xffff) + (sum >> 16);
        }

        !(sum as u16)
    }

    /// Verifies the 'Checksum' field
    pub fn verify_ipv6_checksum(&self, src: ipv6::Addr, dest: ipv6::Addr) -> bool {
        self.compute_checksum(src, dest) == self.get_checksum()
    }

    /* Private */
    fn as_slice(&self) -> &[u8] {
        self.buffer.as_slice()
    }

    fn header_(&self) -> &[u8; HEADER_SIZE as usize] {
        debug_assert!(self.as_slice().len() >= HEADER_SIZE as usize);

        unsafe { &*(self.as_slice().as_ptr() as *const _) }
    }

    fn payload_len(&self) -> u16 {
        self.get_length() - u16(HEADER_SIZE)
    }
}

impl<B> Packet<B>
where
    B: AsSlice<Element = u8> + AsMutSlice<Element = u8>,
{
    /* Setters */
    /// Sets the Source (port) field of the header
    pub fn set_source(&mut self, port: u16) {
        NE::write_u16(&mut self.header_mut_()[SOURCE], port)
    }

    /// Sets the Destination (port) field of the header
    pub fn set_destination(&mut self, port: u16) {
        NE::write_u16(&mut self.header_mut_()[DESTINATION], port)
    }

    unsafe fn set_length(&mut self, len: u16) {
        NE::write_u16(&mut self.header_mut_()[LENGTH], len)
    }

    /// Zeroes the Checksum field of the header
    pub fn zero_checksum(&mut self) {
        self.set_checksum(0);
    }

    /// Sets the Destination (port) field of the header
    fn set_checksum(&mut self, checksum: u16) {
        NE::write_u16(&mut self.header_mut_()[CHECKSUM], checksum)
    }

    /* Miscellaneous */
    /// Mutable view into the payload
    pub fn payload_mut(&mut self) -> &mut [u8] {
        &mut self.as_mut_slice()[PAYLOAD]
    }

    /// Recomputes and updates the 'Checksum' field
    pub fn update_ipv6_checksum(&mut self, src: ipv6::Addr, dest: ipv6::Addr) {
        let cksum = self.compute_checksum(src, dest);
        self.set_checksum(cksum)
    }

    /* Private */
    fn as_mut_slice(&mut self) -> &mut [u8] {
        self.buffer.as_mut_slice()
    }

    fn header_mut_(&mut self) -> &mut [u8; HEADER_SIZE as usize] {
        debug_assert!(self.as_slice().len() >= HEADER_SIZE as usize);

        unsafe { &mut *(self.as_mut_slice().as_mut_ptr() as *mut _) }
    }
}

impl<B> Packet<B>
where
    B: AsSlice<Element = u8> + AsMutSlice<Element = u8> + Truncate<u16>,
{
    /* Constructors */
    /// Transforms the given buffer into an UDP packet
    ///
    /// NOTE The UDP packet will span the whole buffer and the Checksum field will be zeroed.
    ///
    /// # Panics
    ///
    /// This constructor panics if the given `buffer` is not large enough to contain the UDP header.
    pub fn new(mut buffer: B) -> Self {
        assert!(buffer.as_slice().len() >= usize(HEADER_SIZE));

        let len = u16(buffer.as_slice().len()).unwrap_or(u16::MAX);
        buffer.truncate(len);
        let mut packet = Packet { buffer };

        packet.set_checksum(0);
        unsafe { packet.set_length(len) }

        packet
    }

    /* Setters */
    /// Fills the payload with the given data and adjusts the length of the UDP packet
    pub fn set_payload(&mut self, data: &[u8]) {
        let len = u16(data.len()).unwrap();
        assert!(self.payload_len() >= len);

        self.truncate(len);
        self.payload_mut().copy_from_slice(data);
    }

    /* Miscellaneous */
    /// Fills the payload with a CoAP message
    pub fn coap<F>(&mut self, token_length: u8, f: F)
    where
        F: FnOnce(coap::Message<&mut [u8], Unset>) -> coap::Message<&mut [u8]>,
    {
        let len = {
            let m = coap::Message::new(self.payload_mut(), token_length);
            f(m).len()
        };
        self.truncate(len);
    }

    /// Truncates the *payload* to the specified length
    pub fn truncate(&mut self, len: u16) {
        if len < self.payload_len() {
            let total_len = len + u16(HEADER_SIZE);
            self.buffer.truncate(total_len);
            unsafe { self.set_length(total_len) }
        }
    }
}

/// NOTE excludes the payload
impl<B> fmt::Debug for Packet<B>
where
    B: AsSlice<Element = u8>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("udp::Packet")
            .field("source", &self.get_source())
            .field("destination", &self.get_destination())
            .field("length", &self.get_length())
            .field("checksum", &self.get_checksum())
            // .field("payload", &self.payload())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use cast::u16;
    use rand::{self, RngCore};

    use crate::{ether, ipv4, mac, udp};

    const SIZE: usize = 56;

    const BYTES: &[u8; SIZE] = &[
        255, 255, 255, 255, 255, 255, // ether: destination
        1, 1, 1, 1, 1, 1, // ether: source
        8, 0,  // ether: type
        69, // ipv4: version & IHL
        0,  // ipv4: DSCP & ECN
        0, 42, //ipv4: total length
        0, 0, // ipv4: identification
        64, 0,  // ipv4: fragment
        64, //ipv4: ttl
        17, //ipv4: protocol
        185, 80, // ipv4: checksum
        192, 168, 0, 33, // ipv4: source
        192, 168, 0, 1, // ipv4: destination
        0, 0, // udp: source
        5, 57, // udp: destination
        0, 22, // udp: length
        0, 0, // udp: checksum
        72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 10, // udp: payload
    ];

    const MAC_SRC: mac::Addr = mac::Addr([0x01; 6]);
    const MAC_DST: mac::Addr = mac::Addr([0xff; 6]);

    const IP_SRC: ipv4::Addr = ipv4::Addr([192, 168, 0, 33]);
    const IP_DST: ipv4::Addr = ipv4::Addr([192, 168, 0, 1]);

    const UDP_DST: u16 = 1337;

    const MESSAGE: &[u8] = b"Hello, world!\n";

    #[test]
    fn construct() {
        // NOTE start with randomized array to make sure we set *everything* correctly
        let mut array: [u8; SIZE] = [0; SIZE];
        rand::thread_rng().fill_bytes(&mut array);

        let mut eth = ether::Frame::new(&mut array[..]);

        eth.set_destination(MAC_DST);
        eth.set_source(MAC_SRC);

        eth.ipv4(|ip| {
            ip.set_destination(IP_DST);
            ip.set_source(IP_SRC);

            ip.udp(|udp| {
                udp.set_source(0);
                udp.set_destination(UDP_DST);
                udp.set_payload(MESSAGE);
            });
        });

        assert_eq!(eth.as_bytes(), &BYTES[..]);
    }

    #[test]
    fn new() {
        const SZ: u16 = 128;

        let mut chunk = [0; SZ as usize];
        let buf = &mut chunk[..];

        let udp = udp::Packet::new(buf);
        assert_eq!(udp.len(), SZ);
        assert_eq!(udp.get_length(), SZ);
    }

    #[test]
    fn parse() {
        let eth = ether::Frame::parse(&BYTES[..]).unwrap();
        assert_eq!(eth.get_destination(), MAC_DST);
        assert_eq!(eth.get_source(), MAC_SRC);
        assert_eq!(eth.get_type(), ether::Type::Ipv4);

        let ip = ipv4::Packet::parse(eth.payload()).unwrap();
        assert_eq!(ip.get_source(), IP_SRC);
        assert_eq!(ip.get_destination(), IP_DST);

        let udp = udp::Packet::parse(ip.payload()).unwrap();
        assert_eq!(udp.get_source(), 0);
        assert_eq!(udp.get_destination(), UDP_DST);
        assert_eq!(
            udp.get_length(),
            MESSAGE.len() as u16 + u16(udp::HEADER_SIZE)
        );
        assert_eq!(udp.payload(), MESSAGE);
    }
}