Skip to main content

capsules_extra/net/udp/
udp.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! This file contains the structs and methods associated with the UDP header.
6//!
7//! This includes getters and setters for the various header fields, as well
8//! as the standard encode/decode functionality required for serializing
9//! the struct for transmission.
10
11use crate::net::stream::SResult;
12use crate::net::stream::decode_u16;
13use crate::net::stream::encode_u16;
14
15// Note: All UDP Header fields are stored in network byte order
16
17/// The `UDPHeader` struct follows the layout for the UDP packet header.
18///
19/// Note that the implementation of this struct provides getters and setters
20/// for the various fields of the header, to avoid confusion with endian-ness.
21#[derive(Copy, Clone, Debug)]
22pub struct UDPHeader {
23    src_port: u16,
24    dst_port: u16,
25    len: u16,
26    cksum: u16,
27}
28
29impl Default for UDPHeader {
30    fn default() -> UDPHeader {
31        UDPHeader {
32            src_port: 0,
33            dst_port: 0,
34            len: 8,
35            cksum: 0,
36        }
37    }
38}
39
40impl UDPHeader {
41    pub fn new() -> UDPHeader {
42        UDPHeader::default()
43    }
44    // TODO: Always returns size of UDP header
45    pub fn get_offset(&self) -> usize {
46        8
47    }
48
49    pub fn set_dst_port(&mut self, port: u16) {
50        self.dst_port = port.to_be();
51    }
52    pub fn set_src_port(&mut self, port: u16) {
53        self.src_port = port.to_be();
54    }
55
56    pub fn set_len(&mut self, len: u16) {
57        self.len = len.to_be();
58    }
59
60    pub fn set_cksum(&mut self, cksum: u16) {
61        self.cksum = cksum.to_be();
62    }
63
64    pub fn get_src_port(&self) -> u16 {
65        u16::from_be(self.src_port)
66    }
67
68    pub fn get_dst_port(&self) -> u16 {
69        u16::from_be(self.dst_port)
70    }
71
72    pub fn get_len(&self) -> u16 {
73        u16::from_be(self.len)
74    }
75
76    pub fn get_cksum(&self) -> u16 {
77        u16::from_be(self.cksum)
78    }
79
80    pub fn get_hdr_size(&self) -> usize {
81        // TODO
82        8
83    }
84
85    /// This function serializes the `UDPHeader` into the provided buffer.
86    ///
87    /// # Arguments
88    ///
89    /// `buf` - A mutable buffer to serialize the `UDPHeader` into
90    /// `offset` - The current offset into the provided buffer
91    ///
92    /// # Return Value
93    ///
94    /// This function returns the new offset into the buffer wrapped in an
95    /// SResult.
96    pub fn encode(&self, buf: &mut [u8], offset: usize) -> SResult<usize> {
97        stream_len_cond!(buf, self.get_hdr_size() + offset);
98
99        let mut off = offset;
100        off = enc_consume!(buf, off; encode_u16, self.src_port);
101        off = enc_consume!(buf, off; encode_u16, self.dst_port);
102        off = enc_consume!(buf, off; encode_u16, self.len);
103        off = enc_consume!(buf, off; encode_u16, self.cksum);
104        stream_done!(off, off);
105    }
106
107    /// This function deserializes the `UDPHeader` from the provided buffer.
108    ///
109    /// # Arguments
110    ///
111    /// `buf` - The byte array corresponding to a serialized `UDPHeader`
112    ///
113    /// # Return Value
114    ///
115    /// This function returns a `UDPHeader` struct wrapped in an SResult
116    pub fn decode(buf: &[u8]) -> SResult<UDPHeader> {
117        stream_len_cond!(buf, 8);
118        let mut udp_header = Self::new();
119        let off = 0;
120        let (off, src_port) = dec_try!(buf, off; decode_u16);
121        udp_header.src_port = u16::from_be(src_port);
122        let (off, dst_port) = dec_try!(buf, off; decode_u16);
123        udp_header.dst_port = u16::from_be(dst_port);
124        let (off, len) = dec_try!(buf, off; decode_u16);
125        udp_header.len = u16::from_be(len);
126        let (off, cksum) = dec_try!(buf, off; decode_u16);
127        udp_header.cksum = u16::from_be(cksum);
128        stream_done!(off, udp_header);
129    }
130}