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),
70 RetireConnectionId(Instant, PathId, u64, bool),
75}
76
77#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
81#[cfg_attr(test, derive(test_strategy::Arbitrary))]
82pub struct ConnectionId {
83 #[cfg_attr(test, strategy(1u8..=MAX_CID_SIZE as u8))]
85 len: u8,
86 bytes: [u8; MAX_CID_SIZE],
88}
89
90impl ConnectionId {
91 pub fn new(bytes: &[u8]) -> Self {
93 debug_assert!(bytes.len() <= MAX_CID_SIZE);
94 let mut res = Self {
95 len: bytes.len() as u8,
96 bytes: [0; MAX_CID_SIZE],
97 };
98 res.bytes[..bytes.len()].copy_from_slice(bytes);
99 res
100 }
101
102 pub fn from_buf(buf: &mut (impl Buf + ?Sized), len: usize) -> Self {
106 debug_assert!(len <= MAX_CID_SIZE);
107 let mut res = Self {
108 len: len as u8,
109 bytes: [0; MAX_CID_SIZE],
110 };
111 buf.copy_to_slice(&mut res[..len]);
112 res
113 }
114
115 pub(crate) fn len(&self) -> usize {
116 self.len as usize
117 }
118
119 pub(crate) fn decode_long(buf: &mut impl Buf) -> Option<Self> {
121 let len = buf.get::<u8>().ok()? as usize;
122 match len > MAX_CID_SIZE || buf.remaining() < len {
123 false => Some(Self::from_buf(buf, len)),
124 true => None,
125 }
126 }
127
128 pub(crate) fn encode_long(&self, buf: &mut impl BufMut) {
130 buf.put_u8(self.len() as u8);
131 buf.put_slice(self);
132 }
133}
134
135impl ::std::ops::Deref for ConnectionId {
136 type Target = [u8];
137 fn deref(&self) -> &[u8] {
138 &self.bytes[0..self.len as usize]
139 }
140}
141
142impl ::std::ops::DerefMut for ConnectionId {
143 fn deref_mut(&mut self) -> &mut [u8] {
144 &mut self.bytes[0..self.len as usize]
145 }
146}
147
148impl fmt::Debug for ConnectionId {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 self.bytes[0..self.len as usize].fmt(f)
151 }
152}
153
154impl fmt::Display for ConnectionId {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 for byte in self.iter() {
157 write!(f, "{byte:02x}")?;
158 }
159 Ok(())
160 }
161}
162
163#[repr(u8)]
165#[derive(Debug, Copy, Clone, Eq, PartialEq)]
166pub enum EcnCodepoint {
167 Ect0 = 0b10,
169 Ect1 = 0b01,
171 Ce = 0b11,
173}
174
175impl EcnCodepoint {
176 pub fn from_bits(x: u8) -> Option<Self> {
178 use EcnCodepoint::*;
179 Some(match x & 0b11 {
180 0b10 => Ect0,
181 0b01 => Ect1,
182 0b11 => Ce,
183 _ => {
184 return None;
185 }
186 })
187 }
188
189 pub fn is_ce(self) -> bool {
191 matches!(self, Self::Ce)
192 }
193}
194
195#[derive(Debug, Copy, Clone)]
196pub(crate) struct IssuedCid {
197 pub(crate) path_id: PathId,
198 pub(crate) sequence: u64,
199 pub(crate) id: ConnectionId,
200 pub(crate) reset_token: ResetToken,
201}