udp_socket/
proto.rs

1use std::net::{IpAddr, Ipv6Addr, SocketAddr};
2
3/// The capabilities a UDP socket supports on a certain platform.
4#[derive(Clone, Copy, Debug)]
5pub struct UdpCapabilities {
6    /// The maximum amount of segments which can be transmitted if a platform
7    /// supports Generic Send Offload (GSO).
8    /// This is 1 if the platform doesn't support GSO.
9    pub max_gso_segments: usize,
10}
11
12/// An outgoing packet
13#[derive(Debug)]
14pub struct Transmit {
15    /// The socket this datagram should be sent to.
16    pub destination: SocketAddr,
17    /// Explicit congestion notification bits to set on the packet.
18    pub ecn: Option<EcnCodepoint>,
19    /// Contents of the datagram.
20    pub contents: Vec<u8>,
21    /// The segment size if this transmission contains multiple datagrams.
22    /// This is `None` if the transmit only contains a single datgram.
23    pub segment_size: Option<usize>,
24    /// Optional source IP address for the datagram.
25    pub src_ip: Option<IpAddr>,
26}
27
28/// An incoming packet
29#[derive(Clone, Copy, Debug)]
30pub struct RecvMeta {
31    /// The socket this datagram was sent from.
32    pub source: SocketAddr,
33    /// Length of the payload of the packet.
34    pub len: usize,
35    /// Explicit congestion notification bits set on the packet.
36    pub ecn: Option<EcnCodepoint>,
37    /// Optional destination IP address of the datagram.
38    pub dst_ip: Option<IpAddr>,
39}
40
41impl Default for RecvMeta {
42    fn default() -> Self {
43        Self {
44            source: SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0),
45            len: 0,
46            ecn: None,
47            dst_ip: None,
48        }
49    }
50}
51
52/// Explicit congestion notification codepoint
53#[allow(clippy::upper_case_acronyms)]
54#[repr(u8)]
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub enum EcnCodepoint {
57    #[doc(hidden)]
58    ECT0 = 0b10,
59    #[doc(hidden)]
60    ECT1 = 0b01,
61    #[doc(hidden)]
62    CE = 0b11,
63}
64
65impl EcnCodepoint {
66    /// Create new object from the given bits
67    pub fn from_bits(x: u8) -> Option<Self> {
68        use self::EcnCodepoint::*;
69        Some(match x & 0b11 {
70            0b10 => ECT0,
71            0b01 => ECT1,
72            0b11 => CE,
73            _ => {
74                return None;
75            }
76        })
77    }
78}
79
80/// Socket type.
81#[derive(Clone, Copy, Debug, Eq, PartialEq)]
82pub enum SocketType {
83    /// Socket is bound to an ip4 address.
84    Ipv4,
85    /// Socket is bound to an ip6 address and is not dual stack.
86    Ipv6Only,
87    /// Socket is bound to an ip6 address and supports ip4 packets.
88    Ipv6,
89}