mymq 0.1.0

Broker and message queues
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
use chrono::NaiveDateTime;
use log::{debug, error, info, trace};
use structopt::StructOpt;

use std::{cmp, collections::BTreeMap, mem, net, path, time};

use crate::{Opt, Result, SubCommand};
use mymq::{netw, util, v5};

const PCAP_TIMEOUT: time::Duration = time::Duration::from_millis(1000);
const MQTT_SIGNATURE: [u8; 6] = [0, 4, 77, 81, 84, 84];
const MAX_PACKET_SIZE: u32 = 10 * 1024 * 1024; // 10 MB

#[derive(Clone, StructOpt)]
pub struct Dump {
    #[structopt(short = "w")]
    write_file: Option<path::PathBuf>,

    #[structopt(short = "a")]
    append_file: Option<path::PathBuf>,

    #[structopt(short = "r")]
    read_file: Option<path::PathBuf>,

    #[structopt(short = "t", default_value = "0")]
    time: u64,

    #[structopt(long = "precis", default_value = "micro")]
    precision: String,

    #[structopt(long = "promisc")]
    promisc: bool,

    #[structopt(long = "devices")]
    devices: bool,

    #[structopt(long = "inp")]
    inp: bool,

    #[structopt(long = "out")]
    out: bool,

    #[structopt(long = "eth")]
    eth: bool,

    #[structopt(long = "ip")]
    ip: bool,

    #[structopt(long = "tcp")]
    tcp: bool,

    #[structopt(long = "port", default_value = "0")]
    port: u16,

    device: Option<String>,
}

pub fn run(opts: Opt) -> Result<()> {
    let dump = match &opts.subcmd {
        SubCommand::Dump(dump) => dump.clone(),
        _ => unreachable!(),
    };

    if dump.devices {
        list_devices(&opts, dump)?;
    } else if let Some(_) = &dump.read_file {
        let _capture = run_offline(opts.clone(), dump)?;
    } else if let Some(_) = &dump.device {
        let mut capture = run_active(opts.clone(), dump)?;
        println!("capture stats {:?}", capture.stats().unwrap());
    } else {
        list_connected_devices(&opts, dump)?;
    }

    Ok(())
}

fn run_offline(_opts: Opt, dump: Dump) -> Result<pcap::Capture<pcap::Offline>> {
    let capture = pcap::Capture::<pcap::Offline>::from_file_with_precision(
        &dump.read_file.unwrap(),
        into_precision(&dump.precision)?,
    )
    .map_err(|e| e.to_string())?;

    println!("pcap version:{:?}", capture.version());

    Ok(capture)
}

fn run_active(_opts: Opt, dump: Dump) -> Result<pcap::Capture<pcap::Active>> {
    // TODO: pcap::TimestampType
    // TODO: pcap::Capture::immediate_mode
    // TODO: pcap::Capture::rfmon
    // TODO: pcap::Capture::buffer_size
    // TODO: pcap::Capture::snaplen

    let device_name = dump.device.clone().unwrap();
    let capture = {
        let device = find_device(&device_name)?;
        let capture = pcap::Capture::<pcap::Inactive>::from_device(device)
            .map_err(|e| e.to_string())?
            .timeout(PCAP_TIMEOUT.as_millis() as i32)
            .promisc(dump.promisc)
            .precision(into_precision(&dump.precision)?);
        capture.open().map_err(|e| e.to_string())?
    };

    let save_file = match dump.write_file.clone() {
        Some(wf) => Some(capture.savefile(&wf).map_err(|e| e.to_string())?),
        None => match dump.append_file.clone() {
            Some(af) => Some(capture.savefile_append(&af).map_err(|e| e.to_string())?),
            None => None,
        },
    };

    if dump.inp {
        capture.direction(pcap::Direction::In).map_err(|e| e.to_string())?;
    } else if dump.out {
        capture.direction(pcap::Direction::Out).map_err(|e| e.to_string())?;
    } else {
        capture.direction(pcap::Direction::InOut).map_err(|e| e.to_string())?;
    }

    info!("capture link {}", capture.get_datalink().get_description().unwrap());
    let mut capture_iter = {
        let codec = Codec {
            dump: dump.clone(),
            save_file,
            link_type: capture.get_datalink(),
            mac: dump.device_mac()?,
            inp_conns: BTreeMap::default(),
            out_conns: BTreeMap::default(),
        };
        capture.iter(codec)
    };

    let deadline = time::Instant::now() + time::Duration::from_secs(dump.time);
    loop {
        match capture_iter.next() {
            Some(_pkt) if dump.time > 0 && time::Instant::now() > deadline => break,
            Some(Err(pcap::Error::TimeoutExpired)) => {
                trace!("timeout({:?}) expired from pcap", PCAP_TIMEOUT);
            }
            Some(pkt) => {
                let pkt = pkt.map_err(|e| e.to_string())?;
                pkt.to_string().map(|s| println!("{}", s));
            }
            None => break,
        }
    }
    info!("capture exiting");

    let capture = {
        let device = find_device(&dump.device.unwrap())?;
        let empty = pcap::Capture::<pcap::Inactive>::from_device(device)
            .map_err(|e| e.to_string())?
            .open()
            .map_err(|e| e.to_string())?;
        mem::replace(capture_iter.capture_mut(), empty)
    };
    Ok(capture)
}

