blob: 013fac13946990166ebb5d5cacb5f4cc337f96a1 (
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
45
|
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
*/
use rips_packets::ipv4::Ipv4Packet;
use rips_packets::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> {
match packet.get(0).map(|byte| *byte >> 4) {
Some(4) => Ipv4Packet::new(packet).map(IpPacket::V4),
Some(6) => Ipv6Packet::new(packet).map(IpPacket::V6),
_ => None
}
}
pub fn source(&self) -> IpAddr {
match *self {
IpPacket::V4(ref packet) => packet.source().into(),
IpPacket::V6(ref packet) => packet.source().into(),
}
}
pub fn destination(&self) -> IpAddr {
match *self {
IpPacket::V4(ref packet) => packet.destination().into(),
IpPacket::V6(ref packet) => packet.destination().into(),
}
}
pub fn length(&self) -> u16 {
match *self {
IpPacket::V4(ref packet) => packet.total_length(),
IpPacket::V6(ref packet) => 40 + packet.payload_length(),
}
}
}
|