1use std::{fmt, net::SocketAddr};
2
3use bytes::{Buf, BufMut, BytesMut};
4
5use crate::{Instant, MAX_CID_SIZE, ResetToken, coding::BufExt, packet::PartialDecode};
6
7#[derive(Debug)]
9pub struct ConnectionEvent(pub(crate) ConnectionEventInner);
10
11#[derive(Debug)]
12pub(crate) enum ConnectionEventInner {
13 Datagram(DatagramConnectionEvent),
15 NewIdentifiers(Vec<IssuedCid>, Instant),
17}
18
19#[derive(Debug)]
21pub(crate) struct DatagramConnectionEvent {
22 pub(crate) now: Instant,
23 pub(crate) remote: SocketAddr,
24 pub(crate) ecn: Option<EcnCodepoint>,
25 pub(crate) first_decode: PartialDecode,
26 pub(crate) remaining: Option<BytesMut>,
27}
28
29#[derive(Debug)]
31pub struct EndpointEvent(pub(crate) EndpointEventInner);
32
33impl EndpointEvent {
34 pub fn drained() -> Self {
39 Self(EndpointEventInner::Drained)
40 }
41
42 pub fn is_drained(&self) -> bool {
46 self.0 == EndpointEventInner::Drained
47 }
48}
49
50#[derive(Clone, Debug, Eq, PartialEq)]
51pub(crate) enum EndpointEventInner {
52 Drained,
54 ResetToken(SocketAddr, ResetToken),
56 NeedIdentifiers(Instant, u64),
58 RetireConnectionId(Instant, u64, bool),
61 RelayPunchMeNow([u8; 32], crate::frame::PunchMeNow),
63 SendAddressFrame(crate::frame::AddAddress),
65 NatCandidateValidated { address: SocketAddr, challenge: u64 },
67}
68
69#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
73pub struct ConnectionId {
74 len: u8,
76 bytes: [u8; MAX_CID_SIZE],
78}
79
80impl ConnectionId {
81 pub fn new(bytes: &[u8]) -> Self {
83 debug_assert!(bytes.len() <= MAX_CID_SIZE);
84 let mut res = Self {
85 len: bytes.len() as u8,
86 bytes: [0; MAX_CID_SIZE],
87 };
88 res.bytes[..bytes.len()].copy_from_slice(bytes);
89 res
90 }
91
92 pub fn from_buf(buf: &mut (impl Buf + ?Sized), len: usize) -> Self {
96 debug_assert!(len <= MAX_CID_SIZE);
97 let mut res = Self {
98 len: len as u8,
99 bytes: [0; MAX_CID_SIZE],
100 };
101 buf.copy_to_slice(&mut res[..len]);
102 res
103 }
104
105 pub(crate) fn decode_long(buf: &mut impl Buf) -> Option<Self> {
107 let len = buf.get::<u8>().ok()? as usize;
108 match len > MAX_CID_SIZE || buf.remaining() < len {
109 false => Some(Self::from_buf(buf, len)),
110 true => None,
111 }
112 }
113
114 pub(crate) fn encode_long(&self, buf: &mut impl BufMut) {
116 buf.put_u8(self.len() as u8);
117 buf.put_slice(self);
118 }
119}
120
121impl ::std::ops::Deref for ConnectionId {
122 type Target = [u8];
123 fn deref(&self) -> &[u8] {
124 &self.bytes[0..self.len as usize]
125 }
126}
127
128impl ::std::ops::DerefMut for ConnectionId {
129 fn deref_mut(&mut self) -> &mut [u8] {
130 &mut self.bytes[0..self.len as usize]
131 }
132}
133
134impl fmt::Debug for ConnectionId {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 self.bytes[0..self.len as usize].fmt(f)
137 }
138}
139
140impl fmt::Display for ConnectionId {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 for byte in self.iter() {
143 write!(f, "{byte:02x}")?;
144 }
145 Ok(())
146 }
147}
148
149#[repr(u8)]
151#[derive(Debug, Copy, Clone, Eq, PartialEq)]
152pub enum EcnCodepoint {
153 Ect0 = 0b10,
155 Ect1 = 0b01,
157 Ce = 0b11,
159}
160
161impl EcnCodepoint {
162 pub fn from_bits(x: u8) -> Option<Self> {
164 use EcnCodepoint::*;
165 Some(match x & 0b11 {
166 0b10 => Ect0,
167 0b01 => Ect1,
168 0b11 => Ce,
169 _ => {
170 return None;
171 }
172 })
173 }
174
175 pub fn is_ce(self) -> bool {
177 matches!(self, Self::Ce)
178 }
179}
180
181#[derive(Debug, Copy, Clone)]
182pub(crate) struct IssuedCid {
183 pub(crate) sequence: u64,
184 pub(crate) id: ConnectionId,
185 pub(crate) reset_token: ResetToken,
186}