cross_socket/packet/
ipv6.rs

1use super::ip::IpNextLevelProtocol;
2use pnet::packet::Packet;
3use std::net::Ipv6Addr;
4
5/// IPv6 Header Length
6pub const IPV6_HEADER_LEN: usize = pnet::packet::ipv6::MutableIpv6Packet::minimum_packet_size();
7
8/// Represents the IPv6 options.
9#[derive(Clone, Debug, PartialEq)]
10pub struct Ipv6Packet {
11    /// IP Version
12    pub version: u8,
13    /// Traffic Class
14    pub traffic_class: u8,
15    /// Flow Label
16    pub flow_label: u32,
17    /// Payload Length
18    pub payload_length: u16,
19    /// Next level protocol
20    pub next_protocol: IpNextLevelProtocol,
21    /// Hop Limit
22    pub hop_limit: u8,
23    /// Source IPv6 Address
24    pub source: Ipv6Addr,
25    /// Destination IPv6 Address
26    pub destination: Ipv6Addr,
27    /// Payload. Next level protocol packet.
28    pub payload: Vec<u8>,
29}
30
31impl Ipv6Packet {
32    pub(crate) fn from_pnet_packet(packet: &pnet::packet::ipv6::Ipv6Packet) -> Ipv6Packet {
33        Ipv6Packet {
34            version: packet.get_version(),
35            traffic_class: packet.get_traffic_class(),
36            flow_label: packet.get_flow_label(),
37            payload_length: packet.get_payload_length(),
38            next_protocol: IpNextLevelProtocol::from_u8(packet.get_next_header().0),
39            hop_limit: packet.get_hop_limit(),
40            source: packet.get_source(),
41            destination: packet.get_destination(),
42            payload: packet.payload().to_vec(),
43        }
44    }
45    /// Constructs a new Ipv6Packet from bytes
46    pub fn from_bytes(packet: &[u8]) -> Ipv6Packet {
47        let ipv6_packet = pnet::packet::ipv6::Ipv6Packet::new(packet).unwrap();
48        Ipv6Packet::from_pnet_packet(&ipv6_packet)
49    }
50}
51
52pub(crate) fn build_ipv6_packet(
53    ipv6_packet: &mut pnet::packet::ipv6::MutableIpv6Packet,
54    src_ip: Ipv6Addr,
55    dst_ip: Ipv6Addr,
56    next_protocol: IpNextLevelProtocol,
57) {
58    ipv6_packet.set_source(src_ip);
59    ipv6_packet.set_destination(dst_ip);
60    ipv6_packet.set_version(6);
61    ipv6_packet.set_hop_limit(64);
62    match next_protocol {
63        IpNextLevelProtocol::Tcp => {
64            ipv6_packet.set_next_header(pnet::packet::ip::IpNextHeaderProtocols::Tcp);
65            ipv6_packet.set_payload_length(32);
66        }
67        IpNextLevelProtocol::Udp => {
68            ipv6_packet.set_next_header(pnet::packet::ip::IpNextHeaderProtocols::Udp);
69            ipv6_packet.set_payload_length(8);
70        }
71        IpNextLevelProtocol::Icmpv6 => {
72            ipv6_packet.set_next_header(pnet::packet::ip::IpNextHeaderProtocols::Icmpv6);
73            ipv6_packet.set_payload_length(8);
74        }
75        _ => {}
76    }
77}
78
79/// IPv6 Packet Builder
80#[derive(Clone, Debug)]
81pub struct Ipv6PacketBuilder {
82    /// Source IPv6 address
83    pub src_ip: Ipv6Addr,
84    /// Destination IPv6 address
85    pub dst_ip: Ipv6Addr,
86    /// Next level protocol
87    pub next_protocol: IpNextLevelProtocol,
88    /// Payload Length
89    pub payload_length: Option<u16>,
90    /// Hop Limit
91    pub hop_limit: Option<u8>,
92}
93
94impl Ipv6PacketBuilder {
95    /// Constructs a new Ipv6PacketBuilder
96    pub fn new(src_ip: Ipv6Addr, dst_ip: Ipv6Addr, next_protocol: IpNextLevelProtocol) -> Self {
97        Ipv6PacketBuilder {
98            src_ip,
99            dst_ip,
100            next_protocol,
101            payload_length: None,
102            hop_limit: None,
103        }
104    }
105    /// Buid IPv6 packet and return bytes
106    pub fn build(&self) -> Vec<u8> {
107        let mut buffer: Vec<u8> = vec![0; IPV6_HEADER_LEN];
108        let mut ipv6_packet = pnet::packet::ipv6::MutableIpv6Packet::new(&mut buffer).unwrap();
109        build_ipv6_packet(
110            &mut ipv6_packet,
111            self.src_ip,
112            self.dst_ip,
113            self.next_protocol,
114        );
115        if let Some(payload_length) = self.payload_length {
116            ipv6_packet.set_payload_length(payload_length);
117        }
118        if let Some(hop_limit) = self.hop_limit {
119            ipv6_packet.set_hop_limit(hop_limit);
120        }
121        ipv6_packet.packet().to_vec()
122    }
123}