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"). A response sets
10//! `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." The buffer
37    /// always holds at least one byte on the wire.
38    #[br(parse_with = super::read_buf, args(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(parse_with = super::read_buf, args(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    pub const STANDARD_DATA_OFFSET: u8 = 0x50;
83
84    pub const fn standard_data_offset() -> u8 {
85        Self::STANDARD_DATA_OFFSET
86    }
87
88    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
89        Ok(Self::read(&mut Cursor::new(buf))?)
90    }
91    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
92        let mut c = Cursor::new(Vec::new());
93        BinWrite::write(self, &mut c)?;
94        out.extend_from_slice(&c.into_inner());
95        Ok(())
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn request_round_trips() {
105        let r = ReadRequest {
106            structure_size: 49,
107            padding: 0x50,
108            flags: 0,
109            length: 0x1000,
110            offset: 0x2000,
111            file_id: FileId::new(0xAAAA, 0xBBBB),
112            minimum_count: 1,
113            channel: 0,
114            remaining_bytes: 0,
115            read_channel_info_offset: 0,
116            read_channel_info_length: 0,
117            buffer: vec![0],
118        };
119        let mut buf = Vec::new();
120        r.write_to(&mut buf).unwrap();
121        assert_eq!(ReadRequest::parse(&buf).unwrap(), r);
122    }
123
124    #[test]
125    fn response_round_trips() {
126        let r = ReadResponse {
127            structure_size: 17,
128            data_offset: ReadResponse::STANDARD_DATA_OFFSET,
129            reserved: 0,
130            data_length: 5,
131            data_remaining: 0,
132            flags: 0,
133            data: vec![1, 2, 3, 4, 5],
134        };
135        let mut buf = Vec::new();
136        r.write_to(&mut buf).unwrap();
137        assert_eq!(ReadResponse::parse(&buf).unwrap(), r);
138    }
139}