fn find_device(name: &str) -> Result<pcap::Device> {
    for device in pcap::Device::list().map_err(|e| e.to_string())?.into_iter() {
        if device.name == name {
            return Ok(device);
        }
    }

    Err(format!("cannot find device {}", name))
}

fn list_devices(opts: &Opt, _dump: Dump) -> Result<()> {
    let devices = pcap::Device::list().map_err(|e| e.to_string())?;
    util::make_table(&devices).print_tty(!opts.force_color);

    Ok(())
}

fn list_connected_devices(opts: &Opt, _dump: Dump) -> Result<()> {
    let devices: Vec<pcap::Device> = pcap::Device::list()
        .map_err(|e| e.to_string())?
        .into_iter()
        .filter(|d| d.flags.connection_status == pcap::ConnectionStatus::Connected)
        .collect();
    util::make_table(&devices).print_tty(!opts.force_color);

    Ok(())
}

fn into_precision(val: &str) -> Result<pcap::Precision> {
    match val {
        "micro" => Ok(pcap::Precision::Micro),
        "nano" => Ok(pcap::Precision::Nano),
        _ => Err(format!("invalid precision {}", val)),
    }
}

type Key = (net::Ipv4Addr, u16);
type Val = (Vec<u8>, v5::MQTTRead, bool);
struct Codec {
    dump: Dump,
    save_file: Option<pcap::Savefile>,
    link_type: pcap::Linktype,
    mac: pnet::util::MacAddr,
    inp_conns: BTreeMap<Key, Val>,
    out_conns: BTreeMap<Key, Val>,
}

impl Drop for Codec {
    fn drop(&mut self) {
        match &mut self.save_file {
            Some(file) => match file.flush() {
                Ok(()) => (),
                Err(err) => println!("error saving file: {}", err),
            },
            None => (),
        }
    }
}

impl pcap::PacketCodec for Codec {
    type Item = Packet;

    fn decode(&mut self, packet: pcap::Packet<'_>) -> Self::Item {
        self.save_file.as_mut().map(|f| f.write(&packet));

        if self.dump.eth {
            Packet::parse_l2(packet, self)
        } else if self.dump.ip {
            Packet::parse_l2(packet, self).parse_l3(self)
        } else if self.dump.tcp {
            Packet::parse_l2(packet, self).parse_l3(self).parse_l4(self)
        } else {
            Packet::parse_l2(packet, self).parse_l3(self).parse_l4(self).parse_mqtt(self)
        }
    }
}

