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