Skip to content

Commit

Permalink
Fix min vs max for Channel send_allowed()
Browse files Browse the repository at this point in the history
Also various extra tracing
  • Loading branch information
mkj committed Jun 18, 2023
1 parent 3a5995d commit 0d64eb7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions async/examples/sunsetc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn main() {

async fn run(args: Args) -> Result<()> {

trace!("tracing main");
debug!("verbose main");
trace!("tracing sunsetc. args {:?}", args);
debug!("verbose sunsetc");

if !args.cmd.is_empty() && args.subsystem.is_some() {
bail!("can't have '-s subsystem' with a command")
Expand Down Expand Up @@ -144,7 +144,7 @@ async fn run(args: Args) -> Result<()> {
}
}

#[derive(argh::FromArgs)]
#[derive(argh::FromArgs, Debug)]
/** Sunset SSH Client
*/
struct Args {
Expand Down
8 changes: 7 additions & 1 deletion src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ impl<C: CliBehaviour, S: ServBehaviour> Channels<C, S> {
let ch = self.get_mut(num)?;
let send = ch.send.as_mut().trap()?;
if data.len() > send.max_packet || data.len() > send.window {
trace!("data len {}, max {}, window {}",
data.len(), send.max_packet, send.window);
return Err(Error::bug());
}
send.window -= data.len();
trace!("send_data: new window {}", send.window);

let data = BinString(data);
let p = match dt {
Expand Down Expand Up @@ -357,6 +360,7 @@ impl<C: CliBehaviour, S: ServBehaviour> Channels<C, S> {
Packet::ChannelWindowAdjust(p) => {
let send = self.get_mut(ChanNum(p.num))?.send.as_mut().trap()?;
send.window = send.window.saturating_add(p.adjust as usize);
trace!("new window {}", send.window);
}
Packet::ChannelData(p) => {
self.get(ChanNum(p.num))?;
Expand Down Expand Up @@ -801,7 +805,9 @@ impl Channel {

// None on close
fn send_allowed(&self) -> Option<usize> {
self.send.as_ref().map(|s| usize::max(s.window, s.max_packet))
let r = self.send.as_ref().map(|s| usize::min(s.window, s.max_packet));
trace!("send_allowed {r:?}");
r
}

pub(crate) fn valid_send(&self, _dt: ChanData) -> bool {
Expand Down
4 changes: 3 additions & 1 deletion src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ impl<'a, C: CliBehaviour, S: ServBehaviour> Runner<'a, C, S> {
let payload_space = self.traf_out.send_allowed(&self.keys);
// subtract space for packet headers prior to data
let payload_space = payload_space.saturating_sub(dt.packet_offset());
Ok(self.conn.channels.send_allowed(chan.0).map(|s| s.min(payload_space)))
let r = Ok(self.conn.channels.send_allowed(chan.0).map(|s| s.min(payload_space)));
trace!("ready_channel_send {chan:?} -> {r:?}");
r
}

/// Returns `true` if the channel and `dt` are currently valid for writing.
Expand Down
6 changes: 4 additions & 2 deletions src/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,16 @@ impl<'a> TrafOut<'a> {
/// Returns payload space available to send a packet. Returns 0 if not ready or full
pub fn send_allowed(&self, keys: &KeyState) -> usize {
// TODO: test for full output buffer
match self.state {
let r = match self.state {
TxState::Write { len, .. } => {
keys.max_enc_payload(self.buf.len() - len)
}
TxState::Idle => {
keys.max_enc_payload(self.buf.len())
}
}
};
trace!("traf send_allowed -> {}", r);
r
}

pub fn send_version(&mut self) -> Result<(), Error> {
Expand Down

0 comments on commit 0d64eb7

Please sign in to comment.