impl Codec {
    fn read_packet(
        &mut self,
        key: &(net::Ipv4Addr, u16),
        dir: pcap::Direction,
    ) -> Result<Option<v5::Packet>> {
        use mymq::v5::MQTTRead::{Fin, Header, Init, Remain};

        let conns = match dir {
            pcap::Direction::In => &mut self.inp_conns,
            pcap::Direction::Out => &mut self.out_conns,
            _ => unreachable!(),
        };

        let (buf, pktr) = match conns.get_mut(&key) {
            Some((buf, pktr, _)) => (buf, pktr),
            None => return Ok(None),
        };

        let mut pr = mem::replace(pktr, v5::MQTTRead::default());
        let max_packet_size = pr.to_max_packet_size();
        let mut slice = buf.as_slice();
        // println!("read_packet key:{:?} dir:{:?} slice:{}", key, dir, slice.len());
        let res = loop {
            if slice.len() == 0 {
                break Ok(None);
            }
            pr = match pr.read(&mut slice) {
                Ok((val, true)) => {
                    pr = val;
                    break Ok(None);
                }
                Ok((val, false)) => val,
                Err(err) if err.kind() == mymq::ErrorKind::MalformedPacket => {
                    pr = v5::MQTTRead::new(max_packet_size);
                    break Err(format!("malformed packet from v5::MQTTRead"));
                }
                Err(err) if err.kind() == mymq::ErrorKind::ProtocolError => {
                    pr = v5::MQTTRead::new(max_packet_size);
                    break Err(format!("protocol error from v5::MQTTRead"));
                }
                Err(err) if err.kind() == mymq::ErrorKind::Disconnected => {
                    pr = v5::MQTTRead::new(max_packet_size);
                    break Ok(None);
                }
                Err(err) => unreachable!("unexpected error {}", err),
            };

            match &pr {
                Init { .. } | Header { .. } | Remain { .. } => (),
                Fin { .. } => match pr.parse() {
                    Ok(pkt) => {
                        pr = pr.reset();
                        break Ok(Some(pkt));
                    }
                    Err(err) => {
                        pr = pr.reset();
                        break Err(err.to_string());
                    }
                },
                v5::MQTTRead::None => unreachable!(),
            }
        };
        let m = buf.len() - slice.len();
        buf.drain(..m);

        // println!("read_packet pr:{:?} res:{:?}", pr, res);

        let _none = mem::replace(pktr, pr);
        res
    }
}

enum Packet {
    LoopBack(LoopBack),
    Ethernet(Ethernet),
    Ipv4(Ipv4),
    Tcp(Tcp),
    Mqtt(Mqtt),
    None,
}

