use std::collections::{HashMap, VecDeque};
use std::io::{self, Read, Write};
use std::net::{Shutdown, TcpStream};
use std::os::unix::net::UnixStream;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};
use super::proto::{self, Frame, INITIAL_WINDOW};
const CHUNK: usize = 16 * 1024;
const STREAM_STACK: usize = 128 * 1024;
fn spawn_stream_thread(f: impl FnOnce() + Send + 'static) {
std::thread::Builder::new().stack_size(STREAM_STACK).spawn(f).expect("failed to spawn stream thread");
}
pub const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(15);
pub const KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(45);
fn is_dead(since_last_inbound: Duration, timeout: Duration) -> bool {
since_last_inbound >= timeout
}
const MAX_STREAMS: usize = 64;
const MAX_HOST_STREAMS: usize = 256;
const HOST_ID_BIT: u32 = 1 << 31;
const FLOW_VIOLATION: &str = "flow control violation";
fn should_grant(ungranted: u32) -> bool {
ungranted >= INITIAL_WINDOW / 2
}
fn check_data(len: usize, recv_credit: u32, eof_received: bool) -> Result<(), &'static str> {
if eof_received {
return Err("data after eof");
}
if len > recv_credit as usize {
return Err(FLOW_VIOLATION);
}
Ok(())
}
fn teardown_ready(eof_sent: bool, eof_received: bool, drained: bool) -> bool {
eof_sent && eof_received && drained
}
fn connect_allowed(stream: u32, live: bool, live_host_streams: usize) -> bool {
stream & HOST_ID_BIT != 0 && !live && live_host_streams < MAX_HOST_STREAMS
}
fn next_host_id(next: &mut u32, is_live: impl Fn(u32) -> bool) -> u32 {
loop {
let n = *next;
*next = if n + 1 >= HOST_ID_BIT { 1 } else { n + 1 };
let id = HOST_ID_BIT | n;
if !is_live(id) {
return id;
}
}
}
fn validate_connect_target(host: &str, port: u16) -> io::Result<()> {
let bad = |msg: &str| Err(io::Error::new(io::ErrorKind::InvalidInput, msg.to_string()));
if host.is_empty() {
return bad("connect: empty host");
}
if host.len() > u8::MAX as usize {
return bad("connect: host longer than 255 bytes");
}
if port == 0 {
return bad("connect: port 0");
}
Ok(())
}
#[derive(Debug)]
pub enum Conn {
Unix(UnixStream),
Tcp(TcpStream),
}
impl Conn {
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
match self {
Conn::Unix(s) => s.shutdown(how),
Conn::Tcp(s) => s.shutdown(how),
}
}
pub fn try_clone(&self) -> io::Result<Conn> {
Ok(match self {
Conn::Unix(s) => Conn::Unix(s.try_clone()?),
Conn::Tcp(s) => Conn::Tcp(s.try_clone()?),
})
}
}
impl From<UnixStream> for Conn {
fn from(s: UnixStream) -> Conn {
Conn::Unix(s)
}
}
impl From<TcpStream> for Conn {
fn from(s: TcpStream) -> Conn {
Conn::Tcp(s)
}
}
impl Read for &Conn {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match *self {
Conn::Unix(s) => (&*s).read(buf),
Conn::Tcp(s) => (&*s).read(buf),
}
}
}
impl Write for &Conn {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match *self {
Conn::Unix(s) => (&*s).write(buf),
Conn::Tcp(s) => (&*s).write(buf),
}
}
fn flush(&mut self) -> io::Result<()> {
match *self {
Conn::Unix(s) => (&*s).flush(),
Conn::Tcp(s) => (&*s).flush(),
}
}
}
impl Read for Conn {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&*self).read(buf)
}
}
impl Write for Conn {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&*self).write(buf)
}
fn flush(&mut self) -> io::Result<()> {
(&*self).flush()
}
}
pub type OnClose = Box<dyn FnOnce(String) + Send>;
enum Entry {
Legacy(Arc<Conn>),
Flow(Arc<Flow>),
}
impl Entry {
fn end(self, reason: String) {
match self {
Entry::Legacy(conn) => {
let _ = conn.shutdown(Shutdown::Both);
}
Entry::Flow(flow) => flow.end(reason),
}
}
}
struct Flow {
state: Mutex<FlowState>,
cv: Condvar,
}
struct FlowState {
conn: Option<Arc<Conn>>,
send_credit: u32,
recv_credit: u32,
queue: VecDeque<Vec<u8>>,
ungranted: u32,
eof_sent: bool,
eof_received: bool,
drained: bool,
closed: bool,
on_close: Option<OnClose>,
}
impl Flow {
fn new(conn: Option<Arc<Conn>>, on_close: Option<OnClose>) -> Arc<Flow> {
Arc::new(Flow {
state: Mutex::new(FlowState {
conn,
send_credit: INITIAL_WINDOW,
recv_credit: INITIAL_WINDOW,
queue: VecDeque::new(),
ungranted: 0,
eof_sent: false,
eof_received: false,
drained: false,
closed: false,
on_close,
}),
cv: Condvar::new(),
})
}
fn end(&self, reason: String) {
let (conn, on_close) = {
let mut st = self.state.lock().unwrap();
if st.closed {
return;
}
st.closed = true;
st.queue.clear();
(st.conn.clone(), st.on_close.take())
};
self.cv.notify_all();
if let Some(conn) = conn {
let _ = conn.shutdown(Shutdown::Both);
}
if let Some(f) = on_close {
f(reason);
}
}
}
pub struct ConnectReply {
mux: Arc<Mux>,
stream: u32,
flow: Option<Arc<Flow>>,
}
impl ConnectReply {
pub fn finish(mut self, result: Result<Conn, String>) {
let flow = self.flow.take().expect("finish runs once");
self.mux.complete_connect(self.stream, &flow, result);
}
}
impl Drop for ConnectReply {
fn drop(&mut self) {
if let Some(flow) = self.flow.take() {
self.mux.complete_connect(self.stream, &flow, Err("dial abandoned".into()));
}
}
}
struct Ctl {
queue: VecDeque<Frame>,
writer: bool,
}
pub struct Mux {
out: Mutex<Box<dyn Write + Send>>,
ctl: Mutex<Ctl>,
ctl_cv: Condvar,
streams: Mutex<HashMap<u32, Entry>>,
next_host: AtomicU32,
last_inbound: Mutex<Instant>,
ended: AtomicBool,
peer_caps: Mutex<Option<u32>>,
on_caps: Mutex<Option<Box<dyn FnMut(u32) + Send>>>,
}
impl Mux {
pub fn new(out: impl Write + Send + 'static) -> Arc<Mux> {
Arc::new(Mux {
out: Mutex::new(Box::new(out)),
ctl: Mutex::new(Ctl { queue: VecDeque::new(), writer: false }),
ctl_cv: Condvar::new(),
streams: Mutex::new(HashMap::new()),
next_host: AtomicU32::new(1),
last_inbound: Mutex::new(Instant::now()),
ended: AtomicBool::new(false),
peer_caps: Mutex::new(None),
on_caps: Mutex::new(None),
})
}
#[allow(dead_code)] pub fn peer_caps(&self) -> Option<u32> {
*self.peer_caps.lock().unwrap()
}
#[allow(dead_code)] pub fn on_caps(&self, f: impl FnMut(u32) + Send + 'static) {
*self.on_caps.lock().unwrap() = Some(Box::new(f));
}
pub fn keepalive(
self: &Arc<Self>,
interval: Duration,
timeout: Duration,
on_dead: impl FnOnce() + Send + 'static,
) {
let mux = Arc::clone(self);
std::thread::spawn(move || {
let mut on_dead = Some(on_dead);
while !mux.ended.load(Ordering::Relaxed) {
std::thread::sleep(interval);
if mux.ended.load(Ordering::Relaxed) {
return;
}
let idle = mux.last_inbound.lock().unwrap().elapsed();
if is_dead(idle, timeout) {
if let Some(f) = on_dead.take() {
f();
}
return;
}
if let Ok(mut out) = mux.out.try_lock() {
let _ = proto::write_frame(&mut *out, &Frame::Ping(Vec::new()));
}
}
});
}
pub fn send(&self, frame: &Frame) -> io::Result<()> {
proto::write_frame(&mut *self.out.lock().unwrap(), frame)
}
fn send_ctl(self: &Arc<Self>, frame: Frame) {
let mut ctl = self.ctl.lock().unwrap();
if matches!(frame, Frame::Pong(_)) && ctl.queue.contains(&frame) {
return;
}
ctl.queue.push_back(frame);
if !ctl.writer {
ctl.writer = true;
let mux = Arc::clone(self);
std::thread::spawn(move || mux.ctl_writer());
}
drop(ctl);
self.ctl_cv.notify_all();
}
fn ctl_writer(self: Arc<Self>) {
loop {
let frame = {
let mut ctl = self.ctl.lock().unwrap();
loop {
if let Some(frame) = ctl.queue.pop_front() {
break frame;
}
if self.ended.load(Ordering::Relaxed) {
ctl.writer = false;
return;
}
ctl = self.ctl_cv.wait(ctl).unwrap();
}
};
let _ = self.send(&frame);
}
}
pub fn attach(self: &Arc<Self>, stream: u32, conn: impl Into<Conn>, open: Option<u8>) -> io::Result<()> {
let conn = conn.into();
let reader = conn.try_clone()?;
self.streams.lock().unwrap().insert(stream, Entry::Legacy(Arc::new(conn)));
if let Some(channel) = open {
if let Err(e) = self.send(&Frame::Open { stream, channel }) {
self.streams.lock().unwrap().remove(&stream);
return Err(e);
}
}
let mux = Arc::clone(self);
spawn_stream_thread(move || mux.pump(stream, reader));
Ok(())
}
fn pump(&self, stream: u32, mut conn: Conn) {
let mut buf = vec![0u8; CHUNK];
loop {
match conn.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
if self.send(&Frame::Data { stream, bytes: buf[..n].to_vec() }).is_err() {
break;
}
}
}
}
if self.drop_stream(stream) {
let _ = self.send(&Frame::Close { stream, reason: String::new() });
}
}
fn drop_stream(&self, stream: u32) -> bool {
let entry = self.streams.lock().unwrap().remove(&stream);
match entry {
Some(entry) => {
entry.end(String::new());
true
}
None => false,
}
}
#[allow(dead_code)] pub fn connect(
self: &Arc<Self>,
conn: impl Into<Conn>,
host: &str,
port: u16,
on_close: impl FnOnce(String) + Send + 'static,
) -> io::Result<u32> {
validate_connect_target(host, port)?;
let conn = conn.into();
let reader = conn.try_clone()?;
let flow = Flow::new(None, Some(Box::new(on_close)));
let stream = {
let mut live = self.streams.lock().unwrap();
let mut next = self.next_host.load(Ordering::Relaxed);
let id = next_host_id(&mut next, |id| live.contains_key(&id));
self.next_host.store(next, Ordering::Relaxed);
live.insert(id, Entry::Flow(Arc::clone(&flow)));
id
};
if let Err(e) = self.send(&Frame::Connect { stream, host: host.to_string(), port }) {
self.unregister(stream, &flow);
return Err(e);
}
self.start_flow(stream, &flow, conn, reader);
Ok(stream)
}
fn complete_connect(self: &Arc<Self>, stream: u32, flow: &Arc<Flow>, result: Result<Conn, String>) {
match result.and_then(|conn| {
let reader = conn.try_clone().map_err(|e| format!("clone socket: {e}"))?;
Ok((conn, reader))
}) {
Ok((conn, reader)) => self.start_flow(stream, flow, conn, reader),
Err(reason) => self.close_flow(stream, flow, reason),
}
}
fn start_flow(self: &Arc<Self>, stream: u32, flow: &Arc<Flow>, conn: Conn, reader: Conn) {
let conn = Arc::new(conn);
{
let mut st = flow.state.lock().unwrap();
if st.closed {
return;
}
st.conn = Some(Arc::clone(&conn));
}
let (mux, f) = (Arc::clone(self), Arc::clone(flow));
spawn_stream_thread(move || mux.flow_writer(stream, f, conn));
let (mux, f) = (Arc::clone(self), Arc::clone(flow));
spawn_stream_thread(move || mux.flow_pump(stream, f, reader));
}
fn flow_pump(self: Arc<Self>, stream: u32, flow: Arc<Flow>, mut conn: Conn) {
let mut buf = vec![0u8; CHUNK];
loop {
let want = {
let mut st = flow.state.lock().unwrap();
loop {
if st.closed {
return;
}
if st.send_credit > 0 {
break (st.send_credit as usize).min(CHUNK);
}
st = flow.cv.wait(st).unwrap();
}
};
match conn.read(&mut buf[..want]) {
Ok(0) => {
if flow.state.lock().unwrap().closed || self.send(&Frame::Eof { stream }).is_err() {
return;
}
let ready = {
let mut st = flow.state.lock().unwrap();
st.eof_sent = true;
!st.closed && teardown_ready(st.eof_sent, st.eof_received, st.drained)
};
if ready {
self.finish_flow(stream, &flow);
}
return;
}
Ok(n) => {
{
let mut st = flow.state.lock().unwrap();
if st.closed {
return;
}
st.send_credit -= n as u32;
}
if self.send(&Frame::Data { stream, bytes: buf[..n].to_vec() }).is_err() {
return;
}
}
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => {
self.close_flow(stream, &flow, format!("read: {e}"));
return;
}
}
}
}
fn flow_writer(self: Arc<Self>, stream: u32, flow: Arc<Flow>, conn: Arc<Conn>) {
loop {
let next = {
let mut st = flow.state.lock().unwrap();
loop {
if st.closed {
return;
}
if let Some(bytes) = st.queue.pop_front() {
break Some(bytes);
}
if st.eof_received {
break None;
}
st = flow.cv.wait(st).unwrap();
}
};
let Some(bytes) = next else {
let _ = conn.shutdown(Shutdown::Write);
let ready = {
let mut st = flow.state.lock().unwrap();
st.drained = true;
!st.closed && teardown_ready(st.eof_sent, st.eof_received, st.drained)
};
if ready {
self.finish_flow(stream, &flow);
}
return;
};
if let Err(e) = (&*conn).write_all(&bytes) {
self.close_flow(stream, &flow, format!("write: {e}"));
return;
}
let grant = {
let mut st = flow.state.lock().unwrap();
if st.closed {
return;
}
st.ungranted += bytes.len() as u32;
(should_grant(st.ungranted) && !st.eof_received).then(|| {
let credit = std::mem::take(&mut st.ungranted);
st.recv_credit += credit;
credit
})
};
if let Some(credit) = grant {
let _ = self.send(&Frame::Window { stream, credit });
}
}
}
fn unregister(&self, stream: u32, flow: &Arc<Flow>) -> bool {
let mut live = self.streams.lock().unwrap();
match live.get(&stream) {
Some(Entry::Flow(f)) if Arc::ptr_eq(f, flow) => {
live.remove(&stream);
true
}
_ => false,
}
}
fn close_flow(self: &Arc<Self>, stream: u32, flow: &Arc<Flow>, reason: String) {
if self.unregister(stream, flow) {
flow.end(reason.clone());
self.send_ctl(Frame::Close { stream, reason });
}
}
fn finish_flow(&self, stream: u32, flow: &Arc<Flow>) {
if self.unregister(stream, flow) {
flow.end(String::new());
}
}
fn flow(&self, stream: u32) -> Option<Arc<Flow>> {
match self.streams.lock().unwrap().get(&stream) {
Some(Entry::Flow(f)) => Some(Arc::clone(f)),
_ => None,
}
}
#[allow(dead_code)] pub fn serve(self: &Arc<Self>, reader: impl Read, on_open: impl Fn(u32, u8) -> Option<Conn>) {
self.serve_with(reader, on_open, |_, _, _, reply: ConnectReply| {
reply.finish(Err("port forwarding not accepted by this peer".into()))
});
}
pub fn serve_with(
self: &Arc<Self>,
mut reader: impl Read,
on_open: impl Fn(u32, u8) -> Option<Conn>,
on_connect: impl Fn(u32, String, u16, ConnectReply),
) {
while let Ok(Some(frame)) = proto::read_frame(&mut reader) {
*self.last_inbound.lock().unwrap() = Instant::now();
match frame {
Frame::Data { stream, bytes } => {
let conn = match self.streams.lock().unwrap().get(&stream) {
Some(Entry::Legacy(conn)) => Ok(Arc::clone(conn)),
Some(Entry::Flow(flow)) => Err(Arc::clone(flow)),
None => continue,
};
match conn {
Ok(conn) => {
if (&*conn).write_all(&bytes).is_err() && self.drop_stream(stream) {
self.send_ctl(Frame::Close { stream, reason: String::new() });
}
}
Err(flow) => self.queue_data(stream, &flow, bytes),
}
}
Frame::Window { stream, credit } => {
if let Some(flow) = self.flow(stream) {
let mut st = flow.state.lock().unwrap();
st.send_credit = st.send_credit.saturating_add(credit);
drop(st);
flow.cv.notify_all();
}
}
Frame::Eof { stream } => {
let entry = match self.streams.lock().unwrap().get(&stream) {
Some(Entry::Legacy(conn)) => Ok(Arc::clone(conn)),
Some(Entry::Flow(flow)) => Err(Arc::clone(flow)),
None => continue,
};
match entry {
Ok(conn) => {
let _ = conn.shutdown(Shutdown::Write);
}
Err(flow) => {
flow.state.lock().unwrap().eof_received = true;
flow.cv.notify_all();
}
}
}
Frame::Close { stream, reason } => {
let entry = self.streams.lock().unwrap().remove(&stream);
if let Some(entry) = entry {
entry.end(reason);
}
}
Frame::Ping(payload) => self.send_ctl(Frame::Pong(payload)),
Frame::Open { stream, channel } => {
let attached = {
let live = self.streams.lock().unwrap();
let peer_streams = live.keys().filter(|&&id| id & HOST_ID_BIT == 0).count();
let allowed = stream & HOST_ID_BIT == 0
&& !live.contains_key(&stream)
&& peer_streams < MAX_STREAMS;
drop(live);
allowed
&& match on_open(stream, channel) {
Some(conn) => self.attach(stream, conn, None).is_ok(),
None => false,
}
};
if !attached {
self.send_ctl(Frame::Close { stream, reason: String::new() });
}
}
Frame::Connect { stream, host, port } => {
let flow = {
let mut live = self.streams.lock().unwrap();
let host_streams = live.keys().filter(|&&id| id & HOST_ID_BIT != 0).count();
connect_allowed(stream, live.contains_key(&stream), host_streams).then(|| {
let flow = Flow::new(None, None);
live.insert(stream, Entry::Flow(Arc::clone(&flow)));
flow
})
};
match flow {
Some(flow) => {
on_connect(stream, host, port, ConnectReply { mux: Arc::clone(self), stream, flow: Some(flow) })
}
None => self.send_ctl(Frame::Close { stream, reason: "stream refused".into() }),
}
}
Frame::Caps(c) => {
*self.peer_caps.lock().unwrap() = Some(c);
if let Some(f) = self.on_caps.lock().unwrap().as_mut() {
f(c);
}
}
Frame::Hello { .. } | Frame::Pong(_) | Frame::Quit => {}
}
}
self.ended.store(true, Ordering::Relaxed);
drop(self.ctl.lock().unwrap());
self.ctl_cv.notify_all();
self.close_all();
}
fn queue_data(self: &Arc<Self>, stream: u32, flow: &Arc<Flow>, bytes: Vec<u8>) {
let verdict = {
let mut st = flow.state.lock().unwrap();
if st.closed {
return;
}
let verdict = check_data(bytes.len(), st.recv_credit, st.eof_received);
if verdict.is_ok() {
st.recv_credit -= bytes.len() as u32;
st.queue.push_back(bytes);
}
verdict
};
match verdict {
Ok(()) => flow.cv.notify_all(),
Err(reason) => self.close_flow(stream, flow, reason.to_string()),
}
}
pub fn close_all(&self) {
let entries: Vec<Entry> = self.streams.lock().unwrap().drain().map(|(_, e)| e).collect();
for entry in entries {
entry.end("bridge closed".into());
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use proto::channel;
use std::io::{BufRead, BufReader};
use std::time::Duration;
fn echo_agent(conn: UnixStream) {
std::thread::spawn(move || {
let mut w = conn.try_clone().unwrap();
for line in BufReader::new(conn).lines() {
let Ok(line) = line else { break };
if writeln!(w, "re:{line}").is_err() {
break;
}
}
});
}
fn pair() -> Arc<Mux> {
let (d_r, h_w) = std::io::pipe().unwrap();
let (h_r, d_w) = std::io::pipe().unwrap();
let daemon = Mux::new(d_w);
let host = Mux::new(h_w);
let d = Arc::clone(&daemon);
std::thread::spawn(move || d.serve(d_r, |_, _| None));
std::thread::spawn(move || {
host.serve(h_r, |_, ch| {
(ch == channel::SSH_AGENT).then(|| {
let (ours, agent) = UnixStream::pair().unwrap();
echo_agent(agent);
ours.into()
})
})
});
daemon
}
fn client(daemon: &Arc<Mux>, stream: u32, channel: u8) -> BufReader<UnixStream> {
let (ours, theirs) = UnixStream::pair().unwrap();
daemon.attach(stream, theirs, Some(channel)).unwrap();
ours.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
BufReader::new(ours)
}
#[test]
fn streams_relay_independently() {
let daemon = pair();
let mut a = client(&daemon, 1, channel::SSH_AGENT);
let mut b = client(&daemon, 2, channel::SSH_AGENT);
let roundtrip = |c: &mut BufReader<UnixStream>, msg: &str| {
writeln!(c.get_mut(), "{msg}").unwrap();
let mut line = String::new();
c.read_line(&mut line).unwrap();
assert_eq!(line, format!("re:{msg}\n"));
};
roundtrip(&mut a, "one");
roundtrip(&mut b, "two");
roundtrip(&mut a, "three");
}
#[test]
fn tcp_conns_relay_through_the_mux() {
use std::net::{TcpListener, TcpStream};
let echo = TcpListener::bind("127.0.0.1:0").unwrap();
let echo_addr = echo.local_addr().unwrap();
std::thread::spawn(move || {
for conn in echo.incoming() {
let conn = conn.unwrap();
std::thread::spawn(move || {
let mut w = conn.try_clone().unwrap();
for line in BufReader::new(conn).lines() {
let Ok(line) = line else { break };
if writeln!(w, "re:{line}").is_err() {
break;
}
}
});
}
});
let (d_r, h_w) = std::io::pipe().unwrap();
let (h_r, d_w) = std::io::pipe().unwrap();
let daemon = Mux::new(d_w);
let host = Mux::new(h_w);
let d = Arc::clone(&daemon);
std::thread::spawn(move || d.serve(d_r, |_, _| None));
std::thread::spawn(move || host.serve(h_r, |_, _| TcpStream::connect(echo_addr).ok().map(Into::into)));
let front = TcpListener::bind("127.0.0.1:0").unwrap();
let ours = TcpStream::connect(front.local_addr().unwrap()).unwrap();
let (theirs, _) = front.accept().unwrap();
daemon.attach(1, theirs, Some(channel::SSH_AGENT)).unwrap();
ours.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
let mut c = BufReader::new(ours);
for msg in ["one", "two"] {
writeln!(c.get_mut(), "{msg}").unwrap();
let mut line = String::new();
c.read_line(&mut line).unwrap();
assert_eq!(line, format!("re:{msg}\n"));
}
}
#[test]
fn refused_open_closes_the_client() {
let daemon = pair();
let mut c = client(&daemon, 1, channel::HTTP_PROXY);
let mut buf = String::new();
assert_eq!(c.read_line(&mut buf).unwrap(), 0, "EOF after refusal");
assert!(daemon.streams.lock().unwrap().is_empty());
}
#[test]
fn client_close_is_forwarded_and_unregistered() {
let daemon = pair();
let c = client(&daemon, 1, channel::SSH_AGENT);
drop(c);
for _ in 0..100 {
if daemon.streams.lock().unwrap().is_empty() {
return;
}
std::thread::sleep(Duration::from_millis(10));
}
panic!("stream still registered after client closed");
}
#[test]
fn eof_half_closes_a_legacy_stream_and_the_reply_flows_back() {
let (d_r, h_w) = std::io::pipe().unwrap();
let (h_r, d_w) = std::io::pipe().unwrap();
let daemon = Mux::new(d_w);
let host = Mux::new(h_w);
let d = Arc::clone(&daemon);
std::thread::spawn(move || d.serve(d_r, |_, _| None));
std::thread::spawn(move || {
host.serve(h_r, |_, _| {
let (ours, mut handler) = UnixStream::pair().unwrap();
std::thread::spawn(move || {
let mut req = Vec::new();
handler.read_to_end(&mut req).unwrap();
handler.write_all(format!("got {}", req.len()).as_bytes()).unwrap();
});
Some(ours.into())
})
});
let (mut ours, theirs) = UnixStream::pair().unwrap();
ours.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
daemon.attach(1, theirs, Some(channel::NOTIFY)).unwrap();
daemon.send(&Frame::Data { stream: 1, bytes: b"hello".to_vec() }).unwrap();
daemon.send(&Frame::Eof { stream: 1 }).unwrap();
let mut reply = String::new();
ours.read_to_string(&mut reply).unwrap();
assert_eq!(reply, "got 5");
}
#[test]
fn peer_loss_shuts_every_stream() {
let (d_r, h_w) = std::io::pipe().unwrap();
let (_h_r, d_w) = std::io::pipe().unwrap();
let daemon = Mux::new(d_w);
let mut c = client(&daemon, 1, channel::SSH_AGENT);
drop(h_w); daemon.serve(d_r, |_, _| None);
let mut buf = String::new();
assert_eq!(c.read_line(&mut buf).unwrap(), 0);
}
fn wait_ctl_drained(mux: &Mux) {
let deadline = Instant::now() + Duration::from_secs(10);
loop {
let ctl = mux.ctl.lock().unwrap();
if ctl.queue.is_empty() && !ctl.writer {
return;
}
drop(ctl);
assert!(Instant::now() < deadline, "control writer never drained");
std::thread::sleep(Duration::from_millis(1));
}
}
#[derive(Clone)]
struct Sink(Arc<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn refused_opens(opens: &[u32]) -> Vec<u32> {
let mut wire = Vec::new();
for &stream in opens {
proto::write_frame(&mut wire, &Frame::Open { stream, channel: channel::SSH_AGENT }).unwrap();
}
let out = Arc::new(Mutex::new(Vec::new()));
let mux = Mux::new(Sink(Arc::clone(&out)));
let peers = Mutex::new(Vec::new());
mux.serve(io::Cursor::new(wire), |_, _| {
let (ours, theirs) = UnixStream::pair().unwrap();
peers.lock().unwrap().push(theirs);
Some(ours.into())
});
wait_ctl_drained(&mux);
let mut r = io::Cursor::new(std::mem::take(&mut *out.lock().unwrap()));
let mut closed = Vec::new();
while let Ok(Some(frame)) = proto::read_frame(&mut r) {
if let Frame::Close { stream, .. } = frame {
closed.push(stream);
}
}
closed
}
#[test]
fn is_dead_fires_only_after_the_timeout() {
let timeout = Duration::from_secs(45);
assert!(!is_dead(Duration::from_secs(0), timeout));
assert!(!is_dead(Duration::from_secs(44), timeout));
assert!(is_dead(Duration::from_secs(45), timeout));
assert!(is_dead(Duration::from_secs(90), timeout));
}
#[test]
fn keepalive_calls_on_dead_when_peer_stops_answering() {
let (mux_r, peer_w) = std::io::pipe().unwrap();
let (peer_r, mux_w) = std::io::pipe().unwrap();
let mux = Mux::new(mux_w);
std::thread::spawn(move || {
let mut sink = peer_r;
let mut buf = [0u8; 256];
while let Ok(n) = sink.read(&mut buf) {
if n == 0 {
break;
}
}
});
let (tx, rx) = std::sync::mpsc::channel();
mux.keepalive(Duration::from_millis(5), Duration::from_millis(30), move || {
let _ = tx.send(());
});
let m = Arc::clone(&mux);
std::thread::spawn(move || m.serve(mux_r, |_, _| None));
rx.recv_timeout(Duration::from_secs(2)).expect("on_dead fired");
drop(peer_w);
}
#[test]
fn keepalive_stops_once_serve_returns_without_calling_on_dead() {
let (mux_r, peer_w) = std::io::pipe().unwrap();
let (_peer_r, mux_w) = std::io::pipe().unwrap();
let mux = Mux::new(mux_w);
let fired = Arc::new(AtomicBool::new(false));
let f = Arc::clone(&fired);
mux.keepalive(Duration::from_millis(5), Duration::from_secs(30), move || {
f.store(true, Ordering::Relaxed);
});
drop(peer_w);
mux.serve(mux_r, |_, _| None);
std::thread::sleep(Duration::from_millis(40));
assert!(!fired.load(Ordering::Relaxed), "on_dead must not fire after serve ended");
}
#[test]
fn open_with_high_bit_is_refused() {
assert_eq!(refused_opens(&[HOST_ID_BIT, HOST_ID_BIT | 5]), vec![HOST_ID_BIT, HOST_ID_BIT | 5]);
}
#[test]
fn duplicate_open_id_is_refused() {
assert_eq!(refused_opens(&[1, 1]), vec![1]);
}
#[test]
fn opens_past_the_stream_cap_are_refused() {
let opens: Vec<u32> = (1..=(MAX_STREAMS as u32 + 2)).collect();
let over = vec![MAX_STREAMS as u32 + 1, MAX_STREAMS as u32 + 2];
assert_eq!(refused_opens(&opens), over);
}
use std::sync::mpsc;
#[test]
fn grant_rule_batches_to_half_a_window() {
assert!(!should_grant(0));
assert!(!should_grant(INITIAL_WINDOW / 2 - 1));
assert!(should_grant(INITIAL_WINDOW / 2));
assert!(should_grant(INITIAL_WINDOW));
}
#[test]
fn data_admission_enforces_credit_and_eof() {
assert_eq!(check_data(10, 10, false), Ok(()), "exactly the credit is fine");
assert_eq!(check_data(0, 0, false), Ok(()));
assert_eq!(check_data(11, 10, false), Err(FLOW_VIOLATION));
assert_eq!(check_data(1, 10, true), Err("data after eof"));
}
#[test]
fn teardown_needs_both_eofs_and_a_drained_queue() {
assert!(teardown_ready(true, true, true));
for (sent, received, drained) in [(false, true, true), (true, false, true), (true, true, false), (false, false, false)] {
assert!(!teardown_ready(sent, received, drained), "{sent} {received} {drained}");
}
}
#[test]
fn connect_id_rule() {
assert!(connect_allowed(HOST_ID_BIT | 1, false, 0));
assert!(!connect_allowed(1, false, 0), "daemon id space");
assert!(!connect_allowed(HOST_ID_BIT | 1, true, 0), "live id");
assert!(connect_allowed(HOST_ID_BIT | 1, false, MAX_HOST_STREAMS - 1));
assert!(!connect_allowed(HOST_ID_BIT | 1, false, MAX_HOST_STREAMS), "cap");
}
#[test]
fn host_ids_are_monotonic_skip_live_and_wrap() {
let mut next = 1;
assert_eq!(next_host_id(&mut next, |_| false), HOST_ID_BIT | 1);
assert_eq!(next_host_id(&mut next, |_| false), HOST_ID_BIT | 2);
assert_eq!(next_host_id(&mut next, |id| id == HOST_ID_BIT | 3), HOST_ID_BIT | 4);
assert_eq!(next_host_id(&mut next, |_| false), HOST_ID_BIT | 5);
let mut next = HOST_ID_BIT - 1;
assert_eq!(next_host_id(&mut next, |_| false), u32::MAX);
assert_eq!(next_host_id(&mut next, |_| false), HOST_ID_BIT | 1, "wraps to 1, never HOST_ID_BIT | 0");
}
#[test]
fn connect_rejects_bad_targets_before_the_wire() {
let out = Arc::new(Mutex::new(Vec::new()));
let mux = Mux::new(Sink(Arc::clone(&out)));
let long = "h".repeat(256);
for (host, port) in [("", 80), (long.as_str(), 80), ("db", 0)] {
let (a, _b) = UnixStream::pair().unwrap();
let err = mux.connect(a, host, port, |_| {}).unwrap_err();
assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{host:?}:{port}");
}
assert!(out.lock().unwrap().is_empty());
assert!(mux.streams.lock().unwrap().is_empty());
let (a, _b) = UnixStream::pair().unwrap();
mux.connect(a, &"h".repeat(255), 80, |_| {}).unwrap();
}
fn flow_pair_with(dial: impl Fn(&str, u16) -> Result<Conn, String> + Send + 'static) -> (Arc<Mux>, Arc<Mux>) {
let (d_r, h_w) = std::io::pipe().unwrap();
let (h_r, d_w) = std::io::pipe().unwrap();
let daemon = Mux::new(d_w);
let host = Mux::new(h_w);
let d = Arc::clone(&daemon);
std::thread::spawn(move || d.serve_with(d_r, |_, _| None, move |_, h, p, reply| reply.finish(dial(&h, p))));
let h = Arc::clone(&host);
std::thread::spawn(move || h.serve(h_r, |_, _| None));
(host, daemon)
}
struct Flows {
host: Arc<Mux>,
daemon: Arc<Mux>,
servers: mpsc::Receiver<UnixStream>,
}
fn flow_pair() -> Flows {
let (tx, servers) = mpsc::channel();
let (host, daemon) = flow_pair_with(move |_, _| {
let (ours, theirs) = UnixStream::pair().map_err(|e| e.to_string())?;
tx.send(theirs).unwrap();
Ok(ours.into())
});
Flows { host, daemon, servers }
}
fn open(f: &Flows) -> (UnixStream, UnixStream, u32, mpsc::Receiver<String>) {
let (client, theirs) = UnixStream::pair().unwrap();
let (tx, closed) = mpsc::channel();
let id = f
.host
.connect(theirs, "svc", 80, move |reason| {
let _ = tx.send(reason);
})
.unwrap();
let server = f.servers.recv_timeout(Duration::from_secs(10)).unwrap();
for s in [&client, &server] {
s.set_read_timeout(Some(Duration::from_secs(30))).unwrap();
s.set_write_timeout(Some(Duration::from_secs(30))).unwrap();
}
(client, server, id, closed)
}
fn wait_until(what: &str, mut cond: impl FnMut() -> bool) {
let deadline = Instant::now() + Duration::from_secs(10);
while !cond() {
assert!(Instant::now() < deadline, "timed out waiting for {what}");
std::thread::sleep(Duration::from_millis(5));
}
}
fn both_unregistered(f: &Flows) -> bool {
f.host.streams.lock().unwrap().is_empty() && f.daemon.streams.lock().unwrap().is_empty()
}
fn pat(i: usize) -> u8 {
(i ^ (i >> 8) ^ (i >> 17)).wrapping_mul(31) as u8
}
fn write_pattern(mut w: impl Write, n: usize) -> io::Result<()> {
let mut buf = vec![0u8; 64 * 1024];
let mut off = 0;
while off < n {
let len = buf.len().min(n - off);
for (j, b) in buf[..len].iter_mut().enumerate() {
*b = pat(off + j);
}
w.write_all(&buf[..len])?;
off += len;
}
Ok(())
}
fn read_check(mut r: impl Read) -> usize {
let mut buf = vec![0u8; 64 * 1024];
let mut off = 0;
loop {
let n = r.read(&mut buf).unwrap();
if n == 0 {
return off;
}
for (j, &b) in buf[..n].iter().enumerate() {
assert_eq!(b, pat(off + j), "byte {} differs", off + j);
}
off += n;
}
}
#[test]
fn bulk_transfer_is_byte_identical_both_ways() {
const N: usize = 64 << 20;
let f = flow_pair();
let (client, server, _, closed) = open(&f);
let (cw, sw) = (client.try_clone().unwrap(), server.try_clone().unwrap());
let up = std::thread::spawn(move || {
write_pattern(&cw, N).unwrap();
cw.shutdown(Shutdown::Write).unwrap();
});
let down = std::thread::spawn(move || {
write_pattern(&sw, N).unwrap();
sw.shutdown(Shutdown::Write).unwrap();
});
let received_up = std::thread::spawn(move || read_check(&server));
assert_eq!(read_check(&client), N);
assert_eq!(received_up.join().unwrap(), N);
up.join().unwrap();
down.join().unwrap();
assert_eq!(closed.recv_timeout(Duration::from_secs(10)).unwrap(), "");
wait_until("both sides to unregister", || both_unregistered(&f));
}
#[test]
fn bidirectional_bulk_with_pings_does_not_deadlock() {
const N: usize = 32 << 20;
const STREAMS: usize = 8;
let f = flow_pair();
let fired = Arc::new(AtomicBool::new(false));
for mux in [&f.host, &f.daemon] {
let fired = Arc::clone(&fired);
mux.keepalive(Duration::from_millis(1), Duration::from_secs(60), move || {
fired.store(true, Ordering::Relaxed);
});
}
let (done_tx, done) = mpsc::channel();
for _ in 0..STREAMS {
let (client, server, _, _) = open(&f);
let done_tx = done_tx.clone();
std::thread::spawn(move || {
let (cw, sw) = (client.try_clone().unwrap(), server.try_clone().unwrap());
std::thread::spawn(move || {
let _ = write_pattern(&cw, N).and_then(|_| cw.shutdown(Shutdown::Write));
});
std::thread::spawn(move || {
let _ = write_pattern(&sw, N).and_then(|_| sw.shutdown(Shutdown::Write));
});
let up = std::thread::spawn(move || read_check(&server));
let down = read_check(&client);
let _ = done_tx.send((up.join().unwrap(), down));
});
}
drop(done_tx);
let deadline = Instant::now() + Duration::from_secs(20);
for _ in 0..STREAMS {
let left = deadline.saturating_duration_since(Instant::now());
let got = done.recv_timeout(left).expect("bulk transfer wedged: mux deadlock");
assert_eq!(got, (N, N));
}
assert!(!fired.load(Ordering::Relaxed), "keepalive declared a live peer dead");
}
#[test]
fn half_close_still_delivers_the_response() {
let f = flow_pair();
let (mut client, mut server, _, closed) = open(&f);
client.write_all(b"GET /").unwrap();
client.shutdown(Shutdown::Write).unwrap();
let mut req = Vec::new();
server.read_to_end(&mut req).unwrap();
assert_eq!(req, b"GET /");
let response: Vec<u8> = (0..1 << 20).map(pat).collect();
let expected = response.clone();
std::thread::spawn(move || {
server.write_all(&response).unwrap();
});
let mut got = Vec::new();
client.read_to_end(&mut got).unwrap();
assert!(got == expected, "response differs ({} of {} bytes)", got.len(), expected.len());
assert_eq!(closed.recv_timeout(Duration::from_secs(10)).unwrap(), "");
wait_until("both sides to unregister", || both_unregistered(&f));
}
fn stalled(f: &Flows) -> (UnixStream, UnixStream, UnixStream) {
let (a_client, a_server, a_id, _) = open(f);
std::thread::spawn(move || {
let _ = write_pattern(&a_client, 64 << 20);
});
wait_until("A to exhaust its credit", || {
f.host.flow(a_id).is_some_and(|fl| fl.state.lock().unwrap().send_credit == 0)
});
let (b_client, b_server, _, _) = open(f);
(a_server, b_client, b_server)
}
fn echo(s: UnixStream) {
std::thread::spawn(move || {
let mut w = s.try_clone().unwrap();
let _ = io::copy(&mut &s, &mut w);
});
}
#[test]
fn stalled_stream_does_not_delay_others() {
let f = flow_pair();
let (_a_server, b_client, b_server) = stalled(&f);
echo(b_server);
let mut b = BufReader::new(b_client);
for i in 0..20 {
let t = Instant::now();
writeln!(b.get_mut(), "ping {i}").unwrap();
let mut line = String::new();
b.read_line(&mut line).unwrap();
assert_eq!(line, format!("ping {i}\n"));
assert!(t.elapsed() < Duration::from_secs(2), "B round-trip took {:?}", t.elapsed());
}
}
#[test]
fn keepalive_holds_while_a_stream_is_stalled() {
let f = flow_pair();
let fired = Arc::new(AtomicBool::new(false));
for mux in [&f.host, &f.daemon] {
let fired = Arc::clone(&fired);
mux.keepalive(Duration::from_millis(5), Duration::from_millis(300), move || {
fired.store(true, Ordering::Relaxed);
});
}
let _ends = stalled(&f);
std::thread::sleep(Duration::from_secs(1));
assert!(!fired.load(Ordering::Relaxed), "keepalive declared a live peer dead");
}
#[test]
fn dial_failure_reaches_the_host_with_its_reason() {
let (host, _daemon) = flow_pair_with(|_, _| Err("connection refused".into()));
let (mut client, theirs) = UnixStream::pair().unwrap();
client.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
let (tx, rx) = mpsc::channel();
host.connect(theirs, "db", 5432, move |reason| {
let _ = tx.send(reason);
})
.unwrap();
assert_eq!(rx.recv_timeout(Duration::from_secs(10)).unwrap(), "connection refused");
let mut buf = [0u8; 1];
assert_eq!(client.read(&mut buf).unwrap(), 0, "client sees EOF");
assert!(host.streams.lock().unwrap().is_empty());
}
#[test]
fn plain_serve_refuses_connect() {
let (d_r, h_w) = std::io::pipe().unwrap();
let (h_r, d_w) = std::io::pipe().unwrap();
let (a, b) = (Mux::new(d_w), Mux::new(h_w));
let a2 = Arc::clone(&a);
std::thread::spawn(move || a2.serve(d_r, |_, _| None));
std::thread::spawn(move || b.serve(h_r, |_, _| None));
let (_client, theirs) = UnixStream::pair().unwrap();
let (tx, rx) = mpsc::channel();
a.connect(theirs, "svc", 80, move |reason| {
let _ = tx.send(reason);
})
.unwrap();
assert_eq!(rx.recv_timeout(Duration::from_secs(10)).unwrap(), "port forwarding not accepted by this peer");
}
fn pending_daemon() -> (Arc<Mux>, Arc<Mutex<Vec<u8>>>, std::io::PipeWriter, mpsc::Receiver<ConnectReply>) {
let (r, w) = std::io::pipe().unwrap();
let out = Arc::new(Mutex::new(Vec::new()));
let mux = Mux::new(Sink(Arc::clone(&out)));
let (tx, replies) = mpsc::channel();
let m = Arc::clone(&mux);
std::thread::spawn(move || m.serve_with(r, |_, _| None, move |_, _, _, reply| tx.send(reply).unwrap()));
(mux, out, w, replies)
}
#[test]
fn data_racing_the_dial_is_queued_and_delivered() {
let (_mux, _out, mut w, replies) = pending_daemon();
let id = HOST_ID_BIT | 1;
proto::write_frame(&mut w, &Frame::Connect { stream: id, host: "svc".into(), port: 80 }).unwrap();
proto::write_frame(&mut w, &Frame::Data { stream: id, bytes: b"early".to_vec() }).unwrap();
proto::write_frame(&mut w, &Frame::Eof { stream: id }).unwrap();
let reply = replies.recv_timeout(Duration::from_secs(10)).unwrap();
let (ours, mut theirs) = UnixStream::pair().unwrap();
theirs.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
std::thread::sleep(Duration::from_millis(50));
reply.finish(Ok(ours.into()));
let mut got = Vec::new();
theirs.read_to_end(&mut got).unwrap();
assert_eq!(got, b"early");
}
#[test]
fn close_while_dialing_drops_the_late_socket() {
let (mux, out, mut w, replies) = pending_daemon();
let id = HOST_ID_BIT | 1;
proto::write_frame(&mut w, &Frame::Connect { stream: id, host: "svc".into(), port: 80 }).unwrap();
let reply = replies.recv_timeout(Duration::from_secs(10)).unwrap();
proto::write_frame(&mut w, &Frame::Data { stream: id, bytes: b"early".to_vec() }).unwrap();
proto::write_frame(&mut w, &Frame::Close { stream: id, reason: String::new() }).unwrap();
wait_until("the pending stream to unregister", || mux.streams.lock().unwrap().is_empty());
let (ours, mut theirs) = UnixStream::pair().unwrap();
theirs.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
reply.finish(Ok(ours.into()));
let mut got = Vec::new();
assert_eq!(theirs.read_to_end(&mut got).unwrap(), 0, "late socket dropped, nothing delivered");
assert!(mux.streams.lock().unwrap().is_empty());
assert!(out.lock().unwrap().is_empty(), "no Close echoed, nothing sent");
}
fn daemon_frames(wire: Vec<u8>, dial: bool) -> Vec<Frame> {
let out = Arc::new(Mutex::new(Vec::new()));
let mux = Mux::new(Sink(Arc::clone(&out)));
let peers = Mutex::new(Vec::new());
let pending = Mutex::new(Vec::new());
mux.serve_with(io::Cursor::new(wire), |_, _| None, |_, _, _, reply| {
if dial {
let (ours, theirs) = UnixStream::pair().unwrap();
peers.lock().unwrap().push(theirs);
reply.finish(Ok(ours.into()));
} else {
pending.lock().unwrap().push(reply);
}
});
wait_ctl_drained(&mux);
let mut r = io::Cursor::new(std::mem::take(&mut *out.lock().unwrap()));
let mut frames = Vec::new();
while let Ok(Some(frame)) = proto::read_frame(&mut r) {
frames.push(frame);
}
frames
}
fn closes(frames: &[Frame]) -> Vec<(u32, String)> {
frames
.iter()
.filter_map(|f| match f {
Frame::Close { stream, reason } => Some((*stream, reason.clone())),
_ => None,
})
.collect()
}
#[test]
fn credit_overrun_closes_only_that_stream() {
let (a, b) = (HOST_ID_BIT | 1, HOST_ID_BIT | 2);
let mut wire = Vec::new();
for frame in [
Frame::Connect { stream: a, host: "svc".into(), port: 80 },
Frame::Connect { stream: b, host: "svc".into(), port: 80 },
Frame::Data { stream: a, bytes: vec![0; INITIAL_WINDOW as usize + 1] },
Frame::Data { stream: b, bytes: vec![0; 10] },
Frame::Ping(b"alive".to_vec()),
] {
proto::write_frame(&mut wire, &frame).unwrap();
}
let frames = daemon_frames(wire, true);
assert_eq!(closes(&frames), [(a, FLOW_VIOLATION.to_string())]);
assert!(frames.contains(&Frame::Pong(b"alive".to_vec())), "connection stays up");
}
fn refused_connects(ids: &[u32]) -> Vec<u32> {
let mut wire = Vec::new();
for &stream in ids {
proto::write_frame(&mut wire, &Frame::Connect { stream, host: "svc".into(), port: 80 }).unwrap();
}
let frames = daemon_frames(wire, false);
let closes = closes(&frames);
assert!(closes.iter().all(|(_, r)| r == "stream refused"), "{closes:?}");
closes.into_iter().map(|(s, _)| s).collect()
}
#[test]
fn connect_id_policy() {
let h = |n: u32| HOST_ID_BIT | n;
assert_eq!(refused_connects(&[5, h(1), h(1)]), [5, h(1)]);
let ids: Vec<u32> = (1..=MAX_HOST_STREAMS as u32 + 2).map(h).collect();
assert_eq!(refused_connects(&ids), [h(MAX_HOST_STREAMS as u32 + 1), h(MAX_HOST_STREAMS as u32 + 2)]);
}
}