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
use byteorder::{ByteOrder, BigEndian, LittleEndian, NativeEndian};

use crate::{
    Endianness,
    errors::*,
    pcap::PcapHeader,
    pcap::{Packet, PacketHeader}
};

use std::{
    borrow::Cow,
    io::Write
};


/// This struct wraps another writer and uses it to write a Pcap formated stream.
///
/// # Examples
///
/// ```rust,no_run
/// use std::fs::File;
/// use pcap_file::pcap::{PcapReader, PcapWriter};
///
///
/// let file_in = File::open("test.pcap").expect("Error opening file");
/// let pcap_reader = PcapReader::new(file_in).unwrap();
///
/// let file_out = File::create("out.pcap").expect("Error creating file out");
/// let mut pcap_writer = PcapWriter::new(file_out).expect("Error writing file");
///
/// // Read test.pcap
/// for pcap in pcap_reader {
///
///     //Check if there is no error
///     let pcap = pcap.unwrap();
///
///     //Write each packet of test.pcap in out.pcap
///     pcap_writer.write_packet(&pcap).unwrap();
/// }
/// ```
#[derive(Debug)]
pub struct PcapWriter<W: Write> {
    pub header: PcapHeader,
    writer: W
}


impl<W: Write> PcapWriter<W> {

    /// Creates a new `PcapWriter` from an existing writer in the choosen endianess.
    /// Defaults to the native endianness of the CPU.
    ///
    /// Automatically writes this default global pcap header to the file:
    ///
    /// ```rust, ignore
    /// PcapHeader {
    ///
    ///     version_major : 2,
    ///     version_minor : 4,
    ///     ts_correction : 0,
    ///     ts_accuracy : 0,
    ///     snaplen : 65535,
    ///     datalink : DataLink::ETHERNET
    /// };
    /// ```
    ///
    /// # Errors
    ///
    /// Return an error if the writer can't be written to.
    ///
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use std::fs::File;
    /// use pcap_file::pcap::PcapWriter;
    ///
    /// let file_out = File::create("out.pcap").expect("Error creating file");
    /// let mut pcap_writer = PcapWriter::new(file_out);
    /// ```
    pub fn new(writer: W) -> ResultParsing<PcapWriter<W>> {

        let tmp = NativeEndian::read_u16(&[0x42, 0x00]);

        let endianness = match tmp {
            0x4200 => Endianness::Big,
            0x0042 => Endianness::Little,
            _ => unreachable!()
        };

        let mut header = PcapHeader::default();
        header.set_endianness(endianness);
        PcapWriter::with_header(header, writer)
    }

    /// Create a new `PcapWriter` from an existing writer with a user defined pcap header.
    ///
    /// The endianness and the timestamp resolution are defined by the magic number of the header.
    /// It is possible to change them with 'set_endianess()' and 'set_ts_resolution()'
    ///
    /// It Automatically writes the pcap header to the file.
    ///
    /// # Errors
    ///
    /// Return an error if the writer can't be written to.
    ///
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use std::fs::File;
    /// use pcap_file::{
    ///     DataLink,
    ///     pcap::{PcapHeader, PcapWriter},
    /// };
    ///
    /// let file = File::create("out.pcap").expect("Error creating file");
    ///
    /// let header = PcapHeader {
    ///
    ///     magic_number : 0xa1b2c3d4,
    ///     version_major : 2,
    ///     version_minor : 4,
    ///     ts_correction : 0,
    ///     ts_accuracy : 0,
    ///     snaplen : 65535,
    ///     datalink : DataLink::ETHERNET
    /// };
    ///
    /// let mut pcap_writer = PcapWriter::with_header(header, file);
    /// ```
    pub fn with_header(header: PcapHeader, mut writer: W) -> ResultParsing<PcapWriter<W>> {

        match header.endianness() {
            Endianness::Big => header.write_to::<_, BigEndian>(&mut writer)?,
            Endianness::Little => header.write_to::<_, LittleEndian>(&mut writer)?,
        }

        Ok(
            PcapWriter {
                header,
                writer
            }
        )
    }

    /// Consumes the `PcapWriter`, returning the wrapped writer.
    pub fn into_writer(self) -> W {
        self.writer
    }

    /// Gets a reference to the underlying writer.
    pub fn get_ref(&self) -> &W {
        &self.writer
    }

    /// Gets a mutable reference to the underlying writer.
    ///
    /// It is inadvisable to directly write to the underlying writer.
    pub fn get_mut(&mut self) -> &mut W {
        &mut self.writer
    }

    /// Writes some raw data, converting it to the pcap file format.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use std::fs::File;
    /// use pcap_file::pcap::PcapWriter;
    ///
    /// let data = [0u8; 10];
    /// let file = File::create("out.pcap").expect("Error creating file");
    /// let mut pcap_writer = PcapWriter::new(file).unwrap();
    ///
    /// pcap_writer.write(1, 0, &data, data.len() as u32).unwrap();
    /// ```
    pub fn write(&mut self, ts_sec: u32, ts_nsec: u32, data: &[u8], orig_len: u32) -> ResultParsing<()> {

        let packet = Packet {

            header: PacketHeader::new(ts_sec, ts_nsec, data.len() as u32, orig_len),
            data: Cow::Borrowed(data)
        };

        self.write_packet(&packet)
    }

    /// Writes a `Packet`.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use std::fs::File;
    /// use pcap_file::pcap::{
    ///     Packet,
    ///     PcapWriter
    /// };
    ///
    /// let data = [0u8; 10];
    /// let packet = Packet::new(1, 0, &data, data.len() as u32);
    ///
    /// let file = File::create("out.pcap").expect("Error creating file");
    /// let mut pcap_writer = PcapWriter::new(file).unwrap();
    ///
    /// pcap_writer.write_packet(&packet).unwrap();
    /// ```
    pub fn write_packet(&mut self, packet: &Packet) -> ResultParsing<()> {

        let ts_resolution = self.header.ts_resolution();

        match self.header.endianness() {

            Endianness::Big => packet.header.write_to::<_, BigEndian>(&mut self.writer, ts_resolution)?,
            Endianness::Little => packet.header.write_to::<_, LittleEndian>(&mut self.writer, ts_resolution)?,
        }

        self.writer.write_all(&packet.data)?;

        Ok(())
    }
}