impl Packet {
    fn to_string(&self) -> Option<String> {
        match self {
            Packet::Ethernet(pkt) if pkt.dir == pcap::Direction::In => {
                Some(format!("{} <- {} {}", pkt.ts, pkt.eth.source, pkt.eth.ethertype))
            }
            Packet::Ethernet(pkt) if pkt.dir == pcap::Direction::Out => Some(format!(
                "{} -> {} {}",
                pkt.ts, pkt.eth.destination, pkt.eth.ethertype
            )),
            Packet::Ethernet(_) => unreachable!(),
            Packet::Ipv4(pkt) if pkt.dir == pcap::Direction::In => {
                let ip = &pkt.ip;
                Some(format!(
                    concat!(
                        "{} <- {:15} ecn:{} frag:{} flags:{:x} ttl:{:3} ",
                        "id:{:5} proto:{}"
                    ),
                    pkt.ts,
                    ip.source,
                    ip.ecn,
                    ip.fragment_offset,
                    ip.flags,
                    ip.ttl,
                    ip.identification,
                    ip.next_level_protocol,
                ))
            }
            Packet::Ipv4(pkt) if pkt.dir == pcap::Direction::Out => {
                let ip = &pkt.ip;
                Some(format!(
                    concat!(
                        "{} -> {:15} ecn:{} frag:{} flags:{:x} ttl:{:3} ",
                        "id:{:5} proto:{}"
                    ),
                    pkt.ts,
                    ip.destination,
                    ip.ecn,
                    ip.fragment_offset,
                    ip.flags,
                    ip.ttl,
                    ip.identification,
                    ip.next_level_protocol,
                ))
            }
            Packet::Ipv4(pkt) => {
                let ip = &pkt.ip;
                Some(format!(
                    concat!(
                        "{} ** ecn:{} frag:{} flags:{:x} ttl:{:3} ",
                        "id:{:5} proto:{}"
                    ),
                    pkt.ts,
                    ip.ecn,
                    ip.fragment_offset,
                    ip.flags,
                    ip.ttl,
                    ip.identification,
                    ip.next_level_protocol,
                ))
            }
            Packet::Tcp(pkt) if pkt.dir == pcap::Direction::In => {
                let tcp = &pkt.tcp;
                let payload = &tcp.payload;
                let remote = format!("{}:{}", pkt.ip_remote, tcp.source);
                let n = cmp::min(payload.len(), 4);
                Some(format!(
                    "{} <- {:19} port:{} flags:{:2x} payload:{}({:?})",
                    pkt.ts,
                    remote,
                    tcp.destination,
                    tcp.flags,
                    payload.len(),
                    &payload[..n],
                ))
            }
            Packet::Tcp(pkt) if pkt.dir == pcap::Direction::Out => {
                let tcp = &pkt.tcp;
                let payload = &tcp.payload;
                let remote = format!("{}:{}", pkt.ip_remote, tcp.destination);
                let n = cmp::min(payload.len(), 4);
                Some(format!(
                    "{} -> {:19} port:{} flags:{:2x} payload:{}({:?})",
                    pkt.ts,
                    remote,
                    tcp.source,
                    tcp.flags,
                    payload.len(),
                    &payload[..n],
                ))
            }
            Packet::Tcp(pkt) => {
                let tcp = &pkt.tcp;
                let payload = &tcp.payload;
                let n = cmp::min(payload.len(), 4);
                Some(format!(
                    "{} ** src:{:5} dst:{:5} flags:{:2x} payload:{}({:?})",
                    pkt.ts,
                    tcp.source,
                    tcp.destination,
                    tcp.flags,
                    payload.len(),
                    &payload[..n],
                ))
            }
            Packet::Mqtt(pkt) if pkt.dir == pcap::Direction::In => {
                let remote = format!("{}:{}", pkt.ip_remote, pkt.src_port);
                Some(format!("{} <- {:19} {}", pkt.ts, remote, pkt.mqtt))
            }
            Packet::Mqtt(pkt) if pkt.dir == pcap::Direction::Out => {
                let remote = format!("{}:{}", pkt.ip_remote, pkt.dst_port);
                Some(format!("{} -> {:19} {}", pkt.ts, remote, pkt.mqtt))
            }
            Packet::Mqtt(pkt) => Some(format!("{} ** {}", pkt.ts, pkt.mqtt)),
            Packet::None => None,
            Packet::LoopBack(_) => None,
        }
    }
}

impl Packet {
    fn parse_l2(packet: pcap::Packet<'_>, codec: &Codec) -> Packet {
        use pnet::packet::{ethernet::EthernetPacket, FromPacket};

        let ts = NaiveDateTime::from_timestamp(
            packet.header.ts.tv_sec,
            u32::try_from(packet.header.ts.tv_usec).unwrap(),
        );

        match codec.link_type {
            pcap::Linktype::NULL => {
                let l3_typ = u32::from_ne_bytes(packet.data[..4].try_into().unwrap());
                let l3_typ = match l3_typ {
                    2 => pnet::packet::ethernet::EtherTypes::Ipv4,
                    _ => unreachable!(),
                };

                let pkt = LoopBack {
                    ts,
                    dir: pcap::Direction::InOut,
                    l3_typ,
                    payload: packet.data[4..].to_vec(),
                };

                Packet::LoopBack(pkt).map(codec)
            }
            pcap::Linktype::ETHERNET => match EthernetPacket::new(packet.data) {
                Some(ep) => {
                    let pkt = Ethernet {
                        ts,
                        dir: pcap::Direction::InOut,
                        eth: ep.from_packet(),
                    };
                    Packet::Ethernet(pkt).map(codec)
                }
                None => Packet::None,
            },
            _ => Packet::None,
        }
    }

