smb_server/proto/messages/
change_notify.rs1use binrw::{BinRead, BinWrite, binrw};
5use std::io::Cursor;
6
7use super::create::FileId;
8use crate::proto::error::ProtoResult;
9
10pub const FILE_NOTIFY_CHANGE_FILE_NAME: u32 = 0x0000_0001;
12pub const FILE_NOTIFY_CHANGE_DIR_NAME: u32 = 0x0000_0002;
13pub const FILE_NOTIFY_CHANGE_ATTRIBUTES: u32 = 0x0000_0004;
14pub const FILE_NOTIFY_CHANGE_SIZE: u32 = 0x0000_0008;
15pub const FILE_NOTIFY_CHANGE_LAST_WRITE: u32 = 0x0000_0010;
16pub const FILE_NOTIFY_CHANGE_LAST_ACCESS: u32 = 0x0000_0020;
17pub const FILE_NOTIFY_CHANGE_CREATION: u32 = 0x0000_0040;
18pub const FILE_NOTIFY_CHANGE_EA: u32 = 0x0000_0080;
19pub const FILE_NOTIFY_CHANGE_SECURITY: u32 = 0x0000_0100;
20pub const FILE_NOTIFY_CHANGE_STREAM_NAME: u32 = 0x0000_0200;
21pub const FILE_NOTIFY_CHANGE_STREAM_SIZE: u32 = 0x0000_0400;
22pub const FILE_NOTIFY_CHANGE_STREAM_WRITE: u32 = 0x0000_0800;
23pub const FILE_NOTIFY_VALID_MASK: u32 = 0x0000_0FFF;
25
26pub const FILE_ACTION_ADDED: u32 = 1;
28pub const FILE_ACTION_REMOVED: u32 = 2;
29pub const FILE_ACTION_MODIFIED: u32 = 3;
30pub const FILE_ACTION_RENAMED_OLD_NAME: u32 = 4;
31pub const FILE_ACTION_RENAMED_NEW_NAME: u32 = 5;
32
33const ENTRY_HEADER_LEN: usize = 12;
35
36#[binrw]
37#[brw(little)]
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct ChangeNotifyRequest {
40 pub structure_size: u16,
41 pub flags: u16,
42 pub output_buffer_length: u32,
43 pub file_id: FileId,
44 pub completion_filter: u32,
45 pub reserved: u32,
46}
47
48impl ChangeNotifyRequest {
49 pub const FLAG_WATCH_TREE: u16 = 0x0001;
51
52 pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
53 Ok(Self::read(&mut Cursor::new(buf))?)
54 }
55 pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
56 let mut c = Cursor::new(Vec::new());
57 BinWrite::write(self, &mut c)?;
58 out.extend_from_slice(&c.into_inner());
59 Ok(())
60 }
61}
62
63#[binrw]
64#[brw(little)]
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct ChangeNotifyResponse {
67 pub structure_size: u16,
68 pub output_buffer_offset: u16,
69 pub output_buffer_length: u32,
70 #[br(parse_with = super::read_buf, args(output_buffer_length as usize))]
71 pub buffer: Vec<u8>,
72}
73
74impl ChangeNotifyResponse {
75 pub const BUFFER_OFFSET: u16 = 0x48;
77
78 pub(crate) fn encode(buffer: &[u8]) -> Vec<u8> {
81 let mut out = Vec::with_capacity(8 + buffer.len());
82 out.extend_from_slice(&9u16.to_le_bytes());
83 out.extend_from_slice(&Self::BUFFER_OFFSET.to_le_bytes());
84 out.extend_from_slice(&(buffer.len() as u32).to_le_bytes());
85 out.extend_from_slice(buffer);
86 out
87 }
88
89 pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
90 Ok(Self::read(&mut Cursor::new(buf))?)
91 }
92 pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
93 let mut c = Cursor::new(Vec::new());
94 BinWrite::write(self, &mut c)?;
95 out.extend_from_slice(&c.into_inner());
96 Ok(())
97 }
98}
99
100pub(crate) fn push_entry(buf: &mut Vec<u8>, last: &mut Option<usize>, action: u32, name: &[u16]) {
105 let name_len = name.len() * 2;
106 if let Some(at) = *last {
107 let same_name = buf[at + 8..at + 12] == (name_len as u32).to_le_bytes()
108 && buf[at + ENTRY_HEADER_LEN..]
109 .chunks_exact(2)
110 .zip(name)
111 .all(|(unit, &c)| unit == c.to_le_bytes());
112 if same_name && buf[at + 4..at + 8] == action.to_le_bytes() {
113 return;
114 }
115 let next = (buf.len() - at) as u32;
116 buf[at..at + 4].copy_from_slice(&next.to_le_bytes());
117 }
118 let start = buf.len();
119 buf.extend_from_slice(&0u32.to_le_bytes());
120 buf.extend_from_slice(&action.to_le_bytes());
121 buf.extend_from_slice(&(name_len as u32).to_le_bytes());
122 buf.extend(name.iter().flat_map(|c| c.to_le_bytes()));
123 buf.resize(start + (ENTRY_HEADER_LEN + name_len).next_multiple_of(4), 0);
124 *last = Some(start);
125}
126
127pub(crate) fn entries_len(buf: &[u8], last: Option<usize>) -> usize {
130 match last {
131 Some(at) => {
132 let name_len = u32::from_le_bytes(buf[at + 8..at + 12].try_into().expect("4 bytes"));
133 at + ENTRY_HEADER_LEN + name_len as usize
134 }
135 None => 0,
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use super::*;
142
143 #[test]
144 fn request_round_trips() {
145 let r = ChangeNotifyRequest {
146 structure_size: 32,
147 flags: ChangeNotifyRequest::FLAG_WATCH_TREE,
148 output_buffer_length: 0x1000,
149 file_id: FileId::new(1, 2),
150 completion_filter: 0xFF,
151 reserved: 0,
152 };
153 let mut buf = Vec::new();
154 r.write_to(&mut buf).unwrap();
155 assert_eq!(ChangeNotifyRequest::parse(&buf).unwrap(), r);
156 }
157
158 #[test]
159 fn response_round_trips() {
160 let r = ChangeNotifyResponse {
161 structure_size: 9,
162 output_buffer_offset: 0x48,
163 output_buffer_length: 0,
164 buffer: vec![],
165 };
166 let mut buf = Vec::new();
167 r.write_to(&mut buf).unwrap();
168 assert_eq!(ChangeNotifyResponse::parse(&buf).unwrap(), r);
169 }
170
171 fn units(s: &str) -> Vec<u16> {
172 s.encode_utf16().collect()
173 }
174
175 fn entries(buf: &[u8]) -> Vec<(u32, u32, String)> {
178 let mut out = Vec::new();
179 let mut at = 0;
180 loop {
181 let field = |o: usize| u32::from_le_bytes(buf[at + o..at + o + 4].try_into().unwrap());
182 let (next, action, len) = (field(0), field(4), field(8) as usize);
183 assert_eq!(len % 2, 0);
184 let name: Vec<u16> = buf[at + 12..at + 12 + len]
185 .chunks_exact(2)
186 .map(|c| u16::from_le_bytes([c[0], c[1]]))
187 .collect();
188 out.push((next, action, String::from_utf16(&name).unwrap()));
189 if next == 0 {
190 assert_eq!(at + 12 + len, buf.len(), "the last entry is not padded");
191 return out;
192 }
193 assert_eq!(next % 4, 0);
194 assert_eq!(next as usize, (12 + len).next_multiple_of(4));
195 at += next as usize;
196 }
197 }
198
199 #[test]
200 fn entries_chain_and_pad_to_four_bytes() {
201 let mut buf = Vec::new();
202 let mut last = None;
203 assert_eq!(entries_len(&buf, last), 0);
204 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("ab"));
205 assert_eq!(buf.len(), 16);
206 assert_eq!(entries_len(&buf, last), 16);
207 push_entry(&mut buf, &mut last, FILE_ACTION_REMOVED, &units("odd"));
208 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("d\\été"));
209 assert_eq!(buf.len() % 4, 0);
210 let len = entries_len(&buf, last);
211 assert_eq!(len, 16 + 20 + 12 + 10);
212 assert_eq!(
213 entries(&buf[..len]),
214 [
215 (16, FILE_ACTION_ADDED, "ab".to_string()),
216 (20, FILE_ACTION_REMOVED, "odd".to_string()),
217 (0, FILE_ACTION_ADDED, "d\\été".to_string()),
218 ]
219 );
220 }
221
222 #[test]
223 fn only_adjacent_duplicates_collapse() {
224 let mut buf = Vec::new();
225 let mut last = None;
226 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("x"));
227 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("x"));
228 push_entry(&mut buf, &mut last, FILE_ACTION_REMOVED, &units("x"));
229 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("x"));
230 push_entry(&mut buf, &mut last, FILE_ACTION_ADDED, &units("xy"));
231 let len = entries_len(&buf, last);
232 let found: Vec<_> = entries(&buf[..len])
233 .into_iter()
234 .map(|(_, action, name)| (action, name))
235 .collect();
236 assert_eq!(
237 found,
238 [
239 (FILE_ACTION_ADDED, "x".to_string()),
240 (FILE_ACTION_REMOVED, "x".to_string()),
241 (FILE_ACTION_ADDED, "x".to_string()),
242 (FILE_ACTION_ADDED, "xy".to_string()),
243 ]
244 );
245 }
246
247 #[test]
248 fn responses_without_entries_are_8_bytes() {
249 let empty = ChangeNotifyResponse::encode(&[]);
250 assert_eq!(empty, [9, 0, 0x48, 0, 0, 0, 0, 0]);
251 assert_eq!(ChangeNotifyResponse::parse(&empty).unwrap().buffer, []);
252 let full = ChangeNotifyResponse::encode(&[1, 2, 3]);
253 let parsed = ChangeNotifyResponse::parse(&full).unwrap();
254 assert_eq!(parsed.output_buffer_length, 3);
255 assert_eq!(parsed.buffer, [1, 2, 3]);
256 }
257}