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
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use std::mem;
use std::slice;
use libc::c_void;
#[cfg(feature = "T2_PRI_HDRDESC")]
use libc::c_char;
use nethdr::{EthernetHeader, Ip4Header, Ip6Header, UdpHeader, TcpHeader, IcmpHeader, L3Type, L4Type, T2IpAddr};

/// Pcap packet header
#[repr(C, packed)]
struct PacketHeader {
    tv_sec: u32,
    tv_usec: u32,
    snap_len: u32,
    orig_len: u32,
}

/// Represents a packet with its different headers and associated lengths.
#[repr(C)]
pub struct Packet {
    #[cfg(feature = "FLOW_LIFETIME")]
    findex: u64,

    /// Per packet status bits.
    pub status: u64,

    pcap_pkthdr: *const PacketHeader,

    raw_packet: *const u8,
    end_packet: *const u8,

    l2_header: *const u8,
    l3_header: *const u8,
    l4_header: *const u8,
    l7_header: *const u8,

    ether_llc: *const c_void,
    ppp_hdr: *const c_void,
    pppoe_hdr: *const c_void,
    mpls: *const u32,
    vlans: *const u32,

    gre_header: *const c_void,
    gre_l3_hdr: *const u8,

    gtp_hdr: *const u8,

    l2tp_hdr: *const u16,
    l2tp_l3_hdr: *const u8,

    ip6_hh_opt_hdr: *const c_void,
    ip6_d_opt_hdr: *const c_void,
    ip6_frag_hdr: *const c_void,
    ip6_route_hdr: *const c_void,

    trdo_oi_hdr: *const u8,
    trdo_a_hdr: *const u8,

    /// Raw pointer to SCTP header. Only present if `SCTP_ACTIVATE = 1`.
    #[cfg(feature = "SCTP_ACTIVATE")]
    pub l7_sctp_hdr: *const u8,

    /// On wire full packet length (from the per-packet PCAP header).
    pub packet_raw_len: u32,
    /// On wire packet length starting from layer2.
    pub packet_l2_len: u32,
    /// Packet length depending on Tranalyzer2 `PACKETLENGTH` value, see `networkHeaders.h` for details.
    pub packet_len: u32,

    /// Packet snapped length
    pub snap_len: u32,
    /// Packet snapped length starting from layer 2.
    pub snap_l2_len: u32,
    /// Packet snapped length starting from layer 3.
    pub snap_l3_len: u32,
    /// Packet snapped length starting from layer 4.
    pub snap_l4_len: u16,
    /// Packet snapped length starting from layer 7.
    pub snap_l7_len: u16,

    /// Packet payload length: layer 7 length.
    pub packet_l7_len: u16,

    /// SCTP L7 snapped length. Only present if `SCTP_ACTIVATE = 1`.
    #[cfg(feature = "SCTP_ACTIVATE")]
    pub snap_sctp_l7_len: u16,

    /// Length of the layer 2 header (Ethernet, ...).
    pub l2_hdr_len: u16,
    /// Length of the layer 3 header (IPv4, IPv6, ...).
    pub l3_hdr_len: u16,
    /// Length of the layer 4 header (TCP, UDP, ICMP, ...).
    pub l4_hdr_len: u16,

    #[cfg(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE"))]
    src_ip: T2IpAddr,
    #[cfg(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE"))]
    dst_ip: T2IpAddr,
    #[cfg(not(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE")))]
    src_ip: [u8; 4],
    #[cfg(not(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE")))]
    dst_ip: [u8; 4],

    #[cfg(all(feature = "FLOW_AGGREGATION", any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE")))]
    src_ip_c: T2IpAddr,
    #[cfg(all(feature = "FLOW_AGGREGATION", any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE")))]
    dst_ip_c: T2IpAddr,
    #[cfg(all(feature = "FLOW_AGGREGATION", not(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE"))))]
    src_ip_c: T2IpAddr,
    #[cfg(all(feature = "FLOW_AGGREGATION", not(any(feature = "IPV6_ACTIVATE", feature = "IPV6_DUALMODE"))))]
    dst_ip_c: T2IpAddr,

    #[cfg(feature = "FLOW_AGGREGATION")]
    subnet_num_src: u32,
    #[cfg(feature = "FLOW_AGGREGATION")]
    subnet_num_dst: u32,
    #[cfg(feature = "FLOW_AGGREGATION")]
    src_port_c: u16,
    #[cfg(feature = "FLOW_AGGREGATION")]
    dst_port_c: u16,
    #[cfg(feature = "FLOW_AGGREGATION")]
    l4_type_c: u8,

    /// Inner VLAN ID
    pub inner_vlan: u16,
    /// Source port in host order.
    pub src_port: u16,
    /// Destination port in host order.
    pub dst_port: u16,
    /// Type of the layer 2 header.
    pub l2_type: u16,
    /// Outer type of the layer 2 header.
    pub outer_l2_type: u16,
    /// Type of the layer 3 header as defined in [`L3Type`].
    l3_type: u16,

    #[cfg(feature = "T2_PRI_HDRDESC")]
    num_hdr_desc: u16,
    #[cfg(feature = "T2_PRI_HDRDESC")]
    hdr_desc_pos: u16,
    #[cfg(feature = "T2_PRI_HDRDESC")]
    hdr_desc: [c_char; 128],

    /// Type of the layer 4 header as defined in [`L4Type`].
    l4_type: u8,
    mpls_hdr_count: u8,
    vlan_hdr_count: u8,