    fn parse_l3(self, codec: &Codec) -> Packet {
        use pnet::packet::{ipv4::Ipv4Packet, FromPacket};

        let (ts, dir, l3_typ, payload) = match self {
            Packet::LoopBack(pkt) => (pkt.ts, pkt.dir, pkt.l3_typ, pkt.payload),
            Packet::Ethernet(pkt) => {
                (pkt.ts, pkt.dir, pkt.eth.ethertype, pkt.eth.payload)
            }
            Packet::None => return Packet::None,
            _ => unreachable!(),
        };

        match l3_typ {
            pnet::packet::ethernet::EtherTypes::Ipv4 => match Ipv4Packet::new(&payload) {
                Some(ip) => {
                    let pkt = Ipv4 { ts, dir, ip: ip.from_packet() };
                    Packet::Ipv4(pkt).map(codec)
                }
                None => Packet::None,
            },
            _ => Packet::None,
        }
    }

    fn parse_l4(self, codec: &Codec) -> Packet {
        use pnet::packet::ip::IpNextHeaderProtocols;
        use pnet::packet::{tcp::TcpPacket, FromPacket};

        let (ts, dir, l4_typ, ip_remote, payload) = match self {
            Packet::Ipv4(pkt) if pkt.dir == pcap::Direction::In => (
                pkt.ts,
                pkt.dir,
                pkt.ip.next_level_protocol,
                pkt.ip.source,
                pkt.ip.payload,
            ),
            Packet::Ipv4(pkt) if pkt.dir == pcap::Direction::Out => (
                pkt.ts,
                pkt.dir,
                pkt.ip.next_level_protocol,
                pkt.ip.destination,
                pkt.ip.payload,
            ),
            Packet::Ipv4(pkt) => (
                pkt.ts,
                pkt.dir,
                pkt.ip.next_level_protocol,
                pkt.ip.source, // loopback
                pkt.ip.payload,
            ),
            Packet::None => return Packet::None,
            _ => unreachable!(),
        };

        match l4_typ {
            IpNextHeaderProtocols::Tcp => match TcpPacket::new(&payload) {
                Some(tcpp) => {
                    let pkt = Tcp { ts, dir, ip_remote, tcp: tcpp.from_packet() };
                    Packet::Tcp(pkt).map(codec)
                }
                None => Packet::None,
            },
            _ => Packet::None,
        }
    }

    fn parse_mqtt(self, codec: &mut Codec) -> Packet {
        let (ts, dir, ip_remote, tcp) = match self {
            Packet::Tcp(pkt) => (pkt.ts, pkt.dir, pkt.ip_remote, pkt.tcp),
            Packet::None => return Packet::None,
            _ => unreachable!(),
        };

        let (key, dir) = if tcp.source < 2000 || codec.dump.port == tcp.source {
            let dir = match dir {
                pcap::Direction::InOut => pcap::Direction::Out,
                dir => dir,
            };
            ((ip_remote, tcp.destination), dir)
        } else if tcp.destination < 2000 || codec.dump.port == tcp.destination {
            let dir = match dir {
                pcap::Direction::InOut => pcap::Direction::In,
                dir => dir,
            };
            ((ip_remote, tcp.source), dir)
        } else {
            error!("unexpected");
            return Packet::None;
        };

        let conns = match dir {
            pcap::Direction::In => &mut codec.inp_conns,
            pcap::Direction::Out => &mut codec.out_conns,
            _ => unreachable!(),
        };

        let flags = tcp.flags;
        if (flags & 0x1) > 0 || (flags & 0x2) > 0 || (flags & 0x4) > 0 {
            if conns.remove(&key).is_some() {
                debug!("removing key:{:?} dir:{:?} flags:{:?}", key, dir, flags);
            }
        }
        // println!("dir:{:?} flags:{:x}", dir, flags);

        let mqtt_ok = match conns.get_mut(&key) {
            Some((buf, _, mqtt_ok)) => {
                buf.extend(&tcp.payload);
                *mqtt_ok
            }
            None if (flags & 0x2) > 0 => {
                let buf = tcp.payload.to_vec();
                let pktr = v5::MQTTRead::new(MAX_PACKET_SIZE);
                debug!("inserting key:{:?} dir:{:?}", key, dir);
                conns.insert(key, (buf, pktr, false));
                false
            }
            None => return Packet::None,
        };

        if dir == pcap::Direction::In && !mqtt_ok {
            let (buf, _, mqtt_ok) = conns.get_mut(&key).unwrap();
            let tri = check_connect(buf);
            match tri {
                Triplet::Maybe => return Packet::None,
                Triplet::True => {
                    *mqtt_ok = true;
                    let buf = Vec::default();
                    let pktr = v5::MQTTRead::new(MAX_PACKET_SIZE);
                    let key = (ip_remote, tcp.source);
                    debug!("inserting remote key:{:?} dir:{:?}", key, dir);
                    codec.out_conns.insert(key, (buf, pktr, true));
                }
                Triplet::False => {
                    if conns.remove(&key).is_some() {
                        debug!("removing (not mqtt) key:{:?} dir:{:?}", key, dir);
                    }
                    return Packet::None;
                }
            }
        }

        // println!("dir:{:?} flags:{:x}", dir, flags);

        match codec.read_packet(&key, dir) {
            Ok(Some(mqtt)) => {
                // println!("mqtt flags:{:x} dir:{:?}", flags, dir);
                let pkt = Mqtt {
                    ts,
                    dir,
                    ip_remote,
                    src_port: tcp.source,
                    dst_port: tcp.destination,
                    mqtt,
                };
                Packet::Mqtt(pkt).map(codec)
            }
            Ok(None) => {
                // println!("none flags:{:x} dir:{:?}", flags, dir);
                Packet::None
            }
            Err(err) => {
                println!("error reading packet: {}", err);
                Packet::None
            }
        }
    }

