aboutsummaryrefslogtreecommitdiffstats
path: root/src/ip_packet.rs
blob: 3de6a6005890d1cd2810a8d5d243a7dd5fe271ae (plain) (blame)
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
use pnet_packet::ipv4::Ipv4Packet;
use pnet_packet::ipv6::Ipv6Packet;
use std::net::IpAddr;

pub enum IpPacket<'a> {
    V4(Ipv4Packet<'a>),
    V6(Ipv6Packet<'a>),
}

impl<'a> IpPacket<'a> {
    pub fn new(packet: &'a [u8]) -> Option<Self> {
        if packet.is_empty() {
            return None;
        }

        match packet[0] >> 4 {
            4 => Ipv4Packet::new(packet).map(IpPacket::V4),
            6 => Ipv6Packet::new(packet).map(IpPacket::V6),
            _ => None
        }
    }

    pub fn source(&self) -> IpAddr {
        match *self {
            IpPacket::V4(ref packet) => packet.get_source().into(),
            IpPacket::V6(ref packet) => packet.get_source().into(),
        }
    }

    pub fn destination(&self) -> IpAddr {
        match *self {
            IpPacket::V4(ref packet) => packet.get_destination().into(),
            IpPacket::V6(ref packet) => packet.get_destination().into(),
        }
    }

    pub fn length(&self) -> u16 {
        match *self {
            IpPacket::V4(ref packet) => packet.get_total_length(),
            IpPacket::V6(ref packet) => 40 + packet.get_payload_length(),
        }

    }
}