    #[cfg(feature = "SCTP_ACTIVATE")]
    sctp_pad: u8,
}

impl Packet {
    /// Timestamp of when the packet was captured (as the number of seconds since 1970-01-01).
    pub fn timestamp(&self) -> f64 {
        unsafe {
            let ref hdr = *self.pcap_pkthdr;
            hdr.tv_sec as f64 + (hdr.tv_usec as f64 / 1000000.0)
        }
    }

    /// Returns an [`EthernetHeader`] if the packet contains an Ethernet header and is long enough.
    /// Returns `None` otherwise.
    pub fn ethernethdr(&self) -> Option<&EthernetHeader> {
        // assumes that all traffic is ethernet
        let size = mem::size_of::<EthernetHeader>();
        if self.snap_l2_len as usize >= size && self.l2_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l2_header as *const EthernetHeader))
            }
        } else {
            None
        }
    }

    /// Returns an [`Ip4Header`] if the packet contains an IPv4 header and is long enough. Returns
    /// `None` otherwise.
    pub fn ip4hdr(&self) -> Option<&Ip4Header> {
        let size = mem::size_of::<Ip4Header>();
        if self.l3_type() == L3Type::IPv4 && self.snap_l3_len as usize >= size && 
                self.l3_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l3_header as *const Ip4Header))
            }
        } else {
            None
        }
    }

    /// Returns an [`Ip4Header`] if the packet contains an IPv6 header and is long enough. Returns
    /// `None` otherwise.
    pub fn ip6hdr(&self) -> Option<&Ip6Header> {
        let size = mem::size_of::<Ip6Header>();
        if self.l3_type() == L3Type::IPv6 && self.snap_l3_len as usize >= size &&
                self.l3_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l3_header as *const Ip6Header))
            }
        } else {
            None
        }
    }

    /// Returns a [`TcpHeader`] if the packet contains a TCP header and is long enough. Returns
    /// `None` otherwise.
    pub fn tcphdr(&self) -> Option<&TcpHeader> {
        let size = mem::size_of::<TcpHeader>();
        if self.l4_type() == L4Type::TCP && self.snap_l4_len as usize >= size &&
                self.l4_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l4_header as *const TcpHeader))
            }
        } else {
            None
        }
    }

    /// Returns a [`UdpHeader`] if the packet contains a UDP header and is long enough. Returns
    /// `None` otherwise.
    pub fn udphdr(&self) -> Option<&UdpHeader> {
        let size = mem::size_of::<UdpHeader>();

        if self.l4_type() == L4Type::UDP && self.snap_l4_len as usize >= size &&
                self.l4_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l4_header as *const UdpHeader))
            }
        } else {
            None
        }
    }

    /// Returns an [`IcmpHeader`] if the packet contains an ICMP header and is long enough. Returns
    /// `None` otherwise.
    pub fn icmphdr(&self) -> Option<&IcmpHeader> {
        let size = mem::size_of::<IcmpHeader>();
        if self.l4_type() == L4Type::ICMP && self.snap_l4_len as usize >= size &&
                self.l4_header != 0 as *const u8 {
            unsafe {
                Some(&*(self.l4_header as *const IcmpHeader))
            }
        } else {
            None
        }
    }

    /// Returns the layer 7 as a slice of bytes.
    ///
    /// This is how the layer 7 is typically accessed in content processing plugins.
    pub fn l7_header(&self) -> &[u8] {
        if self.snap_l7_len == 0 || self.l7_header == 0 as *const u8 {
            return &[];
        }
        unsafe {
            slice::from_raw_parts(self.l7_header, self.snap_l7_len as usize)
        }
    }

    /// Returns the layer 2 header as a slice of bytes.
    pub fn raw_l2_header(&self) -> &[u8] {
        let ptr = self.l2_header as *const u8;
        if self.snap_l2_len == 0 || ptr == 0 as *const u8 {
            return &[];
        }
        unsafe {
            slice::from_raw_parts(ptr, (self.snap_l2_len - self.snap_l3_len) as usize)
        }
    }

    /// Returns the layer 3 header as a slice of bytes.
    ///
    /// This function can be used to access the IP options as a Rust inferface is not yet
    /// implemented for IPv4 options and IPv6 extension headers.
    pub fn raw_l3_header(&self) -> &[u8] {
        let ptr = self.l3_header as *const u8;
        if self.snap_l3_len == 0 || ptr == 0 as *const u8 {
            return &[];
        }
        unsafe {
            slice::from_raw_parts(ptr, (self.snap_l3_len - (self.snap_l4_len as u32)) as usize)
        }
    }

    /// Returns the layer 4 header as a slice of bytes.
    ///
    /// This function can be used to access TCP options as a Rust interface is not yet implemented.
    pub fn raw_l4_header(&self) -> &[u8] {
        let ptr = self.l4_header as *const u8;
        if self.snap_l4_len == 0 || ptr == 0 as *const u8 {
            return &[];
        }
        unsafe {
            slice::from_raw_parts(ptr, (self.snap_l4_len - self.snap_l7_len) as usize)
        }
    }

    /// Type of the layer 3 header as defined in [`L3Type`].
    pub fn l3_type(&self) -> L3Type {
        L3Type::from_u16(self.l3_type)
    }

    /// Type of the layer 4 header as defined in [`L4Type`].
    pub fn l4_type(&self) -> L4Type {
        L4Type::from_u8(self.l4_type)
    }
}