aria2-protocol 0.3.10

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! SFTP Session Management
//!
//! Manages the SFTP protocol session lifecycle including version negotiation,
//! request ID tracking, and operation timeout handling. Built entirely on
//! pure Rust using `russh` channels and the packet codec.
//!
//! ## Session Lifecycle
//!
//! ```text
//! SSH Connection  ->  Open SFTP Channel  ->  Send INIT  ->  Receive VERSION
//!       |                  |                    |              |
//!       v                  v                    v              v
//!   Authenticated     russh::Channel      Request ID=0   Server Version + Extensions
//!                                                    |
//!                                                    v
//!                                             Ready for file operations
//! ```

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::Mutex;
use tracing::{debug, info, warn};

use super::connection::SshConnection;
use super::packet::SftpPacket;

mod error;
#[cfg(test)]
mod tests;

pub use error::SftpSessionError;

/// Minimum supported SFTP protocol version.
pub const SFTP_VERSION_MIN: u32 = 3;
/// Maximum supported SFTP protocol version.
///
/// The packet codec currently implements the SFTP v3 wire format.
pub const SFTP_VERSION_MAX: u32 = 3;
/// Represents a server-side SFTP protocol extension
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SftpExtension {
    /// Extension name (e.g., "hardlink@openssh.com")
    pub name: String,
    /// Extension-specific data (often version number)
    pub data: String,
}

/// Serialized I/O state for one SFTP subsystem channel.
///
/// `russh::Channel` already owns the receive half for its channel. Keeping
/// that stream and the packet buffer together prevents a second forwarding
/// queue, unnecessary packet copies, and response-order races.
struct SftpIo {
    stream: russh::ChannelStream<russh::client::Msg>,
    recv_buffer: Vec<u8>,
}

struct InFlightRequest {
    session_poisoned: Arc<AtomicBool>,
    completed: bool,
}

impl InFlightRequest {
    fn new(session_poisoned: Arc<AtomicBool>) -> Self {
        Self {
            session_poisoned,
            completed: false,
        }
    }

    fn complete(mut self) {
        self.completed = true;
    }
}

impl Drop for InFlightRequest {
    fn drop(&mut self) {
        if !self.completed {
            self.session_poisoned.store(true, Ordering::Release);
        }
    }
}

impl std::fmt::Display for SftpExtension {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}={}", self.name, self.data)
    }
}

/// Manages an active SFTP session over a russh SSH channel.
///
/// This struct handles:
/// - SFTP version negotiation during initialization (using the packet codec)
/// - Monotonically increasing request IDs for request/response correlation
/// - Timeout tracking for in-flight operations
/// - Server extension advertisement tracking
/// - Packet I/O through the russh channel using packet encoding/decoding
///
/// **No unsafe code** -- all memory management is handled by Rust's ownership system.
pub struct SftpSession {
    /// Serialized SFTP channel I/O and packet reassembly state.
    io: Arc<Mutex<SftpIo>>,
    /// The negotiated server SFTP protocol version
    server_version: u32,
    /// Monotonically increasing request ID counter
    next_request_id: Arc<AtomicU32>,
    /// Server-advertised extensions from the VERSION response
    extensions: Vec<SftpExtension>,
    /// When this session was initialized
    created_at: std::time::Instant,
    /// Completed request/response exchanges (for metrics/diagnostics)
    operation_count: Arc<AtomicU32>,
    /// An interrupted exchange leaves the response stream unsynchronized.
    session_poisoned: Arc<AtomicBool>,
    /// Read timeout for individual operations
    read_timeout: Duration,
}

impl std::fmt::Debug for SftpSession {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SftpSession")
            .field("server_version", &self.server_version)
            .field("extensions", &self.extensions)
            .field("age_secs", &self.age().as_secs())
            .finish()
    }
}

/// Clone implementation for SftpSession -- all mutable state is shared, so
/// request IDs and metrics remain correct across clones.
impl Clone for SftpSession {
    fn clone(&self) -> Self {
        Self {
            io: Arc::clone(&self.io),
            server_version: self.server_version,
            next_request_id: Arc::clone(&self.next_request_id),
            extensions: self.extensions.clone(),
            created_at: self.created_at,
            operation_count: Arc::clone(&self.operation_count),
            session_poisoned: Arc::clone(&self.session_poisoned),
            read_timeout: self.read_timeout,
        }
    }
}

