crafter 0.3.1

Packet-level network interaction for Rust tools and agents.
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Stateful packet transform contracts.

use super::record::{PacketRecord, TransformTrace};
use super::Result;

/// Stateful packet-record transform for sniffer and transmitter pipelines.
///
/// A transform consumes one [`PacketRecord`] and may emit zero, one, or many
/// packet records through the supplied callback. Implementers can keep state
/// across calls for decryption, defragmentation, reassembly, or protocol
/// decoding without changing the packet-shaped stream contract. Future WPA
/// decryptors belong here: they can observe handshake records, retain key
/// state, and emit decrypted packet records when payload frames become
/// decodable.
pub trait PacketTransform {
    /// Stable transform name used in diagnostics and transform traces.
    fn name(&self) -> &'static str;

    /// Transform one input record and emit zero or more output records.
    fn transform(
        &mut self,
        record: PacketRecord,
        emit: &mut dyn FnMut(PacketRecord) -> Result<()>,
    ) -> Result<()>;

    /// Run the transform and collect emitted records into a small buffer.
    fn transform_to_output(&mut self, record: PacketRecord) -> Result<TransformOutput>
    where
        Self: Sized,
    {
        let mut output = TransformOutput::new();
        self.transform(record, &mut |record| {
            output.push(record);
            Ok(())
        })?;
        Ok(output)
    }
}

/// Buffered output from one packet transform invocation.
#[derive(Debug, Clone, Default)]
pub struct TransformOutput {
    records: Vec<PacketRecord>,
}

impl TransformOutput {
    /// Create an empty transform output buffer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Buffered records in emission order.
    pub fn records(&self) -> &[PacketRecord] {
        &self.records
    }

    /// Number of buffered records.
    pub fn len(&self) -> usize {
        self.records.len()
    }

    /// Whether no records were emitted.
    pub fn is_empty(&self) -> bool {
        self.records.is_empty()
    }

    /// Append one output record.
    pub fn push(&mut self, record: PacketRecord) -> &mut Self {
        self.records.push(record);
        self
    }

    /// Append one output record through a callback-compatible method.
    pub fn emit(&mut self, record: PacketRecord) -> Result<()> {
        self.records.push(record);
        Ok(())
    }

    /// Remove all buffered output records.
    pub fn clear(&mut self) -> &mut Self {
        self.records.clear();
        self
    }

    /// Consume the output buffer and return records in emission order.
    pub fn into_records(self) -> Vec<PacketRecord> {
        self.records
    }
}

/// Test and fixture transform that emits each input record unchanged.
#[derive(Debug, Clone, Default)]
pub struct PassThroughTransform {
    input_count: usize,
    emitted_count: usize,
}

impl PassThroughTransform {
    /// Create a pass-through transform.
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of input records seen.
    pub const fn input_count(&self) -> usize {
        self.input_count
    }

    /// Number of records successfully emitted.
    pub const fn emitted_count(&self) -> usize {
        self.emitted_count
    }
}

impl PacketTransform for PassThroughTransform {
    fn name(&self) -> &'static str {
        "pass-through"
    }

    fn transform(
        &mut self,
        record: PacketRecord,
        emit: &mut dyn FnMut(PacketRecord) -> Result<()>,
    ) -> Result<()> {
        self.input_count += 1;
        emit(record)?;
        self.emitted_count += 1;
        Ok(())
    }
}

/// Test and fixture transform that drops every input record.
#[derive(Debug, Clone, Default)]
pub struct DropAllTransform {
    dropped_count: usize,
}

impl DropAllTransform {
    /// Create a drop-all transform.
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of input records dropped.
    pub const fn dropped_count(&self) -> usize {
        self.dropped_count
    }
}

impl PacketTransform for DropAllTransform {
    fn name(&self) -> &'static str {
        "drop-all"
    }

    fn transform(
        &mut self,
        _record: PacketRecord,
        _emit: &mut dyn FnMut(PacketRecord) -> Result<()>,
    ) -> Result<()> {
        self.dropped_count += 1;
        Ok(())
    }
}

/// Test and fixture transform that emits two copies of each input record.
#[derive(Debug, Clone, Default)]
pub struct DuplicateTransform {
    input_count: usize,
    emitted_count: usize,
}

impl DuplicateTransform {
    /// Create a duplicate transform.
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of input records seen.
    pub const fn input_count(&self) -> usize {
        self.input_count
    }

    /// Number of records successfully emitted.
    pub const fn emitted_count(&self) -> usize {
        self.emitted_count
    }
}

impl PacketTransform for DuplicateTransform {
    fn name(&self) -> &'static str {
        "duplicate"
    }

    fn transform(
        &mut self,
        record: PacketRecord,
        emit: &mut dyn FnMut(PacketRecord) -> Result<()>,
    ) -> Result<()> {
        self.input_count += 1;
        emit(record.clone())?;
        self.emitted_count += 1;
        emit(record)?;
        self.emitted_count += 1;
        Ok(())
    }
}

/// Test and fixture transform that appends a transform trace and emits one record.
#[derive(Debug, Clone)]
pub struct TraceAppendTransform {
    name: &'static str,
    note: Option<String>,
    input_count: usize,
    emitted_count: usize,
}

impl TraceAppendTransform {
    /// Create a trace-append transform with a stable trace name.
    pub const fn new(name: &'static str) -> Self {
        Self {
            name,
            note: None,
            input_count: 0,
            emitted_count: 0,
        }
    }

