1use crate::ext::*;
2use crate::packets::types::{EntryType, EntryValue};
3use crate::{NTVersion, Result};
4use bytes::{Buf, BufMut, BytesMut};
5
6pub mod types;
7
8pub trait Packet: Send + Sync {
9 fn serialize(&self, buf: &mut BytesMut) -> Result<()>;
10 fn deserialize(buf: &mut dyn Buf) -> Result<(Self, usize)>
11 where
12 Self: Sized;
13}
14
15#[derive(Clone, Debug)]
16pub struct ClientHello {
17 pub version: NTVersion,
18 pub name: String,
19}
20
21impl ClientHello {
22 pub fn new(version: NTVersion, name: String) -> ClientHello {
23 if version == NTVersion::V2 {
24 panic!("V2 is not supported");
25 }
26
27 ClientHello { version, name }
28 }
29}
30
31impl Packet for ClientHello {
32 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
33 buf.put_u8(0x01);
34 buf.put_u16(self.version as u16);
35 self.name.serialize(buf)?; Ok(())
37 }
38
39 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
40 where
41 Self: Sized,
42 {
43 let version = NTVersion::from_u16(buf.read_u16_be()?)?;
44 let (name, name_bytes) = String::deserialize(buf)?;
45 Ok((ClientHello { version, name }, 2 + name_bytes))
46 }
47}
48
49#[derive(Debug, Clone)]
50pub struct ServerHello {
51 pub flags: u8,
52 pub name: String,
53}
54
55impl ServerHello {
56 pub fn new(flags: u8, name: String) -> ServerHello {
57 ServerHello { flags, name }
58 }
59}
60
61impl Packet for ServerHello {
62 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
63 buf.put_u8(0x04);
64 buf.put_u8(self.flags);
65 self.name.serialize(buf)?;
66 Ok(())
67 }
68
69 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
70 where
71 Self: Sized,
72 {
73 let flags = buf.read_u8()?;
74 let (name, bytes) = String::deserialize(buf)?;
75 Ok((ServerHello::new(flags, name), 1 + bytes))
76 }
77}
78
79#[derive(Clone, Debug)]
80pub struct EntryAssignment {
81 pub entry_name: String,
82 pub entry_type: EntryType,
83 pub entry_id: u16,
84 pub entry_seqnum: u16,
85 pub entry_flags: u8,
86 pub entry_value: EntryValue,
87}
88
89impl EntryAssignment {
90 pub fn new(
91 entry_name: String,
92 entry_type: EntryType,
93 entry_id: u16,
94 entry_seqnum: u16,
95 entry_flags: u8,
96 entry_value: EntryValue,
97 ) -> EntryAssignment {
98 EntryAssignment {
99 entry_name,
100 entry_type,
101 entry_id,
102 entry_seqnum,
103 entry_flags,
104 entry_value,
105 }
106 }
107}
108
109impl Packet for EntryAssignment {
110 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
111 buf.put_u8(0x10);
112 self.entry_name.serialize(buf)?;
113 self.entry_type.serialize(buf)?;
114 buf.put_u16(self.entry_id);
115 buf.put_u16(self.entry_seqnum);
116 buf.put_u8(self.entry_flags);
117 self.entry_type.write_value(&self.entry_value, buf)?;
118 Ok(())
119 }
120
121 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
122 where
123 Self: Sized,
124 {
125 let (s, mut read) = String::deserialize(buf)?;
126 let (ty, bytes) = EntryType::deserialize(buf)?;
127 read += bytes;
128 let entry_id = buf.read_u16_be()?;
129 let entry_seqnum = buf.read_u16_be()?;
130 let flags = buf.read_u8()?;
131 let (value, bytes) = ty.read_value(buf)?;
132 read += bytes;
133 Ok((
134 EntryAssignment::new(s, ty, entry_id, entry_seqnum, flags, value),
135 5 + read,
136 ))
137 }
138}
139
140#[derive(Copy, Clone, Debug)]
141pub struct ClientHelloComplete;
142
143impl Packet for ClientHelloComplete {
144 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
145 buf.put_u8(0x05);
146 Ok(())
147 }
148
149 fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)>
150 where
151 Self: Sized,
152 {
153 Ok((ClientHelloComplete, 0))
154 }
155}
156
157#[derive(Copy, Clone, Debug)]
158pub struct ServerHelloComplete;
159
160impl Packet for ServerHelloComplete {
161 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
162 buf.put_u8(0x03);
163 Ok(())
164 }
165
166 fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)>
167 where
168 Self: Sized,
169 {
170 Ok((ServerHelloComplete, 0))
171 }
172}
173
174pub struct KeepAlive;
175
176impl Packet for KeepAlive {
177 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
178 buf.put_u8(0x00);
179 Ok(())
180 }
181
182 fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)>
183 where
184 Self: Sized,
185 {
186 Ok((KeepAlive, 0))
187 }
188}
189
190#[derive(Debug, Clone)]
191pub struct ProtocolVersionUnsupported {
192 pub supported_version: u16,
193}
194
195impl ProtocolVersionUnsupported {
196 pub fn new(supported_version: NTVersion) -> ProtocolVersionUnsupported {
197 ProtocolVersionUnsupported {
198 supported_version: supported_version as _,
199 }
200 }
201}
202
203impl Packet for ProtocolVersionUnsupported {
204 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
205 buf.put_u8(0x02);
206 buf.put_u16(self.supported_version);
207 Ok(())
208 }
209
210 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
211 where
212 Self: Sized,
213 {
214 let supported_version = buf.read_u16_be()?;
215 Ok((ProtocolVersionUnsupported { supported_version }, 2))
216 }
217}
218
219#[derive(Debug, Clone)]
220pub struct EntryUpdate {
221 pub entry_id: u16,
222 pub entry_seqnum: u16,
223 pub entry_type: EntryType,
224 pub entry_value: EntryValue,
225}
226
227impl EntryUpdate {
228 pub fn new(
229 entry_id: u16,
230 entry_seqnum: u16,
231 entry_type: EntryType,
232 entry_value: EntryValue,
233 ) -> EntryUpdate {
234 EntryUpdate {
235 entry_id,
236 entry_seqnum,
237 entry_type,
238 entry_value,
239 }
240 }
241}
242
243impl Packet for EntryUpdate {
244 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
245 buf.put_u8(0x11);
246 buf.put_u16(self.entry_id);
247 buf.put_u16(self.entry_seqnum);
248 self.entry_type.serialize(buf)?;
249 self.entry_type.write_value(&self.entry_value, buf)?;
250 Ok(())
251 }
252
253 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
254 where
255 Self: Sized,
256 {
257 let entry_id = buf.read_u16_be()?;
258 let entry_seqnum = buf.read_u16_be()?;
259 let (entry_type, type_bytes) = EntryType::deserialize(buf)?;
260 let (entry_value, value_bytes) = entry_type.read_value(buf)?;
261
262 Ok((
263 EntryUpdate::new(entry_id, entry_seqnum, entry_type, entry_value),
264 2 + 2 + type_bytes + value_bytes,
265 ))
266 }
267}
268
269#[derive(Debug, Copy, Clone)]
270pub struct EntryFlagsUpdate {
271 pub entry_id: u16,
272 pub entry_flags: u8,
273}
274
275impl EntryFlagsUpdate {
276 pub fn new(entry_id: u16, entry_flags: u8) -> EntryFlagsUpdate {
277 EntryFlagsUpdate {
278 entry_id,
279 entry_flags,
280 }
281 }
282}
283
284impl Packet for EntryFlagsUpdate {
285 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
286 buf.put_u8(0x12);
287 buf.put_u16(self.entry_id);
288 buf.put_u8(self.entry_flags);
289 Ok(())
290 }
291
292 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
293 where
294 Self: Sized,
295 {
296 let entry_id = buf.read_u16_be()?;
297 let entry_flags = buf.get_u8();
298 Ok((
299 EntryFlagsUpdate {
300 entry_id,
301 entry_flags,
302 },
303 3,
304 ))
305 }
306}
307
308#[derive(Debug, Copy, Clone)]
309pub struct EntryDelete {
310 pub entry_id: u16,
311}
312
313impl EntryDelete {
314 pub fn new(entry_id: u16) -> EntryDelete {
315 EntryDelete { entry_id }
316 }
317}
318
319impl Packet for EntryDelete {
320 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
321 buf.put_u8(0x13);
322 buf.put_u16(self.entry_id);
323 Ok(())
324 }
325
326 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
327 where
328 Self: Sized,
329 {
330 let entry_id = buf.read_u16_be()?;
331 Ok((EntryDelete { entry_id }, 2))
332 }
333}
334
335#[derive(Debug, Copy, Clone)]
336pub struct ClearAllEntries {
337 pub magic: u32,
338}
339
340impl ClearAllEntries {
341 pub const fn new() -> ClearAllEntries {
342 ClearAllEntries {
343 magic: 0xD0_6C_B2_7A,
344 }
345 }
346
347 pub fn is_valid(self) -> bool {
348 self.magic == 0xD0_6C_B2_7A
349 }
350}
351
352impl Packet for ClearAllEntries {
353 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
354 buf.put_u8(0x14);
355 buf.put_u32(self.magic);
356 Ok(())
357 }
358
359 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
360 where
361 Self: Sized,
362 {
363 let magic = buf.read_u32_be()?;
364 Ok((ClearAllEntries { magic }, 4))
365 }
366}
367
368#[derive(Debug, Clone)]
369pub struct RpcExecute {
370 pub entry_id: u16,
371 pub unique_id: u16,
372 pub parameter: Vec<u8>,
373}
374
375impl RpcExecute {
376 pub fn new(entry_id: u16, unique_id: u16, parameter: Vec<u8>) -> RpcExecute {
377 RpcExecute {
378 entry_id,
379 unique_id,
380 parameter,
381 }
382 }
383}
384
385impl Packet for RpcExecute {
386 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
387 buf.put_u8(0x20);
388 buf.put_u16(self.entry_id);
389 buf.put_u16(self.unique_id);
390 Packet::serialize(&self.parameter, buf)
391 }
392
393 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
394 where
395 Self: Sized,
396 {
397 let entry_id = buf.read_u16_be()?;
398 let unique_id = buf.read_u16_be()?;
399 let (parameter, len) = Packet::deserialize(buf)?;
400 Ok((
401 RpcExecute {
402 entry_id,
403 unique_id,
404 parameter,
405 },
406 4 + len,
407 ))
408 }
409}
410
411#[derive(Debug, Clone)]
412pub struct RpcResponse {
413 pub entry_id: u16,
414 pub unique_id: u16,
415 pub result: Vec<u8>,
416}
417
418impl RpcResponse {
419 pub fn new(entry_id: u16, unique_id: u16, result: Vec<u8>) -> RpcResponse {
420 RpcResponse {
421 entry_id,
422 unique_id,
423 result,
424 }
425 }
426}
427
428impl Packet for RpcResponse {
429 fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
430 buf.put_u8(0x21);
431 buf.put_u16(self.entry_id);
432 buf.put_u16(self.unique_id);
433 Packet::serialize(&self.result, buf)
434 }
435
436 fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)>
437 where
438 Self: Sized,
439 {
440 let entry_id = buf.read_u16_be()?;
441 let unique_id = buf.read_u16_be()?;
442 let (result, len) = Packet::deserialize(buf)?;
443 Ok((
444 RpcResponse {
445 entry_id,
446 unique_id,
447 result,
448 },
449 4 + len,
450 ))
451 }
452}