Skip to main content

smb_server/proto/messages/
write.rs

1//! WRITE Request/Response (MS-SMB2 §2.2.21 / §2.2.22).
2//!
3//! ## Data buffer offsets
4//!
5//! `DataOffset` is from the **start of the SMB2 header**, not from the start
6//! of this structure (MS-SMB2 §2.2.21). The canonical layout puts the data
7//! immediately after the fixed 48-byte prefix, giving 64 + 48 = 112 = 0x70.
8
9use binrw::{BinRead, BinWrite, binrw};
10use std::io::Cursor;
11
12use super::create::FileId;
13use crate::proto::error::ProtoResult;
14
15/// SMB2_WRITE_REQUEST (MS-SMB2 §2.2.21).
16#[binrw]
17#[brw(little)]
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct WriteRequest {
20    pub structure_size: u16,
21    pub data_offset: u16,
22    pub length: u32,
23    pub offset: u64,
24    pub file_id: FileId,
25    pub channel: u32,
26    pub remaining_bytes: u32,
27    pub write_channel_info_offset: u16,
28    pub write_channel_info_length: u16,
29    pub flags: u32,
30    /// MS-SMB2: at least 1 byte of payload buffer is required on the wire
31    /// even when length=0.
32    #[br(count = if length == 0 { 1 } else { length as usize })]
33    pub data: Vec<u8>,
34}
35
36impl WriteRequest {
37    /// Canonical `DataOffset` placing the data buffer immediately after the
38    /// fixed 48-byte WRITE prefix: 64 (SMB2 header) + 48 = 112 = 0x70.
39    pub const STANDARD_DATA_OFFSET: u16 = 0x70;
40    /// Flag: SMB2_WRITEFLAG_WRITE_THROUGH.
41    pub const FLAG_WRITE_THROUGH: u32 = 0x0000_0001;
42    /// Flag: SMB2_WRITEFLAG_WRITE_UNBUFFERED (3.0.2+).
43    pub const FLAG_WRITE_UNBUFFERED: u32 = 0x0000_0002;
44
45    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
46        Ok(Self::read(&mut Cursor::new(buf))?)
47    }
48    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
49        let mut c = Cursor::new(Vec::new());
50        BinWrite::write(self, &mut c)?;
51        out.extend_from_slice(&c.into_inner());
52        Ok(())
53    }
54}
55
56/// SMB2_WRITE_RESPONSE (MS-SMB2 §2.2.22).
57#[binrw]
58#[brw(little)]
59#[derive(Debug, Clone, PartialEq, Eq, Default)]
60pub struct WriteResponse {
61    pub structure_size: u16,
62    pub reserved: u16,
63    pub count: u32,
64    pub remaining: u32,
65    pub write_channel_info_offset: u16,
66    pub write_channel_info_length: u16,
67}
68
69impl WriteResponse {
70    pub fn new(count: u32) -> Self {
71        Self {
72            structure_size: 17,
73            reserved: 0,
74            count,
75            remaining: 0,
76            write_channel_info_offset: 0,
77            write_channel_info_length: 0,
78        }
79    }
80
81    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
82        Ok(Self::read(&mut Cursor::new(buf))?)
83    }
84    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
85        let mut c = Cursor::new(Vec::new());
86        BinWrite::write(self, &mut c)?;
87        out.extend_from_slice(&c.into_inner());
88        Ok(())
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn request_round_trips() {
98        let r = WriteRequest {
99            structure_size: 49,
100            data_offset: WriteRequest::STANDARD_DATA_OFFSET,
101            length: 4,
102            offset: 0x100,
103            file_id: FileId::new(0xAA, 0xBB),
104            channel: 0,
105            remaining_bytes: 0,
106            write_channel_info_offset: 0,
107            write_channel_info_length: 0,
108            flags: 0,
109            data: vec![1, 2, 3, 4],
110        };
111        let mut buf = Vec::new();
112        r.write_to(&mut buf).unwrap();
113        assert_eq!(WriteRequest::parse(&buf).unwrap(), r);
114    }
115
116    #[test]
117    fn response_round_trips() {
118        let r = WriteResponse::new(0x1000);
119        let mut buf = Vec::new();
120        r.write_to(&mut buf).unwrap();
121        assert_eq!(WriteResponse::parse(&buf).unwrap(), r);
122    }
123}