    fn map(self, codec: &Codec) -> Packet {
        match self {
            Packet::LoopBack(pkt) => Packet::LoopBack(pkt),
            Packet::Ethernet(Ethernet { ts, eth, .. }) => {
                if codec.mac == eth.source {
                    let pkt = Ethernet { ts, dir: pcap::Direction::Out, eth };
                    Packet::Ethernet(pkt)
                } else if codec.mac == eth.destination {
                    let pkt = Ethernet { ts, dir: pcap::Direction::In, eth };
                    Packet::Ethernet(pkt)
                } else {
                    Packet::None
                }
            }
            pkt => pkt,
        }
    }
}

struct LoopBack {
    ts: NaiveDateTime,
    dir: pcap::Direction,
    l3_typ: pnet::packet::ethernet::EtherType,
    payload: Vec<u8>,
}

struct Ethernet {
    ts: NaiveDateTime,
    dir: pcap::Direction,
    eth: pnet::packet::ethernet::Ethernet,
}

struct Ipv4 {
    ts: NaiveDateTime,
    dir: pcap::Direction,
    ip: pnet::packet::ipv4::Ipv4,
}

struct Tcp {
    ts: NaiveDateTime,
    dir: pcap::Direction,
    ip_remote: net::Ipv4Addr,
    tcp: pnet::packet::tcp::Tcp,
}

struct Mqtt {
    ts: NaiveDateTime,
    dir: pcap::Direction,
    ip_remote: net::Ipv4Addr,
    src_port: u16,
    dst_port: u16,
    mqtt: v5::Packet,
}

impl Dump {
    fn device_mac(&self) -> Result<pnet::util::MacAddr> {
        let device_name = self.device.clone().unwrap();
        match netw::interfaces().into_iter().filter(|inf| inf.name == device_name).next()
        {
            Some(inf) => match inf.mac {
                Some(mac) => Ok(mac.to_string().parse::<pnet::util::MacAddr>().unwrap()),
                None => Err(format!("cannot find mac for device {:?}", device_name)),
            },
            None => Err(format!("cannot find mac for device {:?}", device_name)),
        }
    }
}

fn check_connect(buf: &[u8]) -> Triplet {
    use mymq::Packetize;

    if buf.len() < 7 {
        Triplet::Maybe
    } else {
        match mymq::VarU32::decode(&buf[1..7]).ok() {
            Some((_, n)) if buf.len() >= (1 + n + 6) => {
                if MQTT_SIGNATURE == &buf[1 + n..1 + n + 6] {
                    Triplet::True
                } else {
                    Triplet::False
                }
            }
            Some((_, _)) => Triplet::Maybe,
            None => Triplet::False,
        }
    }
}

#[derive(Debug)]
enum Triplet {
    True,
    False,
    Maybe,
}