Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions async/src/async_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl ChanIO<'_> {
poll_fn(|cx| self.sunset.poll_term_window_change(cx, self.num, &winch)).await
}

pub async fn send_eof(&self) -> Result<()> {
poll_fn(|cx| self.sunset.poll_send_eof(cx, self.num)).await
}

pub async fn close_channel(&self) -> Result<()> {
poll_fn(|cx| self.sunset.poll_close_channel(cx, self.num)).await
}
Expand Down Expand Up @@ -200,6 +204,18 @@ impl<'g> ChanOut<'g> {
self.0.term_window_change(winch).await
}

/// Send EOF, leaving the channel open for reading.
///
/// Further non-empty writes (including stderr) fail with
/// `sunset::Error::ChannelEOF`.
/// Repeated calls have no effect.
///
/// Do not overlap with writes or channel-control operations on the
/// same channel, as this shares the normal write waker.
pub async fn send_eof(&self) -> Result<()> {
self.0.send_eof().await
}

/// Close the channel.
///
/// Sends EOF and Close, further writes will fail.
Expand Down Expand Up @@ -277,6 +293,18 @@ impl<'g> ChanInOut<'g> {
self.0.term_window_change(winch).await
}

/// Send EOF, leaving the channel open for reading.
///
/// Further non-empty writes (including stderr) fail with
/// `sunset::Error::ChannelEOF`.
/// Repeated calls have no effect.
///
/// Do not overlap with writes or channel-control operations on the
/// same channel, as this shares the normal write waker.
pub async fn send_eof(&self) -> Result<()> {
self.0.send_eof().await
}

/// Close the channel.
///
/// Sends EOF and Close, further writes will fail.
Expand Down
23 changes: 23 additions & 0 deletions async/src/async_sunset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ pub(crate) trait ChanCore: MaybeSend {
buf: &[u8],
) -> Poll<Result<usize>>;

fn poll_send_eof(&self, cx: &mut Context, num: ChanNum) -> Poll<Result<()>>;

