Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tcp-ip-forward global request responses. #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions russh/src/client/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,20 @@ impl Session {
}
Ok((client, self))
}
Some(&msg::REQUEST_SUCCESS) => {
let mut r = buf.reader(1);
let port_num = r.read_u32().map_err(crate::Error::from)?;
self.sender
.send(Reply::GlobalRequestSuccess(port_num))
.map_err(|_| crate::Error::SendError)?;
Ok((client, self))
}
Some(&msg::REQUEST_FAILURE) => {
self.sender
.send(Reply::GlobalRequestFailure)
.map_err(|_| crate::Error::SendError)?;
Ok((client, self))
}
Some(&msg::CHANNEL_SUCCESS) => {
let mut r = buf.reader(1);
let channel_num = ChannelId(r.read_u32().map_err(crate::Error::from)?);
Expand Down
21 changes: 17 additions & 4 deletions russh/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ impl Drop for Session {
enum Reply {
AuthSuccess,
AuthFailure,
// TODO GlobalRequestSuccess its data depends on the type of request send. for now only support an u32 because thats what's needed for tcp-ip-forward.
GlobalRequestSuccess(u32),
GlobalRequestFailure,
ChannelOpenFailure,
SignRequest {
key: key::PublicKey,
Expand Down Expand Up @@ -424,19 +427,29 @@ impl<H: Handler> Handle<H> {
&mut self,
address: A,
port: u32,
) -> Result<bool, crate::Error> {
) -> Result<u32, crate::Error> {
self.sender
.send(Msg::TcpIpForward {
want_reply: true,
want_reply: port == 0,
address: address.into(),
port,
})
.await
.map_err(|_| crate::Error::SendError)?;
if port == 0 {
self.wait_recv_reply().await?;
loop {
match self.receiver.recv().await {
Some(Reply::GlobalRequestSuccess(port)) => return Ok(port),
Some(Reply::GlobalRequestFailure) => {
return Err(crate::Error::GlobalRequestFailure)
}
None => return Err(crate::Error::GlobalRequestFailure),
_ => {}
}
}
} else {
Ok(port)
}
Ok(true)
}

pub async fn cancel_tcpip_forward<A: Into<String>>(
Expand Down
3 changes: 3 additions & 0 deletions russh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ pub enum Error {
#[error("Failed to decrypt a packet")]
DecryptionError,

#[error("Global request failed")]
GlobalRequestFailure,

#[error(transparent)]
Keys(#[from] russh_keys::Error),

Expand Down