1use std::{fmt, net::SocketAddr};
2
3use bytes::{Buf, BufMut, BytesMut};
4
5use crate::FourTuple;
6use crate::PathId;
7use crate::{Duration, Instant, MAX_CID_SIZE, ResetToken, coding::BufExt, packet::PartialDecode};
8
9#[derive(Debug)]
11pub struct ConnectionEvent(pub(crate) ConnectionEventInner);
12
13#[derive(Debug)]
14pub(crate) enum ConnectionEventInner {
15 Datagram(DatagramConnectionEvent),
17 NewIdentifiers(Vec<IssuedCid>, Instant, usize, Option<Duration>),
19}
20
21#[derive(Debug)]
23pub(crate) struct DatagramConnectionEvent {
24 pub(crate) now: Instant,
25 pub(crate) network_path: FourTuple,
26 pub(crate) path_id: PathId,
27 pub(crate) ecn: Option<EcnCodepoint>,
28 pub(crate) first_decode: PartialDecode,
29 pub(crate) remaining: Option<BytesMut>,
30}
31
32#[derive(Debug)]
34pub struct EndpointEvent(pub(crate) EndpointEventInner);
35
36impl EndpointEvent {
37 pub fn drained() -> Self {
42 Self(EndpointEventInner::Drained)
43 }
44
45 pub fn is_drained(&self) -> bool {
49 self.0 == EndpointEventInner::Drained
50 }
51}
52
53#[derive(Clone, Debug, Eq, PartialEq)]
54pub(crate) enum EndpointEventInner {
55 Drained,
57 ResetToken(PathId, SocketAddr, ResetToken),
63 RetireResetToken(PathId),
68 NeedIdentifiers(PathId, Instant, u64),
77 RetireConnectionId(Instant, PathId, u64, bool),
82}
83
84#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
88#[cfg_attr(test, derive(test_strategy::Arbitrary))]
89pub struct ConnectionId {
90 #[cfg_attr(test, strategy(1u8..=MAX_CID_SIZE as u8))]
92 len: u8,
93 bytes: [u8; MAX_CID_SIZE],
95}
96
97impl ConnectionId {
98 pub fn new(bytes: &[u8]) -> Self {
100 debug_assert!(bytes.len() <= MAX_CID_SIZE);
101 let mut res = Self {
102 len: bytes.len() as u8,
103 bytes: [0; MAX_CID_SIZE],
104 };
105 res.bytes[..bytes.len()].copy_from_slice(bytes);
106 res
107 }
108
109 pub fn from_buf(buf: &mut (impl Buf + ?Sized), len: usize) -> Self {
113 debug_assert!(len <= MAX_CID_SIZE);
114 let mut res = Self {
115 len: len as u8,
116 bytes: [0; MAX_CID_SIZE],
117 };
118 buf.copy_to_slice(&mut res[..len]);
119 res
120 }
121
122 pub(crate) fn len(&self) -> usize {
123 self.len as usize
124 }
125
126 pub(crate) fn decode_long(buf: &mut impl Buf) -> Option<Self> {
128 let len = buf.get::<u8>().ok()? as usize;
129 match len > MAX_CID_SIZE || buf.remaining() < len {
130 false => Some(Self::from_buf(buf, len)),
131 true => None,
132 }
133 }
134
135 pub(crate) fn encode_long(&self, buf: &mut impl BufMut) {
137 buf.put_u8(self.len() as u8);
138 buf.put_slice(self);
139 }
140}
141
142impl ::std::ops::Deref for ConnectionId {
143 type Target = [u8];
144 fn deref(&self) -> &[u8] {
145 &self.bytes[0..self.len as usize]
146 }
147}
148
149impl ::std::ops::DerefMut for ConnectionId {
150 fn deref_mut(&mut self) -> &mut [u8] {
151 &mut self.bytes[0..self.len as usize]
152 }
153}
154
155impl fmt::Debug for ConnectionId {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 self.bytes[0..self.len as usize].fmt(f)
158 }
159}
160
161impl fmt::Display for ConnectionId {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 for byte in self.iter() {
164 write!(f, "{byte:02x}")?;
165 }
166 Ok(())
167 }
168}
169
170#[repr(u8)]
172#[derive(Debug, Copy, Clone, Eq, PartialEq)]
173pub enum EcnCodepoint {
174 Ect0 = 0b10,
176 Ect1 = 0b01,
178 Ce = 0b11,
180}
181
182impl EcnCodepoint {
183 pub fn from_bits(x: u8) -> Option<Self> {
185 use EcnCodepoint::*;
186 Some(match x & 0b11 {
187 0b10 => Ect0,
188 0b01 => Ect1,
189 0b11 => Ce,
190 _ => {
191 return None;
192 }
193 })
194 }
195
196 pub fn is_ce(self) -> bool {
198 matches!(self, Self::Ce)
199 }
200}
201
202#[derive(Debug, Copy, Clone)]
203pub(crate) struct IssuedCid {
204 pub(crate) path_id: PathId,
205 pub(crate) sequence: u64,
206 pub(crate) id: ConnectionId,
207 pub(crate) reset_token: ResetToken,
208}