// Client only
fn poll_term_window_change(
&self,
Expand Down Expand Up @@ -671,6 +673,27 @@ impl<'a, CS: CliServ> ChanCore for AsyncSunset<'a, CS> {
Poll::Ready(runner.term_window_change(h, winch))
}

fn poll_send_eof(&self, cx: &mut Context, num: ChanNum) -> Poll<Result<()>> {
// Attempt to lock .inner
let i = self.inner.lock();
let i = pin!(i);
let Ready(mut inner) = i.poll(cx) else {
return Pending;
};
let (runner, h) = inner.fetch(num)?;
match runner.channel_send_eof(h) {
Err(sunset::Error::BusySend { .. }) => {
// Shares the normal write waker, like poll_close_channel().
runner.set_channel_write_waker(h, Normal, cx.waker());
Poll::Pending
}
r => {
self.wake_progress();
Poll::Ready(r)
}
}
}

fn poll_close_channel(
&self,
cx: &mut Context,
Expand Down
41 changes: 41 additions & 0 deletions async/tests/channel_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,44 @@ async fn close_channel_with_handle() {
};
run(&server, &client, shells, authed, opened, body).await;
}

/// EOF only ends the sending direction; a reply can still be received.
#[tokio::test]
async fn reply_after_peer_eof() {
let server = SSHServer::new_owned();
let client = SSHClient::new_owned();
let (shells, mut shell) = unbounded_channel();
let (authed, mut authed_rx) = unbounded_channel();
let (opened, mut opened_rx) = unbounded_channel();
let (ready, ready_rx) = tokio::sync::oneshot::channel();

let server_session = async {
let (mut io, mut ext) =
server.stdio_stderr(shell.recv().await.unwrap()).await.unwrap();
ready.send(()).unwrap();
assert_eq!(read_to_eof(&mut io).await, b"request");
io.write_all(b"reply").await.unwrap();
ext.send_eof().await.unwrap();
assert!(matches!(io.write(b"more").await, Err(sunset::Error::ChannelEOF)));
assert!(matches!(ext.write(b"more").await, Err(sunset::Error::ChannelEOF)));
// Dropping the handles still closes the channel.
};

let client_session = async {
authed_rx.recv().await.unwrap();
let (mut io, _ext) = client.open_session_nopty().await.unwrap();
opened_rx.recv().await.unwrap();
ready_rx.await.unwrap();
io.write_all(b"request").await.unwrap();
io.send_eof().await.unwrap();
io.send_eof().await.unwrap();
assert!(matches!(io.write(b"more").await, Err(sunset::Error::ChannelEOF)));
assert_eq!(read_to_eof(&mut io).await, b"reply");
io.until_closed().await.unwrap();
};

let body = async {
tokio::join!(server_session, client_session);
};
run(&server, &client, shells, authed, opened, body).await;
}
44 changes: 27 additions & 17 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ impl Channels {
self.get_mut(num)?.send_close(s)
}

/// Send EOF if not sent yet. The channel stays open for reading.
pub fn send_eof(&mut self, num: ChanNum, s: &mut TrafSend) -> Result<()> {
self.get_mut(num)?.send_eof(s)
}

fn remove_any(&mut self, num: ChanNum) -> Result<()> {
trace!("remove_any channel {}", num);
self.ch[num.0 as usize] = None;
Expand Down Expand Up @@ -505,7 +510,7 @@ impl Channels {
}
Packet::ChannelEof(p) => {
let ch = self.get_mut(ChanNum(p.num))?;
ch.handle_eof(s, is_client)?;
ch.handle_eof(is_client);
}
Packet::ChannelClose(p) => {
let is_client = self.is_client;
Expand Down Expand Up @@ -1042,22 +1047,16 @@ impl Channel {
}
}

fn handle_eof(&mut self, s: &mut TrafSend, is_client: bool) -> Result<()> {
//TODO: check existing state?
if !self.sent_eof {
s.send(packets::ChannelEof { num: self.send_num()? })?;
self.sent_eof = true;
}
fn handle_eof(&mut self, is_client: bool) {
// EOF only ends the peer's sending direction (RFC 4254 5.3).

self.state = ChanState::RecvEof;

// Wake readers on EOF
self.wake_read(ChanData::Normal, is_client);
if is_client {
self.wake_read(ChanData::Stderr, is_client);
}

self.state = ChanState::RecvEof;
// todo!();
Ok(())
}

fn handle_close(&mut self, s: &mut TrafSend, is_client: bool) -> Result<()> {
Expand Down Expand Up @@ -1090,16 +1089,30 @@ impl Channel {
matches!(self.state, ChanState::RecvClose)
}

// None on close
// None after EOF or close was sent
fn send_allowed(&self) -> Option<usize> {
if self.sent_close {
if self.sent_eof || self.sent_close {
return None;
}
let r = self.send.as_ref().map(|s| usize::min(s.window, s.max_packet));
trace!("send_allowed {r:?}");
r
}

/// Send EOF if required.
fn send_eof(&mut self, s: &mut TrafSend) -> Result<()> {
if self.sent_eof || self.sent_close {
return Ok(());
}
let Ok(num) = self.send_num() else {
// not open yet
return Ok(());
};
s.send(packets::ChannelEof { num })?;
self.sent_eof = true;
Ok(())
}

/// Send EOF and Close if required.
fn send_close(&mut self, s: &mut TrafSend) -> Result<()> {
if self.sent_close {
Expand All @@ -1110,10 +1123,7 @@ impl Channel {
return Ok(());
};

if !self.sent_eof {
s.send(packets::ChannelEof { num })?;
self.sent_eof = true;
}
self.send_eof(s)?;
s.send(packets::ChannelClose { num })?;
self.sent_close = true;
Ok(())
Expand Down
20 changes: 17 additions & 3 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> {

/// Send data from this application out the wire.
///
/// Returns `Ok(len)` consumed, `Err(Error::ChannelEof)` on EOF,
/// Returns `Ok(len)` consumed, `Err(Error::ChannelEOF)` on EOF,
/// or other errors.
pub fn write_channel(
&mut self,
Expand Down Expand Up @@ -540,7 +540,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> {

/// Receive data coming from the wire into this application.
///
/// Returns `Ok(len)` received, `Err(Error::ChannelEof)` on EOF,
/// Returns `Ok(len)` received, `Err(Error::ChannelEOF)` on EOF,
/// or other errors. Ok(0) indicates no data available, ie pending.
/// TODO: EOF is unimplemented
pub fn read_channel(
Expand Down Expand Up @@ -621,7 +621,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> {

/// Returns the maximum data that may be sent to a channel
///
/// Returns `Ok(None)` on channel closed.
/// Returns `Ok(None)` when no more data can be sent, including after EOF.
///
/// May fail with `BadChannelData` if dt is invalid for this session.
pub fn write_channel_ready(
Expand Down Expand Up @@ -694,6 +694,20 @@ impl<'a, CS: CliServ> Runner<'a, CS> {
Ok(())
}

/// Send EOF, leaving the channel open for reading.
///
/// Further non-empty writes (including stderr) fail with `Error::ChannelEOF`.
/// Does nothing if EOF or Close was already sent.
///
/// Independent of the channel send window. On `BusySend`, retry after
/// transport progress.
pub fn channel_send_eof(&mut self, chan: &ChanHandle) -> Result<()> {
let mut s = self.traf_out.sender(&mut self.keys);
self.conn.channels.send_eof(chan.0, &mut s)?;
self.wake();
Ok(())
}

/// Must be called when an application has finished with a channel.
///
/// Closes the channel if it hasn't been already.
Expand Down