    /// Create the default trace-append helper.
    pub const fn trace_append() -> Self {
        Self::new("trace-append")
    }

    /// Set a note copied into appended transform traces.
    pub fn with_note(mut self, note: impl Into<String>) -> Self {
        self.note = Some(note.into());
        self
    }

    /// Number of input records seen.
    pub const fn input_count(&self) -> usize {
        self.input_count
    }

    /// Number of records successfully emitted.
    pub const fn emitted_count(&self) -> usize {
        self.emitted_count
    }
}

impl Default for TraceAppendTransform {
    fn default() -> Self {
        Self::trace_append()
    }
}

impl PacketTransform for TraceAppendTransform {
    fn name(&self) -> &'static str {
        self.name
    }

    fn transform(
        &mut self,
        mut record: PacketRecord,
        emit: &mut dyn FnMut(PacketRecord) -> Result<()>,
    ) -> Result<()> {
        self.input_count += 1;

        let mut trace = TransformTrace::new(self.name());
        if let Some(note) = &self.note {
            trace = trace.with_note(note.clone());
        }
        record.metadata_mut().push_transform_trace(trace);

        emit(record)?;
        self.emitted_count += 1;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::super::record::{BackendKind, PacketOrigin};
    use super::super::WireError;
    use super::*;
    use crate::Raw;

    fn record(payload: &'static str) -> PacketRecord {
        PacketRecord::new(Raw::from(payload))
            .with_origin(PacketOrigin::Generated)
            .with_backend(BackendKind::Memory)
            .with_interface("lo")
    }

    #[test]
    fn transform_output_buffers_records_in_order() {
        let mut output = TransformOutput::new();
        assert!(output.is_empty());

        output.push(record("one"));
        output.emit(record("two")).unwrap();

        assert_eq!(output.len(), 2);
        assert_eq!(output.records()[0].packet().summary(), "Raw(len=3)");
        assert_eq!(output.records()[1].packet().summary(), "Raw(len=3)");

        output.clear();
        assert!(output.is_empty());
    }

    #[test]
    fn pass_through_emits_one_record_unchanged() {
        let input = record("payload");
        let mut transform = PassThroughTransform::new();

        let output = transform.transform_to_output(input).unwrap();

        assert_eq!(transform.name(), "pass-through");
        assert_eq!(transform.input_count(), 1);
        assert_eq!(transform.emitted_count(), 1);
        assert_eq!(output.len(), 1);
        assert_eq!(output.records()[0].packet().summary(), "Raw(len=7)");
        assert_eq!(
            output.records()[0].metadata().origin(),
            PacketOrigin::Generated
        );
        assert_eq!(
            output.records()[0].metadata().backend(),
            &BackendKind::Memory
        );
        assert_eq!(output.records()[0].metadata().interface(), Some("lo"));
    }

    #[test]
    fn drop_all_emits_zero_records() {
        let mut transform = DropAllTransform::new();

        let output = transform.transform_to_output(record("payload")).unwrap();

        assert_eq!(transform.name(), "drop-all");
        assert_eq!(transform.dropped_count(), 1);
        assert!(output.is_empty());
    }

    #[test]
    fn duplicate_emits_two_records_per_input() {
        let mut transform = DuplicateTransform::new();

        let output = transform.transform_to_output(record("payload")).unwrap();

        assert_eq!(transform.name(), "duplicate");
        assert_eq!(transform.input_count(), 1);
        assert_eq!(transform.emitted_count(), 2);
        assert_eq!(output.len(), 2);
        assert_eq!(output.records()[0].packet().summary(), "Raw(len=7)");
        assert_eq!(output.records()[1].packet().summary(), "Raw(len=7)");
        assert_eq!(
            output.records()[0].metadata().origin(),
            PacketOrigin::Generated
        );
        assert_eq!(
            output.records()[1].metadata().backend(),
            &BackendKind::Memory
        );
    }

    #[test]
    fn trace_append_adds_transform_history() {
        let mut transform = TraceAppendTransform::new("decode-ip").with_note("decoded");

        let output = transform.transform_to_output(record("payload")).unwrap();

        assert_eq!(transform.name(), "decode-ip");
        assert_eq!(transform.input_count(), 1);
        assert_eq!(transform.emitted_count(), 1);
        assert_eq!(output.len(), 1);
        let traces = output.records()[0].metadata().transforms();
        assert_eq!(traces.len(), 1);
        assert_eq!(traces[0].name(), "decode-ip");
        assert_eq!(traces[0].note(), Some("decoded"));
        assert_eq!(output.records()[0].packet().summary(), "Raw(len=7)");
    }

    #[test]
    fn packet_transform_is_object_safe() {
        let mut transform: Box<dyn PacketTransform> = Box::new(PassThroughTransform::new());
        let mut output = TransformOutput::new();

        transform
            .transform(record("payload"), &mut |record| output.emit(record))
            .unwrap();

        assert_eq!(transform.name(), "pass-through");
        assert_eq!(output.len(), 1);
        assert_eq!(output.records()[0].packet().summary(), "Raw(len=7)");
    }

    #[test]
    fn transform_propagates_emitter_errors() {
        let mut transform = DuplicateTransform::new();

        let err = transform
            .transform(record("payload"), &mut |_record| {
                Err(WireError::transform("collector", "closed"))
            })
            .unwrap_err();

        assert_eq!(err.to_string(), "wire transform 'collector' failed: closed");
        assert_eq!(transform.input_count(), 1);
        assert_eq!(transform.emitted_count(), 0);
    }
}