-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathchannel_connection.rs
226 lines (182 loc) · 8.05 KB
/
channel_connection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::mem;
use ironrdp_pdu::write_buf::WriteBuf;
use ironrdp_pdu::{mcs, PduHint};
use crate::{ConnectorError, ConnectorErrorExt as _, ConnectorResult, Sequence, State, Written};
#[derive(Default, Debug)]
#[non_exhaustive]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ChannelConnectionState {
#[default]
Consumed,
SendErectDomainRequest,
SendAttachUserRequest,
WaitAttachUserConfirm,
SendChannelJoinRequest {
user_channel_id: u16,
index: usize,
},
WaitChannelJoinConfirm {
user_channel_id: u16,
index: usize,
},
AllJoined {
user_channel_id: u16,
},
}
impl State for ChannelConnectionState {
fn name(&self) -> &'static str {
match self {
Self::Consumed => "Consumed",
Self::SendErectDomainRequest => "SendErectDomainRequest",
Self::SendAttachUserRequest => "SendAttachUserRequest",
Self::WaitAttachUserConfirm => "WaitAttachUserConfirm",
Self::SendChannelJoinRequest { .. } => "SendChannelJoinRequest",
Self::WaitChannelJoinConfirm { .. } => "WaitChannelJoinConfirm",
Self::AllJoined { .. } => "AllJoined",
}
}
fn is_terminal(&self) -> bool {
matches!(self, Self::AllJoined { .. })
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
}
#[derive(Debug)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ChannelConnectionSequence {
pub state: ChannelConnectionState,
pub channel_ids: Vec<u16>,
}
impl ChannelConnectionSequence {
pub fn new(io_channel_id: u16, mut channel_ids: Vec<u16>) -> Self {
// I/O channel ID must be joined as well
channel_ids.push(io_channel_id);
Self {
state: ChannelConnectionState::SendErectDomainRequest,
channel_ids,
}
}
}
impl Sequence for ChannelConnectionSequence {
fn next_pdu_hint(&self) -> Option<&dyn PduHint> {
match self.state {
ChannelConnectionState::Consumed => None,
ChannelConnectionState::SendErectDomainRequest => None,
ChannelConnectionState::SendAttachUserRequest => None,
ChannelConnectionState::WaitAttachUserConfirm => Some(&ironrdp_pdu::X224_HINT),
ChannelConnectionState::SendChannelJoinRequest { .. } => None,
ChannelConnectionState::WaitChannelJoinConfirm { .. } => Some(&ironrdp_pdu::X224_HINT),
ChannelConnectionState::AllJoined { .. } => None,
}
}
fn step(&mut self, input: &[u8], output: &mut WriteBuf) -> ConnectorResult<Written> {
let (written, next_state) = match mem::take(&mut self.state) {
ChannelConnectionState::Consumed => {
return Err(general_err!(
"channel connection sequence state is consumed (this is a bug)",
))
}
ChannelConnectionState::SendErectDomainRequest => {
let erect_domain_request = mcs::ErectDomainPdu {
sub_height: 0,
sub_interval: 0,
};
debug!(message = ?erect_domain_request, "Send");
let written = ironrdp_pdu::encode_buf(&erect_domain_request, output).map_err(ConnectorError::pdu)?;
(
Written::from_size(written)?,
ChannelConnectionState::SendAttachUserRequest,
)
}
ChannelConnectionState::SendAttachUserRequest => {
let attach_user_request = mcs::AttachUserRequest;
debug!(message = ?attach_user_request, "Send");
let written = ironrdp_pdu::encode_buf(&attach_user_request, output).map_err(ConnectorError::pdu)?;
(
Written::from_size(written)?,
ChannelConnectionState::WaitAttachUserConfirm,
)
}
ChannelConnectionState::WaitAttachUserConfirm => {
let attach_user_confirm =
ironrdp_pdu::decode::<mcs::AttachUserConfirm>(input).map_err(ConnectorError::pdu)?;
let user_channel_id = attach_user_confirm.initiator_id;
// user channel ID must also be joined
self.channel_ids.push(user_channel_id);
debug!(message = ?attach_user_confirm, user_channel_id, "Received");
debug_assert!(!self.channel_ids.is_empty());
(
Written::Nothing,
ChannelConnectionState::SendChannelJoinRequest {
user_channel_id,
index: 0,
},
)
}
// TODO(#112): send all the join requests in a single batch
// (RDP 4.0, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 8.0, 10.2, 10.3,
// 10.4, and 10.5 clients send a Channel Join Request to the server only after the
// Channel Join Confirm for a previously sent request has been received. RDP 8.1,
// 10.0, and 10.1 clients send all of the Channel Join Requests to the server in a
// single batch to minimize the overall connection sequence time.)
ChannelConnectionState::SendChannelJoinRequest { user_channel_id, index } => {
let channel_id = self.channel_ids[index];
let channel_join_request = mcs::ChannelJoinRequest {
initiator_id: user_channel_id,
channel_id,
};
debug!(message = ?channel_join_request, "Send");
let written = ironrdp_pdu::encode_buf(&channel_join_request, output).map_err(ConnectorError::pdu)?;
(
Written::from_size(written)?,
ChannelConnectionState::WaitChannelJoinConfirm { user_channel_id, index },
)
}
ChannelConnectionState::WaitChannelJoinConfirm { user_channel_id, index } => {
let channel_id = self.channel_ids[index];
let channel_join_confirm =
ironrdp_pdu::decode::<mcs::ChannelJoinConfirm>(input).map_err(ConnectorError::pdu)?;
debug!(message = ?channel_join_confirm, "Received");
if channel_join_confirm.initiator_id != user_channel_id {
warn!(
channel_join_confirm.initiator_id,
user_channel_id, "Inconsistent initiator ID for MCS Channel Join Confirm",
)
}
if channel_id != channel_join_confirm.requested_channel_id {
return Err(reason_err!(
"ChannelJoinConfirm",
"unexpected requested_channel_id in MCS Channel Join Confirm: received {}, got {}",
channel_id,
channel_join_confirm.requested_channel_id,
));
}
if channel_id != channel_join_confirm.channel_id {
return Err(reason_err!(
"ChannelJoinConfirm",
"unexpected channel_id in MCS Channel Join Confirm: received {}, got {}",
channel_id,
channel_join_confirm.channel_id,
));
}
let next_index = index.checked_add(1).unwrap();
let next_state = if next_index == self.channel_ids.len() {
ChannelConnectionState::AllJoined { user_channel_id }
} else {
ChannelConnectionState::SendChannelJoinRequest {
user_channel_id,
index: next_index,
}
};
(Written::Nothing, next_state)
}
ChannelConnectionState::AllJoined { .. } => return Err(general_err!("all channels are already joined")),
};
self.state = next_state;
Ok(written)
}
fn state(&self) -> &dyn State {
&self.state
}
}