Skip to content

feat: Add Send bounds to AsyncNetworkClient (fixes #542) #757

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

Open
wants to merge 3 commits 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
8 changes: 4 additions & 4 deletions crates/ironrdp-acceptor/src/channel_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ impl State for ChannelConnectionState {
}

impl Sequence for ChannelConnectionSequence {
fn next_pdu_hint(&self) -> Option<&dyn pdu::PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn pdu::PduHint>> {
match &self.state {
ChannelConnectionState::Consumed => None,
ChannelConnectionState::WaitErectDomainRequest => Some(&pdu::X224_HINT),
ChannelConnectionState::WaitAttachUserRequest => Some(&pdu::X224_HINT),
ChannelConnectionState::WaitErectDomainRequest => Some(Box::new(pdu::X224_HINT)),
ChannelConnectionState::WaitAttachUserRequest => Some(Box::new(pdu::X224_HINT)),
ChannelConnectionState::SendAttachUserConfirm => None,
ChannelConnectionState::WaitChannelJoinRequest { .. } => Some(&pdu::X224_HINT),
ChannelConnectionState::WaitChannelJoinRequest { .. } => Some(Box::new(pdu::X224_HINT)),
ChannelConnectionState::SendChannelJoinConfirm { .. } => None,
ChannelConnectionState::AllJoined => None,
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ironrdp-acceptor/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,22 +262,22 @@ impl State for AcceptorState {
}

impl Sequence for Acceptor {
fn next_pdu_hint(&self) -> Option<&dyn pdu::PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn pdu::PduHint>> {
match &self.state {
AcceptorState::Consumed => None,
AcceptorState::InitiationWaitRequest => Some(&pdu::X224_HINT),
AcceptorState::InitiationWaitRequest => Some(Box::new(pdu::X224_HINT)),
AcceptorState::InitiationSendConfirm { .. } => None,
AcceptorState::SecurityUpgrade { .. } => None,
AcceptorState::Credssp { .. } => None,
AcceptorState::BasicSettingsWaitInitial { .. } => Some(&pdu::X224_HINT),
AcceptorState::BasicSettingsWaitInitial { .. } => Some(Box::new(pdu::X224_HINT)),
AcceptorState::BasicSettingsSendResponse { .. } => None,
AcceptorState::ChannelConnection { connection, .. } => connection.next_pdu_hint(),
AcceptorState::RdpSecurityCommencement { .. } => None,
AcceptorState::SecureSettingsExchange { .. } => Some(&pdu::X224_HINT),
AcceptorState::SecureSettingsExchange { .. } => Some(Box::new(pdu::X224_HINT)),
AcceptorState::LicensingExchange { .. } => None,
AcceptorState::CapabilitiesSendServer { .. } => None,
AcceptorState::MonitorLayoutSend { .. } => None,
AcceptorState::CapabilitiesWaitConfirm { .. } => Some(&pdu::X224_HINT),
AcceptorState::CapabilitiesWaitConfirm { .. } => Some(Box::new(pdu::X224_HINT)),
AcceptorState::ConnectionFinalization { finalization, .. } => finalization.next_pdu_hint(),
AcceptorState::Accepted { .. } => None,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ironrdp-acceptor/src/credssp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl CredentialsProxy for CredentialsProxyImpl<'_> {
}

impl<'a> CredsspSequence<'a> {
pub(crate) fn next_pdu_hint(&self) -> ConnectorResult<Option<&dyn PduHint>> {
pub(crate) fn next_pdu_hint(&self) -> ConnectorResult<Option<Box<dyn PduHint>>> {
match &self.state {
CredsspState::Ongoing => Ok(Some(&CREDSSP_TS_REQUEST_HINT)),
CredsspState::Ongoing => Ok(Some(Box::new(CREDSSP_TS_REQUEST_HINT))),
CredsspState::Finished => Ok(None),
CredsspState::ServerError(err) => Err(custom_err!("Credssp server error", err.clone())),
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ironrdp-acceptor/src/finalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl State for FinalizationState {
}

impl Sequence for FinalizationSequence {
fn next_pdu_hint(&self) -> Option<&dyn pdu::PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn pdu::PduHint>> {
match &self.state {
FinalizationState::Consumed => None,
FinalizationState::WaitSynchronize => Some(&pdu::X224Hint),
FinalizationState::WaitControlCooperate => Some(&pdu::X224Hint),
FinalizationState::WaitRequestControl => Some(&pdu::X224Hint),
FinalizationState::WaitFontList => Some(&pdu::RdpHint),
FinalizationState::WaitSynchronize => Some(Box::new(pdu::X224Hint)),
FinalizationState::WaitControlCooperate => Some(Box::new(pdu::X224Hint)),
FinalizationState::WaitRequestControl => Some(Box::new(pdu::X224Hint)),
FinalizationState::WaitFontList => Some(Box::new(pdu::RdpHint)),
FinalizationState::SendSynchronizeConfirm => None,
FinalizationState::SendControlCooperateConfirm => None,
FinalizationState::SendGrantedControlConfirm => None,
Expand Down
147 changes: 146 additions & 1 deletion crates/ironrdp-async/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ironrdp_connector::{
use ironrdp_core::WriteBuf;

use crate::framed::{Framed, FramedRead, FramedWrite};
use crate::{single_sequence_step, AsyncNetworkClient};
use crate::{single_sequence_step, AsyncNetworkClient, WasmAsyncNetworkClient};

#[non_exhaustive]
pub struct ShouldUpgrade;
Expand Down Expand Up @@ -85,6 +85,47 @@ where
Ok(result)
}

#[instrument(skip_all)]
pub async fn wasm_connect_finalize<S>(
_: Upgraded,
framed: &mut Framed<S>,
mut connector: ClientConnector,
server_name: ServerName,
server_public_key: Vec<u8>,
network_client: Option<&mut dyn WasmAsyncNetworkClient>,
kerberos_config: Option<KerberosConfig>,
) -> ConnectorResult<ConnectionResult>
where
S: FramedRead + FramedWrite,
{
let mut buf = WriteBuf::new();

if connector.should_perform_credssp() {
wasm_perform_credssp_step(
framed,
&mut connector,
&mut buf,
server_name,
server_public_key,
network_client,
kerberos_config,
)
.await?;
}

let result = loop {
single_sequence_step(framed, &mut connector, &mut buf).await?;

if let ClientConnectorState::Connected { result } = connector.state {
break result;
}
};

info!("Connected with success");

Ok(result)
}

async fn resolve_generator(
generator: &mut CredsspProcessGenerator<'_>,
network_client: &mut dyn AsyncNetworkClient,
Expand All @@ -105,6 +146,26 @@ async fn resolve_generator(
}
}

async fn wasm_resolve_generator(
generator: &mut CredsspProcessGenerator<'_>,
network_client: &mut dyn WasmAsyncNetworkClient,
) -> ConnectorResult<ClientState> {
let mut state = generator.start();

loop {
match state {
GeneratorState::Suspended(request) => {
let response = network_client.send(&request).await?;
state = generator.resume(Ok(response));
}
GeneratorState::Completed(client_state) => {
break client_state
.map_err(|e| ConnectorError::new("CredSSP", ironrdp_connector::ConnectorErrorKind::Credssp(e)))
}
}
}
}

#[instrument(level = "trace", skip_all)]
async fn perform_credssp_step<S>(
framed: &mut Framed<S>,
Expand Down Expand Up @@ -188,3 +249,87 @@ where

Ok(())
}

#[instrument(level = "trace", skip_all)]
async fn wasm_perform_credssp_step<S>(
framed: &mut Framed<S>,
connector: &mut ClientConnector,
buf: &mut WriteBuf,
server_name: ServerName,
server_public_key: Vec<u8>,
mut network_client: Option<&mut dyn WasmAsyncNetworkClient>,
kerberos_config: Option<KerberosConfig>,
) -> ConnectorResult<()>
where
S: FramedRead + FramedWrite,
{
assert!(connector.should_perform_credssp());

let selected_protocol = match connector.state {
ClientConnectorState::Credssp { selected_protocol, .. } => selected_protocol,
_ => return Err(general_err!("invalid connector state for CredSSP sequence")),
};

let (mut sequence, mut ts_request) = CredsspSequence::init(
connector.config.credentials.clone(),
connector.config.domain.as_deref(),
selected_protocol,
server_name,
server_public_key,
kerberos_config,
)?;

loop {
let client_state = {
let mut generator = sequence.process_ts_request(ts_request);

if let Some(network_client_ref) = network_client.as_deref_mut() {
trace!("resolving network");
wasm_resolve_generator(&mut generator, network_client_ref).await?
} else {
generator
.resolve_to_result()
.map_err(|e| custom_err!("resolve without network client", e))?
}
}; // drop generator

buf.clear();
let written = sequence.handle_process_result(client_state, buf)?;

if let Some(response_len) = written.size() {
let response = &buf[..response_len];
trace!(response_len, "Send response");
framed
.write_all(response)
.await
.map_err(|e| ironrdp_connector::custom_err!("write all", e))?;
}

let Some(next_pdu_hint) = sequence.next_pdu_hint() else {
break;
};

debug!(
connector.state = connector.state.name(),
hint = ?next_pdu_hint,
"Wait for PDU"
);

let pdu = framed
.read_by_hint(next_pdu_hint)
.await
.map_err(|e| ironrdp_connector::custom_err!("read frame by hint", e))?;

trace!(length = pdu.len(), "PDU received");

if let Some(next_request) = sequence.decode_server_message(&pdu)? {
ts_request = next_request;
} else {
break;
}
}

connector.mark_credssp_as_done();

Ok(())
}
2 changes: 1 addition & 1 deletion crates/ironrdp-async/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
/// `tokio::select!` statement and some other branch
/// completes first, then it is safe to drop the future and re-create it later.
/// Data may have been read, but it will be stored in the internal buffer.
pub async fn read_by_hint(&mut self, hint: &dyn PduHint) -> io::Result<Bytes> {
pub async fn read_by_hint(&mut self, hint: Box<dyn PduHint>) -> io::Result<Bytes> {
loop {
match hint
.find_size(self.peek())
Expand Down
9 changes: 8 additions & 1 deletion crates/ironrdp-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ pub use self::connector::*;
pub use self::framed::*;
// pub use self::session::*;

pub trait AsyncNetworkClient {
pub trait AsyncNetworkClient: Send {
fn send<'a>(
&'a mut self,
network_request: &'a NetworkRequest,
) -> Pin<Box<dyn Future<Output = ConnectorResult<Vec<u8>>> + Send + 'a>>;
}

pub trait WasmAsyncNetworkClient {
fn send<'a>(
&'a mut self,
network_request: &'a NetworkRequest,
Expand Down
2 changes: 1 addition & 1 deletion crates/ironrdp-blocking/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
}

/// Reads a frame using the provided PduHint.
pub fn read_by_hint(&mut self, hint: &dyn PduHint) -> io::Result<Bytes> {
pub fn read_by_hint(&mut self, hint: Box<dyn PduHint>) -> io::Result<Bytes> {
loop {
match hint
.find_size(self.peek())
Expand Down
2 changes: 1 addition & 1 deletion crates/ironrdp-client/src/rdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
// RDCleanPath response

let rdcleanpath_res = framed
.read_by_hint(&RDCLEANPATH_HINT)
.read_by_hint(Box::new(RDCLEANPATH_HINT))
.await
.map_err(|e| connector::custom_err!("read RDCleanPath request", e))?;

Expand Down
6 changes: 3 additions & 3 deletions crates/ironrdp-connector/src/channel_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ impl ChannelConnectionSequence {
}

impl Sequence for ChannelConnectionSequence {
fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>> {
match self.state {
ChannelConnectionState::Consumed => None,
ChannelConnectionState::SendErectDomainRequest => None,
ChannelConnectionState::SendAttachUserRequest => None,
ChannelConnectionState::WaitAttachUserConfirm => Some(&ironrdp_pdu::X224_HINT),
ChannelConnectionState::WaitAttachUserConfirm => Some(Box::new(ironrdp_pdu::X224_HINT)),
ChannelConnectionState::SendChannelJoinRequest { .. } => None,
ChannelConnectionState::WaitChannelJoinConfirm { .. } => Some(&ironrdp_pdu::X224_HINT),
ChannelConnectionState::WaitChannelJoinConfirm { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
ChannelConnectionState::AllJoined { .. } => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ironrdp-connector/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ impl ClientConnector {
}

impl Sequence for ClientConnector {
fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>> {
match &self.state {
ClientConnectorState::Consumed => None,
ClientConnectorState::ConnectionInitiationSendRequest => None,
ClientConnectorState::ConnectionInitiationWaitConfirm { .. } => Some(&ironrdp_pdu::X224_HINT),
ClientConnectorState::ConnectionInitiationWaitConfirm { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
ClientConnectorState::EnhancedSecurityUpgrade { .. } => None,
ClientConnectorState::Credssp { .. } => None,
ClientConnectorState::BasicSettingsExchangeSendInitial { .. } => None,
ClientConnectorState::BasicSettingsExchangeWaitResponse { .. } => Some(&ironrdp_pdu::X224_HINT),
ClientConnectorState::BasicSettingsExchangeWaitResponse { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
ClientConnectorState::ChannelConnection { channel_connection, .. } => channel_connection.next_pdu_hint(),
ClientConnectorState::SecureSettingsExchange { .. } => None,
ClientConnectorState::ConnectTimeAutoDetection { .. } => None,
Expand Down
4 changes: 2 additions & 2 deletions crates/ironrdp-connector/src/connection_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ impl ConnectionActivationSequence {
}

impl Sequence for ConnectionActivationSequence {
fn next_pdu_hint(&self) -> Option<&dyn ironrdp_pdu::PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn ironrdp_pdu::PduHint>> {
match &self.state {
ConnectionActivationState::Consumed => None,
ConnectionActivationState::Finalized { .. } => None,
ConnectionActivationState::CapabilitiesExchange { .. } => Some(&ironrdp_pdu::X224_HINT),
ConnectionActivationState::CapabilitiesExchange { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
ConnectionActivationState::ConnectionFinalization {
connection_finalization,
..
Expand Down
4 changes: 2 additions & 2 deletions crates/ironrdp-connector/src/connection_finalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ impl ConnectionFinalizationSequence {
}

impl Sequence for ConnectionFinalizationSequence {
fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>> {
match self.state {
ConnectionFinalizationState::Consumed => None,
ConnectionFinalizationState::SendSynchronize => None,
ConnectionFinalizationState::SendControlCooperate => None,
ConnectionFinalizationState::SendRequestControl => None,
ConnectionFinalizationState::SendFontList => None,
ConnectionFinalizationState::WaitForResponse => Some(&ironrdp_pdu::X224_HINT),
ConnectionFinalizationState::WaitForResponse => Some(Box::new(ironrdp_pdu::X224_HINT)),
ConnectionFinalizationState::Finished => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ironrdp-connector/src/credssp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub(crate) enum CredsspState {
}

impl CredsspSequence {
pub fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
pub fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>> {
match self.state {
CredsspState::Ongoing => Some(&CREDSSP_TS_REQUEST_HINT),
CredsspState::EarlyUserAuthResult => Some(&CREDSSP_EARLY_USER_AUTH_RESULT_HINT),
CredsspState::Ongoing => Some(Box::new(CREDSSP_TS_REQUEST_HINT)),
CredsspState::EarlyUserAuthResult => Some(Box::new(CREDSSP_EARLY_USER_AUTH_RESULT_HINT)),
CredsspState::Finished => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ironrdp-connector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Written {
}

pub trait Sequence: Send {
fn next_pdu_hint(&self) -> Option<&dyn PduHint>;
fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>>;

fn state(&self) -> &dyn State;

Expand Down
8 changes: 4 additions & 4 deletions crates/ironrdp-connector/src/license_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ impl LicenseExchangeSequence {
}

impl Sequence for LicenseExchangeSequence {
fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
fn next_pdu_hint(&self) -> Option<Box<dyn PduHint>> {
match self.state {
LicenseExchangeState::Consumed => None,
LicenseExchangeState::NewLicenseRequest => Some(&ironrdp_pdu::X224_HINT),
LicenseExchangeState::PlatformChallenge { .. } => Some(&ironrdp_pdu::X224_HINT),
LicenseExchangeState::UpgradeLicense { .. } => Some(&ironrdp_pdu::X224_HINT),
LicenseExchangeState::NewLicenseRequest => Some(Box::new(ironrdp_pdu::X224_HINT)),
LicenseExchangeState::PlatformChallenge { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
LicenseExchangeState::UpgradeLicense { .. } => Some(Box::new(ironrdp_pdu::X224_HINT)),
LicenseExchangeState::LicenseExchanged => None,
}
}
Expand Down
Loading