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    /// The channel information: `read_channel_info_length` bytes at
36    /// `read_channel_info_offset`, counted from the start of the SMB2 header
37    /// and not before the end of the fixed fields. Empty when the length is
38    /// 0; the single padding byte a client sends then is not read.
39    #[br(parse_with = super::read_buf_at, args(
40        read_channel_info_offset.into(),
41        read_channel_info_length as usize,
42        READ_BUFFER_OFFSET,
43    ))]
44    pub buffer: Vec<u8>,
45}
46
47/// Offset of the first byte after the fixed READ request fields, counted
48/// from the start of the SMB2 header: 64 + 48.
49const READ_BUFFER_OFFSET: u64 = 0x70;
50
51impl ReadRequest {
52    /// Flag: SMB2_READFLAG_READ_UNBUFFERED (3.0.2+).
53    pub const FLAG_READ_UNBUFFERED: u8 = 0x01;
54    /// Flag: SMB2_READFLAG_REQUEST_COMPRESSED (3.1.1+).
55    pub const FLAG_REQUEST_COMPRESSED: u8 = 0x02;
56
57    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
58        Ok(Self::read(&mut Cursor::new(buf))?)
59    }
60    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
61        let mut c = Cursor::new(Vec::new());
62        BinWrite::write(self, &mut c)?;
63        out.extend_from_slice(&c.into_inner());
64        Ok(())
65    }
66}
67
68/// SMB2_READ_RESPONSE (MS-SMB2 §2.2.20).
69///
70/// `data_offset` is from the start of the SMB2 header. Use
71/// [`ReadResponse::standard_data_offset`] for the canonical "data immediately
72/// after the fixed prefix" layout.
73#[binrw]
74#[brw(little)]
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct ReadResponse {
77    pub structure_size: u16,
78    pub data_offset: u8,
79    pub reserved: u8,
80    pub data_length: u32,
81    pub data_remaining: u32,
82    /// 3.x: `Flags`. 2.x: reserved.
83    pub flags: u32,
84    #[br(parse_with = super::read_buf, args(data_length as usize))]
85    pub data: Vec<u8>,
86}
87
88impl ReadResponse {
89    /// Canonical `DataOffset` value when the data buffer immediately follows
90    /// the fixed 16-byte response prefix and the SMB2 header (64 + 16 = 80).
91    pub const STANDARD_DATA_OFFSET: u8 = 0x50;
92
93    pub const fn standard_data_offset() -> u8 {
94        Self::STANDARD_DATA_OFFSET
95    }
96
97    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
98        Ok(Self::read(&mut Cursor::new(buf))?)
99    }
100    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
101        let mut c = Cursor::new(Vec::new());
102        BinWrite::write(self, &mut c)?;
103        out.extend_from_slice(&c.into_inner());
104        Ok(())
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn request_round_trips() {
114        let r = ReadRequest {
115            structure_size: 49,
116            padding: 0x50,
117            flags: 0,
118            length: 0x1000,
119            offset: 0x2000,
120            file_id: FileId::new(0xAAAA, 0xBBBB),
121            minimum_count: 1,
122            channel: 0,
123            remaining_bytes: 0,
124            read_channel_info_offset: 0,
125            read_channel_info_length: 0,
126            buffer: vec![],
127        };
128        let mut buf = Vec::new();
129        r.write_to(&mut buf).unwrap();
130        assert_eq!(ReadRequest::parse(&buf).unwrap(), r);
131        // The padding byte a client sends with no channel information.
132        buf.push(0);
133        assert_eq!(ReadRequest::parse(&buf).unwrap(), r);
134    }
135
136    #[test]
137    fn response_round_trips() {
138        let r = ReadResponse {
139            structure_size: 17,
140            data_offset: ReadResponse::STANDARD_DATA_OFFSET,
141            reserved: 0,
142            data_length: 5,
143            data_remaining: 0,
144            flags: 0,
145            data: vec![1, 2, 3, 4, 5],
146        };
147        let mut buf = Vec::new();
148        r.write_to(&mut buf).unwrap();
149        assert_eq!(ReadResponse::parse(&buf).unwrap(), r);
150    }
151}