impl SftpSession {
    /// Initialize an SFTP session over an established SSH connection.
    ///
    /// This method:
    /// 1. Opens an SFTP subsystem channel via the SSH connection
    /// 2. Sends SSH_FXP_INIT(version=3) using packet encoding
    /// 3. Receives SSH_FXP_VERSION response via channel data callback
    /// 4. Parses the response using packet decoding
    /// 5. Extracts server version and advertised extensions
    ///
    /// # Arguments
    /// * `conn` - An authenticated SSH connection ready for subsystem use
    ///
    /// # Returns
    /// A fully initialized `SftpSession` ready for file operations.
    pub async fn open(conn: &mut SshConnection) -> Result<Self, SftpSessionError> {
        debug!("[SFTP] Initializing SFTP session (pure Rust)...");

        // Step 1: Open SFTP subsystem channel on the connection
        let channel = conn.open_sftp_channel().await?;

        let read_timeout = conn.options().read_timeout;

        let mut io = SftpIo {
            stream: channel.into_stream(),
            recv_buffer: Vec::new(),
        };

        // Step 2: Send SSH_FXP_INIT(version=3) using the packet codec
        let init_pkt = SftpPacket::Init { version: 3 };
        let encoded = init_pkt.encode().map_err(SftpSessionError::PacketEncode)?;

        Self::write_packet(&mut io, &encoded, "SFTP INIT").await?;

        debug!("[SFTP] Sent SFTP INIT (version=3), awaiting VERSION...");

        // Step 3: Receive SSH_FXP_VERSION response with timeout
        let version_response = Self::recv_packet_from_io(&mut io, read_timeout).await?;

        // Step 4: Parse the VERSION response
        let (server_version, extensions) = match version_response {
            SftpPacket::Version {
                version,
                extensions,
            } => {
                Self::validate_server_version(version)?;
                (version, extensions)
            }
            other => {
                return Err(SftpSessionError::UnexpectedPacket {
                    expected: "VERSION",
                    actual_type: other.packet_type(),
                });
            }
        };

        info!(
            "[SFTP] Session established (server version=v{}, supports v{}-v{}, {} extensions)",
            server_version,
            SFTP_VERSION_MIN,
            SFTP_VERSION_MAX,
            extensions.len()
        );

        Ok(Self {
            io: Arc::new(Mutex::new(io)),
            server_version,
            next_request_id: Arc::new(AtomicU32::new(1)), // Start at 1, 0 reserved for INIT
            extensions: extensions
                .into_iter()
                .map(|(name, data)| SftpExtension { name, data })
                .collect(),
            created_at: std::time::Instant::now(),
            operation_count: Arc::new(AtomicU32::new(0)),
            session_poisoned: Arc::new(AtomicBool::new(false)),
            read_timeout,
        })
    }

    fn validate_server_version(version: u32) -> Result<(), SftpSessionError> {
        if version < SFTP_VERSION_MIN {
            Err(SftpSessionError::UnsupportedVersion {
                server_version: version,
                minimum: SFTP_VERSION_MIN,
            })
        } else if version > SFTP_VERSION_MAX {
            Err(SftpSessionError::UnsupportedVersionAboveMaximum {
                server_version: version,
                maximum: SFTP_VERSION_MAX,
            })
        } else {
            Ok(())
        }
    }

    async fn write_packet(
        io: &mut SftpIo,
        encoded: &[u8],
        operation: &'static str,
    ) -> Result<(), SftpSessionError> {
        io.stream
            .write_all(encoded)
            .await
            .map_err(|source| SftpSessionError::ChannelWrite { operation, source })?;
        io.stream
            .flush()
            .await
            .map_err(|source| SftpSessionError::ChannelFlush { operation, source })
    }

    /// Internal implementation of packet reception with buffering.
    ///
    /// SFTP packets may arrive fragmented across multiple channel data callbacks.
    /// This method buffers incoming data until a complete packet (with length prefix)
    /// can be decoded.
    async fn recv_packet_from_io(
        io: &mut SftpIo,
        timeout: Duration,
    ) -> Result<SftpPacket, SftpSessionError> {
        Self::recv_packet_from_reader(&mut io.stream, &mut io.recv_buffer, timeout).await
    }

    async fn recv_packet_from_reader<R>(
        reader: &mut R,
        recv_buffer: &mut Vec<u8>,
        timeout: Duration,
    ) -> Result<SftpPacket, SftpSessionError>
    where
        R: tokio::io::AsyncRead + Unpin,
    {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            if !recv_buffer.is_empty() {
                match SftpPacket::decode(recv_buffer.as_slice()) {
                    Ok((packet, consumed)) => {
                        recv_buffer.drain(..consumed);
                        return Ok(packet);
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => {}
                    Err(error) => return Err(SftpSessionError::PacketDecode(error)),
                }
            }

            let mut chunk = [0_u8; 32 * 1024];
            let count = tokio::time::timeout_at(deadline, reader.read(&mut chunk))
                .await
                .map_err(|_| SftpSessionError::Timeout(timeout))?
                .map_err(SftpSessionError::ChannelRead)?;
            if count == 0 {
                return Err(SftpSessionError::ChannelClosed);
            }
            recv_buffer.extend_from_slice(&chunk[..count]);
        }
    }

