1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use crate::{NTVersion, Result};
use bytes::{Buf, BufMut, BytesMut};
use crate::ext::*;
use crate::packets::types::{EntryType, EntryValue};

pub mod types;

pub trait Packet: Send + Sync {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()>;
    fn deserialize(buf: &mut dyn Buf) -> Result<(Self, usize)>
        where Self: Sized;
}

#[derive(Clone, Debug)]
pub struct ClientHello {
    pub version: NTVersion,
    pub name: String,
}

impl ClientHello {
    pub fn new(version: NTVersion, name: String) -> ClientHello {
        if version == NTVersion::V2 {
            panic!("V2 is not supported");
        }

        ClientHello {
            version,
            name,
        }
    }
}

impl Packet for ClientHello {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x01);
        buf.put_u16_be(self.version as u16);
        self.name.serialize(buf)?; // cant use the method on buf because dum
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let version = buf.read_u16_be()?;
        let (name, name_bytes) = String::deserialize(buf)?;
        unsafe {
            Ok((ClientHello {
                version: std::mem::transmute::<u16, NTVersion>(version), //TODO: oh god change it
                name,
            }, 2 + name_bytes))
        }
    }
}

#[derive(Debug, Clone)]
pub struct ServerHello {
    pub flags: u8,
    pub name: String,
}

impl ServerHello {
    pub fn new(flags: u8, name: String) -> ServerHello {
        ServerHello {
            flags,
            name,
        }
    }
}

impl Packet for ServerHello {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x04);
        buf.put_u8(self.flags);
        self.name.serialize(buf)?;
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let flags = buf.read_u8()?;
        let (name, bytes) = String::deserialize(buf)?;
        Ok((ServerHello::new(flags, name), 1 + bytes))
    }
}

#[derive(Clone, Debug)]
pub struct EntryAssignment {
    pub entry_name: String,
    pub entry_type: EntryType,
    pub entry_id: u16,
    pub entry_seqnum: u16,
    pub entry_flags: u8,
    pub entry_value: EntryValue,
}

impl EntryAssignment {
    pub fn new(entry_name: String, entry_type: EntryType, entry_id: u16,
               entry_seqnum: u16, entry_flags: u8, entry_value: EntryValue) -> EntryAssignment {
        EntryAssignment {
            entry_name,
            entry_type,
            entry_id,
            entry_seqnum,
            entry_flags,
            entry_value,
        }
    }
}

impl Packet for EntryAssignment {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x10);
        self.entry_name.serialize(buf)?;
        self.entry_type.serialize(buf)?;
        buf.put_u16_be(self.entry_id);
        buf.put_u16_be(self.entry_seqnum);
        buf.put_u8(self.entry_flags);
        self.entry_type.write_value(&self.entry_value, buf)?;
        Ok(())
    }

    fn deserialize(buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let (s, mut read) = String::deserialize(buf)?;
        let (ty, bytes) = EntryType::deserialize(buf)?;
        read += bytes;
        let entry_id = buf.get_u16_be();
        let entry_seqnum = buf.get_u16_be();
        let flags = buf.get_u8();
        let (value, bytes) = ty.read_value(buf)?;
        read += bytes;
        Ok((EntryAssignment::new(s, ty, entry_id, entry_seqnum, flags, value), 5 + read))
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ClientHelloComplete;

impl Packet for ClientHelloComplete {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x05);
        Ok(())
    }

    fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        Ok((ClientHelloComplete, 0))
    }
}

#[derive(Copy, Clone, Debug)]
pub struct ServerHelloComplete;

impl Packet for ServerHelloComplete {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x03);
        Ok(())
    }

    fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        Ok((ServerHelloComplete, 0))
    }
}

pub struct KeepAlive;

impl Packet for KeepAlive {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x00);
        Ok(())
    }

    fn deserialize(_buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        Ok((KeepAlive, 0))
    }
}

#[derive(Debug, Clone)]
pub struct ProtocolVersionUnsupported {
    pub supported_version: u16,
}

impl Packet for ProtocolVersionUnsupported {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x02);
        buf.put_u16_be(self.supported_version);
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let supported_version = buf.read_u16_be()?;
        Ok((ProtocolVersionUnsupported { supported_version }, 2))
    }
}

#[derive(Debug, Clone)]
pub struct EntryUpdate {
    pub entry_id: u16,
    pub entry_seqnum: u16,
    pub entry_type: EntryType,
    pub entry_value: EntryValue
}

impl EntryUpdate {
    pub fn new(entry_id: u16, entry_seqnum: u16, entry_type: EntryType, entry_value: EntryValue) -> EntryUpdate {
        EntryUpdate {
            entry_id,
            entry_seqnum,
            entry_type,
            entry_value
        }
    }
}

impl Packet for EntryUpdate {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x11);
        buf.put_u16_be(self.entry_id);
        buf.put_u16_be(self.entry_seqnum);
        self.entry_type.serialize(buf)?;
        self.entry_type.write_value(&self.entry_value, buf)?;
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let entry_id = buf.read_u16_be()?;
        let entry_seqnum = buf.read_u16_be()?;
        let (entry_type, type_bytes) = EntryType::deserialize(buf)?;
        let (entry_value, value_bytes) = entry_type.read_value(buf)?;

        Ok((EntryUpdate::new(entry_id, entry_seqnum, entry_type, entry_value), 2 + 2 + type_bytes + value_bytes))
    }
}

#[derive(Debug, Copy, Clone)]
pub struct EntryFlagsUpdate {
    pub entry_id: u16,
    pub entry_flags: u8
}

impl EntryFlagsUpdate {
    pub fn new(entry_id: u16, entry_flags: u8) -> EntryFlagsUpdate {
        EntryFlagsUpdate {
            entry_id,
            entry_flags
        }
    }
}

impl Packet for EntryFlagsUpdate {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x12);
        buf.put_u16_be(self.entry_id);
        buf.put_u8(self.entry_flags);
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let entry_id = buf.read_u16_be()?;
        let entry_flags = buf.get_u8();
        Ok((EntryFlagsUpdate { entry_id, entry_flags }, 3))
    }
}

#[derive(Debug, Copy, Clone)]
pub struct EntryDelete {
    pub entry_id: u16,
}

impl EntryDelete {
    pub fn new(entry_id: u16) -> EntryDelete {
        EntryDelete {
            entry_id
        }
    }
}

impl Packet for EntryDelete {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x13);
        buf.put_u16_be(self.entry_id);
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let entry_id = buf.read_u16_be()?;
        Ok((EntryDelete { entry_id }, 2))
    }
}

#[derive(Debug, Copy, Clone)]
pub struct ClearAllEntries {
    pub magic: u32
}

impl ClearAllEntries {
    pub const fn new() -> ClearAllEntries {
        ClearAllEntries {
            magic: 0xD06CB27A
        }
    }

    pub fn is_valid(&self) -> bool {
        self.magic == 0xD06CB27A
    }
}

impl Packet for ClearAllEntries {
    fn serialize(&self, buf: &mut BytesMut) -> Result<()> {
        buf.put_u8(0x14);
        buf.put_u32_be(self.magic);
        Ok(())
    }

    fn deserialize(mut buf: &mut dyn Buf) -> Result<(Self, usize)> where Self: Sized {
        let magic = buf.read_u32_be()?;
        Ok((ClearAllEntries { magic }, 4))
    }
}