Skip to main content

smb_server/proto/messages/
set_info.rs

1//! SET_INFO Request/Response (MS-SMB2 §2.2.39 / §2.2.40).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use super::create::FileId;
7use crate::proto::error::ProtoResult;
8
9/// SMB2_SET_INFO_REQUEST (MS-SMB2 §2.2.39).
10#[binrw]
11#[brw(little)]
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct SetInfoRequest {
14    pub structure_size: u16,
15    pub info_type: u8,
16    pub file_information_class: u8,
17    pub buffer_length: u32,
18    pub buffer_offset: u16,
19    pub reserved: u16,
20    pub additional_information: u32,
21    pub file_id: FileId,
22    #[br(count = buffer_length as usize)]
23    pub buffer: Vec<u8>,
24}
25
26impl SetInfoRequest {
27    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
28        Ok(Self::read(&mut Cursor::new(buf))?)
29    }
30    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
31        let mut c = Cursor::new(Vec::new());
32        BinWrite::write(self, &mut c)?;
33        out.extend_from_slice(&c.into_inner());
34        Ok(())
35    }
36}
37
38/// SMB2_SET_INFO_RESPONSE (MS-SMB2 §2.2.40).
39#[binrw]
40#[brw(little)]
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct SetInfoResponse {
43    pub structure_size: u16,
44}
45
46impl Default for SetInfoResponse {
47    fn default() -> Self {
48        Self { structure_size: 2 }
49    }
50}
51
52impl SetInfoResponse {
53    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
54        Ok(Self::read(&mut Cursor::new(buf))?)
55    }
56    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
57        let mut c = Cursor::new(Vec::new());
58        BinWrite::write(self, &mut c)?;
59        out.extend_from_slice(&c.into_inner());
60        Ok(())
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn request_round_trips() {
70        let r = SetInfoRequest {
71            structure_size: 33,
72            info_type: 0x01,              // File
73            file_information_class: 0x14, // FileEndOfFileInformation
74            buffer_length: 8,
75            buffer_offset: 0x60,
76            reserved: 0,
77            additional_information: 0,
78            file_id: FileId::new(1, 2),
79            buffer: vec![0, 0, 0, 0x10, 0, 0, 0, 0],
80        };
81        let mut buf = Vec::new();
82        r.write_to(&mut buf).unwrap();
83        assert_eq!(SetInfoRequest::parse(&buf).unwrap(), r);
84    }
85
86    #[test]
87    fn response_round_trips() {
88        let r = SetInfoResponse::default();
89        let mut buf = Vec::new();
90        r.write_to(&mut buf).unwrap();
91        assert_eq!(SetInfoResponse::parse(&buf).unwrap(), r);
92        assert_eq!(buf.len(), 2);
93    }
94}