cross_socket/packet/
ipv6.rs1use super::ip::IpNextLevelProtocol;
2use pnet::packet::Packet;
3use std::net::Ipv6Addr;
4
5pub const IPV6_HEADER_LEN: usize = pnet::packet::ipv6::MutableIpv6Packet::minimum_packet_size();
7
8#[derive(Clone, Debug, PartialEq)]
10pub struct Ipv6Packet {
11 pub version: u8,
13 pub traffic_class: u8,
15 pub flow_label: u32,
17 pub payload_length: u16,
19 pub next_protocol: IpNextLevelProtocol,
21 pub hop_limit: u8,
23 pub source: Ipv6Addr,
25 pub destination: Ipv6Addr,
27 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 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#[derive(Clone, Debug)]
81pub struct Ipv6PacketBuilder {
82 pub src_ip: Ipv6Addr,
84 pub dst_ip: Ipv6Addr,
86 pub next_protocol: IpNextLevelProtocol,
88 pub payload_length: Option<u16>,
90 pub hop_limit: Option<u8>,
92}
93
94impl Ipv6PacketBuilder {
95 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 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}