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
use crate::writer::Encodable;
use crate::{enums::BlockType, utils::pad_to_32};
use byteorder::{ByteOrder, WriteBytesExt};
use std::io::Write;
use std::{convert::TryInto, io};

/*
    Based on the draft standard:

           PCAP Next Generation (pcapng) Capture File Format
                     draft-tuexen-opsawg-pcapng-02

    https://tools.ietf.org/html/draft-tuexen-opsawg-pcapng-02#section-4.2
*/

/*
        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                          Block Type                           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      Block Total Length                       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       /                          Block Body                           /
       /              variable length, padded to 32 bits               /
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      Block Total Length                       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                     Figure 1: Basic block structure.
*/

trait Block {
    const TYPE: BlockType;

    fn length(&self) -> u32;

    fn padding(&self) -> Vec<u8> {
        let n = pad_to_32(self.length().try_into().unwrap());
        vec![0u8; n]
    }
}

/// A raw pcapng block.
#[derive(Debug)]
pub struct RawBlock<'a> {
    block_type: u32,
    total_length1: u32,
    total_length2: u32,
    body: &'a [u8],
}

impl<'a> RawBlock<'a> {
    pub fn new(block_type: u32, total_length1: u32, total_length2: u32, body: &'a [u8]) -> Self {
        Self {
            block_type,
            total_length1,
            total_length2,
            body,
        }
    }
}

impl<'a, W: Write> Encodable<W> for RawBlock<'a> {
    /// For raw blocks, the total length fields are not automatically
    /// calculated.
    fn encode<B: ByteOrder>(&self, w: &mut W) -> io::Result<()> {
        w.write_u32::<B>(self.block_type)?;
        w.write_u32::<B>(self.total_length1)?;
        w.write_all(self.body)?;
        w.write_u32::<B>(self.total_length2)?;
        Ok(())
    }
}

mod epb;
mod idb;
mod isb;
pub mod options;
mod shb;
mod spb;

pub use crate::blocks::epb::EnhancedPacketBlock;
pub use crate::blocks::idb::InterfaceDescriptionBlock;
pub use crate::blocks::isb::InterfaceStatisticsBlock;
pub use crate::blocks::shb::SectionHeaderBlock;
pub use crate::blocks::spb::SimplePacketBlock;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::blocks::options::*;
    use crate::blocks::EnhancedPacketBlock;
    use byteorder::{BigEndian, LittleEndian};

    #[test]
    fn new_raw_be() {
        let opts = Options::new();
        let epb = EnhancedPacketBlock::new(1, 1, 2, 10, 20, &[9; 10], &opts);
        let mut epb_buf = vec![];
        epb.encode::<BigEndian>(&mut epb_buf).unwrap();
        let raw = RawBlock::new(
            6,
            44,
            44,
            &[
                0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 20, 9, 9, 9, 9, 9, 9, 9,
                9, 9, 9, 0, 0,
            ],
        );
        let mut raw_buf = vec![];
        raw.encode::<BigEndian>(&mut raw_buf).unwrap();
        assert_eq!(epb_buf, raw_buf);
    }

    #[test]
    fn new_raw_le() {
        let opts = Options::new();
        let epb = EnhancedPacketBlock::new(1, 1, 2, 10, 20, &[9; 10], &opts);
        let mut epb_buf = vec![];
        epb.encode::<LittleEndian>(&mut epb_buf).unwrap();
        let raw = RawBlock::new(
            6,
            44,
            44,
            &[
                1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 9, 9, 9, 9, 9, 9, 9,
                9, 9, 9, 0, 0,
            ],
        );
        let mut raw_buf = vec![];
        raw.encode::<LittleEndian>(&mut raw_buf).unwrap();
        assert_eq!(epb_buf, raw_buf);
    }
}