    /// Combined send + receive with automatic request ID management.
    ///
    /// This is the only public request path. It allocates a request ID, sets it
    /// in the packet, sends it, verifies the response ID, and records the
    /// completed exchange.
    pub(super) async fn request(
        &self,
        mut pkt: SftpPacket,
    ) -> Result<SftpPacket, SftpSessionError> {
        let req_id = self.allocate_request_id();

        // Set the request ID in the packet
        Self::set_request_id_in_packet(&mut pkt, req_id);

        // Keep one request/response exchange serialized. SFTP servers can
        // reply out of order, while this client intentionally exposes a
        // synchronous operation seam rather than a speculative pending map.
        let encoded = pkt.encode().map_err(SftpSessionError::PacketEncode)?;
        let mut io = self.io.lock().await;
        if self.session_poisoned.load(Ordering::Acquire) {
            return Err(SftpSessionError::SessionPoisoned);
        }
        let request_guard = InFlightRequest::new(Arc::clone(&self.session_poisoned));
        Self::write_packet(&mut io, &encoded, "SFTP request").await?;
        let response = Self::recv_packet_from_io(&mut io, self.read_timeout).await?;

        let actual_id = response.request_id();
        if actual_id != Some(req_id) {
            return Err(SftpSessionError::RequestIdMismatch {
                expected: req_id,
                actual: actual_id,
            });
        }

        self.operation_count.fetch_add(1, Ordering::Relaxed);
        request_guard.complete();
        Ok(response)
    }

    /// Set the request ID field in any request-type SftpPacket.
    fn set_request_id_in_packet(pkt: &mut SftpPacket, new_request_id: u32) {
        match pkt {
            SftpPacket::Open { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Close { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Read { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Write { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Stat { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Lstat { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Fstat { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Setstat { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Fsetstat { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Opendir { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Readdir { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Realpath { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Remove { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Mkdir { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Rmdir { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Rename { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Readlink { request_id, .. } => *request_id = new_request_id,
            SftpPacket::Symlink { request_id, .. } => *request_id = new_request_id,
            // Init/Version have no request_id; Status/Handle/Data/Name/Attrs are responses
            _ => {} // No-op for non-request or response packets
        }
    }

    /// Get the negotiated server SFTP protocol version
    pub fn server_version(&self) -> u32 {
        self.server_version
    }

    /// Get how long this session has been active
    pub fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    /// Get the number of completed request/response exchanges in this session.
    pub fn operation_count(&self) -> u32 {
        self.operation_count.load(Ordering::Relaxed)
    }

    /// Get the configured read timeout for operations
    pub fn read_timeout(&self) -> Duration {
        self.read_timeout
    }

    // ====================================================================
    // Request ID Management
    // ====================================================================

    /// Allocate the next unique request ID.
    ///
    /// Request IDs are monotonically increasing and used to correlate
    /// requests with their responses. The SFTP protocol requires each
    /// request to carry a unique ID that the server echoes back.
    ///
    /// # Returns
    /// A new unique request ID (never zero)
    fn allocate_request_id(&self) -> u32 {
        let id = self.next_request_id.fetch_add(1, Ordering::Relaxed);

        // Wrap around check: if we've exhausted u32 space, warn but continue
        // In practice this won't happen (4 billion requests per session)
        if id == 0 {
            warn!("[SFTP] Request ID counter wrapped around");
            self.next_request_id.store(1, Ordering::Relaxed);
            1
        } else {
            id
        }
    }

    // ====================================================================
    // Extension Support
    // ====================================================================

    /// Check if the server advertises a specific extension by name.
    ///
    /// Common extensions include:
    /// - `hardlink@openssh.com` - Hard link creation
    /// - `fsync@openssh.com` - File sync to disk
    /// - `posix-rename@openssh.com` - POSIX-compliant rename (atomic)
    /// - `statvfs@openssh.com` - Filesystem statistics
    /// - `fstatvfs@openssh.com` - Filesystem statistics by handle
    /// - `lsetstat@openssh.com` - Set attributes without following symlinks
    /// - `limits@openssh.com` - Server-side limits information
    /// - `expand-path@openssh.com` - Path expansion with tilde/env vars
    /// - `copy-data` - Server-side file copy (draft)
    /// - `space-available` - Available space query (draft)
    /// - `open@openssh.com` - Extended open flags
    /// - `close@openssh.com` - Extended close with reasons
    pub fn has_extension(&self, name: &str) -> bool {
        self.extensions.iter().any(|ext| ext.name == name)
    }

    /// Get extension data by name, if present.
    pub fn get_extension(&self, name: &str) -> Option<&SftpExtension> {
        self.extensions.iter().find(|ext| ext.name == name)
    }

    /// Get all extensions advertised by the server.
    pub fn extensions(&self) -> &[SftpExtension] {
        &self.extensions
    }

    // ====================================================================
    // Diagnostics
    // ====================================================================

    /// Generate a diagnostic summary of this session's state.
    pub fn diagnostics(&self) -> String {
        format!(
            "SftpSession{{version=v{}, age={}s, ops={}, extensions=[{}]}}",
            self.server_version,
            self.age().as_secs(),
            self.operation_count(),
            self.extensions
                .iter()
                .map(|e| e.name.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )
    }
}

// =============================================================================