Skip to main content

smb_server/proto/messages/
lock.rs

1//! LOCK Request/Response (MS-SMB2 §2.2.26 / §2.2.27).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use super::create::FileId;
7use crate::proto::error::ProtoResult;
8
9/// SMB2_LOCK_ELEMENT (MS-SMB2 §2.2.26.1) - exactly 24 bytes.
10#[binrw]
11#[brw(little)]
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct LockElement {
14    pub offset: u64,
15    pub length: u64,
16    pub flags: u32,
17    pub reserved: u32,
18}
19
20impl LockElement {
21    pub const FLAG_SHARED_LOCK: u32 = 0x0000_0001;
22    pub const FLAG_EXCLUSIVE_LOCK: u32 = 0x0000_0002;
23    pub const FLAG_UNLOCK: u32 = 0x0000_0004;
24    pub const FLAG_FAIL_IMMEDIATELY: u32 = 0x0000_0010;
25}
26
27#[binrw]
28#[brw(little)]
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct LockRequest {
31    pub structure_size: u16,
32    pub lock_count: u16,
33    pub lock_sequence: u32,
34    pub file_id: FileId,
35    #[br(count = lock_count as usize)]
36    pub locks: Vec<LockElement>,
37}
38
39impl LockRequest {
40    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
41        Ok(Self::read(&mut Cursor::new(buf))?)
42    }
43    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
44        let mut c = Cursor::new(Vec::new());
45        BinWrite::write(self, &mut c)?;
46        out.extend_from_slice(&c.into_inner());
47        Ok(())
48    }
49}
50
51#[binrw]
52#[brw(little)]
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct LockResponse {
55    pub structure_size: u16,
56    pub reserved: u16,
57}
58
59impl Default for LockResponse {
60    fn default() -> Self {
61        Self {
62            structure_size: 4,
63            reserved: 0,
64        }
65    }
66}
67
68impl LockResponse {
69    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
70        Ok(Self::read(&mut Cursor::new(buf))?)
71    }
72    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
73        let mut c = Cursor::new(Vec::new());
74        BinWrite::write(self, &mut c)?;
75        out.extend_from_slice(&c.into_inner());
76        Ok(())
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn request_round_trips() {
86        let r = LockRequest {
87            structure_size: 48,
88            lock_count: 2,
89            lock_sequence: 0,
90            file_id: FileId::new(1, 2),
91            locks: vec![
92                LockElement {
93                    offset: 0,
94                    length: 16,
95                    flags: LockElement::FLAG_EXCLUSIVE_LOCK,
96                    reserved: 0,
97                },
98                LockElement {
99                    offset: 0,
100                    length: 16,
101                    flags: LockElement::FLAG_UNLOCK,
102                    reserved: 0,
103                },
104            ],
105        };
106        let mut buf = Vec::new();
107        r.write_to(&mut buf).unwrap();
108        assert_eq!(LockRequest::parse(&buf).unwrap(), r);
109    }
110
111    #[test]
112    fn response_round_trips() {
113        let r = LockResponse::default();
114        let mut buf = Vec::new();
115        r.write_to(&mut buf).unwrap();
116        assert_eq!(LockResponse::parse(&buf).unwrap(), r);
117    }
118}