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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::cmp;
use std::io::prelude::*;
use std::io::{self, ErrorKind};
use std::slice;
use libc::{c_uint, c_int, size_t, c_char, c_void, c_uchar, c_ulong};
use {raw, Session, Error};
use util::{Binding, SessionBinding};
pub struct Channel<'sess> {
raw: *mut raw::LIBSSH2_CHANNEL,
sess: &'sess Session,
read_limit: Option<u64>,
}
pub struct Stream<'channel, 'sess: 'channel> {
channel: &'channel mut Channel<'sess>,
id: i32,
}
pub struct ExitSignal {
pub exit_signal: Option<String>,
pub error_message: Option<String>,
pub lang_tag: Option<String>,
}
#[derive(Copy, Clone)]
pub struct ReadWindow {
pub remaining: u32,
pub available: u32,
pub window_size_initial: u32,
}
#[derive(Copy, Clone)]
pub struct WriteWindow {
pub remaining: u32,
pub window_size_initial: u32,
}
impl<'sess> Channel<'sess> {
pub fn setenv(&mut self, var: &str, val: &str) -> Result<(), Error> {
unsafe {
self.sess.rc(raw::libssh2_channel_setenv_ex(self.raw,
var.as_ptr() as *const _,
var.len() as c_uint,
val.as_ptr() as *const _,
val.len() as c_uint))
}
}
pub fn request_pty(&mut self, term: &str,
mode: Option<&str>,
dim: Option<(u32, u32, u32, u32)>)
-> Result<(), Error>{
self.sess.rc(unsafe {
let (width, height, width_px, height_px) =
dim.unwrap_or((80, 24, 0, 0));
raw::libssh2_channel_request_pty_ex(self.raw,
term.as_ptr() as *const _,
term.len() as c_uint,
mode.map(|s| s.as_ptr())
.unwrap_or(0 as *const _)
as *const _,
mode.map(|s| s.len())
.unwrap_or(0) as c_uint,
width as c_int,
height as c_int,
width_px as c_int,
height_px as c_int)
})
}
pub fn request_pty_size(&mut self, width: u32, height: u32,
width_px: Option<u32>, height_px: Option<u32>)
-> Result<(), Error> {
let width_px = width_px.unwrap_or(0);
let height_px = height_px.unwrap_or(0);
self.sess.rc(unsafe {
raw::libssh2_channel_request_pty_size_ex(self.raw,
width as c_int,
height as c_int,
width_px as c_int,
height_px as c_int)
})
}
pub fn exec(&mut self, command: &str) -> Result<(), Error> {
self.process_startup("exec", Some(command))
}
pub fn shell(&mut self) -> Result<(), Error> {
self.process_startup("shell", None)
}
pub fn subsystem(&mut self, system: &str) -> Result<(), Error> {
self.process_startup("subsystem", Some(system))
}
pub fn process_startup(&mut self, request: &str, message: Option<&str>)
-> Result<(), Error> {
let message_len = message.map(|s| s.len()).unwrap_or(0);
let message = message.map(|s| s.as_ptr()).unwrap_or(0 as *const _);
unsafe {
let rc = raw::libssh2_channel_process_startup(self.raw,
request.as_ptr() as *const _, request.len() as c_uint,
message as *const _, message_len as c_uint);
self.sess.rc(rc)
}
}
pub fn stderr<'a>(&'a mut self) -> Stream<'a, 'sess> {
self.stream(::EXTENDED_DATA_STDERR)
}
pub fn stream<'a>(&'a mut self, stream_id: i32) -> Stream<'a, 'sess> {
Stream { channel: self, id: stream_id }
}
pub fn exit_status(&self) -> Result<i32, Error> {
let ret = unsafe { raw::libssh2_channel_get_exit_status(self.raw) };
match Error::last_error(self.sess) {
Some(err) => Err(err),
None => Ok(ret as i32)
}
}
pub fn exit_signal(&self) -> Result<ExitSignal, Error> {
unsafe {
let mut sig = 0 as *mut _;
let mut siglen = 0;
let mut msg = 0 as *mut _;
let mut msglen = 0;
let mut lang = 0 as *mut _;
let mut langlen = 0;
let rc = raw::libssh2_channel_get_exit_signal(self.raw,
&mut sig, &mut siglen,
&mut msg, &mut msglen,
&mut lang,
&mut langlen);
try!(self.sess.rc(rc));
return Ok(ExitSignal {
exit_signal: convert(self, sig, siglen),
error_message: convert(self, msg, msglen),
lang_tag: convert(self, lang, langlen),
})
}
unsafe fn convert(chan: &Channel, ptr: *mut c_char,
len: size_t) -> Option<String> {
if ptr.is_null() { return None }
let slice = slice::from_raw_parts(ptr as *const u8, len as usize);
let ret = slice.to_vec();
raw::libssh2_free(chan.sess.raw(), ptr as *mut c_void);
String::from_utf8(ret).ok()
}
}
pub fn read_window(&self) -> ReadWindow {
unsafe {
let mut avail = 0;
let mut init = 0;
let remaining = raw::libssh2_channel_window_read_ex(self.raw,
&mut avail,
&mut init);
ReadWindow {
remaining: remaining as u32,
available: avail as u32,
window_size_initial: init as u32,
}
}
}
pub fn write_window(&self) -> WriteWindow {
unsafe {
let mut init = 0;
let remaining = raw::libssh2_channel_window_write_ex(self.raw,
&mut init);
WriteWindow {
remaining: remaining as u32,
window_size_initial: init as u32,
}
}
}
pub fn adjust_receive_window(&mut self, adjust: u64, force: bool)
-> Result<u64, Error> {
let mut ret = 0;
let rc = unsafe {
raw::libssh2_channel_receive_window_adjust2(self.raw,
adjust as c_ulong,
force as c_uchar,
&mut ret)
};
try!(self.sess.rc(rc));
Ok(ret as u64)
}
pub fn limit_read(&mut self, limit: u64) {
self.read_limit = Some(limit);
}
pub fn eof(&self) -> bool {
self.read_limit == Some(0) ||
unsafe { raw::libssh2_channel_eof(self.raw) != 0 }
}
pub fn send_eof(&mut self) -> Result<(), Error> {
unsafe {
self.sess.rc(raw::libssh2_channel_send_eof(self.raw))
}
}
pub fn wait_eof(&mut self) -> Result<(), Error> {
unsafe { self.sess.rc(raw::libssh2_channel_wait_eof(self.raw)) }
}
pub fn close(&mut self) -> Result<(), Error> {
unsafe {
self.sess.rc(raw::libssh2_channel_close(self.raw))
}
}
pub fn wait_close(&mut self) -> Result<(), Error> {
unsafe { self.sess.rc(raw::libssh2_channel_wait_closed(self.raw)) }
}
}
impl<'sess> SessionBinding<'sess> for Channel<'sess> {
type Raw = raw::LIBSSH2_CHANNEL;
unsafe fn from_raw(sess: &'sess Session,
raw: *mut raw::LIBSSH2_CHANNEL) -> Channel<'sess> {
Channel {
raw: raw,
sess: sess,
read_limit: None,
}
}
fn raw(&self) -> *mut raw::LIBSSH2_CHANNEL { self.raw }
}
impl<'sess> Write for Channel<'sess> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stream(0).write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.stream(0).flush()
}
}
impl<'sess> Read for Channel<'sess> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.stream(0).read(buf)
}
}
impl<'sess> Drop for Channel<'sess> {
fn drop(&mut self) {
unsafe { assert_eq!(raw::libssh2_channel_free(self.raw), 0) }
}
}
impl<'channel, 'sess> Read for Stream<'channel, 'sess> {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
if self.channel.eof() { return Ok(0) }
let data = match self.channel.read_limit {
Some(amt) => {
let len = data.len();
&mut data[..cmp::min(amt as usize, len)]
}
None => data,
};
let ret = unsafe {
let rc = raw::libssh2_channel_read_ex(self.channel.raw,
self.id as c_int,
data.as_mut_ptr() as *mut _,
data.len() as size_t);
self.channel.sess.rc(rc as c_int).map(|()| rc as usize)
};
match ret {
Ok(n) => {
if let Some(ref mut amt) = self.channel.read_limit {
*amt -= n as u64;
}
Ok(n)
}
Err(e) => Err(io::Error::new(ErrorKind::Other, e)),
}
}
}
impl<'channel, 'sess> Write for Stream<'channel, 'sess> {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
unsafe {
let rc = raw::libssh2_channel_write_ex(self.channel.raw,
self.id as c_int,
data.as_ptr() as *mut _,
data.len() as size_t);
self.channel.sess.rc(rc as c_int).map(|()| rc as usize)
}.map_err(|e| {
io::Error::new(ErrorKind::Other, e)
})
}
fn flush(&mut self) -> io::Result<()> {
unsafe {
let rc = raw::libssh2_channel_flush_ex(self.channel.raw,
self.id as c_int);
self.channel.sess.rc(rc)
}.map_err(|e| {
io::Error::new(ErrorKind::Other, e)
})
}
}