Skip to main content

smb_server/proto/messages/
read.rs

1//! READ Request/Response (MS-SMB2 §2.2.19 / §2.2.20).
2//!
3//! ## Data buffer offsets
4//!
5//! Both the READ request `ReadChannelInfoOffset` and the READ response
6//! `DataOffset` are measured from the **start of the SMB2 header**, not from
7//! the start of this structure (MS-SMB2 §2.2.20 explicitly: "DataOffset (1
8//! byte): The offset, in bytes, from the beginning of the SMB2 header to the
9//! data being read"). When constructing a response, the server crate must
10//! compute `DataOffset = SMB2_HEADER_LEN + offset_within_body_of_data`.
11
12use binrw::{BinRead, BinWrite, binrw};
13use std::io::Cursor;
14
15use super::create::FileId;
16use crate::proto::error::ProtoResult;
17
18/// SMB2_READ_REQUEST (MS-SMB2 §2.2.19).
19#[binrw]
20#[brw(little)]
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ReadRequest {
23    pub structure_size: u16,
24    pub padding: u8,
25    /// 3.0+ flags (`SMB2_READFLAG_*`); reserved on 2.x.
26    pub flags: u8,
27    pub length: u32,
28    pub offset: u64,
29    pub file_id: FileId,
30    pub minimum_count: u32,
31    pub channel: u32,
32    pub remaining_bytes: u32,
33    pub read_channel_info_offset: u16,
34    pub read_channel_info_length: u16,
35    /// MS-SMB2: "If ReadChannelInfoOffset and ReadChannelInfoLength are both
36    /// 0, the client MUST set this field to a single 0 byte." We follow that
37    /// — at least one byte of buffer is required on the wire.
38    #[br(count = if read_channel_info_length == 0 { 1 } else { read_channel_info_length as usize })]
39    pub buffer: Vec<u8>,
40}
41
42impl ReadRequest {
43    /// Flag: SMB2_READFLAG_READ_UNBUFFERED (3.0.2+).
44    pub const FLAG_READ_UNBUFFERED: u8 = 0x01;
45    /// Flag: SMB2_READFLAG_REQUEST_COMPRESSED (3.1.1+).
46    pub const FLAG_REQUEST_COMPRESSED: u8 = 0x02;
47
48    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
49        Ok(Self::read(&mut Cursor::new(buf))?)
50    }
51    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
52        let mut c = Cursor::new(Vec::new());
53        BinWrite::write(self, &mut c)?;
54        out.extend_from_slice(&c.into_inner());
55        Ok(())
56    }
57}
58
59/// SMB2_READ_RESPONSE (MS-SMB2 §2.2.20).
60///
61/// `data_offset` is from the start of the SMB2 header. Use
62/// [`ReadResponse::standard_data_offset`] for the canonical "data immediately
63/// after the fixed prefix" layout.
64#[binrw]
65#[brw(little)]
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct ReadResponse {
68    pub structure_size: u16,
69    pub data_offset: u8,
70    pub reserved: u8,
71    pub data_length: u32,
72    pub data_remaining: u32,
73    /// 3.x: `Flags`. 2.x: reserved.
74    pub flags: u32,
75    #[br(count = data_length as usize)]
76    pub data: Vec<u8>,
77}
78
79impl ReadResponse {
80    /// Canonical `DataOffset` value when the data buffer immediately follows
81    /// the fixed 16-byte response prefix and the SMB2 header (64 + 16 = 80).
82    ///
83    /// Most servers (ksmbd, Samba) emit 0x50 = 80 here.
84    pub const STANDARD_DATA_OFFSET: u8 = 0x50;
85
86    pub const fn standard_data_offset() -> u8 {
87        Self::STANDARD_DATA_OFFSET
88    }
89
90    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
91        Ok(Self::read(&mut Cursor::new(buf))?)
92    }
93    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
94        let mut c = Cursor::new(Vec::new());
95        BinWrite::write(self, &mut c)?;
96        out.extend_from_slice(&c.into_inner());
97        Ok(())
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn request_round_trips() {
107        let r = ReadRequest {
108            structure_size: 49,
109            padding: 0x50,
110            flags: 0,
111            length: 0x1000,
112            offset: 0x2000,
113            file_id: FileId::new(0xAAAA, 0xBBBB),
114            minimum_count: 1,
115            channel: 0,
116            remaining_bytes: 0,
117            read_channel_info_offset: 0,
118            read_channel_info_length: 0,
119            buffer: vec![0],
120        };
121        let mut buf = Vec::new();
122        r.write_to(&mut buf).unwrap();
123        assert_eq!(ReadRequest::parse(&buf).unwrap(), r);
124    }
125
126    #[test]
127    fn response_round_trips() {
128        let r = ReadResponse {
129            structure_size: 17,
130            data_offset: ReadResponse::STANDARD_DATA_OFFSET,
131            reserved: 0,
132            data_length: 5,
133            data_remaining: 0,
134            flags: 0,
135            data: vec![1, 2, 3, 4, 5],
136        };
137        let mut buf = Vec::new();
138        r.write_to(&mut buf).unwrap();
139        assert_eq!(ReadResponse::parse(&buf).unwrap(), r);
140    }
141}