use std::{
collections::{BTreeMap, HashMap, HashSet},
hash::{Hash, Hasher},
sync::{
Arc,
atomic::{AtomicU32, AtomicU64, Ordering},
},
time::{Duration, Instant, SystemTime},
};
#[cfg(feature = "security")]
use std::{net::SocketAddr, sync::atomic::AtomicBool};
use tokio::sync::oneshot;
use bytes::Bytes;
use parking_lot::{Mutex, RwLock};
use crate::PeerAddr;
use crate::cc::CongestionCtrl;
use crate::frame::MAX_NACK_SEQS;
#[cfg(feature = "security")]
use crate::security::SecureChannel;
pub struct SentPacket {
pub frame: Bytes,
pub sent_at: Instant,
}
pub struct PeerState {
addr: Arc<RwLock<PeerAddr>>,
pub generation: u64,
their_session_id: Mutex<Option<u64>>,
next_seqs: Mutex<HashMap<u16, u64>>,
pub sent: Mutex<HashMap<u16, BTreeMap<u64, SentPacket>>>,
pub last_sent: Mutex<Instant>,
pub last_seen: Mutex<Instant>,
pub cc: Mutex<CongestionCtrl>,
effective_mtu: AtomicU32,
pub probe_acks: Mutex<HashMap<u16, oneshot::Sender<()>>>,
#[cfg(feature = "security")]
pub channel: RwLock<Option<SecureChannel>>,
#[cfg(feature = "security")]
remote_static_key: RwLock<Option<[u8; 32]>>,
#[cfg(feature = "security")]
confirmed: AtomicBool,
#[cfg(feature = "security")]
pub confirmed_notify: tokio::sync::Notify,
#[cfg(feature = "security")]
finish_wire: Mutex<Option<Bytes>>,
#[cfg(feature = "security")]
initiator: AtomicBool,
#[cfg(feature = "security")]
rehandshake_started: Mutex<Option<Instant>>,
#[cfg(feature = "security")]
highest_nonce: AtomicU64,
#[cfg(feature = "security")]
direct: Mutex<Option<(SocketAddr, Instant)>>,
#[cfg(feature = "security")]
last_intro: Mutex<Option<Instant>>,
#[cfg(feature = "security")]
intro_open: Mutex<Option<Instant>>,
#[cfg(feature = "security")]
punching: AtomicBool,
#[cfg(feature = "security")]
challenges: Mutex<HashMap<SocketAddr, Challenge>>,
pub rx_bytes: AtomicU64,
pub tx_bytes: AtomicU64,
pub retransmit_count: AtomicU64,
pub tail_probe_armed: Mutex<HashSet<u16>>,
announced: std::sync::atomic::AtomicBool,
}
impl PeerState {
#[must_use]
pub fn new(addr: PeerAddr) -> Self {
Self::with_addr(Arc::new(RwLock::new(addr)))
}
#[must_use]
pub fn with_addr(addr: Arc<RwLock<PeerAddr>>) -> Self {
let now = Instant::now();
Self {
addr,
generation: random_u64(),
their_session_id: Mutex::new(None),
next_seqs: Mutex::new(HashMap::new()),
sent: Mutex::new(HashMap::new()),
last_sent: Mutex::new(now),
last_seen: Mutex::new(now),
cc: Mutex::new(CongestionCtrl::new()),
effective_mtu: AtomicU32::new(0),
probe_acks: Mutex::new(HashMap::new()),
#[cfg(feature = "security")]
channel: RwLock::new(None),
#[cfg(feature = "security")]
remote_static_key: RwLock::new(None),
#[cfg(feature = "security")]
confirmed: AtomicBool::new(false),
#[cfg(feature = "security")]
confirmed_notify: tokio::sync::Notify::new(),
#[cfg(feature = "security")]
finish_wire: Mutex::new(None),
#[cfg(feature = "security")]
initiator: AtomicBool::new(false),
#[cfg(feature = "security")]
rehandshake_started: Mutex::new(None),
#[cfg(feature = "security")]
highest_nonce: AtomicU64::new(0),
#[cfg(feature = "security")]
direct: Mutex::new(None),
#[cfg(feature = "security")]
last_intro: Mutex::new(None),
#[cfg(feature = "security")]
intro_open: Mutex::new(None),
#[cfg(feature = "security")]
punching: AtomicBool::new(false),
#[cfg(feature = "security")]
challenges: Mutex::new(HashMap::new()),
rx_bytes: AtomicU64::new(0),
tx_bytes: AtomicU64::new(0),
retransmit_count: AtomicU64::new(0),
tail_probe_armed: Mutex::new(HashSet::new()),
announced: std::sync::atomic::AtomicBool::new(false),
}
}
pub fn addr(&self) -> PeerAddr {
*self.addr.read()
}
pub fn addr_arc(&self) -> Arc<RwLock<PeerAddr>> {
self.addr.clone()
}
pub fn migrate_to(&self, new_addr: PeerAddr) {
*self.addr.write() = new_addr;
}
#[must_use]
pub fn their_session_id(&self) -> Option<u64> {
*self.their_session_id.lock()
}
pub fn set_their_session_id(&self, id: u64) {
*self.their_session_id.lock() = Some(id);
}
pub fn alloc_seq(&self, stream_id: u16) -> u64 {
let mut map = self.next_seqs.lock();
let counter = map.entry(stream_id).or_insert(1);
let seq = *counter;
*counter += 1;
seq
}
pub fn record_sent(&self, stream_id: u16, seq: u64, frame: Bytes, max_frames: usize) {
self.tx_bytes
.fetch_add(frame.len() as u64, Ordering::Relaxed);
let mut map = self.sent.lock();
let stream_buf = map.entry(stream_id).or_default();
if stream_buf.len() >= max_frames
&& let Some((evicted_seq, _)) = stream_buf.pop_first()
{
tracing::warn!(
target: "zudp::peer",
stream_id,
evicted_seq,
cap = max_frames,
"sent buffer full — evicting oldest frame (NACK for it will not retransmit)"
);
}
stream_buf.insert(
seq,
SentPacket {
frame,
sent_at: Instant::now(),
},
);
drop(map);
*self.last_sent.lock() = Instant::now();
}
pub fn frames_for_retransmit(&self, stream_id: u16, seqs: &[u64]) -> Vec<(u64, Bytes)> {
let map = self.sent.lock();
let Some(stream_buf) = map.get(&stream_id) else {
return vec![];
};
let frames: Vec<(u64, Bytes)> = seqs
.iter()
.filter_map(|seq| stream_buf.get(seq).map(|p| (*seq, p.frame.clone())))
.collect();
self.retransmit_count
.fetch_add(frames.len() as u64, Ordering::Relaxed);
frames
}
pub fn newest_sent(&self, stream_id: u16) -> Option<(u64, Bytes, Instant)> {
let map = self.sent.lock();
let (seq, packet) = map.get(&stream_id)?.last_key_value()?;
Some((*seq, packet.frame.clone(), packet.sent_at))
}
pub fn prune_sent(&self, max_age: Duration) {
if let Some(cutoff) = Instant::now().checked_sub(max_age) {
let mut map = self.sent.lock();
for stream_buf in map.values_mut() {
stream_buf.retain(|_, p| p.sent_at > cutoff);
}
}
}
pub fn mark_seen(&self) {
*self.last_seen.lock() = Instant::now();
}
pub fn mark_sent(&self) {
*self.last_sent.lock() = Instant::now();
}
pub fn since_sent(&self) -> Duration {
self.last_sent.lock().elapsed()
}
pub fn since_seen(&self) -> Duration {
self.last_seen.lock().elapsed()
}
pub fn announce(&self) -> bool {
!self.announced.swap(true, Ordering::AcqRel)
}
pub fn effective_mtu_or(&self, default: usize) -> usize {
match self.effective_mtu.load(Ordering::Relaxed) {
0 => default,
v => v as usize,
}
}
pub fn set_effective_mtu(&self, mtu: usize) {
self.effective_mtu
.store(u32::try_from(mtu).unwrap_or(u32::MAX), Ordering::Relaxed);
}
#[cfg(feature = "security")]
#[must_use]
pub fn remote_static_key(&self) -> Option<[u8; 32]> {
*self.remote_static_key.read()
}
#[cfg(feature = "security")]
pub fn set_channel(
&self,
channel: SecureChannel,
remote_static: Option<[u8; 32]>,
finish_wire: Option<Bytes>,
initiator: bool,
) {
self.initiator.store(initiator, Ordering::Release);
*self.channel.write() = Some(channel);
*self.remote_static_key.write() = remote_static;
*self.finish_wire.lock() = finish_wire;
self.confirmed.store(false, Ordering::Release);
}
#[cfg(feature = "security")]
#[must_use]
pub fn local_index(&self) -> Option<u64> {
self.channel.read().as_ref().map(SecureChannel::local_index)
}
#[cfg(feature = "security")]
#[must_use]
pub fn remote_index(&self) -> Option<u64> {
self.channel
.read()
.as_ref()
.map(SecureChannel::remote_index)
}
#[cfg(feature = "security")]
#[must_use]
pub fn is_initiator(&self) -> bool {
self.initiator.load(Ordering::Acquire)
}
#[cfg(feature = "security")]
pub fn begin_rehandshake(&self, cooldown: Duration) -> bool {
let mut started = self.rehandshake_started.lock();
if started.is_some_and(|t| t.elapsed() < cooldown) {
return false;
}
*started = Some(Instant::now());
true
}
#[cfg(feature = "security")]
pub fn advance_nonce(&self, nonce: u64) -> bool {
self.highest_nonce.fetch_max(nonce, Ordering::AcqRel) < nonce
}
#[cfg(feature = "security")]
#[must_use]
pub fn direct_path(&self) -> Option<SocketAddr> {
self.direct.lock().map(|(addr, _)| addr)
}
#[cfg(feature = "security")]
pub fn touch_direct(&self, addr: SocketAddr, may_switch: bool) -> Option<Option<SocketAddr>> {
let mut direct = self.direct.lock();
match *direct {
Some((current, ref mut seen)) if current == addr => {
*seen = Instant::now();
None
}
previous if may_switch => {
*direct = Some((addr, Instant::now()));
Some(previous.map(|(old, _)| old))
}
_ => None,
}
}
#[cfg(feature = "security")]
pub fn expire_direct(&self, timeout: Duration) -> Option<SocketAddr> {
let mut direct = self.direct.lock();
let (addr, seen) = (*direct)?;
if seen.elapsed() < timeout {
return None;
}
*direct = None;
Some(addr)
}
#[cfg(feature = "security")]
pub fn begin_intro(&self, cooldown: Duration) -> bool {
let mut last = self.last_intro.lock();
if last.is_some_and(|t| t.elapsed() < cooldown) {
return false;
}
let now = Instant::now();
*last = Some(now);
*self.intro_open.lock() = Some(now);
true
}
#[cfg(feature = "security")]
pub fn take_intro(&self, window: Duration) -> bool {
self.intro_open
.lock()
.take()
.is_some_and(|asked| asked.elapsed() < window)
}
#[cfg(feature = "security")]
pub fn begin_punch(&self) -> bool {
!self.punching.swap(true, Ordering::AcqRel)
}
#[cfg(feature = "security")]
pub fn end_punch(&self) {
self.punching.store(false, Ordering::Release);
}
#[cfg(feature = "security")]
pub fn begin_challenge(
&self,
addr: SocketAddr,
retry: Duration,
lifetime: Duration,
) -> Option<u64> {
let mut challenges = self.challenges.lock();
challenges.retain(|_, c| c.issued.elapsed() < lifetime);
if let Some(c) = challenges.get_mut(&addr) {
if c.sent.elapsed() < retry {
return None;
}
c.sent = Instant::now();
return Some(c.token);
}
if challenges.len() >= MAX_PATH_CANDIDATES {
return None;
}
let now = Instant::now();
let token = random_u64();
challenges.insert(
addr,
Challenge {
token,
issued: now,
sent: now,
},
);
Some(token)
}
#[cfg(feature = "security")]
pub fn answer_challenge(&self, addr: SocketAddr, token: u64) -> bool {
let mut challenges = self.challenges.lock();
if challenges.get(&addr).is_some_and(|c| c.token == token) {
challenges.clear();
return true;
}
false
}
#[must_use]
#[cfg_attr(not(feature = "security"), allow(clippy::unused_self))]
pub fn has_channel(&self) -> bool {
#[cfg(feature = "security")]
{
self.channel.read().is_some()
}
#[cfg(not(feature = "security"))]
{
false
}
}
#[cfg(feature = "security")]
pub fn mark_confirmed(&self) {
if !self.confirmed.swap(true, Ordering::AcqRel) {
self.confirmed_notify.notify_waiters();
}
}
#[cfg(feature = "security")]
#[must_use]
pub fn is_confirmed(&self) -> bool {
self.confirmed.load(Ordering::Acquire)
}
#[cfg(feature = "security")]
#[must_use]
pub fn finish_wire(&self) -> Option<Bytes> {
self.finish_wire.lock().clone()
}
#[cfg(feature = "security")]
pub fn clear_finish_wire(&self) {
self.finish_wire.lock().take();
}
}
#[cfg(feature = "security")]
pub const MAX_PATH_CANDIDATES: usize = 4;
#[cfg(feature = "security")]
struct Challenge {
token: u64,
issued: Instant,
sent: Instant,
}
#[must_use]
pub fn random_u64() -> u64 {
use std::hash::{BuildHasher, RandomState};
static SEQ: AtomicU64 = AtomicU64::new(0);
let mut h = RandomState::new().build_hasher();
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.hash(&mut h);
SEQ.fetch_add(1, Ordering::Relaxed).hash(&mut h);
h.finish()
}
#[derive(Debug)]
pub enum Inbound {
Data(Bytes),
Fragment {
msg_id: u32,
frag_idx: u16,
frag_total: u16,
data: Bytes,
},
}
pub struct RecvState {
expected_seq: u64,
buf: BTreeMap<u64, Inbound>,
window: u64,
gap_since: Option<Instant>,
}
impl RecvState {
#[must_use]
pub fn new(window: usize) -> Self {
Self {
expected_seq: 1,
buf: BTreeMap::new(),
window: u64::try_from(window).unwrap_or(u64::MAX).max(1),
gap_since: None,
}
}
pub fn ingest(&mut self, seq: u64, payload: Inbound, allow_reorder: bool) -> Vec<u64> {
if seq < self.expected_seq
|| seq - self.expected_seq >= self.window
|| (seq != self.expected_seq && !allow_reorder)
|| self.buf.contains_key(&seq)
{
return vec![];
}
let nack_seqs = (self.expected_seq..seq)
.filter(|missing| !self.buf.contains_key(missing))
.take(MAX_NACK_SEQS)
.collect();
self.buf.insert(seq, payload);
self.refresh_gap();
nack_seqs
}
#[must_use]
pub fn has_ready(&self) -> bool {
self.buf.contains_key(&self.expected_seq)
}
pub fn pop_ready(&mut self) -> Option<Inbound> {
let next = self.buf.remove(&self.expected_seq)?;
self.expected_seq += 1;
self.refresh_gap();
Some(next)
}
fn refresh_gap(&mut self) {
if self.buf.is_empty() || self.has_ready() {
self.gap_since = None;
} else if self.gap_since.is_none() {
self.gap_since = Some(Instant::now());
}
}
pub fn prune_gap(&mut self, max_age: Duration) -> bool {
let Some(since) = self.gap_since else {
return false;
};
if since.elapsed() < max_age {
return false;
}
if let Some((&first, _)) = self.buf.first_key_value() {
self.expected_seq = first;
}
self.gap_since = None;
true
}
}
#[cfg(test)]
mod tests {
use super::*;
fn data() -> Inbound {
Inbound::Data(Bytes::from_static(b"x"))
}
fn drain(recv: &mut RecvState) -> usize {
std::iter::from_fn(|| recv.pop_ready()).count()
}
#[cfg(feature = "security")]
#[test]
fn path_challenges_survive_slow_answers_and_other_candidates() {
let peer = PeerState::new(PeerAddr::Direct("127.0.0.1:1".parse().unwrap()));
let (x, y): (SocketAddr, SocketAddr) = (
"192.0.2.1:1".parse().unwrap(),
"192.0.2.2:1".parse().unwrap(),
);
let retry = Duration::from_millis(10);
let life = Duration::from_secs(5);
let token = peer.begin_challenge(x, retry, life).unwrap();
assert_eq!(
peer.begin_challenge(x, retry, life),
None,
"resent too soon"
);
std::thread::sleep(Duration::from_millis(20));
assert_eq!(
peer.begin_challenge(x, retry, life),
Some(token),
"token rotated"
);
assert!(peer.begin_challenge(y, retry, life).is_some());
assert!(
peer.answer_challenge(x, token),
"another candidate clobbered x"
);
for port in 0..10u16 {
let candidate = SocketAddr::new(x.ip(), 1000 + port);
let _ = peer.begin_challenge(candidate, retry, life);
}
assert!(peer.challenges.lock().len() <= MAX_PATH_CANDIDATES);
}
#[test]
fn nack_skips_seqs_already_buffered() {
let mut recv = RecvState::new(1024);
assert_eq!(recv.ingest(3, data(), true), vec![1, 2]);
assert_eq!(recv.ingest(5, data(), true), vec![1, 2, 4], "3 is buffered");
assert!(recv.ingest(1, data(), true).is_empty());
assert_eq!(
drain(&mut recv),
1,
"2 still missing, so only 1 is delivered"
);
}
#[test]
fn huge_sequence_number_is_dropped_without_allocating() {
let mut recv = RecvState::new(1024);
assert!(recv.ingest(u64::MAX - 1, data(), true).is_empty());
assert!(recv.ingest(1 << 40, data(), true).is_empty());
assert!(recv.buf.is_empty());
assert!(recv.ingest(1, data(), true).is_empty());
assert_eq!(drain(&mut recv), 1);
}
#[test]
fn nack_list_is_capped() {
let mut recv = RecvState::new(4096);
assert_eq!(recv.ingest(4000, data(), true).len(), MAX_NACK_SEQS);
}
#[test]
fn undelivered_frames_stay_buffered() {
let mut recv = RecvState::new(1024);
recv.ingest(1, data(), true);
recv.ingest(2, data(), true);
assert!(recv.has_ready());
assert!(
recv.ingest(1, data(), true).is_empty(),
"duplicate of a buffered frame"
);
assert_eq!(drain(&mut recv), 2);
assert!(!recv.has_ready());
}
#[test]
fn in_order_only_rejects_out_of_order() {
let mut recv = RecvState::new(1024);
assert!(recv.ingest(2, data(), false).is_empty());
assert!(!recv.has_ready());
recv.ingest(1, data(), false);
assert_eq!(drain(&mut recv), 1);
}
#[test]
fn gap_timeout_keeps_frames_after_the_hole() {
let mut recv = RecvState::new(1024);
recv.ingest(3, data(), true);
recv.ingest(4, data(), true);
assert!(recv.prune_gap(Duration::ZERO));
assert_eq!(drain(&mut recv), 2);
}
}